A Mechanized Proof of Type Safety for the Polymorphic -Calculus - - PowerPoint PPT Presentation

a mechanized proof of type safety for the polymorphic
SMART_READER_LITE
LIVE PREVIEW

A Mechanized Proof of Type Safety for the Polymorphic -Calculus - - PowerPoint PPT Presentation

A Mechanized Proof of Type Safety for the Polymorphic -Calculus with References Michalis A. Papakyriakou Prodromos E. Gerakios Nikolaos S. Papaspyrou National Technical University of Athens School of Electrical and Computer Engineering


slide-1
SLIDE 1

A Mechanized Proof of Type Safety for the Polymorphic λ-Calculus with References

Michalis A. Papakyriakou Prodromos E. Gerakios Nikolaos S. Papaspyrou

National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory {mpapakyr, pgerakios, nickie}@softlab.ntua.gr

6th Panhellenic Logic Symposium Volos, Greece, 5-8 July 2007

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-2
SLIDE 2

Outline

Introduction Type systems and type safety Polymorphic λ-calculus References Mechanized proof The language λ∀, ref Encoding λ∀, ref in Isabelle/HOL A tour of the proof Conclusions and future work

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-3
SLIDE 3

What is this paper about?

◮ The language

Polymorphic λ-calculus with references

◮ The goal

A proof of type safety

◮ The method

Mechanized proof Using Isabelle/HOL

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-4
SLIDE 4

Type systems

◮ A type system defines:

◮ how a programming language classifies values

and expressions into types

◮ how elements of these types can be

manipulated

◮ how these types can interact

◮ A type indicates a set of values that have the

same generic meaning or intended purpose

◮ The purpose of type systems: to prevent certain

forms of erroneous or undesirable program behaviour

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-5
SLIDE 5

Type safety

◮ If a program is free of static type errors, then its

execution is free of dynamic type errors

◮ Kinds of dynamic errors that can be avoided:

◮ programs can only access appropriate memory

locations (memory safety)

◮ programs can only transfer control to

appropriate program points (control safety)

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-6
SLIDE 6

Type safety

◮ If a program is free of static type errors, then its

execution is free of dynamic type errors

◮ Kinds of dynamic errors that can be avoided:

◮ programs can only access appropriate memory

locations (memory safety)

◮ programs can only transfer control to

appropriate program points (control safety)

◮ The standard procedure

◮ Syntax ◮ Operational semantics ◮ Typing rules ◮ Safety = preservation + progress

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-7
SLIDE 7

Polymorphic λ-calculus

◮ System F, F2 ◮ Girard, 1971; Reynolds, 1974 ◮ First-class polymorphism

◮ Useful for code reuse and modular type

checking

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-8
SLIDE 8

Polymorphic λ-calculus

◮ System F, F2 ◮ Girard, 1971; Reynolds, 1974 ◮ First-class polymorphism

◮ Useful for code reuse and modular type

checking

◮ Polymorphic types and functions

id : ∀α.α → α = Λα. λ x:α. x

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-9
SLIDE 9

Polymorphic λ-calculus

◮ System F, F2 ◮ Girard, 1971; Reynolds, 1974 ◮ First-class polymorphism

◮ Useful for code reuse and modular type

checking

◮ Polymorphic types and functions

id : ∀α.α → α = Λα. λ x:α. x

◮ Explicit type application

append : ∀α. listα → listα → listα . . . append [int] [1, 2, 3] [4, 5] . . .

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-10
SLIDE 10

ML-style references

Imperative programming in functional style

◮ Reference allocation

let r = new 7 . . . r : Ref int

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-11
SLIDE 11

ML-style references

Imperative programming in functional style

◮ Reference allocation

let r = new 7 . . . r : Ref int

◮ Assignment

. . . in r := 42; . . . destructive update!

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-12
SLIDE 12

ML-style references

Imperative programming in functional style

◮ Reference allocation

let r = new 7 . . . r : Ref int

◮ Assignment

