54
Toward a machine-certified correctness proof of Wand’s type reconstruction algorithm Presented by Sunil Kothari Joint work with Prof. James Caldwell Department of Computer Science, University of Wyoming, USA

Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Toward a machine-certified correctness proof ofWand’s type reconstruction algorithm

Presented by Sunil KothariJoint work with Prof. James Caldwell

Department of Computer Science,University of Wyoming, USA

Page 2: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Outline

1 OverviewType Reconstruction Algorithms

2 IntroductionWand’s AlgorithmSubstitution

3 Correctness ProofIssues In FormalizationSoundness and Completeness Proofs

4 Conclusions and Future Work

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 2 / 31

Page 3: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Outline

1 OverviewType Reconstruction Algorithms

2 IntroductionWand’s AlgorithmSubstitution

3 Correctness ProofIssues In FormalizationSoundness and Completeness Proofs

4 Conclusions and Future Work

Page 4: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Highlights

Essential feature of many functional programming languages (ML,Haskell, OCaml, etc.).

Automated type reconstruction is possible.Substitution-based algorithms.

Intermittent constraint generation and constraint solving.Constraint-based algorithms.

Two distinct phases: constraint generation and constraint solving.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 3 / 31

Page 5: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Highlights

Essential feature of many functional programming languages (ML,Haskell, OCaml, etc.).Automated type reconstruction is possible.

Substitution-based algorithms.Intermittent constraint generation and constraint solving.

Constraint-based algorithms.Two distinct phases: constraint generation and constraint solving.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 3 / 31

Page 6: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Highlights

Essential feature of many functional programming languages (ML,Haskell, OCaml, etc.).Automated type reconstruction is possible.

Substitution-based algorithms.Intermittent constraint generation and constraint solving.

Constraint-based algorithms.Two distinct phases: constraint generation and constraint solving.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 3 / 31

Page 7: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Highlights

Essential feature of many functional programming languages (ML,Haskell, OCaml, etc.).Automated type reconstruction is possible.

Substitution-based algorithms.Intermittent constraint generation and constraint solving.

Constraint-based algorithms.Two distinct phases: constraint generation and constraint solving.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 3 / 31

Page 8: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Substitution-based Algorithms

ExamplesAlgorithm W, J by Milner, 1978.Algorithm M by Leroy, 1993.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 4 / 31

Page 9: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Substitution-based Algorithms

Machine-Certified Correctness ProofAlgorithm W in Coq, Isabelle/HOL [DM99, NN99, NN96].

Nominal verification of Algorithm W (in Isabelle/HOL) [UN09].The formalization in Coq is not available online.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 5 / 31

Page 10: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Substitution-based Algorithms

Machine-Certified Correctness ProofAlgorithm W in Coq, Isabelle/HOL [DM99, NN99, NN96].Nominal verification of Algorithm W (in Isabelle/HOL) [UN09].

The formalization in Coq is not available online.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 5 / 31

Page 11: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Substitution-based Algorithms

Machine-Certified Correctness ProofAlgorithm W in Coq, Isabelle/HOL [DM99, NN99, NN96].Nominal verification of Algorithm W (in Isabelle/HOL) [UN09].The formalization in Coq is not available online.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 5 / 31

Page 12: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Constraint-based Frameworks/Algorithms

ExamplesWand’s algorithm [Wan87].HM(X) [SOW97] by Sulzmann et al. 1999, Pottier and Rémy 2005[PR05], Qualified types [Jon95].Top quality error messages [Hee05].

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 6 / 31

Page 13: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Constraint-based Algorithms/Frameworks

Machine-Certified Correctness ProofWe know of no correctness proof of Wand’s type reconstructionalgorithm not verified in any theorem prover.

We want to verify our extension of Wand’s algorithm forpolymorphic let.POPLMark challenge also aims at mechanizing meta-theory.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 7 / 31

Page 14: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Constraint-based Algorithms/Frameworks

Machine-Certified Correctness ProofWe know of no correctness proof of Wand’s type reconstructionalgorithm not verified in any theorem prover.We want to verify our extension of Wand’s algorithm forpolymorphic let.

POPLMark challenge also aims at mechanizing meta-theory.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 7 / 31

Page 15: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Overview Type Reconstruction Algorithms

Constraint-based Algorithms/Frameworks

