Digression on Universal Polymorphism -------------------------------------- r: ('a -> 'a) ref -> "polymorphic reference" r:= (\x:int . x + 1) : int -> int ; ((!r): bool -> bool) true (this gets stuck!) r: (\forall A.A -> A) ref r:= (\x:int. x + 1) r: \forall A ((A -> A) ref) /\A. ref(\x:A.x) : \forall A. ((A -> A) ref) let r = /\ A. ref (\x:A.x) in r[int]:= (\x:int. x + 1); (!r[bool]) true (this is correct and doesn't get stuck!) -> (/\ A. ref(\x:A.x)[int] := (\x:int.x + 1); !(/\ A. ref(\x:A.x)[bool] true -> (.; ref(\x:int.x) := (\x:int.x + 1); ...) -> (l -> \x:int.x+1; l:=(\x:int.x+1); ...) -> (l -> \x:int.x+1; !(/\ A. ref(\x:A.x)[bool] true)) -> (l -> \x:int.x+1; !(ref(\x:bool.x) true)) -> (l -> \x:int.x+1, l' -> \x:bool.x; !(l' true)) -> (l -> \x:int.x+1, l' -> \x:bool.x; (\x:bool.x true)) -> (l -> \x:int.x+1, l' -> \x:bool.x; true) ML has a value restriction: you can't make an expression polymorphic unless it's a value. ref e is not a value, therefore it can't have polymorphic types. Rewrite it to \forall A.(() -> ('a -> 'a) ref) Subtyping --------- Progress Lemma: If . |- e: t then either e is a value or e -> e'. Proof: By induction on the derivation of |- e : t Case: |- e1: t1 -> t2 |- e2 : t1 --------------------------- |- e1 e2 : t2 By induction: (a) e1 is a value or e1 -> e1' (b) e2 is a value or e2 -> e2' There are several combinations of cases One subcase: e1 = v1, e2 = v2 v1 v2 -> e[v2/x] (By canonical forms, v1 = \x:t.e) Canonical forms --------------- if |- v : t then (1) if t = t1 -> t2 then v = \x:t1'.e and t1 <= t1' (2) if t = t1 * t2 * ... * tn) then v = (v1, ..., vm) and n <= m (3) if t = top then v can be anything (4) if t = bool then v can be true or false Proof: By induction on the typing derivation |- v: t case: |- v : t' t' <= t ------------------- (subsumption rule) |- v : t subcase (1) t = t1 -> t2 if we know that t' <= t1 -> t2 and t' = t1' -> t2' and t1 <= t1' and t2' <= t2 then by induction v = \x:t".e and t1 <= t" By transitivity and subtyping, t1 <= t'. (qed) Lemma subtyping --------------- (1) if t <= t1' -> t2' then t = t1 -> t2 and t1' <= t1 and t2 <= t2' (2) if t <= (t1' * ... * tn') then t = (t1 * ... * tm) and m >= n and for i = 1, ... n, ti <= ti' (3) if t <= top then t can be anything (4) if t <= bool then t = bool Prove by induction on the subtyping relations