Implementing a Modal Dependent Type Theory Daniel Gratzer 0 Jonathan - - PowerPoint PPT Presentation

implementing a modal dependent type theory
SMART_READER_LITE
LIVE PREVIEW

Implementing a Modal Dependent Type Theory Daniel Gratzer 0 Jonathan - - PowerPoint PPT Presentation

Implementing a Modal Dependent Type Theory Daniel Gratzer 0 Jonathan Sterling 1 Lars Birkedal 0 August 21, 2019 ICFP 19 0 Aarhus University 1 Carnegie Mellon University 0 Modalities an idempotent monad for a left adjoint. A Category


slide-1
SLIDE 1

Implementing a Modal Dependent Type Theory

Daniel Gratzer0 Jonathan Sterling1 Lars Birkedal0 August 21, 2019 ICFP ’19

0Aarhus University 1Carnegie Mellon University

slide-2
SLIDE 2

Modalities

We want to add a single modality MLTT, . Γ ⊢ M : A

  • M ":" A and M only mentions variables of the shape B
  • In staged programming, A represents precomputed values.
  • In modal FRP, A represents stable types.
  • In distributed programming, A represents globally available values.

is just a comonad with an idempotent monad for a left adjoint. A Category Theorist 1

slide-3
SLIDE 3

Our Contribution: MLTT

We contribute MLTT, a dependent type theory with...

  • the box modality, A
  • dependent sums, Σ(A,B)
  • dependent products, Π(A,B)

           With both β and η

  • natural numbers, nat
  • intensional identity types, Id(A,M,N)
  • a cumulative hierarchy of universes, U0,U1...

We have constructed a precise syntactic account of MLTT, and proved the decidability of type-checking for it.

2

slide-4
SLIDE 4

Typical Problems with Modalities

We could imagine just dropping all local variables when constructing A:

tm/lock?!

Γ ⊢ M : A Γ,∆ ⊢ box(M) : A In this case box(M) cannot commute with substitution: box(M)[N/x] could be well-typed while box(M[N/x]) is ill-typed! We can try versions of this rule,1 but we’ll opt for another approach.

1Prawitz 1967

3

slide-5
SLIDE 5

Adding Judgmental Structure

We’ll incorporate Fitch-style judgmental structure2 to handle A: (Contexts) Γ

  • · | Γ,x : A | Γ.

Instead of dropping part of the context we can lock it away:

tm/lock

Γ. ⊢ M : A Γ ⊢ [M] : A

tm/var

Γ = Γ0,x : A,Γ1  Γ1 Γ ⊢ x : A Crucially, later on we are able to unlock the context:

tm/unlock

M A M  A

2Clouston 2018

4

slide-6
SLIDE 6

Adding Judgmental Structure

We’ll incorporate Fitch-style judgmental structure2 to handle A: (Contexts) Γ

  • · | Γ,x : A | Γ.

Instead of dropping part of the context we can lock it away:

tm/lock

Γ. ⊢ M : A Γ ⊢ [M] : A

tm/var

Γ = Γ0,x : A,Γ1  Γ1 Γ ⊢ x : A Crucially, later on we are able to unlock the context:

tm/unlock

Γ ⊢ M : A Γ ⊢ [M] : A

2Clouston 2018

4

slide-7
SLIDE 7

Adding Judgmental Structure

We’ll incorporate Fitch-style judgmental structure2 to handle A: (Contexts) Γ

  • · | Γ,x : A | Γ.

Instead of dropping part of the context we can lock it away:

tm/lock

Γ. ⊢ M : A Γ ⊢ [M] : A

tm/var

Γ = Γ0,x : A,Γ1  Γ1 Γ ⊢ x : A Crucially, later on we are able to unlock the context:

tm/unlock

Γ ⊢ M : A Γ ⊢ [M] : A Not obvious, but these rules respect substitution!

2Clouston 2018

4

slide-8
SLIDE 8

A Small Programming Break