Machine-Certified Correctness ProofWe know of no correctness proof of Wand’s type reconstructionalgorithm not verified in any theorem prover.We want to verify our extension of Wand’s algorithm forpolymorphic let.POPLMark challenge also aims at mechanizing meta-theory.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 7 / 31

Page 16: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Outline

1 OverviewType Reconstruction Algorithms

2 IntroductionWand’s AlgorithmSubstitution

3 Correctness ProofIssues In FormalizationSoundness and Completeness Proofs

4 Conclusions and Future Work

Page 17: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Terms and Constraint Syntax

Termsτ ::= TyVar(x) | τ ′ → τ ′′

Atomic types (of the form TyVar x) are denoted by α, β, α′ etc.

Constraints

Constraint are of the form τc=τ ′.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 8 / 31

Page 18: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Terms and Constraint Syntax

Termsτ ::= TyVar(x) | τ ′ → τ ′′

Atomic types (of the form TyVar x) are denoted by α, β, α′ etc.

Constraints

Constraint are of the form τc=τ ′.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 8 / 31

Page 19: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

SubstitutionA substitution (denoted by σ) maps type variables to types.

Unifier

We write σ |= (τ1c= τ2), if σ(τ1) = σ(τ2).

Most General UnifierA unifier σ is the most general unifier(MGU) if for any other unifierσ′′ there is a substitution σ′ such that σ ◦ σ′ ≈ σ′′.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 9 / 31

Page 20: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

SubstitutionA substitution (denoted by σ) maps type variables to types.

Unifier

We write σ |= (τ1c= τ2), if σ(τ1) = σ(τ2).

Most General UnifierA unifier σ is the most general unifier(MGU) if for any other unifierσ′′ there is a substitution σ′ such that σ ◦ σ′ ≈ σ′′.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 9 / 31

Page 21: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

SubstitutionA substitution (denoted by σ) maps type variables to types.

Unifier

We write σ |= (τ1c= τ2), if σ(τ1) = σ(τ2).

Most General UnifierA unifier σ is the most general unifier(MGU) if for any other unifierσ′′ there is a substitution σ′ such that σ ◦ σ′ ≈ σ′′.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 9 / 31

Page 22: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

SubstitutionA substitution (denoted by σ) maps type variables to types.

Unifier

We write σ |= (τ1c= τ2), if σ(τ1) = σ(τ2).

Most General UnifierA unifier σ is the most general unifier(MGU) if for any other unifierσ′′ there is a substitution σ′ such that σ ◦ σ′ ≈ σ′′.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 9 / 31

Page 23: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Wand’s Algorithm

Let G denote a set of goals. And E a set of equations.Input. A term M of Λ.

Initialization. Set E = ∅ and G = {(Γ,M, α0)}.

Loop Step. If G = ∅ then return E else choose a subgoal (Γ,M, τ)from G and add to E and G new verification conditions andsubgoals by looking at the action table.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 10 / 31

Page 24: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Wand’s Algorithm

Action Table

Case (Γ, x , τ ). Generate the equation τ c= Γ(x).

Case (Γ,MN, τ ). Generate subgoals (Γ,M, τ ′ → τ ) and (Γ,N, τ ′).

Case (Γ, λx .M, τ ). Generate equation τ c= τ ′ → τ ′′ and subgoal

([x : τ ′] :: Γ,M, τ ′′).

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 11 / 31

Page 25: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Wand’s Algorithm

Action Table

Case (Γ, x , τ ). Generate the equation τ c= Γ(x).

Case (Γ,MN, τ ). Generate subgoals (Γ,M, τ ′ → τ ) and (Γ,N, τ ′).

Case (Γ, λx .M, τ ). Generate equation τ c= τ ′ → τ ′′ and subgoal

([x : τ ′] :: Γ,M, τ ′′).

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 11 / 31

Page 26: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Wand’s Algorithm

Action Table

Case (Γ, x , τ ). Generate the equation τ c= Γ(x).

Case (Γ,MN, τ ). Generate subgoals (Γ,M, τ ′ → τ ) and (Γ,N, τ ′).

Case (Γ, λx .M, τ ). Generate equation τ c= τ ′ → τ ′′ and subgoal

([x : τ ′] :: Γ,M, τ ′′).

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 11 / 31

Page 27: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Wand’s Algorithm - Example

