有关构建更高阶Quine程序的想法吗?

时间:2009-08-28 15:02:36

标签: algorithm quine

这是一个特殊的Haskell程序,它输出一个Python程序,输出一个Ruby程序,输出原始的Haskell程序(来自http://blog.sigfpe.com/2008/02/third-order-quine-in-three-languages.html

更准确地说,输出是这个Haskell程序的

q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"

是一个Python程序,

$ runhaskell test.hs
def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'
q("def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'","def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']")

在运行后输出Ruby程序,

$ runhaskell test.hs | python
def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end
q("def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']","def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'")

最后Ruby程序打印出原始的Haskell程序。

$ runhaskell test.hs | python | ruby
q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"

由于传统的quine程序可以通过将程序分成两部分来构建,其中partA包含partB的描述,而partB从描述中计算A.

但这样的三阶quine是如何构建的呢?

4 个答案:

答案 0 :(得分:2)

首先,围绕this programming assignment缠绕。相信我,一旦你花了一些时间,它实际上并不困难。我们的想法是,您可以编写一个程序,可以将另一个程序作为输入,并将第三个程序作为输出结合,将两个程序结合起来并理解其自己的文本。这是一种更高阶的quine。如果您了解所有三种编程语言的结构,您可以从这个分配中获取想法并进一步扩展它们。

答案 1 :(得分:2)

从理论上讲,{p> Kleene's recursion theorem可以用几乎任何语言构建一个quine。 (More information here.)虽然我自己还没有设法让它发挥作用。

对于更高阶的quine,要考虑的功能是语言评估机制的组成。如果你可以从KRT获得一个基本的quine,也许你可以从中获得更高阶的quine。

答案 2 :(得分:1)

在那篇文章的第一段中,我写了一个简短的解释。我建议从那里开始。

我从Barwise和Moss的Vicious Circles一书中学到了一些这些技巧。

答案 3 :(得分:0)

我不是程序员()但对我来说听起来像这样:

... - > C - > A - > B - > C-> A-> B - > C - > ...

一个(三角形)圆圈,没有真正的开始或结束。

程序A共同描述B,其中包含C的描述。

程序B包含对C的描述,其中包含A的描述。

程序C包含对A的描述,其中包含对B的描述。

可能会更深入,你可以使用许多不同的语言在圆圈上获得更多的角落。