Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground...

28
Verifying a Semantic βη -Conversion Test for Martin-L¨of Type Theory Andreas Abel 1 Thierry Coquand 2 Peter Dybjer 2 1 Ludwig-Maximilians-University Munich 2 Chalmers University of Technology Mathematics of Program Construction Marseille, France 18 July 2008 Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 1 / 27

Transcript of Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground...

Page 1: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Verifying a Semantic βη-Conversion Testfor Martin-Lof Type Theory

Andreas Abel1

Thierry Coquand2 Peter Dybjer2

1Ludwig-Maximilians-University Munich2Chalmers University of Technology

Mathematics of Program ConstructionMarseille, France

18 July 2008

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 1 / 27

Page 2: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Background

Dependently typed languages allow specification, implementation,and verification in the same language.

Strong data invariants.Pre- and post-conditions.Soundness.

Programs (e.g., add) can occur in types of other programs (e.g.,append).append : (n m : Nat) -> Vec n -> Vec m -> Vec (add n m)

Type equality can be establishedautomatically, e.g., Vec (add 0 m) = Vec m (by computation), orby proof, e.g., Vec (add n m) = Vec (add m n).

Goal: establish more equalities automatically.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 2 / 27

Page 3: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Building η into Definitional Equality

Coq’s definitional equality is β (+ δ + ι).

The stronger definitional equality, the fewer the user has to revertto equality proofs.

Why not η? (f = λx . f x if x new)

Validates, for instance, f = comp f id.

But η complicates the meta theory.

Twelf, Epigram, and Agda check for βη-convertibility.

Twelf’s type-directed conversion check has been verified by Harper& Pfenning (2005).

This work: towards verification of Epigram and Agda’s equalitycheck.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 3 / 27

Page 4: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Language

Core type theory:Dependent function types Fun AλxB (= (x : A) -> B) with η.Predicative universes Set0,Set1, . . . .Natural numbers.

We handle large eliminations (types defined by cases andrecursion), in contrast to Harper & Pfenning (2005).Scales to Σ types with surjective pairing.Goal: handle all types with at most one constructor (Π, Σ, 1, 0,singleton types).Not a goal?: handle enumeration types (2, disjoint sums, . . . ).

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 4 / 27

Page 5: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Syntax of Terms and Types

Lambda-calculus with constants

r , s, t ::= c | x | λx .t | r s

c ::= N type of natural numbersz zeros successorrec primitive recursionFun function space constructorSeti universe of sets of level i

Πx :A.B (Agda: (x : A) -> B) is written Fun A (λx .B).

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 5 / 27

Page 6: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Judgements

Essential judgements

Γ ` t : A t has type A in ΓΓ ` t = t ′ : A t and t ′ are equal expressions of type A in Γ

Typing of functions:

Γ, x :A ` t : B

Γ ` λx .t : Fun A (λx .B)

Γ ` r : Fun A (λx .B) Γ ` s : A

Γ ` r s : B[s/x ]

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 6 / 27

Page 7: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Set formation rules

Small types (sets):

Γ ` N : Set0

Γ ` A : Seti Γ, x :A ` B : SetiΓ ` Fun A (λx .B) : Seti

Set0 includes types defined by recursion like Vec A n.(Large) types:

Γ ` A : SetiΓ ` A : Seti+1 Γ ` Seti : Seti+1

E.g., Fun Set0 (λA.A→ (N→ A)) : Set1.In Agda: (A : Set) -> A -> N -> A : Set1.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 7 / 27

Page 8: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Equality

Conversion rule:

Γ ` t : A Γ ` A = A′ : SetiΓ ` t : A′

Type checking requires checking type equality!Equality axioms:

(β)Γ, x :A ` t : B Γ ` s : A

Γ ` (λx .t) s = t[s/x ] : B[s/x ]

(η)Γ ` t : Fun A (λx .B)

Γ ` (λx . t x) = t : Fun A (λx .B)x 6∈ FV(t)

Add computation axioms for primitive recursion.Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 8 / 27

Page 9: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

The Type Checking Task

Input a sequence of typed definitions in β-normal form

x0 : A0 = t0...

xn−1 : An−1 = tn−1

Check the sequence in order1 check that Ai is well-formed2 evaluate Ai to Xi in current environment3 check that ti is of type Xi

4 evaluate ti to di in current environment5 add binding xi : Xi = di to environment

Type conversion: need to check type values X , X ′ for equality

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 9 / 27

Page 10: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Values

In implementation of type theory, values could be:1 Normal forms (Agda 2)2 Weak head normal forms (Constructive Engine, Pollack)3 Explicit substitutions (Twelf)4 Closures (Epigram 2)5 Virtual machine code (Coq, Gregoire & Leroy (2002))6 Compiled code (Cayenne, Dirk Kleeblatt)

Need symbolic execution at compile time.Abstract over implementation via applicative structures.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 10 / 27

Page 11: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Applicative Structure

Domain D of values with 2 operations:1 Application · : D× D→ D2 Evaluation : Exp× (Var→ D)→ D.

