References A Crash Course on the -Calculus J. Roger Hindley, Basic - - PowerPoint PPT Presentation

references a crash course on the calculus
SMART_READER_LITE
LIVE PREVIEW

References A Crash Course on the -Calculus J. Roger Hindley, Basic - - PowerPoint PPT Presentation

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) 411440. Originally


slide-1
SLIDE 1

A Crash Course on the λ-Calculus

Jim Royer

Types Seminar

January 21, 2014

Jim Royer A Crash Course on the λ-Calculus 1 / 24

References

  • J. Roger Hindley, Basic Simple Type Theory, Cambridge, 1997.
  • D. Scott, A type-theoretical alternative to ISWIM, CUCH, OWHY,

Theoretical Computer Science 121 (1993) 411–440. Originally written in 1969.

Jim Royer A Crash Course on the λ-Calculus 2 / 24

The λ-calculus

Church (with Kleene and Rosser) in the 1930s. A family of prototype programming languages. Functional and higher-order. The pure λ-calculus ≡ no atomic constants. Untyped (or uni-typed) considered first. Syntax: E ::= X variables/atoms/. .. | (E1 E2) applications | (λX.E) abstractions Associativity: λxyz.M ≡ (λx.(λy.(λz.M))) MNPQ ≡ (((MN)P)Q)

Jim Royer A Crash Course on the λ-Calculus 3 / 24

Subterms, occurrences, free, and bound

subterms(x) = { x } subterms( (λx.M) ) = { λx.M } ∪ subterms(M) subterms( (M N) ) = { (M N) } ∪ subterms(M) ∪ subterms(N) An occurrence of a subterm is . . . roughly what you think it is . . . λx.M λx: an abstractor and x (in λx): binding occurrence of x M: the body or scope of the abstractor λx. Free and bound occurrences and variables An occurrence of variable x in M is bound in M iff the occurrence is in the scope of some λx in M; o/w the occurrence is said to be free. A variable x is bound in M iff an λx occurs in M. A variable x is free in M iff M contains a free occurrence of x. FV(M) = the set of variables free in M.

Jim Royer A Crash Course on the λ-Calculus 4 / 24

slide-2
SLIDE 2

The full, fussy rules for substitution

And why all the fuss?

Jim Royer A Crash Course on the λ-Calculus 5 / 24

α-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

  • f 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.

Jim Royer A Crash Course on the λ-Calculus 6 / 24

Closed terms

A closed term or combinator is a term in which no variable occurs free. Common Combinators B ≡ λxyz.x(yz) B′ ≡ λxyz.y(xz) C ≡ λxyz.xzy I ≡ λx.x K ≡ λxy.x S ≡ λxyz.xz(yz) W ≡ λxy.xyy Y ≡ λx.(λy.x(yy))(λy.x(yy)) 0 ≡ λsz.z 1 ≡ λsz.sz n ≡ λsz. s(s(. . . (s

  • n-many s’s

z) . . . ))

Jim Royer A Crash Course on the λ-Calculus 7 / 24

β-reduction

(λx.M)N: β-redex [N/x]M: its contractum (λx.M)N ✄1β [N/x]M: its re-write rule If P contains an occurrence R = (λx.M)N and Q is the result of replacing this with [N/x]M, then P β-contracts to Q (written: P ✄1β Q); P, R, Q: a β-contraction of P. A β-reduction of P is a sequence (finite or infinite) P, R1, P2, P2, R2, P3, . . . P β-reduces to Q (written: P ✄β Q) means the sequence ends with Q. β-expansion: a reverse β-contraction. P =β Q means: one can convert P to Q by a series of β-contractions and β-expansions.

Jim Royer A Crash Course on the λ-Calculus 8 / 24

slide-3
SLIDE 3

Confluence (aka Church-Rosser) and normal forms

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. A β-normal form term is one without any β-redex. N is a β-normal form of M when N is in β-n.f. and M ✄β N. β-normal forms ≈ result of computations. 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

η-reductions

