Skip to content

Commit 8a42c5b

Browse files
committed
Fix issue #1042 MetaCoq Run does not support evars.
1 parent dfea0a5 commit 8a42c5b

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

template-coq/src/g_template_coq.mlg

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ let to_ltac_val c = Tacinterp.Value.of_constr c
5151
let run_template_program ~pm env evm ~poly pgm =
5252
Run_template_monad.run_template_program_rec ~poly (fun ~st _ _ _ -> st) ~st:pm env (evm, pgm)
5353

54-
let fresh_env () =
54+
let fresh_env () =
5555
let env = Global.env () in
5656
let sigma = Evd.from_env env in
5757
env, sigma
5858

5959
let to_constr_evars sigma c = EConstr.to_constr ~abort_on_undefined_evars:false sigma c
6060
}
61-
61+
6262
(** ********* Commands ********* *)
6363

6464
VERNAC COMMAND EXTEND TemplateCoq_Test_Quote CLASSIFIED AS QUERY STATE program
@@ -76,7 +76,7 @@ VERNAC COMMAND EXTEND TemplateCoq_Quote_Definition CLASSIFIED AS SIDEFF STATE pr
7676
{ let (env, evm) = fresh_env () in
7777
let (evm, def) = Constrintern.interp_open_constr env evm def in
7878
let (evm, pgm) = EConstr.fresh_global env evm (Lazy.force Template_monad.ptmQuoteDefinition) in
79-
let pgm = Constr.mkApp (EConstr.to_constr evm pgm, [|Constr_quoter.quote_ident name; Constr.mkRel 0;
79+
let pgm = Constr.mkApp (EConstr.to_constr evm pgm, [|Constr_quoter.quote_ident name; Constr.mkRel 0;
8080
to_constr_evars evm def|]) in
8181
run_template_program env evm ~poly pgm }
8282
END
@@ -98,7 +98,7 @@ VERNAC COMMAND EXTEND TemplateCoq_Quote_Recursively_Definition CLASSIFIED AS SID
9898
{ let (env, evm) = fresh_env () in
9999
let (evm, def) = Constrintern.interp_open_constr env evm def in
100100
let (evm, pgm) = EConstr.fresh_global env evm (Lazy.force Template_monad.ptmQuoteRecDefinition) in
101-
let pgm = Constr.mkApp (EConstr.to_constr evm pgm, [|Constr_quoter.quote_ident name; Constr.mkRel 0;
101+
let pgm = Constr.mkApp (EConstr.to_constr evm pgm, [|Constr_quoter.quote_ident name; Constr.mkRel 0;
102102
to_constr_evars evm def|]) in
103103
run_template_program env evm ~poly pgm }
104104
END
@@ -108,7 +108,7 @@ VERNAC COMMAND EXTEND TemplateCoq_Test_Unquote CLASSIFIED AS QUERY STATE program
108108
{ let (env, evm) = fresh_env () in
109109
let (evm, def) = Constrintern.interp_open_constr env evm def in
110110
let (evm, pgm) = EConstr.fresh_global env evm (Lazy.force Template_monad.ptmTestUnquote) in
111-
let pgm = Constr.mkApp (EConstr.to_constr evm pgm,
111+
let pgm = Constr.mkApp (EConstr.to_constr evm pgm,
112112
[|to_constr_evars evm def|]) in
113113
run_template_program env evm ~poly pgm }
114114
END
@@ -119,7 +119,7 @@ VERNAC COMMAND EXTEND TemplateCoq_Make_Definition CLASSIFIED AS SIDEFF STATE pro
119119
let (evm, def) = Constrintern.interp_open_constr env evm def in
120120
let (evm, pgm) = EConstr.fresh_global env evm (Lazy.force Template_monad.ptmMkDefinition) in
121121
let pgm = Constr.mkApp (EConstr.to_constr evm pgm,
122-
[|Constr_quoter.quote_ident name;
122+
[|Constr_quoter.quote_ident name;
123123
to_constr_evars evm def|]) in
124124
run_template_program env evm ~poly pgm }
125125
END
@@ -129,16 +129,17 @@ VERNAC COMMAND EXTEND TemplateCoq_Make_Inductive CLASSIFIED AS SIDEFF STATE prog
129129
{ let (env, evm) = fresh_env () in
130130
let (evm, def) = Constrintern.interp_open_constr env evm def in
131131
let (evm, pgm) = EConstr.fresh_global env evm (Lazy.force Template_monad.ptmMkInductive) in
132-
let pgm = Constr.mkApp (EConstr.to_constr evm pgm,
132+
let pgm = Constr.mkApp (EConstr.to_constr evm pgm,
133133
[|Constr_quoter.quote_bool false; to_constr_evars evm def|]) in
134134
run_template_program env evm ~poly pgm }
135135
END
136136

137137
VERNAC COMMAND EXTEND TemplateCoq_Run_Template_Program CLASSIFIED AS SIDEFF STATE program
138138
| #[ poly = polymorphic ] [ "MetaCoq" "Run" constr(def) ] ->
139139
{ let (env, evm) = fresh_env () in
140-
let (evm, def) = Constrintern.interp_open_constr env evm def in
141-
let pgm = to_constr_evars evm def in
140+
let (pgm, ctx) = Constrintern.interp_constr env evm def in
141+
let evm = Evd.from_ctx ctx in
142+
let pgm = EConstr.to_constr ~abort_on_undefined_evars:true evm pgm in
142143
run_template_program env evm ~poly pgm }
143144
END
144145

template-coq/src/run_template_monad.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ let rec unquote_pos trm : int =
7070
let (h,args) = app_full trm [] in
7171
match args with
7272
[x] ->
73-
if constr_equall h cposI then
73+
if constr_equall h cposI then
7474
(2 * unquote_pos x + 1)
7575
else if constr_equall h cposO then
7676
(2 * unquote_pos x)
7777
else not_supported_verb trm "unquote_pos"
78-
| [] ->
78+
| [] ->
7979
if constr_equall h cposzero then 1
8080
else not_supported_verb trm "unquote_pos"
8181
| _ -> bad_term_verb trm "unquote_pos"
@@ -87,7 +87,7 @@ let unquote_Z trm : int =
8787
if constr_equall h cZpos then unquote_pos x
8888
else if constr_equall h cZneg then - unquote_pos x
8989
else not_supported_verb trm "unquote_pos"
90-
| [] ->
90+
| [] ->
9191
if constr_equall h cZ0 then 0
9292
else not_supported_verb trm "unquote_pos"
9393
| _ -> bad_term_verb trm "unquote_pos"
@@ -96,12 +96,12 @@ let unquote_constraint_type trm (* of type constraint_type *) : constraint_type
9696
let (h,args) = app_full trm [] in
9797
match args with
9898
[x] ->
99-
if constr_equall h tunivLe then
99+
if constr_equall h tunivLe then
100100
let n = unquote_Z x in
101101
if n = 0 then Univ.Le
102102
else Univ.Lt
103103
else not_supported_verb trm "unquote_constraint_type"
104-
| [] ->
104+
| [] ->
105105
if constr_equall h tunivEq then Univ.Eq
106106
else not_supported_verb trm "unquote_constraint_type"
107107
| _ -> bad_term_verb trm "unquote_constraint_type"
@@ -176,7 +176,7 @@ let denote_variance trm (* of type Variance *) : Variance.t =
176176
else if constr_equall trm cInvariant then Variance.Invariant
177177
else not_supported_verb trm "denote_variance"
178178

179-
179+
180180
let denote_variance evm trm (* of type Variance.t list *) : _ * Variance.t array =
181181
let variances = List.map denote_variance (unquote_list trm) in
182182
evm, Array.of_list variances
@@ -243,9 +243,9 @@ let unquote_one_inductive_entry env evm trm (* of type one_inductive_entry *) :
243243
let map_option f o =
244244
match o with
245245
| Some x -> Some (f x)
246-
| None -> None
246+
| None -> None
247247

248-
let denote_decl env evm d =
248+
let denote_decl env evm d =
249249
let (h, args) = app_full d [] in
250250
if constr_equall h tmkdecl then
251251
match args with
@@ -262,7 +262,7 @@ let denote_decl env evm d =
262262

263263
let denote_context env evm ctx =
264264
fold_env_evm_right denote_decl env evm (unquote_list ctx)
265-
265+
266266
let unquote_mutual_inductive_entry env evm trm (* of type mutual_inductive_entry *) : _ * _ * Entries.mutual_inductive_entry =
267267
let (h,args) = app_full trm [] in
268268
if constr_equall h tBuild_mutual_inductive_entry then
@@ -301,7 +301,7 @@ let declare_inductive (env: Environ.env) (evm: Evd.evar_map) (infer_univs : bool
301301
let evm' = Evd.from_env env in
302302
let evm', ctx, mind = unquote_mutual_inductive_entry env evm' mind in
303303
let () = DeclareUctx.declare_universe_context ~poly:false ctx in
304-
let evm, mind =
304+
let evm, mind =
305305
if infer_univs then
306306
let ctx, mind = Tm_util.RetypeMindEntry.infer_mentry_univs env evm' mind in
307307
debug (fun () -> Pp.(str "Declaring universe context " ++ Univ.pr_universe_context_set (Level.pr) ctx));

test-suite/issue1042.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
From MetaCoq.Utils Require Import utils.
2+
From MetaCoq.Template Require Import All.
3+
Import MCMonadNotation.
4+
5+
6+
(*Exemple with a typing error*)
7+
Definition ident_term (a : term) : term := a.
8+
9+
Definition quote_inductive {X}(inductive:X): TemplateMonad _ :=
10+
quote <- tmQuote inductive;;
11+
tmReturn quote.
12+
13+
Inductive tm : Set := .
14+
15+
Definition d1 : TemplateMonad unit.
16+
(* Set Debug "backtrace". *)
17+
Fail MetaCoq Run(
18+
quote <- quote_inductive tm;;
19+
constructor <- ident_term quote;;
20+
tmPrint constructor)
21+
.
22+
Fail run_template_program (quote <- quote_inductive tm;;
23+
constructor <- ident_term quote;;
24+
tmPrint constructor) ltac:(fun x => idtac).
25+
Fail refine (
26+
quote <- quote_inductive tm;;
27+
constructor <- ident_term quote;;
28+
tmPrint constructor).

0 commit comments

Comments
 (0)