comp2111 week 5 term 1 2020 lambda calculus
play

COMP2111 Week 5 Term 1, 2020 Lambda Calculus 1 -calculus Alonzo - PowerPoint PPT Presentation

COMP2111 Week 5 Term 1, 2020 Lambda Calculus 1 -calculus Alonzo Church lived 19031995 supervised people like Alan Turing, Stephen Kleene famous for Church-Turing thesis, lambda calculus, first undecidability results invented


  1. Substitution x [ x ← t ] = t y [ x ← t ] = y if x � = y c [ x ← t ] = c ( s 1 s 2 ) [ x ← t ] = ( s 1 [ x ← t ] s 2 [ x ← t ]) ( λ x . s ) [ x ← t ] = ( λ x . s ) ( λ y . s ) [ x ← t ] = ( λ y . s [ x ← t ]) if x � = y and y / ∈ FV ( t ) ( λ y . s ) [ x ← t ] = ( λ z . s [ y ← z ][ x ← t ]) if x � = y and z / ∈ FV ( t ) ∪ FV ( s ) 38

  2. Substitution x [ x ← t ] = t y [ x ← t ] = y if x � = y c [ x ← t ] = c ( s 1 s 2 ) [ x ← t ] = ( s 1 [ x ← t ] s 2 [ x ← t ]) ( λ x . s ) [ x ← t ] = ( λ x . s ) ( λ y . s ) [ x ← t ] = ( λ y . s [ x ← t ]) if x � = y and y / ∈ FV ( t ) ( λ y . s ) [ x ← t ] = ( λ z . s [ y ← z ][ x ← t ]) if x � = y and z / ∈ FV ( t ) ∪ FV ( s ) 39

  3. Substitution x [ x ← t ] = t y [ x ← t ] = y if x � = y c [ x ← t ] = c ( s 1 s 2 ) [ x ← t ] = ( s 1 [ x ← t ] s 2 [ x ← t ]) ( λ x . s ) [ x ← t ] = ( λ x . s ) ( λ y . s ) [ x ← t ] = ( λ y . s [ x ← t ]) if x � = y and y / ∈ FV ( t ) ( λ y . s ) [ x ← t ] = ( λ z . s [ y ← z ][ x ← t ]) if x � = y and z / ∈ FV ( t ) ∪ FV ( s ) 40

  4. Substitution x [ x ← t ] = t y [ x ← t ] = y if x � = y c [ x ← t ] = c ( s 1 s 2 ) [ x ← t ] = ( s 1 [ x ← t ] s 2 [ x ← t ]) ( λ x . s ) [ x ← t ] = ( λ x . s ) ( λ y . s ) [ x ← t ] = ( λ y . s [ x ← t ]) if x � = y and y / ∈ FV ( t ) ( λ y . s ) [ x ← t ] = ( λ z . s [ y ← z ][ x ← t ]) if x � = y and z / ∈ FV ( t ) ∪ FV ( s ) 41

  5. Substitution x [ x ← t ] = t y [ x ← t ] = y if x � = y c [ x ← t ] = c ( s 1 s 2 ) [ x ← t ] = ( s 1 [ x ← t ] s 2 [ x ← t ]) ( λ x . s ) [ x ← t ] = ( λ x . s ) ( λ y . s ) [ x ← t ] = ( λ y . s [ x ← t ]) if x � = y and y / ∈ FV ( t ) ( λ y . s ) [ x ← t ] = ( λ z . s [ y ← z ][ x ← t ]) if x � = y and z / ∈ FV ( t ) ∪ FV ( s ) 42

  6. Substitution Example ( x ( λ x . x ) ( λ y . z x ))[ x ← y ] = ( x [ x ← y ]) (( λ x . x )[ x ← y ]) (( λ y . z x )[ x ← y ]) = y ( λ x . x ) ( λ y ′ . z y ) 43

  7. Substitution Example ( x ( λ x . x ) ( λ y . z x ))[ x ← y ] = ( x [ x ← y ]) (( λ x . x )[ x ← y ]) (( λ y . z x )[ x ← y ]) = y ( λ x . x ) ( λ y ′ . z y ) 44

  8. Substitution Example ( x ( λ x . x ) ( λ y . z x ))[ x ← y ] = ( x [ x ← y ]) (( λ x . x )[ x ← y ]) (( λ y . z x )[ x ← y ]) = y ( λ x . x ) ( λ y ′ . z y ) 45

  9. α Conversion Bound names are irrelevant: λ x . x and λ y . y denote the same function. α conversion: s = α t means s = t up to renaming of bound variables. 46

  10. α Conversion Bound names are irrelevant: λ x . x and λ y . y denote the same function. α conversion: s = α t means s = t up to renaming of bound variables. Formally: ( λ x . t ) − → α ( λ y . t [ x ← y ]) if y / ∈ FV ( t ) ( s ′ t ) − → α s ′ = ⇒ ( s t ) − → α s t ′ ( s t ′ ) t − → α = ⇒ ( s t ) − → α − → α s ′ = ⇒ ( λ x . s ) − → α ( λ x . s ′ ) s 47

  11. α Conversion Bound names are irrelevant: λ x . x and λ y . y denote the same function. α conversion: s = α t means s = t up to renaming of bound variables. Formally: ( λ x . t ) − → α ( λ y . t [ x ← y ]) if y / ∈ FV ( t ) ( s ′ t ) s − → α s ′ = ⇒ ( s t ) − → α t ′ ( s t ′ ) t − → α = ⇒ ( s t ) − → α − → α s ′ = ⇒ ( λ x . s ) − → α ( λ x . s ′ ) s s = α t iff s − → ∗ α t ( − → ∗ α = transitive, reflexive closure of − → α = multiple steps) 48

  12. α Conversion Examples: x ( λ x y . x y ) = α x ( λ y x . y x ) = α x ( λ z y . z y ) � = α z ( λ z y . z y ) � = α x ( λ x x . x x ) 49

  13. α Conversion Examples: x ( λ x y . x y ) = α x ( λ y x . y x ) = α x ( λ z y . z y ) � = α z ( λ z y . z y ) � = α x ( λ x x . x x ) 50

  14. α Conversion Examples: x ( λ x y . x y ) = α x ( λ y x . y x ) = α x ( λ z y . z y ) � = α z ( λ z y . z y ) � = α x ( λ x x . x x ) 51

  15. α Conversion Examples: x ( λ x y . x y ) = α x ( λ y x . y x ) = α x ( λ z y . z y ) � = α z ( λ z y . z y ) � = α x ( λ x x . x x ) 52

  16. α Conversion Examples: x ( λ x y . x y ) = α x ( λ y x . y x ) = α x ( λ z y . z y ) � = α z ( λ z y . z y ) � = α x ( λ x x . x x ) 53

  17. Back to β We have defined β reduction: − → β Some notation and concepts: β conversion: s = β t iff ∃ n . s − → ∗ β n ∧ t − → ∗ β n t is reducible if there is an s such that t − → β s ( λ x . s ) t is called a redex (reducible expression) t is reducible iff it contains a redex if it is not reducible, t is in normal form 54

  18. Back to β We have defined β reduction: − → β Some notation and concepts: β conversion: s = β t iff ∃ n . s − → ∗ β n ∧ t − → ∗ β n t is reducible if there is an s such that t − → β s ( λ x . s ) t is called a redex (reducible expression) t is reducible iff it contains a redex if it is not reducible, t is in normal form 55

  19. Back to β We have defined β reduction: − → β Some notation and concepts: β conversion: s = β t iff ∃ n . s − → ∗ β n ∧ t − → ∗ β n t is reducible if there is an s such that t − → β s ( λ x . s ) t is called a redex (reducible expression) t is reducible iff it contains a redex if it is not reducible, t is in normal form 56

  20. Back to β We have defined β reduction: − → β Some notation and concepts: β conversion: s = β t iff ∃ n . s − → ∗ β n ∧ t − → ∗ β n t is reducible if there is an s such that t − → β s ( λ x . s ) t is called a redex (reducible expression) t is reducible iff it contains a redex if it is not reducible, t is in normal form 57

  21. Back to β We have defined β reduction: − → β Some notation and concepts: β conversion: s = β t iff ∃ n . s − → ∗ β n ∧ t − → ∗ β n t is reducible if there is an s such that t − → β s ( λ x . s ) t is called a redex (reducible expression) t is reducible iff it contains a redex if it is not reducible, t is in normal form 58

  22. Does every λ term have a normal form? No! Example: ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β . . . (but: ( λ x y . y ) (( λ x . x x ) ( λ x . x x )) − → β λ y . y ) λ calculus is not terminating 59

  23. Does every λ term have a normal form? No! Example: ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β . . . (but: ( λ x y . y ) (( λ x . x x ) ( λ x . x x )) − → β λ y . y ) λ calculus is not terminating 60

  24. Does every λ term have a normal form? No! Example: ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β . . . (but: ( λ x y . y ) (( λ x . x x ) ( λ x . x x )) − → β λ y . y ) λ calculus is not terminating 61

  25. Does every λ term have a normal form? No! Example: ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β . . . (but: ( λ x y . y ) (( λ x . x x ) ( λ x . x x )) − → β λ y . y ) λ calculus is not terminating 62

  26. Does every λ term have a normal form? No! Example: ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β ( λ x . x x ) ( λ x . x x ) − → β . . . (but: ( λ x y . y ) (( λ x . x x ) ( λ x . x x )) − → β λ y . y ) λ calculus is not terminating 63

  27. β reduction is confluent s − → ∗ β x ∧ s − → ∗ β y = ⇒ ∃ t . x − → ∗ β t ∧ y − → ∗ Confluence: β t s ∗ ∗ y x ∗ ∗ t 64

  28. β reduction is confluent Confluence: s − → ∗ β x ∧ s − → ∗ β y = ⇒ ∃ t . x − → ∗ β t ∧ y − → ∗ β t s ∗ ∗ y x ∗ ∗ t Order of reduction does not matter for result Normal forms in λ calculus are unique 65

  29. β reduction is confluent Example: ( λ x y . y ) (( λ x . x x ) a ) − → β ( λ x y . y ) ( a a ) − → β λ y . y ( λ x y . y ) (( λ x . x x ) a ) − → β λ y . y 66

  30. β reduction is confluent Example: ( λ x y . y ) (( λ x . x x ) a ) − → β ( λ x y . y ) ( a a ) − → β λ y . y ( λ x y . y ) (( λ x . x x ) a ) − → β λ y . y 67

  31. β reduction is confluent Example: ( λ x y . y ) (( λ x . x x ) a ) − → β ( λ x y . y ) ( a a ) − → β λ y . y ( λ x y . y ) (( λ x . x x ) a ) − → β λ y . y 68

  32. So, what can you do with λ calculus? λ calculus is very expressive, you can encode: logic, set theory turing machines, functional programs, etc. Examples: Now, not , and , or , etc is easy: 69

  33. So, what can you do with λ calculus? λ calculus is very expressive, you can encode: logic, set theory turing machines, functional programs, etc. Examples: ≡ λ x y . x if true x y − → ∗ β x true false ≡ λ x y . y if false x y − → ∗ β y ≡ λ z x y . z x y if Now, not , and , or , etc is easy: 70

  34. So, what can you do with λ calculus? λ calculus is very expressive, you can encode: logic, set theory turing machines, functional programs, etc. Examples: ≡ λ x y . x if true x y − → ∗ β x true false ≡ λ x y . y if false x y − → ∗ β y ≡ λ z x y . z x y if Now, not , and , or , etc is easy: 71

  35. So, what can you do with λ calculus? λ calculus is very expressive, you can encode: logic, set theory turing machines, functional programs, etc. Examples: ≡ λ x y . x if true x y − → ∗ β x true false ≡ λ x y . y if false x y − → ∗ β y ≡ λ z x y . z x y if Now, not , and , or , etc is easy: 72

  36. So, what can you do with λ calculus? λ calculus is very expressive, you can encode: logic, set theory turing machines, functional programs, etc. Examples: ≡ λ x y . x if true x y − → ∗ β x true → ∗ false ≡ λ x y . y if false x y − β y ≡ λ z x y . z x y if Now, not , and , or , etc is easy: not ≡ λ x . if x false true and ≡ λ x y . if x y false ≡ λ x y . if x true y or 73

  37. Fix Points ( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t − → β ( λ f . f (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) f )) t − → β t (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t ) ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) is Turing’s fix point operator 74

  38. Fix Points ( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t − → β ( λ f . f (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) f )) t − → β t (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t ) ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) is Turing’s fix point operator 75

  39. Fix Points ( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t − → β ( λ f . f (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) f )) t − → β t (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t ) ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) is Turing’s fix point operator 76

  40. Fix Points ( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t − → β ( λ f . f (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) f )) t − → β t (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t ) µ = ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) µ t − → β t ( µ t ) − → β t ( t ( µ t )) − → β t ( t ( t ( µ t ))) − → β . . . ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) is Turing’s fix point operator 77

  41. Fix Points ( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t − → β ( λ f . f (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) f )) t − → β t (( λ x f . f ( x x f )) ( λ x f . f ( x x f )) t ) µ = ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) µ t − → β t ( µ t ) − → β t ( t ( µ t )) − → β t ( t ( t ( µ t ))) − → β . . . ( λ xf . f ( x x f )) ( λ xf . f ( x x f )) is Turing’s fix point operator 78

  42. Nice, but ... As a mathematical foundation, λ does not work. It is inconsistent. Problem: with { x | P x } ≡ λ x . P x x ∈ M ≡ M x you can write R ≡ λ x . not ( x x ) and get ( R R ) = β not ( R R ) because ( R R ) = ( λ x . not ( x x )) R − → β not ( R R ) 79

  43. Nice, but ... As a mathematical foundation, λ does not work. It is inconsistent. Frege (Predicate Logic, ∼ 1879): allows arbitrary quantification over predicates Russell (1901): Paradox R ≡ { X | X / ∈ X } Whitehead & Russell (Principia Mathematica, 1910-1913): Fix the problem Church (1930): λ calculus as logic, true , false , ∧ , . . . as λ terms Problem: with { x | P x } ≡ λ x . P x x ∈ M ≡ M x you can write R ≡ λ x . not ( x x ) and get ( R R ) = β not ( R R ) because ( R R ) = ( λ x . not ( x x )) R − → β not ( R R ) 80

  44. Nice, but ... As a mathematical foundation, λ does not work. It is inconsistent. Frege (Predicate Logic, ∼ 1879): allows arbitrary quantification over predicates Russell (1901): Paradox R ≡ { X | X / ∈ X } Whitehead & Russell (Principia Mathematica, 1910-1913): Fix the problem Church (1930): λ calculus as logic, true , false , ∧ , . . . as λ terms Problem: with { x | P x } ≡ λ x . P x x ∈ M ≡ M x you can write R ≡ λ x . not ( x x ) and get ( R R ) = β not ( R R ) because ( R R ) = ( λ x . not ( x x )) R − → β not ( R R ) 81

  45. Nice, but ... As a mathematical foundation, λ does not work. It is inconsistent. Frege (Predicate Logic, ∼ 1879): allows arbitrary quantification over predicates Russell (1901): Paradox R ≡ { X | X / ∈ X } Whitehead & Russell (Principia Mathematica, 1910-1913): Fix the problem Church (1930): λ calculus as logic, true , false , ∧ , . . . as λ terms Problem: with { x | P x } ≡ λ x . P x x ∈ M ≡ M x you can write R ≡ λ x . not ( x x ) and get ( R R ) = β not ( R R ) because ( R R ) = ( λ x . not ( x x )) R − → β not ( R R ) 82

  46. Nice, but ... As a mathematical foundation, λ does not work. It is inconsistent. Frege (Predicate Logic, ∼ 1879): allows arbitrary quantification over predicates Russell (1901): Paradox R ≡ { X | X / ∈ X } Whitehead & Russell (Principia Mathematica, 1910-1913): Fix the problem Church (1930): λ calculus as logic, true , false , ∧ , . . . as λ terms Problem: with { x | P x } ≡ λ x . P x x ∈ M ≡ M x you can write R ≡ λ x . not ( x x ) and get ( R R ) = β not ( R R ) because ( R R ) = ( λ x . not ( x x )) R − → β not ( R R ) 83

  47. Nice, but ... As a mathematical foundation, λ does not work. It is inconsistent. Frege (Predicate Logic, ∼ 1879): allows arbitrary quantification over predicates Russell (1901): Paradox R ≡ { X | X / ∈ X } Whitehead & Russell (Principia Mathematica, 1910-1913): Fix the problem Church (1930): λ calculus as logic, true , false , ∧ , . . . as λ terms Problem: with { x | P x } ≡ λ x . P x x ∈ M ≡ M x you can write R ≡ λ x . not ( x x ) and get ( R R ) = β not ( R R ) because ( R R ) = ( λ x . not ( x x )) R − → β not ( R R ) 84

  48. Summary λ calculus syntax free variables, substitution α conversion β reduction β reduction is confluent λ calculus is very expressive (Turing complete) λ calculus is inconsistent (as a logic) 85

  49. Exercise Reduce ( λ x . y ( λ v . x v )) ( λ y . v y ) to β normal form. 86

  50. λ calculus is inconsistent Can find term R such that R R = β not ( R R ) There are more terms that do not make sense: true false , etc. Solution : rule out ill-formed terms by using types. (Church 1940) 87

  51. λ calculus is inconsistent Can find term R such that R R = β not ( R R ) There are more terms that do not make sense: true false , etc. Solution : rule out ill-formed terms by using types. (Church 1940) 88

  52. Introducing types Idea: assign a type to each “sensible” λ term. Examples: for term t has type α write t :: α if x has type α then λ x . x is a function from α to α Write: ( λ x . x ) :: α ⇒ α for s t to be sensible: s must be a function t must be right type for parameter If s :: α ⇒ β and t :: α then ( s t ) :: β 89

  53. Introducing types Idea: assign a type to each “sensible” λ term. Examples: for term t has type α write t :: α if x has type α then λ x . x is a function from α to α Write: ( λ x . x ) :: α ⇒ α for s t to be sensible: s must be a function t must be right type for parameter If s :: α ⇒ β and t :: α then ( s t ) :: β 90

  54. Introducing types Idea: assign a type to each “sensible” λ term. Examples: for term t has type α write t :: α if x has type α then λ x . x is a function from α to α Write: ( λ x . x ) :: α ⇒ α for s t to be sensible: s must be a function t must be right type for parameter If s :: α ⇒ β and t :: α then ( s t ) :: β 91

  55. Introducing types Idea: assign a type to each “sensible” λ term. Examples: for term t has type α write t :: α if x has type α then λ x . x is a function from α to α Write: ( λ x . x ) :: α ⇒ α for s t to be sensible: s must be a function t must be right type for parameter If s :: α ⇒ β and t :: α then ( s t ) :: β 92

  56. That’s it! Now Formally 93

  57. That’s it! Now Formally 94

  58. Syntax for λ → ::= v | c | ( t t ) | ( λ x . t ) t Terms: v , x ∈ V , c ∈ C , V , C sets of names ::= b | ν | τ ⇒ τ Types: τ b ∈ { bool , int , . . . } base types ν ∈ { α, β, . . . } type variables α ⇒ β ⇒ γ = α ⇒ ( β ⇒ γ ) Context Γ : Γ: function from variable and constant names to types. Term t has type τ in context Γ: Γ ⊢ t :: τ 95

  59. Syntax for λ → ::= v | c | ( t t ) | ( λ x . t ) t Terms: v , x ∈ V , c ∈ C , V , C sets of names ::= b | ν | τ ⇒ τ Types: τ b ∈ { bool , int , . . . } base types ν ∈ { α, β, . . . } type variables α ⇒ β ⇒ γ = α ⇒ ( β ⇒ γ ) Context Γ : Γ: function from variable and constant names to types. Term t has type τ in context Γ: Γ ⊢ t :: τ 96

  60. Syntax for λ → ::= v | c | ( t t ) | ( λ x . t ) t Terms: v , x ∈ V , c ∈ C , V , C sets of names ::= b | ν | τ ⇒ τ Types: τ b ∈ { bool , int , . . . } base types ν ∈ { α, β, . . . } type variables α ⇒ β ⇒ γ = α ⇒ ( β ⇒ γ ) Context Γ : Γ: function from variable and constant names to types. Term t has type τ in context Γ: Γ ⊢ t :: τ 97

  61. Examples Γ ⊢ ( λ x . x ) :: α ⇒ α [ y ← int ] ⊢ y :: int [ z ← bool ] ⊢ ( λ y . y ) z :: bool [] ⊢ λ f x . f x :: ( α ⇒ β ) ⇒ α ⇒ β 98

  62. Examples Γ ⊢ ( λ x . x ) :: α ⇒ α [ y ← int ] ⊢ y :: int [ z ← bool ] ⊢ ( λ y . y ) z :: bool [] ⊢ λ f x . f x :: ( α ⇒ β ) ⇒ α ⇒ β 99

  63. Examples Γ ⊢ ( λ x . x ) :: α ⇒ α [ y ← int ] ⊢ y :: int [ z ← bool ] ⊢ ( λ y . y ) z :: bool [] ⊢ λ f x . f x :: ( α ⇒ β ) ⇒ α ⇒ β 100

Recommend


More recommend