An η-redex is a term of the form λx.(Mx) with x / ∈ FV(M). λx.(Mx) ✄η M: the re-write rule for η-redeces. η-contracts, η-reduces, η-converts, η-normal form, ...are defined analogously to the β-notions. Church-Rosser extends to η- and βη-reductions.

Jim Royer A Crash Course on the λ-Calculus 10 / 24

What does this strange PL compute?

Recall n = λs z.snz. Definition A partial function f : N ⇀ N is λ-computable iff there is some closed λ-term Mf such that, for all n ∈ N: if f(n)↑ is undefined, then Mf n has no β-n.f., and if f(n)↓ = m, then Mf n =βη m. Theorem (Some collection of Church, Turing, Kleene, & Rosser) λ-computable = Turing computable. 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

Dana Scott in 1969

October 1969, Oxford The untyped λ-calculus ≈ an unmotivated pile of syntax that, by shear luck, is consistent.

  • D. Scott, A type-theoretical alternative to ISWIM, CUCH, OWHY,

Theoretical Computer Science 121 (1993) 411–440. Originally written in 1969. November 1969, Oxford Oh dear!

Jim Royer A Crash Course on the λ-Calculus 12 / 24

slide-4
SLIDE 4

The simplest simple types

T ::= B | (T → T) B ::= atomic type variables Associativity: ρ → σ → τ ≡ (ρ → (σ → τ)) Recall: MNP ≡ ((MN)P) Interpretation (rough): Each type names a set σ → τ names a set of functions from (the set named by) σ to the (set named by) τ.

Jim Royer A Crash Course on the λ-Calculus 13 / 24

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. Curry: Rules for assigning types to terms (proto-polymorphism) TAλ: theory of type assignment to lambda terms. All untyped terms are in the theory. But not all terms can be assigned a type, e.g., λx.xx. Types can contain type-variables. If a term t is assigned type τ, then t can also be assigned all substitution instances of τ. E.g.: λx.x : σ → σ implies

λx.x : a → a, λx.x : (a → b) → (a → b), . . .

Jim Royer A Crash Course on the λ-Calculus 14 / 24

Type assignments

a type assignment M : τ M the subject τ the predicate A type context: Γ = { x1 : τ1, . . . , xk : τk }, a consistent set of variable type assignments, i.e., any variable is assigned at most one type. Subjects(Γ) = { x1, . . . , xk }. Γ − x = Γ with any assignments to x removed. Γ↾M = Γ with any assignments to y / ∈ FV(M) removed 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 : τ. Abbreviations: x1 : σ1, . . . , xn : σn → M : τ for { x1 : σ1, . . . , xn : σn } → M : τ Γ, y1 : σ1, . . . , yn : σn → M : τ for Γ ∪ { y1 : σ1, . . . , yn : σn } → M : τ

Jim Royer A Crash Course on the λ-Calculus 15 / 24

The system TAλ

Axiom: x : τ → x : τ →E: Γ1 → P : (σ → τ)

Γ2 → Q : σ Γ1 ∪ Γ2 → (P Q) : τ (Γ1 ∪ Γ2 is consistent)

→I:

Γ → P : τ Γ − x → (λx.P) : (σ → τ) (Γ is consistent with x : σ)

→Imain:

Γ1, x : σ → P : τ Γ1 → (λx.P) : (σ → τ) (x / ∈ Subjects(Γ1))

→Ivac:

Γ1 → P : τ Γ1 → (λx.P) : (σ → τ) (x / ∈ Subjects(Γ1))

Jim Royer A Crash Course on the λ-Calculus 16 / 24

slide-5
SLIDE 5

TAλ deductions

Recall B ≡ λxyz.x(yz) I ≡ λx.x K ≡ λxy.x Also, note II = (λx.x)(λx.x) Show:

1

→ B : (a → b) → (c → a) → c → b.

2

→ I : a → a.

3

→ K : a → b → a.

4

→ II : a → a. Also, what goes wrong with an attempt to derive a type for λx.xx?

Jim Royer A Crash Course on the λ-Calculus 17 / 24

