Share
Embed
Untyped Lambda Calculus
An introduction to the Untyped Lambda Calculus.
Topics:
- Structure and Evaluation
- Higher Order Functions, Currying
- Church Encodings (Booleans, Pairs, Numerals)
This talk was given at the Lambda Pi MeetUp in Hamburg (https://github.com/lambda-pi-meetup/talks).
Sven Tennie
August 09, 2018
Transcript
156 slides
-
Untyped Lambda Calculus Structure and Evaluation, Currying, Church Encodings Sven
Tennie August 9, 2018 Dream IT https://dreamit.de -
Lambda Calculus • Invented by Alonzo Church (1920s) • Equally
expressive to the Turing Machine(s) • Formal Language • Computational Model • Lisp (1950s) • ML • Haskell • "Lambda Expressions" in almost every modern programming language 1 -
Why should I care? • Simple Computational Model • to
describe structure and behaviour (E.g. Operational Semantics, Type Systems) • to reason and prove 2 -
Why should I care? • Simple Computational Model • to
describe structure and behaviour (E.g. Operational Semantics, Type Systems) • to reason and prove • Explains why things in FP are like they are • Pure Functions • Higher-Order Functions • Currying • Lazy Evaluation 2 -
Why should I care? • Simple Computational Model • to
describe structure and behaviour (E.g. Operational Semantics, Type Systems) • to reason and prove • Explains why things in FP are like they are • Pure Functions • Higher-Order Functions • Currying • Lazy Evaluation • Understand FP Compilers • Introduce FP stuff into other languages • Write your own compiler • GHC uses an enriched Lambda Calculus internally 2 -
Untyped Lambda Calculus t ::= Terms: x Variable λx.t Abstraction
t t Application 3 -
Untyped Lambda Calculus t ::= Terms: x Variable λx.t Abstraction
t t Application Example - Identity Lambda Calculus λx.x Abstraction y Variable Application → y 3 -
Untyped Lambda Calculus t ::= Terms: x Variable λx.t Abstraction
t t Application Example - Identity Lambda Calculus λx.x Abstraction y Variable Application → y Javascript (function (x){return x; } Abstraction ) ( y Variable ) Application 3 -
Example - (λx.λy.x y) a b Abstractions Think: Function Definitions
(λx.λy.x y) a b 4 -
Example - (λx.λy.x y) a b Abstractions Think: Function Definitions
(λx.λy.x y) a b Variables Think: Parameters (λx.λy.x y) a b 4 -
Example - (λx.λy.x y) a b Abstractions Think: Function Definitions
(λx.λy.x y) a b Variables Think: Parameters (λx.λy.x y) a b Applications Think: Function Calls (λx.λy.x y) a b 4 -
Example - (λx.λy.x y) a b (λx. λy.x y) a
b Substitute x → a 5 -
Example - (λx.λy.x y) a b (λx. λy.x y) a
b Substitute x → a 5 -
Example - (λx.λy.x y) a b (λx. λy.x y) a
b Substitute x → a → (λy.a y) b 5 -
Example - (λx.λy.x y) a b (λx. λy.x y) a
b Substitute x → a → (λy.a y) b Substitute y → b 5 -
Example - (λx.λy.x y) a b (λx. λy.x y) a
b Substitute x → a → (λy.a y) b Substitute y → b 5 -
Example - (λx.λy.x y) a b (λx. λy.x y) a
b Substitute x → a → (λy.a y) b Substitute y → b → a b 5 -
Notational Conventions • We use parentheses to clearify what’s meant
• Applications associate to the left s t u ≡ (s t) u • Abstractions expand as much to the right as possible λx.λy.x y x ≡ λx.(λy.(x y x)) 6 -
Scope λx.λy.x y z Bound and Free λy y is
bound, x and z are free λx x and y are bound, z is free λx, λy binders 7 -
Scope λx.λy.x y z Bound and Free λy y is
bound, x and z are free λx x and y are bound, z is free λx, λy binders A term with no free variables is closed • A combinator • id ≡ λx.x • Y, S, K, I . . . 7 -
Higher Order Functions • Functions that take or return functions
• Are there "by definition" λx.x Abstraction λy.y Abstraction Application → λy.y Abstraction 8 -
Currying Idea • Take a function with n arguments •
Create a function that takes one argument and returns a function with n − 1 arguments 9 -
Currying Idea • Take a function with n arguments •
Create a function that takes one argument and returns a function with n − 1 arguments Example • (+1) Section in Haskell • (λx.λy. + x y) 1 → λy. + 1 y 9 -
Currying Idea • Take a function with n arguments •
Create a function that takes one argument and returns a function with n − 1 arguments Example • (+1) Section in Haskell • (λx.λy. + x y) 1 → λy. + 1 y • Partial Function Application is there "by definition" • You can use this stunt to "curry" in every language that supports "Lambda Expressions" 9 -
Reductions and Conversions Alpha Conversion λx.x →α λy.y Beta Reduction
(λx.x) y →β y Eta Conversion Iff (if and only if) x is not free in f : λx.f x →η f (λx. (λy.y) f x) a →η (λy.y) f a 10 -
Reductions and Conversions Alpha Conversion λx.x →α λy.y Beta Reduction
(λx.x) y →β y Eta Conversion Iff (if and only if) x is not free in f : λx.f x →η f (λx. (λy.y) f x) a →η (λy.y) f a If x is free in f , η conversion not possible: λx. (λy.y Bound ↓ x ) f x →η (λy.y Free?! ↓ x ) 10 -
Remarks • Everything (Term) is an Expression • No statements
• No "destructive" Assignments • The reason why FP Languages promote pure functions • But you could invent a built-in function to manipulate "state". . . 11 -
Operational Semantics • We learned how to write down and
talk about Lambda Calculus Terms • How to evaluate them? • Different Strategies • Interesting outcomes 12 -
Full Beta-Reduction • RedEx • Reducible Expression • Always an
Application (λx.x) ((λy.y) (λz. (λa.a) z RedEx ) RedEx ) RedEx 13 -
Full Beta-Reduction • RedEx • Reducible Expression • Always an
Application (λx.x) ((λy.y) (λz. (λa.a) z RedEx ) RedEx ) RedEx Full Beta-Reduction • Any RedEx, Any Time • Like in Arithmetics • Too vague for programming. . . 13 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) Normal Order Reduction
• Left-most, Outer-most RedEx 14 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) Normal Order Reduction
• Left-most, Outer-most RedEx 14 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z)
Normal Order Reduction • Left-most, Outer-most RedEx 14 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z)
Normal Order Reduction • Left-most, Outer-most RedEx 14 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z Normal Order Reduction • Left-most, Outer-most RedEx 14 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z Normal Order Reduction • Left-most, Outer-most RedEx 14 -
Normal Order Reduction (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z)
→λz.(λa.a) z →λz.z Normal Order Reduction • Left-most, Outer-most RedEx 14 -
Call-by-Name (λx.x) ((λy.y) (λz.(λa.a) z)) Call-by-Name • like Normal Order
Reduction, but no reductions inside Abstractions • Abstractions are values • lazy, non-strict • Parameters are not evaluated before they are used • Optimization: Save results → Call-by-Need 15 -
Call-by-Name (λx.x) ((λy.y) (λz.(λa.a) z)) Call-by-Name • like Normal Order
Reduction, but no reductions inside Abstractions • Abstractions are values • lazy, non-strict • Parameters are not evaluated before they are used • Optimization: Save results → Call-by-Need 15 -
Call-by-Name (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z) Call-by-Name •
like Normal Order Reduction, but no reductions inside Abstractions • Abstractions are values • lazy, non-strict • Parameters are not evaluated before they are used • Optimization: Save results → Call-by-Need 15 -
Call-by-Name (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z) Call-by-Name •
like Normal Order Reduction, but no reductions inside Abstractions • Abstractions are values • lazy, non-strict • Parameters are not evaluated before they are used • Optimization: Save results → Call-by-Need 15 -
Call-by-Name (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z) →λz.(λa.a) z
Call-by-Name • like Normal Order Reduction, but no reductions inside Abstractions • Abstractions are values • lazy, non-strict • Parameters are not evaluated before they are used • Optimization: Save results → Call-by-Need 15 -
Call-by-Name (λx.x) ((λy.y) (λz.(λa.a) z)) →(λy.y) (λz.(λa.a) z) →λz.(λa.a) z
→ Call-by-Name • like Normal Order Reduction, but no reductions inside Abstractions • Abstractions are values • lazy, non-strict • Parameters are not evaluated before they are used • Optimization: Save results → Call-by-Need 15 -
Call-by-Value (λx.x) ((λy.y) (λz.(λa.a) z)) Call-by-Value • Outer-most, only if
right-hand side was reduced to a value • No reductions inside Abstractions • Abstractions are values • eager, strict • Parameters are evaluated before they are used 16 -
Call-by-Value (λx.x) ((λy.y) (λz.(λa.a) z)) Call-by-Value • Outer-most, only if
right-hand side was reduced to a value • No reductions inside Abstractions • Abstractions are values • eager, strict • Parameters are evaluated before they are used 16 -
Call-by-Value (λx.x) ((λy.y) (λz.(λa.a) z)) →(λx.x) (λz.(λa.a) z) Call-by-Value •
Outer-most, only if right-hand side was reduced to a value • No reductions inside Abstractions • Abstractions are values • eager, strict • Parameters are evaluated before they are used 16 -
Call-by-Value (λx.x) ((λy.y) (λz.(λa.a) z)) →(λx.x) (λz.(λa.a) z) Call-by-Value •
Outer-most, only if right-hand side was reduced to a value • No reductions inside Abstractions • Abstractions are values • eager, strict • Parameters are evaluated before they are used 16 -
Call-by-Value (λx.x) ((λy.y) (λz.(λa.a) z)) →(λx.x) (λz.(λa.a) z) →λz.(λa.a) z
Call-by-Value • Outer-most, only if right-hand side was reduced to a value • No reductions inside Abstractions • Abstractions are values • eager, strict • Parameters are evaluated before they are used 16 -
Call-by-Value (λx.x) ((λy.y) (λz.(λa.a) z)) →(λx.x) (λz.(λa.a) z) →λz.(λa.a) z
→ Call-by-Value • Outer-most, only if right-hand side was reduced to a value • No reductions inside Abstractions • Abstractions are values • eager, strict • Parameters are evaluated before they are used 16 -
Church Encodings • Encode Data into the Lambda Calculus •
To simplify our formulas, let’s say that we have declarations id ≡ λx.x id y → y 17 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b ≡(λt.λf .t) a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b ≡(λt.λf .t) a b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b ≡(λt.λf .t) a b →(λf .a) b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b ≡(λt.λf .t) a b →(λf .a) b 18 -
Booleans true ≡ λt.λf .t false ≡ λt.λf .f test
≡ λc.λt.λf .c t f test true a b ≡ (λc.λt.λf .c t f ) true a b → (λt.λf .true t f ) a b → (λf .true a f ) b →true a b ≡(λt.λf .t) a b →(λf .a) b →a 18 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true ≡(λt.λf .t) false true 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true ≡(λt.λf .t) false true 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true ≡(λt.λf .t) false true →(λf .false) true 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true ≡(λt.λf .t) false true →(λf .false) true 19 -
And true ≡ λt.λf .t false ≡ λt.λf .f and
≡ λp.λq.p q p and true false ≡(λp.λq.p q p) true false →(λq.true q true) false →true false true ≡(λt.λf .t) false true →(λf .false) true →false 19 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b ≡ (λx.λy.λz.z x y) a b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b ≡ (λx.λy.λz.z x y) a b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b ≡ (λx.λy.λz.z x y) a b → (λy.λz.z a y) b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b ≡ (λx.λy.λz.z x y) a b → (λy.λz.z a y) b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b ≡ (λx.λy.λz.z x y) a b → (λy.λz.z a y) b → λz.z a b 21 -
Pairs pair ≡ λx.λy.λz.z x y first ≡ (λp.p) λx.λy.x
second ≡ (λp.p) λx.λy.y pairAB ≡ pair a b ≡ (λx.λy.λz.z x y) a b → (λy.λz.z a y) b → λz.z a b ≡ pairab 21 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) → (λx.λy.x) a b 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) → (λx.λy.x) a b 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) → (λx.λy.x) a b → (λy.a) b 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) → (λx.λy.x) a b → (λy.a) b 22 -
Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)
λx.λy.x pairab ≡ λz.z a b first pairab ≡ (λp.p) (λx.λy.x) pairab → pairab (λx.λy.x) ≡ (λz.z a b) (λx.λy.x) → (λx.λy.x) a b → (λy.a) b → a 22 -
Numerals Peano Axioms Every natural number can be defined with
0 and a successor function 0 ≡ λf .λx.x 1 ≡ λf .λx.f x 2 ≡ λf .λx.f (f x) 3 ≡ λf .λx.f (f (f x)) Meaning 0 f is evaluated 0 times 1 f is evaluated once x can be every lambda term 23 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) ≡ λf .λx.f ((λf .λx.f x) f x) Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) ≡ λf .λx.f ((λf .λx.f x) f x) Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) ≡ λf .λx.f ((λf .λx.f x) f x) → λf .λx.f ((λx.f x) x) Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) ≡ λf .λx.f ((λf .λx.f x) f x) → λf .λx.f ((λx.f x) x) Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) ≡ λf .λx.f ((λf .λx.f x) f x) → λf .λx.f ((λx.f x) x) → λf .λx.f (f x) Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡
λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 ≡ (λn.λf .λx.f (n f x)) 1 → λf .λx.f (1 f x) ≡ λf .λx.f ((λf .λx.f x) f x) → λf .λx.f ((λx.f x) x) → λf .λx.f (f x) ≡ 2 Note We use Normal Order Reduction to reduce inside abstractions! 24 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x ≡ λf .λx.(λf .λx.x) f x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x ≡ λf .λx.(λf .λx.x) f x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x ≡ λf .λx.(λf .λx.x) f x → λf .λx.(λx.x) x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x ≡ λf .λx.(λf .λx.x) f x → λf .λx.(λx.x) x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x ≡ λf .λx.(λf .λx.x) f x → λf .λx.(λx.x) x → λf .λx.x 25 -
Numerals Example - 0 + 0 0 ≡ λf .λx.x
plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 ≡ (λm.λn.λf .λx.m f (n f x)) 0 0 → (λn.λf .λx.0 f (n f x)) 0 → λf .λx.0 f (0 f x) ≡ λf .λx.(λf .λx.x) f (0 f x) → λf .λx.(λx.x) (0 f x) → λf .λx.0 f x ≡ λf .λx.(λf .λx.x) f x → λf .λx.(λx.x) x → λf .λx.x ≡ 0 25 -
Thanks • Hope you enjoyed this talk and learned something
new. • Hope it wasn’t too much math and dusty formulas . . . :) 26 -
Good Math "A Geek’s Guide to the Beauty of Numbers,
Logic, and Computation" • Easy to understand -
The Implementation of Functional Programming Languages • How to compile
to the Lambda Calculus? • Out-of-print, but freely available • https://www.microsoft.com/en- us/research/publication/the- implementation-of-functional- programming-languages/ -
Types and Programming Languages • Types systems explained by building
interpreters and proving properties • Very "mathematical", but very complete and self-contained