Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Untyped Lambda Calculus

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
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. Why should I care? • Simple Computational Model • to

    describe structure and behaviour (E.g. Operational Semantics, Type Systems) • to reason and prove 2
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Example - (λx.λy.x y) a b (λx. λy.x y) a

    b Substitute x → a → (λy.a y) b 5
  10. Example - (λx.λy.x y) a b (λx. λy.x y) a

    b Substitute x → a → (λy.a y) b Substitute y → b 5
  11. Example - (λx.λy.x y) a b (λx. λy.x y) a

    b Substitute x → a → (λy.a y) b Substitute y → b 5
  12. 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
  13. 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
  14. 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
  15. 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
  16. Higher Order Functions • Functions that take or return functions

    • Are there "by definition" λx.x Abstraction λy.y Abstraction Application → λy.y Abstraction 8
  17. Currying Idea • Take a function with n arguments •

    Create a function that takes one argument and returns a function with n − 1 arguments 9
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. Operational Semantics • We learned how to write down and

    talk about Lambda Calculus Terms • How to evaluate them? • Different Strategies • Interesting outcomes 12
  24. Full Beta-Reduction • RedEx • Reducible Expression • Always an

    Application (λx.x) ((λy.y) (λz. (λa.a) z RedEx ) RedEx ) RedEx 13
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. Booleans true ≡ λt.λf .t false ≡ λt.λf .f test

    ≡ λc.λt.λf .c t f test true a b 18
  45. Booleans true ≡ λt.λf .t false ≡ λt.λf .f test

    ≡ λc.λt.λf .c t f test true a b 18
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. And true ≡ λt.λf .t false ≡ λt.λf .f and

    ≡ λp.λq.p q p and true false 19
  60. And true ≡ λt.λf .t false ≡ λt.λf .f and

    ≡ λp.λq.p q p and true false 19
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. 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
  78. 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
  79. 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
  80. Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)

    λx.λy.x pairab ≡ λz.z a b first pairab 22
  81. Pairs (continued) pair ≡ λx.λy.λz.z x y first ≡ (λp.p)

    λx.λy.x pairab ≡ λz.z a b first pairab 22
  82. 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
  83. 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
  84. 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
  85. 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
  86. 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
  87. 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
  88. 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
  89. 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
  90. 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
  91. 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
  92. 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
  93. 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
  94. Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡

    λf .λx.f x successor ≡ λn.λf .λx.f (n f x) 24
  95. Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡

    λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 24
  96. Numerals Example - Successor 0 ≡ λf .λx.x 1 ≡

    λf .λx.f x successor ≡ λn.λf .λx.f (n f x) successor 1 24
  97. 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
  98. 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
  99. 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
  100. 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
  101. 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
  102. 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
  103. 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
  104. 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
  105. 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
  106. 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
  107. Numerals Example - 0 + 0 0 ≡ λf .λx.x

    plus ≡ λm.λn.λf .λx.m f (n f x) 25
  108. Numerals Example - 0 + 0 0 ≡ λf .λx.x

    plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 25
  109. Numerals Example - 0 + 0 0 ≡ λf .λx.x

    plus ≡ λm.λn.λf .λx.m f (n f x) plus 0 0 25
  110. 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
  111. 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
  112. 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
  113. 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
  114. 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
  115. 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
  116. 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
  117. 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
  118. 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
  119. 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
  120. 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
  121. 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
  122. 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
  123. 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
  124. 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
  125. 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
  126. 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
  127. 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
  128. End

  129. Thanks • Hope you enjoyed this talk and learned something

    new. • Hope it wasn’t too much math and dusty formulas . . . :) 26
  130. Good Math "A Geek’s Guide to the Beauty of Numbers,

    Logic, and Computation" • Easy to understand
  131. 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/
  132. Types and Programming Languages • Types systems explained by building

    interpreters and proving properties • Very "mathematical", but very complete and self-contained