λ - CALCULUS Alonzo Church ➜ lived 1903–1995 ➜ supervised people like Alan Turing, Stephen Kleene NICTA Advanced Course ➜ famous for Church-Turing thesis, lambda calculus, Theorem Proving first undecidability results Principles, Techniques, Applications Slide 1 Slide 3 ➜ invented λ calculus in 1930’s λ λ -calculus ➜ originally meant as foundation of mathematics ➜ important applications in theoretical computer science ➜ foundation of computability and functional programming C ONTENT UNTYPED λ - CALCULUS ➜ turing complete model of computation ➜ Intro & motivation, getting started with Isabelle ➜ Foundations & Principles ➜ a simple way of writing down functions • Lambda Calculus Basic intuition: • Higher Order Logic, natural deduction instead of f ( x ) = x + 5 • Term rewriting Slide 2 Slide 4 write f = λx. x + 5 ➜ Proof & Specification Techniques λx. x + 5 • Datatypes, recursion, induction ➜ a term • Inductively defined sets, rule induction • Calculational reasoning, mathematics style proofs ➜ a nameless function • Hoare logic, proofs about programs ➜ that adds 5 to its parameter λ - CALCULUS 1 F UNCTION A PPLICATION 2
F UNCTION A PPLICATION For applying arguments to functions instead of f ( x ) write f x Example: ( λx. x + 5) a Slide 5 Slide 7 N OW F ORMAL in ( λx. t ) a replace x by a in t Evaluating: (computation!) Example: ( λx. x + 5) ( a + b ) evaluates to ( a + b ) + 5 S YNTAX t ::= v | c | ( t t ) | ( λx. t ) Terms: v, x ∈ V, c ∈ C, V, C sets of names Slide 6 Slide 8 T HAT ’ S IT ! ➜ v, x variables ➜ c constants ➜ ( t t ) application ➜ ( λx. t ) abstraction 3 C ONVENTIONS 4
C ONVENTIONS C OMPUTATION Intuition: replace parameter by argument ➜ leave out parentheses where possible this is called β -reduction ➜ list variables instead of multiple λ Example Example: instead of ( λy. ( λx. ( x y ))) write λy x. x y Slide 9 Slide 11 ( λx y. f ( y x )) 5 ( λx. x ) − → β Rules: ( λy. f ( y 5)) ( λx. x ) − → β ➜ list variables: λx. ( λy. t ) = λx y. t f (( λx. x ) 5) − → β ➜ application binds to the left: x y z = ( x y ) z � = x ( y z ) f 5 ➜ abstraction binds to the right: λx. x y = λx. ( x y ) � = ( λx. x ) y ➜ leave out outermost parentheses G ETTING USED TO THE S YNTAX D EFINING C OMPUTATION β reduction: Example: λx y z. x z ( y z ) = ( λx. s ) t − → β s [ x ← t ] ( s ′ t ) s − → β s ′ = ⇒ ( s t ) − → β λx y z. ( x z ) ( y z ) = t − → β t ′ = ⇒ ( s t ) − → β ( s t ′ ) Slide 10 Slide 12 λx y z. (( x z ) ( y z )) = s − → β s ′ = ⇒ ( λx. s ) − → β ( λx. s ′ ) λx. λy. λz. (( x z ) ( y z )) = ( λx. ( λy. ( λz. (( x z ) ( y z ))))) Still to do: defi ne s [ x ← t ] C OMPUTATION 5 D EFINING S UBSTITUTION 6
D EFINING S UBSTITUTION S UBSTITUTION x [ x ← t ] = t Easy concept. Small problem: variable capture. y [ x ← t ] = y if x � = y Example: ( λx. x z )[ z ← x ] c [ x ← t ] = c We do not want: ( λx. x x ) as result. ( s 1 s 2 ) [ x ← t ] = ( s 1 [ x ← t ] s 2 [ x ← t ]) Slide 13 Slide 15 What do we want? ( λx. s ) [ x ← t ] = ( λx. s ) ( λy. s ) [ x ← t ] = ( λy. s [ x ← t ]) if x � = y and y / ∈ FV ( t ) In ( λy. y z ) [ z ← x ] = ( λy. y x ) there would be no problem. ( λy. s ) [ x ← t ] = ( λz. s [ y ← z ][ x ← t ]) if x � = y and z / ∈ FV ( t ) ∪ FV ( t ) So, solution is: rename bound variables. F REE V ARIABLES S UBSTITUTION E XAMPLE Bound variables: in ( λx. t ) , x is a bound variable. ( x ( λx. x ) ( λy. z x ))[ x ← y ] Free variables FV of a term: = ( x [ x ← y ]) (( λx. x )[ x ← y ]) (( λy. z x )[ x ← y ]) FV ( x ) = { x } = y ( λx. x ) ( λy ′ . z y ) FV ( c ) = {} Slide 14 Slide 16 FV ( s t ) = FV ( s ) ∪ FV ( t ) FV ( λx. t ) = FV ( t ) \ { x } Example: FV ( λx. ( λy. ( λx. x ) y ) y x ) = { y } Term t is called closed if FV ( t ) = {} α C ONVERSION S UBSTITUTION 7 8
α C ONVERSION B ACK TO β Bound names are irrelevant: We have defi ned β reduction: − → λx. x and λy. y denote the same function. β α conversion: s = α t means s = t up to renaming of bound variables. Some notation and concepts: ➜ β conversion: s = β t iff ∃ n. s − → ∗ β n ∧ t − → ∗ β n Formally: Slide 17 Slide 19 ➜ t is reducible if there is an s such that t − → β s ( λx. t ) − → α ( λy. t [ x ← y ]) if y / ∈ FV ( t ) ( s ′ t ) s − → α s ′ = ⇒ ( s t ) − → α ➜ ( λx. s ) t is called a redex (reducible expression) t − → α t ′ = ⇒ ( s t ) − → α ( s t ′ ) ➜ t is reducible iff it contains a redex s − → α s ′ = ⇒ ( λx. s ) − → α ( λx. s ′ ) ➜ if it is not reducible, t is in normal form ➜ t has a normal form if there is an irreducible s such that t − → ∗ β s s = α t iff s − → ∗ α t ( − → ∗ α = transitive, reflexive closure of − → α = multiple steps) α C ONVERSION D OES EVERY λ TERM HAVE A NORMAL FORM ? Equality in Isabelle is equality modulo α conversion: No! if s = α t then s and t are syntactically equal. Example: Examples: ( λx. x x ) ( λx. x x ) − → β x ( λx y. x y ) Slide 18 Slide 20 ( λx. x x ) ( λx. x x ) − → β = α x ( λy x. y x ) ( λx. x x ) ( λx. x x ) − → β . . . = α x ( λz y. z y ) � = α z ( λz y. z y ) (but: ( λx y. y ) (( λx. x x ) ( λx. x x )) − → β λy. y ) � = α x ( λx x. x x ) λ calculus is not terminating B ACK TO β β REDUCTION IS CONFLUENT 9 10
η C ONVERSION β REDUCTION IS CONFLUENT Another case of trivially equal functions: t = ( λx. t x ) Confluence: s − → ∗ β x ∧ s − → ∗ β y = ⇒ ∃ t. x − → ∗ β t ∧ y − → ∗ β t Defi nition: ( λx. t x ) − → η t if x / ∈ FV ( t ) ( s ′ t ) s − → η s ′ = ⇒ ( s t ) − → η s t − → η t ′ = ⇒ ( s t ) − → η ( s t ′ ) ∗ ∗ s − → η s ′ = ⇒ ( λx. s ) − → η ( λx. s ′ ) Slide 21 Slide 23 x y → ∗ → ∗ s = η t iff ∃ n. s − η n ∧ t − η n ∗ ∗ Example: ( λx. f x ) ( λy. g y ) − → η ( λx. f x ) g − → η f g t ➜ η reduction is confluent and terminating. ➜ − → βη is confluent. Order of reduction does not matter for result − → βη means − → β and − → η steps are both allowed. Normal forms in λ calculus are unique ➜ Equality in Isabelle is also modulo η conversion. β REDUCTION IS CONFLUENT I N FACT ... Example: Equality in Isabelle is modulo α , β , and η conversion. ( λx y. y ) (( λx. x x ) a ) − → β ( λx y. y ) ( a a ) − → β λy. y ( λx y. y ) (( λx. x x ) a ) − → β λy. y We will see next lecture why that is possible. Slide 22 Slide 24 η C ONVERSION 11 S O , WHAT CAN YOU DO WITH λ CALCULUS ? 12
S O , WHAT CAN YOU DO WITH λ CALCULUS ? F IX P OINTS λ calculus is very expressive, you can encode: ( λx f. f ( x x f )) ( λx f. f ( x x f )) t − → β ➜ logic, set theory ( λf. f (( λx f. f ( x x f )) ( λx f. f ( x x f )) f )) t − → β ➜ turing machines, functional programs, etc. t (( λx f. f ( x x f )) ( λx f. f ( x x f )) t ) Examples: Slide 25 Slide 27 ≡ λx y. x if true x y − → ∗ β x true µ = ( λxf. f ( x x f )) ( λxf. f ( x x f )) false ≡ λx y. y if false x y − → ∗ β y µ t − → β t ( µ t ) − → β t ( t ( µ t )) − → β t ( t ( t ( µ t ))) − → β . . . ≡ λz x y. z x y if ( λxf. f ( x x f )) ( λxf. f ( x x f )) is Turing’s fi x point operator 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 M ORE E XAMPLES N ICE , BUT ... As a mathematical foundation, λ does not work. It is inconsistent. Encoding natural numbers (Church Numerals) 0 ≡ λf x. x ➜ Frege (Predicate Logic, ∼ 1879): allows arbitrary quantification over predicates 1 ≡ λf x. f x ➜ Russel (1901): Paradox R ≡ { X | X / ∈ X } 2 ≡ λf x. f ( f x ) 3 ≡ λf x. f ( f ( f x )) ➜ Whitehead & Russel (Principia Mathematica, 1910-1913): Slide 26 Slide 28 Fix the problem . . . ➜ Church (1930): λ calculus as logic, true , false , ∧ , . . . as λ terms Numeral n is takes arguments f and x , applies f n -times to x . iszero ≡ λn. n ( λx. false ) true Problem: with { x | P x } ≡ λx. P x x ∈ M ≡ M x ≡ λn f x. f ( n f x ) succ you can write R ≡ λx. not ( x x ) ≡ λm n. λf x. m f ( n f x ) add and get ( R R ) = β not ( R R ) F IX P OINTS 13 W E HAVE LEARNED SO FAR ... 14
W E HAVE LEARNED SO FAR ... E XERCISES ➜ λ calculus syntax ➜ Play around with the syntax. Enter a number of λ terms into Isabelle. ➜ free variables, substitution ➜ Not all λ terms are accepted by Isabelle. Which are not? Why? ➜ β reduction ➜ Evaluate the substitution ( y ( λv. x v ))[ x ← ( λy. v y )] on paper. ➜ α and η conversion ➜ Reduce ( λn. λf x. f ( n f x )) (( λn. λf x. f ( n f x )) ( λf x. x )) to its β Slide 29 Slide 31 normal form on paper and in Isabelle. ➜ β reduction is confluent ➜ Pairs in λ calculus: define functions fs , sn , and pair such that ➜ λ calculus is very expressive (turing complete) fs ( pair a b ) − → ∗ β a and sn ( pair a b ) − → ∗ β b ➜ λ calculus is inconsistent ➜ What can be done to fix the inconsistency in λ calculus? Slide 30 I SABELLE D EMO E XERCISES 15 E XERCISES 16
Recommend
More recommend