How does our intuition for A square with [−] and [−]? Programs extractA : A → A extractA(x) [x] dupA A A dupA x Holes

5

slide-9
SLIDE 9

A Small Programming Break

How does our intuition for A square with [−] and [−]? Programs extractA : A → A extractA(x) [x] dupA : A → A dupA(x) ? Holes x : A ⊢ ? : A

5

slide-10
SLIDE 10

A Small Programming Break

How does our intuition for A square with [−] and [−]? Programs extractA : A → A extractA(x) [x] dupA : A → A dupA(x) [?] Holes x : A, ⊢ ? : A

5

slide-11
SLIDE 11

A Small Programming Break

How does our intuition for A square with [−] and [−]? Programs extractA : A → A extractA(x) [x] dupA : A → A dupA(x) [[?]] Holes x : A,, ⊢ ? : A

5

slide-12
SLIDE 12

A Small Programming Break

How does our intuition for A square with [−] and [−]? Programs extractA : A → A extractA(x) [x] dupA : A → A dupA(x) [[[?]]] Holes x : A ⊢ ? : A

5

slide-13
SLIDE 13

A Small Programming Break

How does our intuition for A square with [−] and [−]? Programs extractA : A → A extractA(x) [x] dupA : A → A dupA(x) [[[x]]] Holes

5

slide-14
SLIDE 14

Making Hard Choices: Defjnitional Equalities for MLTT

We are able to equip A with both a β and η rule in MLTT:

tm/unlock-lock

Γ. ⊢ M : A Γ ⊢ [[M]] = M : A

tm/lock-unlock

Γ ⊢ M : A Γ ⊢ M = [[M]] : A Notice, no commutating conversions, this is a win from the Fitch style.3 The premises of these rules are subtle and important!

 

M A M A M   A M A

3Clouston 2018 and Birkedal, Clouston, Mannaa, Møgelberg, Pitts, Spitters 2019

6

slide-15
SLIDE 15

Making Hard Choices: Defjnitional Equalities for MLTT

We are able to equip A with both a β and η rule in MLTT:

tm/unlock-lock

Γ. ⊢ M : A Γ ⊢ [[M]] = M : A

tm/lock-unlock

Γ ⊢ M : A Γ ⊢ M = [[M]] : A Notice, no commutating conversions, this is a win from the Fitch style.3 The premises of these rules are subtle and important! Γ. ⊢ M : A =⇒ Γ ⊢ M : A Γ ⊢ [[M]] : A

  • =⇒ Γ ⊢ M : A

3Clouston 2018 and Birkedal, Clouston, Mannaa, Møgelberg, Pitts, Spitters 2019

6

slide-16
SLIDE 16

Taking Stock

What do we have at this point?

  • MLTT: a declarative modal dependent type theory.
  • We can prove the expected admissibilities: substitution, presupposition, ...
  • As well as modal admissibilities: lock contraction, strengthening...

These are important checks to ensure that MLTT behaves well. Big remaining question: can we implement this?

7

slide-17
SLIDE 17

Taking Stock

What do we have at this point?

  • MLTT: a declarative modal dependent type theory.
  • We can prove the expected admissibilities: substitution, presupposition, ...
  • As well as modal admissibilities: lock contraction, strengthening...

Complication: non-local and sensitive to extensions. These are important checks to ensure that MLTT behaves well. Big remaining question: can we implement this?

7

slide-18
SLIDE 18

Taking Stock

What do we have at this point?

  • MLTT: a declarative modal dependent type theory.
  • We can prove the expected admissibilities: substitution, presupposition, ...
  • As well as modal admissibilities: lock contraction, strengthening...

These are important checks to ensure that MLTT behaves well. Big remaining question: can we implement this?

7

slide-19
SLIDE 19

Implementing a Type Theory: A General Recipe

The process of implementing some type theory T might follow these steps:

  • 1. Construct a bidirectional syntax for T: T⇆.
  • 2. Prove that T admits a normalization theorem.
  • 3. Conclude that T enjoys decidable conversion.
  • 4. Prove that T⇆ enjoys decidable type-checking.
  • 5. Prove that every term of T is convertible with a term from T⇆.
  • 6. Conclude that T⇆ presents T and is implementable.