. . . in r := 42; . . . destructive update!

◮ Dereference

. . . print (deref r); prints 42

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-13
SLIDE 13

ML-style references

Imperative programming in functional style

◮ Reference allocation

let r = new 7 . . . r : Ref int

◮ Assignment

. . . in r := 42; . . . destructive update!

◮ Dereference

. . . print (deref r); prints 42

◮ No reference deallocation!

. . . free r use garbage collection!

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-14
SLIDE 14

Polymorphic references

◮ The problem

let r = Λα. new (λ x:α. x) in r : ∀α. Ref (α → α) r [int] := succ; deref (r [bool]) true dynamic type error

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-15
SLIDE 15

Polymorphic references

◮ The problem

let r = Λα. new (λ x:α. x) in r : ∀α. Ref (α → α) r [int] := succ; deref (r [bool]) true dynamic type error

◮ A solution: value restriction

In Λα. v, the term v must be a value

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-16
SLIDE 16

Mechanized proof (i)

◮ Why not with pencil and paper?

◮ easy to make a mistake ◮ easy to “fix” a mistake ◮ if one is willing to spend time and effort to write a

thorough proof with pencil and paper, why not use a proof assistant?

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-17
SLIDE 17

Mechanized proof (i)

◮ Why not with pencil and paper?

◮ easy to make a mistake ◮ easy to “fix” a mistake ◮ if one is willing to spend time and effort to write a

thorough proof with pencil and paper, why not use a proof assistant?

◮ Proof assistants

◮ tools to develop formal proofs by man-machine

collaboration

◮ interactive proof editor, with which a human can

guide the search for proofs

◮ some steps of the proofs can be provided by the

computer

◮ not (necessarily) automatic theorem proving!

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-18
SLIDE 18

Mechanized proof (ii)

◮ Some available proof assistants:

Isabelle/HOL, Coq, Twelf, NuPRL, PVS, PhoX, MINLOG, . . .

◮ Isabelle/HOL

◮ Larry Paulson, Cambridge University ◮ Tobias Nipkow, TU München ◮ ❤tt♣✿✴✴✐s❛❜❡❧❧❡✳✐♥✳t✉♠✳❞❡✴

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-19
SLIDE 19

Syntax of λ∀, ref

τ ::= Unit | α | τ → τ | ∀α. τ | Ref τ e ::= unit | x | λ x:τ. e | Λα. e | e1 e2 | e [τ]

|

new e | deref e | e1 := e2 | loc l v ::= unit | λ x:τ. e | Λα. v | loc l

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-20
SLIDE 20

Typing rules of λ∀, ref

Γ; ∆; M ⊢ unit : Unit Γ, x : τ; ∆; M ⊢ x : τ ∆ | = τ Γ, x : τ; ∆; M ⊢ e : τ′ Γ; ∆; M ⊢ λ x:τ. e : τ → τ′ Γ; ∆,α; M ⊢ v : τ Γ; ∆; M ⊢ Λα. v : ∀α. τ Γ; ∆; M ⊢ e1 : τ → τ′ Γ; ∆; M ⊢ e2 : τ Γ; ∆; M ⊢ e1 e2 : τ′ Γ; ∆; M ⊢ e : ∀α. τ ∆ | = τ′ Γ; ∆; M ⊢ e [τ′] : τ{α → τ′} Γ; ∆; M ⊢ e : τ Γ; ∆; M ⊢ new e : Ref τ Γ; ∆; M ⊢ e : Ref τ Γ; ∆; M ⊢ deref e : τ Γ; ∆; M ⊢ e1 : Ref τ Γ; ∆; M ⊢ e2 : τ Γ; ∆; M ⊢ e1 := e2 : Unit Γ; ∆; M, l : τ ⊢ loc l : Ref τ

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-21
SLIDE 21

Operational semantics of λ∀, ref

S; e1 − → S′; e′

1

S; e1 e2 − → S′; e′

1 e2

S; e2 − → S′; e′