Laws:cρ = c e.g. Fun,Setixρ = ρ(x)

(r s)ρ = rρ · sρ(λxt)ρ · d = t(ρ, x =d)

Variables x1, x2 ∈ D aka de Bruijn levels, generic values Coquand(1996).Neutral objects xi · d1 · . . . · dk are eliminations of variables akaatomic objects / accumulators.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 11 / 27

Page 12: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Checking Type Equality

Comparing type values

∆ ` X = X ′ ⇑ Set i X and X ′ are equal types at level i∆ ` e = e ′ ⇓ X neutral e and e ′ are equal, inferring type X∆ ` d = d ′ ⇑ X d and d ′ are equal, checked at type X

Roots:1 Setting of Coquand (1996)2 Type-directed η-equality of Harper & Pfenning (2005), extended to

dependent types3 Implementations: Agdalight, Epigram 2

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 12 / 27

Page 13: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Algorithmic Equality

Type mode ∆ ` X = X ′ ⇑ Set i (inputs: ∆,X ,X ′, output: i orfail).

∆ ` Seti = Seti ⇑ Set i + 1

∆ ` X = X ′ ⇑ Set i ∆, x∆ :X ` F · x∆ = F ′ · x∆ ⇑ Set j

∆ ` Fun X F = Fun X ′ F ′ ⇑ Set max(i , j)

∆ ` E = E ′ ⇓ Seti∆ ` E = E ′ ⇑ Set i

Arbitrary choice: asymmetric.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 13 / 27

Page 14: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Algorithmic Equality

Inference mode ∆ ` e = e ′ ⇓ X (inputs: ∆,e,e ′, output: X or fail).

∆ ` x = x ⇓ ∆(x)

∆ ` e = e ′ ⇓ Fun X F ∆ ` d = d ′ ⇑ X

∆ ` e d = e ′ d ′ ⇓ F · d

Checking mode ∆ ` d = d ′ ⇑ X (inputs: ∆, d , d ′,X , output: succeed orfail).

∆ ` e = e ′ ⇓ E1 ∆ ` E1 = E2 ⇓ Seti∆ ` e = e ′ ⇑ E2

∆, x∆ :X ` f · x∆ = f ′ · x∆ ⇑ F · x∆

∆ ` f = f ′ ⇑ Fun X F

∆ ` X = X ′ ⇑ Set i

∆ ` X = X ′ ⇑ Setji ≤ j

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 14 / 27

Page 15: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Verification of Algorithmic Equality

Completeness: Any two judgmentally equal expressions arerecognized equal by the algorithm.` t = t ′ : A implies ` tρid = t ′ρid ⇑ Aρid.

Soundness: Any two well-typed expressions recognized as equal arealso judgmentally equal.` t, t ′ : A and ` tρid = t ′ρid ⇑ Aρid imply ` t = t ′ : A.

Termination: the equality algorithm terminates on all well-typedexpressions.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 15 / 27

Page 16: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Towards a Kripke model

Completeness of algorithmic equality usually established viaKripke logical relation (semantic equality)

∆ ` d = d ′ : X

At base type X this could be defined as ∆ ` d = d ′ ⇑ X .Should model declarative judgements.Problem: transitivity of algorithmic equality non-trivial because ofasymmetries.Solution: two objects at base type shall be equal if they reify tothe same term.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 16 / 27

Page 17: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Contextual reification

Reification converts values to η-long β-normal forms.Reification of neutral objects x ~d involves reification of argumentsdi at their types.Thus, must be parameterized by context ∆ and type X .Structure similar to algorithmic equality.

∆ ` X ↘ A ⇑ Set i∆ ` e ↘ u ⇓ X∆ ` d ↘ t ⇑ X

Reification of functions (η-expansion):

∆, x :X ` f · x ↘ t ⇑ F · x∆ ` f ↘ λxt ⇑ Fun X F

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 17 / 27

Page 18: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Completeness

Objects that reify to the same term are algorithmically equal.

LemmaIf ∆ ` d ↘ t ⇑ X and ∆′ ` d ′ ↘ t ⇑ X ′ then ∆ ` d = d ′ ⇑ X .

Kripke logical relation between objects in a semantic typingenvironment.

for base types: ∆ ` d : X s ∆′ ` d ′ : X ′ iff ∆ ` d ↘ t ⇑ X and∆′ ` d ′ ↘ t ⇑ X ′ for some t,for function types: ∆ ` f : Fun X F s ∆′ ` f ′ : Fun X ′ F ′ iff∆ ` d : X s ∆′ ` d ′ : X ′ implies∆ ` f · d : F · d s ∆′ ` f ′ · d ′ : F ′ · d ′.

Symmetric and transitive by construction.Semantic equality ∆ ` d = d ′ : X iff ∆ ` d : X s ∆ ` d ′ : X .

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 18 / 27

Page 19: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Validity

Define ∆ ` ρ = ρ′ : Γ iff ∆ ` ρ(x) = ρ′(x) : Γ(x) for all x .

Theorem (Fundamental theorem)

If Γ ` t = t ′ : A and ∆ ` ρ = ρ′ : Γ then ∆ ` tρ = t ′ρ′ : Aρ.

