Truth, deduction, computation lecture g

Click here to load reader

  • date post

    01-Nov-2014
  • Category

    Education

  • view

    306
  • download

    1

Embed Size (px)

description

My logic lectures at SCU untyped lambda, combinators

Transcript of Truth, deduction, computation lecture g

  • 1. Truth, Deduction, Computation Lecture G Untyped , Combinators Vlad Patryshev SCU 2013
  • 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. -reduction - weirder, Javascript weirder = function(x) { return x(x)(x(x)) } weirder( function(a) { println("called with " + a); return a } )
  • 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. -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. -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. -conversion in Javascript function(x) { return y(x) } is functionally the same as y
  • 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. 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. Define addition in Lambda f taken n times f taken m times add n m f x ((n f) ((m f) x))) Lets try 2+2=4...
  • 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. 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. 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. 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. 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 += '
    ; }
  • 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 += '
    ; } 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. 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. Exponentiation? Even easier mn = m * m**m // n times so: pow n m (n m)
  • 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. 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. 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. 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. 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. Pair pair x y f (f x y) first p (p true) second p (p false)
  • 25. Moses Schnfinkel Invented currying Invented combinators His other papers were burned by his neighbors
  • 26. Combinators All lambda expressions can be build from these three: S, K, I No variables required. Hold on.
  • 27. Combinators I x x K x (y x) S x y z ((x z) (y z))
  • 28. Combinator I I x x It is identity function; in Javascript: I = function(x) { return x }
  • 29. Combinator K K x (y x) It builds a constant function; in Javascript: K = function(x) { return function(y){return x} }
  • 30. Combinator S S x y z ((x z) (y z)) It expresses the idea of sub