Logical Types for Scheme Sam Tobin-Hochstadt PLT @ Northeastern University NEPLS, April 29, 2010 1
What do these languages have in common? • COBOL • Scheme • Ruby • Haskell 2
What do these languages have in common? • COBOL [Komondoor 05] • Scheme [Tobin-Hochstadt 06] • Ruby [Furr 09] • Haskell [Vytiniotis 10] New static checks 3
What do these languages have in common? • COBOL [Komondoor 05] • Scheme [Tobin-Hochstadt 06] • Ruby [Furr 09] • Haskell [Vytiniotis 10] Millions of lines of code 4
Types for Existing Languages 5
What’s Hard? How programmers reason 6
What’s Hard? Simple Types How programmers reason 7
What’s Hard? Simple Types Reflection How programmers reason 8
What’s Hard? Simple Types Parametricity Reflection How programmers reason 9
What’s Hard? Simple Types Dependent Types Parametricity Reflection How programmers reason 10
What’s Hard? Simple Types Dependent Types Parametricity Reflection Generic Functions How programmers reason 11
What’s Hard? Simple Types Classes and Objects Dependent Types Parametricity Reflection Generic Functions How programmers reason 12
Checking Existing Code • New static checking is valuable for existing code Maintenance, Optimization, Trust • Work with existing idioms Survey, Analyze, Design, Validate 13
What Can We Learn? • New points in the design space Ruby, Scheme, ... • New type system ideas Occurrence Typing 14
Occurrence Typing 15
Simple Examples #lang typed/scheme (: twice : Any -> Number) (define (twice x) (if (number? x) (* 2 x) 0)) 16
Simple Examples #lang typed/scheme (: twice : Any -> Number) (define (twice x) (if (number? x) (* 2 x) 0)) Number x 17
Simple Examples #lang typed/scheme (: twice : (U Number String) -> Number) (define (twice x) (if (number? x) (* 2 x) (* 2 (string-length x)))) 18
Simple Examples #lang typed/scheme (: twice : (U Number String) -> Number) (define (twice x) (if (number? x) (* 2 x) (* 2 (string-length x)))) Number x 19
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(or (number? x) (string? x)) (twice x)] [else 0])) 20
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(or (number? x) (string? x)) (twice x)] [else 0])) Number x ∨ String x 21
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(or (number? x) (string? x)) (twice x)] [else 0])) Number x ∨ String x (U Number String) x 22
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(and (number? x) (string? y)) (+ (twice x) (twice y))] [else 0])) 23
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(and (number? x) (string? y)) (+ (twice x) (twice y))] [else 0])) Number x ∧ String y 24
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(and (number? x) (string? y)) (+ (twice x) (twice y))] [(number? x) (* 2 y)] [else 0])) 25
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(and (number? x) (string? y)) (+ (twice x) (twice y))] [(number? x) (* 2 y)] [else 0])) Number x ∨ String y 26
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(and (number? x) (string? y)) (+ (twice x) (twice y))] [(number? x) (* 2 y)] [else 0])) Number x ∨ String y , Number x String y 27
Logical Combination #lang typed/scheme (: twice : (U Number String) -> Number) (: g : Any (U Number String) -> Number) (define (g x y) (cond [(and (number? x) (string? y)) (+ (twice x) (twice y))] [(number? x) (* 2 y)] [else 0])) (U Number String) y , String y Number y 28
Data Structures #lang typed/scheme (: twice-car : (Pair Any Any) -> Number) (define (twice-car x) (if (number? (car x)) (* 2 (car x)) 0)) 29
Data Structures #lang typed/scheme (: twice-car : (Pair Any Any) -> Number) (define (twice-car x) (if (number? (car x)) (* 2 (car x)) 0)) Number_car(x) 30
Abstraction #lang typed/scheme (: car-num? : (Pair Any Any) -> Boolean : Number @ car) (define (car-num? x) (number? (car x))) 31
Abstraction #lang typed/scheme (: car-num? : (Pair Any Any) -> Boolean : Number @ car) (define (car-num? x) (number? (car x))) 32
Propositional Logic 33
Judgments Γ e : T ; φ 1 | φ 2 34
Judgments Γ e : T ; φ 1 | φ 2 e ::= n | c | ( λ x : T . e) | (e e) | (if e e e) 35
Judgments Γ e : T ; φ 1 | φ 2 T ::= Number | (U T ...) | #t | #f | (x:T -> T : φ | φ ) 36
Judgments Γ e : T ; φ 1 | φ 2 φ ::= T_ π (x) | T_ π (x) | φ 1 ∨ φ 2 | φ 1 ∧ φ 2 | φ 1 ⊃ φ 2 37
Judgments Γ e : T ; φ 1 | φ 2 φ ::= T_ π (x) | T_ π (x) | φ 1 ∨ φ 2 | φ 1 ∧ φ 2 | φ 1 ⊃ φ 2 38
Judgments Γ e : T ; φ 1 | φ 2 Γ ::= T_ π (x) ... 39
Judgments Γ e : T ; φ 1 | φ 2 Γ ::= φ ... 40
Judgments Γ φ 41
Judgments Γ φ Number x ∨ String y , Number x String y 42
Typing (if e 1 e 2 e 3 ) 43
Typing (if e 1 e 2 e 3 ) Γ e 1 : T 1 ; φ _+| φ _- 44
Typing (if e 1 e 2 e 3 ) Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 45
Typing (if e 1 e 2 e 3 ) Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 46
Typing Γ (if e 1 e 2 e 3 ) : T ; φ 1_+ ∨φ 2_+ | φ 1_- ∨φ 2_- Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 47
Typing Γ (if e 1 e 2 e 3 ) : T ; φ 1_+ ∨φ 2_+ | φ 1_- ∨φ 2_- Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 48
Typing Γ (if e 1 e 2 e 3 ) : T ; φ 1_+ ∨φ 2_+ | φ 1_- ∨φ 2_- Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 49
Typing Γ x : T 50
Typing Γ x : T Γ T x 51
Try Typed Scheme Installer and Documentation http://www.plt-scheme.org 52
Try Typed Scheme Installer and Documentation http://www.plt-scheme.org 53
Recommend
More recommend