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

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Static Contract Checking for Haskell Dana N. Xu University of Cambridge Ph.D. Supervisor: Simon Peyton Jones Microsoft Research Cambridge.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 How to transform an analyzer into a verifier. 2 OUTLINE OF THE LECTURE a verification technique which combines abstract interpretation and Park’s fixpoint.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Type checking © Marcelo d’Amorim 2010.
Chapter 7:: Data Types Programming Language Pragmatics
Compiler Construction
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Safety as a Software Metric Matthias Felleisen and Robert Corky Cartwright Rice University.
Subtyping COS 441 Princeton University Fall 2004.
Data Abstraction COS 441 Princeton University Fall 2004.
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] [
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
MinML: an idealized programming language CS 510 David Walker.
Type Inference with Run-time Logs Ravi Chugh. Motivation: Dynamic Languages Dynamically-typed languages – Enable rapid prototyping – Facilitate inter-language.
Aggregate Data Structures COS 441 Princeton University Fall 2004.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
1 Static Contract Checking for Haskell Dana N. Xu INRIA France Work done at University of Cambridge Joint work with Simon Peyton Jones Microsoft Research.
Programming Language Support for Generic Libraries Jeremy Siek and Walid Taha Abstract The generic programming methodology is revolutionizing the way we.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Martin-Gay, Beginning Algebra, 5ed 22 Location of NewportD1 Location of GatlinburgC2 Location of RobbinsvilleA5.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Imperative Languages Section 5 Continued.... A Dynamically Typed Language Variable may take on values from different data types. Run­time type checking.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Recursion on Lists Lecture 5, Programmeringsteknik del A.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Simplifying Algebraic Expressions. 1. Evaluate each expression using the given values of the variables (similar to p.72 #37-49)
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing.
CMSC 330: Organization of Programming Languages Operational Semantics.
1 Space-Efficient Gradual Typing David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Recursion Higher Order Functions CSCE 314 Spring 2016.
A Theory of Hygienic Macros David Herman Northeastern University.
The Ins and Outs of Gradual Type Inference Avik Chaudhuri Basil Hosmer Adobe Systems Aseem Rastogi Stony Brook University.
Chapter 4: Types. Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);
Hybrid Type Checking An implementation of λ H David Waern Rickard Nilsson.
Notes Over 1.2.
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Distributive Property
MEMORY REPRESENTATION OF STACKS
A lightening tour in 45 minutes
Stateful Manifest Contracts
Chapter 4: Types.
Functional Programming
Space-Efficient Gradual Typing
Continuation Marks A john clements talk.
Organization of Programming Languages
Language-based Security
Evaluating expressions and Properties of operations
Combining Compile-Time and Run-Time Components
Programming Languages Dan Grossman 2013
Static Contract Checking for Haskell
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 = x - 3 in …

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

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

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

13 Evaluation let y : 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 = x - 3 in …

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

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

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

20 Static semantics (Siek & Taha) Type compatibility E ⊢ e 1 : (S → T)E ⊢ e 2 : S ′ S ′ ~: S E ⊢ (e 1 e 2 ) : T

21 Static semantics (Siek & Taha) Type compatibility E ⊢ e 1 : (S → T)E ⊢ e 2 : S ′ S ′ ~: S E ⊢ (e 1 e 2 ) : T

22 Static semantics (Siek & Taha) Type compatibility ≠ subtyping! T ~: TT ~: ?? ~: T T 1 ~: S 1 S 2 ~: T 2 (S 1 → S 2 ) ~: (T 1 → T 2 )

23 Static semantics (Siek & Taha) Cast insertion E ⊢ e 1 ↪ t 1 : (S → T) E ⊢ e 2 ↪ t 2 : S ′ S ′ ~: S E ⊢ (e 1 e 2 ) ↪ (t 1 ( t 2 )) : T E ⊢ e ↪ t : T cast argument expression

24 Space Leaks

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

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

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

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

29 Space leaks even(n) →* odd(n - 1) →* even(n - 2) →* odd(n - 3) →* even(n - 4) →* odd(n - 5) →* even(n - 6) →* … 

30 Naïve Function Casts

31 Casts in functional languages n → n v → error: “v not an Int ” (if v ∉ Int ) λx:?.e → …

32 Casts in functional languages n → n 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

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

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

35 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 !) 

