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 some vanilla . . . and sprinkle some Agda . . . rewrite rules on top 1 / 17
Goals of adding rewrite rules 1 Turn propositional equalities into definitional ones 2 Add new primitives with custom computation rules 2 / 17
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
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
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?
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?
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
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
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
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
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?
Make neutral terms reduce 1 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 1 See New equations for neutral terms by Guillaume Allais, Conor McBride, and Pierre Boutillier. 8 / 17
Implement higher inductive types : Set Circle : Circle base : base ≡ base loop elim Circle : ( P : Circle → Set )( b : P base ) ( l : subst P loop b ≡ b ) ( x : Circle ) → P x elim Circle P b l base � b cong ( elim Circle P b l ) loop � l 9 / 17
Add custom resizing rules 2 resize : Set i → Set j Prop ′ : Set 1 Prop ′ = Σ[ X : Set ] (( x y : X ) → x ≡ y ) Prop : Set 0 Prop = resize Prop ′ 2 Based on code by Martin Escardo, see cs.bham.ac.uk/~mhe/impredicativity-via-rewriting/ 10 / 17
Add custom resizing rules 2 resize : Set i → Set j Prop ′ : Set 1 Prop ′ = Σ[ X : Set ] (( x y : X ) → x ≡ y ) Prop : Set 0 Prop = resize Prop ′ 2 Based on code by Martin Escardo, see cs.bham.ac.uk/~mhe/impredicativity-via-rewriting/ 10 / 17
Do shallow embeddings: cubical 3 : Set I : I 0 1 — : 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 ) 3 Based on A cubical crossroads by Conor McBride at AIM XXIII, see github.com/jespercockx/cubes for the Agda code 11 / 17
Do shallow embeddings: cubical 3 : Set I : I 0 1 — : 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 ) 3 Based on A cubical crossroads by Conor McBride at AIM XXIII, see github.com/jespercockx/cubes for the Agda code 11 / 17
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?
Higher-order Miller matching The LHS is compiled into a pattern f p 1 . . . p n f should be a defined symbol or postulate patterns p 1 , . . . , p n should bind all variables Patterns can be higher-order and non-linear f p 1 . . . p n x y 1 . . . y n ( x free, y i bound, y i � = y j ) λ x . p y p 1 . . . p n ( y bound) ( x : P 1 ) → P 2 Arbitrary terms t Set p 12 / 17
Higher-order Miller matching The LHS is compiled into a pattern f p 1 . . . p n f should be a defined symbol or postulate patterns p 1 , . . . , p n should bind all variables Patterns can be higher-order and non-linear f p 1 . . . p n x y 1 . . . y n ( x free, y i bound, y i � = y j ) λ x . p y p 1 . . . p n ( y bound) ( x : P 1 ) → P 2 Arbitrary terms t Set p 12 / 17
Applying rewrite rules How to rewrite f t 1 . . . t n with rewrite rule f p 1 . . . p n � r ? 1 t 1 . . . t n are matched against linear part of p 1 . . . p n , producing a substitution σ 2 Non-linear parts are checked for equality after applying σ 3 f t 1 . . . t n is rewritten to r σ 13 / 17
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
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
Rewriting systems in type theory Other systems based on rewrite rules: Dedukti ( dedukti.gforge.inria.fr ) CoqMT ( github.com/strub/coqmt ) . . . 15 / 17
Future work Add proper import system Add confluence checking / completion Custom eta rules 16 / 17
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
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
Recommend
More recommend