Contenu connexe Similaire à Lambda演算与邱奇编码 (20) Lambda演算与邱奇编码3. Lambda Calculus
● Programming Languages
● Turing Machine
● Combinatory logic
● …...
4. 历史
●
人物
– David Hilbert => 23 个数学问题 => 计算理论与可计
算性
– 图灵 Turing => 图灵机 【曾经师从邱奇】
– 哥德尔 Godel => …...
– 邱奇 => Lambda Calculus
●
最终等价
– 丘奇 - 图灵论题?
– 丘奇论题?
5. LC
●
原来用于研究数学的形式化的一部分内容 , 后出现问题 .
将演算这一部分拿出来研究可计算性 .
●
目前的 lambda 演算应用在数学 , 哲学 , 计算机科学中 . 在
可计算理论中也有应用 , 但是图灵机用的更多一点 . 在程序
语言理论中有重要影响 . 比如 , 函数式编程语言 , 基本在演
算基础上做了少量扩充 .
6. LC- 描述
●
递归函数是计算机科学和数学上的重要部分 . 考虑两个函
数:
– id(x)=x
– sqsum(x,y) = x*x + y*y
7. LC- 描述
●
首先 , 其中函数名并不重要 , 因此可以改写为 " 匿名函数 "
– x -> x
– (x,y) -> x*x + y*y
8. LC- 描述
●
其次 , 函数中的变量名也无所谓 , x,y 的名称可以任意修
改 , 只要对应的函数体也改变 .
●
最后 , 多个参数的函数 , 其实可以变成一个等价函数 : 单参
数的函数 , 这个函数返回另一个单参数的函数 .
– x -> (y -> x*x + y*y)
– 这种将一个多参数的函数转换为多个单参数的函数的转
换叫做 currying.
●
9. LC- 描述
●
由此 , 我们可以应用实际数据在以上的 sqsum 中 :
– ((x,y) -> x*x+y*y)(5,2)= 5*5+2*2
●
完成 currying 转换后的过程就是 :
– (x -> (y -> x*x + y*y)(5))(2)
=(y -> 5*5 + y*y)(2)
= 5*5 + 2*2
10. LC
●
通过 lambda terms 来组成
1. 函数名 , 不重要 , 所以 lambda 演算不需要 .
2. 多参数的函数都可以转换为单参数的函数 , 所以不需要
多参数函数 .
3. 参数名无所谓 , 所以 the naive notion of equality on
lambda terms is alpha-equivalence 。
11. Lambda Terms
●
语法十分简单 :
– lt 可以是一个变量 ,x;
– t 如果是一个 lt, x 是一个变量的话 , λx.t 也是一个 lt( 叫做
lambda abstraction)
– 如果 t 和 s 是 lt, 那么 ts 也是 ( 叫做 application)
●
括号用来解决表达式上的歧义问题 , 比如【 λ 写作 Y 】:
– Yx.((Yx.x)x) 和 (Yx.(Yx.x))x 两者不同 , 要是没括号就弄
不清楚了 .
12. LC
● Alpha equivalence
● Free variables
– 没绑定的都算 .Yx.x 没有 .Yx.y 中 y 是 .
● Capture-avoiding substitutions
– 设定 t,s,r 是 lt,x 、 y 是变量的情况下 , t[x:=r] 的意思是将 t 中的 x 以 r 来替换 .
– x[x:=r] = r
– y[x:=r] = y if x != y
– (ts)[x:=r] = (t[x:=r])(s[x:=r])
– (Yx.t)[x:=t] = Yx.t
– (Yy.t)[x:=r] = Yy.(t[x:=r]) if x!=y 且 y 非 r 中的自由变量
– 示例 :
● (Yx.x)[y :=y] = Yx.(x[y:=y]) = Yx.x
● ((Yx.y)x)[x:=y] = ((Yx.y)[x:=y])(x[x:=y])=(Yx.y)y
13. LC- 续上
– 最后一个变化式中 y 非 r 中的自由变量的作用在于避免变换过程中出现
歧义 , 避免歧义的方法就是修改变量名称就可以 .
●
(Yx.y)[y:=x]=Yx.(y[y:=x])=Yx.x 有歧义
– 修改名称后 :
●
(Yz.x)[x:=y]=Yz.(x[x:=y])=Yz.y 无歧义
● Beta reduction
– (Yx.t)s 可以转化成 t[x:=s]
– 其实应该就是求值的过程 :
● (Yx.xx)(Yx.xx)
-> (Yx.xx)[x:=Yx.xx] = (x[x:=Yx.xx])(x[x:=Yx.xx]) = (Yx.xx)(Yx.xx)
●
14. LC 形式化定义
●
和上面提到的三条一样,描述相对严密。
●
记法
– 最外的括号可省略
– 默认左结合
– 默认参数去尽量远 Yx.M N 是 Yx.(M N) 不是 (Yx.M)
N
– Yx.Yy.Yz 记做 Yxyz.
15. LC- 变量【闭包?】
●
自由和绑定变量
– Y 操作符内的是被绑定的 , 外部的是自由变量 .
– 比如 ,y 是绑定的 ,x 是自由的 :
● Yy.x x y
– 绑定按照最近原则
●
Yx.y(Yx.z x) 最后的 x 绑定的是第二个 .
●
变量集的定义 :
– FV(x)={x} , x is a variable
– FV(Yx.M)=FV(M)/{x}
– FV(M N)=FV(M)∪FV(N)
●
没有自由变量的表达式是 closed 的 . 也即 组合子 或者是组合逻辑中的表达式
16. LC- 三个转换
●
也叫 alpha-renaming, 绑定的变量可以随时改变名称 .
– 通过 alpha 变换的表达式都是 alpha-equivalent , static scope 的语言 [ 比
如 c,cpp], 只要保证变量名称不掩盖 scope 中原有的就可以了 .
– 代换中的使用就需要 alpha 来防止不必要的歧义 .
● β-reduction
– 函数求值
– ((Yv.e)e') is e[v:=e'e], 举例是 ((Yn.n*2)7) -> 7*2
● η-conversion
– 用于表示两个函数的相等性
– 此变换用于化简 , 不是必须的 .
17. Church encoding
● Church Numerals
– 对自然数的建模
● Church Booleans
– 逻辑
● Church pairs
– List 基本操作的影子
● List encodings
●
递归
● ......
18. Church Numerals
● 0 := λf.λx.x
● 1 := λf.λx.f x
● 2 := λf.λx.f (f x)
● 3 := λf.λx.f (f (f x))
● ......
●
N 为 n 次幂操作
●
两个参数的高阶函数
19. Church Numerals
● S = λr .“ring the small bell (ding) and apply r”
● Z = λr .“ring the big bell (dong)”
● S = λr . 1 + r()
● Z = λr . 0
20. Church Numerals
●
加法【后继的泛化】
– C3+4 = λf . λx . C3 f (C4 f x)
– add = λM . λN . λf . λx . N f (M f x)
●
乘法【加法的泛化】
– λM . λN . λf . λx . N (Mf) x
●
幂操作
– λb.λe.e b
●
前驱
– λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u)
21. Church Booleans
●
基本
– true = Ya.Yb. a
– false = Ya.Yb b
●
谓词
– and=Ym.Yn. m n m
– or=Ym.Yn. m m n
– not_1= Ym.Ya.Yb. m b a 【正则运算序与应用运算序 -SICP 】
– not_2= Ym.m(Ya.Yb. b)(Ya.Yb.a)
– xor=Ya.Yb. a(not_2 b) b
– if=Ym.Ya.Yb. m a b
22. Church pairs
● pair ≡ λx.λy.λz.z x y
– 求值后会编程等待一个处理两个参数的函数的函数
● fst ≡ λp.p (λx.λy.x)
● snd ≡ λp.p (λx.λy.y)
23. LISP
● Scheme [SICP]
– http://www.gnu.org/software/mit-scheme/
● Ranket
– http://racket-lang.org/
● Clojure on JVM or Java Script
– 由于基于 JVM ,对尾递归支持缺失
– http://clojure.org/
● Emacs Lisp
24. Functional Programming
●
http://erlang-china.org/study/yet-another-pf-guide.html
– 文笔很好的介绍,作者
– 其中热部署的问题 与 Smalltalk 的关系?
● http://gnosis.cx/publish/programming/charming_python_13.html
– Python 的一些 FP 相关的用法
● http://www.cnblogs.com/Alexander-Lee/archive/2010/09/16/1827587.ht
ml
– Python 的尾递归的 hack
● http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html
– Python 作者对不提供尾递归优化的说明
25. 参考
● http://www.cs.rice.edu/~javaplt/311/Readings/supplementa
l.pdf
● http://www.cs.rice.edu/~javaplt/311/
● http://en.wikipedia.org/wiki/Lambda_calculus
● http://en.wikipedia.org/wiki/Church_numeral
● http://blog.csdn.net/g9yuayon/article/details/790953
● http://www.cnblogs.com/dragonpig/archive/2010/01/26/16
57052.html
26. 参考
● http://bitwjg.wordpress.com/2011/12/02/lam
bda%E6%BC%94%E7%AE%97%E5%AD%A6%E4%B9%A0%
%AC%94%E8%AE%B0/
● http://zh.wikipedia.org/wiki/%E5%93%A5%E5
%BE%B7%E5%B0%94%E4%B8%8D%E5%AE%8C%E5%A4%
7%E5%AE%9A%E7%90%86
● http://blog.zhaojie.me/2010/05/trends-and-future-direct
ions-in-programming-languages-by-anders-3-functional-pr
ogramming-and-fsharp.html
● http://blog.csdn.net/pongba/article/details/1336028
●
《暗时间》
27. 参考
● http://goodmath.blogspot.com/2006/05/my-favorite-cal
culus-lambda-part-1.html
● 《黑客与画家》
● 《 SICP 》
● http://blog.sina.com.cn/s/blog_5d90e82f010184hx.html
● http://blog.sina.com.cn/s/blog_5d90e82f01018ge9.html
●
《Theoretical Foundations for Practical ‘Totally
Functional Programming’》-- Chapter 2
●
http://blog.flier.lu/files/函数式编程入门.pdf
●
28. Further...
● http://maths.swan.ac.uk/staff/jrh/papers/JRHHislamWeb.p
df
● http://www.utdallas.edu/~gupta/courses/apl/lambda.pdf
● http://www.info.ucl.ac.be/~pvr/book.html
●
《哥德尔,埃舍尔,巴赫 - 集异壁之大成》
● http://blog.csdn.net/g9yuayon/article/details/1676688
● http://en.wikipedia.org/wiki/Alonzo_Church
●
29. Further...
● 《 How to Design Programs 》
● 《 How We Got Here Andy Kessler 》
● 《 Programming Languages and Lambda Calculi 》
● 《 The little schemer 》
● http://blog.tomtung.com/2012/10/yet-another-y-comb
inator-tutorial/
●