1 / 61

Space-Efficient Gradual Typing

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:

presencia
Download Presentation

Space-Efficient Gradual Typing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Gradual Typing: Software evolution via hybrid type checking

  4. Dynamic vs. static typing Dynamic Typing 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 … Int

  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>45 - 3 in …

  14. Evaluation let y : Int = 45 - 3 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. 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. 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) = if (n = 0) 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) = if (n = 0) then k(false) else evenk(n – 1, <?→?>k)

  31. 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

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

  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 bykP·sizeOR(S)sizeOR(S) = size of Swithout any casts

  42. Earlier error detection <Int→Int>(<Bool→Bool> e) → <Fail→Fail> e → error: “incompatible casts”

  43. Implementation

  44. Continuation marks E [mark x = 1 in e end] E

  45. Continuation marks E [mark x = 1 ine end] x: 1 E x: 1

  46. Continuation marks E [e] x: 1 E x: 1

  47. Continuation marks and tail calls E′ [mark x = 2 in mark x = 3 in e end end] E′ x: 1

  48. Continuation marks and tail calls E′ [mark x = 2 in mark x = 3 in e end end] x: 2 E′ x: 1 x: 2

  49. Continuation marks and tail calls E′ [mark x = 3 in e end] x: 2 E′ x: 1 x: 2

  50. Continuation marks and tail calls E′ [mark x = 3 ine end] x: 3 E′ x: 1 x: 3

More Related