Debian Codegraph @

plugins/firstorder/sequent.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 CErrors
13 open Names
14 open Formula
15 open Unify
16
17 let newcnt ()=
18 let cnt=ref (-1) in
19 fun b->if b then incr cnt;!cnt
20
21 let priority : type a. a pattern -> int = (* pure heuristics, <=0 for non reversible *)
22 function
23 RightPattern rf->
24 begin
25 match rf with
26 Rarrow -> 100
27 | Rand -> 40
28 | Ror -> -15
29 | Rfalse -> -50
30 | Rforall -> 100
31 | Rexists (_,_,_) -> -29
32 end
33 | LeftPattern lf ->
34 match lf with
35 Lfalse -> 999
36 | Land _ -> 90
37 | Lor _ -> 40
38 | Lforall (_,_,_) -> -30
39 | Lexists _ -> 60
40 | LA lap ->
41 match lap with
42 LLatom -> 0
43 | LLfalse (_,_) -> 100
44 | LLand (_,_) -> 80
45 | LLor (_,_) -> 70
46 | LLforall _ -> -20
47 | LLexists (_,_) -> 50
48 | LLarrow (_,_,_) -> -10
49
50 module OrderedFormula=
51 struct
52 type t=Formula.any_formula
53 let compare (AnyFormula e1) (AnyFormula e2) =
54 (priority e1.pat) - (priority e2.pat)
55 end
56
57 type h_item = GlobRef.t * Unify.Item.t option
58
59 let h_canonize env (hr, item) =
60 (Environ.QGlobRef.canonize env hr, item)
61
62 module Hitem=
63 struct
64 type t = h_item
65 let compare (id1,co1) (id2,co2)=
66 let c = GlobRef.UserOrd.compare id1 id2 in
67 if c = 0 then Option.compare Unify.Item.compare co1 co2
68 else c
69 end
70
71 module History=Set.Make(Hitem)
72
73 module HP=Heap.Functional(OrderedFormula)
74
75 type seqgoal = GoalTerm of Formula.uid | GoalAtom of atom | GoalDummy
76
77 type t=
78 {redexes:HP.t;
79 state : Env.t;
80 latoms:atom list;
81 gl: seqgoal;
82 cnt:counter;
83 history:History.t;
84 hyps : Id.Set.t;
85 depth:int}
86
87 let has_fuel seq = seq.depth > 0
88
89 let iter_redexes f seq = HP.iter (fun (AnyFormula p) -> f p.atoms) seq.redexes
90
91 let deepen seq={seq with depth=seq.depth-1}
92
93 let record env item seq={seq with history=History.add (h_canonize env item) seq.history}
94
95 let lookup env sigma item seq=
96 History.mem (h_canonize env item) seq.history ||
97 match item with
98 (_,None)->false
99 | (id,Some i1)->
100 let p (id2,o)=
101 match o with
102 None -> false
103 | Some i2 -> Environ.QGlobRef.equal env id id2 && more_general env sigma i2 i1 in
104 History.exists p seq.history
105
106 let add_concl ~flags env sigma t seq =
107 match build_formula ~flags seq.state env sigma Concl goal_id t seq.cnt with
108 | state, Left f ->
109 {seq with redexes=HP.add (AnyFormula f) seq.redexes; gl = GoalTerm f.uid; state }
110 | state, Right t ->
111 {seq with gl = GoalAtom t; state}
112
113 let add_formula ~flags ~hint env sigma id t seq =
114 let side = Hyp hint in
115 let hyps = match id with
116 | GlobRef.VarRef id -> Id.Set.add id seq.hyps
117 | _ -> seq.hyps
118 in
119 match build_formula ~flags seq.state env sigma side (formula_id env id) t seq.cnt with
120 | state, Left f ->
121 {seq with
122 redexes=HP.add (AnyFormula f) seq.redexes;
123 hyps;
124 state}
125 | state, Right t ->
126 {seq with
127 latoms=t::seq.latoms;
128 hyps;
129 state}
130
131 let re_add_formula_list sigma lf seq=
132 let do_id (AnyFormula f) hyps = match f.id with
133 | GoalId -> hyps
134 | FormulaId (GlobRef.VarRef id) -> Id.Set.add id hyps
135 | FormulaId _ -> hyps
136 in
137 {seq with
138 redexes=List.fold_right HP.add lf seq.redexes;
139 hyps = List.fold_right do_id lf seq.hyps; }
140
141 let mem_hyp id seq = Id.Set.mem id seq.hyps
142
143 let rec take_formula env sigma seq=
144 let hd = HP.maximum seq.redexes in
145 let hp = HP.remove seq.redexes in
146 let AnyFormula hd0 = hd in
147 match hd0.id with
148 | GoalId ->
149 let nseq={seq with redexes=hp} in
150 begin match seq.gl with
151 | GoalTerm t when Formula.eq_uid t hd0.uid -> hd, nseq
152 | GoalAtom _ | GoalTerm _ | GoalDummy -> take_formula env sigma nseq (* discarding deprecated goal *)
153 end
154 | FormulaId id ->
155 let hyps = match id with
156 | GlobRef.VarRef id -> Id.Set.remove id seq.hyps
157 | _ -> seq.hyps
158 in
159 hd, { seq with redexes=hp; hyps; }
160
161 let empty_seq depth=
162 {redexes=HP.empty;
163 latoms=[];
164 gl = GoalDummy;
165 cnt=newcnt ();
166 history=History.empty;
167 depth=depth;
168 hyps = Id.Set.empty;
169 state=Env.empty}
170
171 let make_simple_atoms seq =
172 let ratoms=
173 match seq.gl with
174 | GoalAtom t -> [t]
175 | GoalTerm _ | GoalDummy -> []
176 in {negative=seq.latoms;positive=ratoms}
177
178 let expand_constructor_hints =
179 List.map_append (function
180 | GlobRef.IndRef ind ->
181 List.init (Inductiveops.nconstructors (Global.env()) ind)
182 (fun i -> GlobRef.ConstructRef (ind,i+1))
183 | gr ->
184 [gr])
185
186 let extend_with_ref_list ~flags env sigma l seq =
187 let l = expand_constructor_hints l in
188 let f gr (seq, sigma) =
189 let sigma, c = Evd.fresh_global env sigma gr in
190 let sigma, typ= Typing.type_of env sigma c in
191 (add_formula ~flags ~hint:false env sigma gr typ seq, sigma) in
192 List.fold_right f l (seq, sigma)
193
194 open Hints
195
196 let extend_with_auto_hints ~flags env sigma l seq =
197 let f (seq,sigma) p_a_t =
198 match FullHint.repr p_a_t with
199 | Res_pf c | Give_exact c
200 | Res_pf_THEN_trivial_fail c ->
201 let c = snd @@ hint_as_term c in
202 (match EConstr.destRef sigma c with
203 | exception Constr.DestKO -> seq, sigma
204 | gr, _ ->
205 let sigma, typ = Typing.type_of env sigma c in
206 add_formula ~flags ~hint:true env sigma gr typ seq, sigma)
207 | _ -> seq, sigma
208 in
209 let h acc dbname =
210 let hdb =
211 try
212 searchtable_map dbname
213 with Not_found->
214 user_err Pp.(str ("Firstorder: "^dbname^" : No such Hint database"))
215 in
216 Hint_db.fold (fun _ _ l acc -> List.fold_left f acc l) hdb acc
217 in
218 List.fold_left h (seq,sigma) l
219
220 let state seq = seq.state