SLIDE 1
Well-typed programs cant be blamed Philip Wadler University of - - PowerPoint PPT Presentation
Well-typed programs cant be blamed Philip Wadler University of - - PowerPoint PPT Presentation
Well-typed programs cant be blamed Philip Wadler University of Edinburgh Robert Bruce Findler University of Chicago The mathematics of programming languages is deep and elegant examples other than Curry-Howard? terms other than
SLIDE 2
SLIDE 3
SLIDE 4
SLIDE 5
SLIDE 6
A repeated theme
Thatte (1988): Partial types Henglein (1994): Dynamic typing Findler and Felleisen (2002): Contracts Flanagan (2006): Hybrid types Siek and Taha (2006): Gradual types
SLIDE 7
A repeated theme
Visual Basic 9.0 Perl 6.0 ECMAScript 4.0
SLIDE 8
Evolving a program
SLIDE 9
An untyped program
⌈let x = 2 f = λy. y + 1 h = λg. g (g x) in h f⌉ − → ⌈4⌉
SLIDE 10
A typed program
let x = 2 f = λy : Int. y + 1 h = λg : Int → Int. g (g x) in h f − → 4 : Int
SLIDE 11
A partly typed program—narrowing
let x = 2 f = Int → Int ⇐ Dynp ⌈λy. y + 1⌉ h = λg : Int → Int. g (g x) in h f − → 4 : Int
SLIDE 12
A partly typed program—narrowing
let x = 2 f = Int → Int ⇐ Dynp ⌈λy. ’b’⌉ h = λg : Int → Int. g (g x) in h f − → blame p Positive (covariant): blame the term contained in the cast
SLIDE 13
Another partly typed program—widening
let x = ⌈2⌉ f = Dyn ⇐ Int → Intp (λy : Int. y + 1) h = ⌈λg. g (g x)⌉ in ⌈h f⌉ − → ⌈4⌉
SLIDE 14
Another partly typed program—widening
let x = ⌈’a’⌉ f = Dyn ⇐ Int → Intp (λy : Int. y + 1) h = ⌈λg. g (g x)⌉ in ⌈h f⌉ − → blame ¯ p Negative (contravariant): blame the context containg the cast
SLIDE 15
The Blame Game
SLIDE 16
Blame
Int ⇐ Dynp ⌈2⌉ − → 2 Int ⇐ Dynp ⌈’a’⌉ − → blame p
SLIDE 17
The Blame Game—widening
(Dyn → Dyn ⇐ Int → Intp (λy : Int. y + 1)) ⌈2⌉ − → Dyn ⇐ Intp ((λy : Int. y + 1) (Int ⇐ Dyn¯
p ⌈2⌉))
− → ⌈3⌉
SLIDE 18
The Blame Game—widening
(Dyn → Dyn ⇐ Int → Intp (λy : Int. y + 1)) ⌈’a’⌉ − → Dyn ⇐ Intp ((λy : Int. y + 1) (Int ⇐ Dyn¯
p ⌈’a’⌉))
− → blame ¯ p Widening can give rise to negative blame, but never positive blame
SLIDE 19
The Blame Game—narrowing
(Int → Int ⇐ Dyn → Dynp (λy : Dyn. ⌈y + 1⌉)) 2 − → Int ⇐ Dynp ((λy : Dyn. ⌈y + 1⌉) (Dyn ⇐ Int¯
p 2))
− → 3
SLIDE 20
The Blame Game—narrowing
(Int → Int ⇐ Dyn → Dynp (λy : Dyn. ⌈’b’⌉)) 2 − → Int ⇐ Dynp ((λy : Dyn. ⌈’b’⌉) (Dyn ⇐ Int¯
p 2))
− → blame p Narrowing can give rise to positive blame, but never negative blame
SLIDE 21
Untyped and supertyped
SLIDE 22
Untyped = Uni-typed
⌈x⌉ = x ⌈n⌉ = Dyn ⇐ Int n ⌈λx. N⌉ = Dyn ⇐ Dyn → Dyn (λx : Dyn. ⌈N⌉) ⌈L M⌉ = (Dyn → Dyn ⇐ Dyn ⌈L⌉) ⌈M⌉ (slogan due to Bob Harper)
SLIDE 23
Contracts
Nat = {x : Int | x ≥ 0} let x = Nat ⇐ Int 2 f = Nat → Nat ⇐ Int → Int (λy : Int. y + 1) h = λg : Nat → Nat. g (g x) in h f − → 4Nat : Nat
SLIDE 24
Subtyping
SLIDE 25
Subtype
Dyn <: Dyn S′ <: S T <: T ′ S → T <: S′ → T ′ s implies t {x : B | s} <: {x : B | t} Example: Dyn → Int <: Int → Dyn Int → Nat <: Nat → Int
SLIDE 26
Positive subtype—widening
S <:+ Dyn S′ <:− S T <:+ T ′ S → T <:+ S′ → T ′ s implies t {x : B | s} <:+ {x : B | t} Examples: Int → Int <:+ Dyn → Dyn Nat → Nat <:+ Int → Int
SLIDE 27
Negative subtype—narrowing
Dyn <:− T S′ <:+ S T <:− T ′ S → T <:− S′ → T ′ {x : B | s} <:− {x : B | t} Examples: Dyn → Dyn <:− Int → Int Int → Int <:− Nat → Nat
SLIDE 28
Naive subtype
S <:n Dyn S <:n S′ T <:n T ′ S → T <:n S′ → T ′ s implies t {x : B | s} <:n {x : B | t} Example: Int → Int <:n Dyn → Dyn Nat → Nat <:n Int → Int
SLIDE 29
And now ...a theorem!
SLIDE 30
The Blame Theorem
Consider a source program, where p appears only once.
- If S <:+ T then T ⇐ Sps
- −
→ blame p.
- If S <:− T then T ⇐ Sps
- −
→ blame ¯ p.
SLIDE 31
The Blame Lemma
Let t be a well-typed term and p be a blame label, and consider all subterms of t containing p. If
- every cast with label p is a positive subtype,
T ⇐ Sp s has S <:+ T
- every cast with label ¯
p is a negative subtype, T ⇐ S¯
p s has S <:− T
then t
- −
→∗ blame p.
SLIDE 32
SLIDE 33
The First Tangram Theorem
S <: T if and only if S <:+ T and S <:− T The Blame Corollary Consider a source program, where p appears only once.
- If S <: T then T ⇐ Sps
- −
→ blame p, blame ¯ p.
SLIDE 34
The Second Tangram Theorem
S <:n T if and only if S <:+ T and T <:− S The Blame Corollaries Consider a source program, where p appears only once.
- If S <:n T then T ⇐ Sps
- −
→ blame p.
- If T <:n S then T ⇐ Sps
- −
→ blame ¯ p.
SLIDE 35
And there’s more!
SLIDE 36
Merging casts
Three-place cast (R <:n S, R <:n T): T
R
⇐ Sp s = T ⇐ Rp R ⇐ Sp s Greatest-lower bound: Dyn ∧ S = S = S ∧ Dyn (S → T) ∧ (S′ → T ′) = (S ∧ S′) → (T ∧ T ′) {x : B | s} ∧ {x : B | t} = {x : B | s ∧ t} Every cast is a three-way cast: T ⇐ Sp s = T
S∧T
⇐ Sp s Two adjacent three-place casts can be merged: U
R
⇐ Tp T
Q
⇐ Sp s = U
Q∧R
⇐ Sp s
SLIDE 37
Conclusion
SLIDE 38