logical types for scheme
play

Logical Types for Scheme Sam Tobin-Hochstadt PLT @ Northeastern - PowerPoint PPT Presentation

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


  1. Logical Types for Scheme Sam Tobin-Hochstadt PLT @ Northeastern University NEPLS, April 29, 2010 1

  2. What do these languages have in common? • COBOL • Scheme • Ruby • Haskell 2

  3. What do these languages have in common? • COBOL [Komondoor 05] • Scheme [Tobin-Hochstadt 06] • Ruby [Furr 09] • Haskell [Vytiniotis 10] New static checks 3

  4. 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

  5. Types for Existing Languages 5

  6. What’s Hard? How programmers reason 6

  7. What’s Hard? Simple Types How programmers reason 7

  8. What’s Hard? Simple Types Reflection How programmers reason 8

  9. What’s Hard? Simple Types Parametricity Reflection How programmers reason 9

  10. What’s Hard? Simple Types Dependent Types Parametricity Reflection How programmers reason 10

  11. What’s Hard? Simple Types Dependent Types Parametricity Reflection Generic Functions How programmers reason 11

  12. What’s Hard? Simple Types Classes and Objects Dependent Types Parametricity Reflection Generic Functions How programmers reason 12

  13. Checking Existing Code • New static checking is valuable for existing code Maintenance, Optimization, Trust • Work with existing idioms Survey, Analyze, Design, Validate 13

  14. What Can We Learn? • New points in the design space Ruby, Scheme, ... • New type system ideas Occurrence Typing 14

  15. Occurrence Typing 15

  16. Simple Examples #lang typed/scheme (: twice : Any -> Number) (define (twice x) (if (number? x) (* 2 x) 0)) 16

  17. Simple Examples #lang typed/scheme (: twice : Any -> Number) (define (twice x) (if (number? x) (* 2 x) 0)) Number x 17

  18. Simple Examples #lang typed/scheme (: twice : (U Number String) -> Number) (define (twice x) (if (number? x) (* 2 x) (* 2 (string-length x)))) 18

  19. 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

  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])) 20

  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 21

  22. 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

  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])) 23

  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))] [else 0])) Number x ∧ String y 24

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

  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 26

  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])) Number x ∨ String y , Number x String y 27

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

  29. Data Structures #lang typed/scheme (: twice-car : (Pair Any Any) -> Number) (define (twice-car x) (if (number? (car x)) (* 2 (car x)) 0)) 29

  30. 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

  31. Abstraction #lang typed/scheme (: car-num? : (Pair Any Any) -> Boolean : Number @ car) (define (car-num? x) (number? (car x))) 31

  32. Abstraction #lang typed/scheme (: car-num? : (Pair Any Any) -> Boolean : Number @ car) (define (car-num? x) (number? (car x))) 32

  33. Propositional Logic 33

  34. Judgments Γ e : T ; φ 1 | φ 2 34

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

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

  37. Judgments Γ e : T ; φ 1 | φ 2 φ ::= T_ π (x) | T_ π (x) | φ 1 ∨ φ 2 | φ 1 ∧ φ 2 | φ 1 ⊃ φ 2 37

  38. Judgments Γ e : T ; φ 1 | φ 2 φ ::= T_ π (x) | T_ π (x) | φ 1 ∨ φ 2 | φ 1 ∧ φ 2 | φ 1 ⊃ φ 2 38

  39. Judgments Γ e : T ; φ 1 | φ 2 Γ ::= T_ π (x) ... 39

  40. Judgments Γ e : T ; φ 1 | φ 2 Γ ::= φ ... 40

  41. Judgments Γ φ 41

  42. Judgments Γ φ Number x ∨ String y , Number x String y 42

  43. Typing (if e 1 e 2 e 3 ) 43

  44. Typing (if e 1 e 2 e 3 ) Γ e 1 : T 1 ; φ _+| φ _- 44

  45. Typing (if e 1 e 2 e 3 ) Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 45

  46. Typing (if e 1 e 2 e 3 ) Γ e 1 : T 1 ; φ _+| φ _- Γ , φ _+ e 2 : T ; φ 1_+| φ 1_- Γ , φ _- e 3 : T ; φ 2_+| φ 2_- 46

  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_- 47

  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_- 48

  49. 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

  50. Typing Γ x : T 50

  51. Typing Γ x : T Γ T x 51

  52. Try Typed Scheme Installer and Documentation http://www.plt-scheme.org 52

  53. Try Typed Scheme Installer and Documentation http://www.plt-scheme.org 53

Recommend


More recommend