Debian Codegraph @

plugins/firstorder/rules.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 CErrors
12 open Util
13 open Names
14 open EConstr
15 open Vars
16 open Tactics
17 open Tacticals
18 open Proofview.Notations
19 open Termops
20 open Formula
21 open Sequent
22
23 module NamedDecl = Context.Named.Declaration
24
25 type tactic = unit Proofview.tactic
26
27 type seqtac= (Sequent.t -> tactic) -> Sequent.t -> tactic
28
29 type lseqtac= GlobRef.t -> seqtac
30
31 type 'a with_backtracking = tactic -> 'a
32
33 let wrap ~flags n b continue seq =
34 Proofview.Goal.enter begin fun gls ->
35 Control.check_for_interrupt ();
36 let nc = Proofview.Goal.hyps gls in
37 let env = Proofview.Goal.env gls in
38 let sigma = Proofview.Goal.sigma gls in
39 let concl = Proofview.Goal.concl gls in
40 let rec aux i nc ctx=
41 if i<=0 then seq else
42 match nc with
43 []->anomaly (Pp.str "Not the expected number of hyps.")
44 | nd::q->
45 let id = NamedDecl.get_id nd in
46 if occur_var env sigma id concl ||
47 List.exists (occur_var_in_decl env sigma id) ctx then
48 (aux (i-1) q (nd::ctx))
49 else
50 add_formula ~flags ~hint:false env sigma (GlobRef.VarRef id) (NamedDecl.get_type nd) (aux (i-1) q (nd::ctx)) in
51 let seq1=aux n nc [] in
52 let seq2 =
53 if b then add_concl ~flags env sigma concl seq1 else seq1
54 in
55 continue seq2
56 end
57
58 let clear_global=function
59 | GlobRef.VarRef id-> clear [id]
60 | _->tclIDTAC
61
62 (* connection rules *)
63
64 let find_hyp ~flags seq env sigma typ =
65 let ts = RedFlags.red_transparent flags.reds in
66 let rec find conv = function
67 | [] -> None
68 | decl :: rest ->
69 let id = NamedDecl.get_id decl in
70 let t = NamedDecl.get_type decl in
71 if not (Sequent.mem_hyp id seq) then find conv rest
72 else if conv then match Reductionops.infer_conv ~ts ~pb:CUMUL env sigma t typ with
73 | None -> find conv rest
74 | Some sigma -> Some (sigma, id)
75 else
76 if EConstr.eq_constr sigma t typ then Some (sigma, id)
77 else find conv rest
78 in
79 let hyps = EConstr.named_context env in
80 match find false hyps with
81 | Some ans -> Some ans
82 | None -> find true hyps
83
84 let axiom_tac ~flags seq =
85 Proofview.Goal.enter begin fun gl ->
86 let env = Proofview.Goal.env gl in
87 let sigma = Proofview.Goal.sigma gl in
88 let concl = Proofview.Goal.concl gl in
89 match find_hyp ~flags seq env sigma concl with
90 | Some (sigma, hyp) ->
91 Proofview.Unsafe.tclEVARS sigma <*>
92 exact_no_check (mkVar hyp)
93 | None -> tclFAIL (Pp.str "No axiom link")
94 end
95
96 let ll_atom_tac ~flags backtrack id continue seq =
97 let open EConstr in
98 let tac =
99 Proofview.Goal.enter begin fun gl ->
100 let env = Proofview.Goal.env gl in
101 let sigma = Proofview.Goal.sigma gl in
102 let (sigma, id) = Evd.fresh_global env sigma id in
103 let typ = Retyping.get_type_of env sigma id in
104 let typ = Reductionops.clos_whd_flags flags.reds env sigma typ in
105 let _, dom, _ = destProd sigma typ in
106 match find_hyp ~flags seq env sigma dom with
107 | Some (sigma, hyp) ->
108 Proofview.Unsafe.tclEVARS sigma <*>
109 Generalize.generalize [mkApp (id, [|mkVar hyp|])]
110 | None -> tclFAIL (Pp.str "No link")
111 end
112 in
113 tclIFTHENELSE
114 (tclTHENLIST [tac; clear_global id; intro])
115 (wrap ~flags 1 false continue seq) backtrack
116
117 (* right connectives rules *)
118
119 let and_tac ~flags backtrack continue seq=
120 tclIFTHENELSE simplest_split (wrap ~flags 0 true continue seq) backtrack
121
122 let or_tac ~flags backtrack continue seq=
123 tclORELSE
124 (any_constructor false (Some (tclCOMPLETE (wrap ~flags 0 true continue seq))))
125 backtrack
126
127 let arrow_tac ~flags backtrack continue seq=
128 tclIFTHENELSE intro (wrap ~flags 1 true continue seq)
129 (tclORELSE
130 (tclTHEN introf (tclCOMPLETE (wrap ~flags 1 true continue seq)))
131 backtrack)
132 (* left connectives rules *)
133
134 let left_and_tac ~flags ind backtrack id continue seq =
135 Proofview.Goal.enter begin fun gl ->
136 let env = Proofview.Goal.env gl in
137 let n = (construct_nhyps env ind).(0) in
138 tclIFTHENELSE
139 (tclTHENLIST
140 [(pf_constr_of_global id >>= simplest_elim);
141 clear_global id;
142 tclDO n intro])
143 (wrap ~flags n false continue seq)
144 backtrack
145 end
146
147 let left_or_tac ~flags ind backtrack id continue seq =
148 Proofview.Goal.enter begin fun gl ->
149 let env = Proofview.Goal.env gl in
150 let v = construct_nhyps env ind in
151 let f n=
152 tclTHENLIST
153 [clear_global id;
154 tclDO n intro;
155 wrap ~flags n false continue seq] in
156 tclIFTHENSVELSE
157 (pf_constr_of_global id >>= simplest_elim)
158 (Array.map f v)
159 backtrack
160 end
161
162 let left_false_tac id=
163 Tacticals.pf_constr_of_global id >>= simplest_elim
164
165 (* left arrow connective rules *)
166
167 (* We use this function for false, and, or, exists *)
168
169 let ll_ind_tac ~flags (ind,u as indu) largs backtrack id continue seq =
170 Proofview.Goal.enter begin fun gl ->
171 let env = Proofview.Goal.env gl in
172 let sigma = Proofview.Goal.sigma gl in
173 let rcs = ind_hyps env sigma 0 indu largs in
174 let vargs=Array.of_list largs in
175 (* construire le terme H->B, le generaliser etc *)
176 let myterm idc i=
177 let rc=rcs.(i) in
178 let p=List.length rc in
179 let u = EInstance.make u in
180 let cstr=mkApp ((mkConstructU ((ind,(i+1)),u)),vargs) in
181 let vars=Array.init p (fun j->mkRel (p-j)) in
182 let capply=mkApp ((lift p cstr),vars) in
183 let head=mkApp ((lift p idc),[|capply|]) in
184 EConstr.it_mkLambda_or_LetIn head rc in
185 let lp=Array.length rcs in
186 let newhyps idc =List.init lp (myterm idc) in
187 tclIFTHENELSE
188 (tclTHENLIST
189 [(pf_constr_of_global id >>= fun idc -> Generalize.generalize (newhyps idc));
190 clear_global id;
191 tclDO lp intro])
192 (wrap ~flags lp false continue seq) backtrack
193 end
194
195 let ll_arrow_tac ~flags a b c backtrack id continue seq=
196 let open EConstr in
197 let open Vars in
198 let cc=mkProd(Context.make_annot Anonymous ERelevance.relevant,a,(lift 1 b)) in
199 let d idc = mkLambda (Context.make_annot Anonymous ERelevance.relevant,b,
200 mkApp (idc, [|mkLambda (Context.make_annot Anonymous ERelevance.relevant,(lift 1 a),(mkRel 2))|])) in
201 tclORELSE
202 (tclTHENS (cut c)
203 [tclTHENLIST
204 [introf;
205 clear_global id;
206 wrap ~flags 1 false continue seq];
207 tclTHENS (cut cc)
208 [(pf_constr_of_global id >>= fun c -> exact_no_check c);
209 tclTHENLIST
210 [(pf_constr_of_global id >>= fun idc -> Generalize.generalize [d idc]);
211 clear_global id;
212 introf;
213 introf;
214 tclCOMPLETE (wrap ~flags 2 true continue seq)]]])
215 backtrack
216
217 (* quantifier rules (easy side) *)
218
219 let forall_tac ~flags backtrack continue seq=
220 tclORELSE
221 (tclIFTHENELSE intro (wrap ~flags 0 true continue seq)
222 (tclORELSE
223 (tclTHEN introf (tclCOMPLETE (wrap ~flags 0 true continue seq)))
224 backtrack))
225 (tclFAIL (Pp.str "reversible in 1st order mode"))
226
227 let left_exists_tac ~flags ind backtrack id continue seq =
228 Proofview.Goal.enter begin fun gl ->
229 let env = Proofview.Goal.env gl in
230 let n = (construct_nhyps env ind).(0) in
231 tclIFTHENELSE
232 (Tacticals.pf_constr_of_global id >>= simplest_elim)
233 (tclTHENLIST [clear_global id;
234 tclDO n intro;
235 (wrap (n-1) ~flags false continue seq)])
236 backtrack
237 end
238
239 let ll_forall_tac ~flags prod backtrack id continue seq=
240 tclORELSE
241 (tclTHENS (cut prod)
242 [tclTHENLIST
243 [intro;
244 (pf_constr_of_global id >>= fun idc ->
245 Proofview.Goal.enter begin fun gls->
246 let open EConstr in
247 let id0 = List.nth (Tacmach.pf_ids_of_hyps gls) 0 in
248 let term=mkApp(idc,[|mkVar(id0)|]) in
249 tclTHEN (Generalize.generalize [term]) (clear [id0])
250 end);
251 clear_global id;
252 intro;
253 tclCOMPLETE (wrap ~flags 1 false continue (deepen seq))];
254 tclCOMPLETE (wrap ~flags 0 true continue (deepen seq))])
255 backtrack
256
257 (* rules for instantiation with unification moved to instances.ml *)