Library MetaRocq.Template.Lib
From MetaRocq.Common Require Import uGraph.
From MetaRocq.Template Require Import Ast TemplateMonad Loader Checker.
From MetaRocq.Utils Require Import utils.
#[local] Set Universe Polymorphism.
From MetaRocq.Template Require Import Ast TemplateMonad Loader Checker.
From MetaRocq.Utils Require Import utils.
#[local] Set Universe Polymorphism.
Run a template program.
Notation "'$run' f" :=
ltac:(
let p y := exact y in
run_template_program f p
) (at level 0, only parsing).
ltac:(
let p y := exact y in
run_template_program f p
) (at level 0, only parsing).
Quote and term and its environment.
Unquote a term (using tmUnquote).
Notation "'$unquote' x" :=
ltac:(
let p y :=
match y with
| existT_typed_term ?T ?b ⇒ exact b
end
in
run_template_program (tmUnquote x) p
) (at level 0, only parsing).
ltac:(
let p y :=
match y with
| existT_typed_term ?T ?b ⇒ exact b
end
in
run_template_program (tmUnquote x) p
) (at level 0, only parsing).
Unquote a term (using tmUnquoteTyped).
Notation "'$unquote' x : T" :=
($run (tmUnquoteTyped T x))
(at level 0, T at level 100, x at next level, only parsing).
($run (tmUnquoteTyped T x))
(at level 0, T at level 100, x at next level, only parsing).
Definition unfold_toplevel {A} (x : A) : TemplateMonad A :=
tmBind (tmQuote x) (fun y ⇒
match y with
| tConst na _ ⇒ tmEval (Common.unfold na) x
| _ ⇒ tmFail "unfold_toplevel: not a constant"%bs
end).
tmBind (tmQuote x) (fun y ⇒
match y with
| tConst na _ ⇒ tmEval (Common.unfold na) x
| _ ⇒ tmFail "unfold_toplevel: not a constant"%bs
end).
Quote the definition of a constant.
Notation "'$quote_def' x" :=
($run (tmBind (unfold_toplevel x) tmQuote))
(at level 0, only parsing).
($run (tmBind (unfold_toplevel x) tmQuote))
(at level 0, only parsing).
Definition get_kername (t : term) : TemplateMonad kername :=
match t with
| tConst c u ⇒ tmReturn c
| _ ⇒ tmFail "get_kername: not a constant"%bs
end.
match t with
| tConst c u ⇒ tmReturn c
| _ ⇒ tmFail "get_kername: not a constant"%bs
end.
Get the kernel name of a constant.
Recursively quote the definition of a constant.
Notation "'$quote_def_rec' x" :=
($run (tmBind (unfold_toplevel x) tmQuoteRec))
(at level 0, only parsing).
($run (tmBind (unfold_toplevel x) tmQuoteRec))
(at level 0, only parsing).
Short-form notation for tLambda.
Short-form notation for tProd.
Short-form notation for tLetIn.
Notation tLet x A t b :=
(tLetIn {| binder_name := nNamed x; binder_relevance := Relevant |} t A b).
Notation "'__'" := (hole) (no associativity, at level 0).
(tLetIn {| binder_name := nNamed x; binder_relevance := Relevant |} t A b).
Notation "'__'" := (hole) (no associativity, at level 0).
Monadic notations.
Module TemplateMonadNotations.
Declare Scope tm_scope.
Delimit Scope tm_scope with tm.
Notation "c >>= f" :=
(tmBind c f)
(at level 50, left associativity) : tm_scope.
Notation "f =<< c" :=
(c >>= f)%tm
(at level 51, right associativity) : tm_scope.
Notation "'mlet' x <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ c2))%tm
(at level 100, c1 at next level, right associativity, x pattern) : tm_scope.
Notation "'mlet' ' pat <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ match x with pat ⇒ c2 end))%tm
(at level 100, pat pattern, c1 at next level, right associativity) : tm_scope.
Notation "x <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ c2))%tm
(at level 100, c1 at next level, right associativity) : tm_scope.
Notation "' pat <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ match x with pat ⇒ c2 end))%tm
(at level 100, pat pattern, c1 at next level, right associativity) : tm_scope.
Notation "e1 ;; e2" :=
(_ <- e1%tm ;; e2%tm)%tm
(at level 100, right associativity) : tm_scope.
End TemplateMonadNotations.
Declare Scope tm_scope.
Delimit Scope tm_scope with tm.
Notation "c >>= f" :=
(tmBind c f)
(at level 50, left associativity) : tm_scope.
Notation "f =<< c" :=
(c >>= f)%tm
(at level 51, right associativity) : tm_scope.
Notation "'mlet' x <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ c2))%tm
(at level 100, c1 at next level, right associativity, x pattern) : tm_scope.
Notation "'mlet' ' pat <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ match x with pat ⇒ c2 end))%tm
(at level 100, pat pattern, c1 at next level, right associativity) : tm_scope.
Notation "x <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ c2))%tm
(at level 100, c1 at next level, right associativity) : tm_scope.
Notation "' pat <- c1 ;; c2" :=
(c1 >>= (fun x ⇒ match x with pat ⇒ c2 end))%tm
(at level 100, pat pattern, c1 at next level, right associativity) : tm_scope.
Notation "e1 ;; e2" :=
(_ <- e1%tm ;; e2%tm)%tm
(at level 100, right associativity) : tm_scope.
End TemplateMonadNotations.
Packed inductives and constructors.
Record packed_inductive :=
{ pi_ind : inductive
; pi_body : one_inductive_body
; pi_mbody : mutual_inductive_body }.
{ pi_ind : inductive
; pi_body : one_inductive_body
; pi_mbody : mutual_inductive_body }.
Same as packed_inductive but for constructors.
The inductive this constructor belongs to.
The index of this constructor.
The body of this constructor.
Definition pi_ctors (pi : packed_inductive) : list packed_constructor :=
mapi (fun i ctor ⇒ {| pc_pi := pi ; pc_idx := i ; pc_body := ctor |}) pi.(pi_body).(ind_ctors).
mapi (fun i ctor ⇒ {| pc_pi := pi ; pc_idx := i ; pc_body := ctor |}) pi.(pi_body).(ind_ctors).
pi_block pi returns the list of packed inductives in the same block
as pi, including pi itself, ordered from first to last.
Definition pi_block (pi : packed_inductive) : list packed_inductive :=
mapi
(fun i body ⇒
{| pi_ind := {| inductive_mind := pi.(pi_ind).(inductive_mind) ; inductive_ind := i |}
; pi_body := body
; pi_mbody := pi.(pi_mbody) |})
pi.(pi_mbody).(ind_bodies).
mapi
(fun i body ⇒
{| pi_ind := {| inductive_mind := pi.(pi_ind).(inductive_mind) ; inductive_ind := i |}
; pi_body := body
; pi_mbody := pi.(pi_mbody) |})
pi.(pi_mbody).(ind_bodies).