{(∅, λx .λy .λz.xz(yz), α0)}; {}{((x : α1), λy .λz.xz(yz), α2)}; {α0

c= α1 → α2}

{((x : α1, y : α3), λz.xz(yz), α4)}; {α2c= α3 → α4}

{((x : α1, y : α3, z : α5), xz(yz), α6)}; {α4c= α5 → α6}

{(((x : α1, z : α5), xz, α7 → α6), ((y : α3, z : α5), yz, α7))}; {}{((x : α1), x , α8 → (α7 → α6)), ((z : α5), z, α8), ((y : α3, z : α5), yz, α7)}; {}{(((z : α5), z, α8), ((y : α3, z : α5), yz, α7))}; {α1

c= α8 → α7 → α6}

{((y : α3, z : α5), yz, α7)}; {α8c= α5}

{((y : α3), y , α9 → α7), ((z : α5), z, α9)}; {}{((z : α5), z, α9)}; {α9 → α7

c= α3}

∅; {α9c= α5}

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 12 / 31

Page 28: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Wand’s Algorithm Example - Alternate View

{x : α1}{α8→α7→α6

c= α1}

` x : α8 → α7 → α6 {z : α5}{α8

c= α5}`z : α8

{x : α1, z : α5}{}` xz : α7 → α6

{y : α3}{α9→α7

c= α3}

` y : α9 → α7 {z : α5}{α9

c= α5}`z : α9

{y : α3, z : α5}{}` yz : α7

{x : α1, y : α3, z : α5}{}` xz(yz) : α6

{x : α1, y : α3}{α4

c= α5→α6}` λz.xz(yz) : α4

{x : α1}{α2

c= α3→α4}` λy.λz.xz(yz) : α2

{}{α0

c= α1→α2}` λx.λy.λz.xz(yz) : α0

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 13 / 31

Page 29: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Wand’s Algorithm

Example - Solution

α0c= α1 → α2

α2c= α3 → α4

α4c= α5 → α9

α1c= α8 → α7 → α9

α8c= α5

α9 → α7c= α3

α9c= α5

After unifying the above constraints,α0 7→ (α5 → α7 → α6)→ (α5 → α7)→ (α5 → α6)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 14 / 31

Page 30: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Substitution

Finite maps in Coq

Representing substitutionsSubstitution represented as a list of pairs, set of pairs, and normalfunction.We represent a substitution as a finite function.

Substitution as a finite mapUsed the Coq’s finite maps library Coq.FSets.FMapInterface (ver.8.1pl3).Axiomatic presentation.Provides no induction principle.Forward reasoning is often required.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 15 / 31

Page 31: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Substitution

Finite maps in Coq

Representing substitutionsSubstitution represented as a list of pairs, set of pairs, and normalfunction.We represent a substitution as a finite function.

Substitution as a finite mapUsed the Coq’s finite maps library Coq.FSets.FMapInterface (ver.8.1pl3).Axiomatic presentation.Provides no induction principle.Forward reasoning is often required.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 15 / 31

Page 32: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Substitution

Substitution

Related ConceptsSubstitution application to a type τ is defined as:

σ (TyVar(x))def= if 〈x , τ〉 ∈ σ then τ else TyVar(x)

σ (τ1 → τ2)def= σ(τ1)→ σ(τ2)

Application of a substitution to a constraint is defined similarly:

σ(τ1c= τ2)

def= σ(τ1)

c= σ(τ2)

Assumption: Idempotent substitution.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 16 / 31

Page 33: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Substitution

Substitution

Related ConceptsSubstitution application to a type τ is defined as:

σ (TyVar(x))def= if 〈x , τ〉 ∈ σ then τ else TyVar(x)

σ (τ1 → τ2)def= σ(τ1)→ σ(τ2)

Application of a substitution to a constraint is defined similarly:

σ(τ1c= τ2)

def= σ(τ1)

c= σ(τ2)

Assumption: Idempotent substitution.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 16 / 31

Page 34: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Introduction Substitution

Substitution

Substitution CompositionSubstitution composition definition using Coq’s finite maps isdelicate.But the following theorem holds

Theorem 1 (Composition apply)

∀σ, σ′.∀τ.(σ ◦ σ′)τ = σ′(σ(τ))

Substitution representation determines the reasoning.A list of pairs: 600 proof steps [DM99].Finite maps: 100 proof steps.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 17 / 31

