Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Logical Types for Untyped Languages

Logical Types for Untyped Languages

From ICFP 2010.

Sam Tobin-Hochstadt

September 26, 2011
Tweet

More Decks by Sam Tobin-Hochstadt

Other Decks in Programming

Transcript

  1. Logical Types for Untyped Languages Sam Tobin-Hochstadt & Matthias Felleisen

    PLT @ Northeastern University ICFP, September 27, 2010
  2. Types for Untyped Languages: • Ruby [Furr et al 09]

    • Perl [Tang 06] • Thorn [Wrigstad et al 09] • ActionScript [Adobe 06] • Typed Racket
  3. Types for Untyped Languages: • Reynolds 68 • Cartwright 75

    • Wright & Cartwright 94 • Henglein & Rehof 95
  4. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))]))
  5. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n : Peano n n : Peano
  6. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n : Peano n n : Peano
  7. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n : 'Z 0 n : 'Z
  8. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n : (List 'S Peano) n n : List 'S Peano
  9. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))]))
  10. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) s : (U String Symbol) s* : (U String Symbol) s s : U String Symbol s* s* : U String Symbol
  11. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) s : String s* : String s s : String s* s* : String
  12. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) s : String s* : Symbol s s : String s* s* : Symbol
  13. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) s : Symbol s* : String s s : Symbol s* s* : String
  14. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) s : Symbol s* : Symbol s s : Symbol s* s* : Symbol
  15. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))]))
  16. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n : (List 'S Peano) add1 convert rest n n : List 'S Peano
  17. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) ⊢ (List 'S Peano) @ n add1 convert rest n ⊢ List 'S Peano @ n
  18. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) ⊢ (List 'S Peano) @ n symbol? n ⊢ Symbol @ n
  19. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) ⊢ Peano @ n ⊢ Symbol @ n ⊢ (List 'S Peano) @ n symbol? n ⊢ Symbol @ n
  20. (define-type Peano (U 'Z (List 'S Peano))) (: convert :

    Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) ⊢ (U 'Z (List 'S Peano)) @ n ⊢ Symbol @ n ⊢ (List 'S Peano) @ n symbol? n ⊢ Symbol @ n
  21. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))]))
  22. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) ⊢ String @ s ⊢ String @ s* string-append s s* ⊢ String @ s ⊢ String @ s*
  23. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) ⊢ String @ s ∧ String @ s* ⊢ String @ s ⊢ String @ s* string? s String @ s string? s* String @ s*
  24. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) ⊢ Symbol @ s* string-append s symbol->string s* Symbol @ s*
  25. (: combine : (U String Symbol) (U String Symbol) ->

    String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] [(string? s) (string-append s (symbol->string s*))] [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) ⊢ String @ s ⊢ String @ s ⊃ String @ s* ⊢ Symbol @ s* and string? s string? s* String @ s ⊃ String @ s* string? s String @ s
  26. (x:Any -> Bool : String@x | String@x) s (string? s

    ) String @ s | String @ s x:Any -> Bool : String@x | String@x string? s s
  27. Latent Propositions (x:Any -> Bool : String@x | String@x) Objects

    s (string? s ) String @ s | String @ s Propositions x:Any -> Bool : String@x | String@x string? s s
  28. Latent Propositions (x:Any -> Bool : String@x | String@x) Objects

    car(s) (string? (car s)) String @ car(s) | String @ car(s) Propositions x:Any -> Bool : String@x | String@x string? car s car(s)
  29. (λ ([s : (Pair Any Any)]) (string? (car s))) (s:(Pair

    Any Any) -> Bool : String @ car(s) | String @ car(s))
  30. Judgments Γ ⊢ e : T ; φ1 | φ2

    ; o e ::= n | c | (λ x : T . e) | (e e) | (if e e e)
  31. Judgments Γ ⊢ e : T ; φ1 | φ2

    ; o T ::= Number | (U T ...) | #t | #f | (x:T -> T : φ|φ)
  32. Judgments Γ ⊢ e : T ; φ1 | φ2

    ; o φ ::= T@π(x) | T@π(x) | φ1 ∨ φ2 | φ1 ∧ φ2 | φ1 ⊃ φ2
  33. Judgments Γ ⊢ e : T ; φ1 | φ2

    ; o Γ ::= x:T T@π(x) ...
  34. Judgments Γ ⊢ e : T ; φ1 | φ2

    ; o Γ ::= x:T T@π(x) φ ...
  35. Judgments Γ ⊢ φ Number @ x ∨ String @

    y , Number @ x ⊢ String @ y
  36. Typing Γ,φ+ ⊢ e2 : T ; φ1+ |φ1- ;

    o Γ,φ- ⊢ e3 : T ; φ2+ |φ2- ; o Γ ⊢ e1 : T' ; φ+ |φ- ; o' (if e1 e2 e3 )
  37. Typing Γ,φ+ ⊢ e2 : T ; φ1+ |φ1- ;

    o Γ,φ- ⊢ e3 : T ; φ2+ |φ2- ; o Γ ⊢ e1 : T' ; φ+ |φ- ; o' Γ ⊢ (if e1 e2 e3 ) : T ; φ1+ ∨φ2+ | φ1- ∨φ2- ; o
  38. Local Binding Γ ⊢ e0 : S ; φ*+ |

    φ*- ; o Γ, S@x, #f @ x ⊃ φ*+ ⊢ e1 : T ; φ+ | φ- ; o* Γ ⊢ (let [x e0 ] e1 ) : T[o/x] ; φ+ | φ- [o/x] ; o*[o/x]
  39. Empirical Evaluation Estimated usage of two idioms in Racket code

    base (600k lines) • Local binding with or: ~470 uses • Predicates with Selectors: ~440 uses
  40. Prior Work None of the Examples Shivers 91, Henglein &

    Rehof 95, Crary et al 98, ... Just convert Aiken et al 94, Wright 94, Flanagan 97, Komondoor et al 2005 Everything but abstraction Bierman et al 2010