Many of these proofs rely on the admissiblities we established!

8

slide-20
SLIDE 20

Implementing a Type Theory: A General Recipe

The process of implementing some type theory T might follow these steps:

  • 1. Construct a bidirectional syntax for T: T⇆.
  • 2. Prove that T admits a normalization theorem.
  • 3. Conclude that T enjoys decidable conversion.
  • 4. Prove that T⇆ enjoys decidable type-checking.
  • 5. Prove that every term of T is convertible with a term from T⇆.
  • 6. Conclude that T⇆ presents T and is implementable.

Many of these proofs rely on the admissiblities we established!

8

slide-21
SLIDE 21

Implementing a Type Theory: A General Recipe

The process of implementing some type theory T might follow these steps:

  • 1. Construct a bidirectional syntax for T: T⇆.
  • 2. Prove that T admits a normalization theorem.
  • 3. Conclude that T enjoys decidable conversion.
  • 4. Prove that T⇆ enjoys decidable type-checking.
  • 5. Prove that every term of T is convertible with a term from T⇆.
  • 6. Conclude that T⇆ presents T and is implementable.

Many of these proofs rely on the admissiblities we established!

8

slide-22
SLIDE 22

Implementing a Type Theory: A General Recipe

The process of implementing some type theory T might follow these steps:

  • 1. Construct a bidirectional syntax for T: T⇆.
  • 2. Prove that T admits a normalization theorem.
  • 3. Conclude that T enjoys decidable conversion.
  • 4. Prove that T⇆ enjoys decidable type-checking.
  • 5. Prove that every term of T is convertible with a term from T⇆.
  • 6. Conclude that T⇆ presents T and is implementable.

Many of these proofs rely on the admissiblities we established!

8

slide-23
SLIDE 23

Implementing a Type Theory: A General Recipe

The process of implementing some type theory T might follow these steps:

  • 1. Construct a bidirectional syntax for T: T⇆.
  • 2. Prove that T admits a normalization theorem.
  • 3. Conclude that T enjoys decidable conversion.
  • 4. Prove that T⇆ enjoys decidable type-checking.
  • 5. Prove that every term of T is convertible with a term from T⇆.
  • 6. Conclude that T⇆ presents T and is implementable.

Many of these proofs rely on the admissiblities we established!

8

slide-24
SLIDE 24

Implementing a Type Theory: A General Recipe

The process of implementing some type theory T might follow these steps:

  • 1. Construct a bidirectional syntax for T: T⇆.
  • 2. Prove that T admits a normalization theorem.
  • 3. Conclude that T enjoys decidable conversion.
  • 4. Prove that T⇆ enjoys decidable type-checking.
  • 5. Prove that every term of T is convertible with a term from T⇆.
  • 6. Conclude that T⇆ presents T and is implementable.

Many of these proofs rely on the admissiblities we established!

8

slide-25
SLIDE 25

Implementing MLTT: Bidirectional Syntax

MLTT is a simple enough that we can extend a bidirectional presentation of MLTT:

  • Terms are split into two categories:

(Checkable) N,M

  • R | λx. M | . . .

(Synthesizable) R,S

  • (M : A) | x | R(M) | . . .
  • We split the judgments along these lines as well:

check

Γ ⊢ M ⇐ A

synth

Γ ⊢ S ⇒ A

  • We can extend the standard rules with the new rules for A:

Γ. ⊢ M ⇐ A Γ ⊢ [M] ⇐ A Γ ⊢ M ⇒ A Γ ⊢ [M] ⇒ A

9

slide-26
SLIDE 26

The Payoff of Bidirectionalism

By restricting MLTT to MLTT⇆

 we can obtain the following result:

Theorem If we can Γ ⊢ A = B type is decidable* then so are Γ ⊢ M ⇐ A and Γ ⊢ M ⇒ A. We’ve restricted MLTT⇆

 so that most one rule applies in each case.