Implies completeness of algorithmic equality.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 19 / 27

Page 20: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Soundness

Easy for algorithmic equality defined on terms.Uses substitution principle for declarative judgements.Substitution principle fails for algorithmic equality.

∆, x∆ :X ` f · x∆ = f ′ · x∆ ⇑ F · x∆

∆ ` f = f ′ ⇑ Fun X F

But it should hold for all values that come from syntax.Need to strengthen our notion of semantic equality byincorporating substitutions (Coquand et al., 2005).

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 20 / 27

Page 21: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Strong Semantic Equality

Equip D with reevaluation dρ ∈ D.Define strong semantic equality by

Θ |= d = d ′ : X ⇐⇒ ∀∆ ` ρ = ρ′ : Θ. ∆ ` dρ = d ′ρ′ : Xρ

Algorithmic equality is sound for strong semantic equality.Strong semantic equality models declarative judgements.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 21 / 27

Page 22: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Logical Relation between Syntax and Semantics

Theorem (Soundness)

If Γ ` t, t ′ : A and Γρid ` tρid = t ′ρid ⇑ Aρid then Γ ` t = t ′ : A.

Proof.Define a Kripke logical relation Γ ` t : A R© ∆ ` d : X between syntaxand semantics.For base types X , it holds if ∆ ` d ↘ t ′ ⇑ X and Γ ` t = t ′ : A.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 22 / 27

Page 23: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Conclusions

Verified βη-conversion test which scales to universes and largeeliminations.

Necessary tools came from Normalization-by-Evaluation.

From the distance: algorithm is β-evaluation followed byη-expansion.

Future work: scale to singleton types.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 23 / 27

Page 24: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Related Work

Martin-Lof 1975: NbE for Type Theory (weak conversion)Martin-Lof 2004: Talk on NbE (philosophical justification)Altenkirch Hofmann Streicher 1996: NbE for λ-free System FGregoire Leroy 2002: β-normalization by compilation for CICCoquand Pollack Takeyama 2003: LF with singleton typesDanielsson 2006: strongly typed NbE for LFAltenkirch Chapman 2007: big step normalization

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 24 / 27

Page 25: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Strong Validity

Define ∆ |= ρ = ρ′ : Γ iff ∆ |= ρ(x) = ρ′(x) : Γ(x) for all x .

Theorem (Fundamental theorem)

If Γ ` t = t ′ : A and ∆ |= ρ = ρ′ : Γ then ∆ |= tρ = t ′ρ′ : Aρ.

Implies completeness of algorithmic equality.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 25 / 27

Page 26: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Example: A Regular Expression Matcher in Agda(N.A.Danielsson)

data RegExp : Set where0 : RegExp -- Matches nothing.eps : RegExp -- Matches the empty string.+ : RegExp -> RegExp -> RegExp -- Choice.

data in : [ carrier ] -> RegExp -> Set wherematches-eps : [] in epsmatches-+l : forall {xs re re’}

-> xs in re -> xs in (re + re’)matches-+r : forall {xs re re’}

-> xs in re’ -> xs in (re + re’)

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 26 / 27

Page 27: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

Example: A Regular Expression Matcher in Agda(N.A.Danielsson)

matches : (xs : [ carrier ]) -> (re : RegExp) ->Maybe (xs in re)

matches [] eps = just matches-epsmatches xs (re + re’) with matches xs re... | just p = just (matches-+l p)... | nothing with matches xs re’... | just p = just (matches-+r) p)... | nothing = nothing

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 27 / 27

Page 28: Verifying a Semantic -Conversion Test for Martin-Löf Type ...abel/talkMPC08.pdfBackground Dependently typed languages allow speci cation, implementation, and veri cation in the same

T. Coquand (1996). ‘An Algorithm for Type-Checking DependentTypes’. In Mathematics of Program Construction. Selected Papersfrom the Third International Conference on the Mathematics ofProgram Construction (July 17–21, 1995, Kloster Irsee, Germany),vol. 26 of Science of Computer Programming, pp. 167–177. ElsevierScience.

T. Coquand, et al. (2005). ‘A Logical Framework with DependentlyTyped Records’. Fundamenta Informaticae 65(1-2):113–134.

B. Gregoire & X. Leroy (2002). ‘A compiled implementation of strongreduction’. In Proceedings of the seventh ACM SIGPLANInternational Conference on Functional Programming (ICFP ’02),Pittsburgh, Pennsylvania, USA, October 4-6, 2002, vol. 37 ofSIGPLAN Notices, pp. 235–246. ACM Press.

R. Harper & F. Pfenning (2005). ‘On Equivalence and CanonicalForms in the LF Type Theory’. ACM Transactions onComputational Logic 6(1):61–101.

Abel Coquand Dybjer (LMU, CTH) Semantic βη-Conversion DTP’08 27 / 27