References A Crash Course on the λ -Calculus J. Roger Hindley, Basic Simple Type Theory , Cambridge, 1997. Jim Royer D. Scott, A type-theoretical alternative to ISWIM, CUCH, OWHY, Theoretical Computer Science 121 (1993) 411–440. Originally written in 1969. Types Seminar January 21, 2014 Jim Royer A Crash Course on the λ -Calculus 1 / 24 Jim Royer A Crash Course on the λ -Calculus 2 / 24 The λ -calculus Subterms, occurrences, free, and bound subterms ( x ) = { x } Church (with Kleene and Rosser) in the 1930s. subterms ( ( λ x . M ) ) = { λ x . M } ∪ subterms ( M ) A family of prototype programming languages. subterms ( ( M N ) ) = { ( M N ) } ∪ subterms ( M ) ∪ subterms ( N ) Functional and higher-order. An occurrence of a subterm is . . . roughly what you think it is . . . The pure λ -calculus ≡ no atomic constants. λ x . M Untyped (or uni-typed) considered first. λ x : an abstractor and x (in λ x ): binding occurrence of x Syntax: M : the body or scope of the abstractor λ x . E :: = X variables/atoms/. .. Free and bound occurrences and variables | ( E 1 E 2 ) applications An occurrence of variable x in M is bound in M iff the occurrence is in the | ( λ X . E ) abstractions scope of some λ x in M ; o/w the occurrence is said to be free . Associativity: A variable x is bound in M iff an λ x occurs in M . λ xyz . M ≡ ( λ x . ( λ y . ( λ z . M ))) A variable x is free in M iff M contains a free occurrence of x . MNPQ ≡ ((( MN ) P ) Q ) FV ( M ) = the set of variables free in M . Jim Royer A Crash Course on the λ -Calculus 3 / 24 Jim Royer A Crash Course on the λ -Calculus 4 / 24
The full, fussy rules for substitution α -reduction Change of bound variables: λ x . M ≡ α λ y . [ y / x ] M for y / ∈ FV ( M ) . P α -converts to Q ( P ≡ α Q ) iff P changes to Q by some finite number of change of bound variables. M has a bound variable clash when M contains an abstractor λ x and an occurrence of x not in this abstractor’s scope. Lemma Every term can be α -converted to a term without bound variable clashes. And why all the fuss? Jim Royer A Crash Course on the λ -Calculus 5 / 24 Jim Royer A Crash Course on the λ -Calculus 6 / 24 Closed terms β -reduction ( λ x . M ) N : β -redex [ N / x ] M : its contractum A closed term or combinator is a term in which no variable occurs free. ( λ x . M ) N ✄ 1 β [ N / x ] M : its re-write rule Common Combinators If P contains an occurrence R = ( λ x . M ) N and Q is the result of replacing this with [ N / x ] M , B ′ ≡ λ xyz . y ( xz ) then P β -contracts to Q (written: P ✄ 1 β Q ); B ≡ λ xyz . x ( yz ) � P , R , Q � : a β -contraction of P . C ≡ λ xyz . xzy I ≡ λ x . x A β -reduction of P is a sequence (finite or infinite) K ≡ λ xy . x S ≡ λ xyz . xz ( yz ) W ≡ λ xy . xyy Y ≡ λ x . ( λ y . x ( yy ))( λ y . x ( yy )) � P , R 1 , P 2 � , � P 2 , R 2 , P 3 � , . . . 0 ≡ λ sz . z 1 ≡ λ sz . sz P β -reduces to Q (written: P ✄ β Q ) means the sequence ends with Q . n ≡ λ sz . s ( s ( . . . ( s z ) . . . )) β -expansion : a reverse β -contraction. � �� � n -many s ’s P = β Q means: one can convert P to Q by a series of β -contractions and β -expansions. Jim Royer A Crash Course on the λ -Calculus 7 / 24 Jim Royer A Crash Course on the λ -Calculus 8 / 24
Confluence (aka Church-Rosser) and normal forms η -reductions Theorem (Church-Rosser) (a) If M ✄ β P and M ✄ β Q, then for some T, P ✄ β T and Q ✄ β T. (b) If P = β Q, then for some T, P ✄ β T and Q ✄ β T. An η -redex is a term of the form λ x . ( Mx ) with x / ∈ FV ( M ) . λ x . ( Mx ) ✄ η M : the re-write rule for η -redeces. A β -normal form term is one without any β -redex. η -contracts, η -reduces, η -converts, η -normal form, ...are defined N is a β -normal form of M when N is in β -n.f. and M ✄ β N . analogously to the β -notions. β -normal forms ≈ result of computations. Church-Rosser extends to η - and βη -reductions. Not all terms have β -nf’s, e.g., (( λ x . xx ) ( λ x . xx )) Corollary If a term has a β -normal form, it is unique (up to α -equivalence). Jim Royer A Crash Course on the λ -Calculus 9 / 24 Jim Royer A Crash Course on the λ -Calculus 10 / 24 What does this strange PL compute? Dana Scott in 1969 Recall n = λ s z . s n z . Definition October 1969, Oxford A partial function f : N ⇀ N is λ -computable iff there is some closed The untyped λ -calculus λ -term M f such that, for all n ∈ N : ≈ an unmotivated pile of syntax that, by shear luck, is consistent. if f ( n ) ↑ is undefined, then M f n has no β -n.f., and D. Scott, A type-theoretical alternative to ISWIM, CUCH, OWHY, if f ( n ) ↓ = m , then M f n = βη m . Theoretical Computer Science 121 (1993) 411–440. Originally written in 1969. Theorem (Some collection of Church, Turing, Kleene, & Rosser) November 1969, Oxford λ -computable = Turing computable. Oh dear! The above is very nice, but it still doesn’t mean that the λ -calculus makes sense. Jim Royer A Crash Course on the λ -Calculus 11 / 24 Jim Royer A Crash Course on the λ -Calculus 12 / 24
The simplest simple types Terms, types, and λ x . x Church: The type is part of the term Each type variable has an assigned type. λ x σ . x σ : σ → σ — an infinite number of identity functions. T :: = B | ( T → T ) B :: = atomic type variables Curry: Rules for assigning types to terms (proto-polymorphism) Associativity: TA λ : theory of type assignment to lambda terms. ρ → σ → τ ≡ ( ρ → ( σ → τ )) All untyped terms are in the theory. Recall: MNP ≡ (( MN ) P ) But not all terms can be assigned a type, e.g., λ x . xx . Types can contain type-variables. Interpretation (rough): If a term t is assigned type τ , then t can also be assigned all Each type names a set substitution instances of τ . σ → τ names a set of functions from (the set named by) σ to the E.g.: λ x . x : σ → σ implies (set named by) τ . λ x . x : a → a , λ x . x : ( a → b ) → ( a → b ) , . . . Jim Royer A Crash Course on the λ -Calculus 13 / 24 Jim Royer A Crash Course on the λ -Calculus 14 / 24 Type assignments The system TA λ Axiom: x : τ �→ x : τ a type assignment M : τ M the subject τ the predicate A type context: Γ = { x 1 : τ 1 , . . . , x k : τ k } , a consistent set of variable → E: Γ 1 �→ P : ( σ → τ ) Γ 2 �→ Q : σ type assignments, i.e., any variable is assigned at most one type. ( Γ 1 ∪ Γ 2 is consistent ) Subjects ( Γ ) = { x 1 , . . . , x k } . Γ 1 ∪ Γ 2 �→ ( P Q ) : τ Γ − x = Γ with any assignments to x removed. Γ �→ P : τ Γ − x �→ ( λ x . P ) : ( σ → τ ) ( Γ is consistent with x : σ ) Γ ↾ M = Γ with any assignments to y / ∈ FV ( M ) removed → I: A Γ with Subjects ( Γ ) = FV ( M ) is called an M-context . Γ 1 , . . . , Γ k are consistent iff Γ 1 ∪ · · · ∪ Γ k is consistent. TA λ formulas are all of the form: Γ �→ M : τ . Γ 1 , x : σ �→ P : τ Γ 1 �→ ( λ x . P ) : ( σ → τ ) ( x / ∈ Subjects ( Γ 1 )) → I main : Abbreviations: Γ 1 �→ P : τ x 1 : σ 1 , . . . , x n : σ n �→ M : τ { x 1 : σ 1 , . . . , x n : σ n } �→ M : τ for → I vac : Γ 1 �→ ( λ x . P ) : ( σ → τ ) ( x / ∈ Subjects ( Γ 1 )) Γ , y 1 : σ 1 , . . . , y n : σ n �→ M : τ Γ ∪ { y 1 : σ 1 , . . . , y n : σ n } �→ M : τ for Jim Royer A Crash Course on the λ -Calculus 15 / 24 Jim Royer A Crash Course on the λ -Calculus 16 / 24
�→ and ⊢ λ TA λ deductions Recall Lemma If Γ �→ M : τ is deducible in TA λ , then Subjects ( Γ ) = FV ( M ) . B ≡ λ xyz . x ( yz ) I ≡ λ x . x Definition K ≡ λ xy . x Γ ⊢ λ M : τ means that, for some Γ ′ ⊆ Γ , Γ ′ �→ M : τ is TA λ derivable. Also, note N.B. Γ ⊢ λ M : τ is not a TA λ -formula. II = ( λ x . x )( λ x . x ) Lemma (Weakening) If Γ ⊢ λ M : τ and Γ + ⊇ Γ , then Γ + ⊢ λ M : τ . Show: �→ B : ( a → b ) → ( c → a ) → c → b . 1 Lemma �→ I : a → a . 2 (i) Γ ⊢ λ M : τ iff Subjects ( Γ ) ⊇ FV ( M ) and TA λ proves Γ ↾ M �→ M : τ . �→ K : a → b → a . 3 (ii) ( ∃ Γ )[ Γ ⊢ λ M : τ ] iff ( ∃ Γ | Γ is an M-context )[ Γ ⊢ λ M : τ ] . �→ II : a → a . 4 (iii) For each closed term M: ( ∃ Γ )[ Γ ⊢ λ M : τ ] iff ⊢ λ M : τ Also, what goes wrong with an attempt to derive a type for λ x . xx ? Jim Royer A Crash Course on the λ -Calculus 17 / 24 Jim Royer A Crash Course on the λ -Calculus 18 / 24 The subject-construction theorem for TA λ Utility lemmas Let [ y / x ] Γ = the result of substituting y for x in Γ . Lemma (1st substitution lemma for deductions) Theorem (Informal) Suppose Γ ⊢ λ M : τ . If either The tree-structure of a type derivation of a term matches the tree-structure of (i) y / ∈ Subjects ( Γ ) or the (syntactic construction of the) term. (ii) Γ assigns x and y the same type, then [ y / x ] Γ ⊢ λ ([ y / x ] M ) : τ . Corollary Each normal form term has a unique type derivation. Lemma ( α -invariance) If Γ ⊢ λ P : τ and P ≡ α Q, then Γ ⊢ λ Q : τ See Hindley § 2B for details. Lemma (2nd substitution lemma for deductions) Suppose Γ 1 , x : σ ⊢ λ M : τ and Γ 2 ⊢ λ N : σ where Γ 1 ∪ Γ 2 is consistent. Then Γ 1 ∪ Γ 2 ⊢ λ [ N / x ] M : τ Jim Royer A Crash Course on the λ -Calculus 19 / 24 Jim Royer A Crash Course on the λ -Calculus 20 / 24
Recommend
More recommend