Debian Codegraph @

tactics/evar_tactics.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Util
12 open Names
13 open Constr
14 open CErrors
15 open Evd
16 open Evarsolve
17 open Locus
18 open Context.Named.Declaration
19 open Pp
20 open Ltac_pretype
21
22 module NamedDecl = Context.Named.Declaration
23
24 (******************************************)
25 (* Instantiation of existential variables *)
26 (******************************************)
27
28 let define_and_solve_constraints evk c env evd =
29 if Termops.occur_evar evd evk c then
30 Pretype_errors.error_occur_check env evd evk c;
31 let evd = define evk c evd in
32 let (evd,pbs) = extract_changed_conv_pbs_from evd (Some (Evar.Set.singleton evk)) in
33 match
34 List.fold_left
35 (fun p (pbty,env,t1,t2) -> match p with
36 | Success evd -> Evarconv.evar_conv_x (Evarconv.default_flags_of TransparentState.full) env evd pbty t1 t2
37 | UnifFailure _ as x -> x) (Success evd)
38 pbs
39 with
40 | Success evd -> evd
41 | UnifFailure _ -> user_err Pp.(str "Instance does not satisfy the constraints.")
42
43 let w_refine evk rawc env sigma =
44 let evi =
45 try Evd.find_undefined sigma evk
46 with Not_found ->
47 let () = assert (Evd.mem sigma evk) in
48 user_err Pp.(str "Instantiate called on already-defined evar")
49 in
50 let env = Evd.evar_filtered_env env evi in
51 let sigma',typed_c =
52 let flags = Pretyping.{
53 use_coercions = true;
54 use_typeclasses = UseTC;
55 solve_unification_constraints = true;
56 fail_evar = false;
57 expand_evars = true;
58 program_mode = false;
59 poly = PolyFlags.default;
60 undeclared_evars_rr = false;
61 unconstrained_sorts = false;
62 } in
63 let expected_type = Pretyping.OfType (Evd.evar_concl evi) in
64 try Pretyping.understand_uconstr ~flags ~expected_type env sigma rawc
65 with e when CErrors.noncritical e ->
66 let loc = Glob_ops.loc_of_glob_constr rawc.term in
67 user_err ?loc
68 (str "Instance is not well-typed in the environment of " ++
69 Termops.pr_existential_key env sigma evk ++ str ".")
70 in
71 define_and_solve_constraints evk typed_c env sigma'
72
73 (* The instantiate tactic *)
74
75 let instantiate_evar evk rawc =
76 let open Proofview.Notations in
77 Proofview.tclENV >>= fun env ->
78 Proofview.Goal.enter begin fun gl ->
79 let sigma = Proofview.Goal.sigma gl in
80 let sigma' = w_refine evk rawc env sigma in
81 Proofview.Unsafe.tclEVARS sigma'
82 end
83
84 let evar_list sigma c =
85 let rec evrec acc c =
86 match EConstr.kind sigma c with
87 | Evar (evk, _ as ev) -> ev :: acc
88 | _ -> EConstr.fold sigma evrec acc c in
89 evrec [] c
90
91 let instantiate_tac n c ido =
92 Proofview.Goal.enter begin fun gl ->
93 let sigma = Proofview.Goal.sigma gl in
94 let env = Proofview.Goal.env gl in
95 let concl = Proofview.Goal.concl gl in
96 let evl =
97 match ido with
98 | None -> evar_list sigma concl
99 | Some (id, hloc) ->
100 let decl = Environ.lookup_named id env in
101 match hloc with
102 InHyp ->
103 (match decl with
104 | LocalAssum (_,typ) -> evar_list sigma (EConstr.of_constr typ)
105 | _ -> user_err Pp.(str "Please be more specific: in type or value?"))
106 | InHypTypeOnly ->
107 evar_list sigma (EConstr.of_constr (NamedDecl.get_type decl))
108 | InHypValueOnly ->
109 (match decl with
110 | LocalDef (_,body,_) -> evar_list sigma (EConstr.of_constr body)
111 | _ -> user_err Pp.(str "Not a defined hypothesis.")) in
112 if List.length evl < n then
113 user_err Pp.(str "Not enough uninstantiated existential variables.");
114 if n <= 0 then user_err Pp.(str "Incorrect existential variable index.");
115 let evk,_ = List.nth evl (n-1) in
116 instantiate_evar evk c
117 end
118
119 let instantiate_tac_by_name id c =
120 Proofview.Goal.enter begin fun gl ->
121 let sigma = Proofview.Goal.sigma gl in
122 let id = Libnames.make_qualid DirPath.empty id in
123 let evk =
124 try Evd.evar_key id sigma
125 with Not_found -> user_err Pp.(str "Unknown existential variable.") in
126 instantiate_evar evk c
127 end
128
129 let let_evar name typ =
130 let src = (Loc.tag Evar_kinds.GoalEvar) in
131 Proofview.Goal.enter begin fun gl ->
132 let sigma = Proofview.Goal.sigma gl in
133 let env = Proofview.Goal.env gl in
134 let sigma, _ = Typing.sort_of env sigma typ in
135 let id = match name with
136 | Name.Anonymous ->
137 let id = Namegen.id_of_name_using_hdchar env sigma typ name in
138 Namegen.next_ident_away_in_goal env id (Termops.vars_of_env env)
139 | Name.Name id -> id
140 in
141 let typeclass_candidate = Typeclasses.is_maybe_class_type env sigma typ in
142 let (sigma, evar) = Evarutil.new_evar ~typeclass_candidate env sigma ~src ~naming:(Namegen.IntroFresh id) typ in
143 Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS sigma)
144 (Tactics.pose_tac (Name.Name id) evar)
145 end