680 likes | 788 Views
Space-Efficient Gradual Typing. David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz. The point. Naïve type conversions in functional programming languages are not safe for space. But they can and should be. Gradual Typing:
E N D
Space-Efficient Gradual Typing David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz
The point Naïve type conversions in functional programming languages are not safe for space. But they can and should be.
Gradual Typing: Software evolution via hybrid type checking
Dynamic vs. static typing Dynamic Typing Static Typing
Gradual typing Dynamic Typing Static Typing
Type checking let x = f() in … let y : Int = x - 3 in …
Type checking let x : ? = f() in … let y : Int = x - 3 in …
Type checking let x : ? = f() in … let y : Int = x - 3 in … - : Int × Int → Int
Type checking let x : ? = f() in … let y : Int = <Int>x - 3 in …
Type checking let x : ? = f() in … let y : Int = <Int>x - 3 in … Int
Evaluation let x : ? = f() in … let y : Int = <Int>x - 3 in …
Evaluation let x : ? = 45 in … let y : Int = <Int>x - 3 in …
Evaluation let y : Int = <Int>45 - 3 in …
Evaluation let y : Int = 45 - 3 in …
Evaluation let y : Int = 42 in …
Evaluation (take 2) let x : ? = f() in … let y : Int = <Int>x - 3 in …
Evaluation (take 2) let x : ? = true in … let y : Int = <Int>x - 3 in …
Evaluation (take 2) let y : Int = <Int>true - 3 in …
Evaluation (take 2) error: “true is not an Int”
Static semantics (Siek & Taha) Type compatibility E ⊢ e1 : (S→T) E ⊢ e2 : S′ S′ ~: S E ⊢ (e1 e2) : T
Static semantics (Siek & Taha) Type compatibility E ⊢ e1 : (S→T) E ⊢ e2 : S′ S′ ~: S E ⊢ (e1 e2) : T
Static semantics (Siek & Taha) Type compatibility ≠ subtyping! T ~: T T ~: ? ? ~: T T1 ~: S1 S2 ~: T2 (S1→S2) ~: (T1→T2)
Static semantics (Siek & Taha) Cast insertion E ⊢ e1↪ t1 : (S→T) E ⊢ e2↪ t2 : S′ S′ ~: S E ⊢ (e1 e2) ↪ (t1 (<S> t2)) : T E ⊢ e ↪ t : T cast argument expression
Space leaks fun even(n) = if (n = 0) then true else odd(n - 1) fun odd(n) = if (n = 0) then false else even(n - 1)
Space leaks fun even(n : Int) = if (n = 0) then true else odd(n - 1) fun odd(n : Int) : Bool = if (n = 0) then false else even(n - 1)
Space leaks fun even(n : Int) = if (n = 0) then true else odd(n - 1) fun odd(n : Int) : Bool = if (n = 0) then false else <Bool>even(n - 1)
Space leaks fun even(n : Int) = if (n = 0) then true else odd(n - 1) fun odd(n : Int) : Bool = if (n = 0) then false else <Bool>even(n - 1) non-tail call!
Space leaks even(n) →*odd(n - 1) →* <Bool>even(n - 2) →* <Bool>odd(n - 3) →* <Bool><Bool>even(n - 4) →* <Bool><Bool>odd(n - 5) →* <Bool><Bool><Bool>even(n - 6) →* … x
Casts in functional languages <Int>n → n <Int>v → error: “v not an Int”(if v∉Int) <σ→τ>λx:?.e → …
Casts in functional languages <Int>n → n <Int>v → error: “v not an Int”(if v∉Int) <σ→τ>λx:?.e →λz:σ.<τ>((λx:?.e) z) Very useful, very popular… unsafe for space. fresh, typed proxy cast result
More space leaks fun evenk(n : Int, k : ? → ?) = if (n = 0) then k(true) else oddk(n – 1, k) fun oddk(n : Int, k : Bool → Bool) = if (n = 0) then k(false) else evenk(n – 1, k)
More space leaks fun evenk(n : Int, k : ? → ?) = if (n = 0) then k(true) else oddk(n – 1, <Bool→Bool>k) fun oddk(n : Int, k : Bool → Bool) = if (n = 0) then k(false) else evenk(n – 1, <?→?>k)
More space leaks evenk(n, k0) →*oddk(n - 1, <Bool→Bool>k0) →*oddk(n - 1, λz:Bool.<Bool>k0(z)) →*evenk(n - 2, <?→?>λz:Bool.<Bool>k0(z)) →*evenk(n - 2, λy:?.(λz:Bool.<Bool>k0(z))(y)) →*oddk(n - 3, <Bool→Bool>λy:?.(λz:Bool.<Bool>k0(z))(y)) →*oddk(n – 3, λx:Bool.(λy:?.(λz:Bool.<Bool>k0(z))(y))(x)) →*evenk(n - 4, <?→?>λx:Bool.(λy:?.(λz:Bool.<Bool>k0(z))(y))(x)) →*evenk(n - 4, λw:?.(λx:Bool.(λy:?.(λz:Bool.<Bool>k0(z))(y))(x))(w)) →*oddk(n - 5, <Bool→Bool>λw:?.(λx:Bool.(λy:?.(λz:Bool.<Bool>k0(z))(y))(x))(w)) →*oddk(n - 5, λv:Bool.<Bool>(λw:?.(λx:Bool.(λy:?.(λz:Bool.<Bool>k0(z))(y))(x))(w))(v)) →* … (…without even using k0!) x
Intuition Casts are like function restrictions(Findler and Blume, 2006) Can their representation exploit the properties of restrictions?
Exploiting algebraic properties Closure under composition: <Bool>(<Bool> v) = (<Bool>◦<Bool>) v
Exploiting algebraic properties Idempotence: <Bool>(<Bool> v) = (<Bool>◦<Bool>) v= <Bool> v
Exploiting algebraic properties Distributivity: (<?→?>◦<Bool→Bool>) v = <(Bool◦?)→(?◦Bool)> v
Space-efficient gradual typing • Generalize casts to coercions(Henglein, 1994) • Change representation of casts from <τ> to <c> • Merge casts at runtime: This coercion can be simplified! merged before evaluating e
Space-efficient gradual typing • Generalize casts to coercions(Henglein, 1994) • Change representation of casts from <τ> to <c> • Merge casts at runtime:
Static semantics Type compatibility E ⊢ e1 : (S→T) E ⊢ e2 : S′ S′ ~: S E ⊢ (e1 e2) : T
Static semantics Type compatibility T ~: T T ~: ? ? ~: T T1 ~: S1 S2 ~: T2 (S1→S2) ~: (T1→T2)
Static semantics Cast insertion E ⊢ e1↪ t1 : (S→T) E ⊢ e2↪ t2 : S′ c=coerce(S′, S) E ⊢ (e1 e2) ↪ (t1 (<c> t2)) : T E ⊢ e ↪ t : T computes a type coercion replaces the type cast with a coercion
Henglein’s coercions c ::= Succ | Fail | D! | D? | Fun c c | c◦c D ::= Int | Bool | Fun coerce(T, T) = Succ coerce(Int, ?) = Int! coerce(?, Int) = Int?
Henglein’s coercions c ::= Succ | Fail | D! | D? | Fun c c | c◦c D ::= Int | Bool | Fun coerce(S1→S2, T1→T2) =Funcoerce(T1, S1) coerce(S2, T2) coerce(?, T1→T2) =coerce(?→?, T1→T2)◦Fun? coerce(T1→T2, ?) =Fun!◦coerce(T1→T2, ?→?)
Coercion normalization c◦Succ = c Succ◦c = c c◦Fail = Fail Fail◦c = Fail D?◦D! = Succ Int?◦Bool! = Fail (Fun d1 d2)◦(Fun c1 c2) = Fun (c1◦d1) (d2◦c2)
Dynamic semantics v ::= u | <c> u u ::= n | b | λx:S.t
Dynamic semantics E ::= [] | (E t) | (v E) | <c> P P ::= [] | (E t) | (v E) E[<c> (<d> t)] → E[<c◦d> t] E[(λx:S.t) v] → E[t[v/x]] E[(<Fun c d> u) v] → E[<d> (u (<c> v))] E[<Succ> u] → E[u] (if E ≠ E′[<c> []]) E[<Fail> u] →error (if E ≠ E′[<c> []])