Sprinkles of extensionality for your vanilla type theory Adding - - PowerPoint PPT Presentation

sprinkles of extensionality for your vanilla type theory
SMART_READER_LITE
LIVE PREVIEW

Sprinkles of extensionality for your vanilla type theory Adding - - PowerPoint PPT Presentation

Sprinkles of extensionality for your vanilla type theory Adding custom rewrite rules to Agda Jesper Cockx Andreas Abel DistriNet KU Leuven 24 May 2016 What are we doing? Take some vanilla Agda . . . 1 / 17 What are we doing? Take


slide-1
SLIDE 1

Sprinkles of extensionality for your vanilla type theory

Adding custom rewrite rules to Agda Jesper Cockx Andreas Abel

DistriNet – KU Leuven

24 May 2016

slide-2
SLIDE 2

What are we doing?

Take some vanilla Agda . . .

1 / 17

slide-3
SLIDE 3

What are we doing?

Take some vanilla Agda . . . . . . and sprinkle some rewrite rules on top

1 / 17

slide-4
SLIDE 4

Goals of adding rewrite rules

1 Turn propositional equalities

into definitional ones

2 Add new primitives with

custom computation rules

2 / 17

slide-5
SLIDE 5

Disclaimer

This is basically one big hack. We are not responsible for any unintended side-effects such as unsoundness, non-termination, lack of subject reduction, shark attacks, or zombie outbreaks.

3 / 17

slide-6
SLIDE 6

Acknowledgements

A big thanks to Guillaume Brunerie Nils Anders Danielsson Martin Escardo and other brave early adopters to point out bugs and limitations of the rewriting mechanism!

4 / 17

slide-7
SLIDE 7

Sprinkles of extensionality for your vanilla type theory

1 What are rewrite rules? 2 What can you do with them? 3 How do they work?

slide-8
SLIDE 8

Sprinkles of extensionality for your vanilla type theory

1 What are rewrite rules? 2 What can you do with them? 3 How do they work?

slide-9
SLIDE 9

What are rewrite rules?

