Debian Codegraph @

plugins/rtauto/proof_search.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 Goptions
14
15 type s_info=
16 {mutable created_steps : int; (* node count*)
17 mutable pruned_steps : int;
18 mutable created_branches : int; (* path count *)
19 mutable pruned_branches : int;
20 mutable created_hyps : int; (* hyps count *)
21 mutable pruned_hyps : int;
22 mutable branch_failures : int;
23 mutable branch_successes : int;
24 mutable nd_branching : int}
25
26 let s_info=
27 {created_steps = 0; (* node count*)
28 pruned_steps = 0;
29 created_branches = 0; (* path count *)
30 pruned_branches = 0;
31 created_hyps = 0; (* hyps count *)
32 pruned_hyps = 0;
33 branch_failures = 0;
34 branch_successes = 0;
35 nd_branching = 0}
36
37 let reset_info () =
38 s_info.created_steps <- 0; (* node count*)
39 s_info.pruned_steps <- 0;
40 s_info.created_branches <- 0; (* path count *)
41 s_info.pruned_branches <- 0;
42 s_info.created_hyps <- 0; (* hyps count *)
43 s_info.pruned_hyps <- 0;
44 s_info.branch_failures <- 0;
45 s_info.branch_successes <- 0;
46 s_info.nd_branching <- 0
47
48 let { Goptions.get = pruning } =
49 declare_bool_option_and_ref
50 ~key:["Rtauto";"Pruning"]
51 ~value:true
52 ()
53
54 type form=
55 Atom of int
56 | Arrow of form * form
57 | Bot
58 | Conjunct of form * form
59 | Disjunct of form * form
60
61 module FOrd = struct
62 type t = form
63 let rec compare x y =
64 match x, y with
65 | Bot, Bot -> 0
66 | Bot, _ -> -1
67 | Atom _, Bot -> 1
68 | Atom a1, Atom a2 -> Int.compare a1 a2
69 | Atom _, _ -> -1
70 | Arrow _, (Bot | Atom _) -> 1
71 | Arrow (f1, g1), Arrow (f2, g2) ->
72 let cmp = compare f1 f2 in
73 if cmp = 0 then compare g1 g2 else cmp
74 | Arrow _, _ -> -1
75 | Conjunct _, (Bot | Atom _ | Arrow _) -> 1
76 | Conjunct (f1, g1), Conjunct (f2, g2) ->
77 let cmp = compare f1 f2 in
78 if cmp = 0 then compare g1 g2 else cmp
79 | Conjunct _, _ -> -1
80 | Disjunct _, (Bot | Atom _ | Arrow _ | Conjunct _) -> 1
81 | Disjunct (f1, g1), Disjunct (f2, g2) ->
82 let cmp = compare f1 f2 in
83 if cmp = 0 then compare g1 g2 else cmp
84 end
85 module Fmap = Map.Make(FOrd)
86
87 type sequent =
88 {rev_hyps: form Int.Map.t;
89 norev_hyps: form Int.Map.t;
90 size:int;
91 left:int Fmap.t;
92 right:(int*form) list Fmap.t;
93 cnx:(int*int*form*form) list;
94 abs:int option;
95 gl:form}
96
97 let add_one_arrow i f1 f2 m=
98 try Fmap.add f1 ((i,f2)::(Fmap.find f1 m)) m with
99 Not_found ->
100 Fmap.add f1 [i,f2] m
101
102 type proof =
103 Ax of int
104 | I_Arrow of proof
105 | E_Arrow of int*int*proof
106 | D_Arrow of int*proof*proof
107 | E_False of int
108 | I_And of proof*proof
109 | E_And of int*proof
110 | D_And of int*proof
111 | I_Or_l of proof
112 | I_Or_r of proof
113 | E_Or of int*proof*proof
114 | D_Or of int*proof
115 | Pop of int*proof
116
117 type rule =
118 SAx of int
119 | SI_Arrow
120 | SE_Arrow of int*int
121 | SD_Arrow of int
122 | SE_False of int
123 | SI_And
124 | SE_And of int
125 | SD_And of int
126 | SI_Or_l
127 | SI_Or_r
128 | SE_Or of int
129 | SD_Or of int
130
131 let add_step s sub =
132 match s,sub with
133 SAx i,[] -> Ax i
134 | SI_Arrow,[p] -> I_Arrow p
135 | SE_Arrow(i,j),[p] -> E_Arrow (i,j,p)
136 | SD_Arrow i,[p1;p2] -> D_Arrow (i,p1,p2)
137 | SE_False i,[] -> E_False i
138 | SI_And,[p1;p2] -> I_And(p1,p2)
139 | SE_And i,[p] -> E_And(i,p)
140 | SD_And i,[p] -> D_And(i,p)
141 | SI_Or_l,[p] -> I_Or_l p
142 | SI_Or_r,[p] -> I_Or_r p
143 | SE_Or i,[p1;p2] -> E_Or(i,p1,p2)
144 | SD_Or i,[p] -> D_Or(i,p)
145 | _,_ -> anomaly ~label:"add_step" (Pp.str "wrong arity.")
146
147 type 'a with_deps =
148 {dep_it:'a;
149 dep_goal:bool;
150 dep_hyps:Int.Set.t}
151
152 type slice=
153 {proofs_done:proof list;
154 proofs_todo:sequent with_deps list;
155 step:rule;
156 needs_goal:bool;
157 needs_hyps:Int.Set.t;
158 changes_goal:bool;
159 creates_hyps:Int.Set.t}
160
161 type state =
162 Complete of proof
163 | Incomplete of sequent * slice list
164
165 let project = function
166 Complete prf -> prf
167 | Incomplete (_,_) -> anomaly (Pp.str "not a successful state.")
168
169 let pop n prf =
170 let nprf=
171 match prf.dep_it with
172 Pop (i,p) -> Pop (i+n,p)
173 | p -> Pop(n,p) in
174 {prf with dep_it = nprf}
175
176 let rec fill stack proof =
177 match stack with
178 [] -> Complete proof.dep_it
179 | slice::super ->
180 if
181 pruning () &&
182 List.is_empty slice.proofs_done &&
183 not (slice.changes_goal && proof.dep_goal) &&
184 not (Int.Set.exists
185 (fun i -> Int.Set.mem i proof.dep_hyps)
186 slice.creates_hyps)
187 then
188 begin
189 s_info.pruned_steps<-s_info.pruned_steps+1;
190 s_info.pruned_branches<- s_info.pruned_branches +
191 List.length slice.proofs_todo;
192 let created_here=Int.Set.cardinal slice.creates_hyps in
193 s_info.pruned_hyps<-s_info.pruned_hyps+
194 List.fold_left
195 (fun sum dseq -> sum + Int.Set.cardinal dseq.dep_hyps)
196 created_here slice.proofs_todo;
197 fill super (pop (Int.Set.cardinal slice.creates_hyps) proof)
198 end
199 else
200 let dep_hyps=
201 Int.Set.union slice.needs_hyps
202 (Int.Set.diff proof.dep_hyps slice.creates_hyps) in
203 let dep_goal=
204 slice.needs_goal ||
205 ((not slice.changes_goal) && proof.dep_goal) in
206 let proofs_done=
207 proof.dep_it::slice.proofs_done in
208 match slice.proofs_todo with
209 [] ->
210 fill super {dep_it =
211 add_step slice.step (List.rev proofs_done);
212 dep_goal = dep_goal;
213 dep_hyps = dep_hyps}
214 | current::next ->
215 let nslice=
216 {proofs_done=proofs_done;
217 proofs_todo=next;
218 step=slice.step;
219 needs_goal=dep_goal;
220 needs_hyps=dep_hyps;
221 changes_goal=current.dep_goal;
222 creates_hyps=current.dep_hyps} in
223 Incomplete (current.dep_it,nslice::super)
224
225 let append stack (step,subgoals) =
226 s_info.created_steps<-s_info.created_steps+1;
227 match subgoals with
228 [] ->
229 s_info.branch_successes<-s_info.branch_successes+1;
230 fill stack {dep_it=add_step step.dep_it [];
231 dep_goal=step.dep_goal;
232 dep_hyps=step.dep_hyps}
233 | hd :: next ->
234 s_info.created_branches<-
235 s_info.created_branches+List.length next;
236 let slice=
237 {proofs_done=[];
238 proofs_todo=next;
239 step=step.dep_it;
240 needs_goal=step.dep_goal;
241 needs_hyps=step.dep_hyps;
242 changes_goal=hd.dep_goal;
243 creates_hyps=hd.dep_hyps} in
244 Incomplete(hd.dep_it,slice::stack)
245
246 let embed seq=
247 {dep_it=seq;
248 dep_goal=false;
249 dep_hyps=Int.Set.empty}
250
251 let change_goal seq gl=
252 {seq with
253 dep_it={seq.dep_it with gl=gl};
254 dep_goal=true}
255
256 let add_hyp seqwd f=
257 s_info.created_hyps<-s_info.created_hyps+1;
258 let seq=seqwd.dep_it in
259 let num = seq.size+1 in
260 let left = Fmap.add f num seq.left in
261 let cnx,right=
262 try
263 let l=Fmap.find f seq.right in
264 List.fold_right (fun (i,f0) l0 -> (num,i,f,f0)::l0) l seq.cnx,
265 Fmap.remove f seq.right
266 with Not_found -> seq.cnx,seq.right in
267 let nseq=
268 match f with
269 Bot ->
270 {seq with
271 left=left;
272 right=right;
273 size=num;
274 abs=Some num;
275 cnx=cnx}
276 | Atom _ ->
277 {seq with
278 size=num;
279 left=left;
280 right=right;
281 cnx=cnx}
282 | Conjunct (_,_) | Disjunct (_,_) ->
283 {seq with
284 rev_hyps=Int.Map.add num f seq.rev_hyps;
285 size=num;
286 left=left;
287 right=right;
288 cnx=cnx}
289 | Arrow (f1,f2) ->
290 let ncnx,nright=
291 try
292 let i = Fmap.find f1 seq.left in
293 (i,num,f1,f2)::cnx,right
294 with Not_found ->
295 cnx,(add_one_arrow num f1 f2 right) in
296 match f1 with
297 Conjunct (_,_) | Disjunct (_,_) ->
298 {seq with
299 rev_hyps=Int.Map.add num f seq.rev_hyps;
300 size=num;
301 left=left;
302 right=nright;
303 cnx=ncnx}
304 | Arrow(_,_) ->
305 {seq with
306 norev_hyps=Int.Map.add num f seq.norev_hyps;
307 size=num;
308 left=left;
309 right=nright;
310 cnx=ncnx}
311 | _ ->
312 {seq with
313 size=num;
314 left=left;
315 right=nright;
316 cnx=ncnx} in
317 {seqwd with
318 dep_it=nseq;
319 dep_hyps=Int.Set.add num seqwd.dep_hyps}
320
321 exception Here_is of (int*form)
322
323 let choose m=
324 try
325 Int.Map.iter (fun i f -> raise (Here_is (i,f))) m;
326 raise Not_found
327 with
328 Here_is (i,f) -> (i,f)
329
330
331 let search_or seq=
332 match seq.gl with
333 Disjunct (f1,f2) ->
334 [{dep_it = SI_Or_l;
335 dep_goal = true;
336 dep_hyps = Int.Set.empty},
337 [change_goal (embed seq) f1];
338 {dep_it = SI_Or_r;
339 dep_goal = true;
340 dep_hyps = Int.Set.empty},
341 [change_goal (embed seq) f2]]
342 | _ -> []
343
344 let search_norev seq=
345 let goals=ref (search_or seq) in
346 let add_one i f=
347 match f with
348 Arrow (Arrow (f1,f2),f3) ->
349 let nseq =
350 {seq with norev_hyps=Int.Map.remove i seq.norev_hyps} in
351 goals:=
352 ({dep_it=SD_Arrow(i);
353 dep_goal=false;
354 dep_hyps=Int.Set.singleton i},
355 [add_hyp
356 (add_hyp
357 (change_goal (embed nseq) f2)
358 (Arrow(f2,f3)))
359 f1;
360 add_hyp (embed nseq) f3]):: !goals
361 | _ -> anomaly ~label:"search_no_rev" (Pp.str "can't happen.") in
362 Int.Map.iter add_one seq.norev_hyps;
363 List.rev !goals
364
365 let search_in_rev_hyps seq=
366 try
367 let i,f=choose seq.rev_hyps in
368 let make_step step=
369 {dep_it=step;
370 dep_goal=false;
371 dep_hyps=Int.Set.singleton i} in
372 let nseq={seq with rev_hyps=Int.Map.remove i seq.rev_hyps} in
373 match f with
374 Conjunct (f1,f2) ->
375 [make_step (SE_And(i)),
376 [add_hyp (add_hyp (embed nseq) f1) f2]]
377 | Disjunct (f1,f2) ->
378 [make_step (SE_Or(i)),
379 [add_hyp (embed nseq) f1;add_hyp (embed nseq) f2]]
380 | Arrow (Conjunct (f1,f2),f0) ->
381 [make_step (SD_And(i)),
382 [add_hyp (embed nseq) (Arrow (f1,Arrow (f2,f0)))]]
383 | Arrow (Disjunct (f1,f2),f0) ->
384 [make_step (SD_Or(i)),
385 [add_hyp (add_hyp (embed nseq) (Arrow(f1,f0))) (Arrow (f2,f0))]]
386 | _ -> anomaly ~label:"search_in_rev_hyps" (Pp.str "can't happen.")
387 with
388 Not_found -> search_norev seq
389
390 let search_rev seq=
391 match seq.cnx with
392 (i,j,f1,f2)::next ->
393 let nseq=
394 match f1 with
395 Conjunct (_,_) | Disjunct (_,_) ->
396 {seq with cnx=next;
397 rev_hyps=Int.Map.remove j seq.rev_hyps}
398 | Arrow (_,_) ->
399 {seq with cnx=next;
400 norev_hyps=Int.Map.remove j seq.norev_hyps}
401 | _ ->
402 {seq with cnx=next} in
403 [{dep_it=SE_Arrow(i,j);
404 dep_goal=false;
405 dep_hyps=Int.Set.add i (Int.Set.singleton j)},
406 [add_hyp (embed nseq) f2]]
407 | [] ->
408 match seq.gl with
409 Arrow (f1,f2) ->
410 [{dep_it=SI_Arrow;
411 dep_goal=true;
412 dep_hyps=Int.Set.empty},
413 [add_hyp (change_goal (embed seq) f2) f1]]
414 | Conjunct (f1,f2) ->
415 [{dep_it=SI_And;
416 dep_goal=true;
417 dep_hyps=Int.Set.empty},[change_goal (embed seq) f1;
418 change_goal (embed seq) f2]]
419 | _ -> search_in_rev_hyps seq
420
421 let search_all seq=
422 match seq.abs with
423 Some i ->
424 [{dep_it=SE_False (i);
425 dep_goal=false;
426 dep_hyps=Int.Set.singleton i},[]]
427 | None ->
428 try
429 let ax = Fmap.find seq.gl seq.left in
430 [{dep_it=SAx (ax);
431 dep_goal=true;
432 dep_hyps=Int.Set.singleton ax},[]]
433 with Not_found -> search_rev seq
434
435 let bare_sequent = embed
436 {rev_hyps=Int.Map.empty;
437 norev_hyps=Int.Map.empty;
438 size=0;
439 left=Fmap.empty;
440 right=Fmap.empty;
441 cnx=[];
442 abs=None;
443 gl=Bot}
444
445 let init_state hyps gl=
446 let init = change_goal bare_sequent gl in
447 let goal=List.fold_right (fun (_,f,_) seq ->add_hyp seq f) hyps init in
448 Incomplete (goal.dep_it,[])
449
450 let success= function
451 Complete _ -> true
452 | Incomplete (_,_) -> false
453
454 let branching = function
455 Incomplete (seq,stack) ->
456 Control.check_for_interrupt ();
457 let successors = search_all seq in
458 let _ =
459 match successors with
460 [] -> s_info.branch_failures<-s_info.branch_failures+1
461 | _::next ->
462 s_info.nd_branching<-s_info.nd_branching+List.length next in
463 List.map (append stack) successors
464 | Complete prf -> anomaly (Pp.str "already succeeded.")
465
466 open Pp
467
468 let rec pp_form =
469 function
470 Arrow(f1,f2) -> (pp_or f1) ++ (str " -> ") ++ (pp_form f2)
471 | f -> pp_or f
472 and pp_or = function
473 Disjunct(f1,f2) ->
474 (pp_or f1) ++ (str " \\/ ") ++ (pp_and f2)
475 | f -> pp_and f
476 and pp_and = function
477 Conjunct(f1,f2) ->
478 (pp_and f1) ++ (str " /\\ ") ++ (pp_atom f2)
479 | f -> pp_atom f
480 and pp_atom= function
481 Bot -> str "#"
482 | Atom n -> int n
483 | f -> str "(" ++ hv 2 (pp_form f) ++ str ")"
484
485 let pr_form f = pp_form f
486
487 let pp_intmap map =
488 let pp=ref (str "") in
489 Int.Map.iter (fun i obj -> pp:= (!pp ++
490 pp_form obj ++ cut ())) map;
491 str "{ " ++ v 0 (!pp) ++ str " }"
492
493 let pp_list pp_obj l=
494 let pp=ref (str "") in
495 List.iter (fun o -> pp := !pp ++ (pp_obj o) ++ str ", ") l;
496 str "[ " ++ !pp ++ str "]"
497
498 let pp_mapint map =
499 let pp=ref (str "") in
500 Fmap.iter (fun obj l -> pp:= (!pp ++
501 pp_form obj ++ str " => " ++
502 pp_list (fun (i,f) -> pp_form f) l ++
503 cut ()) ) map;
504 str "{ " ++ hv 0 (!pp ++ str " }")
505
506 let pp_connect (i,j,f1,f2) = pp_form f1 ++ str " => " ++ pp_form f2
507
508 let pp_gl gl= cut () ++
509 str "{ " ++ hv 0 (
510 begin
511 match gl.abs with
512 None -> str ""
513 | Some i -> str "ABSURD" ++ cut ()
514 end ++
515 str "rev =" ++ pp_intmap gl.rev_hyps ++ cut () ++
516 str "norev =" ++ pp_intmap gl.norev_hyps ++ cut () ++
517 str "arrows=" ++ pp_mapint gl.right ++ cut () ++
518 str "cnx =" ++ pp_list pp_connect gl.cnx ++ cut () ++
519 str "goal =" ++ pp_form gl.gl ++ str " }")
520
521 let pp =
522 function
523 Incomplete(gl,ctx) -> pp_gl gl ++ fnl ()
524 | _ -> str "<complete>"
525
526 let pp_info () =
527 let count_info =
528 if pruning () then
529 str "Proof steps : " ++
530 int s_info.created_steps ++ str " created / " ++
531 int s_info.pruned_steps ++ str " pruned" ++ fnl () ++
532 str "Proof branches : " ++
533 int s_info.created_branches ++ str " created / " ++
534 int s_info.pruned_branches ++ str " pruned" ++ fnl () ++
535 str "Hypotheses : " ++
536 int s_info.created_hyps ++ str " created / " ++
537 int s_info.pruned_hyps ++ str " pruned" ++ fnl ()
538 else
539 str "Pruning is off" ++ fnl () ++
540 str "Proof steps : " ++
541 int s_info.created_steps ++ str " created" ++ fnl () ++
542 str "Proof branches : " ++
543 int s_info.created_branches ++ str " created" ++ fnl () ++
544 str "Hypotheses : " ++
545 int s_info.created_hyps ++ str " created" ++ fnl () in
546 Feedback.msg_info
547 ( str "Proof-search statistics :" ++ fnl () ++
548 count_info ++
549 str "Branch ends: " ++
550 int s_info.branch_successes ++ str " successes / " ++
551 int s_info.branch_failures ++ str " failures" ++ fnl () ++
552 str "Non-deterministic choices : " ++
553 int s_info.nd_branching ++ str " branches")
554
555
556