Debian Codegraph @

plugins/ltac/tauto.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 Hipattern
14 open Names
15 open Geninterp
16 open Ltac_plugin
17 open Tacexpr
18 open Tacinterp
19 open Util
20 open Tacticals
21 open Proofview.Notations
22
23 module NamedDecl = Context.Named.Declaration
24
25 let tauto_plugin = "rocq-runtime.plugins.tauto"
26
27 let assoc_var s ist =
28 let v = Id.Map.find (Names.Id.of_string s) ist.lfun in
29 match Value.to_constr v with
30 | Some c -> c
31 | None -> failwith "tauto: anomaly"
32
33 (** Parametrization of tauto *)
34
35 type tauto_flags = {
36
37 (* Whether conjunction and disjunction are restricted to binary connectives *)
38 binary_mode : bool;
39
40 (* Whether conjunction and disjunction are restricted to the connectives *)
41 (* having the structure of "and" and "or" (up to the choice of sorts) in *)
42 (* contravariant position in an hypothesis *)
43 strict_in_contravariant_hyp : bool;
44
45 (* Whether conjunction and disjunction are restricted to the connectives *)
46 (* having the structure of "and" and "or" (up to the choice of sorts) in *)
47 (* an hypothesis and in the conclusion *)
48 strict_in_hyp_and_ccl : bool;
49
50 (* Whether unit type includes equality types *)
51 }
52
53 let tag_tauto_flags : tauto_flags Val.typ = Val.create "tauto_flags"
54
55 let assoc_flags ist : tauto_flags =
56 let Val.Dyn (tag, v) = Id.Map.find (Names.Id.of_string "tauto_flags") ist.lfun in
57 match Val.eq tag tag_tauto_flags with
58 | None -> assert false
59 | Some Refl -> v
60
61 (* Whether inner not are unfolded *)
62 let negation_unfolding = ref true
63
64 open Goptions
65 let () =
66 declare_bool_option
67 { optstage = Summary.Stage.Interp;
68 optdepr = None;
69 optkey = ["Intuition";"Negation";"Unfolding"];
70 optread = (fun () -> !negation_unfolding);
71 optwrite = (:=) negation_unfolding }
72
73 (** Base tactics *)
74
75 let idtac = Proofview.tclUNIT ()
76 let fail = Proofview.tclINDEPENDENT (tclFAIL (Pp.mt ()))
77
78 let intro = Tactics.intro
79
80 let assert_ ?by c =
81 let tac = match by with
82 | None -> None
83 | Some tac -> Some (Some tac)
84 in
85 Proofview.tclINDEPENDENT (Tactics.forward true tac None c)
86
87 let apply c = Tactics.apply c
88
89 let clear id = Tactics.clear [id]
90
91 let assumption = Tactics.assumption
92
93 let split = Tactics.split_with_bindings false [Tactypes.NoBindings]
94
95 (** Test *)
96
97 let is_empty _ ist =
98 Proofview.tclENV >>= fun genv ->
99 Proofview.tclEVARMAP >>= fun sigma ->
100 if is_empty_type genv sigma (assoc_var "X1" ist) then idtac else fail
101
102 (* Strictly speaking, this exceeds the propositional fragment as it
103 matches also equality types (and solves them if a reflexivity) *)
104 let is_unit_or_eq _ ist =
105 Proofview.tclENV >>= fun genv ->
106 Proofview.tclEVARMAP >>= fun sigma ->
107 if is_unit_or_eq_type genv sigma (assoc_var "X1" ist) then idtac else fail
108
109 (** Dealing with conjunction *)
110
111 let is_conj _ ist =
112 Proofview.tclENV >>= fun genv ->
113 Proofview.tclEVARMAP >>= fun sigma ->
114 let flags = assoc_flags ist in
115 let ind = assoc_var "X1" ist in
116 if is_conjunction genv sigma
117 ~strict:flags.strict_in_hyp_and_ccl
118 ~onlybinary:flags.binary_mode ind
119 then idtac
120 else fail
121
122 let flatten_contravariant_conj _ ist =
123 Proofview.tclENV >>= fun genv ->
124 Proofview.tclEVARMAP >>= fun sigma ->
125 let flags = assoc_flags ist in
126 let typ = assoc_var "X1" ist in
127 let c = assoc_var "X2" ist in
128 let hyp = assoc_var "id" ist in
129 match match_with_conjunction genv sigma
130 ~strict:flags.strict_in_contravariant_hyp
131 ~onlybinary:flags.binary_mode typ
132 with
133 | Some (_,args) ->
134 let newtyp = List.fold_right (fun a b -> mkArrow a ERelevance.relevant b) args c in
135 let intros = tclMAP (fun _ -> intro) args in
136 let by = tclTHENLIST [intros; apply hyp; split; assumption] in
137 tclTHENLIST [assert_ ~by newtyp; clear (destVar sigma hyp)]
138 | _ -> fail
139
140 (** Dealing with disjunction *)
141
142 let is_disj _ ist =
143 Proofview.tclENV >>= fun genv ->
144 Proofview.tclEVARMAP >>= fun sigma ->
145 let flags = assoc_flags ist in
146 let t = assoc_var "X1" ist in
147 if is_disjunction genv sigma
148 ~strict:flags.strict_in_hyp_and_ccl
149 ~onlybinary:flags.binary_mode t
150 then idtac
151 else fail
152
153 let flatten_contravariant_disj _ ist =
154 Proofview.tclENV >>= fun genv ->
155 Proofview.tclEVARMAP >>= fun sigma ->
156 let flags = assoc_flags ist in
157 let typ = assoc_var "X1" ist in
158 let c = assoc_var "X2" ist in
159 let hyp = assoc_var "id" ist in
160 match match_with_disjunction genv sigma
161 ~strict:flags.strict_in_contravariant_hyp
162 ~onlybinary:flags.binary_mode
163 typ with
164 | Some (_,args) ->
165 let map i arg =
166 let typ = mkArrow arg ERelevance.relevant c in
167 let ci = Tactics.constructor_tac false None (succ i) Tactypes.NoBindings in
168 let by = tclTHENLIST [intro; apply hyp; ci; assumption] in
169 assert_ ~by typ
170 in
171 let tacs = List.mapi map args in
172 let tac0 = clear (destVar sigma hyp) in
173 tclTHEN (tclTHENLIST tacs) tac0
174 | _ -> fail
175
176 let evalglobref_of_globref =
177 function
178 | GlobRef.VarRef v -> Evaluable.EvalVarRef v
179 | GlobRef.ConstRef c -> Evaluable.EvalConstRef c
180 | GlobRef.IndRef _ | GlobRef.ConstructRef _ -> assert false
181
182 let make_unfold name =
183 let const = evalglobref_of_globref (Rocqlib.lib_ref name) in
184 Locus.(AllOccurrences, ArgArg (const, None))
185
186 let reduction_not_iff _ ist =
187 let make_reduce c = CAst.make @@ TacAtom (TacReduce (Genredexpr.Unfold c, Locusops.allHypsAndConcl)) in
188 let tac = match !negation_unfolding with
189 | true -> make_reduce [make_unfold "core.not.type"]
190 | false -> CAst.make (TacId [])
191 in
192 eval_tactic_ist ist tac
193
194 let apply_nnpp _ ist =
195 let nnpp = "core.nnpp.type" in
196 Proofview.tclBIND
197 (Proofview.tclUNIT ())
198 begin fun () ->
199 if Rocqlib.has_ref nnpp
200 then Tacticals.pf_constr_of_global (Rocqlib.lib_ref nnpp) >>= apply
201 else tclFAIL (Pp.mt ())
202 end
203
204 (* This is the uniform mode dealing with ->, not, iff and types isomorphic to
205 /\ and *, \/ and +, False and Empty_set, True and unit, _and_ eq-like types.
206 For the moment not and iff are still always unfolded. *)
207 let tauto_uniform_unit_flags = {
208 binary_mode = true;
209 strict_in_contravariant_hyp = true;
210 strict_in_hyp_and_ccl = true;
211 }
212
213 (* This is the improved mode *)
214 let tauto_power_flags = {
215 binary_mode = false; (* support n-ary connectives *)
216 strict_in_contravariant_hyp = false; (* supports non-regular connectives *)
217 strict_in_hyp_and_ccl = false;
218 }
219
220 let with_flags flags _ ist =
221 let f = CAst.make @@ Id.of_string "f" in
222 let x = CAst.make @@ Id.of_string "x" in
223 let arg = Val.Dyn (tag_tauto_flags, flags) in
224 let ist = { ist with lfun = Id.Map.add x.CAst.v arg ist.lfun } in
225 eval_tactic_ist ist (CAst.make @@ TacArg (TacCall (CAst.make (Locus.ArgVar f, [Reference (Locus.ArgVar x)]))))
226
227 let warn_auto_with_star = CWarnings.create ~name:"intuition-auto-with-star" ~category:Deprecation.Version.v8_17
228 Pp.(fun () ->
229 str "\"auto with *\" was used through the default \"intuition_solver\" tactic."
230 ++ spc() ++ str "This will be replaced by just \"auto\" in the future.")
231
232 let warn_auto_with_star_tac _ _ =
233 Proofview.tclBIND
234 (Proofview.tclUNIT ())
235 begin fun () ->
236 warn_auto_with_star ();
237 Proofview.tclUNIT()
238 end
239
240 let val_of_id id =
241 let open Geninterp in
242 let id = CAst.make @@ Tactypes.IntroNaming (IntroIdentifier id) in
243 Val.inject (val_tag @@ Genarg.topwit Tacarg.wit_intropattern) id
244
245 let find_cut _ ist =
246 let k = Id.Map.find (Names.Id.of_string "k") ist.lfun in
247 Proofview.Goal.enter begin fun gl ->
248 let sigma = Proofview.Goal.sigma gl in
249 let hyps0 = Proofview.Goal.hyps gl in
250 (* Beware of the relative order of hypothesis picking! *)
251 let rec find_arg = function
252 | [] -> Tacticals.tclFAIL (Pp.str ("No matching clause"))
253 | arg :: hyps ->
254 let typ = NamedDecl.get_type arg in
255 let arg = NamedDecl.get_id arg in
256 let rec find_fun = function
257 | [] -> Tacticals.tclFAIL (Pp.str ("No matching clause"))
258 | fnc :: hyps ->
259 match EConstr.kind sigma (NamedDecl.get_type fnc) with
260 | Prod (na, dom, codom) when EConstr.Vars.noccurn sigma 1 codom ->
261 let f = NamedDecl.get_id fnc in
262 if Id.equal f arg then find_fun hyps
263 else
264 Proofview.tclOR
265 (Proofview.tclUNIT () >>= fun () -> find_fun hyps)
266 (fun _ -> Tactics.convert dom typ <*> Proofview.tclUNIT (f, arg, codom))
267 | _ -> find_fun hyps
268 in
269 Proofview.tclOR (Proofview.tclUNIT () >>= fun () -> find_arg hyps) (fun _ -> find_fun hyps0)
270 in
271 let tac =
272 find_arg hyps0 >>= fun (f, arg, t) ->
273 Tacinterp.Value.apply k [val_of_id f; val_of_id arg; Value.of_constr t]
274 in
275 Proofview.tclONCE tac
276 end
277
278 let register_tauto_tactic tac name0 args =
279 let ids = List.map (fun id -> Id.of_string id) args in
280 let ids = List.map (fun id -> Name id) ids in
281 let name = { mltac_plugin = tauto_plugin; mltac_tactic = name0; } in
282 let entry = { mltac_name = name; mltac_index = 0 } in
283 let () = Tacenv.register_ml_tactic name [| tac |] in
284 let tac = CAst.make (TacFun (ids, CAst.make (TacML (entry, [])))) in
285 let obj () = Tacenv.register_ltac true true (Id.of_string name0) tac in
286 Mltop.(declare_cache_obj_full (interp_only_obj obj) tauto_plugin)
287
288 let () = register_tauto_tactic is_empty "is_empty" ["tauto_flags"; "X1"]
289 let () = register_tauto_tactic is_unit_or_eq "is_unit_or_eq" ["tauto_flags"; "X1"]
290 let () = register_tauto_tactic is_disj "is_disj" ["tauto_flags"; "X1"]
291 let () = register_tauto_tactic is_conj "is_conj" ["tauto_flags"; "X1"]
292 let () = register_tauto_tactic flatten_contravariant_disj "flatten_contravariant_disj" ["tauto_flags"; "X1"; "X2"; "id"]
293 let () = register_tauto_tactic flatten_contravariant_conj "flatten_contravariant_conj" ["tauto_flags"; "X1"; "X2"; "id"]
294 let () = register_tauto_tactic apply_nnpp "apply_nnpp" []
295 let () = register_tauto_tactic reduction_not_iff "reduction_not_iff" []
296 let () = register_tauto_tactic (with_flags tauto_uniform_unit_flags) "with_uniform_flags" ["f"]
297 let () = register_tauto_tactic (with_flags tauto_power_flags) "with_power_flags" ["f"]
298 let () = register_tauto_tactic warn_auto_with_star_tac "warn_auto_with_star" []
299 let () = register_tauto_tactic find_cut "find_cut" ["k"]