Debian Codegraph @

plugins/ltac/internals.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 Constr
12 open EConstr
13 open Tacexpr
14 open CErrors
15 open Util
16 open Tactypes
17 open Tactics
18 open Proofview.Notations
19
20 let assert_succeeds tac =
21 let open Proofview in
22 let exception Succeeded in
23 tclORELSE (tclONCE tac <*> tclZERO Succeeded) (function
24 | Succeeded, _ -> tclUNIT ()
25 | exn, info -> tclZERO ~info exn)
26
27 let mytclWithHoles tac with_evars c =
28 Proofview.Goal.enter begin fun gl ->
29 let env = Proofview.Goal.env gl in
30 let sigma = Proofview.Goal.sigma gl in
31 let sigma',c = Tactics.force_destruction_arg with_evars env sigma c in
32 Tacticals.tclWITHHOLES with_evars (tac with_evars (Some c)) sigma'
33 end
34
35 (**********************************************************************)
36 (* replace, discriminate, injection, simplify_eq *)
37 (* dependent rewrite *)
38
39 let with_delayed_uconstr ist c tac =
40 let flags = Pretyping.{
41 use_coercions = true;
42 use_typeclasses = NoUseTC;
43 solve_unification_constraints = true;
44 fail_evar = false;
45 expand_evars = true;
46 program_mode = false;
47 poly = PolyFlags.default;
48 undeclared_evars_rr = false;
49 unconstrained_sorts = false;
50 } in
51 let c = Tacinterp.type_uconstr ~flags ist c in
52 Tacticals.tclDELAYEDWITHHOLES false c tac
53
54 let replace_in_clause_maybe_by ist dir_opt c1 c2 cl tac =
55 with_delayed_uconstr ist c1
56 (fun c1 -> Equality.replace_in_clause_maybe_by dir_opt c1 c2 cl (Option.map (Tacinterp.tactic_of_value ist) tac))
57
58 let replace_term ist dir_opt c cl =
59 with_delayed_uconstr ist c (fun c -> Equality.replace_term dir_opt c cl)
60
61 let elimOnConstrWithHoles tac with_evars c =
62 Tacticals.tclDELAYEDWITHHOLES with_evars c
63 (fun c -> tac with_evars (Some (None,ElimOnConstr c)))
64
65 let discr_main c = elimOnConstrWithHoles Equality.discr_tac false c
66
67 let discrHyp id =
68 Proofview.tclEVARMAP >>= fun sigma ->
69 discr_main (fun env sigma -> (sigma, (EConstr.mkVar id, NoBindings)))
70
71 let injection_main with_evars c =
72 elimOnConstrWithHoles (Equality.injClause None None) with_evars c
73
74 let injHyp id =
75 Proofview.tclEVARMAP >>= fun sigma ->
76 injection_main false (fun env sigma -> (sigma, (EConstr.mkVar id, NoBindings)))
77
78 let constr_flags () = Pretyping.{
79 use_coercions = true;
80 use_typeclasses = UseTC;
81 solve_unification_constraints = Proof.use_unification_heuristics ();
82 fail_evar = false;
83 expand_evars = true;
84 program_mode = false;
85 poly = PolyFlags.default;
86 undeclared_evars_rr = false;
87 unconstrained_sorts = false;
88 }
89
90 let refine_tac ist ~simple ~with_classes c =
91 let with_classes = if with_classes then Pretyping.UseTC else Pretyping.NoUseTC in
92 Proofview.Goal.enter begin fun gl ->
93 let concl = Proofview.Goal.concl gl in
94 let env = Proofview.Goal.env gl in
95 let flags =
96 { (constr_flags ()) with Pretyping.use_typeclasses = with_classes } in
97 let expected_type = Pretyping.OfType concl in
98 let c = Tacinterp.type_uconstr ~flags ~expected_type ist c in
99 let update = begin fun sigma ->
100 c env sigma
101 end in
102 let refine = Refine.refine ~typecheck:false update in
103 if simple then refine
104 else refine <*>
105 Tactics.reduce_after_refine <*>
106 Proofview.shelve_unifiable
107 end
108
109 (** Term introspection *)
110
111 let is_evar x =
112 Proofview.tclEVARMAP >>= fun sigma ->
113 match EConstr.kind sigma x with
114 | Evar _ -> Proofview.tclUNIT ()
115 | _ -> Tacticals.tclFAIL (Pp.str "Not an evar")
116
117 let has_evar x =
118 Proofview.tclEVARMAP >>= fun sigma ->
119 if Evarutil.has_undefined_evars sigma x
120 then Proofview.tclUNIT ()
121 else Tacticals.tclFAIL (Pp.str "No evars")
122
123 let is_var x =
124 Proofview.tclEVARMAP >>= fun sigma ->
125 match EConstr.kind sigma x with
126 | Var _ -> Proofview.tclUNIT ()
127 | _ -> Tacticals.tclFAIL (Pp.str "Not a variable or hypothesis")
128
129 let is_fix x =
130 Proofview.tclEVARMAP >>= fun sigma ->
131 match EConstr.kind sigma x with
132 | Fix _ -> Proofview.tclUNIT ()
133 | _ -> Tacticals.tclFAIL (Pp.str "not a fix definition")
134
135 let is_cofix x =
136 Proofview.tclEVARMAP >>= fun sigma ->
137 match EConstr.kind sigma x with
138 | CoFix _ -> Proofview.tclUNIT ()
139 | _ -> Tacticals.tclFAIL (Pp.str "not a cofix definition")
140
141 let is_ind x =
142 Proofview.tclEVARMAP >>= fun sigma ->
143 match EConstr.kind sigma x with
144 | Ind _ -> Proofview.tclUNIT ()
145 | _ -> Tacticals.tclFAIL (Pp.str "not an (co)inductive datatype")
146
147 let is_constructor x =
148 Proofview.tclEVARMAP >>= fun sigma ->
149 match EConstr.kind sigma x with
150 | Construct _ -> Proofview.tclUNIT ()
151 | _ -> Tacticals.tclFAIL (Pp.str "not a constructor")
152
153 let is_proj x =
154 Proofview.tclEVARMAP >>= fun sigma ->
155 match EConstr.kind sigma x with
156 | Proj _ -> Proofview.tclUNIT ()
157 | _ -> Tacticals.tclFAIL (Pp.str "not a primitive projection")
158
159 let is_const x =
160 Proofview.tclEVARMAP >>= fun sigma ->
161 match EConstr.kind sigma x with
162 | Const _ -> Proofview.tclUNIT ()
163 | _ -> Tacticals.tclFAIL (Pp.str "not a constant")
164
165 let unshelve ist t =
166 Proofview.with_shelf (Tacinterp.tactic_of_value ist t) >>= fun (gls, ()) ->
167 let gls = List.map Proofview.with_empty_state gls in
168 Proofview.Unsafe.tclGETGOALS >>= fun ogls ->
169 Proofview.Unsafe.tclSETGOALS (gls @ ogls)
170
171 (** tactic analogous to "OPTIMIZE HEAP" *)
172
173 let tclOPTIMIZE_HEAP =
174 Proofview.tclLIFT (Proofview.NonLogical.make (fun () -> Gc.compact ()))
175
176 let onSomeWithHoles tac = function
177 | None -> tac None
178 | Some c -> Tacticals.tclDELAYEDWITHHOLES false c (fun c -> tac (Some c))
179
180 let decompose l c =
181 Proofview.Goal.enter begin fun gl ->
182 let sigma = Proofview.Goal.sigma gl in
183 let to_ind c =
184 if isInd sigma c then fst (destInd sigma c)
185 else user_err Pp.(str "not an inductive type")
186 in
187 let l = List.map to_ind l in
188 Elim.h_decompose l c
189 end
190
191 let exact ist (c : Ltac_pretype.closed_glob_constr) =
192 Proofview.Goal.enter begin fun gl ->
193 let env = Proofview.Goal.env gl in
194 let sigma = Proofview.Goal.sigma gl in
195 let concl = Proofview.Goal.concl gl in
196 let expected_type = Pretyping.OfType concl in
197 let sigma, c = Tacinterp.type_uconstr ~expected_type ist c env sigma in
198 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (Tactics.exact_no_check c)
199 end
200
201 (** ProofGeneral specific command *)
202
203 (* Execute tac, show the names of new hypothesis names created by tac
204 in the "as" format and then forget everything. From the logical
205 point of view [infoH tac] is therefore equivalent to idtac,
206 except that it takes the time and memory of tac and prints "as"
207 information. The resulting (unchanged) goals are printed *after*
208 the as-expression, which forces pg to some gymnastic.
209 TODO: Have something similar (better?) in the xml protocol.
210 NOTE: some tactics delete hypothesis and reuse names (induction,
211 destruct), this is not detected by this tactical. *)
212 let infoH ~pstate (tac : raw_tactic_expr) : unit =
213 let oldhyps =
214 try snd @@ Declare.Proof.get_goal_context pstate 1
215 with Proof.NoSuchGoal _ -> Global.env ()
216 in
217 let oldhyps = List.map Context.Named.Declaration.get_id @@ Environ.named_context oldhyps in
218 let tac = Tacinterp.interp tac in
219 let tac =
220 let open Proofview.Notations in
221 tac <*>
222 Proofview.Unsafe.tclGETGOALS >>= fun gls ->
223 Proofview.tclEVARMAP >>= fun sigma ->
224 let map gl =
225 let gl = Proofview_monad.drop_state gl in
226 let hyps = Evd.evar_filtered_context (Evd.find_undefined sigma gl) in
227 List.map Context.Named.Declaration.get_id @@ hyps
228 in
229 let hyps = List.map map gls in
230 let newhyps = List.map (fun hypl -> List.subtract Names.Id.equal hypl oldhyps) hyps in
231 let s =
232 let frst = ref true in
233 List.fold_left
234 (fun acc lh -> acc ^ (if !frst then (frst:=false;"") else " | ")
235 ^ (List.fold_left
236 (fun acc d -> (Names.Id.to_string d) ^ " " ^ acc)
237 "" lh))
238 "" newhyps in
239 let () = Feedback.msg_notice
240 Pp.(str "<infoH>"
241 ++ (hov 0 (str s))
242 ++ (str "</infoH>")) in
243 Proofview.tclUNIT ()
244 in
245 ignore (Declare.Proof.by (Global.env ()) tac pstate)
246
247 let declare_equivalent_keys c c' =
248 let get_key c =
249 let env = Global.env () in
250 let evd = Evd.from_env env in
251 let (evd, c) = Constrintern.interp_open_constr env evd c in
252 let kind c = EConstr.kind evd c in
253 Keys.constr_key env kind c
254 in
255 let k1 = get_key c in
256 let k2 = get_key c' in
257 match k1, k2 with
258 | Some k1, Some k2 -> Keys.declare_equiv_keys k1 k2
259 | _ -> ()