2

S; v1 e2 − → S′; v1 e′

2

S; e − → S′; e′ S; e [τ] − → S′; e′ [τ] S; e − → S′; e′ S; new e − → S′; new e′ S; e − → S′; e′ S; deref e − → S′; deref e′ S; e1 − → S′; e′

1

S; e1 := e2 − → S′; e′

1 := e2

S; e2 − → S′; e′

2

S; v1 := e2 − → S′; v1 := e′

2

S; (λ x:τ. e) v − → S; e{x → v} S; (Λα. v) [τ] − → S; v{α → τ} S; new v − → S, l → v; loc l S, l → v; deref (loc l) − → S, l → v; v S, l → v′; loc l := v − → S, l → v; v

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-22
SLIDE 22

Encoding λ∀, ref in Isabelle/HOL

◮ Main problems

◮ The representation of bound variables ◮ The representation of type environments

◮ The details are usually ignored in pencil and

paper proofs

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-23
SLIDE 23

Encoding λ∀, ref in Isabelle/HOL

◮ Main problems

◮ The representation of bound variables ◮ The representation of type environments

◮ The details are usually ignored in pencil and

paper proofs

◮ We represent type environments as finite sets

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-24
SLIDE 24

Bound variables (i)

Proposed solutions

◮ Named variables ◮ DeBruijin indices ◮ Locally nameless ◮ Nominal ◮ Higher-order abstract syntax

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-25
SLIDE 25

Bound variables (ii)

Named variables

◮ Typical in the pencil and paper study of

λ-calculus

◮ ... assuming fresh variable names! ◮ Main problem: α-equivalence ◮ Capture avoiding substitution may have to

rename variables

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-26
SLIDE 26

Bound variables (iii)

DeBruijn indices

◮ Variables are index numbers, counting enclosing

λ-abstractions

◮ Idea:

λ f :τ → τ. λ x:τ. f ( f x) becomes: λ[τ → τ]. λ[τ]. 1 (1 0)

◮ Main problems:

◮ global variables ◮ substitution requires shifting indices

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-27
SLIDE 27

Bound variables (iv)

Locally nameless is a combination of named variables and DeBruijn indices

◮ “Global” variables are named ◮ Bound variables are indices ◮ Substitution only

◮ of bound variables (indices), especially index 0 ◮ with closed terms (without indices)

◮ Advantages

◮ no need for renaming ◮ no need for shifting

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-28
SLIDE 28

A tour of the proof

We proved the type safety of λ∀, ref incrementally

◮ simply typed λ-calculus

414 lines in Isabelle/HOL

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-29
SLIDE 29

A tour of the proof

We proved the type safety of λ∀, ref incrementally

◮ simply typed λ-calculus

414 lines in Isabelle/HOL

◮ simply typed λ-calculus with references

941 lines in Isabelle/HOL

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-30
SLIDE 30

A tour of the proof

We proved the type safety of λ∀, ref incrementally

◮ simply typed λ-calculus

414 lines in Isabelle/HOL

◮ simply typed λ-calculus with references

941 lines in Isabelle/HOL

◮ λ∀, ref

2,815 lines in Isabelle/HOL

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-31
SLIDE 31

Well formed environments

Environments are finite set of pairs, containing unique bindings for variables

◮ Type environment

∆ | = ❚❨ ≡ ❢✐♥✐t❡ ∆

◮ Term environment

Γ❀∆ | = ❖❑ ≡ ❢✐♥✐t❡ Γ ∧ ∆ | = ❚❨ ∧ ✭∀ ① τ✶ τ✷✳ ✭①⊲τ✶✮∈Γ ∧ ✭①⊲τ✷✮∈Γ − → τ✶❂τ✷ ∧ ∆ | = τ✶✮

◮ Store

❙ | = ❙t♦r❡ ≡ ❢✐♥✐t❡ ❙ ∧ ✭∀ ① ✈✶ ✈✷✳ ✭①→✈✶✮∈❙ ∧ ✭①→✈✷✮∈❙ − → ✈✶❂✈✷ ∧ ✈❛❧✉❡ ✈✶✮

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-32
SLIDE 32

