Well-typed programs cant be blamed (ESOP 2009) Robert Bruce Findler - - PowerPoint PPT Presentation

well typed programs can t be blamed
SMART_READER_LITE
LIVE PREVIEW

Well-typed programs cant be blamed (ESOP 2009) Robert Bruce Findler - - PowerPoint PPT Presentation

Well-typed programs cant be blamed (ESOP 2009) Robert Bruce Findler Northwestern University Philip Wadler University of Edinburgh Aussois, 1418 October 2013 Part I Evolving a program An untyped program let x = 2 f = y. y + 1 h =


slide-1
SLIDE 1

Well-typed programs can’t be blamed

(ESOP 2009) Robert Bruce Findler Northwestern University Philip Wadler University of Edinburgh Aussois, 14–18 October 2013

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4

Part I

Evolving a program

slide-5
SLIDE 5

An untyped program

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

slide-6
SLIDE 6

A typed program

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

slide-7
SLIDE 7

A partly typed program—narrowing

let x = 2 f = ⌈λy. y + 1⌉ : ⋆

p

⇒ Int → Int h = λg : Int → Int. g (g x) in h f − → 4 : Int

slide-8
SLIDE 8

A partly typed program—narrowing

let x = 2 f = ⌈λy. false⌉ : ⋆

p

⇒ Int → Int h = λg : Int → Int. g (g x) in h f − → blame p Positive (covariant): blame the term contained in the cast Narrowing can give rise to positive blame, but never negative blame

slide-9
SLIDE 9

Another partly typed program—widening

let x = ⌈2⌉ f = (λy : Int. y + 1) : Int → Int

p

⇒ ⋆ h = ⌈λg. g (g x)⌉ in ⌈h f⌉ − → ⌈4⌉

slide-10
SLIDE 10

Another partly typed program—widening

let x = ⌈true⌉ f = (λy : Int. y + 1) : Int → Int

p

⇒ ⋆ h = ⌈λg. g (g x)⌉ in ⌈h f⌉ − → blame ¯ p Negative (contravariant): blame the context containg the cast Widening can give rise to negative blame, but never positive blame

slide-11
SLIDE 11

Part II

Blame calculus

slide-12
SLIDE 12

Untyped = Uni-typed

⌈x⌉ = x ⌈c⌉ = c : A

p

⇒ ⋆ if ty(c) = A ⌈op( M)⌉ =

  • p(⌈

M⌉ : ⋆

  • p

⇒ A) : B

p

⇒ ⋆ if ty(op) = A → B ⌈λx. N⌉ = (λx : ⋆. ⌈N⌉) : ⋆ → ⋆ ⇒ ⋆ ⌈L M⌉ = (⌈L⌉ : ⋆

p

⇒ ⋆ → ⋆) ⌈M⌉ (slogan due to Bob Harper)

slide-13
SLIDE 13

Blame calculus: Compatibility

A ≺ A A ≺ ⋆ ⋆ ≺ B A′ ≺ A B ≺ B′ A → B ≺ A′ → B′

slide-14
SLIDE 14

Types

ty(c) = ι Γ ⊢ c : ι Γ ⊢ t : A ty(op) = A→B Γ ⊢ op( t ) : B x : A ∈ Γ Γ ⊢ x : A Γ, x : A ⊢ t : B Γ ⊢ λx:A. t : A→B Γ ⊢ t : A→B Γ ⊢ s : A Γ ⊢ t s : B Γ ⊢ s : A A ≺ B Γ ⊢ (s : A

p

⇒ B) : B Γ ⊢ s : G Γ ⊢ (s : G ⇒ ⋆) : ⋆

slide-15
SLIDE 15

Reductions

(λx:A. t) v − → t[x := v]

  • p(

v ) − → δ(op, v ) v : A→B

p

⇒ A′→B′ − → λx′:A′. (v (x′ : A′

p

⇒ A) : B

p

⇒ B′) v : ι

p

⇒ ι − → v v : A

p

⇒ ⋆ − → (v : A

p

⇒ G) : G ⇒ ⋆ if ⋆ = A ≺ G v : (G ⇒ ⋆) : ⋆

p

⇒ A − → v : G

p

⇒ A if G ≺ A v : (G ⇒ ⋆) : G

p

⇒ A − → blame p if G ≺ A s − → t E[s] − → E[t] s − → blame p E[s] − → blame p

slide-16
SLIDE 16

Part III

Subtyping <: <:+ <:− <:n

slide-17
SLIDE 17

Subtype

