Skip to content

Commit 7033a53

Browse files
authored
Merge pull request #1113 from MathisBD/tm-unquote-evars
Improve evar handling in tmUnquote/tmUnquoteTyped
2 parents 58718b5 + 4fa8ed5 commit 7033a53

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

template-coq/src/run_template_monad.ml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,17 +544,21 @@ let rec run_template_program_rec ~poly ?(intactic=false) (k : Constr.t Plugin_co
544544
| TmUnquote t ->
545545
begin
546546
try
547-
debug Pp.(fun () -> str"Unquoting:" ++ Printer.pr_constr_env env evm t);
548-
let t = reduce_all env evm t in
549-
let evm, t' = denote_term env evm t in
550-
let typ = Retyping.get_type_of env evm (EConstr.of_constr t') in
547+
(*debug Pp.(fun () -> str"Unquoting:" ++ Printer.pr_constr_env env evm t);*)
548+
let evm, t' = denote_term env evm (reduce_all env evm t) in
549+
(* Typecheck. *)
550+
let evm, typ = Typing.type_of env evm (EConstr.of_constr t') in
551551
let evm, typ = Evarsolve.refresh_universes (Some false) env evm typ in
552+
(* Solve evars. *)
553+
let evm = Typeclasses.resolve_typeclasses env evm in
554+
let t' = Evarutil.nf_evars_universes evm t' in
555+
(* Package the unquoted term with its type. *)
552556
let make_typed_term typ term evm =
553557
match Lazy.force texistT_typed_term with
554558
| GlobRef.ConstructRef ctor ->
555559
let (evm,c) = Evd.fresh_global (Global.env ()) evm (Lazy.force texistT_typed_term) in
556560
let term = Constr.mkApp
557-
(EConstr.to_constr evm c, [|EConstr.to_constr evm typ; t'|]) in
561+
(EConstr.to_constr evm c, [|EConstr.to_constr ~abort_on_undefined_evars:false evm typ; t'|]) in
558562
let evm, _ = Typing.type_of env evm (EConstr.of_constr term) in
559563
(env, evm, term)
560564
| _ -> CErrors.anomaly (str "texistT_typed_term does not refer to a constructor")
@@ -565,8 +569,11 @@ let rec run_template_program_rec ~poly ?(intactic=false) (k : Constr.t Plugin_co
565569
end
566570
| TmUnquoteTyped (typ, t) ->
567571
let evm, t' = denote_term env evm (reduce_all env evm t) in
568-
(* let t' = Typing.e_solve_evars env evdref (EConstr.of_constr t') in *)
572+
(* Typecheck. *)
569573
let evm = Typing.check env evm (EConstr.of_constr t') (EConstr.of_constr typ) in
574+
(* Solve evars. *)
575+
let evm = Typeclasses.resolve_typeclasses env evm in
576+
let t' = Evarutil.nf_evars_universes evm t' in
570577
k ~st env evm t'
571578
| TmFreshName name ->
572579
let name = reduce_all env evm name in

test-suite/unquote_evars.v

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
From MetaCoq.Template Require Import All.
2+
Import MCMonadNotation.
3+
4+
(* Unquoting evars. *)
5+
MetaCoq Run (mlet t <- tmUnquote (tEvar fresh_evar_id []) ;; tmPrint t).
6+
MetaCoq Run (mlet t <- tmUnquoteTyped nat (tEvar fresh_evar_id []) ;; tmPrint t).
7+
8+
(* Unquoting evars, with typeclass resolution. *)
9+
Existing Class nat.
10+
Existing Instance O.
11+
12+
MetaCoq Quote Definition quoted_nat := nat.
13+
MetaCoq Run (
14+
mlet t <- tmUnquote (tCast (tEvar fresh_evar_id []) Cast quoted_nat) ;;
15+
tmEval cbv t
16+
).
17+
MetaCoq Run (
18+
mlet t <- tmUnquoteTyped nat (tEvar fresh_evar_id []) ;;
19+
tmEval cbv t
20+
).

0 commit comments

Comments
 (0)