Store typing

The store in which a term is evaluated must correspond to the typing environments

| = ❙ ✿ Γ❀∆ ≡ ❙ | = ❙t♦r❡ ∧ ✭∀ ① τ✳ ✭①⊲τ✮∈Γ − → ✭∃ ✈✳ ✭①→✈✮∈❙ ∧ Γ❀∆ ⊢ ✈ ✿ τ✮✮

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-33
SLIDE 33

Standard lemmata/theorems (i)

◮ weakening: the environment can be extended

with fresh bindings that are not used

s✉❜st✐t✉t✐♦♥✿ ❀ ❡✬ ✿ ✬ ✱✭①✿ ✬✮❀ ✈s✉❜st❴t♠ ✭❚♠❋r❡❡❱❛r ①✮ ✐ ❡ ✿ ① ❢r❡❡ ✐♥ ❡ ❀ ✈s✉❜st❴t♠ ❡✬ ✐ ❡ ✿

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-34
SLIDE 34

Standard lemmata/theorems (i)

◮ weakening: the environment can be extended

with fresh bindings that are not used

◮ substitution: typing is preserved when a term of

the same type is substituted for a free variable

lemma s✉❜st✐t✉t✐♦♥✿ assumes Γ❀∆ ⊢ ❡✬ ✿ τ✬ and Γ✱✭①✿τ✬✮❀∆ ⊢ ✈s✉❜st❴t♠ ✭❚♠❋r❡❡❱❛r ①✮ ✐ ❡ ✿ τ and ¬ ① ❢r❡❡ ✐♥ ❡ shows Γ❀∆ ⊢ ✈s✉❜st❴t♠ ❡✬ ✐ ❡ ✿ τ

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-35
SLIDE 35

Standard lemmata/theorems (ii)

◮ preservation: operational semantics preserves

typing

theorem ♣r❡s❡r✈❛t✐♦♥✿ assumes Γ❀∆ ⊢ ❡ ✿ τ and ❙❀❡ ֒ → ❙✬❀❡✬ and | = ❙ ✿ Γ❀∆ shows ∃ Γ✬ ∆✬✳ Γ ⊆ Γ✬ ∧ ∆ ⊆ ∆✬ ∧ | = ❙✬ ✿ Γ✬❀∆✬ ∧ Γ✬❀∆✬ ⊢ ❡✬ ✿ τ

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-36
SLIDE 36

Standard lemmata/theorems (iii)

◮ progress: a well-typed term is either a computed

value or the operational semantics can make

  • ne more evaluation step

theorem ♣r♦❣r❡ss✿ assumes Γ❀∆ ⊢ ❡ ✿ τ and | = ❙ ✿ Γ❀∆ shows ♥♦t❴st✉❝❦ ❡ ❙

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-37
SLIDE 37

Lines of code in Isabelle/HOL

file lines Environ.thy 46 Syntax.thy 729 Typing.thy 757 Semantics.thy 138 Metatheory.thy 1,145 total 2,815 theorem/lemma lines preservation 274 progress 149 weakening of term environment 23 weakening of type environment 45 substitution of terms 192 substitution of types 360

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-38
SLIDE 38

Conclusions

◮ Mechanized proof of type-safety for λ∀, ref using

Isabelle/HOL

◮ The first fully mechanized type safety proof for a

language with mutable references and impredicative polymorphism

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref

slide-39
SLIDE 39

Future work

◮ Extend the language with explicit reference

deallocation

◮ Employ a substructural (linear) type system ◮ This language should have a way of converting

linear to unrestricted references

◮ Extend the linear language with a way to

convert a linear reference to an unrestricted one

  • M. A. Papakyriakou, P

. E. Gerakios, N. S. Papaspyrou Mechanized Proof of Type Safety for λ∀, ref