Truth, deduction, computation lecture g

319 views
214 views

Published on

My logic lectures at SCU
untyped lambda, combinators

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
319
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Truth, deduction, computation lecture g

  1. 1. Truth, Deduction, Computation Lecture G Untyped λ, Combinators Vlad Patryshev SCU 2013
  2. 2. β-reduction - weirder case ● ● ● ● t = λx ((x x) x) t t = ? (λx ((x x) x)) (λx ((x x) x)) ⇒... (((λx ((x x) x)) (λx ((x x) x))) (λx ((x x) x)), that is, ((t t) t) - ouch.
  3. 3. β-reduction - weirder, Javascript weirder = function(x) { return x(x)(x(x)) } weirder( function(a) { println("called with " + a); return a } )
  4. 4. β-reduction - careful with names t=λz (x y) s=z Try: ● (λx t) z = ● (λx (λz (x y))) z = ● λz (z y) Wrong!
  5. 5. β-reduction - careful with names t=λz (x y) = λw (x y) s=z Try: ● (λx t) z = ● (λx (λw (x y))) z = ● λw (x y) Right
  6. 6. η-conversion rule λx (F x) ⇔ F - where F does not contain x Wait! What the difference with β? ● β: (λx (y x)) z ⇔ (y z) ● η: (λx (y x)) z ⇔ (y z)
  7. 7. η-conversion in Javascript function(x) { return y(x) } is functionally the same as y
  8. 8. Church Numerals in Lambda ● ● ● ● ● 0 ≡ λ f (λ 1 ≡ λ f (λ 2 ≡ λ f (λ etc S(n) ≡ λ f x x) x (f x)) x (f (f x))) (λ x (f ((n f) x))) f taken (n+1) times
  9. 9. Church Numerals in Javascript c0=function(f){return function(x){return x}} c1=function(f){return function(x){return f(x)}} c2=function(f){return function(x){return f(f(x))}} // or just def times(n)=function(f){ return n ? times(n-1)(f) : c0 } //… c1=times(1); c2=times(2) etc //or def next(n) = function(f) { return function(x) { return f(n(f)(x)) } }
  10. 10. Define addition in Lambda f taken n times f taken m times add ≡ λ n λ m λ f λ x ((n f) ((m f) x))) Let’s try 2+2=4...
  11. 11. Proof that 1+3=3+1, in plain Peano According to the definition of the successor function s, we have that s(0)=1, s(s(0))=2, s(s(s(0)))=3, s(s(s(s(0))))=4 and so forth. Then we have 3+1 = s(s(s(0))) + s(0) = s(s(s(s(0))) + 0) by axiom x+s(y)=s(x+y) = s(s(s(s(0)))) by axiom x+0=x = 4 1+3 = s(0) + s(s(s(0))) = s(s(0) + s(s(0))) by axiom x+s(y)=s(x+y) = s(s(s(0) + s(0))) by axiom x+s(y)=s(x+y) = s(s(s(s(0) + 0))) by axiom x+s(y)=s(x+y) = s(s(s(s(0)))) by axiom x+0=x =4 Hence we have proved that 3+1=1+3=4
  12. 12. Proof that 1+3=3+1, λ, page 1 S :⇔ λ abc.b(abc) S0 = λ abc.b(abc) (λ sz.z) = λ bc.b((λ sz.z) bc) = λ bc.b((λ z.z) c) = λ bc.b(c) λ bc.b(c) = λ sz.s(z) = 1 S1 = λ abc.b(abc) (λ sz.s(z)) = λ bc.b((λ sz.s(z)) bc) = λ bc.b((λ z.b(z)) c) = λ bc.b(b(c)) λ bc.b(b(c)) = λ sz.s(s(z)) = 2 Thus, we have the following derivations for the successor function. It does exactly what it is supposed to do: starting from 0, it can produce any natural number. 0 = 0 S0 = 1 S1 = SS0 = 2 S2 = SS1 = SSS0 = 3 S3 = SS2 = SSS1 = SSSS0 = 4 ... Now, let's prove 3+1 = 1+3.
  13. 13. Proof that 1+3=3+1, λ, page 2 Now, let's prove 3+1 = 1+3. 3+1 = 3S1 = (λsz.s(s(s(z))))(λabc.b(abc))(λxy.x(y)) = (λz.(λabc.b(abc)(λabc.b(abc)(λabc.b(abc)(z)))))(λxy.x(y)) = (λabc.b(abc)(λabc.b(abc)(λabc.b(abc))))(λxy.x(y))) = SSS1 = 4 We can continue to tediously reduce the expression further instead of using the quick solution by reference above to get 3+1 = SSS1 = 4 3+1 = (λabc.b(abc)(λabc.b(abc)(λabc.b(abc))))(λxy.x(y))) = (λabc.b(abc)(λabc.b(abc)(λbc.b((λxy.x(y))bc)))) = (λabc.b(abc)(λabc.b(abc)(λbc.b((λy.b(y))c)))) = (λabc.b(abc)(λabc.b(abc)(λbc.b(b(c))))) = (λabc.b(abc)(λbc.b((λbc.b(b(c))bc))) = (λabc.b(abc)(λbc.b((λc.b(b(c))c))) = (λabc.b(abc)(λbc.b(b(b(c)))) = (λbc.b((λbc.b(b(b(c))bc)))) = (λbc.b((λc.b(b(b(c)c)))) = (λbc.b(b(b(b(c))))) = 4 1+3 = 1S3 = (λsz.s(z))(λabc.b(abc))(λxy.x(x(x(y)))) = (λz.((λabc.b(abc))(z)))(λxy.x(x(x(y)))) = (λabc.b(abc))(λxy.x(x(x(y)))) = S3 = 4 We can continue to tediously reduce the expression further instead of using the quick solution by reference above to get 1+3 = S3 = 4
  14. 14. Proof that 1+3=3+1, λ, page 3 We can continue to tediously reduce the expression further instead of using the quick solution by reference above to get 1+3 = S3 = 4 1+3 = (λabc.b(abc))(λxy.x(x(x(y)))) = (λbc.b((λxy.x(x(x(y)))))bc) = (λbc.b((λy.b(b(b(y)))))c) = (λbc.b(b(b(b(c))))) = 4 Hence, it's mathematically proven that 3+1 = 1+3 = 4 in Lambda calculus
  15. 15. Proof that 1+3=3+1, in JavaScript // define var zero = function(f){ return function(x){return x}} var succ = function(n){return function(f){return function(x){return f(n(f)(x))}}} var add = function(m){ return function(n){ return function(f){ return function(x){ return m(f)(n(f)(x))}}}} // execute function $(id){ return document.getElementById(id)} var one = succ(zero) var two = succ(one) var three = succ (two) var four = add(two)(two) var three_plus_one = add(three)(one) var one_plus_three = add(one)(three) var numbers = [one, two, three, four, three_plus_one, one_plus_three,] $('result').innerHTML = '' for (var i = 0; i < numbers.length; i++){ var n = numbers[i]; $('result').innerHTML += numbers[i](function(n){return 1+n})(0); $('result').innerHTML += ' = '; $('result').innerHTML += numbers[i](function(n){return '(1+' + n + ')'})(0); $('result').innerHTML += '<br />; }
  16. 16. Proof that 1+3=3+1, in JavaScript $('result').innerHTML = '' for (var i = 0; i < numbers.length; i++){ var n = numbers[i]; $('result').innerHTML += numbers[i](function(n){return 1+n})(0); $('result').innerHTML += ' = '; $('result').innerHTML += numbers[i](function(n){return '(1+' + n + ')'})(0); $('result').innerHTML += '<br />; } The following result can be obtained from onclick="eval(document.getElementById ("lambda").firstChild.nodeValue)". 1 = (1+0) 2 = (1+(1+0)) 3 = (1+(1+(1+0))) 4 = (1+(1+(1+(1+0)))) 4 = (1+(1+(1+(1+0)))) 4 = (1+(1+(1+(1+0))))
  17. 17. Multiplication in Lambda take it n times f taken m times mult ≡ λ n λ m λ f λ x (n (m f)) x))) How about 2*2=4? http://dankogai.typepad.com/blog/2006/03/lambda_calculus.html
  18. 18. Exponentiation? Even easier mn = m * m*…*m // n times… so: pow ≡ λ n λ m (n m)
  19. 19. Can we have booleans? ● ● ● ● ● ● true false and or not cond ≡ ≡ ≡ ≡ ≡ ≡ λ λ λ λ λ λ x x x x x c λ y x λ y y λ y ((x λ y ((x (x (λ a λ t λ f y) x) x) y) λ c c) (λ a λc a)) ((c t) f) Try It!!!
  20. 20. λ Booleans in Javascript? ● ● ● ● ● True=function(x){return function(y){return x}} False=function(x){return function(y){return y}} And=function(x){return function(y){return x(y)(x)}} Or =function(x){return function(y){return x(x)(y)}} Cond = function(c){ return function(t){ return function(f){ return cond(t)(f) }}} Try It!!! you may need this: function p(f) {println(f == True ? "TRUE" : f == False ? "FALSE" : f)}
  21. 21. Or we can try to reduce 1. 2. 3. 4. 5. and true false = (λ x λ y (x y x)) true false = true false true = (λ x λ y x) false true = false
  22. 22. How cond works 1. 2. 3. 4. cond true λ c λ t λ ((true A) ((λ x λ y A B = f ((c t) f) true A B = B) = x) A) B = A 5. 6. 7. 8. cond false A B = λ c λ t λ f ((c t) f) false A B = ((false A) B) = ((λ x λ y y) A) B = B
  23. 23. Can we check a number for zero? is_zero ≡ λ n n (λ x false) true in Javascript: is_zero=function(n){ return n(function(x){return false})(true) }
  24. 24. Pair ● pair ≡ λ x λ y λ f (f x y) ● first ≡ λ p (p true) ● second ≡ λ p (p false)
  25. 25. Moses Schönfinkel ● Invented currying ● Invented combinators ● His other papers were burned by his neighbors
  26. 26. Combinators All lambda expressions can be build from these three: S, K, I No variables required. Hold on.
  27. 27. Combinators ● I ≡ λx x ● K ≡ λx (λy x) ● S ≡ λx λ y λ z ((x z) (y z))
  28. 28. Combinator I ● I ≡ λx x It is identity function; in Javascript: I = function(x) { return x }
  29. 29. Combinator K ● K ≡ λx (λy x) It builds a constant function; in Javascript: K = function(x) { return function(y){return x} }
  30. 30. Combinator S ● S ≡ λx λ y λ z ((x z) (y z)) It expresses the idea of substitution; in Javascript: S = function(x) { return function(y) { return function(z) { return x(z)(y(z)) } }}
  31. 31. Using Combinators ● ((S K K) x) = (S K K x) = (K x (K x)) = x So I = SKK ● true=K ● false=KI ● numbers?
  32. 32. Translating λ to Combinators To convert an expression e in the lambda calculus to an expression in SKI, define a function ϕ(e): e ϕ(e) λx x I λx c Kc λx (α β) (S(λx ϕ(α))(λx ϕ(β))) Now try Church Numerals...
  33. 33. That’s it for today

×