Typed Lambda Calculus CURRY SYSTEM Types: T ::= V | T --> T Terms: L ::= x | \x.L | | LL Notation: --> is right associative DEF. A Statement or Judgment is of the form M : A with M in Lambda and A in T A Declaration is a Judgment x:A A Basis is a set of declarations with distinct variable names DEF. A Statement M:A is derivable from a basis G, G |- M:A if provable using the rules:... G |- x:A if x:A in G G |- M:A->B G |- N:A --------------------- G |- (MN) : B G,x:A |- M:B -------------------- G |- \x.M : A->B ADDING Constants - consider as variables or as distinct signature (0, succ) PROPERTIES: Let G = {x1:A1, ... xn:An} dom(G) = {x1,...,xn} G(xi) = Ai Let V be a set of variables. G|V = {x:A | x in V and G(x)=A} Substitution: A[alpha := B] PROPOSITION. Let G be a basis. (1) If G' supset G is another basis then G |- M:A implies G' |- M:A (2) G |- M:A implies FV(M) subset G (3) G |- M:A implies G|FV(M) |- M:A LEMMA. (Generation) (1) G |- x:A implies x:A in G (1) G |- MN:B implies there exists A G |- M:A->B and G |- N:A (1) G |- \x.M:A implies there exist B,C G,x:B |- M:C and A == B->C PROPOSITION. (Typability of subterms) Let M' be a subterm of M. Then G |- M:A implies G' |- M':A' for some G',A' PROPOSITION. (Substitution Lemma) (1) G |- M:A implies G[alpha:=B] |- M:A[alpha:=B] (2) If G,x:A |- M:B and G |- N:A Then G |- M[x:=N]:B PROPOSITION. (Subject Reduction) If M -->> M' Then G |- M:A implies G |- M':A PROOF: by Induction on the generation of M -->> M' and using Generation Lemma and Substitution Lemma. Essential Case: M == (\x.P)Q and M' == P[x:=Q] Assume G |- (\x.P)Q : A Then by Generation Lemma, G |- \x.P :B->A and G |- Q:B Again by Generation Lemma, G,x:B |- P:A Then by Substitution Lemma G |- P[x:=Q]:A CHURCH Type System: Types - Same Terms - Explicitly Annotate bound variables w/ types Terms: LT ::= x | \x:A.L | | LT LT Similar rules, G |- x:A if x:A in G G |- M:A->B G |- N:A --------------------- G |- (MN) : B G,x:A |- M:B -------------------- G |- \x:A.M : A->B Similar Results: - Church Rosser - Basis Proposition - Generation Proposition - Typeability of Subterms - Substitution Lemma - Subject Reduction Additionally, PROPOSITION. (Unicity of Types) 1. G |- M:A and G |- M:B implies A==B 2. G |- M:A, G|- M':A' and M=M' (beta) implies A==A' PROOF. 1. By induction on the structure of M 2. By the Church Rosser & Subject Reduction. RELATING CURRY AND CHURCH SYSTEMS Erasure: Erase annotations to give untyped term |x| = x |\x:A.M| = \x.|M| |MN| = |M||N| PROPOSITION. (1) Let M in LT. Then G |- M:A implies G |- |M|:A (2) Let M in L. Then G |- M:A implies exists M' in LT such that G |- M':A and |M'| = M PROOF. By induction on the given derivation. COROLLARY. For a type A, A is inhabited in lambda-Curry iff A is inhabited in lambda-Church. PROOF. Immediate. STANDARD ML: to run on Sparcs: ~sml/bin/sml-sparc to run on Crayola: sml HISTORY: o Scott's Logic of Computable Functions (early 70's) - logic to reason about functions, denotational semantics, functional programming - terms of the logic constitute a tiny functional language o Edinburgh LCF (Robin Milner) - A theorem prover based on LCF, permits experimentation with LCF - The user interacts with the prover through a programmable meta language, ML. - Logical formulae, theorems, rules and proof strategies are ML data - The prover guarantees soundness: it checks each inference and records each assumption. o ML (Meta Language for LCF) - A functional, higher-order language for symbolic processing (trees, lists) - After several dialects began to appear as independent prog. lang., Milner began to establish a standard Syntax for implicitly typed \-calculus - variables, application, abstraction E ::= x | fn x => E | E E Additional Features - Constants (ints, bools, strings) - Pairs, lists - Conditional - Declarations val/fun - Let Expressions Static Typing: Reject untypable terms Reduction Rules Examples: fact, length, append, map, curry, uncurry Can't type FixedPoint combinator The Polymorphic Typed Lambda Calculus (2nd-order typed Lambda Calculus, System F, Lambda2, Girard-Reynolds Polymorphism) History: Girard - proof theory Reynolds - study of explicit polymorphism Again, Curry and Church Versions Idea: (\x.x) : (A->A) for arbitrary A So in Polylam, we write: (\x.x) : all A.(A->A) The set of types in Polylam: T ::= V | T->T | all V.T Notation: (1) all A1 A2...An.T == all A1(all A2(...all An.T)) (2) all binds more tighly than -> Type Assignment in Polylam is given by: x:A in G -------- G |- x:A G |- M:A->B G |- N:A G,x:A |- M:B --------------------- ------------------ G |- (MN) : B G |- \x.M : A->B G |- M: all x.A G |- M:A --------------------- ------------------ G |- M : A[s:=B] G |- M : all s.A s not in FV(G) EXAMPLES: \x.x : all s.(s->s) \xy.y : all s t(s->t->t \fx.f^n(x) : all s.((s->s)->(s->s)) \x.xx STANDARD ML - weak polymorphism, Let-polymorphism Rational: o most useful programs don't require full polymorphism o type inference problem for full poly is undecidable o type inference problem for weak poly is decidable MonoTypes: T ::= V | T -> T PolyTypes: P ::= T | all s.P x:P in G -------- G |- x:P G |- M:A->B G |- N:A G,x:A |- M:B --------------------- ------------------ G |- (MN) : B G |- \x.M : A->B G |- M: all x.P G |- M:P --------------------- ------------------ G |- M : P[s:=B] G |- M : all s.P s not in FV(G) G |- M:P G,x:P |- N:B ---------------------------- G |- let x=M in N : B Var rule and last three rules can be combined: x:P in G G |- M:A G,x:P |- N:B -------- ---------------------------- G |- x:P G |- let x=M in N : B where P = all s1 s2...sn.A and s1,...,sn are all the free variables Examples: let fun id x = x in id id end let fun length nil = 0 |length (a::k) = 1 + length k in (length [1,2,3], length [true]) end; Goedel's System T Y - not typeable In fact, all typeable terms are strongly normalizing Goedel extended typed lambda calculus as follows: Type constant: nat Term constants: 0: nat S: nat -> nat For each type t, R_t : t -> (t->nat->t) -> nat -> t New reduction rules (for each type t): R_t M N 0 --> M R_t M N (S X) --> N (R_t M N x) x Define Addition... NOTE: M need not be a "base" value : fun selfcompose f n = R f (fn g=> fn k => fn x => f(g x)) n; Note that SML Weak polymorphism allows us to define a single function R: fun R M f 0 = M |R M f k = f (R M f (k-1)) (k-1); = val R = fn : 'a -> ('a -> int -> 'a) -> int -> 'a PROPERTIES of POLYLAM BASIS LEMMA: (1) If G' supset G is another basis then G |- M:A implies G' |- M:A (2) G |- M:A implies FV(M) subset G (3) G |- M:A implies G|FV(M) |- M:A SUBTERM LEMMA: Let M' be a subterm of M. Then G |- M:A implies G' |- M':A' for some G',A' SUBSTITUTION LEMMA: (1) G |- M:A implies G[alpha:=B] |- M:A[alpha:=B] (2) If G,x:A |- M:B and G |- N:A Then G |- M[x:=N]:B (No Generation Lemma) SUBJECT REDUCTION: