Normalization by Evaluation for Martin-L of Type Theory Daniel - - PowerPoint PPT Presentation

normalization by evaluation for martin l of type theory
SMART_READER_LITE
LIVE PREVIEW

Normalization by Evaluation for Martin-L of Type Theory Daniel - - PowerPoint PPT Presentation

Normalization by Evaluation for Martin-L of Type Theory Daniel Gratzer October 1, 2018 Goal Produce a function nf ( , t, A ) : Ctx Term Type Term so that the following 3 conditions hold: 1. t 1 t 2 : A = nf ( , t


slide-1
SLIDE 1

Normalization by Evaluation for Martin-L¨

  • f Type Theory

Daniel Gratzer October 1, 2018

slide-2
SLIDE 2

Goal

Produce a function nf(Γ, t, A) : Ctx × Term × Type ⇀ Term so that the following 3 conditions hold:

  • 1. Γ ⊢ t1 ≡ t2 : A =

⇒ nf(Γ, t1, A) = nf(Γ, t2, A)

  • 2. If Γ ⊢ t : A then Γ ⊢ t ≡ nf(Γ, t, A) : A
  • 3. If Γ ⊢ t : A then nf(Γ, t, A) is a normal form

– more on this shortly.

slide-3
SLIDE 3

Why Bother?

Why bother to do this when it’s so much easier to not do things?

  • 1. Lars told me to prove normalization for a type theory
slide-4
SLIDE 4

Why Bother?

Why bother to do this when it’s so much easier to not do things?

  • 1. Lars told me to prove normalization for a type theory
  • 2. Termination, canonicity, consistency are corollaries
  • 3. Decidability of type-checking

This because of the conversion rule: Γ ⊢ A ≡ B Γ ⊢ t : A Γ ⊢ t : B

  • 4. Adequacy in logical frameworks depends on normalization
  • 5. Completeness of focused proof strategies is equivalent
  • 6. Coherence theorems are normalization theorems in disguise
slide-5
SLIDE 5

Why Normalization by Evaluation (NbE)?

Techniques for proving normalization abound, why NbE?

  • 1. Scales to support many languages
  • full dependent types
  • proof-irrelevant types
  • impredicative quantification
  • sized types
  • (conjectured) fitch-style guarded dependent type theory
  • (conjectured) cubical type theory.
  • 2. Amenable to formalization in a (stronger) type theory
  • 3. Practical for implementation*
  • 4. Principled semantic interpretation
slide-6
SLIDE 6

What Semantic Interpretation?

It’s too much to discuss today, Jon & Bas have a paper though.

slide-7
SLIDE 7

What Semantic Interpretation?

It’s too much to discuss today, Jon & Bas have a paper though.

slide-8
SLIDE 8

Why Not X Instead?1

The most common alternatives to NbE are based on rewriting:

  • Define some relation → (steps to) between terms
  • a term is normal when it cannot be reduced further with →.
  • Use logical relations/reducibility candidates to show that →

terminates for well-typed terms.

1for X = NbE

slide-9
SLIDE 9

Why Not X Instead?1

The most common alternatives to NbE are based on rewriting:

  • Define some relation → (steps to) between terms
  • a term is normal when it cannot be reduced further with →.
  • Use logical relations/reducibility candidates to show that →

terminates for well-typed terms. Not all equalities make sense as reduction rules!

1for X = NbE

slide-10
SLIDE 10

Why Not X Instead?1

The most common alternatives to NbE are based on rewriting:

  • Define some relation → (steps to) between terms
  • a term is normal when it cannot be reduced further with →.
  • Use logical relations/reducibility candidates to show that →

terminates for well-typed terms. Not all equalities make sense as reduction rules! These proofs are extremely brittle!

1for X = NbE

slide-11
SLIDE 11

Why Not X Instead?1

The most common alternatives to NbE are based on rewriting:

  • Define some relation → (steps to) between terms
  • a term is normal when it cannot be reduced further with →.
  • Use logical relations/reducibility candidates to show that →

terminates for well-typed terms. Not all equalities make sense as reduction rules! These proofs are extremely brittle! Entangles questions of reduction strategy!

1for X = NbE

slide-12
SLIDE 12

A Language

We need to specify the language that we’re going to normalize.

slide-13
SLIDE 13

The Main Judgments

Our type theory is divided into various judgments: Γ ⊢ Γ is a valid context Γ ⊢ T In context Γ, T is a type Γ ⊢ t : T In context Γ, t has type T

slide-14
SLIDE 14

The Main Judgments

Our type theory is divided into various judgments: Γ ⊢ Γ is a valid context Γ ⊢ T In context Γ, T is a type Γ ⊢ t : T In context Γ, t has type T Corresponding equality judgments: Γ ⊢ t1 ≡ t2 : T.

slide-15
SLIDE 15

Explicit Substitutions

We use explicit substitutions, Γ ⊢ σ : ∆, in our type theory: Γ ⊢ Γ ⊢ · : () Γ ⊢ 1 : Γ Γ ⊢ T Γ.T ⊢ ↑1 : Γ Γ ⊢ σ1 : ∆ ∆ ⊢ σ2 : Ξ Γ ⊢ σ2 ◦ σ1 : Ξ Γ ⊢ σ : ∆ ∆ ⊢ T Γ ⊢ t : T{σ} Γ ⊢ σ.t : ∆.T Crucial rule: Γ ⊢ t : T ∆ ⊢ σ : Γ ∆ ⊢ t{σ} : T{σ}

slide-16
SLIDE 16

A Language

The rules for types and contexts: () ⊢ Γ ⊢ Γ ⊢ A Γ.A ⊢ Γ ⊢ A Γ.A ⊢ B Γ ⊢ A → B Γ ⊢ Γ ⊢ Unit Γ ⊢ Γ ⊢ U Γ ⊢ A : U Γ ⊢ A

slide-17
SLIDE 17

A Language

The rules for terms: Γ ⊢ Γ ⊢ Unit : U Γ ⊢ tt : Unit Γ ⊢ A : U Γ.A ⊢ B : U Γ ⊢ A → B : U Γ ⊢ A Γ.A ⊢ t : B Γ ⊢ λt : A → B Γ ⊢ t : A → B Γ ⊢ u : A Γ ⊢ t(u) : B{1.u} Γ1.T.Γ2 ⊢ |Γ2| = k Γ1.T.Γ2 ⊢ xk : T{↑k+1}

slide-18
SLIDE 18

The Wrinkle

We need the conversion rule for any sort of type theory. Γ ⊢ t : A Γ ⊢ A ≡ B Γ ⊢ t : B Dependence means term equality matters for type equality. Γ ⊢ A ≡ B : U Γ ⊢ A ≡ B

slide-19
SLIDE 19

The Wrinkle – The Main Equality Rules

Γ ⊢ u : A Γ.A ⊢ t : B Γ ⊢ (λt)(u) ≡ t{1.u} : B{1.u} Γ ⊢ t : A → B Γ ⊢ λ(t{↑1}(x0)) ≡ t : A → B Γ ⊢ t : Unit Γ ⊢ t ≡ tt : Unit

slide-20
SLIDE 20

Neutral and Normal Forms

Let us isolate special terms which will be canonical for ≡.

  • 1. Neutral terms: variables or normals stuck on variables.
  • 2. Normal forms: terms in β-normal and η-long forms.

Γ ⊢ xn : A Γ ⊢neu xn : A Γ ⊢neu e : A → B Γ ⊢nf v : A Γ ⊢neu e(v) : B{1.v}

slide-21
SLIDE 21

Neutral and Normal Forms

Let us isolate special terms which will be canonical for ≡.

  • 1. Neutral terms: variables or normals stuck on variables.
  • 2. Normal forms: terms in β-normal and η-long forms.

Γ ⊢ xn : A Γ ⊢neu xn : A Γ ⊢neu e : A → B Γ ⊢nf v : A Γ ⊢neu e(v) : B{1.v} Γ ⊢ Γ ⊢nf tt : Unit Γ ⊢nf Unit : U Γ ⊢ A Γ.A ⊢nf t : B Γ ⊢nf λt : A → B Γ ⊢nf A : U Γ.A ⊢nf B : U Γ ⊢nf A → B : U Γ ⊢neu e : U Γ ⊢nf e : U

slide-22
SLIDE 22

Normalization by Evaluation

Now we have a goal, construct Γ ⊢nf nf(Γ, t, A) : A given Γ ⊢ t : A.

slide-23
SLIDE 23

Normalization by Evaluation – Historical Context

Original idea: normalize programs using the ambient semantic universe. Latent in Martin-L¨

  • f’s original proofs of the decidability of typing.
slide-24
SLIDE 24

Normalization by Evaluation – Historical Context

Next found in implementation of Minlog: eval : (Term t) → t quote : t → (Term t) normalize = quote . eval Done in Scheme for the simply-typed lambda calculus at first, adapted to other settings.

slide-25
SLIDE 25

Normalization by Evaluation – Historical Context

To adapt to a proof people opted for domains instead of a PL D ∼ = (D → D) ⊕ (N ∪ V)⊥ Then define the following: eval : Term → D quote : D ⇀ Term

slide-26
SLIDE 26

Normalization by Evaluation – Historical Context

These historical approaches are imperfect:

  • Intrinsic typing proved intractable for impredicativity or

dependent types.

  • Using domains adds unnecessary complexity and is far removed

from implementations.

  • The direct “reflect to the metatheory” approach does not scale

to extrensic typing.

slide-27
SLIDE 27

Normalization by Evaluation – Historical Context

These historical approaches are imperfect:

  • Intrinsic typing proved intractable for impredicativity or

dependent types.

  • Using domains adds unnecessary complexity and is far removed

from implementations.

  • The direct “reflect to the metatheory” approach does not scale

to extrensic typing. Many presentations now use a different semantic model: syntax.

slide-28
SLIDE 28

A Syntactic Semantic Domain

Construct a syntax in which all expressions are canonical. Divided between neutrals, normals, values, closures.

slide-29
SLIDE 29

A Syntactic Semantic Domain – Neutrals

Neutral elements represent computations which are stuck on some variable. e ::= xℓ | app(e, ↓A v) N.B. The argument to app(e, −) must be fully evaluated and annotated.

slide-30
SLIDE 30

A Syntactic Semantic Domain – Closures

What happens when we go under a binder?

slide-31
SLIDE 31

A Syntactic Semantic Domain – Closures

What happens when we go under a binder? We choose to suspend evaluation and record the current state with a closure. f ::= t{ρ} ρ is the environment we’re interpreting t. This removes the need for domains, is called defunctionalization.

slide-32
SLIDE 32

A Syntactic Semantic Domain – Values

It’s difficult to isolate η-long forms for dependent type theory. We settle for isolating β-normal forms for now. v, A ::= λ. f | tt | Unit | Uni | Π A. F

slide-33
SLIDE 33

A Syntactic Semantic Domain – Values

It’s difficult to isolate η-long forms for dependent type theory. We settle for isolating β-normal forms for now. v, A ::= λ. f | tt | Unit | Uni | Π A. F | ↑A e Need to include neutrals with type information to allow η-expansions later.

slide-34
SLIDE 34

A Syntactic Semantic Domain

v, A ::= λ. f | tt | Unit | Uni | Π A1. F | ↑A e f, F ::= t{ρ} e ::= xℓ | app(e, v) n ::= ↓A v ρ ::= · | ρ.v

slide-35
SLIDE 35

Paying the Piper – Typing Information

The usage of ↓A v and ↑A e seems very arbitrary. Why do we need typing information?

  • We need type information to know whether η-expansion is

necessary now that we have neutrals of all types. In the domain-theoretic or intrinsic formulation this was baked in as we disallowed such neutrals.

slide-36
SLIDE 36

Paying the Piper – Typing Information

The usage of ↓A v and ↑A e seems very arbitrary. Why do we need typing information?

  • We need type information to know whether η-expansion is

necessary now that we have neutrals of all types. In the domain-theoretic or intrinsic formulation this was baked in as we disallowed such neutrals.

  • Coquand proposed adding ↓A v to mark a value that should be

η-expanded at type A during quotation.

  • Quotation proceeds by casing on this type.
slide-37
SLIDE 37

The Algorithm

Now that we have defined our sorts of terms, we can define the algorithm.

  • 1. Evaluate a term to a value in some environment

ρ | = t ⇓ v

  • 2. Quote a normal form back to a term in a context of length c.

c n ⇑ t

  • 3. Inject/reflect a term context into an environment.

↑Γ ρ

slide-38
SLIDE 38

The Algorithm

nf(Γ, t, T) = t′ ⇐ ⇒ ↑Γ ρ ∧ (ρ | = t ⇓ v) ∧ (ρ | = T ⇓ A) ∧ |Γ| ↓A v ⇑ t′ The relational presentation is ideal for a constructive setting.

slide-39
SLIDE 39

The Algorithm – Defining Evaluation

The evaluation judgment is defined by inspection on t. ρ.v | = x0 ⇓ v ρ | = tt ⇓ tt ρ | = Unit ⇓ Unit ρ | = U ⇓ Uni ρ | = λt ⇓ λ. t{ρ} ρ | = T1 ⇓ A ρ | = T1 → T2 ⇓ Π A. T2{ρ}

slide-40
SLIDE 40

The Algorithm – Defining Evaluation

The evaluation judgment is defined by inspection on t. ρ.v | = x0 ⇓ v ρ | = tt ⇓ tt ρ | = Unit ⇓ Unit ρ | = U ⇓ Uni ρ | = λt ⇓ λ. t{ρ} ρ | = T1 ⇓ A ρ | = T1 → T2 ⇓ Π A. T2{ρ} What about the only construct in our language that computes?

slide-41
SLIDE 41

The Algorithm – Defining Evaluation

Application uses an auxiliary relation: v1 @ v2 v. ρ.a | = t ⇓ v λ. t{ρ} @ a v ρ.a | = T ⇓ B ↑Π A. T{ρ} e @ a ↑B app(e, ↓A a) ρ | = t ⇓ v1 ρ | = u ⇓ v2 v1 @ v2 v ρ | = t(u) ⇓ v Rule of thumb: each eliminator gets an auxiliary judgment to either perform β-reduction or construct a new neutral.

slide-42
SLIDE 42

The Algorithm – Defining Evaluation

We use a judgment so that syntactic substitutions produce new semantic environments. ρ | = 1 ⇓ ρ ρ.v | = ↑1 ⇓ ρ ρ1 | = σ1 ⇓ ρ2 ρ2 | = σ2 ⇓ ρ3 ρ1 | = σ2 ◦ σ1 ⇓ ρ3 ρ1 | = σ ⇓ ρ2 ρ2 | = t ⇓ v ρ1 | = σ.t ⇓ ρ2.v

slide-43
SLIDE 43

The Algorithm – Defining Evaluation

We use a judgment so that syntactic substitutions produce new semantic environments. ρ | = 1 ⇓ ρ ρ.v | = ↑1 ⇓ ρ ρ1 | = σ1 ⇓ ρ2 ρ2 | = σ2 ⇓ ρ3 ρ1 | = σ2 ◦ σ1 ⇓ ρ3 ρ1 | = σ ⇓ ρ2 ρ2 | = t ⇓ v ρ1 | = σ.t ⇓ ρ2.v Using this, we can interpret t{σ}: ρ | = σ ⇓ ρ′ ρ′ | = t ⇓ v ρ | = t{σ} ⇓ v

slide-44
SLIDE 44

The Algorithm – Defining Quotation

In order to define c n ⇑ t we need to define two other forms of quotation:

  • c v ⇑ T – quotation of semantic types.
  • c e ⇑ t – quotation of neutrals.
slide-45
SLIDE 45

The Algorithm – Defining Quotation

Quotation for normals proceeds by casing on the type. v @ ↑A xc b ρ.xc | = T ⇓ B c + 1 ↓B b ⇑ t c ↓Π A. T{ρ} v ⇑ λt c ↓Unit v ⇑ tt c ↓Uni Unit ⇑ Unit c ↓Uni A ⇑ T1 ρ.xc | = T ⇓ B c + 1 ↓Uni B ⇑ T2 c ↓Uni Π A. T{ρ} ⇑ T1 → T2 c e ⇑ t c ↓− ↑− e ⇑ t

slide-46
SLIDE 46

The Algorithm – Defining Quotation

Quotation for neutrals proceeds by casing on the neutral itself. c xℓ ⇑ x0{↑c−(ℓ+1)} c e ⇑ t1 c n ⇑ t2 c app(e, n) ⇑ t1(t2)

slide-47
SLIDE 47

The Algorithm – Defining Quotation

Quotation for neutrals proceeds by casing on the neutral itself. c xℓ ⇑ x0{↑c−(ℓ+1)} c e ⇑ t1 c n ⇑ t2 c app(e, n) ⇑ t1(t2) Quotation for types likewise proceed by casing on the type. c Unit ⇑ Unit c Uni ⇑ U c A ⇑ T1 ρ.xc | = T ⇓ B c + 1 B ⇑ T2 c Π A. T{ρ} ⇑ T1 → T2 c e ⇑ t c ↑− e ⇑ t

slide-48
SLIDE 48

Final Step

  • 1. Evaluate a term to a value in some environment
  • 2. Quote a normal form back to a term in a context of length c.
  • 3. Inject/reflect a term context into an environment.
slide-49
SLIDE 49

Final Step

  • 1. Evaluate a term to a value in some environment
  • 2. Quote a normal form back to a term in a context of length c.
  • 3. Inject/reflect a term context into an environment.

↑() · ↑Γ ρ ρ | = T ⇓ A ↑Γ.T ρ.↑A x|Γ|

slide-50
SLIDE 50

Why is This Correct?

Now we have to prove some stuff.

  • 1. Γ ⊢ t1 ≡ t2 : A =

⇒ nf(Γ, t1, A) = nf(Γ, t2, A)

  • 2. If Γ ⊢ t : A then Γ ⊢ t ≡ nf(Γ, t, A) : A
  • 3. If Γ ⊢ t : A then nf(Γ, t, A) is a normal form
slide-51
SLIDE 51

Why is This Correct?

Now we have to prove some stuff.

  • 1. Γ ⊢ t1 ≡ t2 : A =

⇒ nf(Γ, t1, A) = nf(Γ, t2, A)

  • 2. If Γ ⊢ t : A then Γ ⊢ t ≡ nf(Γ, t, A) : A
  • 3. If Γ ⊢ t : A then nf(Γ, t, A) is a normal form

Can now prove this by induction!

slide-52
SLIDE 52

Completeness

Γ ⊢ t1 ≡ t2 : A = ⇒ nf(Γ, t1, A) = nf(Γ, t2, A) Proof intuition: build a PER model!

  • Each type A is associated with a PER of values: A = R.
  • Each PER satisfies the neutral-normal yoga
slide-53
SLIDE 53

Completeness – Neutral-normal yoga

Fix two distinguished PERs: Nf = {(n1, n2) | ∀m. ∃t. m n1 ⇑ t ∧ m n2 ⇑ t} Ne = {(e1, e2) | ∀m. ∃t. m e1 ⇑ t ∧ m e2 ⇑ t} For each R = A we require that R is sandwiched between these two PERs. {(↑A e1, ↑A e2) | (e1, e2) ∈ Ne} ⊆ R ⊆ {(v1, v2) | (↓A v1, ↓A v2) ∈ Nf}

slide-54
SLIDE 54

Completeness – The fundamental lemma

We can define a notion of related environments ρ1 = ρ2 ∈ Γ.

  • 1. If Γ ⊢ t1 ≡ t2 : T then for all ρ1 = ρ2 ∈ Γ the following

holds.

  • ρ1 |

= t1 ⇓ v1

  • ρ2 |

= t2 ⇓ v2

  • ρ1 |

= T ⇓ A

  • A = R
  • (v1, v2) ∈ R
  • 2. If Γ ⊢ T1 ≡ T2 then for all ρ1 = ρ2 ∈ Γ the following holds.
  • ρ1 |

= T1 ⇓ A1

  • ρ2 |

= T2 ⇓ A2

  • A1 = A2 = R
  • ∀m. ∃T. m A1 ⇑ T ∧ m A2 ⇑ T
slide-55
SLIDE 55

Completeness – explicit substitutions

Without explicit substitutions, the fundamental lemma is doomed: no β rules will hold!

slide-56
SLIDE 56

Completeness – explicit substitutions

Without explicit substitutions, the fundamental lemma is doomed: no β rules will hold! Let us suppose that ρ | = u ⇓ va: ρ | = (λt)(u) ⇓ v ⇐ ⇒ (λ. t{ρ}) @ va v ⇐ ⇒ ρ.va | = t ⇓ v ⇐ ⇒ (ρ | = 1.u ⇓ ρ.va) ∧ (ρ.va | = t ⇓ v) ⇐ ⇒ ρ | = t{1.u} ⇓ v With implicit substitutions this last step fails!

slide-57
SLIDE 57

Completeness – explicit substitutions

Without explicit substitutions, the fundamental lemma is doomed: no β rules will hold! Let us suppose that ρ | = u ⇓ va: ρ | = (λt)(u) ⇓ v ⇐ ⇒ (λ. t{ρ}) @ va v ⇐ ⇒ ρ.va | = t ⇓ v ⇐ ⇒ (ρ | = 1.u ⇓ ρ.va) ∧ (ρ.va | = t ⇓ v) ⇐ ⇒ ρ | = t{1.u} ⇓ v With implicit substitutions this last step fails! I learned this Saturday afternoon. Whoops.

slide-58
SLIDE 58

Completeness

the fundamental lemma + neutral-normal yoga = completeness

slide-59
SLIDE 59

Soundness

To prove if Γ ⊢ t : A then Γ ⊢ t ≡ nf(Γ, t, A) : A we construct a logical relation!

slide-60
SLIDE 60

Soundness – the logical relation

We define some relation Γ | = t : T v ∈ A.

slide-61
SLIDE 61

Soundness – the logical relation

We define some relation Γ | = t : T v ∈ A. Γ | = t : T v ∈ A = ⇒ ∃t′.

  • |Γ| ↓A v ⇑ t′

  • Γ ⊢ t ≡ t′ : T
slide-62
SLIDE 62

Soundness – the fundamental lemma

We can extend the logical relation to substitutions: Γ | = σ : Γ ρ.

  • If Γ ⊢ t : T
  • for any σ and ρ such that ∆ |

= σ : Γ ρ

  • for any v and A such that ρ |

= t ⇓ v and ρ | = T ⇓ A

slide-63
SLIDE 63

Soundness – the fundamental lemma

We can extend the logical relation to substitutions: Γ | = σ : Γ ρ.

  • If Γ ⊢ t : T
  • for any σ and ρ such that ∆ |

= σ : Γ ρ

  • for any v and A such that ρ |

= t ⇓ v and ρ | = T ⇓ A

  • ∆ |

= t{σ} : T{σ} v ∈ A

slide-64
SLIDE 64

Soundness – the fundamental lemma

We can extend the logical relation to substitutions: Γ | = σ : Γ ρ.

  • If Γ ⊢ t : T
  • for any σ and ρ such that ∆ |

= σ : Γ ρ

  • for any v and A such that ρ |

= t ⇓ v and ρ | = T ⇓ A

  • ∆ |

= t{σ} : T{σ} v ∈ A If this holds then Γ ⊢ t : T implies Γ ⊢ t ≡ nf(Γ, t, T) : T

slide-65
SLIDE 65

Dependent Types Complicates Things

  • Defining the PER model for completeness requires either

induction-recursion or Allen-style spines.

  • The logical-relation is well-founded only with respect to an
  • rdering on semantic types.
  • All type constructions must be done relationally to account for

universes. e.g., A must be A = B

slide-66
SLIDE 66

Dependent Types Complicates Things

  • Defining the PER model for completeness requires either

induction-recursion or Allen-style spines.

  • The logical-relation is well-founded only with respect to an
  • rdering on semantic types.
  • All type constructions must be done relationally to account for

universes. e.g., A must be A = B Happy to discuss these issues offline.

slide-67
SLIDE 67

Dependent Types Complicates Things

  • Defining the PER model for completeness requires either

induction-recursion or Allen-style spines.

  • The logical-relation is well-founded only with respect to an
  • rdering on semantic types.
  • All type constructions must be done relationally to account for

universes. e.g., A must be A = B Happy to discuss these issues offline. Thanks.