Download presentation
Presentation is loading. Please wait.
Published byCharlene Miller Modified over 8 years ago
1
1 Space-Efficient Gradual Typing David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz
2
2 The point Naïve type conversions in functional programming languages are not safe for space. But they can and should be.
3
3 Gradual Typing: Software evolution via hybrid type checking
4
4 Dynamic vs. static typing Dynamic Typing Static Typing
5
5 Gradual typing Dynamic Typing Static Typing
6
6 Type checking let x = f() in … let y : Int = x - 3 in …
7
7 Type checking let x : ? = f() in … let y : Int = x - 3 in …
8
8 Type checking let x : ? = f() in … let y : Int = x - 3 in … - : Int × Int → Int
9
9 Type checking let x : ? = f() in … let y : Int = x - 3 in …
10
10 Type checking let x : ? = f() in … let y : Int = x - 3 in … Int
11
11 Evaluation let x : ? = f() in … let y : Int = x - 3 in …
12
12 Evaluation let x : ? = 45 in … let y : Int = x - 3 in …
13
13 Evaluation let y : Int = 45 - 3 in …
14
14 Evaluation let y : Int = 45 - 3 in …
15
15 Evaluation let y : Int = 42 in …
16
16 Evaluation (take 2) let x : ? = f() in … let y : Int = x - 3 in …
17
17 Evaluation (take 2) let x : ? = true in … let y : Int = x - 3 in …
18
18 Evaluation (take 2) let y : Int = true - 3 in …
19
19 Evaluation (take 2) error: “true is not an Int”
20
20 Space Leaks
21
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
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
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 even(n - 1)
24
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 even(n - 1) non-tail call!
25
25 Space leaks even(n) →* odd(n - 1) →* even(n - 2) →* odd(n - 3) →* even(n - 4) →* odd(n - 5) →* even(n - 6) →* …
26
26 Naïve Function Casts
27
27 Casts in functional languages n → n v → error: “failed cast” (if v ∉ Int ) λx:?.e → …
28
28 Casts in functional languages n → n 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
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
30 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)
31
31 More space leaks evenk(n, k 0 ) →* oddk(n - 1, k 0 ) →* oddk(n - 1, λz:Bool. k 0 (z)) →* evenk(n - 2, λz:Bool. k 0 (z)) →* evenk(n - 2, λy:?.(λz:Bool. k 0 (z))(y)) →* oddk(n - 3, λy:?.(λz:Bool. k 0 (z))(y)) →* oddk(n – 3, λx:Bool.(λy:?.(λz:Bool. k 0 (z))(y))(x)) →* evenk(n - 4, λx:Bool.(λy:?.(λz:Bool. k 0 (z))(y))(x)) →* evenk(n - 4, λw:?.(λx:Bool.(λy:?.(λz:Bool. k 0 (z))(y))(x))(w)) →* oddk(n - 5, λw:?.(λx:Bool.(λy:?.(λz:Bool. k 0 (z))(y))(x))(w)) →* oddk(n - 5, λv:Bool. (λw:?.(λx:Bool.(λy:?.(λz:Bool. k 0 (z))(y))(x))(w))(v)) →* … (…without even using k 0 !)
32
32 Space-Efficient Gradual Typing
33
33 Intuition Casts are like function restrictions (Findler and Blume, 2006) Can their representation exploit the properties of restrictions?
34
34 Exploiting algebraic properties Closure under composition: ( v) = ( ◦ ) v
35
35 Exploiting algebraic properties Idempotence: ( v) = ( ◦ ) v = v
36
36 Exploiting algebraic properties Distributivity: ( ◦ ) v = v
37
37 Space-efficient gradual typing Generalize casts to coercions (Henglein, 1994) Change representation of casts from to Merge casts at runtime: ( e) → e merged before evaluating e This coercion can be simplified!
38
38 Space-efficient gradual typing Generalize casts to coercions (Henglein, 1994) Change representation of casts from to Merge casts at runtime: ( e) → e →
39
39 Tail recursion even(n) →* odd(n - 1) →* even(n - 2) →* odd(n - 3) →* even(n - 4) →* odd(n - 5) →* even(n - 6) →* …
40
40 Bounded proxies evenk(n, k 0 ) →* oddk(n - 1, k 0 ) →* evenk(n - 2, k 0 ) →* oddk(n - 3, k 0 ) →* evenk(n - 4, k 0 ) →* oddk(n - 5, k 0 ) →* …
41
41 Guaranteed. Theorem: any program state S during evaluation of a program P is bounded by k P · size OR (S) size OR (S) = size of S without any casts
42
42 Earlier error detection ( e) → e → error: “incompatible casts”
43
43 Implementation
44
44 Continuation marks E [ mark x = 1 in e end ] E
45
45 Continuation marks E [ mark x = 1 in e end ] x: 1 E
46
46 Continuation marks E [e] x: 1 E
47
47 Continuation marks and tail calls E′ [ mark x = 2 in mark x = 3 in e end end ] x: 1 E′
48
48 Continuation marks and tail calls E′ [ mark x = 2 in mark x = 3 in e end end ] x: 1 x: 2 E′ x: 2
49
49 Continuation marks and tail calls E′ [ mark x = 3 in e end ] x: 1 x: 2 E′ x: 2
50
50 Continuation marks and tail calls E′ [ mark x = 3 in e end ] x: 3 x: 1 E′ x: 3
51
51 Continuation marks and tail calls E′ [e] x: 3 x: 1 E′ x: 3
52
52 Coercions as continuation marks E [ e] E
53
53 Coercions as continuation marks E [ e] E ?→??→?
54
54 Coercions as continuation marks E [ e] E ?→??→?
55
55 Coercions as continuation marks E [ e] E Bool→Bool
56
56 Coercions as continuation marks E [e] E Bool→Bool
57
57 Alternative approaches Coercion-passing style λ(x,c).f(x,simplify(c ◦ d)) Trampoline λ(x).(d,λ().f(x))
58
58 Parting Thoughts
59
59 Related work Gradual typing 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
60
60 Contributions Space-safe representation and semantics of casts for functional languages Supports function casts and tail recursion Earlier error detection Proof of space efficiency Three implementation strategies
61
61 The point, again Naïve type conversions in functional programming languages are not safe for space. But they can and should be. Thank you. dherman@ccs.neu.edu
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.