Skip to content

Commit 7312ca8

Browse files
committed
Add a kludgy implementation of tmTry
Partial work towards #874. Does not support `tmDefinition`, `tmAxiom`, etc.
1 parent a8cb841 commit 7312ca8

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

template-coq/theories/Loader.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Notation "<% x %>" := (ltac:(let p y := exact y in quote_term x p))
1212
(* Use [return _] to avoid running the program twice on failure *)
1313
Notation "<# x #>" := (match TemplateMonad.Core.tmQuoteRec x return _ with qx => ltac:(let p y := exact y in run_template_program qx p) end)
1414
(only parsing).
15+
16+
#[global] Hint Extern 0 (Core.tmTryHelper ?run) => run_template_program run (fun v => refine v) : typeclass_instances.

template-coq/theories/TemplateMonad/Common.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Monomorphic Inductive option_instance (A : Type) : Type := my_Some : A -> option
2323
Arguments Some {A} a.
2424
Arguments None {A}.
2525

26+
Monomorphic Variant exn : Set := GenericError.
27+
28+
Variant option_try (A : Type) : Type := my_Value (val : A) | my_Error (err : exn).
29+
30+
Arguments my_Value {A} val.
31+
Arguments my_Error {A} _.
32+
2633
Record TMInstance@{t u r} :=
2734
{ TemplateMonad : Type@{t} -> Type@{r}
2835
; tmReturn : forall {A:Type@{t}}, A -> TemplateMonad A

template-coq/theories/TemplateMonad/Core.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,11 @@ Definition tmFix@{a b t u} {A : Type@{a}} {B : Type@{b}} (f : (A -> TemplateMona
181181
qu <- tmQuoteLevel@{u _ _};;
182182
let self := tConst (MPfile ["Core"; "TemplateMonad"; "Template"; "MetaCoq"], "tmFix'")%bs [qa;qb;qt;qu] in
183183
@tmFix'@{a b t u} A B (mkApps self [qA; qB]) f a)).
184+
185+
Class tmTryHelper@{t u} {A : Type@{t}} (run : TemplateMonad@{t u} A) := tmTry_ret : A.
186+
Definition tmTry@{t u} {A : Type@{t}} (run : TemplateMonad@{t u} A) : TemplateMonad@{t u} (option_try@{t} A)
187+
:= tmBind (tmInferInstance None (tmTryHelper run))
188+
(fun inst => match inst with
189+
| my_Some x => tmReturn (my_Value x)
190+
| my_None => tmReturn (my_Error GenericError)
191+
end).

test-suite/tmTry.v

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
From MetaCoq.Template Require Import All.
2+
3+
Import MCMonadNotation.
4+
Import bytestring.
5+
Open Scope bs.
6+
Open Scope list_scope.
7+
8+
Universes u0 u1.
9+
Constraint u0 < u1.
10+
MetaCoq Run (u <- tmQuote Type@{u0};;
11+
v <- tmTry (tmUnquoteTyped Type@{u0} u);;
12+
match v with
13+
| my_Value v => tmPrint (v -> True);; tmFail "first should not succeed"
14+
| my_Error _ => v' <- tmUnquoteTyped Type@{u1} u;;
15+
ret (v' -> False)
16+
end >>= tmPrint).
17+
18+
(*MetaCoq Run (tmDefinition "a" I;; tmTry (tmDefinition "a" I) >>= tmPrint).*)
19+
(*a is defined
20+
21+
Error: Anomaly "in Univ.repr: Universe MetaCoq.TestSuite.tmTry.101 undefined." Please report at http://coq.inria.fr/bugs/.*)
22+
(*MetaCoq Run (tmTry (tmDefinition "b" I);; mp <- tmCurrentModPath tt;; tmUnquote (tConst (mp, "b") []) >>= tmPrint).*)
23+
(*Error: Anomaly "Constant MetaCoq.TestSuite.tmTry.b does not appear in the environment."
24+
Please report at http://coq.inria.fr/bugs/.*)
25+
(*MetaCoq Run (tmDefinition "c" I;; mp <- tmCurrentModPath tt;;
26+
v <- tmTry (tmUnquoteTyped False (tConst (mp, "c") []));;
27+
match v with
28+
| my_Value v => ret (inl v)
29+
| my_Error _ => v' <- tmUnquoteTyped True (tConst (mp, "c") []);;
30+
ret (inr v')
31+
end >>= tmPrint).*)
32+
(*Error: Anomaly "in Univ.repr: Universe MetaCoq.TestSuite.tmTry.172 undefined." Please report at http://coq.inria.fr/bugs/.*)
33+
MetaCoq Run (tmAxiom "a'" True;; tmTry (tmAxiom "a'" True) >>= tmPrint).
34+
(*MetaCoq Run (tmTry (tmAxiom "b'" True);; mp <- tmCurrentModPath tt;; tmUnquote (tConst (mp, "b'") []) >>= tmPrint).*)
35+
(*Error: Anomaly "Constant MetaCoq.TestSuite.tmTry.b' does not appear in the environment."
36+
Please report at http://coq.inria.fr/bugs/.*)
37+
MetaCoq Run (tmAxiom "c'" True;; mp <- tmCurrentModPath tt;;
38+
v <- tmTry (tmUnquoteTyped False (tConst (mp, "c'") []));;
39+
match v with
40+
| my_Value v => tmPrint v;; tmFail "too early"
41+
| my_Error _ => v' <- tmUnquoteTyped True (tConst (mp, "c'") []);;
42+
ret v'
43+
end >>= tmPrint).

0 commit comments

Comments
 (0)