*We also need whnfs, but this will follow from how we prove the decidability of conversion.

10

slide-27
SLIDE 27

Implementing MLTT: Normalization

Normalization is a common way to decide equality. Defjnition The normalization function has the following type: normA

Γ : TermΓ,A → TermΓ,A,

Completeness: If Γ ⊢ M1 = M2 : A then normA

Γ(M1) = normA Γ(M2).

Soundness: If Γ ⊢ M : A then Γ ⊢ M = normA

Γ(M) : A

Corollary M N A normA M and normA N are identical.

11

slide-28
SLIDE 28

Implementing MLTT: Normalization

Normalization is a common way to decide equality. Defjnition The normalization function has the following type: normA

Γ : TermΓ,A → TermΓ,A,

Completeness: If Γ ⊢ M1 = M2 : A then normA

Γ(M1) = normA Γ(M2).

Soundness: If Γ ⊢ M : A then Γ ⊢ M = normA

Γ(M) : A

Corollary Γ ⊢ M = N : A ⇐⇒ normA

Γ(M) and normA Γ(N) are identical. 11

slide-29
SLIDE 29

Normalization-by-Evaluation

In order to actually defjne normA

Γ we use normalization-by-evaluation.4

Slogan: evaluate syntax to a computational domain, quote it to a normal form.

  • Evaluation performs β-reduction.
  • Quotation is type-directed and handles η-expansion.
  • The algorithm scales to support A, even with η.

4Martin-Löf 1975, see Abel 2013 for an overview.

12

slide-30
SLIDE 30

A Sketch of a Proof Sketch

Lots of details to balance here, since we also support a full dependent type theory! Completeness: Construct a Kripke PER model on the computational domain. Soundness: Construct a double Kripke cross-language logical relation between the computational domain and syntax. The main sources of complexity are the modality and universes.

13

slide-31
SLIDE 31

A Sketch of a Proof Sketch

Lots of details to balance here, since we also support a full dependent type theory! Completeness: Construct a Kripke PER model on the computational domain. Soundness: Construct a double Kripke cross-language logical relation between the computational domain and syntax. The main sources of complexity are the modality and universes.

13

slide-32
SLIDE 32

Describing Normal Forms for MLTT

After normalization we end up with normal forms, but what do these look like? Γ ⊢

ne R : Π(A,x.B)

Γ ⊢

nf M : A

Γ ⊢

ne R(M) : B[M/x]

Γ ⊢

ne S : Ui

Γ ⊢

nf S : Ui

Γ. ⊢

nf M : A

Γ ⊢

nf [M] : A

Γ ⊢

ne M : A

Γ ⊢

ne [M] : A

Corollary There is no term · ⊢ bad : Π(A,A) Observe that neutral terms are synthesizable, normal forms are checkable.5

5Coquand 1996

14

slide-33
SLIDE 33

The Full Theorem

Theorem (Decidability of Type-Checking)

  • Both Γ ⊢ M ⇒ A and Γ ⊢ M ⇐ A are decidable.
  • If Γ ⊢ M ⇒ A or Γ ⊢ M ⇐ A then Γ ⊢ M : A.
  • If Γ ⊢ M : A, there exists* some N such that Γ ⊢ M = N : A and Γ ⊢ N ⇐ A.

This theorem provides the foundation for our implementation of MLTT. To our knowledge, this is the fjrst such result for MLTT with A.

*In particular, N normA Γ(M).

15

slide-34
SLIDE 34

Conclusions

We contribute MLTT, a dependent type theory with...

  • the box modality, A
  • dependent sums, Σ(A,B)
  • dependent products, Π(A,B)

           With both β and η

  • natural numbers, nat
  • intensional identity types, Id(A,M,N)
  • a cumulative hierarchy of universes, U0,U1...

We have proved the decidability of typechecking for MLTT, and implemented it. http://github.com/jozefg/blott

16