Debian Codegraph @

plugins/firstorder/instances.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 Unify
12 open Rules
13 open CErrors
14 open Util
15 open EConstr
16 open Vars
17 open Tactics
18 open Tacticals
19 open Proofview.Notations
20 open Reductionops
21 open Formula
22 open Sequent
23 open Names
24 open Context.Rel.Declaration
25
26 let compare_instance inst1 inst2=
27 let cmp c1 c2 = Constr.compare (EConstr.Unsafe.to_constr c1) (EConstr.Unsafe.to_constr c2) in
28 match inst1,inst2 with
29 Phantom(d1),Phantom(d2)->
30 (cmp d1 d2)
31 | Real(i1, n1), Real(i2, n2)->
32 let (m1, c1) = Unify.Item.repr i1 in
33 let (m2, c2) = Unify.Item.repr i2 in
34 ((-) =? (-) ==? cmp) m2 m1 n1 n2 c1 c2
35 | Phantom _, Real (i, _)-> if Unify.Item.is_ground i then -1 else 1
36 | Real (i, _), Phantom _ -> if Unify.Item.is_ground i then 1 else -1
37
38 type any_identifier = AnyId : 'a identifier -> any_identifier
39
40 let compare_gr id1 id2 = match id1, id2 with
41 | AnyId GoalId, AnyId GoalId -> 0
42 | AnyId GoalId, AnyId (FormulaId _) -> 1
43 | AnyId (FormulaId _), AnyId GoalId -> -1
44 | AnyId (FormulaId id1), AnyId (FormulaId id2) -> GlobRef.UserOrd.compare id1 id2
45
46 module OrderedInstance=
47 struct
48 type t = Unify.instance * any_identifier
49 let compare (inst1,id1) (inst2,id2)=
50 (compare_instance =? compare_gr) inst2 inst1 id2 id1
51 (* we want a __decreasing__ total order *)
52 end
53
54 module IS=Set.Make(OrderedInstance)
55
56 let do_sequent env sigma setref triv id seq i dom atoms=
57 let flag=ref true in
58 let phref=ref triv in
59 let do_atoms a1 a2 =
60 let do_pair t1 t2 =
61 match unif_atoms ~check:(not !phref) (Sequent.state seq) env sigma i dom t1 t2 with
62 | None-> ()
63 | Some (Phantom _) ->phref:=true
64 | Some c ->flag:=false;setref:=IS.add (c,id) !setref in
65 List.iter (fun t->List.iter (do_pair t) a2.negative) a1.positive;
66 List.iter (fun t->List.iter (do_pair t) a2.positive) a1.negative in
67 Sequent.iter_redexes (function lf -> do_atoms atoms lf) seq;
68 do_atoms atoms (Sequent.make_simple_atoms seq);
69 !flag && !phref
70
71 let match_one_quantified_hyp (type a) env sigma setref seq (lf : a Formula.t) =
72 match lf.pat with
73 LeftPattern (Lforall(i,dom,triv))|RightPattern(Rexists(i,dom,triv))->
74 if do_sequent env sigma setref triv (AnyId lf.id) seq i dom lf.atoms then
75 setref:=IS.add ((Phantom dom), AnyId lf.id) !setref
76 | _ -> anomaly (Pp.str "can't happen.")
77
78 let give_instances env sigma lf seq=
79 let setref=ref IS.empty in
80 let () = List.iter (function AnyFormula f -> match_one_quantified_hyp env sigma setref seq f) lf in
81 IS.elements !setref
82
83 (* collector for the engine *)
84
85 let rec collect_quantified env sigma seq =
86 try
87 (* This works because the only caller of this function ensures that at this
88 point we only have formulae with priority lower than forall / exists. *)
89 let hd, seq1 = take_formula env sigma seq in
90 let AnyFormula hd0 = hd in
91 (match hd0.pat with
92 LeftPattern(Lforall(_,_,_)) | RightPattern(Rexists(_,_,_)) ->
93 let (q, seq2) = collect_quantified env sigma seq1 in
94 ((hd::q),seq2)
95 | _->[],seq)
96 with Heap.EmptyHeap -> [],seq
97
98 (* open instances processor *)
99
100 let dummy_bvid=Id.of_string "x"
101
102 let mk_open_instance env sigma id idc c =
103 let (m, t) = Item.repr c in
104 let var_id =
105 let typ = Retyping.get_type_of env sigma idc in
106 (* since we know we will get a product,
107 reduction is not too expensive *)
108 let (nam,_,_) = destProd sigma (whd_all env sigma typ) in
109 match nam.Context.binder_name with
110 | Name id -> id
111 | Anonymous -> dummy_bvid
112 in
113 let revt = substl (List.init m (fun i->mkRel (m-i))) t in
114 let rec aux n avoid env sigma decls =
115 if Int.equal n 0 then sigma, decls else
116 let nid = fresh_id_in_env avoid var_id env in
117 let (sigma, (c, s)) = Evarutil.new_type_evar env sigma Evd.univ_flexible in
118 let decl = LocalAssum (Context.make_annot (Name nid) (ESorts.relevance_of_sort s), c) in
119 aux (n-1) (Id.Set.add nid avoid) (EConstr.push_rel decl env) sigma (decl::decls)
120 in
121 let sigma, decls = aux m Id.Set.empty env sigma [] in
122 (sigma, decls, revt)
123
124 (* tactics *)
125
126 let left_instance_tac ~flags (inst,id) continue seq=
127 let open EConstr in
128 Proofview.Goal.enter begin fun gl ->
129 let env = Proofview.Goal.env gl in
130 let sigma = Proofview.Goal.sigma gl in
131 match inst with
132 Phantom dom->
133 if lookup env sigma (id,None) seq then
134 tclFAIL (Pp.str "already done")
135 else
136 tclTHENS (cut dom)
137 [tclTHENLIST
138 [introf;
139 (pf_constr_of_global id >>= fun idc ->
140 Proofview.Goal.enter begin fun gl ->
141 let id0 = List.nth (Tacmach.pf_ids_of_hyps gl) 0 in
142 Generalize.generalize [mkApp(idc, [|mkVar id0|])]
143 end);
144 introf;
145 tclSOLVE [wrap ~flags 1 false continue
146 (deepen (record env (id,None) seq))]];
147 tclTRY assumption]
148 | Real (c, _)->
149 if lookup env sigma (id,Some c) seq then
150 tclFAIL (Pp.str "already done")
151 else
152 let special_generalize=
153 if not @@ Unify.Item.is_ground c then
154 (pf_constr_of_global id >>= fun idc ->
155 Proofview.Goal.enter begin fun gl->
156 let env = Proofview.Goal.env gl in
157 let sigma = Proofview.Goal.sigma gl in
158 let (evmap, rc, ot) = mk_open_instance env sigma id idc c in
159 let gt=
160 it_mkLambda_or_LetIn
161 (mkApp(idc,[|ot|])) rc in
162 let evmap, _ =
163 try Typing.type_of env evmap gt
164 with e when CErrors.noncritical e ->
165 user_err Pp.(str "Untypable instance, maybe higher-order non-prenex quantification") in
166 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS evmap)
167 (Generalize.generalize [gt])
168 end)
169 else
170 pf_constr_of_global id >>= fun idc -> Generalize.generalize [mkApp(idc,[|snd @@ Item.repr c|])]
171 in
172 tclTHENLIST
173 [special_generalize;
174 introf;
175 tclSOLVE
176 [wrap ~flags 1 false continue (deepen (record env (id,Some c) seq))]]
177 end
178
179 let right_instance_tac ~flags inst continue seq=
180 let open EConstr in
181 Proofview.Goal.enter begin fun gl ->
182 match inst with
183 Phantom dom ->
184 tclTHENS (cut dom)
185 [tclTHENLIST
186 [introf;
187 Proofview.Goal.enter begin fun gl ->
188 let id0 = List.nth (Tacmach.pf_ids_of_hyps gl) 0 in
189 split (Tactypes.ImplicitBindings [mkVar id0])
190 end;
191 tclSOLVE [wrap ~flags 0 true continue (deepen seq)]];
192 tclTRY assumption]
193 | Real (c,_) ->
194 if Item.is_ground c then
195 (tclTHEN (split (Tactypes.ImplicitBindings [snd @@ Item.repr c]))
196 (tclSOLVE [wrap ~flags 0 true continue (deepen seq)]))
197 else
198 tclFAIL (Pp.str "not implemented ... yet")
199 end
200
201 let instance_tac ~flags (hd, AnyId id) = match id with
202 | GoalId -> right_instance_tac ~flags hd
203 | FormulaId id -> left_instance_tac ~flags (hd, id)
204
205 let quantified_tac ~flags lf backtrack continue seq =
206 Proofview.Goal.enter begin fun gl ->
207 let env = Proofview.Goal.env gl in
208 let sigma = Proofview.Goal.sigma gl in
209 let insts = give_instances env sigma lf seq in
210 tclORELSE
211 (tclFIRST (List.map (fun inst->instance_tac ~flags inst continue seq) insts))
212 backtrack
213 end