Skip to content

Commit d19cfb9

Browse files
committed
Merge pull request #1113 from MathisBD/tm-unquote-evars
Improve evar handling in tmUnquote/tmUnquoteTyped
1 parent 1d9755a commit d19cfb9

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
@@ -538,17 +538,21 @@ let rec run_template_program_rec ~poly ?(intactic=false) (k : Constr.t Plugin_co
538538
| TmUnquote t ->
539539
begin
540540
try
541-
debug Pp.(fun () -> str"Unquoting:" ++ Printer.pr_constr_env env evm t);
542-
let t = reduce_all env evm t in
543-
let evm, t' = denote_term env evm t in
544-
let typ = Retyping.get_type_of env evm (EConstr.of_constr t') in
541+
(*debug Pp.(fun () -> str"Unquoting:" ++ Printer.pr_constr_env env evm t);*)
542+
let evm, t' = denote_term env evm (reduce_all env evm t) in
543+
(* Typecheck. *)
544+
let evm, typ = Typing.type_of env evm (EConstr.of_constr t') in
545545
let evm, typ = Evarsolve.refresh_universes (Some false) env evm typ in
546+
(* Solve evars. *)
547+
let evm = Typeclasses.resolve_typeclasses env evm in
548+
let t' = Evarutil.nf_evars_universes evm t' in
549+
(* Package the unquoted term with its type. *)
546550
let make_typed_term typ term evm =
547551
match Lazy.force texistT_typed_term with
548552
| GlobRef.ConstructRef ctor ->
549553
let (evm,c) = Evd.fresh_global (Global.env ()) evm (Lazy.force texistT_typed_term) in
550554
let term = Constr.mkApp
551-
(EConstr.to_constr evm c, [|EConstr.to_constr evm typ; t'|]) in
555+
(EConstr.to_constr evm c, [|EConstr.to_constr ~abort_on_undefined_evars:false evm typ; t'|]) in
552556
let evm, _ = Typing.type_of env evm (EConstr.of_constr term) in
553557
(env, evm, term)
554558
| _ -> CErrors.anomaly (str "texistT_typed_term does not refer to a constructor")
@@ -559,8 +563,11 @@ let rec run_template_program_rec ~poly ?(intactic=false) (k : Constr.t Plugin_co
559563
end
560564
| TmUnquoteTyped (typ, t) ->
561565
let evm, t' = denote_term env evm (reduce_all env evm t) in
562-
(* let t' = Typing.e_solve_evars env evdref (EConstr.of_constr t') in *)
566+
(* Typecheck. *)
563567
let evm = Typing.check env evm (EConstr.of_constr t') (EConstr.of_constr typ) in
568+
(* Solve evars. *)
569+
let evm = Typeclasses.resolve_typeclasses env evm in
570+
let t' = Evarutil.nf_evars_universes evm t' in
564571
k ~st env evm t'
565572
| TmFreshName name ->
566573
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)