* Explicit type systems ** Curry-Howard Isomorphism, Propositions as Types ---------------------------- proposition ~~ type constructive proof ~~ term in a typed lambda calculus (evidence) (algorithm) ---------------------------- *** Higher-order logic --------------------- order formulae quantif over 0 x 1 all x:T . P T 2 all P . all x:T . P T -> T 3 all double . all f . all x . P(double(f)(x)) (T->T)->(T->T) --------------------- *** Higher-order programming --------------------- order terms arg. types 0 x 1 \x:T . P T 2 \P:* . \x:T . P * -> * 3 \double . \f . \x . P(double(f)(x)) (*->*)->(*->*) --------------------- ** \2-Church, SOL, Girard-Reynolds calculus, system F (i.e., F2) ------------------- T ::= V | C | T -> T | all V . T e ::= v | c | e e | e T | \ v:T . e | poly V . e ------------------- *** rules *** pragmatics **** representability ***** some total recursive functions not expressible ***** can express provably total functions in 2nd order peano arithmetic **** type defs **** abstract data types (as in CLU or ML abstypes) (page 22) ***** booleans --------------------- let bool = all T . T -> T -> T true = poly T . \x:T . \y:T . x false = poly T . \x:T . \y:T . y if = poly T . \b:bool . \e1:T . \e2:T . b T e1 e2 --------------------- ***** products --------------------- let (X U V) = all T . (U -> V -> T) -> T pair = poly U . poly V . \u:U . \v:V . poly T . \f:(U->V->T) . f u v fst = poly U . poly V . \p:(X U V) . p U (\f:U . \s:V . f) snd = poly U . poly V . \p:(X U V) . p V (\f:U . \s:V . s) --------------------- ***** sums --------------------- let (+ U V) = all T . (U -> T) -> (V -> T) -> T inl = poly U . poly V . \u:U . poly T . \l:U->T . \r:V->T . l u inr = poly U . poly V . \v:V . poly T . \l:U->T . \r:V->T . r v cases = poly U . poly V . \x:(+ U V) . x --------------------- ***** etc. ---------------------- name constructors type bool true:bool all T . T -> false: bool T -> T (X U V) pair:U -> V -> (X U V) all T . (U -> V -> T) -> T (+ U V) inl:U -> (+ U V), all T . (U -> T) -> inr:V -> (+ U V) (V -> T) -> T ---------------------- **** relation to ML ** \-omega_ *** Kinds ------------------------- * is the kind of types (set of all types) so t: * means t is a type (has kind *) * -> * is a constructor (maps types to types) K ::= * | K -> K ------------------------- *** Syntax and rules ------------------------------- e ::= V | C | e e | \ V:e . e | e -> e C ::= * | [] | ... ------------------------------- **** no mixing of kinds and types (elements cannot depend on types) **** types can depend on types ---------------------------------- |- *:[], |- *:[] axiom, axiom _____________________________ type/kind formation |- *:[], |- *->*:[] axiom (on left) _____________________________ type/kind formation |- *->(*->*):[] _____________________________ weakening U:* |- (*->*->*):[] ---------------------------------- ---------------------------------- |- *:[] axiom ________________________ weakening U:*, V:*, T:* |- *:[] ___________________________________________ start U:*, V:*, T:* |- V:*, U:*, V:*, T:* |- T:* _________________________________________________ t/k formation U:*, V:*, T:* |- U:*, U:*, V:*, T:* |- (V -> T):* _______________________________________________________ t/k formation U:*, V:*, T:* |- (U -> V -> T):*, U:*, V:*, T:* |- T:* ___________________________________________ type/kind formation U:*, V:* |- *:[], U:*, V:*, T:* |- *:[], U:*, V:*, T:* |- ((U -> V -> T) -> T) : * ___________________________________________ (->I) U:* |- *:[], U:*, V:* |- *->* : [], U:*, V:* |- (\T:*.(U -> V -> T) -> T) : * -> * ___________________________________________ (->I) |- *:[], U:* |- (*->*->*):[], U:* |- (\V:*.\T:*.(U -> V -> T) -> T) : * -> * -> * ________________________________________________________ (->I) |- (\U:*.\V:*.\T:*.(U -> V -> T) -> T) : * -> * -> * -> * ---------------------------------- *** pragmatics **** \omega ***** problem (page 26 at top): ------------------------------------------------- num:*, 0:num |- (*->*):[], (axioms, t/k form) num:*, 0:num, a:(*->*) |- num:[] invalid! num:*, 0:num, a:(*->*) |- 0:num (start, weakening) _____________________________________ ->I num:*, 0:num |- (\a:*->*.0) : (*->*)->num, num:*, 0:num |- (\b:*.b->b): (*->*) (ignore this) _____________________________________ ->E num:*, 0:num |- (\a:*->*.0)(\b:*.b->b) : num ------------------------------------------------- ** \P *** syntax ---------------------- e ::= V | C | e e | (\V:e)e | [V:e]e C ::= * | [] | ... ---------------------- **** abbreviation: A -> B abbreviates [x:A] B, if x not free in B. *** rules *** pragmatics, formalization of constructive math (page 28) ---------------------- * is set of valid propositions (truth) A => B translated A -> B \forall x:A . Px translated [x:A] Px predicate P on A translated P : A -> * ---------------------- ------------------------------- proof of A => (B => A) translation A -> (B -> A) : * proofs: (\x:A) (\y:B) x : A -> (B -> A) (\x:A) (\y:B) y : A -> (B -> A) proof of (forall x:A . Px => Qx) => (forall x:A .Px => forall x:A . Qx) translation ([x:A] (Px -> Qx)) -> ([x:A]Px) -> [x:A]Qx proof: (\x:A)(\f:(Px -> Qx))(\g:[x:A]Px)(\x:A) (f (g x)) has the above type Even : Nat -> * = (\n:Nat)[\P:(Nat -> *)] ([u:Nat] P (2 u)) -> (P n) Multof4 : Nat -> * = (\n:Nat)[\P:(Nat -> *)] ([u:Nat] P (2 (2 u))) -> (P n) proof of forall x:Nat . Multof4 x => Even x translation [x:Nat] (Multof4 x) -> (Even x) proof: (\x:Nat) (\a:(Multof4 x)) (\P:(Nat -> *)) (\D:([u:Nat] (P (2 u)))) (a P ((\u:Nat) D (2 u))) ------------------------------- ** Generalized type systems, \P-omega *** Syntax ------------------------- e ::= V | C | e e | (\V:e)e | [V:e]e ------------------------- *** rules **** specification = (S,A,R) **** general rules *** examples **** examples in \-omega --------------------- ([T:*] (U -> V -> T) -> T) : * note that ([],*) in rules for \-omega X : [U:*][V:*] * = (\U:*)(\V:*)[T:*] (U -> V -> T) -> T pair : [U:*][V:*] U -> V -> (X U V) = (\U:*)(\V:*)(\u:U)(\v:V)(\T:*)(\f:(U->V->T)) f u v fst = (\U:*)(\V:*)(\p:(X U V)) p U ((\f:U)(\s:V) f) snd = (\U:*)(\V:*)(\p:(X U V)) p V ((\f:U)(\s:V) s) Compose : [A:*][B:*][C:*][f:A->B][g:B->C] A -> C = (\A:*)(\B:*)(\C:*)(\f:A->B)(\g:B->C)(\x:A) g (f x) --------------------- **** examples in \P-omega ***** types are first-class citizens ------------------------------- -> : [A:*][B:*] * = (\A:*)(\B:*)[x:A]B Pi : [A:*][B:[u:A]*] * = (\A:*)(\B:[u:A]*)[x:A] B x elminPi : [A:*][B:*][a:A] (Pi A B) -> (B a) = (\A:*)(\B:*)(\a:A)(\p:(Pi A B)) p a introPi : [A:*][B:*][C:*][f:[x:A] (C -> (B x))] (C -> (Pi A B)) = (\A:*)(\B:*)(\C:*)(\f:[x:A](C->(B x)))(\c:C)(\a:A)(f a c) ------------------------------- ***** types are propositions ------------------- let A:* in Id : A -> A -> * = (\x:A)(\y:A)[P:(A -> *)] (P x) -> (P y) IdIsReflexive : * = [x:A] Id x x ProofOfAbove : IdIsReflexive = (\x:A)(\P:(A -> *))(\p:(P x)) p ------------------- ***** type structure is very fine ---------------------- EvenNats : * = [n:Nat] (Even n) EvenNatPair : * = (X EvenNats EvenNats) EqualNatPair : * = [p:(X Nat Nat)][Q:(Nat -> Nat -> *)] (Q 0 0) -> ([u:Nat][v:Nat] (Q u v) -> (Q (S u) (S v))) -> (Q (fst Nat Nat p) (snd Nat Nat p)) ---------------------- ***** all well-typed programs terminate ---------------------------- Factit : (X Nat Nat) -> (X Nat Nat) = (\z:(X Nat Nat)) (pair Nat Nat (S (fst Nat Nat z)) (mult (S (fst Nat Nat z)) (snd Nat Nat z))) fact : Nat -> Nat = (\n:Nat) (snd Nat Nat (n (Nat->Nat) Factit (pair Nat Nat 0 (S 0)))) ---------------------------- --------------------- Even : Nat -> * = (\n:Nat)[\P:(Nat -> *)] ([u:Nat] P (2 u)) -> (P n) partial : [n:Nat] EvenNats -> (Even n) = (\n:Nat) (\m:[x:Nat](Even x)) (m n Even ((\u:Nat) m (2 u))) ------------------- *** properties (page 31)