Skip to content

Commit 8905d68

Browse files
committed
refactor: Return tmQuote to inductive and made TemplateMonad sort poly
Not fully sort poly, but at least some for tmQuote
1 parent 150527f commit 8905d68

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

template-rocq/theories/TemplateMonad/Core.v

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Import MRMonadNotation.
1616

1717

1818
(** *** The TemplateMonad type *)
19-
Cumulative Inductive TemplateMonad@{t u} : Type@{t} -> Prop :=
19+
Cumulative Inductive TemplateMonad@{s; t u} : Type@{t} -> Prop :=
2020
(* Monadic operations *)
2121
| tmReturn : forall {A:Type@{t}}, A -> TemplateMonad A
2222
| tmBind : forall {A B : Type@{t}}, TemplateMonad A -> (A -> TemplateMonad B)
@@ -46,6 +46,12 @@ Cumulative Inductive TemplateMonad@{t u} : Type@{t} -> Prop :=
4646
| tmCurrentModPath : unit -> TemplateMonad modpath
4747

4848
(* Quoting and unquoting commands *)
49+
(* Similar to MetaRocq Quote Definition ... := ... *)
50+
| tmQuote : forall {A:Type@{s;t}}, A -> TemplateMonad Ast.term
51+
(* Similar to MetaRocq Quote Recursively Definition but takes a boolean "bypass opacity" flag.
52+
([true] - quote bodies of all dependencies (transparent and opaque);
53+
[false] -quote bodies of transparent definitions only) *)
54+
| tmQuoteRecTransp : forall {A:Type@{s;t}}, A -> bool(* bypass opacity? *) -> TemplateMonad program
4955
(* Quote the body of a definition or inductive. Its name need not be fully qualified *)
5056
| tmQuoteInductive : kername -> TemplateMonad mutual_inductive_body
5157
| tmQuoteUniverses : TemplateMonad ConstraintSet.t
@@ -64,23 +70,9 @@ Cumulative Inductive TemplateMonad@{t u} : Type@{t} -> Prop :=
6470
| tmInferInstance : option reductionStrategy -> forall A : Type@{t}, TemplateMonad (option_instance A)
6571
.
6672

67-
(* Moving outside of the inductive definition only to make it sort polymorphic but
68-
avoid having to update all of the TemplateMonad definition to be sort poylmorphic *)
69-
(* Similar to MetaRocq Quote Definition ... := ... *)
70-
Polymorphic Axiom tmQuote@{s; t u}
71-
: forall {A : Type@{s;t}}, A -> TemplateMonad@{t u} Ast.term.
72-
73-
(* Moving outside of the inductive definition only to make it sort polymorphic but
74-
avoid having to update all of the TemplateMonad definition to be sort poylmorphic *)
75-
(* Similar to MetaRocq Quote Recursively Definition but takes a boolean "bypass opacity" flag.
76-
([true] - quote bodies of all dependencies (transparent and opaque);
77-
[false] -quote bodies of transparent definitions only) *)
78-
Polymorphic Axiom tmQuoteRecTransp@{s; t u}
79-
: forall {A : Type@{s;t}}, A -> bool(* bypass opacity? *) -> TemplateMonad@{t u} program.
80-
8173
(** This version of [tmBind] flattens nesting structure; using it in deeply recursive template programs can speed things up drastically *)
8274
(** We use [tmBind] in the recursive position to avoid quadratic blowup in the number of [tmOptimizedBind]s *)
83-
Fixpoint tmOptimizedBind@{t u} {A B : Type@{t}} (v : TemplateMonad@{t u} A) : (A -> TemplateMonad@{t u} B) -> TemplateMonad@{t u} B
75+
Fixpoint tmOptimizedBind@{s; t u} {A B : Type@{t}} (v : TemplateMonad@{s; t u} A) : (A -> TemplateMonad@{s; t u} B) -> TemplateMonad@{s; t u} B
8476
:= match v with
8577
| tmReturn x => fun f => f x
8678
| tmBind v k => fun f => tmOptimizedBind v (fun v => tmBind (k v) f)
@@ -89,25 +81,25 @@ Fixpoint tmOptimizedBind@{t u} {A B : Type@{t}} (v : TemplateMonad@{t u} A) : (A
8981
end.
9082

9183
(** Flatten nested [tmBind] *)
92-
Fixpoint tmOptimize'@{t u} {A B : Type@{t}} (v : TemplateMonad@{t u} A) : (A -> TemplateMonad@{t u} B) -> TemplateMonad@{t u} B
84+
Fixpoint tmOptimize'@{s; t u} {A B : Type@{t}} (v : TemplateMonad@{s; t u} A) : (A -> TemplateMonad@{s; t u} B) -> TemplateMonad@{s; t u} B
9385
:= match v with
9486
| tmReturn x => fun f => f x
9587
| tmBind v k => fun f => tmOptimize' v (fun v => tmOptimize' (k v) f)
9688
| tmFail msg => fun _ => tmFail msg
9789
| v => tmBind v
9890
end.
99-
Definition tmOptimize@{t u} {A : Type@{t}} (v : TemplateMonad@{t u} A) : TemplateMonad@{t u} A
91+
Definition tmOptimize@{s; t u} {A : Type@{t}} (v : TemplateMonad@{s; t u} A) : TemplateMonad@{s; t u} A
10092
:= tmOptimize' v tmReturn.
10193

