lambda calculus
play

Lambda calculus A starting point for reasoning about functions - PowerPoint PPT Presentation

Lambda calculus A starting point for reasoning about functions The foundation of most functional programming languages, including the Lisp and ML languages v ranges over a countable number of variables e :: = v (variables) | e 1 e 2


  1. Lambda calculus • A starting point for reasoning about functions • The foundation of most functional programming languages, including the Lisp and ML languages v ranges over a countable number of variables e :: = v (variables) | e 1 e 2 (function application) | λv.e (function abstraction) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  2. Evaluation → λf .f 1 λf .f 1 (λf .f ) 1 → 1 (λf .f 1 ) (λx.x + 1 ) → (λx.x + 1 ) 1 → 1 + 1 → 2 (λx.x x) (λy.y y) → (λy.y y) (λy.y y) → (λy.y y) (λy.y y) → (λy.y y) (λy.y y) . . . T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  3. Reduction • Program evaluation uses a substitution semantics. • ``To evaluate an application (λv.e 1 ) e 2 , substi- tute e 2 for v in e 1 .'' • Substitution: e 1 [e 2 /v] means: – Substitute e 2 for v in e 1 – Replace all occurrences of v in e 1 with e 2 • Sometimes written e 1 [v : = e 2 ] . T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  4. Single-step evaluation • A single-step reduction is just a substitution • Called “beta-reduction” (λv.e 1 ) e 2 → β e 1 [e 2 /v] T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  5. Defining substitution • Define substitution e 1 [e 2 /v] by induction on the structure of e 1 – e 1 is a variable v ′ ( v ′ may or may not be the same variable as v ) – e 1 is a function application e 3 e 4 – e 1 is a function abstraction λv ′ .e 3 ( v ′ may or may not be the same variable as v ) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  6. Capture-avoiding substitution if v = v ′ � e 2 • v ′ [e 2 /v] = v ′ otherwise • (e 3 e 4 )[e 2 /v] = (e 3 [e 2 /v] e 4 [e 2 /v]) λv ′ .e 3 if v = v ′   if v ′ �∈ FV (e 2 ) • (λv ′ .e 3 )[e 2 /v] = λv ′ .e 3 [e 2 /v] λv ′′ .e 3 [v ′′ /v ′ ][e 2 /v] otherwise, new v ′′  T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  7. Variables • An occurrence of a variable is a “binding occurrence” if it is defined by a lambda • An occurrence is “bound” if it is in the scope of a binding occurrence with the same name • Other occurrences are “free” T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  8. Free variables Free variables are defined by induction (as usual): = { v } FV (v) FV (e 1 e 2 ) = FV (e 1 ) ∪ FV (e 2 ) FV (λv.e) = FV (e) − { v } FV (λx.λy.x) = {} FV (λx.x + y) = { y } FV (λx.(λy.x) (λz.y)) = { y } T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  9. Some sensible properties • Any two alpha-equivalent terms have the same free variables • Free variables only decrease during evaluation • Beta-reduction works: If e 1 = α e 2 and e 1 → β e 3 , then e 2 → β e 4 and e 3 = α e 4 . T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  10. More terminology (λv.e 1 ) e 2 → β e 1 [e 2 /v] • The left-hand-side (λv.e 1 ) e 2 is called a redex . • The right-hand-side e 1 [e 2 /v] is called the contrac- tum . T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  11. Rules, inductive definitions An inference rule has a set of assumptions above a hor- izontal line, and a single conclusion below the line. assum 1 · · · assum n dummy rule concl T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  12. Multi-step reduction Redex reduction: (λv.e 1 ) e 2 → β e 1 [e 2 /v] redex Beta-reduction can be applied in any context: M → β M ′ M N → β M ′ N app fun N → β N ′ M N → β M N ′ app arg M → β M ′ λv.M → β λv.M ′ fun T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  13. Multi-step reduction β M ref M → ∗ M ′ → ∗ β M β M ′ sym M → ∗ M ′ → ∗ M → ∗ β M ′ β M ′′ trans M → ∗ β M ′′ T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  14. Problems • How is a program evaluated? • What are the values (the result of a program evaluation) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  15. Normal forms • An expression is in normal form if it contains no redexes. • Head normal form: λv 1 . . . λv n .ye 1 · · · e m • Weak head normal form: ye 1 · · · e m T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  16. Programs and normal forms ≡ (λx.x x) (λy.y y) ֓ Y ≡ (λf .(λx.f (xx)) (λx.f (xx)) → ∗ Y f f (Y f ) β Y fact ≡ Y (λf .λi. if i = 0 then 1 else i ∗ (f (i − 1 ))) → λi. if i = 0 then 1 else i ∗ (Y fact (i − 1 )) T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

  17. Normal forms • It is probably best to stop as soon as a functional form is reached • What about evaluation order? • Does evaluation order matter? → (λx. 1 ) ֓ (λx. 1 ) ֓ → (λx. 1 ) ֓ → (λx. 1 ) ֓ → 1 T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Lambda calculus R O O I F L I O L http://www.cs.caltech.edu/courses/cs20/a/ November 7, 2002 A G Y C If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Recommend


More recommend