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

31
LAMBDA CALCULUS

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

Page 1: LAMBDA CALCULUS - cs.umd.edu · 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

LAMBDA CALCULUS

Page 2: LAMBDA CALCULUS - cs.umd.edu · 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

Review

Ocaml Lambda Calculus

fun x e //anonymous function applies parameter x

to expression e

λx.e

Page 3: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 4: LAMBDA CALCULUS - cs.umd.edu · 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

Beta Reduction

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

(λx.x)z

Page 5: LAMBDA CALCULUS - cs.umd.edu · 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

Beta Reduction

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

(λx.x)z z

(λx.y)z

Page 6: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 7: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 8: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 9: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 10: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 11: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 12: LAMBDA CALCULUS - cs.umd.edu · 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

Static Scoping

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

Page 13: LAMBDA CALCULUS - cs.umd.edu · 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

Static Scoping & Alpha Conversion

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

Page 14: LAMBDA CALCULUS - cs.umd.edu · 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

Static Scoping & Alpha Conversion

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

z(λy.y)

Page 15: LAMBDA CALCULUS - cs.umd.edu · 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

Static Scoping & Alpha Conversion

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

z(λy.y)

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

Page 16: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 17: LAMBDA CALCULUS - cs.umd.edu · 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

Lambda Calculus Encodings

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

true= λx.λy.x

false= λx.λy.y

Prove: not(not true) = true

Page 18: LAMBDA CALCULUS - cs.umd.edu · 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

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)

Page 19: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 20: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 21: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 22: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 23: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 24: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 25: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 26: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 27: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 28: LAMBDA CALCULUS - cs.umd.edu · 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

Lambda Calculus Encodings

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

true = λx.λy.x

false = λx.λy.y

Prove: or false true = true

Page 29: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 30: LAMBDA CALCULUS - cs.umd.edu · 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

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

Page 31: LAMBDA CALCULUS - cs.umd.edu · 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

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