Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve evar handling in tmUnquote/tmUnquoteTyped #1113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions template-coq/src/run_template_monad.ml
Original file line number Diff line number Diff line change
Expand Up @@ -538,17 +538,21 @@ let rec run_template_program_rec ~poly ?(intactic=false) (k : Constr.t Plugin_co
| TmUnquote t ->
begin
try
debug Pp.(fun () -> str"Unquoting:" ++ Printer.pr_constr_env env evm t);
let t = reduce_all env evm t in
let evm, t' = denote_term env evm t in
let typ = Retyping.get_type_of env evm (EConstr.of_constr t') in
(*debug Pp.(fun () -> str"Unquoting:" ++ Printer.pr_constr_env env evm t);*)
let evm, t' = denote_term env evm (reduce_all env evm t) in
(* Typecheck. *)
let evm, typ = Typing.type_of env evm (EConstr.of_constr t') in
let evm, typ = Evarsolve.refresh_universes (Some false) env evm typ in
(* Solve evars. *)
let evm = Typeclasses.resolve_typeclasses env evm in
let t' = Evarutil.nf_evars_universes evm t' in
(* Package the unquoted term with its type. *)
let make_typed_term typ term evm =
match Lazy.force texistT_typed_term with
| GlobRef.ConstructRef ctor ->
let (evm,c) = Evd.fresh_global (Global.env ()) evm (Lazy.force texistT_typed_term) in
let term = Constr.mkApp
(EConstr.to_constr evm c, [|EConstr.to_constr evm typ; t'|]) in
(EConstr.to_constr evm c, [|EConstr.to_constr ~abort_on_undefined_evars:false evm typ; t'|]) in
let evm, _ = Typing.type_of env evm (EConstr.of_constr term) in
(env, evm, term)
| _ -> CErrors.anomaly (str "texistT_typed_term does not refer to a constructor")
Expand All @@ -559,8 +563,11 @@ let rec run_template_program_rec ~poly ?(intactic=false) (k : Constr.t Plugin_co
end
| TmUnquoteTyped (typ, t) ->
let evm, t' = denote_term env evm (reduce_all env evm t) in
(* let t' = Typing.e_solve_evars env evdref (EConstr.of_constr t') in *)
(* Typecheck. *)
let evm = Typing.check env evm (EConstr.of_constr t') (EConstr.of_constr typ) in
(* Solve evars. *)
let evm = Typeclasses.resolve_typeclasses env evm in
let t' = Evarutil.nf_evars_universes evm t' in
k ~st env evm t'
| TmFreshName name ->
let name = reduce_all env evm name in
Expand Down
20 changes: 20 additions & 0 deletions test-suite/unquote_evars.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
From MetaCoq.Template Require Import All.
Import MCMonadNotation.

(* Unquoting evars. *)
MetaCoq Run (mlet t <- tmUnquote (tEvar fresh_evar_id []) ;; tmPrint t).
MetaCoq Run (mlet t <- tmUnquoteTyped nat (tEvar fresh_evar_id []) ;; tmPrint t).

(* Unquoting evars, with typeclass resolution. *)
Existing Class nat.
Existing Instance O.

MetaCoq Quote Definition quoted_nat := nat.
MetaCoq Run (
mlet t <- tmUnquote (tCast (tEvar fresh_evar_id []) Cast quoted_nat) ;;
tmEval cbv t
).
MetaCoq Run (
mlet t <- tmUnquoteTyped nat (tEvar fresh_evar_id []) ;;
tmEval cbv t
).
Loading