lambda calculus
play

Lambda Calculus 1 / 43 Outline Introduction and history - PowerPoint PPT Presentation

Lambda Calculus 1 / 43 Outline Introduction and history Definition of lambda calculus Syntax and operational semantics Minutia of -reduction Reduction strategies Programming with lambda calculus Church encodings Recursion De Bruijn


  1. Lambda Calculus 1 / 43

  2. Outline Introduction and history Definition of lambda calculus Syntax and operational semantics Minutia of β -reduction Reduction strategies Programming with lambda calculus Church encodings Recursion De Bruijn indices Introduction and history 2 / 43

  3. What is the lambda calculus? A very simple , but Turing complete , programming language • created before concept of programming language existed! • helped to define what Turing complete means! Lambda calculus syntax Examples x λ x . y x y ( λ x . y) x v ∈ Var ::= x | y | z | . . . λ f . ( λ x . f (x x)) ( λ x . f (x x)) e ∈ Exp ::= v variable reference | e e application | λ v . e (lambda) abstraction Introduction and history 3 / 43

  4. Correspondence to Haskell Lambda calculus is the theoretical foundation for functional programming Lambda calculus Haskell x x f x f x λ x . x \x -> x ( λ f . f x) ( λ y . y) (\f -> f x) (\y -> y) Similar to Haskell with only: variables, application, anonymous functions • amazingly, we don’t lose anything by omitting all of the other features! (for a particular definition of “anything”) Introduction and history 4 / 43

  5. Early history of the lambda calculus Origin of the lambda calculus: • Alonzo Church in 1936, to formalize “computable function” • proves Hilbert’s Entscheidungsproblem undecidable • provide an algorithm to decide truth of arbitrary propositions Meanwhile, in England ... Alonzo Church • young Alan Turing invents the Turing machine • devises halting problem and proves undecidable Turing heads to Princeton, studies under Church • prove lambda calculus, Turing machine, general recursion are equivalent • Church–Turing thesis : these capture all that can be computed Introduction and history 5 / 43

  6. Why lambda? Evolution of notation for a bound variable : • Whitehead and Russell, Principia Mathematica , 1910 • 2 ˆ x + 3 – corresponds to f ( x ) = 2 x + 3 • Church’s early handwritten papers • ˆ x . 2 x + 3 – makes scope of variable explicit • Typesetter #1 • ^ x . 2 x + 3 – couldn’t typeset the circumflex! • Typesetter #2 • λ x . 2 x + 3 – picked a prettier symbol Barendregt, The Impact of the Lambda Calculus in Logic and Computer Science , 1997 Introduction and history 6 / 43

  7. Impact of the lambda calculus Turing machine : theoretical foundation for imperative languages • Fortran, Pascal, C, C++, C#, Java, Python, Ruby, JavaScript, ... Lambda calculus : theoretical foundation for functional languages • Lisp, ML, Haskell, OCaml, Scheme/Racket, Clojure, F#, Coq, ... In programming languages research : • common language of discourse, formal foundation • starting point for new features • extend syntax, type system, semantics • reveals precise impact and utility of feature Introduction and history 7 / 43

  8. Outline Introduction and history Definition of lambda calculus Syntax and operational semantics Minutia of β -reduction Reduction strategies Programming with lambda calculus Church encodings Recursion De Bruijn indices Definition of lambda calculus 8 / 43

  9. Syntax Lambda calculus syntax Syntactic sugar v ∈ Var ::= x | y | z | . . . Multi-parameter functions: λ x . ( λ y . e ) ≡ λ x y . e e ∈ Exp ::= v variable reference λ x . ( λ y . ( λ z . e )) ≡ λ x y z . e | e e application | λ v . e (lambda) abstraction Application is left-associative: ( e 1 e 2 ) e 3 ≡ e 1 e 2 e 3 Abstractions extend as far right as possible (( e 1 e 2 ) e 3 ) e 4 ≡ e 1 e 2 e 3 e 4 so ... λ x . x y ≡ λ x . (x y) e 1 ( e 2 e 3 ) ≡ e 1 ( e 2 e 3 ) NOT ( λ x . x) y Definition of lambda calculus 9 / 43

  10. β -reduction: basic idea e ∈ Exp ::= v | e e | λ v . e A redex is an expression of the form: ( λ v . e 1 ) e 2 (an application with an abstraction on left) Reduce by substituting e 2 for every reference to v in e 1 write this as: [ e 2 / v ] e 1 [ v / e 2 ] e 1 lots of different notations for this! e 1 [ v / e 2 ] e 1 [ v := e 2 ] Simple example [ v �→ e 2 ] e 1 ( λ x . x y x) z �→ z y z Definition of lambda calculus 10 / 43

  11. Operational semantics e ∈ Exp ::= v | e e | λ v . e Reduction semantics e �→ e ′ ( λ v . e 1 ) e 2 �→ [ e 2 / v ] e 1 λ v . e �→ λ v . e ′ e 1 �→ e ′ e 2 �→ e ′ 1 2 e 1 e 2 �→ e ′ e 1 e 2 �→ e 1 e ′ 1 e 2 2 Note: Reduction order is ambiguous! Definition of lambda calculus 11 / 43

  12. Exercise Apply β -reduction in the following expressions Round 1: e ∈ Exp v | e e | λ v . e ::= • ( λ x . x) z • ( λ x y . x) z e �→ e ′ • ( λ x y . x) z u ( λ v . e 1 ) e 2 �→ [ e 2 / v ] e 1 λ v . e �→ λ v . e ′ Round 2: e 1 �→ e ′ e 2 �→ e ′ 1 2 • ( λ x . x x) ( λ y . y) e 1 e 2 �→ e ′ e 1 e 2 �→ e 1 e ′ 1 e 2 • ( λ x . ( λ y . y) z) 2 • ( λ x . (x ( λ y . x))) z Definition of lambda calculus 12 / 43

  13. Outline Introduction and history Definition of lambda calculus Syntax and operational semantics Minutia of β -reduction Reduction strategies Programming with lambda calculus Church encodings Recursion De Bruijn indices Definition of lambda calculus 13 / 43

  14. Variable scoping e ∈ Exp ::= v | e e | λ v . e An abstraction consists of: 1. a variable declaration 2. a function body – the variable can be referenced in here The scope of a declaration: the parts of a program where it can be referenced A reference is bound by its innermost declaration Mini-exercise: ( λ x . e 1 ( λ y . e 2 ( λ x . e 3 ))) ( λ z . e 4 ) • What is the scope of each variable declaration? Definition of lambda calculus 14 / 43

  15. Free and bound variables e ∈ Exp ::= v | e e | λ v . e A variable v is free in e if: • v is referenced in e • the reference is not enclosed in an abstraction declaring v (within e ) If v is referenced and enclosed in such an abstraction, it is bound Closed expression : an expression with no free variables • equivalently, an expression where all variables are bound Definition of lambda calculus 15 / 43

  16. Exercise e ∈ Exp ::= v | e e | λ v . e 1. Define the abstract syntax of lambda calculus as a Haskell data type 2. Define a function: free :: Exp -> Set Var the set of free variables in an expression 3. Define a function: closed :: Exp -> Bool no free variables in an expression Definition of lambda calculus 16 / 43

  17. Potential problem: variable capture Principles of variable bindings: 1. variables should be bound according to their static scope • λ x . ( λ y . ( λ x . y x)) x �→ λ x . λ x . x x 2. how we name bound variables doesn’t really matter • λ x . x ≡ λ y . y ≡ λ z . z ( α -equivalence) If violated, we can’t reason about functions separately from their use! Example with naive substitution A binary function that always returns its first argument: λ x y . x ... or does it? ( λ x y . x) y u �→ ( λ y . y) u �→ u Definition of lambda calculus 17 / 43

  18. Solution: capture-avoiding substitution Capture-avoiding (safe) substitution: [ e / v ] e ′ FV ( e ) is the [ e / v ] v = e set of all free [ e / v ] w = w v � = w variables in e [ e / v ]( e 1 e 2 ) = [ e / v ] e 1 [ e / v ] e 2 [ e / v ]( λ u . e ′ ) = λ w . [ e / v ]([ w / u ] e ′ ) w / ∈ { v } ∪ FV ( λ u . e ′ ) ∪ FV ( e ) Example with safe substitution ( λ x y . x) y u �→ [ y / x ]( λ y . x ) u = ( λ z . [ y / x ]([ z / y ] x ) ) u = ( λ z . [ y / x ] x) u = ( λ z . y) u �→ [ u / z ] y = y Definition of lambda calculus 18 / 43

  19. Example Recall example: λ x . ( λ y . ( λ x . y x)) x �→ λ x . λ x . x x Reduction with safe substitution λ x . ( λ y . ( λ x . y x)) x �→ λ x . [ x / y ]( λ x . y x ) = λ x . λ z . [ x / y ]([ z / x ]( y x )) = λ x . λ z . [ x / y ]( y z ) = λ x . λ z . x z Definition of lambda calculus 19 / 43

  20. Outline Introduction and history Definition of lambda calculus Syntax and operational semantics Minutia of β -reduction Reduction strategies Programming with lambda calculus Church encodings Recursion De Bruijn indices Definition of lambda calculus 20 / 43

  21. Normal form Question: what is a value in the lambda calculus? • how do we know when we’re done reducing? One answer: a value is an expression that contains no redexes • called β -normal form Not all expressions can be reduced to a value! ( λ x . x x) ( λ x . x x) �→ ( λ x . x x) ( λ x . x x) �→ ( λ x . x x) ( λ x . x x) �→ ... Definition of lambda calculus 21 / 43

  22. Does reduction order matter? Recall: operational semantics is ambiguous • in what order should we β -reduce redexes? • does it matter? e �→ e ′ ⊆ Exp × Exp e �→ ∗ e ′ ⊆ Exp × Exp s �→ ∗ s e �→ e ′ ( λ v . e 1 ) e 2 �→ [ e 2 / v ] e 1 λ v . e �→ λ v . e ′ s ′ �→ ∗ s ′′ s �→ s ′ s �→ ∗ s ′′ e 1 �→ e ′ e 2 �→ e ′ 1 2 e 1 e 2 �→ e ′ e 1 e 2 �→ e 1 e ′ 1 e 2 2 Definition of lambda calculus 22 / 43

  23. Church–Rosser Theorem e 1 �→ ∗ �→ ∗ Reduction is confluent If e �→ ∗ e 1 and e �→ ∗ e 2 , then e e ′ ∃ e ′ such that e 1 �→ ∗ e ′ and e 2 �→ ∗ e ′ �→ ∗ �→ ∗ e 2 Corollary : any expression has at most one normal form • if it exists, we can still reach it after any sequence of reductions • ... but if we pick badly, we might never get there! Example: ( λ x . y) (( λ x . x x) ( λ x . x x)) Definition of lambda calculus 23 / 43

Recommend


More recommend