Page 35: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Outline

1 OverviewType Reconstruction Algorithms

2 IntroductionWand’s AlgorithmSubstitution

3 Correctness ProofIssues In FormalizationSoundness and Completeness Proofs

4 Conclusions and Future Work

Page 36: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Issues In Formalization

Wand’s Algorithm

Issues in formalizationRaise exceptions, but that’s not possible.

We choose an option type.

Freshness is now explicit.The W-App rule now generates a constraint.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 18 / 31

Page 37: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Issues In Formalization

Wand’s Algorithm

Issues in formalizationRaise exceptions, but that’s not possible.

We choose an option type.

search_type_env(x, Γ) = Some τ

Wand(Γ, x, n0) = (Some {Tvar(n0)c= τ}, n0 + 1)

(W-Var)

Wand(((x : Tvar(n0 + 1)) :: Γ),M, n0 + 2) = (Some C, n1)

Wand(Γ, λx.M, n0) = (Some {Tvar(n0)c= Tvar(n0 + 1)→ Tvar(n0 + 2)} ∪ C, n1)

(W-Abs)

Wand(Γ,M, n0 + 1) = (Some C′, n1) Wand(Γ,N, n1) = (Some C′′, n2)

Wand(Γ,MN, n0) = (Some {Tvar(n0 + 1)c= Tvar(n1)→ Tvar(n0)} ∪ C′ ∪ C′′, n2)

(W-App)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 19 / 31

Page 38: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Issues In Formalization

Wand’s Algorithm

Issues in formalizationFreshness is now explicit.

search_type_env(x, Γ) = Some τ

Wand(Γ, x, n0) = (Some {Tvar(n0)c= τ}, n0 + 1)

(W-Var)

Wand(((x : Tvar(n0 + 1)) :: Γ),M, n0 + 2) = (Some C, n1)

Wand(Γ, λx.M, n0) = (Some {Tvar(n0)c= Tvar(n0 + 1)→ Tvar(n0 + 2)} ∪ C, n1)

(W-Abs)

Wand(Γ,M, n0 + 1) = (Some C′, n1) Wand(Γ,N, n1) = (Some C′′, n2)

Wand(Γ,MN, n0) = (Some {Tvar(n0 + 1)c= Tvar(n1)→ Tvar(n0)} ∪ C′ ∪ C′′, n2)

(W-App)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 20 / 31

Page 39: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Issues In Formalization

Wand’s Algorithm

Issues in formalizationThe W-App rule now generates a constraint.

search_type_env(x, Γ) = Some τ

Wand(Γ, x, n0) = (Some {Tvar(n0)c= τ}, n0 + 1)

(W-Var)

Wand(((x : Tvar(n0 + 1)) :: Γ),M, n0 + 2) = (Some C, n1)

Wand(Γ, λx.M, n0) = (Some {Tvar(n0)c= Tvar(n0 + 1)→ Tvar(n0 + 2)} ∪ C, n1)

(W-Abs)

Wand(Γ,M, n0 + 1) = (Some C′, n1) Wand(Γ,N, n1) = (Some C′′, n2)

Wand(Γ,MN, n0) = (Some {Tvar(n0 + 1)c= Tvar(n1)→ Tvar(n0)} ∪ C′ ∪ C′′, n2)

(W-App)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 21 / 31

Page 40: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Overview

Correctness is given w.r.t the Hindley-Milner type system:〈x , τ〉 ∈ Γ is the leftmost binding of x in `

Γ B x : τ(HM-Var)

(x , τ) :: Γ B M : τ ′

Γ B λx .M : τ → τ ′ (HM-Abs)

Γ B M : τ ′ → τ Γ B N : τ ′

Γ B MN : τ(HM-App)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 22 / 31

Page 41: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Soundness Proof

Informally

If Wand’s algorithm returns a unifiable constraint set, then there is aHindley-Milner proof.

Our Statement∀Γ,∀M,∀σ, ∀n,∀n′,∀C.Wand(Γ,M,n) = (Some C, n′) ∧ unify C = Some σ

⇒ ` σ(Γ)BHM M : σ(τ)

Wand’s Statement

