LAMBDA CALCULUS - cs.umd.edu · Review Ocaml Lambda Calculus fun x e //anonymous function applies...

Post on 25-Aug-2020

11 views 0 download

Transcript of LAMBDA CALCULUS - cs.umd.edu · Review Ocaml Lambda Calculus fun x e //anonymous function applies...

LAMBDA CALCULUS

Review

Ocaml Lambda Calculus

fun x e //anonymous function applies parameter x

to expression e

λx.e

Review

Ocaml Lambda Calculus

fun x e //anonymous function applies parameter x

on expression e

let x = e1 in e2

λx.e

(λx.e2)e1 //evaluate e2 with x replaced by e1

Beta Reduction

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.x)z

Beta Reduction

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.x)z z

(λx.y)z

Beta Reduction

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.x)z z

(λx.y)z y

(λx.x y)z

Beta Reduction

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.x)z z

(λx.y)z y

(λx.x y)z z y

(λx.λz.x z)y

Beta Reduction

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.x)z z

(λx.y)z y

(λx.x y)z z y

(λx.λz.x z)y (λx.(λz.(x z)))y

e2 e1

Beta Reduction

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.x)z z

(λx.y)z y

(λx.x y)z z y

(λx.λz.x z)y (λx.(λz.(x z)))y λz.y z

e2 e1

Beta Reduction – More Practice

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.xy)(λz.z)

(λx.λy.xy)z

(λx.λy.xy)(λz.zz)x

Beta Reduction – More Practice

Given: (λx.e2)e1 //evaluate e2 with x replaced by e1

(λx.xy)(λz.z) (λz.z)y y

(λx.λy.xy)z λy.zy

(λx.λy.xy)(λz.zz)x (λy.(λz.zz)y)x

(λz.zz)x xx

Static Scoping

(λx.x(λx.x))z

Static Scoping & Alpha Conversion

(λx.x(λx.x))z (λx.x(λy.y))z

Static Scoping & Alpha Conversion

(λx.x(λx.x))z (λx.x(λy.y))z

z(λy.y)

Static Scoping & Alpha Conversion

(λx.x(λx.x))z (λx.x(λy.y))z

z(λy.y)

i.e. (λx.λy.xy)y

Static Scoping & Alpha Conversion

(λx.x(λx.x))z (λx.x(λy.y))z

z(λy.y)

i.e. (λx.λy.xy)y (λx.λz.xz)y λz.yz

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

((((λx.λy.x) false) true)

false) true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

((((λx.λy.x) false) true)

false) true

(((λy.false) true) false)

true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

((((λx.λy.x) false) true)

false) true

(((λy.false) true) false)

true

((false) false) true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

((((λx.λy.x) false) true)

false) true

(((λy.false) true) false)

true

((false) false) true

((λx.λy.y) false) true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

((((λx.λy.x) false) true)

false) true

(((λy.false) true) false)

true

((false) false) true

((λx.λy.y) false) true

(λy.y) true

Lambda Calculus Encodings

Given: not = λx.((x false)true)

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

λx.((x false)true)(not true)

((not true) false) true

((λx.((x false) true) true)

false) true

(((true false) true) false)

true

((((λx.λy.x) false) true)

false) true

(((λy.false) true) false)

true

((false) false) true

((λx.λy.y) false) true

(λy.y) true = true

Lambda Calculus Encodings

Given: or = λx.λy.((x true) y)

true = λx.λy.x

false = λx.λy.y

Prove: or false true = true

Lambda Calculus Encodings

Given: or = λx.λy.((x true) y)

true = λx.λy.x

false = λx.λy.y

Prove: or false true = true

λx.λy.((x true) y) false true

λy.((false true) y) true

(false true) true

((λx.λy.y) true) true

(λy.y) true

true

Lambda Calculus Encodings

Given: if a then b else c = abc

true = λx.λy.x

false = λx.λy.y

Prove: if false then x else y = y

Lambda Calculus Encodings

Given: if a then b else c = abc

true = λx.λy.x

false = λx.λy.y

Prove: if false then x else y = y

false x y

(λx.λy.y) x y

(λy.y) y

y