36 Space-Efficient Gradual Typing

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

38 Exploiting algebraic properties Closure under composition: ( v) = ( ◦ ) v

39 Exploiting algebraic properties Idempotence: ( v) = ( ◦ ) v = v

40 Exploiting algebraic properties Distributivity: ( ◦ ) v = v

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

42 Space-efficient gradual typing Generalize casts to coercions (Henglein, 1994) Change representation of casts from to Merge casts at runtime: ( e) → e →

43 Static semantics Type compatibility E ⊢ e 1 : (S → T)E ⊢ e 2 : S ′ S ′ ~: S E ⊢ (e 1 e 2 ) : T

44 Static semantics Type compatibility T ~: TT ~: ?? ~: T T 1 ~: S 1 S 2 ~: T 2 (S 1 → S 2 ) ~: (T 1 → T 2 )

45 Static semantics Cast insertion E ⊢ e 1 ↪ t 1 : (S → T) E ⊢ e 2 ↪ t 2 : S ′ c=coerce(S ′, S) E ⊢ (e 1 e 2 ) ↪ (t 1 ( t 2 )) : T E ⊢ e ↪ t : T computes a type coercion replaces the type cast with a coercion

46 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 ?

47 Henglein’s coercions c ::= Succ | Fail | D! | D? | Fun c c | c ◦ c D ::= Int | Bool | Fun coerce(S 1 → S 2, T 1 → T 2 ) = Fun coerce(T 1, S 1 ) coerce(S 2, T 2 ) coerce(?, T 1 → T 2 ) = coerce(? → ?, T 1 → T 2 ) ◦ Fun ? coerce(T 1 → T 2, ?) = Fun ! ◦ coerce(T 1 → T 2, ? → ?)

48 Coercion normalization c ◦ Succ = c Succ ◦ c = c c ◦ Fail = Fail Fail ◦ c = Fail D? ◦ D! = Succ Int ? ◦ Bool ! = Fail (Fun d 1 d 2 ) ◦ (Fun c 1 c 2 ) = Fun (c 1 ◦ d 1 ) (d 2 ◦ c 2 )

49 Dynamic semantics v ::= u | u u ::= n | b | λ x:S.t

50 Dynamic semantics E ::= [] | (E t) | (v E) | P P ::= [] | (E t) | (v E) E[ ( t)] → E[ t] E[( λ x:S.t) v] → E[t[v/x]] E[( u) v] → E[ (u ( v))] E[ u] → E[u] (if E ≠ E ′ [ []]) E[ u] → error (if E ≠ E ′ [ []])

51 Tail recursion even(n) →* odd(n - 1) →* even(n - 2) →* odd(n - 3) →* even(n - 4) →* odd(n - 5) →* even(n - 6) →* …

52 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 ) →* …

53 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

54 Earlier error detection ( e) → e → error: “ Int→Int ≠ Bool→Bool ”

55 Earlier error detection E ::= [] | (E t) | (v E) | P P ::= [] | (E t) | (v E) E[ ( t)] → E[ t] E[( λ x:S.t) v] → E[t[v/x]] E[( u) v] → E[ (u ( v))] E[ u] → E[u] (if E ≠ E ′ [ []]) E[ t] → error (if E ≠ E ′ [ []]) why bother evaluating t ?

56 Implementation

57 Coercions as continuation marks E [ e] E

58 Coercions as continuation marks E [ e] E ?→??→?

59 Coercions as continuation marks E [ e] E ?→??→?

60 Coercions as continuation marks E [ e] E Bool→Bool

61 Coercions as continuation marks E [e] E Bool→Bool

62 Alternative approaches Coercion-passing style λ(x,c).f(x,simplify(c ◦ d)) Trampoline λ(x).(d,λ().f(x))

63 Parting Thoughts

64 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  Clements (2004): Security-passing style

65 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

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