# 图灵的停机问题背后令人着迷的数学（哲学）原理

## 1. 停机问题（The Halting Proble）

``````bool God_algo(char* program, char* input)

{

if(<program> halts on <input>)

return true;

return false;

}
``````

emmm......这里我们假设 if 的判断语句里面是你天才思考的结晶，它能够像上帝一样洞察一切程序的宿命 - -！

``````bool Satan_algo(char* program)

{

if( God_algo(program, program) ){

while(1); // loop forever!

return false; // can never get here!

}

else

return true;

}
``````

``````Satan_algo(Satan_algo);
``````

Satan_algo(Satan_algo)能够停机 => 它不能停机

Satan_algo(Satan_algo)不能停机 => 它能够停机

## 2. Y Combinator 与 Lambda Calculus

``````<expr> ::= <identifier>

<expr> ::= lambda <identifier-list>. <expr>

<expr> ::= (<expr> <expr>)
``````

``````lambda x y. x + y
``````

``````((lambda x y. x + y) 2 3)
``````

``````let Add = (lambda x y. x + y)
``````

``````(Add 2 3)
``````

Alpha转换公理：例如，“lambda x y. x + y”转换为“lambda a b. a + b”。换句话说，函数的参数起什么名字没有关系，可以随意替换，只要函数体里面对参数的使用的地方也同时注意相应替换掉就是了。

Beta转换公理：例如，“(lambda x y. x + y) 2 3”转换为“2 + 3”。这个就更简单了，也就是说，当把一个lambda函数用到参数身上时，只需用实际的参数来替换掉其函数体中的相应变量即可。

``````f(n):

if n == 0 return 1

return n*f(n-1)
``````

``````lambda n. If_Else n==0 1 n*<self>(n-1)
``````

``````let F = lambda n. IF_Else n==0 1 n*F(n-1)
``````

``````lambda n. If_Else n==0 1 n*<self>(n-1)
``````

``````lambda self n. If_Else n==0 1 n*self(n-1)
``````

``````let P = lambda self n. If_Else n==0 1 n*self(n-1)
``````

``````P(P, 3)
``````

``````IF_Else n==0 1 n*P(n-1)
``````

``````let P = lambda self n. If_Else n==0 1 n*self(self, n-1)
``````

``````IF_Else 3==0 1 3*P(P, 3-1)
``````

`P(P, 3-1)`是对P合法的递归调用。这次我们真的成功了！

``````let P = lambda self n. If_Else n==0 1 n*self(n-1)
``````

``````power(n):

if(n==0) return 1;

return n*power(n-1);
``````

``````P(power, 3)
``````

``````IF_Else 3==0 1 3*power(3-1)
``````

``````P(power)
``````

``````IF_Else n==0 1 n*power(n-1)
``````

``````IF_Else n==0 1 n*power(n-1)
``````

``````lambda n. IF_Else n==0 1 n*power(n-1)
``````

``````let power = lambda n. IF_Else n==0 1 n*power(n-1)
``````

``````P(power) = power
``````

``````let P = lambda self n. If_Else n==0 1 n*self(n-1) // 注意，不是self(self,n-1)
``````

``````Y(F) = f
``````

``````Y(F) = f = F(f) = F(Y(F))
``````

``````Y(F) = F(Y(F))
``````

``````let P = lambda self n. If_Else n==0 1 n*self(n-1)
``````

``````power = P(power) // 不动点原理
``````

``````let power_gen = lambda self. P(self(self))
``````

``````power_gen(power_gen)
``````

``````P(power_gen(power_gen))
``````

``````power_gen(power_gen) => P(power_gen(power_gen))
``````

``````power => P(power)
``````

OK，我们总结一下：对于给定的P，只要构造出一个相应的power_gen如下：

``````let power_gen = lambda self. P(self(self))
``````

``````let Y = lambda F.

let f_gen = lambda self. F(self(self))

return f_gen(f_gen)
``````

``````let Pwr = lambda self n. If_Else n==0 1 n*self(n-1)
``````

``````Y(Pwr) =>

let f_gen = lambda self. Pwr(self(self))

return f_gen(f_gen)
``````

Y(Pwr)的求值结果就是里面返回的那个f_gen(f_gen)，我们再根据f_gen的定义展开f_gen(f_gen)，得到：

``````Pwr(f_gen(f_gen))
``````

``````Y(Pwr) => f_gen(f_gen) => Pwr(f_gen(f_gen))
``````

``````Pwr(f_gen(f_gen)) => If_Else n==0 1 n*f_gen(f_gen) (n-1)
``````

``````Pwr(f_gen(f_gen)) => If_Else n==0 1 n* Pwr(f_gen(f_gen)) (n-1)
``````

``````f => If_Else n==0 1 n*f(n-1)
``````

## 3. 哥德尔的不完备性定理

``````N(n) is unprovable in T
``````

``````UnPr( N(n) )
``````

``````G(n): UnPr( N(n) )
``````

``````G(g): UnPr( G(g) )
``````

``````G(n): UnPr( N(n) )
``````

``````G(n): P( N(n) )
``````

``````G(g): P( G(g) )
``````

## 4.大道至简——康托尔的天才

``````1 2 3 4 …

2 4 6 8 …
``````

``````1/1 1/2 2/1 1/3 2/2 3/1 1/4 2/3 3/2 4/1 …
``````

``````1 a10.a11a12a13…

2 a20.a21a22a23…

3 a30.a31a32a33…

4 …

5 …

（注：aij里面的ij是下标）
``````

``````       1  2  3  4 …

M1  N  1  N  N …

M2  2  0  N  0 …

M3  0  1  2  0 …

M4  N  0  5  N …

…
``````

M1，M2，M3 … 是逐一列出的图灵机，并且，注意，由于程序即数据，每个图灵机都有唯一编码，所以我们规定在枚举图灵机的时候Mi其实就代表编码为i的图灵机，当然这里很多图灵机将会是根本没用的玩意，但这不要紧。此外，最上面的一行1 2 3 4 … 是输入数据，如，矩阵的第一行代表M1分别在1，2，3，…上面的输出，不停机的话就是N。

``````P(i):

if( H(i, i) == 1 ) then // Mi(i) halts

return 1 + Mi(i)

else // if H(i, i) == 0 (Mi(i) doesn’t halt)

return 0
``````

``````Mk(k) = P(k) =

1+Mk(k) if Mk(k) halts

0 if Mk(k) doesn’t halt
``````

#### 罗素悖论

``````R = {X:X不属于X};
``````

``````      S1  S2  S3 …

S1  0     1     1 …

S2  1     1     0 …

S3  0     0     0 …

… …
``````