→ and ⊢λ

Lemma If Γ → M : τ is deducible in TAλ, then Subjects(Γ) = FV(M). Definition Γ ⊢λ M : τ means that, for some Γ′ ⊆ Γ, Γ′ → M : τ is TAλ derivable. N.B. Γ ⊢λ M : τ is not a TAλ-formula. Lemma (Weakening) If Γ ⊢λ M : τ and Γ+ ⊇ Γ, then Γ+ ⊢λ M : τ. Lemma (i) Γ ⊢λ M : τ iff Subjects(Γ) ⊇ FV(M) and TAλ proves Γ↾M → M : τ. (ii) (∃Γ)[Γ ⊢λ M : τ] iff (∃Γ|Γ is an M-context)[Γ ⊢λ M : τ]. (iii) For each closed term M: (∃Γ)[Γ ⊢λ M : τ] iff ⊢λ M : τ

Jim Royer A Crash Course on the λ-Calculus 18 / 24

The subject-construction theorem for TAλ

Theorem (Informal) The tree-structure of a type derivation of a term matches the tree-structure of the (syntactic construction of the) term. Corollary Each normal form term has a unique type derivation. See Hindley §2B for details.

Jim Royer A Crash Course on the λ-Calculus 19 / 24

Utility lemmas

Let [y/x]Γ = the result of substituting y for x in Γ. Lemma (1st substitution lemma for deductions) Suppose Γ ⊢λ M : τ. If either (i) y / ∈ Subjects(Γ) or (ii) Γ assigns x and y the same type, then [y/x]Γ ⊢λ ([y/x]M) : τ. Lemma (α-invariance) If Γ ⊢λ P : τ and P ≡α Q, then Γ ⊢λ Q : τ 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 20 / 24

slide-6
SLIDE 6

Subject reduction

Theorem (Subject reduction) If Γ ⊢λ P : τ and P ✄βη Q, then Γ ⊢λ Q : τ. Theorem (Subject expansion) If Γ ⊢λ P : τ and P ✄βη Q by non-duplicating and non-cancelling contractions, then Γ ⊢λ Q : τ. For a closed term M: Types(M) = the set of types assignable to M. Fact (see Hindley Chapter 3) Types(M) is either empty or consists of the set of substitution instances

  • f a single type, the principal type M.

Lemma For closed P: P ✄β Q implies Types(P) ⊆ Types(Q).

Jim Royer A Crash Course on the λ-Calculus 21 / 24

Typable terms, 1

A term M is (TAλ-) typable iff, for some Γ and τ, Γ ⊢λ M : τ. Lemma The TAλ-typable terms are closed under: taking subterms βη-reductions non-cancelling and non-duplicating β-expansions λ-abstraction Lemma The set of TAλ-typable terms is decidable.

Jim Royer A Crash Course on the λ-Calculus 22 / 24

Typable terms, 2

Theorem (Weak normalization, Turing 1942, Curry-Feys 1958, etc.) Each TAλ-typable term has both a β- and βη-normal form. Theorem (Strong normalization, Sanchis 1967, Diller 1968, etc.) If M is TAλ-typable term, then every βη-reducion from M terminates. Theorem (=β and =βη are decidable for typable terms) There is an algorithm, that given TAλ-typable terms P and Q, decides whether P =β Q. (Similarly for =βη.) Statman 1979: There are E4-decision procedures =β and =βη on typable

  • terms. But there are no such decision procedures in E3.

Jim Royer A Crash Course on the λ-Calculus 23 / 24

E 3 and E 4

E2 = deterministic linear space E3 = DTIME(2n) ∪ DTIME(22n) ∪ DTIME(222n ) ∪ . . . E4 = DTIME(2↑↑n) ∪ DTIME(2↑↑(2↑↑n)) ∪ DTIME(2↑↑(2↑↑(2↑↑n))) ∪ . . . where a↑↑b = aa···a

  • b-many a’s

. . . the primitive recursive functions =

n>1 En.

Jim Royer A Crash Course on the λ-Calculus 24 / 24