Well-typed programs cant be blamed Philip Wadler University of - - PowerPoint PPT Presentation

well typed programs can t be blamed
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Well-typed programs can’t be blamed

Philip Wadler University of Edinburgh Robert Bruce Findler University of Chicago

slide-2
SLIDE 2

“The mathematics of programming languages is deep and elegant” examples other than Curry-Howard? terms other than ‘deep’, ‘elegant’?

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
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
SLIDE 7

A repeated theme

Visual Basic 9.0 Perl 6.0 ECMAScript 4.0

slide-8
SLIDE 8

Evolving a program

slide-9
SLIDE 9

An untyped program

⌈let x = 2 f = λy. y + 1 h = λg. g (g x) in h f⌉ − → ⌈4⌉

slide-10
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
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
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
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
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
SLIDE 15

The Blame Game

slide-16
SLIDE 16

Blame

Int ⇐ Dynp ⌈2⌉ − → 2 Int ⇐ Dynp ⌈’a’⌉ − → blame p

slide-17
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
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
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
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
SLIDE 21

Untyped and supertyped

slide-22
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
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
SLIDE 24

Subtyping

slide-25
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
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
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
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
SLIDE 29

And now ...a theorem!

slide-30
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
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 32
slide-33
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
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
SLIDE 35

And there’s more!

slide-36
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
SLIDE 37

Conclusion

slide-38
SLIDE 38

A new slogan for type safety

Milner (1978): Well-typed programs can’t go wrong. Harper; Felleisen and Wright (1994): Well-typed programs don’t get stuck. Wadler and Findler (2008): Well-typed programs can’t be blamed.