∀σ.σ |= (E ,G)⇒` σ(Γ0)BHM M0 : σ(τ0)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 23 / 31

Page 42: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Completeness Proof

Informally

If there is a Hindley-Milner proof (that a term has some type), then Wand’salgorithm returns a solvable constraint set that will return the given type.

Our Statement∀Γ′, ∀M, ∀τ.` Γ′ BHM M : τ⇒ ∀Γ, ∀n.(∃σ. σ(Γ) = Γ′) ∧ fresh_env n Γ⇒ ∀C, ∀n′.Wand(Γ,M, n) = (Some C, n′) ∧∃σ′.unify C = Some σ′

⇒ ∃σ′′.(σ′ ◦ σ′′)(Tvar(n)) = τ ∧(σ′ ◦ σ′′)(Γ) = Γ′

Wand’s Statement` ΓBHM M0 : τ ⇒ (∃ρ. ρ |= (E ,G) ∧ Γ = ρΓ0 ∧ τ = ρτ0)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 24 / 31

Page 43: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Modeling MGU

The most general unifier (MGU) is often a first-order unificationalgorithm over simple type terms.

In machine checked correctness proofs, the MGU is modeled as aset of four axioms:

(i) mgu σ (τ1c=τ2)⇒ σ(τ1) = σ(τ2)

(ii) mgu σ (τ1c=τ2) ∧ σ′(τ1) = σ′(τ2)⇒ ∃σ′′.σ′ ≈ σ ◦ σ′′

(iii) mgu σ (τ1c=τ2)⇒ FTVS (σ) ⊆ FVC (τ1

c=τ2)

(iv) σ(τ1) = σ(τ2)⇒ ∃σ′. mgu σ′(τ1c=τ2)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 25 / 31

Page 44: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Modeling MGU

The most general unifier (MGU) is often a first-order unificationalgorithm over simple type terms.In machine checked correctness proofs, the MGU is modeled as aset of four axioms:

(i) mgu σ (τ1c=τ2)⇒ σ(τ1) = σ(τ2)

(ii) mgu σ (τ1c=τ2) ∧ σ′(τ1) = σ′(τ2)⇒ ∃σ′′.σ′ ≈ σ ◦ σ′′

(iii) mgu σ (τ1c=τ2)⇒ FTVS (σ) ⊆ FVC (τ1

c=τ2)

(iv) σ(τ1) = σ(τ2)⇒ ∃σ′. mgu σ′(τ1c=τ2)

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 25 / 31

Page 45: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

MGU Axioms

Old Axioms(i) mgu σ (τ1

c=τ2)⇒ σ(τ1) = σ(τ2)

(ii) mgu σ (τ1c=τ2) ∧ σ′(τ1) = σ′(τ2)⇒ ∃δ.σ′ ≈ σ ◦ δ

(iii) mgu σ (τ1c=τ2)⇒ FTVS (σ) ⊆ FVC (τ1

c=τ2)

(iv) σ(τ1) = σ(τ2)⇒ ∃σ′. mgu σ′(τ1c=τ2)

New Generalized Axioms

(i) unify C = Some σ ⇒ σ |= C(ii) (unify C = Some σ ∧ σ′ |= C)⇒ ∃σ′′. σ′ ≈ σ ◦ σ′′

(iii) unify C = Some σ ⇒ FTVS(σ) ⊆ FVC (C)(iv) σ |= C ⇒ ∃σ′. unify C = Some σ′

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 26 / 31

Page 46: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

MGU Axioms

Old Axioms(i) mgu σ (τ1

c=τ2)⇒ σ(τ1) = σ(τ2)

(ii) mgu σ (τ1c=τ2) ∧ σ′(τ1) = σ′(τ2)⇒ ∃δ.σ′ ≈ σ ◦ δ

(iii) mgu σ (τ1c=τ2)⇒ FTVS (σ) ⊆ FVC (τ1

c=τ2)

(iv) σ(τ1) = σ(τ2)⇒ ∃σ′. mgu σ′(τ1c=τ2)

New Generalized Axioms

(i) unify C = Some σ ⇒ σ |= C(ii) (unify C = Some σ ∧ σ′ |= C)⇒ ∃σ′′. σ′ ≈ σ ◦ σ′′

(iii) unify C = Some σ ⇒ FTVS(σ) ⊆ FVC (C)(iv) σ |= C ⇒ ∃σ′. unify C = Some σ′

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 26 / 31

Page 47: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Functional Induction in Coq

Axioms proved in Coq [KC09].Important first step in proof of the axioms.Requires an induction principle generated before.

functional induction (f x1 x2 x3 .. xn) is a shortform for induction x1 x2 x3 ...xn f(x1 ... xn)using id, where id is the induction principle for f .

functional induction (unify c) induction c(unify c) using unif_ind.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 27 / 31

Page 48: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Functional Induction in Coq

Axioms proved in Coq [KC09].Important first step in proof of the axioms.Requires an induction principle generated before.functional induction (f x1 x2 x3 .. xn) is a shortform for induction x1 x2 x3 ...xn f(x1 ... xn)using id, where id is the induction principle for f .

functional induction (unify c) induction c(unify c) using unif_ind.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 27 / 31

Page 49: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Correctness Proof Soundness and Completeness Proofs

Functional Induction in Coq

Axioms proved in Coq [KC09].Important first step in proof of the axioms.Requires an induction principle generated before.functional induction (f x1 x2 x3 .. xn) is a shortform for induction x1 x2 x3 ...xn f(x1 ... xn)using id, where id is the induction principle for f .

functional induction (unify c) induction c(unify c) using unif_ind.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 27 / 31

Page 50: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Outline

1 OverviewType Reconstruction Algorithms

2 IntroductionWand’s AlgorithmSubstitution

3 Correctness ProofIssues In FormalizationSoundness and Completeness Proofs

4 Conclusions and Future Work

Page 51: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Conclusions and Future Work

Conclusions and Future Work

Used Coq’s finite maps library to represent substitution.MGU is not axiomatized in our verification.Completeness is work in progress, but so far 8000 lines of Coqtactics and specification.The final goal is to have a machine certified correctness proof ofour extension of Wand’s algorithm to polymorphic let.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 28 / 31

Page 52: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Conclusions and Future Work

Catherine Dubois and Valerie M. Morain.Certification of a Type Inference Tool for ML: Damas–Milner withinCoq.J. Autom. Reason., 23(3):319–346, 1999.

Bastiaan Heeren.Top Quality Type Error Messages.PhD thesis, Universitiet Utrecht, 2005.

J. Roger Hindley and Jonathan P. Seldin.Introduction to Combinators and λ-Calculus.Cambridge University Press, 1986.

Mark P. Jones.Qualified Types: Theory and Practice.Distinguished Dissertations in Computer Science. CambridgeUniversity Press, 1995.

Sunil Kothari and James Caldwell.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 28 / 31

Page 53: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Conclusions and Future Work

A machine checked model of MGU axioms: applications of finitemaps and functional induction.In Proceedings of the 23rd International Workshop on Unification,pages 17–31, 2009.

Dieter Nazareth and Tobias Nipkow.Theorem Proving in Higher Order Logics, volume 1125, chapterFormal Verification of Alg. W: The Monomorphic Case, pages331–345.Springer Berlin / Heidelberg, 1996.

Wolfgang Naraschewski and Tobias Nipkow.Type inference verified: Algorithm w in isabelle/hol.J. Autom. Reason., 23(3):299–318, 1999.

F. Pottier and D. Rémy.The essence of ML type inference.In Benjamin C. Pierce, editor, Advanced Topics in Types andProgramming Languages, chapter 10, pages 389–489. MIT Press,2005.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 28 / 31

Page 54: Toward a machine-certified correctness proof of Wand's ...sweirich/wmm/wmm09/Kothari-Slides.pdf · Essential feature of many functional programming languages (ML, Haskell, OCaml,

Conclusions and Future Work

Martin Sulzmann, Martin Odersky, and Martin Wehr.Type inference with constrained types.In Fourth International Workshop on Foundations ofObject-Oriented Programming (FOOL 4), 1997.

Christian Urban and Tobias Nipkow.From Semantics to Computer Science, chapter Nominalverification of algorithm W.Cambridge University Press, 2009.

Mitchell Wand.A simple algorithm and proof for type inference.Fundamenta Informaticae, 10:115–122, 1987.

Andrew K. Wright and Matthias Felleisen.A syntactic approach to type soundness.Information and Computation, 115(1):38–94, 1994.

Kothari Caldwell (U. of Wyoming) Machine-certified proof of Wand’s type reconstruction algorithm WMM’09 28 / 31