1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 let _profiler = <:profiler<_profiler>>;;
33 (* set to false to disable paramodulation inside auto_tac *)
34 let connect_to_auto = true;;
37 (* profiling statistics... *)
38 let infer_time = ref 0.;;
39 let forward_simpl_time = ref 0.;;
40 let forward_simpl_new_time = ref 0.;;
41 let backward_simpl_time = ref 0.;;
42 let passive_maintainance_time = ref 0.;;
44 (* limited-resource-strategy related globals *)
45 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
46 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
47 let start_time = ref 0.;; (* time at which the execution started *)
48 let elapsed_time = ref 0.;;
49 (* let maximal_weight = ref None;; *)
50 let maximal_retained_equality = ref None;;
52 (* equality-selection related globals *)
53 let use_fullred = ref true;;
54 let weight_age_ratio = ref 6 (* 5 *);; (* settable by the user *)
55 let weight_age_counter = ref !weight_age_ratio ;;
56 let symbols_ratio = ref 0 (* 3 *);;
57 let symbols_counter = ref 0;;
59 (* non-recursive Knuth-Bendix term ordering by default *)
60 (* Utils.compare_terms := Utils.rpo;; *)
61 (* Utils.compare_terms := Utils.nonrec_kbo;; *)
62 (* Utils.compare_terms := Utils.ao;; *)
65 let derived_clauses = ref 0;;
66 let kept_clauses = ref 0;;
68 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
71 (* varbiables controlling the search-space *)
72 let maxdepth = ref 3;;
73 let maxwidth = ref 3;;
76 Equality.goal_proof * Equality.proof * Subst.substitution * Cic.metasenv
78 | ParamodulationFailure of string
79 | ParamodulationSuccess of new_proof
82 type goal = Equality.goal_proof * Cic.metasenv * Cic.term;;
84 type theorem = Cic.term * Cic.term * Cic.metasenv;;
86 let symbols_of_equality equality =
87 let (_, _, (_, left, right, _), _,_) = Equality.open_equality equality in
88 let m1 = symbols_of_term left in
93 let c = TermMap.find k res in
94 TermMap.add k (c+v) res
97 (symbols_of_term right) m1
103 module OrderedEquality = struct
104 type t = Equality.equality
106 let compare eq1 eq2 =
107 match Equality.meta_convertibility_eq eq1 eq2 with
110 let w1, _, (ty,left, right, _), m1,_ = Equality.open_equality eq1 in
111 let w2, _, (ty',left', right', _), m2,_ = Equality.open_equality eq2 in
112 match Pervasives.compare w1 w2 with
114 let res = (List.length m1) - (List.length m2) in
115 if res <> 0 then res else
116 Equality.compare eq1 eq2
120 module EqualitySet = Set.Make(OrderedEquality);;
122 exception Empty_list;;
124 let passive_is_empty = function
130 let size_of_passive ((passive_list, ps), _) = List.length passive_list
131 (* EqualitySet.cardinal ps *)
135 let size_of_active (active_list, _) = List.length active_list
138 let age_factor = 0.01;;
141 selects one equality from passive. The selection strategy is a combination
142 of weight, age and goal-similarity
145 let rec select env (goals,_) passive =
146 processed_clauses := !processed_clauses + 1;
148 match (List.rev goals) with goal::_ -> goal | _ -> assert false
150 let (pos_list, pos_set), passive_table = passive in
151 let remove eq l = List.filter (fun e -> Equality.compare e eq <> 0) l in
152 if !weight_age_ratio > 0 then
153 weight_age_counter := !weight_age_counter - 1;
154 match !weight_age_counter with
156 weight_age_counter := !weight_age_ratio;
157 let rec skip_giant pos_list pos_set passive_table =
159 | (hd:EqualitySet.elt)::tl ->
160 let w,_,_,_,_ = Equality.open_equality hd in
162 Indexing.remove_index passive_table hd
164 let pos_set = EqualitySet.remove hd pos_set in
166 hd, ((tl, pos_set), passive_table)
168 (prerr_endline ("\n\n\nGIANT SKIPPED: "^string_of_int w^"\n\n\n");
169 skip_giant tl pos_set passive_table)
172 skip_giant pos_list pos_set passive_table)
173 | _ when (!symbols_counter > 0) ->
174 (symbols_counter := !symbols_counter - 1;
175 let cardinality map =
176 TermMap.fold (fun k v res -> res + v) map 0
179 let _, _, term = goal in
182 let card = cardinality symbols in
183 let foldfun k v (r1, r2) =
184 if TermMap.mem k symbols then
185 let c = TermMap.find k symbols in
186 let c1 = abs (c - v) in
192 let f equality (i, e) =
194 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
196 let c = others + (abs (common - card)) in
197 if c < i then (c, equality)
200 let e1 = EqualitySet.min_elt pos_set in
203 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
205 (others + (abs (common - card))), e1
207 let _, current = EqualitySet.fold f pos_set initial in
209 Indexing.remove_index passive_table current
212 ((remove current pos_list, EqualitySet.remove current pos_set),
215 symbols_counter := !symbols_ratio;
216 let current = EqualitySet.min_elt pos_set in
218 Indexing.remove_index passive_table current
221 ((remove current pos_list, EqualitySet.remove current pos_set),
225 let filter_dependent passive id =
226 prerr_endline ("+++++++++++++++passives "^
227 ( string_of_int (size_of_passive passive)));
228 let (pos_list, pos_set), passive_table = passive in
231 (fun eq ((list,set),table) ->
232 if Equality.depend eq id then
233 (let _,_,_,_,id_eq = Equality.open_equality eq in
235 prerr_endline ("\n\n--------filtering "^(string_of_int id_eq));
237 EqualitySet.remove eq set),
238 Indexing.remove_index table eq))
240 ((eq::list, set),table))
241 pos_list (([],pos_set),passive_table) in
242 prerr_endline ("+++++++++++++++passives "^
243 ( string_of_int (size_of_passive passive)));
248 (* initializes the passive set of equalities *)
249 let make_passive pos =
250 let set_of equalities =
251 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
254 List.fold_left (fun tbl e -> Indexing.index tbl e) Indexing.empty pos
266 (* adds to passive a list of equalities new_pos *)
267 let add_to_passive passive new_pos =
268 let (pos_list, pos_set), table = passive in
269 let ok set equality = not (EqualitySet.mem equality set) in
270 let pos = List.filter (ok pos_set) new_pos in
272 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
274 let add set equalities =
275 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
277 (pos_list @ pos, add pos_set pos),
282 (* removes from passive equalities that are estimated impossible to activate
283 within the current time limit *)
284 let prune_passive howmany (active, _) passive =
285 let (pl, ps), tbl = passive in
286 let howmany = float_of_int howmany
287 and ratio = float_of_int !weight_age_ratio in
290 int_of_float (if t -. v < 0.5 then t else v)
292 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
293 and in_age = round (howmany /. (ratio +. 1.)) in
295 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
296 let counter = ref !symbols_ratio in
301 counter := !counter - 1;
302 if !counter = 0 then counter := !symbols_ratio in
303 let e = EqualitySet.min_elt ps in
304 let ps' = pickw (w-1) (EqualitySet.remove e ps) in
305 EqualitySet.add e ps'
307 let e = EqualitySet.min_elt ps in
308 let ps' = pickw (w-1) (EqualitySet.remove e ps) in
309 EqualitySet.add e ps'
313 let ps = pickw in_weight ps in
314 let rec picka w s l =
318 | hd::tl when not (EqualitySet.mem hd s) ->
319 let w, s, l = picka (w-1) s tl in
320 w, EqualitySet.add hd s, hd::l
322 let w, s, l = picka w s tl in
327 let _, ps, pl = picka in_age ps pl in
328 if not (EqualitySet.is_empty ps) then
329 maximal_retained_equality := Some (EqualitySet.max_elt ps);
332 (fun e tbl -> Indexing.index tbl e) ps Indexing.empty
338 (** inference of new equalities between current and some in active *)
339 let infer env current (active_list, active_table) =
341 if Utils.debug_metas then
342 (ignore(Indexing.check_target c current "infer1");
343 ignore(List.map (function current -> Indexing.check_target c current "infer2") active_list));
345 let maxm, copy_of_current = Equality.fix_metas !maxmeta current in
347 let active_table = Indexing.index active_table copy_of_current in
348 let _ = <:start<current contro active>> in
350 Indexing.superposition_right !maxmeta env active_table current
352 let _ = <:stop<current contro active>> in
353 if Utils.debug_metas then
356 Indexing.check_target c current "sup0") res);
358 let rec infer_positive table = function
362 Indexing.superposition_right ~subterms_only:true !maxmeta env table equality
365 if Utils.debug_metas then
369 Indexing.check_target c current "sup2") res);
370 let pos = infer_positive table tl in
374 let maxm, copy_of_current = Equality.fix_metas !maxmeta current in
377 let curr_table = Indexing.index Indexing.empty current in
378 let _ = <:start<active contro current>> in
379 let pos = infer_positive curr_table ((*copy_of_current::*)active_list) in
380 let _ = <:stop<active contro current>> in
381 if Utils.debug_metas then
384 Indexing.check_target c current "sup3") pos);
387 derived_clauses := !derived_clauses + (List.length new_pos);
388 match !maximal_retained_equality with
391 ignore(assert false);
392 (* if we have a maximal_retained_equality, we can discard all equalities
393 "greater" than it, as they will never be reached... An equality is
394 greater than maximal_retained_equality if it is bigger
395 wrt. OrderedEquality.compare and it is less similar than
396 maximal_retained_equality to the current goal *)
397 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
400 let check_for_deep_subsumption env active_table eq =
401 let _,_,(eq_ty, left, right, order),metas,id = Equality.open_equality eq in
402 let check_subsumed deep l r =
404 Equality.mk_tmp_equality(0,(eq_ty,l,r,Utils.Incomparable),metas)in
405 match Indexing.subsumption env active_table eqtmp with
410 ("\n\n " ^ Equality.string_of_equality ~env eq ^
411 "\nis"^(if deep then " CONTEXTUALLY " else " ")^"subsumed by \n " ^
412 Equality.string_of_equality ~env eq' ^ "\n\n");
416 let rec aux b (ok_so_far, subsumption_used) t1 t2 =
418 | t1, t2 when not ok_so_far -> ok_so_far, subsumption_used
419 | t1, t2 when subsumption_used -> t1 = t2, subsumption_used
421 | Cic.Appl (h1::l),Cic.Appl (h2::l') when h1 = h2 ->
422 let rc = check_subsumed b t1 t1 in
428 (fun (ok_so_far, subsumption_used) t t' ->
429 aux true (ok_so_far, subsumption_used) t t')
430 (ok_so_far, subsumption_used) l l'
431 with Invalid_argument _ -> false,subsumption_used)
433 false, subsumption_used
434 | _ -> false, subsumption_used *)
435 | Cic.Appl (h1::l),Cic.Appl (h2::l') ->
436 let rc = check_subsumed b t1 t2 in
442 (fun (ok_so_far, subsumption_used) t t' ->
443 aux true (ok_so_far, subsumption_used) t t')
444 (ok_so_far, subsumption_used) l l'
445 with Invalid_argument _ -> false,subsumption_used)
447 false, subsumption_used
448 | _ -> false, subsumption_used
450 fst (aux false (true,false) left right)
454 let check_for_deep env active_table eq =
455 match Indexing.subsumption env active_table eq with
461 let profiler = HExtlib.profile "check_for_deep";;
463 let check_for_deep_subsumption env active_table eq =
464 profiler.HExtlib.profile (check_for_deep_subsumption env active_table) eq
467 (* buttare via sign *)
469 (** simplifies current using active and passive *)
470 let forward_simplify env (sign,current) ?passive (active_list, active_table) =
471 let _, context, _ = env in
475 | Some ((_, _), pt) -> Some pt
477 let demodulate table current =
478 let newmeta, newcurrent =
479 Indexing.demodulation_equality !maxmeta env table sign current in
481 if Equality.is_identity env newcurrent then
484 (* (Printf.sprintf "\ncurrent was: %s\nnewcurrent is: %s\n" *)
485 (* (string_of_equality current) *)
486 (* (string_of_equality newcurrent))); *)
489 (* (Printf.sprintf "active is: %s" *)
490 (* (String.concat "\n" *)
491 (* (List.map (fun (_, e) -> (string_of_equality e)) active_list)))); *)
496 let rec demod current =
497 if Utils.debug_metas then
498 ignore (Indexing.check_target context current "demod0");
499 let res = demodulate active_table current in
500 if Utils.debug_metas then
501 ignore ((function None -> () | Some x ->
502 ignore (Indexing.check_target context x "demod1");()) res);
506 match passive_table with
508 | Some passive_table ->
509 match demodulate passive_table newcurrent with
511 | Some newnewcurrent ->
512 if Equality.compare newcurrent newnewcurrent <> 0 then
514 else Some newnewcurrent
516 let res = demod current in
520 if Indexing.in_index active_table c then
523 match passive_table with
525 if check_for_deep_subsumption env active_table c then
530 if Indexing.subsumption env active_table c = None then
535 | Some passive_table ->
536 if Indexing.in_index passive_table c then None
538 if check_for_deep_subsumption env active_table c then
541 (* if Indexing.subsumption env active_table c = None then*)
542 (match Indexing.subsumption env passive_table c with
546 (*prerr_endline "\n\nPESCO DALLE PASSIVE LA PIU' GENERALE\n\n";
554 type fs_time_info_t = {
555 mutable build_all: float;
556 mutable demodulate: float;
557 mutable subsumption: float;
560 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
563 (** simplifies new using active and passive *)
564 let forward_simplify_new env new_pos ?passive active =
565 if Utils.debug_metas then
569 (fun current -> Indexing.check_target c current "forward new pos")
572 let active_list, active_table = active in
576 | Some ((_, _), pt) -> Some pt
578 let demodulate sign table target =
579 let newmeta, newtarget =
580 Indexing.demodulation_equality !maxmeta env table sign target in
584 (* we could also demodulate using passive. Currently we don't *)
586 List.map (demodulate Positive active_table) new_pos
591 if not (Equality.is_identity env e) then
592 (* if EqualitySet.mem e s then s *)
593 (*else*) EqualitySet.add e s
595 EqualitySet.empty new_pos
597 let new_pos = EqualitySet.elements new_pos_set in
600 match passive_table with
602 (fun e -> (Indexing.subsumption env active_table e = None))
603 | Some passive_table ->
604 (fun e -> ((Indexing.subsumption env active_table e = None) &&
605 (Indexing.subsumption env passive_table e = None)))
607 (* let t1 = Unix.gettimeofday () in *)
608 (* let t2 = Unix.gettimeofday () in *)
609 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
611 match passive_table with
613 (fun e -> not (Indexing.in_index active_table e))
614 | Some passive_table ->
616 not ((Indexing.in_index active_table e) ||
617 (Indexing.in_index passive_table e)))
619 List.filter subs (List.filter is_duplicate new_pos)
623 (** simplifies a goal with equalities in active and passive *)
624 let rec simplify_goal env goal ?passive (active_list, active_table) =
628 | Some ((_, _), pt) -> Some pt
630 let demodulate table goal = Indexing.demodulation_goal env table goal in
632 match passive_table with
633 | None -> demodulate active_table goal
634 | Some passive_table ->
635 let changed, goal = demodulate active_table goal in
636 (* let changed', goal = demodulate passive_table goal in*)
637 (changed (*|| changed'*)), goal
643 snd (simplify_goal env goal ?passive (active_list, active_table))
647 let simplify_goals env goals ?passive active =
648 let a_goals, p_goals = goals in
651 (fun g -> snd (simplify_goal env g ?passive active))
656 (fun g -> snd (simplify_goal env g ?passive active))
663 (** simplifies active usign new *)
664 let backward_simplify_active env new_pos new_table min_weight active =
665 let active_list, active_table = active in
666 let active_list, newa, pruned =
668 (fun equality (res, newn,pruned) ->
669 let ew, _, _, _,id = Equality.open_equality equality in
670 if ew < min_weight then
671 equality::res, newn,pruned
673 match forward_simplify env (Utils.Positive, equality) (new_pos, new_table) with
674 | None -> res, newn, id::pruned
676 if Equality.compare equality e = 0 then
679 res, e::newn, pruned)
680 active_list ([], [],[])
683 List.exists (Equality.meta_convertibility_eq eq1) where
686 let _, _, _, _,id = Equality.open_equality eq in id
688 let ((active1,pruned),tbl), newa =
690 (fun eq ((res,pruned), tbl) ->
691 if List.mem eq res then
692 (res, (id_of_eq eq)::pruned),tbl
693 else if (Equality.is_identity env eq) || (find eq res) then (
694 (res, (id_of_eq eq)::pruned),tbl
697 (eq::res,pruned), Indexing.index tbl eq)
698 active_list (([],pruned), Indexing.empty),
701 if (Equality.is_identity env eq) then p
706 | [] -> (active1,tbl), None, pruned
707 | _ -> (active1,tbl), Some newa, pruned
711 (** simplifies passive using new *)
712 let backward_simplify_passive env new_pos new_table min_weight passive =
713 let (pl, ps), passive_table = passive in
714 let f sign equality (resl, ress, newn) =
715 let ew, _, _, _ , _ = Equality.open_equality equality in
716 if ew < min_weight then
717 equality::resl, ress, newn
719 match forward_simplify env (sign, equality) (new_pos, new_table) with
720 | None -> resl, EqualitySet.remove equality ress, newn
723 equality::resl, ress, newn
725 let ress = EqualitySet.remove equality ress in
728 let pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
731 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
734 | [] -> ((pl, ps), passive_table), None
735 | _ -> ((pl, ps), passive_table), Some (newp)
738 let build_table equations =
741 let ew, _, _, _ , _ = Equality.open_equality e in
742 e::l, Indexing.index t e, min ew w)
743 ([], Indexing.empty, 1000000) equations
747 let backward_simplify env new' ?passive active =
748 let new_pos, new_table, min_weight = build_table new' in
752 let ew, _, _, _ , _ = Equality.open_equality e in
753 e::l, Indexing.index t e, min ew w)
754 ([], Indexing.empty, 1000000) new'
757 let active, newa, pruned =
758 backward_simplify_active env new_pos new_table min_weight active in
761 active, (make_passive []), newa, None, pruned
763 active, passive, newa, None, pruned
766 backward_simplify_passive env new_pos new_table min_weight passive in
767 active, passive, newa, newp *)
771 let close env new' given =
772 let new_pos, new_table, min_weight =
775 let ew, _, _, _ , _ = Equality.open_equality e in
776 e::l, Indexing.index t e, min ew w)
777 ([], Indexing.empty, 1000000) (snd new')
781 let pos = infer env c (new_pos,new_table) in
786 let is_commutative_law eq =
787 let w, proof, (eq_ty, left, right, order), metas , _ =
788 Equality.open_equality eq
790 match left,right with
791 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
792 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
793 f1 = f2 && a1 = b2 && a2 = b1
797 let prova env new' active =
798 let given = List.filter is_commutative_law (fst active) in
802 (Printf.sprintf "symmetric:\n%s\n"
805 (fun e -> Equality.string_of_equality ~env e)
810 (* returns an estimation of how many equalities in passive can be activated
811 within the current time limit *)
812 let get_selection_estimate () =
813 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
814 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
816 ceil ((float_of_int !processed_clauses) *.
817 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
821 (** initializes the set of goals *)
822 let make_goals goal =
824 and passive = [0, [goal]] in
828 let make_goal_set goal =
832 (** initializes the set of theorems *)
833 let make_theorems theorems =
838 let activate_goal (active, passive) =
841 | goal_conj::tl -> true, (goal_conj::active, tl)
842 | [] -> false, (active, passive)
844 true, (active,passive)
848 let activate_theorem (active, passive) =
850 | theorem::tl -> true, (theorem::active, tl)
851 | [] -> false, (active, passive)
856 let simplify_theorems env theorems ?passive (active_list, active_table) =
857 let pl, passive_table =
860 | Some ((pn, _), (pp, _), pt) ->
861 let pn = List.map (fun e -> (Negative, e)) pn
862 and pp = List.map (fun e -> (Positive, e)) pp in
865 let a_theorems, p_theorems = theorems in
866 let demodulate table theorem =
867 let newmeta, newthm =
868 Indexing.demodulation_theorem !maxmeta env table theorem in
870 theorem != newthm, newthm
872 let foldfun table (a, p) theorem =
873 let changed, theorem = demodulate table theorem in
874 if changed then (a, theorem::p) else (theorem::a, p)
876 let mapfun table theorem = snd (demodulate table theorem) in
877 match passive_table with
879 let p_theorems = List.map (mapfun active_table) p_theorems in
880 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
881 | Some passive_table ->
882 let p_theorems = List.map (mapfun active_table) p_theorems in
883 let p_theorems, a_theorems =
884 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
885 let p_theorems = List.map (mapfun passive_table) p_theorems in
886 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
890 let rec simpl env e others others_simpl =
891 let active = others @ others_simpl in
894 (fun t e -> Indexing.index t e)
895 Indexing.empty active
897 let res = forward_simplify env (Positive,e) (active, tbl) in
901 | None -> simpl env hd tl others_simpl
902 | Some e -> simpl env hd tl (e::others_simpl)
906 | None -> others_simpl
907 | Some e -> e::others_simpl
911 let simplify_equalities env equalities =
914 (Printf.sprintf "equalities:\n%s\n"
916 (List.map Equality.string_of_equality equalities))));
917 debug_print (lazy "SIMPLYFYING EQUALITIES...");
918 match equalities with
922 List.rev (simpl env hd tl [])
926 (Printf.sprintf "equalities AFTER:\n%s\n"
928 (List.map Equality.string_of_equality res))));
932 let print_goals goals =
939 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
941 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
944 let check_if_goal_is_subsumed ((_,ctx,_) as env) table (goalproof,menv,ty) =
945 (* let names = names_of_context ctx in*)
946 (* Printf.eprintf "check_goal_subsumed: %s\n" (CicPp.pp ty names);*)
948 | Cic.Appl[Cic.MutInd(uri,_,_);eq_ty;left;right]
949 when UriManager.eq uri (LibraryObjects.eq_URI ()) ->
952 (0,Equality.Exact (Cic.Implicit None),(eq_ty,left,right,Eq),menv)
954 (* match Indexing.subsumption env table goal_equation with*)
955 match Indexing.unification env table goal_equation with
956 | Some (subst, equality ) ->
957 let (_,p,(ty,l,r,_),m,id) = Equality.open_equality equality in
958 let cicmenv = Subst.apply_subst_metasenv subst (m @ menv) in
959 Some (goalproof, p, subst, cicmenv)
966 (** given-clause algorithm with full reduction strategy *)
967 let rec given_clause_fullred dbd env goals theorems ~passive active =
968 let goals = simplify_goals env goals ~passive active in
969 let _,context,_ = env in
971 (Equality.goal_proof * Cic.metasenv * Cic.term) list *
972 (Equality.goal_proof * Cic.metasenv * Cic.term) list) = activate_goal
975 (Equality.goal_proof * Cic.metasenv * Cic.term) list *
976 (Equality.goal_proof * Cic.metasenv * Cic.term) list)
978 (* let theorems = simplify_theorems env theorems ~passive active in *)
980 let names = List.map (HExtlib.map_option (fun (name,_) -> name)) context in
981 let _, _, t = List.hd (fst goals) in
982 let _ = prerr_endline ("goal activated = " ^ (CicPp.pp t names)) in
986 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
987 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
988 (* let current = List.hd (fst goals) in *)
989 (* let p, _, t = List.hd (snd current) in *)
992 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
993 (* (CicPp.ppterm t) (string_of_proof p))); *)
996 (* apply_goal_to_theorems dbd env theorems ~passive active goals in *)
997 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
999 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])::_
1000 when left = right && iseq uri ->
1001 let reflproof = Equality.Exact (Equality.refl_proof eq_ty left) in
1002 true, Some (goalproof, reflproof, Subst.empty_subst,m)
1004 (match check_if_goal_is_subsumed env (snd active) goal with
1005 | None -> false,None
1007 prerr_endline "Proof found by subsumption!";
1012 ( prerr_endline "esco qui";
1014 let s = Printf.sprintf "actives:\n%s\n"
1017 (fun (s, e) -> (string_of_sign s) ^ " " ^
1018 (string_of_equality ~env e))
1020 let sp = Printf.sprintf "passives:\n%s\n"
1023 (string_of_equality ~env)
1024 (let x,y,_ = passive in (fst x)@(fst y)))) in
1026 prerr_endline sp; *)
1028 | None -> assert false
1029 | Some p -> ParamodulationSuccess p)
1031 given_clause_fullred_aux dbd env goals theorems passive active
1033 (* let ok', theorems = activate_theorem theorems in *)
1035 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1038 (* match (fst goals) with *)
1039 (* | (_, [proof, _, _])::_ -> Some proof *)
1040 (* | _ -> assert false *)
1042 (* ParamodulationSuccess (proof, env) *)
1044 (* given_clause_fullred_aux env goals theorems passive active *)
1046 if (passive_is_empty passive) then ParamodulationFailure ""
1047 else given_clause_fullred_aux dbd env goals theorems passive active
1049 and given_clause_fullred_aux dbd env goals theorems passive active =
1050 prerr_endline (string_of_int !counter ^
1051 " MAXMETA: " ^ string_of_int !maxmeta ^
1052 " #ACTIVES: " ^ string_of_int (size_of_active active) ^
1053 " #PASSIVES: " ^ string_of_int (size_of_passive passive));
1056 if !counter mod 10 = 0 then
1058 let size = HExtlib.estimate_size (passive,active) in
1059 let sizep = HExtlib.estimate_size (passive) in
1060 let sizea = HExtlib.estimate_size (active) in
1061 let (l1,s1),(l2,s2), t = passive in
1062 let sizetbl = HExtlib.estimate_size t in
1063 let sizel = HExtlib.estimate_size (l1,l2) in
1064 let sizes = HExtlib.estimate_size (s1,s2) in
1066 prerr_endline ("SIZE: " ^ string_of_int size);
1067 prerr_endline ("SIZE P: " ^ string_of_int sizep);
1068 prerr_endline ("SIZE A: " ^ string_of_int sizea);
1069 prerr_endline ("SIZE TBL: " ^ string_of_int sizetbl ^
1070 " SIZE L: " ^ string_of_int sizel ^
1071 " SIZE S:" ^ string_of_int sizes);
1074 if (size_of_active active) mod 50 = 0 then
1075 (let s = Printf.sprintf "actives:\n%s\n"
1078 (fun (s, e) -> (string_of_sign s) ^ " " ^
1079 (string_of_equality ~env e))
1081 let sp = Printf.sprintf "passives:\n%s\n"
1084 (string_of_equality ~env)
1085 (let x,y,_ = passive in (fst x)@(fst y)))) in
1087 prerr_endline sp); *)
1088 let time1 = Unix.gettimeofday () in
1089 let (_,context,_) = env in
1090 let selection_estimate = get_selection_estimate () in
1091 let kept = size_of_passive passive in
1093 if !time_limit = 0. || !processed_clauses = 0 then
1095 else if !elapsed_time > !time_limit then (
1096 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1097 !time_limit !elapsed_time));
1099 ) else if kept > selection_estimate then (
1101 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1102 "(kept: %d, selection_estimate: %d)\n")
1103 kept selection_estimate));
1104 prune_passive selection_estimate active passive
1109 let time2 = Unix.gettimeofday () in
1110 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1112 kept_clauses := (size_of_passive passive) + (size_of_active active);
1113 match passive_is_empty passive with
1114 | true -> ParamodulationFailure ""
1115 (* given_clause_fullred dbd env goals theorems passive active *)
1117 let current, passive = select env goals passive in
1119 ("Selected = " ^ Equality.string_of_equality ~env current);
1121 (let w,p,(t,l,r,o),m = current in
1122 " size w: " ^ string_of_int (HExtlib.estimate_size w)^
1123 " size p: " ^ string_of_int (HExtlib.estimate_size p)^
1124 " size t: " ^ string_of_int (HExtlib.estimate_size t)^
1125 " size l: " ^ string_of_int (HExtlib.estimate_size l)^
1126 " size r: " ^ string_of_int (HExtlib.estimate_size r)^
1127 " size o: " ^ string_of_int (HExtlib.estimate_size o)^
1128 " size m: " ^ string_of_int (HExtlib.estimate_size m)^
1129 " size m-c: " ^ string_of_int
1130 (HExtlib.estimate_size (List.map (fun (x,_,_) -> x) m)))) *)
1131 let time1 = Unix.gettimeofday () in
1132 let res = forward_simplify env (Positive, current) ~passive active in
1133 let time2 = Unix.gettimeofday () in
1134 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1137 (* weight_age_counter := !weight_age_counter + 1; *)
1138 given_clause_fullred dbd env goals theorems passive active
1140 (* prerr_endline (Printf.sprintf "selected simpl: %s"
1141 (Equality.string_of_equality ~env current));*)
1142 let t1 = Unix.gettimeofday () in
1143 let new' = infer env current active in
1147 (Printf.sprintf "new' (senza semplificare):\n%s\n"
1150 (fun e -> "Positive " ^
1151 (Equality.string_of_equality ~env e)) new'))))
1153 let t2 = Unix.gettimeofday () in
1154 infer_time := !infer_time +. (t2 -. t1);
1156 if Equality.is_identity env current then active
1158 let al, tbl = active in
1159 al @ [current], Indexing.index tbl current
1161 let rec simplify new' active passive =
1162 let t1 = Unix.gettimeofday () in
1163 let new' = forward_simplify_new env new'~passive active in
1164 let t2 = Unix.gettimeofday () in
1165 forward_simpl_new_time :=
1166 !forward_simpl_new_time +. (t2 -. t1);
1167 let t1 = Unix.gettimeofday () in
1168 let active, passive, newa, retained, pruned =
1169 backward_simplify env new' ~passive active in
1171 List.fold_left filter_dependent passive pruned in
1172 let t2 = Unix.gettimeofday () in
1173 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1174 match newa, retained with
1175 | None, None -> active, passive, new'
1178 if Utils.debug_metas then
1181 (fun x->Indexing.check_target context x "simplify1")
1184 simplify (new' @ p) active passive
1185 | Some p, Some rp ->
1186 simplify (new' @ p @ rp) active passive
1188 let active, passive, new' = simplify new' active passive in
1190 let a,b,_ = build_table new' in
1191 simplify_goals env goals ~passive (a,b)
1195 let new1 = prova env new' active in
1196 let new' = (fst new') @ (fst new1), (snd new') @ (snd new1) in
1202 (Printf.sprintf "new1:\n%s\n"
1205 (fun e -> "Negative " ^
1206 (string_of_equality ~env e)) neg) @
1208 (fun e -> "Positive " ^
1209 (string_of_equality ~env e)) pos)))))
1212 let k = size_of_passive passive in
1213 if k < (kept - 1) then
1214 processed_clauses := !processed_clauses + (kept - 1 - k);
1219 (Printf.sprintf "active:\n%s\n"
1222 (fun e -> (Equality.string_of_equality ~env e))
1228 (Printf.sprintf "new':\n%s\n"
1231 (fun e -> "Negative " ^
1232 (Equality.string_of_equality ~env e)) new')))))
1234 let passive = add_to_passive passive new' in
1235 given_clause_fullred dbd env goals theorems passive active
1239 let profiler0 = HExtlib.profile "P/Saturation.given_clause_fullred"
1241 let given_clause_fullred dbd env goals theorems passive active =
1242 profiler0.HExtlib.profile
1243 (given_clause_fullred dbd env goals theorems passive) active
1246 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ());;
1248 let check_if_goal_is_identity env = function
1249 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
1250 when left = right && iseq uri ->
1251 let reflproof = Equality.Exact (Equality.refl_proof eq_ty left) in
1252 Some (goalproof, reflproof,Subst.empty_subst,m)
1256 let rec check goal = function
1260 | None -> check goal tl
1261 | (Some p) as ok -> ok
1264 let simplify_goal_set env goals passive active =
1265 let active_goals, passive_goals = goals in
1266 let find (_,_,g) where =
1267 List.exists (fun (_,_,g1) -> Equality.meta_convertibility g g1) where
1272 match simplify_goal env goal ~passive active with
1273 | _, g -> if find g acc then acc else g::acc)
1276 if List.length active_goals <> List.length simplified then
1277 prerr_endline "SEMPLIFICANDO HO SCARTATO...";
1278 (simplified,passive_goals)
1280 HExtlib.list_uniq ~eq:(fun (_,_,t1) (_,_,t2) -> t1 = t2)
1281 (List.sort (fun (_,_,t1) (_,_,t2) -> compare t1 t1)
1282 ((*goals @*) simplified))
1286 let check_if_goals_set_is_solved env active goals =
1287 let active_goals, passive_goals = goals in
1294 check_if_goal_is_identity env;
1295 check_if_goal_is_subsumed env (snd active)])
1299 let infer_goal_set env active goals =
1300 let active_goals, passive_goals = goals in
1301 let rec aux = function
1303 | ((_,_,t1) as hd)::tl when
1305 (fun (_,_,t) -> Equality.meta_convertibility t t1)
1308 let selected = hd in
1309 let passive_goals = tl in
1310 let new' = Indexing.superposition_left env (snd active) selected in
1311 selected::active_goals, passive_goals @ new'
1317 let infer_goal_set_with_current env current goals =
1318 let active_goals, passive_goals = goals in
1319 let _,table,_ = build_table [current] in
1323 let new' = Indexing.superposition_left env table g in
1325 passive_goals active_goals
1330 let size_of_goal_set_a (l,_) = List.length l;;
1331 let size_of_goal_set_p (_,l) = List.length l;;
1333 (** given-clause algorithm with full reduction strategy: NEW implementation *)
1334 (* here goals is a set of goals in OR *)
1336 ((_,context,_) as env) goals theorems passive active max_iterations max_time
1338 let initial_time = Unix.gettimeofday () in
1339 let iterations_left iterno =
1340 let now = Unix.gettimeofday () in
1341 let time_left = max_time -. now in
1342 let time_spent_until_now = now -. initial_time in
1343 let iteration_medium_cost =
1344 time_spent_until_now /. (float_of_int iterno)
1346 let iterations_left = time_left /. iteration_medium_cost in
1347 int_of_float iterations_left
1349 let rec step goals theorems passive active iterno =
1350 if iterno > max_iterations then
1351 (ParamodulationFailure "No more iterations to spend")
1352 else if Unix.gettimeofday () > max_time then
1353 (ParamodulationFailure "No more time to spend")
1355 let _ = prerr_endline "simpl goal with active" in
1356 let goals = simplify_goal_set env goals passive active in
1357 match check_if_goals_set_is_solved env active goals with
1360 (Printf.sprintf "Found a proof in: %f\n"
1361 (Unix.gettimeofday() -. initial_time));
1363 ParamodulationSuccess p
1366 (Printf.sprintf "%d #ACTIVES: %d #PASSIVES: %d #GOALSET: %d(%d)\n"
1367 iterno (size_of_active active) (size_of_passive passive)
1368 (size_of_goal_set_a goals) (size_of_goal_set_p goals));
1369 (* PRUNING OF PASSIVE THAT WILL NEVER BE PROCESSED *)
1371 let selection_estimate = iterations_left iterno in
1372 let kept = size_of_passive passive in
1373 if kept > selection_estimate then
1375 (*Printf.eprintf "Too many passive equalities: pruning...";
1376 prune_passive selection_estimate active*) passive
1381 kept_clauses := (size_of_passive passive) + (size_of_active active);
1383 if passive_is_empty passive then
1384 ParamodulationFailure "No more passive"(*maybe this is a success! *)
1387 let goals = infer_goal_set env active goals in
1388 let current, passive = select env goals passive in
1389 prerr_endline (Printf.sprintf "Selected = %s\n"
1390 (Equality.string_of_equality ~env current));
1391 (* SIMPLIFICATION OF CURRENT *)
1393 forward_simplify env (Positive, current) ~passive active
1396 | None -> step goals theorems passive active (iterno+1)
1398 (* GENERATION OF NEW EQUATIONS *)
1399 prerr_endline "infer";
1400 let new' = infer env current active in
1401 prerr_endline "infer goal";
1402 let goals = infer_goal_set_with_current env current goals in
1404 let al, tbl = active in
1405 al @ [current], Indexing.index tbl current
1407 (* FORWARD AND BACKWARD SIMPLIFICATION *)
1408 prerr_endline "fwd/back simpl";
1409 let rec simplify new' active passive =
1410 let new' = forward_simplify_new env new' ~passive active in
1411 let active, passive, newa, retained, pruned =
1412 backward_simplify env new' ~passive active
1415 List.fold_left filter_dependent passive pruned
1417 match newa, retained with
1418 | None, None -> active, passive, new'
1420 | None, Some p -> simplify (new' @ p) active passive
1421 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1423 let active, passive, new' = simplify new' active passive in
1424 prerr_endline "simpl goal with new";
1426 let a,b,_ = build_table new' in
1427 simplify_goal_set env goals passive (a,b)
1429 let passive = add_to_passive passive new' in
1430 step goals theorems passive active (iterno+1)
1433 step goals theorems passive active 1
1436 let rec saturate_equations env goal accept_fun passive active =
1437 elapsed_time := Unix.gettimeofday () -. !start_time;
1438 if !elapsed_time > !time_limit then
1441 let current, passive = select env ([goal],[]) passive in
1442 let res = forward_simplify env (Positive, current) ~passive active in
1445 saturate_equations env goal accept_fun passive active
1447 debug_print (lazy (Printf.sprintf "selected: %s"
1448 (Equality.string_of_equality ~env current)));
1449 let new' = infer env current active in
1451 if Equality.is_identity env current then active
1453 let al, tbl = active in
1454 al @ [current], Indexing.index tbl current
1456 let rec simplify new' active passive =
1457 let new' = forward_simplify_new env new' ~passive active in
1458 let active, passive, newa, retained, pruned =
1459 backward_simplify env new' ~passive active in
1461 List.fold_left filter_dependent passive pruned in
1462 match newa, retained with
1463 | None, None -> active, passive, new'
1465 | None, Some p -> simplify (new' @ p) active passive
1466 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1468 let active, passive, new' = simplify new' active passive in
1472 (Printf.sprintf "active:\n%s\n"
1475 (fun e -> Equality.string_of_equality ~env e)
1481 (Printf.sprintf "new':\n%s\n"
1484 (fun e -> "Negative " ^
1485 (Equality.string_of_equality ~env e)) new'))))
1487 let new' = List.filter accept_fun new' in
1488 let passive = add_to_passive passive new' in
1489 saturate_equations env goal accept_fun passive active
1492 let main dbd full term metasenv ugraph = ()
1494 let main dbd full term metasenv ugraph =
1495 let module C = Cic in
1496 let module T = CicTypeChecker in
1497 let module PET = ProofEngineTypes in
1498 let module PP = CicPp in
1499 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1500 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1501 let proof, goals = status in
1502 let goal' = List.nth goals 0 in
1503 let _, metasenv, meta_proof, _ = proof in
1504 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1505 let eq_indexes, equalities, maxm = find_equalities context proof in
1506 let lib_eq_uris, library_equalities, maxm =
1508 find_library_equalities dbd context (proof, goal') (maxm+2)
1510 let library_equalities = List.map snd library_equalities in
1511 maxmeta := maxm+2; (* TODO ugly!! *)
1512 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1513 let new_meta_goal, metasenv, type_of_goal =
1514 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1517 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1518 Cic.Meta (maxm+1, irl),
1519 (maxm+1, context, ty)::metasenv,
1522 let env = (metasenv, context, ugraph) in
1523 let t1 = Unix.gettimeofday () in
1526 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1527 let context_hyp = find_context_hypotheses env eq_indexes in
1528 context_hyp @ theorems, []
1531 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1532 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1534 let t = CicUtil.term_of_uri refl_equal in
1535 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1538 let t2 = Unix.gettimeofday () in
1541 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1546 "Theorems:\n-------------------------------------\n%s\n"
1551 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1556 ([],Equality.BasicProof (Equality.empty_subst ,new_meta_goal)), [], goal
1558 let equalities = simplify_equalities env
1559 (equalities@library_equalities) in
1560 let active = make_active () in
1561 let passive = make_passive equalities in
1562 Printf.printf "\ncurrent goal: %s\n"
1563 (let _, _, g = goal in CicPp.ppterm g);
1564 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1565 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1566 Printf.printf "\nequalities:\n%s\n"
1569 (Equality.string_of_equality ~env) equalities));
1570 (* (equalities @ library_equalities))); *)
1571 print_endline "--------------------------------------------------";
1572 let start = Unix.gettimeofday () in
1573 print_endline "GO!";
1574 start_time := Unix.gettimeofday ();
1576 let goals = make_goals goal in
1577 (if !use_fullred then given_clause_fullred else given_clause_fullred)
1578 dbd env goals theorems passive active
1580 let finish = Unix.gettimeofday () in
1583 | ParamodulationFailure ->
1584 Printf.printf "NO proof found! :-(\n\n"
1585 | ParamodulationSuccess (Some ((cicproof,cicmenv),(proof, env))) ->
1586 Printf.printf "OK, found a proof!\n";
1587 let oldproof = Equation.build_proof_term proof in
1588 let newproof,_,newenv,_ =
1589 CicRefine.type_of_aux'
1590 cicmenv context cicproof CicUniv.empty_ugraph
1592 (* REMEMBER: we have to instantiate meta_proof, we should use
1593 apply the "apply" tactic to proof and status
1595 let names = names_of_context context in
1596 prerr_endline "OLD PROOF";
1597 print_endline (PP.pp proof names);
1598 prerr_endline "NEW PROOF";
1599 print_endline (PP.pp newproof names);
1603 let (_, _, _, menv,_) = Equality.open_equality eq in
1610 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1612 print_endline (string_of_float (finish -. start));
1614 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1615 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1617 (fst (CicReduction.are_convertible
1618 context type_of_goal ty ug)));
1620 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1621 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1622 print_endline (string_of_float (finish -. start));*)
1626 | ParamodulationSuccess None ->
1627 Printf.printf "Success, but no proof?!?\n\n"
1632 ((Printf.sprintf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1633 "forward_simpl_new_time: %.9f\n" ^^
1634 "backward_simpl_time: %.9f\n")
1635 !infer_time !forward_simpl_time !forward_simpl_new_time
1636 !backward_simpl_time) ^
1637 (Printf.sprintf "passive_maintainance_time: %.9f\n"
1638 !passive_maintainance_time) ^
1639 (Printf.sprintf " successful unification/matching time: %.9f\n"
1640 !Indexing.match_unif_time_ok) ^
1641 (Printf.sprintf " failed unification/matching time: %.9f\n"
1642 !Indexing.match_unif_time_no) ^
1643 (Printf.sprintf " indexing retrieval time: %.9f\n"
1644 !Indexing.indexing_retrieval_time) ^
1645 (Printf.sprintf " demodulate_term.build_newtarget_time: %.9f\n"
1646 !Indexing.build_newtarget_time) ^
1647 (Printf.sprintf "derived %d clauses, kept %d clauses.\n"
1648 !derived_clauses !kept_clauses))
1652 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1658 let default_depth = !maxdepth
1659 and default_width = !maxwidth;;
1663 symbols_counter := 0;
1664 weight_age_counter := !weight_age_ratio;
1665 processed_clauses := 0;
1668 maximal_retained_equality := None;
1670 forward_simpl_time := 0.;
1671 forward_simpl_new_time := 0.;
1672 backward_simpl_time := 0.;
1673 passive_maintainance_time := 0.;
1674 derived_clauses := 0;
1680 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1681 let module C = Cic in
1683 Indexing.init_index ();
1687 (* CicUnification.unif_ty := false;*)
1688 let proof, goalno = status in
1689 let uri, metasenv, meta_proof, term_to_prove = proof in
1690 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1691 let names = names_of_context context in
1692 let eq_indexes, equalities, maxm = find_equalities context proof in
1693 let ugraph = CicUniv.empty_ugraph in
1694 let env = (metasenv, context, ugraph) in
1695 let goal = [], metasenv, type_of_goal in
1697 let t1 = Unix.gettimeofday () in
1698 let lib_eq_uris, library_equalities, maxm =
1699 find_library_equalities dbd context (proof, goalno) (maxm+2)
1701 let library_equalities = List.map snd library_equalities in
1702 let t2 = Unix.gettimeofday () in
1704 let equalities = simplify_equalities env (equalities@library_equalities) in
1707 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
1708 let t1 = Unix.gettimeofday () in
1711 let thms = find_library_theorems dbd env (proof, goalno) lib_eq_uris in
1712 let context_hyp = find_context_hypotheses env eq_indexes in
1713 context_hyp @ thms, []
1716 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1717 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1719 let t = CicUtil.term_of_uri refl_equal in
1720 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1723 let t2 = Unix.gettimeofday () in
1728 "Theorems:\n-------------------------------------\n%s\n"
1733 "Term: %s, type: %s"
1734 (CicPp.ppterm t) (CicPp.ppterm ty))
1738 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1740 let active = make_active () in
1741 let passive = make_passive equalities in
1742 let start = Unix.gettimeofday () in
1745 let goals = make_goals goal in
1746 given_clause_fullred dbd env goals theorems passive active
1748 let goals = make_goal_set goal in
1749 let max_iterations = 1000 in
1750 let max_time = Unix.gettimeofday () +. 120. (* minutes *) in
1751 given_clause env goals theorems passive active max_iterations max_time
1753 let finish = Unix.gettimeofday () in
1754 (res, finish -. start)
1757 | ParamodulationFailure s ->
1758 raise (ProofEngineTypes.Fail (lazy ("NO proof found: " ^ s)))
1759 | ParamodulationSuccess
1760 (goalproof,newproof,subsumption_subst, proof_menv) ->
1761 prerr_endline "OK, found a proof!";
1762 prerr_endline (Equality.pp_proof names goalproof newproof);
1763 prerr_endline "ENDOFPROOFS";
1764 (* generation of the CIC proof *)
1766 List.filter (fun i -> i <> goalno)
1767 (ProofEngineHelpers.compare_metasenvs
1768 ~newmetasenv:metasenv ~oldmetasenv:proof_menv)
1770 let goal_proof, side_effects_t =
1771 let initial = newproof in
1772 Equality.build_goal_proof goalproof initial type_of_goal side_effects
1774 prerr_endline (CicPp.pp goal_proof names);
1775 let goal_proof = Subst.apply_subst subsumption_subst goal_proof in
1776 let side_effects_t =
1777 List.map (Subst.apply_subst subsumption_subst) side_effects_t
1779 (* replacing fake mets with real ones *)
1780 prerr_endline "replacing metas...";
1781 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1782 let goal_proof_menv, what, with_what,free_meta =
1784 (fun (acc1,acc2,acc3,uniq) (i,_,ty) ->
1787 acc1, (Cic.Meta(i,[]))::acc2, m::acc3, uniq
1789 [i,context,ty], (Cic.Meta(i,[]))::acc2,
1790 (Cic.Meta(i,irl)) ::acc3,Some (Cic.Meta(i,irl)))
1791 ([],[],[],None) proof_menv
1794 ProofEngineReduction.replace_lifting
1795 ~equality:(=) ~what ~with_what ~where
1797 let goal_proof = replace goal_proof in
1798 (* ok per le meta libere... ma per quelle che c'erano e sono rimaste?
1799 * what mi pare buono, sostituisce solo le meta farlocche *)
1800 let side_effects_t = List.map replace side_effects_t in
1802 List.filter (fun i -> i <> goalno)
1803 (ProofEngineHelpers.compare_metasenvs
1804 ~oldmetasenv:metasenv ~newmetasenv:goal_proof_menv)
1806 (* check/refine/... build the new proof *)
1808 ProofEngineReduction.replace
1809 ~what:side_effects ~with_what:side_effects_t
1810 ~equality:(fun i t -> match t with Cic.Meta(j,_)->j=i|_->false)
1813 let subst_side_effects,real_menv,_ =
1814 let fail t s = raise (ProofEngineTypes.Fail (lazy (t^Lazy.force s))) in
1815 let free_metas_menv =
1816 List.map (fun i -> CicUtil.lookup_meta i goal_proof_menv) free_metas
1819 CicUnification.fo_unif_subst [] context (metasenv @ free_metas_menv)
1820 replaced_goal type_of_goal CicUniv.empty_ugraph
1822 | CicUnification.UnificationFailure s
1823 | CicUnification.Uncertain s
1824 | CicUnification.AssertFailure s ->
1825 fail "Maybe the local context of metas in the goal was not an IRL" s
1828 (goalno,(context,goal_proof,type_of_goal))::subst_side_effects
1832 CicTypeChecker.type_of_aux' real_menv context goal_proof
1833 CicUniv.empty_ugraph
1835 | CicUtil.Meta_not_found _
1836 | CicTypeChecker.TypeCheckerFailure _
1837 | CicTypeChecker.AssertFailure _
1838 | Invalid_argument "list_fold_left2" as exn ->
1839 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1840 prerr_endline (CicPp.pp goal_proof names);
1843 let proof, real_metasenv =
1844 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1845 proof goalno (CicMetaSubst.apply_subst final_subst) real_menv
1848 match free_meta with Some(Cic.Meta(m,_)) when m<>goalno ->[m] | _ ->[]
1851 "GOALS APERTI: %s\nMETASENV PRIMA:\n%s\nMETASENV DOPO:\n%s\n"
1852 (String.concat ", " (List.map string_of_int open_goals))
1853 (CicMetaSubst.ppmetasenv [] metasenv)
1854 (CicMetaSubst.ppmetasenv [] real_metasenv);
1855 prerr_endline (Printf.sprintf "\nTIME NEEDED: %8.2f" time);
1859 let retrieve_and_print dbd term metasenv ugraph =
1860 let module C = Cic in
1861 let module T = CicTypeChecker in
1862 let module PET = ProofEngineTypes in
1863 let module PP = CicPp in
1864 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1865 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1866 let proof, goals = status in
1867 let goal' = List.nth goals 0 in
1868 let uri, metasenv, meta_proof, term_to_prove = proof in
1869 let _, context, type_of_goal = CicUtil.lookup_meta goal' metasenv in
1870 let eq_indexes, equalities, maxm = find_equalities context proof in
1871 let ugraph = CicUniv.empty_ugraph in
1872 let env = (metasenv, context, ugraph) in
1873 let t1 = Unix.gettimeofday () in
1874 let lib_eq_uris, library_equalities, maxm =
1875 find_library_equalities dbd context (proof, goal') (maxm+2) in
1876 let t2 = Unix.gettimeofday () in
1878 let equalities = (* equalities @ *) library_equalities in
1881 (Printf.sprintf "\n\nequalities:\n%s\n"
1885 (* Printf.sprintf "%s: %s" *)
1886 (UriManager.string_of_uri u)
1887 (* (string_of_equality e) *)
1890 debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
1891 let rec simpl e others others_simpl =
1893 let active = List.map (fun (u, e) -> (Positive, e))
1894 (others @ others_simpl) in
1897 (fun t (_, e) -> Indexing.index t e)
1898 Indexing.empty active
1900 let res = forward_simplify env (Positive, e) (active, tbl) in
1904 | None -> simpl hd tl others_simpl
1905 | Some e -> simpl hd tl ((u, e)::others_simpl)
1909 | None -> others_simpl
1910 | Some e -> (u, e)::others_simpl
1914 match equalities with
1917 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
1919 List.rev (simpl (*(Positive,*) hd others [])
1923 (Printf.sprintf "\nequalities AFTER:\n%s\n"
1927 Printf.sprintf "%s: %s"
1928 (UriManager.string_of_uri u)
1929 (Equality.string_of_equality e)
1935 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
1939 let main_demod_equalities dbd term metasenv ugraph =
1940 let module C = Cic in
1941 let module T = CicTypeChecker in
1942 let module PET = ProofEngineTypes in
1943 let module PP = CicPp in
1944 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1945 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1946 let proof, goals = status in
1947 let goal' = List.nth goals 0 in
1948 let _, metasenv, meta_proof, _ = proof in
1949 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1950 let eq_indexes, equalities, maxm = find_equalities context proof in
1951 let lib_eq_uris, library_equalities, maxm =
1952 find_library_equalities dbd context (proof, goal') (maxm+2)
1954 let library_equalities = List.map snd library_equalities in
1955 maxmeta := maxm+2; (* TODO ugly!! *)
1956 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1957 let new_meta_goal, metasenv, type_of_goal =
1958 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1961 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
1962 (CicPp.ppterm ty)));
1963 Cic.Meta (maxm+1, irl),
1964 (maxm+1, context, ty)::metasenv,
1967 let env = (metasenv, context, ugraph) in
1969 let goal = [], [], goal
1971 let equalities = simplify_equalities env (equalities@library_equalities) in
1972 let active = make_active () in
1973 let passive = make_passive equalities in
1974 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1975 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1976 Printf.printf "\nequalities:\n%s\n"
1979 (Equality.string_of_equality ~env) equalities));
1980 print_endline "--------------------------------------------------";
1981 print_endline "GO!";
1982 start_time := Unix.gettimeofday ();
1983 if !time_limit < 1. then time_limit := 60.;
1985 saturate_equations env goal (fun e -> true) passive active
1989 List.fold_left (fun s e -> EqualitySet.add e s)
1990 EqualitySet.empty equalities
1993 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
1999 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2003 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2005 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2006 (String.concat "\n" (List.map (Equality.string_of_equality ~env) active))
2007 (* (String.concat "\n"
2008 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2009 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2011 (List.map (fun e -> CicPp.ppterm (Equality.term_of_equality e)) passive));
2015 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2019 let demodulate_tac ~dbd ~pattern ((proof,goal)(*s initialstatus*)) =
2020 let module I = Inference in
2021 let curi,metasenv,pbo,pty = proof in
2022 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2023 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2024 let lib_eq_uris, library_equalities, maxm =
2025 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2026 if library_equalities = [] then prerr_endline "VUOTA!!!";
2027 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2028 let library_equalities = List.map snd library_equalities in
2029 let initgoal = [], [], ty in
2030 let env = (metasenv, context, CicUniv.empty_ugraph) in
2031 let equalities = simplify_equalities env (equalities@library_equalities) in
2034 (fun tbl eq -> Indexing.index tbl eq)
2035 Indexing.empty equalities
2037 let changed,(newproof,newmetasenv, newty) =
2038 Indexing.demodulation_goal
2039 (metasenv,context,CicUniv.empty_ugraph) table initgoal
2043 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
2045 Equality.build_goal_proof newproof opengoal ty [] in
2046 let extended_metasenv = (maxm,context,newty)::metasenv in
2047 let extended_status =
2048 (curi,extended_metasenv,pbo,pty),goal in
2049 let (status,newgoals) =
2050 ProofEngineTypes.apply_tactic
2051 (PrimitiveTactics.apply_tac ~term:proofterm)
2053 (status,maxm::newgoals)
2055 else (* if newty = ty then *)
2056 raise (ProofEngineTypes.Fail (lazy "no progress"))
2057 (*else ProofEngineTypes.apply_tactic
2058 (ReductionTactics.simpl_tac ~pattern)
2062 let demodulate_tac ~dbd ~pattern =
2063 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)
2067 <:show<Saturation.>> ^ Indexing.get_stats () ^ Inference.get_stats ();;