⋆ <: ⋆ ι <: ι A <: G A <: ⋆ A′ <: A B <: B′ A → B <: A′ → B′ Example: Int <: Int Int <: ⋆ Int <: Int Int <: ⋆ ⋆ → Int <: Int → ⋆

slide-18
SLIDE 18

Positive subtype—widening

A <:+ ⋆ ι <: ι A′ <:− A B <:+ B′ A → B <:+ A′ → B′ Example: ⋆ <:− Int Int <:+ ⋆ Int → Int <:+ ⋆ → ⋆

slide-19
SLIDE 19

Negative subtype—narrowing

⋆ <:− A ι <: ι A <:− G A <:− ⋆ A′ <:+ A B <:− B′ A → B <:− A′ → B′ Example: Int <:+ ⋆ ⋆ <:− Int ⋆ → ⋆ <:− Int → Int

slide-20
SLIDE 20

Naive subtype

A <:n ⋆ ι <:n ι A <:n A′ B <:n B′ A → B <:n A′ → B′ Example: Int <:n ⋆ Int <:n ⋆ Int → Int <: ⋆ → ⋆

slide-21
SLIDE 21

Part IV

The Blame Theorem

slide-22
SLIDE 22

Safety

x sf p t sf p λx. t sf p s sf p t sf p s t sf p s sf p A <:+ B s : A

p

⇒ B sf p s sf p A <:− B s : A

p

⇒ B sf p s sf p p = q ¯ p = q s : A

q

⇒ B sf p

slide-23
SLIDE 23

The Blame Theorem

Preservation If s sf p and s − → t then t sf p. Progress If s sf p then s

→ blame p.

slide-24
SLIDE 24
slide-25
SLIDE 25

The First Tangram Theorem

A <: B if and only if A <:+ B and A <:− B

The First Blame Corollary

Let t be a term where s : A

p

⇒ B is the only subterm with label p. If A <: B then t

→ blame p and t

→ blame ¯ p.

slide-26
SLIDE 26

The Second Tangram Theorem

A <:n B if and only if A <:+ B and B <:− A

The Second Blame Corollary

Let t be a term where s : A

p

⇒ B is the only subterm with label p. If A <:n B then t

→ blame p. Let t be a term where s : A

p

⇒ B is the only subterm with label p. If B <:n A then t

→ blame p.

slide-27
SLIDE 27

A new slogan for type safety

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

slide-28
SLIDE 28

References

Well-typed programs can’t be blamed Robert Bruce Findler and Philip Wadler ESOP 2009 Threesomes Jeremy Siek and Philip Wadler POPL 2010 Blame for all Amal Ahmed, Robert Bruce Findler, Jeremy Siek, Philip Wadler POPL 2011

slide-29
SLIDE 29

Part V

TypeScript: The Next Generation

slide-30
SLIDE 30
slide-31
SLIDE 31

F⋆, JavaScript, and TypeScript

A fully abstract compiler from F⋆ to JavaScript Cedric Fournet, Nikhil Swamy, Juan Chen, Pierre-Evariste Dagand, Pierre-Yves Strub, and Benjamin Livshits POPL 2013 Gradual Typing Embedded Securely in JavaScript

  • K. Bhargavan, G. Bierman, J. Chen, C. Fournet, A. Rastogi,
  • P. Strub, N. Swamy

POPL 2014

slide-32
SLIDE 32

TypeScript: The Next Generation

Joint proposal to MSR with Gavin Bierman. TypeScript interface declares types for third-party module. DefinitelyTyped repository declares types for 150 libraries. But the declaration is taken of faith. TypeScript TNG uses blame calculus to generate wrappers from interface declarations. But there are problems!

  • Wrappers on functions violate object identity.
  • How to interpose type checks for update?

Proxies may do the job. Hypothesis: TypeScript TNG will aid debugging and increase reliablility of TypeScript and JavaScript code.

slide-33
SLIDE 33

A wide-spectrum type system

Extend blame calculus to support a wide range of type systems:

  • dynamic types (as in JavaScript or Racket)
  • polymorphic types (as in F# or Haskell)
  • dependent types (as in F⋆ or Coq)

Hypothesis: a wide-spectrum type system will increase the utility of dependent types, by allowing dynamic checks to be used as a fallback when static validation is problematic.

slide-34
SLIDE 34

Part VI

Other

slide-35
SLIDE 35

Other

Propositions as Sessions Philip Wadler ICFP 2012 A Practical Theory of Language-Integrate Query James Cheney, Sam Lindley, and Philip Wadler ICFP 2013