A way to plug new computation rules into Agda’s typechecker plus0 : (n : N) → n + 0 ≡ n plus0 n = . . . {-# REWRITE plus0 #-} This adds a computation rule n + 0 n

5 / 17

slide-10
SLIDE 10

What are rewrite rules?

A way to plug new computation rules into Agda’s typechecker plus0 : (n : N) → n + 0 ≡ n plus0 n = . . . {-# REWRITE plus0 #-} This adds a computation rule n + 0 n

5 / 17

slide-11
SLIDE 11

What are rewrite rules?

Applications the reflection rule . . . Γ ⊢ e : u ≡ v u = v . . . but only for specific equality proofs e ∈ Rew: Γ ⊢ e : f ¯ p ≡ v σ : ∆ ⇒ Γ (e ∈ Rew) f ¯ pσ vσ

6 / 17

slide-12
SLIDE 12

What are rewrite rules not?

Not a conservative extension Can destroy termination e.g. x + y y + x Can destroy confluence e.g. true false Can destroy subject reduction e.g. subst P e x x

7 / 17

slide-13
SLIDE 13

Sprinkles of extensionality for your vanilla type theory

1 What are rewrite rules? 2 What can you do with them? 3 How do they work?

slide-14
SLIDE 14

Make neutral terms reduce1

xs + + [] xs (xs + + ys) + + zs xs + + (ys + + zs) map f (xs + + ys) map f xs + + map f ys map (λx. x) xs xs subst (λ . B) p x x cong (λx. x) p p

1See New equations for neutral terms by Guillaume Allais,

Conor McBride, and Pierre Boutillier.

8 / 17

slide-15
SLIDE 15

Implement higher inductive types

Circle : Set base : Circle loop : base ≡ base elimCircle : (P : Circle → Set)(b : P base) (l : subst P loop b ≡ b) (x : Circle) → P x elimCircle P b l base b cong (elimCircle P b l) loop l

9 / 17

slide-16
SLIDE 16

Add custom resizing rules2

resize : Seti → Setj Prop′ : Set1 Prop′ = Σ[X : Set] ((x y : X) → x ≡ y) Prop : Set0 Prop = resize Prop′

2Based on code by Martin Escardo, see

cs.bham.ac.uk/~mhe/impredicativity-via-rewriting/

10 / 17

slide-17
SLIDE 17

Add custom resizing rules2

resize : Seti → Setj Prop′ : Set1 Prop′ = Σ[X : Set] ((x y : X) → x ≡ y) Prop : Set0 Prop = resize Prop′

2Based on code by Martin Escardo, see

cs.bham.ac.uk/~mhe/impredicativity-via-rewriting/

10 / 17

slide-18
SLIDE 18

Do shallow embeddings: cubical3

I : Set 0 1 : I — : A → A → Set i t : t[i → 0] — t[i → 1] $ : (a — b) → I → A funext : ((x : A) → f x — g x) → (f — g) funext p = i (λx. p x $ i)

3Based on A cubical crossroads by Conor McBride at AIM XXIII,

see github.com/jespercockx/cubes for the Agda code

11 / 17

slide-19
SLIDE 19

Do shallow embeddings: cubical3

I : Set 0 1 : I — : A → A → Set i t : t[i → 0] — t[i → 1] $ : (a — b) → I → A funext : ((x : A) → f x — g x) → (f — g) funext p = i (λx. p x $ i)

3Based on A cubical crossroads by Conor McBride at AIM XXIII,

see github.com/jespercockx/cubes for the Agda code

11 / 17

slide-20
SLIDE 20

Sprinkles of extensionality for your vanilla type theory

1 What are rewrite rules? 2 What can you do with them? 3 How do they work?

slide-21
SLIDE 21

Higher-order Miller matching

The LHS is compiled into a pattern f p1 . . . pn f should be a defined symbol or postulate patterns p1, . . . , pn should bind all variables Patterns can be higher-order and non-linear f p1 . . . pn λx.p (x : P1) → P2 Set p x y1 . . . yn (x free, yi bound, yi = yj) y p1 . . . pn (y bound) Arbitrary terms t

12 / 17

slide-22
SLIDE 22

Higher-order Miller matching

The LHS is compiled into a pattern f p1 . . . pn f should be a defined symbol or postulate patterns p1, . . . , pn should bind all variables Patterns can be higher-order and non-linear f p1 . . . pn λx.p (x : P1) → P2 Set p x y1 . . . yn (x free, yi bound, yi = yj) y p1 . . . pn (y bound) Arbitrary terms t

12 / 17

slide-23
SLIDE 23

Applying rewrite rules

How to rewrite f t1 . . . tn with rewrite rule f p1 . . . pn r?

1 t1 . . . tn are matched against linear part of

p1 . . . pn, producing a substitution σ

2 Non-linear parts are checked for equality

after applying σ

3 f t1 . . . tn is rewritten to rσ

13 / 17

slide-24
SLIDE 24

Effects on constraint solving

Previously inert terms can now reduce, so we have to postpone constraint solving E.g. x + ?0 = x Defined functions become matchable, so pruning has to be more conservative E.g. ?1 (f x) = true

14 / 17

slide-25
SLIDE 25

Effects on constraint solving

Previously inert terms can now reduce, so we have to postpone constraint solving E.g. x + ?0 = x Defined functions become matchable, so pruning has to be more conservative E.g. ?1 (f x) = true

14 / 17

slide-26
SLIDE 26

Rewriting systems in type theory

Other systems based on rewrite rules: Dedukti (dedukti.gforge.inria.fr) CoqMT (github.com/strub/coqmt) . . .

15 / 17

slide-27
SLIDE 27

Future work

Add proper import system Add confluence checking / completion Custom eta rules

16 / 17

slide-28
SLIDE 28

Conclusion

You can use rewrite rules to simplify neutral terms such as x + 0 to implement new primitives such as HIT’s to embed other theories such as cubical . . . but you should know what you are doing Why don’t you give it a try?

17 / 17

slide-29
SLIDE 29

Conclusion

You can use rewrite rules to simplify neutral terms such as x + 0 to implement new primitives such as HIT’s to embed other theories such as cubical . . . but you should know what you are doing Why don’t you give it a try?

17 / 17