space efficient gradual typing n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Space-Efficient Gradual Typing PowerPoint Presentation
Download Presentation
Space-Efficient Gradual Typing

Loading in 2 Seconds...

play fullscreen
1 / 61

Space-Efficient Gradual Typing - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Space-Efficient Gradual Typing' - presencia


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
space efficient gradual typing

Space-Efficient Gradual Typing

David Herman

Northeastern University

Aaron Tomb, Cormac Flanagan

University of California, Santa Cruz

the point
The point

Naïve type conversions in functional programming languages are not safe for space.

But they can and should be.

slide3
Gradual Typing:

Software evolution via hybrid type checking

dynamic vs static typing
Dynamic vs. static typing

Dynamic

Typing

Static

Typing

gradual typing
Gradual typing

Dynamic

Typing

Static

Typing

type checking
Type checking

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

type checking1
Type checking

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

type checking2
Type checking

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

- : Int × Int → Int

type checking3
Type checking

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

type checking4
Type checking

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

Int

evaluation
Evaluation

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

evaluation1
Evaluation

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

evaluation2
Evaluation

let y : Int = <Int>45 - 3 in …

evaluation3
Evaluation

let y : Int = 45 - 3 in …

evaluation4
Evaluation

let y : Int = 42 in …

evaluation take 2
Evaluation (take 2)

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

evaluation take 21
Evaluation (take 2)

let x : ? = true in … let y : Int = <Int>x - 3 in …

evaluation take 22
Evaluation (take 2)

let y : Int = <Int>true - 3 in …

evaluation take 23
Evaluation (take 2)

error: “true is not an Int”

space leaks
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 leaks1
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 leaks2
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 leaks3
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 leaks4
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
Casts in functional languages

<Int>n → n

<Int>v → error: “failed cast”(if v∉Int)

<σ→τ>λx:?.e → …

casts in functional languages1
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

more space leaks
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 leaks1
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 leaks2
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
Intuition

Casts are like function restrictions(Findler and Blume, 2006)

Can their representation exploit the properties of restrictions?

exploiting algebraic properties
Exploiting algebraic properties

Closure under composition:

<Bool>(<Bool> v) = (<Bool>◦<Bool>) v

exploiting algebraic properties1
Exploiting algebraic properties

Idempotence:

<Bool>(<Bool> v) = (<Bool>◦<Bool>) v= <Bool> v

exploiting algebraic properties2
Exploiting algebraic properties

Distributivity:

(<?→?>◦<Bool→Bool>) v

= <(Bool◦?)→(?◦Bool)> v

space efficient gradual typing1
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 typing2
Space-efficient gradual typing
  • Generalize casts to coercions(Henglein, 1994)
  • Change representation of casts from <τ> to <c>
  • Merge casts at runtime:
tail recursion
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)

→* …

ü

bounded proxies
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)

→* …

ü

guaranteed
Guaranteed.

Theorem: any program state S during evaluation of a program P is bounded bykP·sizeOR(S)sizeOR(S) = size of Swithout any casts

earlier error detection
Earlier error detection

<Int→Int>(<Bool→Bool> e)

→ <Fail→Fail> e

→ error: “incompatible casts”

continuation marks
Continuation marks

E [mark x = 1 in e end]

E

continuation marks1
Continuation marks

E [mark x = 1 ine end]

x: 1

E

x: 1

continuation marks2
Continuation marks

E [e]

x: 1

E

x: 1

continuation marks and tail calls
Continuation marks and tail calls

E′ [mark x = 2 in mark x = 3 in e end end]

E′

x: 1

continuation marks and tail calls1
Continuation marks and tail calls

E′ [mark x = 2 in mark x = 3 in e end end]

x: 2

E′

x: 1

x: 2

continuation marks and tail calls2
Continuation marks and tail calls

E′ [mark x = 3 in e end]

x: 2

E′

x: 1

x: 2

continuation marks and tail calls3
Continuation marks and tail calls

E′ [mark x = 3 ine end]

x: 3

E′

x: 1

x: 3

continuation marks and tail calls4
Continuation marks and tail calls

E′ [e]

x: 3

E′

x: 1

x: 3

coercions as continuation marks
Coercions as continuation marks

E [<?→?><Bool→Bool> e]

E

coercions as continuation marks1
Coercions as continuation marks

E [<?→?><Bool→Bool> e]

E

?→?

coercions as continuation marks2
Coercions as continuation marks

E [<Bool→Bool> e]

E

?→?

coercions as continuation marks3
Coercions as continuation marks

E [<Bool→Bool> e]

E

Bool→Bool

alternative approaches
Alternative approaches
  • Coercion-passing styleλ(x,c).f(x,simplify(c◦d))
  • Trampolineλ(x).(d,λ().f(x))
related work
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
contributions
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
the point again
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