Logical Types for Untyped Languages Sam Tobin-Hochstadt & Matthias Felleisen PLT @ Northeastern University ICFP, September 27, 2010
Types for Untyped Languages: • Ruby [Furr et al 09] • Perl [Tang 06] • Thorn [Wrigstad et al 09] • ActionScript [Adobe 06] • Typed Racket
Types for Untyped Languages: • Reynolds 68 • Cartwright 75 • Wright & Cartwright 94 • Henglein & Rehof 95
"Some account should be taken of the premises in conditional expressions." Reynolds, 1968
"Type testing predicates aggravate the loss of static type information." Henglein & Rehof, 1995
Types and Predicates
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))]))
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) n (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n : Peano n : Peano
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] n [else (add1 (convert (rest n)))])) n : Peano n : Peano
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] 0 [else (add1 (convert (rest n)))])) n : 'Z n : 'Z
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) n n : (List 'S Peano) n : List 'S Peano
(: 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*))]))
(: combine : (U String Symbol) (U String Symbol) -> String) (define (combine s s*) 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* : (U String Symbol) s* : U String Symbol
(: combine : (U String Symbol) (U String Symbol) -> String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s s*)] 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* : String s* : String
(: 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*))] s s* [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) s : String s : String s* : Symbol s* : Symbol
(: 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*)] s s* [else (string-append (symbol->string s) (symbol->string s*))])) s : Symbol s : Symbol s* : String s* : String
(: 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) s (symbol->string s*))])) s* s : Symbol s : Symbol s* : Symbol s* : Symbol
Types and Propositions
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))]))
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) add1 convert rest n n : (List 'S Peano) n : List 'S Peano
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] [else (add1 (convert (rest n)))])) add1 convert rest n ⊢ (List 'S Peano) @ n ⊢ List 'S Peano @ n
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] symbol? n [else (add1 (convert (rest n)))])) ⊢ Symbol @ n ⊢ (List 'S Peano) @ n
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] symbol? n [else (add1 (convert (rest n)))])) ⊢ Peano @ n ⊢ Symbol @ n ⊢ Symbol @ n ⊢ (List 'S Peano) @ n
(define-type Peano (U 'Z (List 'S Peano))) (: convert : Peano -> Number) (define (convert n) (cond [(symbol? n) 0] symbol? n [else (add1 (convert (rest n)))])) ⊢ (U 'Z (List 'S Peano)) @ n ⊢ Symbol @ n ⊢ Symbol @ n ⊢ (List 'S Peano) @ n
(: 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*))]))
(: combine : (U String Symbol) (U String Symbol) -> String) (define (combine s s*) (cond [(and (string? s) (string? s*)) (string-append s 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*
(: combine : (U String Symbol) (U String Symbol) -> String) (define (combine s s*) (cond [(and (string? s) (string? s*)) 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*
(: 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-append s symbol->string s* [(string? s*) (string-append (symbol->string s) s*)] [else (string-append (symbol->string s) (symbol->string s*))])) ⊢ Symbol @ s* Symbol @ s*
(: combine : (U String Symbol) (U String Symbol) -> String) (define (combine s s*) (cond [(and (string? s) (string? s*)) and string? s string? s* (string-append s s*)] [(string? 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 ⊢ Symbol @ s*
(string? s )
(string? s ) String @ s
(string? s ) String @ s | String @ s
(x:Any -> Bool : String@x | String@x) x:Any -> Bool : String@x | String@x s s (string? string? s ) s String @ s | String @ s
Latent Propositions (x:Any -> Bool : String@x | String@x) Objects x:Any -> Bool : String@x | String@x s s (string? string? s ) s String @ s | String @ s Propositions
Latent Propositions (x:Any -> Bool : String@x | String@x) Objects x:Any -> Bool : String@x | String@x car(s) car(s) (string? (car s)) string? car s String @ car(s) | String @ car(s) Propositions
( λ ([s : (Pair Any Any)]) (string? (car s))) String @ car(s) | String @ car(s)
( λ ([s : (Pair Any Any)]) (string? (car s))) (s:(Pair Any Any) -> Bool : String @ car(s) | String @ car(s))
Propositional Logic
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o e ::= n | c | ( λ x : T . e) | (e e) | (if e e e)
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o T ::= Number | (U T ...) | #t | #f | (x:T -> T : φ | φ )
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o φ ::= T@ π (x) | T@ π (x) | φ 1 ∨ φ 2 | φ 1 ∧ φ 2 | φ 1 ⊃ φ 2
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o Γ ::= x:T ...
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o Γ ::= x:T T@ π (x) ...
Judgments Γ ⊢ e : T ; φ 1 | φ 2 ; o Γ ::= x:T T@ π (x) φ ...
Judgments Γ ⊢ φ
Judgments Γ ⊢ φ Number @ x ∨ String @ y , Number @ x ⊢ String @ y
Typing (if e 1 e 2 e 3 )
Typing Γ , φ + ⊢ e 2 : T ; φ 1+ | φ 1- ; o Γ , φ - ⊢ e 3 : T ; φ 2+ | φ 2- ; o Γ ⊢ e 1 : T' ; φ + | φ - ; o' (if e 1 e 2 e 3 )
Typing Γ , φ + ⊢ e 2 : T ; φ 1+ | φ 1- ; o Γ , φ - ⊢ e 3 : T ; φ 2+ | φ 2- ; o Γ ⊢ e 1 : T' ; φ + | φ - ; o' Γ ⊢ (if e 1 e 2 e 3 ) : T ; φ 1+ ∨φ 2+ | φ 1- ∨φ 2- ; o
Typing Γ ⊢ x : T
Typing Γ ⊢ T x Γ ⊢ x : T
Evaluation
Recommend
More recommend