Presentation is loading. Please wait.

Presentation is loading. Please wait.

Space-Efficient Gradual Typing

Similar presentations


Presentation on theme: "Space-Efficient Gradual Typing"— Presentation transcript:

1 Space-Efficient Gradual Typing
David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz

2 The point Naïve type conversions in functional programming languages are not safe for space. But they can and should be.

3 Software evolution via hybrid type checking
Gradual Typing: Software evolution via hybrid type checking

4 Dynamic vs. static typing

5 Gradual typing Dynamic Typing Static Typing

6 Type checking let x = f() in … let y : Int = x - 3 in …

7 Type checking let x : ? = f() in … let y : Int = x - 3 in …

8 Type checking let x : ? = f() in … let y : Int = x - 3 in …
- : Int × Int → Int

9 Type checking let x : ? = f() in … let y : Int = <Int>x - 3 in …

10 Type checking let x : ? = f() in … let y : Int = <Int>x - 3 in …

11 Evaluation let x : ? = f() in … let y : Int = <Int>x - 3 in …

12 Evaluation let x : ? = 45 in … let y : Int = <Int>x - 3 in …

13 Evaluation let y : Int = <Int> in …

14 Evaluation let y : Int = in …

15 Evaluation let y : Int = 42 in …

16 Evaluation (take 2) let x : ? = f() in … let y : Int = <Int>x - 3 in …

17 Evaluation (take 2) let x : ? = true in … let y : Int = <Int>x - 3 in …

18 Evaluation (take 2) let y : Int = <Int>true - 3 in …

19 error: “true is not an Int”
Evaluation (take 2) error: “true is not an Int”

20 Space Leaks

21 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)

22 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)

23 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)

24 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!

25 x 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

26 Naïve Function Casts

27 Casts in functional languages
<Int>n → n <Int>v → error: “failed cast” (if v∉Int) <σ→τ>λx:?.e → …

28 Casts in functional languages
<Int>n → n <Int>v → error: “failed cast” (if v∉Int) <σ→τ>λx:?.e → λz:σ.<τ>((λx:?.e) z) Very useful, very popular… unsafe for space. fresh, typed proxy cast result

29 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) = then k(false) else evenk(n – 1, k)

30 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) = then k(false) else evenk(n – 1, <?→?>k)

31 x More space leaks (…without even using k0!) 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

32 Space-Efficient Gradual Typing

33 Intuition Casts are like function restrictions (Findler and Blume, 2006) Can their representation exploit the properties of restrictions?

34 Exploiting algebraic properties
Closure under composition: <Bool>(<Bool> v) = (<Bool>◦<Bool>) v

35 Exploiting algebraic properties
Idempotence: <Bool>(<Bool> v) = (<Bool>◦<Bool>) v = <Bool> v

36 Exploiting algebraic properties
Distributivity: (<?→?>◦<Bool→Bool>) v = <(Bool◦?)→(?◦Bool)> v

37 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! <c>(<d> e) <c◦d>e merged before evaluating e

38 Space-efficient gradual typing
Generalize casts to coercions (Henglein, 1994) Change representation of casts from <τ> to <c> Merge casts at runtime: <c>(<d> e) <c◦d>e <c′>e

39 ü Tail recursion even(n) →* odd(n - 1) →* <Bool>even(n - 2)
→* <Bool>odd(n - 3) →* <Bool><Bool>even(n - 4) →* <Bool>even(n - 4) →* <Bool>odd(n - 5) →* <Bool><Bool>even(n - 6) →* <Bool>even(n - 6) →* … ü

40 ü Bounded proxies evenk(n, k0) →* oddk(n - 1, <Bool→Bool>k0)
→* evenk(n - 2, <?→?><Bool→Bool>k0) →* evenk(n - 2, <Bool→Bool>k0) →* oddk(n - 3, <Bool→Bool>k0) →* evenk(n - 4, <?→?><Bool→Bool>k0) →* evenk(n - 4, <Bool→Bool>k0) →* oddk(n - 5, <Bool→Bool>k0) →* … ü

41 Guaranteed. Theorem: any program state S during evaluation of a program P is bounded by kP · sizeOR(S) sizeOR(S) = size of S without any casts

42 Related work Gradual typing Function proxies Coercions
Siek and Taha (2006, 2007) Function proxies Findler and Felleisen (1998, 2006): Software contracts Gronski, Knowles, Tomb, Freund, Flanagan (2006): Hybrid typing, Sage Tobin-Hochstadt and Felleisen (2006): Interlanguage migration Coercions Henglein (1994): Dynamic typing Space efficiency Clinger (1998): Proper tail recursion

43 Contributions Space-safe representation and semantics of casts for functional languages Supports function casts and tail recursion Three implementation strategies (coercion-passing style, trampoline, continuation marks) Earlier error detection (see paper) Proof of space efficiency

44 The point, again Naïve type conversions in functional programming languages are not safe for space. But they can and should be. Thank you.


Download ppt "Space-Efficient Gradual Typing"

Similar presentations


Ads by Google