introduction to the lambda calculus
TRANSCRIPT
![Page 1: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/1.jpg)
Introduction to the Lambda Calculus
Alonzo Church
1932, “A set of postulates for the foundation of logic”, a formal system with the aim of providing a foundation for logic which would be more natural than Russell’s type theory or Zermelo’s set theory, and would not contain free variables
1936 Church isolated and published just the portion relevant to computation, what is now called the untyped lambda calculus
1940, he also introduced a computationally weaker, but logically consistent system, known as the simply typed lambda calculus
Re-discovered as a versatile tool in Computer Science by people like McCarthy, et al. in the 1960s
![Page 2: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/2.jpg)
Operational Semantics -- Syntax
t ::= x /* variable */ λx.t /* abstraction */ t t /* application */
v ::= λx.t /* abstraction value */
![Page 3: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/3.jpg)
Operational Semantics -- Evaluation t1 t1' /* congruence rule 1 */t1 t2 t1' t2
t2 t2' /* congruence rule 2 */v1 t2 v1 t2'
(λx.t) v [x ↦ v]t /* computation rule */
![Page 4: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/4.jpg)
That’s it!
No data structuresNo control flow statementsNo strings, numbers, or even booleans
Any questions?
![Page 5: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/5.jpg)
Any questions?
Like, how do you do anything?
![Page 6: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/6.jpg)
Church encoding
representing data and operators in the lambda calculus
![Page 7: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/7.jpg)
Let’s start with the booleans
true = λt. λf. t
false = λt. λf. f
not = λa. a false true
![Page 8: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/8.jpg)
Applying not
(λa. a false true) true
(λa. a false true) (λt. λf. t)
(λt. λf. t) false true
false
(λx.t) v [x v]t↦
(from operational semantic rules)
![Page 9: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/9.jpg)
β-reduction
applying functions to their arguments
Normal order reduction – most well-known languages work from the leftmost, outermost, redex and work in, halting when the outermost argument can no longer be applied
Redex – reducible expression
Full β-reduction – reduce available redexes in any desired order
![Page 10: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/10.jpg)
Full β-reduction
λs.λz. (λs'.λz'. (λb. b) s' ((λt.λb. b) s' z')) s ((λs'.λz'.
(λs''.λz''. (λb. b) s'' ((λt.λb. b) s'' z''))
s' ((λt.λb. b) s' z'))
s z)
![Page 11: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/11.jpg)
Full β-reduction
λs.λz. (λs'.λz'. s' z') s ((λs'.λz'. (λs''.λz''. s'' z'') s' z') s z)
![Page 12: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/12.jpg)
Full β-reduction
λs.λz. s ((λs'.λz'. (λs''.λz''. s'' z'') s' z') s z)
![Page 13: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/13.jpg)
Full β-reduction
λs.λz. s ((λs'.λz'. s' z') s z)
![Page 14: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/14.jpg)
Full β-reduction
λs.λz. s (s z)
![Page 15: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/15.jpg)
Back to the booleans
and = λa. λb. a b fls
or = λa. λb. a tru b
nand = λa. λb. not (and a b)
xor = λa. λb. and (nand a b) (or a b)
cond = λp. λt. λf. p t f /* i.e. if else */
![Page 16: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/16.jpg)
and
(λa. λb. a b fls) tru fls
(λt. λf. t) (λt. λf. f) (λt. λf. f)
λt. λf. f
(λa. λb. a b fls) tru tru
(λt. λf. t) (λt. λf. t) (λt. λf. f)
λt. λf. t
![Page 17: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/17.jpg)
Hooray!
xnor = λa. λb. not (xor a b)
![Page 18: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/18.jpg)
Now we have assertions!
xnor = λa. λb. not (xor a b)
xnor tru (cond fls fls tru)
xnor fls (cond fls tru fls)
![Page 19: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/19.jpg)
Tuples
pair = λf. λs. λb. b f s
fst = λp. p tru
snd = λp. p fls
![Page 20: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/20.jpg)
Numbers
c0 = λs. λz. zc1 = λs. λz. s zc2 = λs. λz. s (s z)c3 = λs. λz. s (s (s z))…
Just like Peano arithmetic
![Page 21: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/21.jpg)
Operations on numbersiszro = λm. m (λx. fls) tru
plus = λm. λn. λs. λz. m s (n s z)mult = λm. λn. m (plus n) c0exp = λm. λn. n (mult m) c1
scc = λn. λs. λz. s (n s z) /* number successor */
/* number predecessor */prd = λm. fst (m (λp. pair (snd p) (plus c1 (snd p))) (pair c0 c0))
eql = λm. λn. and (iszro (m prd n)) (iszro (n prd m))
![Page 22: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/22.jpg)
Automating assertions
/* it is true that ... */
xnor tru (eql c1 (exp c1 c1))
xnor tru (eql c6 (plus c2 c4))
xnor tru (eql c1 (fst (pair c1 tru)))
![Page 23: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/23.jpg)
Lists
Lists follow from tuples and numbers(more on that another time)
![Page 24: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/24.jpg)
Recursion
fix (a.k.a the Y-combinator)
= λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))
takes recursive abstractionλf. λx. __f__
Haskell Curry
![Page 25: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/25.jpg)
Recursion
(λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) (λf. λx. __) (bar)
(λx. (λf. λx'. __) (λy. x x y)) (λx. (λf. λx'. __) (λy. x x y)) (bar) (foo)
(λf. λx'. __) (λy. foo foo y) (bar)
λx'. __ (bar)
![Page 26: Introduction to the lambda calculus](https://reader036.vdocuments.mx/reader036/viewer/2022081505/58a708fe1a28ab4c438b613f/html5/thumbnails/26.jpg)
Recursion
fact = λf. λx. cond (iszro x) c1 (mult x (f (prd x)))
(λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) fact c3
/* successive interations */(λy. (λx. f (λy'.x x y')) (λx. f (λy'.x x y')) y) (/* prev recurs*/)