33
Truth, Deduction, Computation Lecture G Untyped λ, Combinators Vlad Patryshev SCU 2013

• Category

## Education

• view

417

2

description

My logic lectures at SCU untyped lambda, combinators

### Transcript of Truth, deduction, computation lecture g

Truth, Deduction, ComputationLecture GUntyped λ, Combinators

β-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.

β-reduction - weirder, Javascript

weirder = function(x) { return x(x)(x(x)) }

weirder( function(a) { println("called with " + a); return a })

β-reduction - careful with names

t=λz (x y)s=z

Try:

● (λx t) z =● (λx (λz (x y))) z =● λz (z y)

Wrong!

β-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

η-conversion rule

● β: (λx (y x)) z ⇔ (y z)● η: (λx (y x)) z ⇔ (y z)

λx (F x) ⇔ F - where F does not contain x

Wait! What the difference with β?

η-conversion in Javascript

function(x) { return y(x) }

is functionally the same as

y

Church Numerals in Lambda

● 0 ≡ λ f (λ x x)● 1 ≡ λ f (λ x (f x)) ● 2 ≡ λ f (λ x (f (f x))) ● etc● S(n) ≡ λ f (λ x (f ((n f) x)))

f taken (n+1) times

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)) }

}

add ≡ λ n λ m λ f λ x ((n f) ((m f) x)))

Let’s try 2+2=4...

f taken n times f taken m times

Proof that 1+3=3+1, in plain PeanoAccording 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

Proof that 1+3=3+1, λ, page 1S :⇔ λ 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.

Proof that 1+3=3+1, λ, page 2Now, 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

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

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 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 />;

}

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))))

Exponentiation? Even easier

mn = m * m*…*m // n times… so:

pow ≡ λ n λ m (n m)

Can we have booleans?

● true ≡ λ x λ y x● false ≡ λ x λ y y● and ≡ λ x λ y ((x y) x)● or ≡ λ x λ y ((x x) y)● not ≡ λ x (x (λ a λ c c) (λ a λc a)) ● cond ≡ λ c λ t λ f ((c t) f)

Try It!!!

λ 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)}

Or we can try to reduce

1. and true false =2. (λ x λ y (x y x)) true false =3. true false true =4. (λ x λ y x) false true =5. false

How cond works

1. cond true A B =2. λ c λ t λ f ((c t) f) true A B =3. ((true A) B) =4. ((λ x λ y x) A) B = A

5. cond false A B =6. λ c λ t λ f ((c t) f) false A B =7. ((false A) B) =8. ((λ x λ y y) A) B = B

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)}

Pair

● pair ≡ λ x λ y λ f (f x y)● first ≡ λ p (p true)● second ≡ λ p (p false)

Moses Schönfinkel

● Invented currying● Invented combinators● His other papers were burned by his neighbors

Combinators

All lambda expressions can be build from these three:S, K, I

No variables required.Hold on.

Combinators

● I ≡ λx x● K ≡ λx (λy x)● S ≡ λx λ y λ z ((x z) (y z))

Combinator I

● I ≡ λx x

It is identity function; in Javascript:

I = function(x) { return x }

Combinator K

● K ≡ λx (λy x)

It builds a constant function;in Javascript:K = function(x) { return function(y){return x} }

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)) } }}

Using Combinators

● ((S K K) x) = (S K K x) = (K x (K x)) = x

So I = SKK

● true=K● false=KI● numbers?

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...

That’s it for today