10294
(** This allow to use notations of MonadNotation *)
103-
Definition TemplateMonad_UnoptimizedMonad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
95+
Definition TemplateMonad_UnoptimizedMonad@{s; t u} : Monad@{t u} TemplateMonad@{s; t u} :=
10496
{| ret := @tmReturn ; bind := @tmBind |}.
10597

106-
Definition TemplateMonad_OptimizedMonad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
98+
Definition TemplateMonad_OptimizedMonad@{s; t u} : Monad@{t u} TemplateMonad@{s; t u} :=
10799
{| ret := @tmReturn ; bind := @tmOptimizedBind |}.
108100

109101
(* We don't want to make the optimized monad an instance, because it blows up performance in some cases *)
110-
Definition TemplateMonad_Monad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
102+
Definition TemplateMonad_Monad@{s; t u} : Monad@{t u} TemplateMonad@{s; t u} :=
111103
Eval hnf in TemplateMonad_UnoptimizedMonad.
112104
Global Existing Instance TemplateMonad_Monad.
113105

@@ -213,30 +205,30 @@ Definition TypeInstanceOptimized : Common.TMInstance :=
213205
Definition TypeInstance : Common.TMInstance :=
214206
Eval hnf in TypeInstanceUnoptimized.
215207

216-
Definition tmQuoteSort@{U t u} : TemplateMonad@{t u} sort
208+
Definition tmQuoteSort@{U t u} : TemplateMonad@{_; t u} sort
217209
:= p <- @tmQuote Prop (Type@{U} -> True);;
218210
match p with
219211
| tProd _ (tSort s) _ => ret s
220212
| _ => tmFail "Anomaly: tmQuote (Type -> True) should be (tProd _ (tSort _) _)!"%bs
221213
end.
222-
Definition tmQuoteUniverse@{U t u} : TemplateMonad@{t u} Universe.t
214+
Definition tmQuoteUniverse@{U t u} : TemplateMonad@{_; t u} Universe.t
223215
:= s <- @tmQuoteSort@{U t u};;
224216
match s with
225217
| sType u => ret u
226218
| _ => tmFail "Sort does not carry a universe (is not Type)"%bs
227219
end.
228-
Definition tmQuoteLevel@{U t u} : TemplateMonad@{t u} Level.t
220+
Definition tmQuoteLevel@{U t u} : TemplateMonad@{_; t u} Level.t
229221
:= u <- tmQuoteUniverse@{U t u};;
230222
match Universe.get_is_level u with
231223
| Some l => ret l
232224
| None => tmFail "Universe is not a level"%bs
233225
end.
234226

235-
Definition tmFix'@{a b t u} {A : Type@{a}} {B : Type@{b}} (qtmFix' : Ast.term) (f : (A -> TemplateMonad@{t u} B) -> (A -> TemplateMonad@{t u} B)) : A -> TemplateMonad@{t u} B
227+
Definition tmFix'@{a b t u} {A : Type@{a}} {B : Type@{b}} (qtmFix' : Ast.term) (f : (A -> TemplateMonad@{_; t u} B) -> (A -> TemplateMonad@{_; t u} B)) : A -> TemplateMonad@{_; t u} B
236228
:= f (fun a
237-
=> tmFix <- tmUnquoteTyped (Ast.term -> ((A -> TemplateMonad@{t u} B) -> (A -> TemplateMonad@{t u} B)) -> A -> TemplateMonad@{t u} B) qtmFix';;
229+
=> tmFix <- tmUnquoteTyped (Ast.term -> ((A -> TemplateMonad@{_; t u} B) -> (A -> TemplateMonad@{_; t u} B)) -> A -> TemplateMonad@{_; t u} B) qtmFix';;
238230
tmFix qtmFix' f a).
239-
Definition tmFix@{a b t u} {A : Type@{a}} {B : Type@{b}} (f : (A -> TemplateMonad@{t u} B) -> (A -> TemplateMonad@{t u} B)) : A -> TemplateMonad@{t u} B
231+
Definition tmFix@{a b t u} {A : Type@{a}} {B : Type@{b}} (f : (A -> TemplateMonad@{_; t u} B) -> (A -> TemplateMonad@{_; t u} B)) : A -> TemplateMonad@{_; t u} B
240232
:= f (fun a
241233
=> (qA <- tmQuote A;;
242234
qB <- tmQuote B;;

test-suite/quote_global_sort.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Axiom ax_raise@{u} : forall (A : Type@{Exc;u}), A.
1212

1313
MetaRocq Run (tmQuote ax_raise >>= tmPrint).
1414

15-
(* This one requires -allow-rewrite-rules enabled
15+
(* This one requires -allow-rewrite-rules enabled *)
16+
(*
1617
Symbol raise@{u} : forall (A : Type@{Exc;u}), A.
1718
1819
MetaRocq Run (tmQuote raise >>= tmPrint).

0 commit comments

Comments
 (0)