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 * int * 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
409 let rec aux b (ok_so_far, subsumption_used) t1 t2 =
411 | t1, t2 when not ok_so_far -> ok_so_far, subsumption_used
412 | t1, t2 when subsumption_used -> t1 = t2, subsumption_used
413 | Cic.Appl (h1::l),Cic.Appl (h2::l') ->
414 let rc = check_subsumed b t1 t2 in
420 (fun (ok_so_far, subsumption_used) t t' ->
421 aux true (ok_so_far, subsumption_used) t t')
422 (ok_so_far, subsumption_used) l l'
423 with Invalid_argument _ -> false,subsumption_used)
425 false, subsumption_used
426 | _ -> false, subsumption_used
428 fst (aux false (true,false) left right)
431 (* buttare via sign *)
433 (** simplifies current using active and passive *)
434 let forward_simplify env (sign,current) ?passive (active_list, active_table) =
435 let _, context, _ = env in
439 | Some ((_, _), pt) -> Some pt
441 let demodulate table current =
442 let newmeta, newcurrent =
443 Indexing.demodulation_equality !maxmeta env table sign current in
445 if Equality.is_identity env newcurrent then
450 let rec demod current =
451 if Utils.debug_metas then
452 ignore (Indexing.check_target context current "demod0");
453 let res = demodulate active_table current in
454 if Utils.debug_metas then
455 ignore ((function None -> () | Some x ->
456 ignore (Indexing.check_target context x "demod1");()) res);
460 match passive_table with
462 | Some passive_table ->
463 match demodulate passive_table newcurrent with
465 | Some newnewcurrent ->
466 if Equality.compare newcurrent newnewcurrent <> 0 then
468 else Some newnewcurrent
470 let res = demod current in
474 if Indexing.in_index active_table c then
477 match passive_table with
479 if check_for_deep_subsumption env active_table c then
484 if Indexing.subsumption env active_table c = None then
489 | Some passive_table ->
490 if Indexing.in_index passive_table c then None
492 if check_for_deep_subsumption env active_table c then
495 (* if Indexing.subsumption env active_table c = None then*)
496 (match Indexing.subsumption env passive_table c with
500 (*prerr_endline "\n\nPESCO DALLE PASSIVE LA PIU' GENERALE\n\n";
508 (** simplifies new using active and passive *)
509 let forward_simplify_new env new_pos ?passive active =
510 if Utils.debug_metas then
514 (fun current -> Indexing.check_target c current "forward new pos")
517 let active_list, active_table = active in
521 | Some ((_, _), pt) -> Some pt
523 let demodulate sign table target =
524 let newmeta, newtarget =
525 Indexing.demodulation_equality !maxmeta env table sign target in
529 (* we could also demodulate using passive. Currently we don't *)
531 List.map (demodulate Positive active_table) new_pos
536 if not (Equality.is_identity env e) then
539 EqualitySet.empty new_pos
541 let new_pos = EqualitySet.elements new_pos_set in
544 match passive_table with
546 (fun e -> (Indexing.subsumption env active_table e = None))
547 | Some passive_table ->
548 (fun e -> ((Indexing.subsumption env active_table e = None) &&
549 (Indexing.subsumption env passive_table e = None)))
552 match passive_table with
554 (fun e -> not (Indexing.in_index active_table e))
555 | Some passive_table ->
557 not ((Indexing.in_index active_table e) ||
558 (Indexing.in_index passive_table e)))
560 List.filter subs (List.filter is_duplicate new_pos)
564 (** simplifies a goal with equalities in active and passive *)
565 let rec simplify_goal env goal ?passive (active_list, active_table) =
569 | Some ((_, _), pt) -> Some pt
571 let demodulate table goal = Indexing.demodulation_goal env table goal in
573 match passive_table with
574 | None -> demodulate active_table goal
575 | Some passive_table ->
576 let changed, goal = demodulate active_table goal in
577 (* let changed', goal = demodulate passive_table goal in*)
578 (changed (*|| changed'*)), goal
584 snd (simplify_goal env goal ?passive (active_list, active_table))
588 let simplify_goals env goals ?passive active =
589 let a_goals, p_goals = goals in
592 (fun g -> snd (simplify_goal env g ?passive active))
597 (fun g -> snd (simplify_goal env g ?passive active))
604 (** simplifies active usign new *)
605 let backward_simplify_active env new_pos new_table min_weight active =
606 let active_list, active_table = active in
607 let active_list, newa, pruned =
609 (fun equality (res, newn,pruned) ->
610 let ew, _, _, _,id = Equality.open_equality equality in
611 if ew < min_weight then
612 equality::res, newn,pruned
614 match forward_simplify env (Utils.Positive, equality) (new_pos, new_table) with
615 | None -> res, newn, id::pruned
617 if Equality.compare equality e = 0 then
620 res, e::newn, pruned)
621 active_list ([], [],[])
624 List.exists (Equality.meta_convertibility_eq eq1) where
627 let _, _, _, _,id = Equality.open_equality eq in id
629 let ((active1,pruned),tbl), newa =
631 (fun eq ((res,pruned), tbl) ->
632 if List.mem eq res then
633 (res, (id_of_eq eq)::pruned),tbl
634 else if (Equality.is_identity env eq) || (find eq res) then (
635 (res, (id_of_eq eq)::pruned),tbl
638 (eq::res,pruned), Indexing.index tbl eq)
639 active_list (([],pruned), Indexing.empty),
642 if (Equality.is_identity env eq) then p
647 | [] -> (active1,tbl), None, pruned
648 | _ -> (active1,tbl), Some newa, pruned
652 (** simplifies passive using new *)
653 let backward_simplify_passive env new_pos new_table min_weight passive =
654 let (pl, ps), passive_table = passive in
655 let f sign equality (resl, ress, newn) =
656 let ew, _, _, _ , _ = Equality.open_equality equality in
657 if ew < min_weight then
658 equality::resl, ress, newn
660 match forward_simplify env (sign, equality) (new_pos, new_table) with
661 | None -> resl, EqualitySet.remove equality ress, newn
664 equality::resl, ress, newn
666 let ress = EqualitySet.remove equality ress in
669 let pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
672 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
675 | [] -> ((pl, ps), passive_table), None
676 | _ -> ((pl, ps), passive_table), Some (newp)
679 let build_table equations =
682 let ew, _, _, _ , _ = Equality.open_equality e in
683 e::l, Indexing.index t e, min ew w)
684 ([], Indexing.empty, 1000000) equations
688 let backward_simplify env new' ?passive active =
689 let new_pos, new_table, min_weight = build_table new' in
693 let ew, _, _, _ , _ = Equality.open_equality e in
694 e::l, Indexing.index t e, min ew w)
695 ([], Indexing.empty, 1000000) new'
698 let active, newa, pruned =
699 backward_simplify_active env new_pos new_table min_weight active in
702 active, (make_passive []), newa, None, pruned
704 active, passive, newa, None, pruned
707 backward_simplify_passive env new_pos new_table min_weight passive in
708 active, passive, newa, newp *)
711 let close env new' given =
712 let new_pos, new_table, min_weight =
715 let ew, _, _, _ , _ = Equality.open_equality e in
716 e::l, Indexing.index t e, min ew w)
717 ([], Indexing.empty, 1000000) (snd new')
721 let pos = infer env c (new_pos,new_table) in
726 let is_commutative_law eq =
727 let w, proof, (eq_ty, left, right, order), metas , _ =
728 Equality.open_equality eq
730 match left,right with
731 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
732 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
733 f1 = f2 && a1 = b2 && a2 = b1
737 let prova env new' active =
738 let given = List.filter is_commutative_law (fst active) in
742 (Printf.sprintf "symmetric:\n%s\n"
745 (fun e -> Equality.string_of_equality ~env e)
750 (* returns an estimation of how many equalities in passive can be activated
751 within the current time limit *)
752 let get_selection_estimate () =
753 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
754 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
756 ceil ((float_of_int !processed_clauses) *.
757 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
761 (** initializes the set of goals *)
762 let make_goals goal =
764 and passive = [0, [goal]] in
768 let make_goal_set goal =
772 (** initializes the set of theorems *)
773 let make_theorems theorems =
778 let activate_goal (active, passive) =
781 | goal_conj::tl -> true, (goal_conj::active, tl)
782 | [] -> false, (active, passive)
784 true, (active,passive)
788 let activate_theorem (active, passive) =
790 | theorem::tl -> true, (theorem::active, tl)
791 | [] -> false, (active, passive)
796 let simplify_theorems env theorems ?passive (active_list, active_table) =
797 let pl, passive_table =
800 | Some ((pn, _), (pp, _), pt) ->
801 let pn = List.map (fun e -> (Negative, e)) pn
802 and pp = List.map (fun e -> (Positive, e)) pp in
805 let a_theorems, p_theorems = theorems in
806 let demodulate table theorem =
807 let newmeta, newthm =
808 Indexing.demodulation_theorem !maxmeta env table theorem in
810 theorem != newthm, newthm
812 let foldfun table (a, p) theorem =
813 let changed, theorem = demodulate table theorem in
814 if changed then (a, theorem::p) else (theorem::a, p)
816 let mapfun table theorem = snd (demodulate table theorem) in
817 match passive_table with
819 let p_theorems = List.map (mapfun active_table) p_theorems in
820 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
821 | Some passive_table ->
822 let p_theorems = List.map (mapfun active_table) p_theorems in
823 let p_theorems, a_theorems =
824 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
825 let p_theorems = List.map (mapfun passive_table) p_theorems in
826 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
830 let rec simpl env e others others_simpl =
831 let active = others @ others_simpl in
834 (fun t e -> Indexing.index t e)
835 Indexing.empty active
837 let res = forward_simplify env (Positive,e) (active, tbl) in
841 | None -> simpl env hd tl others_simpl
842 | Some e -> simpl env hd tl (e::others_simpl)
846 | None -> others_simpl
847 | Some e -> e::others_simpl
851 let simplify_equalities env equalities =
854 (Printf.sprintf "equalities:\n%s\n"
856 (List.map Equality.string_of_equality equalities))));
857 debug_print (lazy "SIMPLYFYING EQUALITIES...");
858 match equalities with
862 List.rev (simpl env hd tl [])
866 (Printf.sprintf "equalities AFTER:\n%s\n"
868 (List.map Equality.string_of_equality res))));
872 let print_goals goals =
879 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
881 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
884 let check_if_goal_is_subsumed ((_,ctx,_) as env) table (goalproof,menv,ty) =
886 let names = names_of_context ctx in
887 Printf.eprintf "check_goal_subsumed: %s\n" (CicPp.pp ty names);
890 | Cic.Appl[Cic.MutInd(uri,_,_);eq_ty;left;right]
891 when UriManager.eq uri (Utils.eq_URI ()) ->
894 (0,Equality.Exact (Cic.Implicit None),(eq_ty,left,right,Eq),menv)
896 (* match Indexing.subsumption env table goal_equation with*)
897 match Indexing.unification env table goal_equation with
898 | Some (subst, equality, swapped ) ->
900 ("GOAL SUBSUMED BY: " ^ Equality.string_of_equality equality);
901 prerr_endline ("SUBST:" ^ Subst.ppsubst subst);
902 let (_,p,(ty,l,r,_),m,id) = Equality.open_equality equality in
903 let cicmenv = Subst.apply_subst_metasenv subst (m @ menv) in
906 Equality.symmetric eq_ty l id uri m
910 Some (goalproof, p, id, subst, cicmenv)
917 (** given-clause algorithm with full reduction strategy *)
918 let rec given_clause_fullred dbd env goals theorems ~passive active =
919 let goals = simplify_goals env goals ~passive active in
920 let _,context,_ = env in
922 (Equality.goal_proof * Cic.metasenv * Cic.term) list *
923 (Equality.goal_proof * Cic.metasenv * Cic.term) list) = activate_goal
926 (Equality.goal_proof * Cic.metasenv * Cic.term) list *
927 (Equality.goal_proof * Cic.metasenv * Cic.term) list)
929 (* let theorems = simplify_theorems env theorems ~passive active in *)
931 let names = List.map (HExtlib.map_option (fun (name,_) -> name)) context in
932 let _, _, t = List.hd (fst goals) in
933 let _ = prerr_endline ("goal activated = " ^ (CicPp.pp t names)) in
937 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
938 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
939 (* let current = List.hd (fst goals) in *)
940 (* let p, _, t = List.hd (snd current) in *)
943 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
944 (* (CicPp.ppterm t) (string_of_proof p))); *)
947 (* apply_goal_to_theorems dbd env theorems ~passive active goals in *)
948 let iseq uri = UriManager.eq uri (Utils.eq_URI ()) in
950 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])::_
951 when left = right && iseq uri ->
952 let reflproof = Equality.Exact (Equality.refl_proof eq_ty left) in
953 true, Some (goalproof, reflproof, 0, Subst.empty_subst,m)
955 (match check_if_goal_is_subsumed env (snd active) goal with
958 prerr_endline "Proof found by subsumption!";
963 ( prerr_endline "esco qui";
965 let s = Printf.sprintf "actives:\n%s\n"
968 (fun (s, e) -> (string_of_sign s) ^ " " ^
969 (string_of_equality ~env e))
971 let sp = Printf.sprintf "passives:\n%s\n"
974 (string_of_equality ~env)
975 (let x,y,_ = passive in (fst x)@(fst y)))) in
979 | None -> assert false
980 | Some p -> ParamodulationSuccess p)
982 given_clause_fullred_aux dbd env goals theorems passive active
984 (* let ok', theorems = activate_theorem theorems in *)
986 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
989 (* match (fst goals) with *)
990 (* | (_, [proof, _, _])::_ -> Some proof *)
991 (* | _ -> assert false *)
993 (* ParamodulationSuccess (proof, env) *)
995 (* given_clause_fullred_aux env goals theorems passive active *)
997 if (passive_is_empty passive) then ParamodulationFailure ""
998 else given_clause_fullred_aux dbd env goals theorems passive active
1000 and given_clause_fullred_aux dbd env goals theorems passive active =
1001 prerr_endline (string_of_int !counter ^
1002 " MAXMETA: " ^ string_of_int !maxmeta ^
1003 " #ACTIVES: " ^ string_of_int (size_of_active active) ^
1004 " #PASSIVES: " ^ string_of_int (size_of_passive passive));
1007 if !counter mod 10 = 0 then
1009 let size = HExtlib.estimate_size (passive,active) in
1010 let sizep = HExtlib.estimate_size (passive) in
1011 let sizea = HExtlib.estimate_size (active) in
1012 let (l1,s1),(l2,s2), t = passive in
1013 let sizetbl = HExtlib.estimate_size t in
1014 let sizel = HExtlib.estimate_size (l1,l2) in
1015 let sizes = HExtlib.estimate_size (s1,s2) in
1017 prerr_endline ("SIZE: " ^ string_of_int size);
1018 prerr_endline ("SIZE P: " ^ string_of_int sizep);
1019 prerr_endline ("SIZE A: " ^ string_of_int sizea);
1020 prerr_endline ("SIZE TBL: " ^ string_of_int sizetbl ^
1021 " SIZE L: " ^ string_of_int sizel ^
1022 " SIZE S:" ^ string_of_int sizes);
1025 if (size_of_active active) mod 50 = 0 then
1026 (let s = Printf.sprintf "actives:\n%s\n"
1029 (fun (s, e) -> (string_of_sign s) ^ " " ^
1030 (string_of_equality ~env e))
1032 let sp = Printf.sprintf "passives:\n%s\n"
1035 (string_of_equality ~env)
1036 (let x,y,_ = passive in (fst x)@(fst y)))) in
1038 prerr_endline sp); *)
1039 let time1 = Unix.gettimeofday () in
1040 let (_,context,_) = env in
1041 let selection_estimate = get_selection_estimate () in
1042 let kept = size_of_passive passive in
1044 if !time_limit = 0. || !processed_clauses = 0 then
1046 else if !elapsed_time > !time_limit then (
1047 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1048 !time_limit !elapsed_time));
1050 ) else if kept > selection_estimate then (
1052 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1053 "(kept: %d, selection_estimate: %d)\n")
1054 kept selection_estimate));
1055 prune_passive selection_estimate active passive
1060 let time2 = Unix.gettimeofday () in
1061 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1063 kept_clauses := (size_of_passive passive) + (size_of_active active);
1064 match passive_is_empty passive with
1065 | true -> ParamodulationFailure ""
1066 (* given_clause_fullred dbd env goals theorems passive active *)
1068 let current, passive = select env goals passive in
1070 ("Selected = " ^ Equality.string_of_equality ~env current);
1072 (let w,p,(t,l,r,o),m = current in
1073 " size w: " ^ string_of_int (HExtlib.estimate_size w)^
1074 " size p: " ^ string_of_int (HExtlib.estimate_size p)^
1075 " size t: " ^ string_of_int (HExtlib.estimate_size t)^
1076 " size l: " ^ string_of_int (HExtlib.estimate_size l)^
1077 " size r: " ^ string_of_int (HExtlib.estimate_size r)^
1078 " size o: " ^ string_of_int (HExtlib.estimate_size o)^
1079 " size m: " ^ string_of_int (HExtlib.estimate_size m)^
1080 " size m-c: " ^ string_of_int
1081 (HExtlib.estimate_size (List.map (fun (x,_,_) -> x) m)))) *)
1082 let time1 = Unix.gettimeofday () in
1083 let res = forward_simplify env (Positive, current) ~passive active in
1084 let time2 = Unix.gettimeofday () in
1085 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1088 (* weight_age_counter := !weight_age_counter + 1; *)
1089 given_clause_fullred dbd env goals theorems passive active
1091 (* prerr_endline (Printf.sprintf "selected simpl: %s"
1092 (Equality.string_of_equality ~env current));*)
1093 let t1 = Unix.gettimeofday () in
1094 let new' = infer env current active in
1098 (Printf.sprintf "new' (senza semplificare):\n%s\n"
1101 (fun e -> "Positive " ^
1102 (Equality.string_of_equality ~env e)) new'))))
1104 let t2 = Unix.gettimeofday () in
1105 infer_time := !infer_time +. (t2 -. t1);
1107 if Equality.is_identity env current then active
1109 let al, tbl = active in
1110 al @ [current], Indexing.index tbl current
1112 let rec simplify new' active passive =
1113 let t1 = Unix.gettimeofday () in
1114 let new' = forward_simplify_new env new'~passive active in
1115 let t2 = Unix.gettimeofday () in
1116 forward_simpl_new_time :=
1117 !forward_simpl_new_time +. (t2 -. t1);
1118 let t1 = Unix.gettimeofday () in
1119 let active, passive, newa, retained, pruned =
1120 backward_simplify env new' ~passive active in
1122 List.fold_left filter_dependent passive pruned in
1123 let t2 = Unix.gettimeofday () in
1124 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1125 match newa, retained with
1126 | None, None -> active, passive, new'
1129 if Utils.debug_metas then
1132 (fun x->Indexing.check_target context x "simplify1")
1135 simplify (new' @ p) active passive
1136 | Some p, Some rp ->
1137 simplify (new' @ p @ rp) active passive
1139 let active, passive, new' = simplify new' active passive in
1141 let a,b,_ = build_table new' in
1142 simplify_goals env goals ~passive (a,b)
1146 let new1 = prova env new' active in
1147 let new' = (fst new') @ (fst new1), (snd new') @ (snd new1) in
1153 (Printf.sprintf "new1:\n%s\n"
1156 (fun e -> "Negative " ^
1157 (string_of_equality ~env e)) neg) @
1159 (fun e -> "Positive " ^
1160 (string_of_equality ~env e)) pos)))))
1163 let k = size_of_passive passive in
1164 if k < (kept - 1) then
1165 processed_clauses := !processed_clauses + (kept - 1 - k);
1170 (Printf.sprintf "active:\n%s\n"
1173 (fun e -> (Equality.string_of_equality ~env e))
1179 (Printf.sprintf "new':\n%s\n"
1182 (fun e -> "Negative " ^
1183 (Equality.string_of_equality ~env e)) new')))))
1185 let passive = add_to_passive passive new' in
1186 given_clause_fullred dbd env goals theorems passive active
1190 let profiler0 = HExtlib.profile "P/Saturation.given_clause_fullred"
1192 let given_clause_fullred dbd env goals theorems passive active =
1193 profiler0.HExtlib.profile
1194 (given_clause_fullred dbd env goals theorems passive) active
1197 let iseq uri = UriManager.eq uri (Utils.eq_URI ());;
1199 let check_if_goal_is_identity env = function
1200 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
1201 when left = right && iseq uri ->
1202 let reflproof = Equality.Exact (Equality.refl_proof eq_ty left) in
1203 Some (goalproof, reflproof, 0, Subst.empty_subst,m)
1204 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
1206 (let _,context,_ = env in
1209 Inference.unification m m context left right CicUniv.empty_ugraph
1211 let reflproof = Equality.Exact (Equality.refl_proof eq_ty left) in
1212 let m = Subst.apply_subst_metasenv s m in
1213 Some (goalproof, reflproof, 0, s,m)
1218 let rec check goal = function
1222 | None -> check goal tl
1223 | (Some p) as ok -> ok
1226 let simplify_goal_set env goals passive active =
1227 let active_goals, passive_goals = goals in
1228 let find (_,_,g) where =
1229 List.exists (fun (_,_,g1) -> Equality.meta_convertibility g g1) where
1234 match simplify_goal env goal ~passive active with
1235 | _, g -> if find g acc then acc else g::acc)
1236 (* active_goals active_goals *)
1239 if List.length active_goals <> List.length simplified then
1240 prerr_endline "SEMPLIFICANDO HO SCARTATO...";
1241 (simplified,passive_goals)
1243 HExtlib.list_uniq ~eq:(fun (_,_,t1) (_,_,t2) -> t1 = t2)
1244 (List.sort (fun (_,_,t1) (_,_,t2) -> compare t1 t1)
1245 ((*goals @*) simplified))
1249 let check_if_goals_set_is_solved env active goals =
1250 let active_goals, passive_goals = goals in
1257 check_if_goal_is_identity env;
1258 check_if_goal_is_subsumed env (snd active)])
1262 let infer_goal_set env active goals =
1263 let active_goals, passive_goals = goals in
1264 let rec aux = function
1266 | ((_,_,t1) as hd)::tl when
1268 (fun (_,_,t) -> Equality.meta_convertibility t t1)
1271 let selected = hd in
1272 let passive_goals = tl in
1273 let new' = Indexing.superposition_left env (snd active) selected in
1274 selected::active_goals, passive_goals @ new'
1280 let infer_goal_set_with_current env current goals =
1281 let active_goals, passive_goals = goals in
1282 let _,table,_ = build_table [current] in
1283 let _,_,_,_,id = Equality.open_equality current in
1287 let new' = Indexing.superposition_left env table g in
1290 prerr_endline "XXXXXXX";
1291 List.iter (fun _,_,e -> prerr_endline (CicPp.ppterm e)) new' ;
1294 passive_goals active_goals
1299 let size_of_goal_set_a (l,_) = List.length l;;
1300 let size_of_goal_set_p (_,l) = List.length l;;
1302 (** given-clause algorithm with full reduction strategy: NEW implementation *)
1303 (* here goals is a set of goals in OR *)
1305 ((_,context,_) as env) goals theorems passive active max_iterations max_time
1307 let names = names_of_context context in
1308 let initial_time = Unix.gettimeofday () in
1309 let iterations_left iterno =
1310 let now = Unix.gettimeofday () in
1311 let time_left = max_time -. now in
1312 let time_spent_until_now = now -. initial_time in
1313 let iteration_medium_cost =
1314 time_spent_until_now /. (float_of_int iterno)
1316 let iterations_left = time_left /. iteration_medium_cost in
1317 int_of_float iterations_left
1319 let rec step goals theorems passive active iterno =
1320 if iterno > max_iterations then
1321 (ParamodulationFailure "No more iterations to spend")
1322 else if Unix.gettimeofday () > max_time then
1323 (ParamodulationFailure "No more time to spend")
1325 let _ = prerr_endline "simpl goal with active" in
1326 let _ = <:start<simplify goal set active>> in
1327 let goals = simplify_goal_set env goals passive active in
1328 let _ = <:stop<simplify goal set active>> in
1329 match check_if_goals_set_is_solved env active goals with
1332 (Printf.sprintf "Found a proof in: %f\n"
1333 (Unix.gettimeofday() -. initial_time));
1335 ParamodulationSuccess p
1338 (Printf.sprintf "%d #ACTIVES: %d #PASSIVES: %d #GOALSET: %d(%d)\n"
1339 iterno (size_of_active active) (size_of_passive passive)
1340 (size_of_goal_set_a goals) (size_of_goal_set_p goals));
1341 (* PRUNING OF PASSIVE THAT WILL NEVER BE PROCESSED *)
1343 let selection_estimate = iterations_left iterno in
1344 let kept = size_of_passive passive in
1345 if kept > selection_estimate then
1347 (*Printf.eprintf "Too many passive equalities: pruning...";
1348 prune_passive selection_estimate active*) passive
1353 kept_clauses := (size_of_passive passive) + (size_of_active active);
1355 if passive_is_empty passive then
1356 ParamodulationFailure "No more passive"(*maybe this is a success! *)
1359 let goals = infer_goal_set env active goals in
1360 let goals = infer_goal_set env active goals in
1361 let goals = infer_goal_set env active goals in
1362 let current, passive = select env goals passive in
1363 let _,_,goaltype = List.hd (fst goals) in
1364 prerr_endline (Printf.sprintf "Current goal = %s\n"
1365 (CicPp.pp goaltype names));
1366 prerr_endline (Printf.sprintf "Selected = %s\n"
1367 (Equality.string_of_equality ~env current));
1368 (* SIMPLIFICATION OF CURRENT *)
1370 forward_simplify env (Positive, current) (*~passive*) active
1373 | None -> step goals theorems passive active (iterno+1)
1375 (* GENERATION OF NEW EQUATIONS *)
1376 prerr_endline "infer";
1377 let new' = infer env current active in
1378 prerr_endline "infer goal";
1379 let goals = infer_goal_set_with_current env current goals in
1381 let al, tbl = active in
1382 al @ [current], Indexing.index tbl current
1384 (* FORWARD AND BACKWARD SIMPLIFICATION *)
1385 prerr_endline "fwd/back simpl";
1386 let rec simplify new' active passive =
1387 let new' = forward_simplify_new env new' ~passive active in
1388 let active, passive, newa, retained, pruned =
1389 backward_simplify env new' ~passive active
1392 List.fold_left filter_dependent passive pruned
1394 match newa, retained with
1395 | None, None -> active, passive, new'
1397 | None, Some p -> simplify (new' @ p) active passive
1398 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1400 let active, passive, new' = simplify new' active passive in
1401 if iterno = 36 || iterno = 654 then
1403 prerr_endline "...................";
1405 (fun x -> prerr_endline (Equality.string_of_equality
1407 prerr_endline "FINE...................";
1409 prerr_endline "simpl goal with new";
1411 let a,b,_ = build_table new' in
1412 let _ = <:start<simplify_goal_set new>> in
1413 <:stop<simplify_goal_set new
1414 simplify_goal_set env goals passive (a,b)
1417 let passive = add_to_passive passive new' in
1418 step goals theorems passive active (iterno+1)
1421 step goals theorems passive active 1
1424 let rec saturate_equations env goal accept_fun passive active =
1425 elapsed_time := Unix.gettimeofday () -. !start_time;
1426 if !elapsed_time > !time_limit then
1429 let current, passive = select env ([goal],[]) passive in
1430 let res = forward_simplify env (Positive, current) ~passive active in
1433 saturate_equations env goal accept_fun passive active
1435 debug_print (lazy (Printf.sprintf "selected: %s"
1436 (Equality.string_of_equality ~env current)));
1437 let new' = infer env current active in
1439 if Equality.is_identity env current then active
1441 let al, tbl = active in
1442 al @ [current], Indexing.index tbl current
1444 let rec simplify new' active passive =
1445 let new' = forward_simplify_new env new' ~passive active in
1446 let active, passive, newa, retained, pruned =
1447 backward_simplify env new' ~passive active in
1449 List.fold_left filter_dependent passive pruned in
1450 match newa, retained with
1451 | None, None -> active, passive, new'
1453 | None, Some p -> simplify (new' @ p) active passive
1454 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1456 let active, passive, new' = simplify new' active passive in
1460 (Printf.sprintf "active:\n%s\n"
1463 (fun e -> Equality.string_of_equality ~env e)
1469 (Printf.sprintf "new':\n%s\n"
1472 (fun e -> "Negative " ^
1473 (Equality.string_of_equality ~env e)) new'))))
1475 let new' = List.filter accept_fun new' in
1476 let passive = add_to_passive passive new' in
1477 saturate_equations env goal accept_fun passive active
1480 let main dbd full term metasenv ugraph = ()
1482 let main dbd full term metasenv ugraph =
1483 let module C = Cic in
1484 let module T = CicTypeChecker in
1485 let module PET = ProofEngineTypes in
1486 let module PP = CicPp in
1487 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1488 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1489 let proof, goals = status in
1490 let goal' = List.nth goals 0 in
1491 let _, metasenv, meta_proof, _ = proof in
1492 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1493 let eq_indexes, equalities, maxm = find_equalities context proof in
1494 let lib_eq_uris, library_equalities, maxm =
1496 find_library_equalities dbd context (proof, goal') (maxm+2)
1498 let library_equalities = List.map snd library_equalities in
1499 maxmeta := maxm+2; (* TODO ugly!! *)
1500 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1501 let new_meta_goal, metasenv, type_of_goal =
1502 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1505 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1506 Cic.Meta (maxm+1, irl),
1507 (maxm+1, context, ty)::metasenv,
1510 let env = (metasenv, context, ugraph) in
1511 let t1 = Unix.gettimeofday () in
1514 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1515 let context_hyp = find_context_hypotheses env eq_indexes in
1516 context_hyp @ theorems, []
1519 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1520 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1522 let t = CicUtil.term_of_uri refl_equal in
1523 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1526 let t2 = Unix.gettimeofday () in
1529 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1534 "Theorems:\n-------------------------------------\n%s\n"
1539 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1544 ([],Equality.BasicProof (Equality.empty_subst ,new_meta_goal)), [], goal
1546 let equalities = simplify_equalities env
1547 (equalities@library_equalities) in
1548 let active = make_active () in
1549 let passive = make_passive equalities in
1550 Printf.printf "\ncurrent goal: %s\n"
1551 (let _, _, g = goal in CicPp.ppterm g);
1552 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1553 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1554 Printf.printf "\nequalities:\n%s\n"
1557 (Equality.string_of_equality ~env) equalities));
1558 (* (equalities @ library_equalities))); *)
1559 print_endline "--------------------------------------------------";
1560 let start = Unix.gettimeofday () in
1561 print_endline "GO!";
1562 start_time := Unix.gettimeofday ();
1564 let goals = make_goals goal in
1565 (if !use_fullred then given_clause_fullred else given_clause_fullred)
1566 dbd env goals theorems passive active
1568 let finish = Unix.gettimeofday () in
1571 | ParamodulationFailure ->
1572 Printf.printf "NO proof found! :-(\n\n"
1573 | ParamodulationSuccess (Some ((cicproof,cicmenv),(proof, env))) ->
1574 Printf.printf "OK, found a proof!\n";
1575 let oldproof = Equation.build_proof_term proof in
1576 let newproof,_,newenv,_ =
1577 CicRefine.type_of_aux'
1578 cicmenv context cicproof CicUniv.empty_ugraph
1580 (* REMEMBER: we have to instantiate meta_proof, we should use
1581 apply the "apply" tactic to proof and status
1583 let names = names_of_context context in
1584 prerr_endline "OLD PROOF";
1585 print_endline (PP.pp proof names);
1586 prerr_endline "NEW PROOF";
1587 print_endline (PP.pp newproof names);
1591 let (_, _, _, menv,_) = Equality.open_equality eq in
1598 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1600 print_endline (string_of_float (finish -. start));
1602 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1603 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1605 (fst (CicReduction.are_convertible
1606 context type_of_goal ty ug)));
1608 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1609 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1610 print_endline (string_of_float (finish -. start));*)
1614 | ParamodulationSuccess None ->
1615 Printf.printf "Success, but no proof?!?\n\n"
1620 ((Printf.sprintf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1621 "forward_simpl_new_time: %.9f\n" ^^
1622 "backward_simpl_time: %.9f\n")
1623 !infer_time !forward_simpl_time !forward_simpl_new_time
1624 !backward_simpl_time) ^
1625 (Printf.sprintf "passive_maintainance_time: %.9f\n"
1626 !passive_maintainance_time) ^
1627 (Printf.sprintf " successful unification/matching time: %.9f\n"
1628 !Indexing.match_unif_time_ok) ^
1629 (Printf.sprintf " failed unification/matching time: %.9f\n"
1630 !Indexing.match_unif_time_no) ^
1631 (Printf.sprintf " indexing retrieval time: %.9f\n"
1632 !Indexing.indexing_retrieval_time) ^
1633 (Printf.sprintf " demodulate_term.build_newtarget_time: %.9f\n"
1634 !Indexing.build_newtarget_time) ^
1635 (Printf.sprintf "derived %d clauses, kept %d clauses.\n"
1636 !derived_clauses !kept_clauses))
1640 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1646 let default_depth = !maxdepth
1647 and default_width = !maxwidth;;
1651 symbols_counter := 0;
1652 weight_age_counter := !weight_age_ratio;
1653 processed_clauses := 0;
1656 maximal_retained_equality := None;
1658 forward_simpl_time := 0.;
1659 forward_simpl_new_time := 0.;
1660 backward_simpl_time := 0.;
1661 passive_maintainance_time := 0.;
1662 derived_clauses := 0;
1668 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1669 let module C = Cic in
1671 Indexing.init_index ();
1675 (* CicUnification.unif_ty := false;*)
1676 let proof, goalno = status in
1677 let uri, metasenv, meta_proof, term_to_prove = proof in
1678 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1679 let names = names_of_context context in
1680 let eq_indexes, equalities, maxm = find_equalities context proof in
1681 let ugraph = CicUniv.empty_ugraph in
1682 let env = (metasenv, context, ugraph) in
1683 let goal = [], List.filter (fun (i,_,_)->i<>goalno) metasenv, type_of_goal in
1685 let t1 = Unix.gettimeofday () in
1686 let lib_eq_uris, library_equalities, maxm =
1687 find_library_equalities dbd context (proof, goalno) (maxm+2)
1689 let library_equalities = List.map snd library_equalities in
1690 let t2 = Unix.gettimeofday () in
1692 let equalities = simplify_equalities env (equalities@library_equalities) in
1695 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
1696 let t1 = Unix.gettimeofday () in
1699 let thms = find_library_theorems dbd env (proof, goalno) lib_eq_uris in
1700 let context_hyp = find_context_hypotheses env eq_indexes in
1701 context_hyp @ thms, []
1704 let us = UriManager.string_of_uri (Utils.eq_URI ()) in
1705 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1707 let t = CicUtil.term_of_uri refl_equal in
1708 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1711 let t2 = Unix.gettimeofday () in
1716 "Theorems:\n-------------------------------------\n%s\n"
1721 "Term: %s, type: %s"
1722 (CicPp.ppterm t) (CicPp.ppterm ty))
1726 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1728 let active = make_active () in
1729 let passive = make_passive equalities in
1730 let start = Unix.gettimeofday () in
1733 let goals = make_goals goal in
1734 given_clause_fullred dbd env goals theorems passive active
1736 let goals = make_goal_set goal in
1737 let max_iterations = 10000 in
1738 let max_time = Unix.gettimeofday () +. 300. (* minutes *) in
1739 given_clause env goals theorems passive active max_iterations max_time
1741 let finish = Unix.gettimeofday () in
1742 (res, finish -. start)
1745 | ParamodulationFailure s ->
1746 raise (ProofEngineTypes.Fail (lazy ("NO proof found: " ^ s)))
1747 | ParamodulationSuccess
1748 (goalproof,newproof,subsumption_id,subsumption_subst, proof_menv) ->
1749 prerr_endline "OK, found a proof!";
1751 (Equality.pp_proof names goalproof newproof subsumption_subst
1752 subsumption_id type_of_goal);
1753 prerr_endline (CicMetaSubst.ppmetasenv [] proof_menv);
1754 prerr_endline "ENDOFPROOFS";
1755 (* generation of the CIC proof *)
1757 List.filter (fun i -> i <> goalno)
1758 (ProofEngineHelpers.compare_metasenvs
1759 ~newmetasenv:metasenv ~oldmetasenv:proof_menv)
1761 let goal_proof, side_effects_t =
1762 let initial = Equality.add_subst subsumption_subst newproof in
1763 Equality.build_goal_proof goalproof initial type_of_goal side_effects
1765 let goal_proof = Subst.apply_subst subsumption_subst goal_proof in
1766 let metas_still_open_in_proof = Utils.metas_of_term goal_proof in
1767 (*prerr_endline (CicPp.pp goal_proof names);*)
1769 let goal_proof = (* Subst.apply_subst subsumption_subst *) goal_proof in
1770 let side_effects_t =
1771 List.map (Subst.apply_subst subsumption_subst) side_effects_t
1773 (* replacing fake mets with real ones *)
1774 prerr_endline "replacing metas...";
1775 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1776 let goal_proof_menv, what, with_what,free_meta =
1778 (fun (acc1,acc2,acc3,uniq) (i,_,ty) ->
1781 acc1, (Cic.Meta(i,[]))::acc2, m::acc3, uniq
1783 [i,context,ty], (Cic.Meta(i,[]))::acc2,
1784 (Cic.Meta(i,irl)) ::acc3,Some (Cic.Meta(i,irl)))
1787 (fun (i,_,_) -> List.mem i metas_still_open_in_proof)
1791 (* we need this fake equality since the metas of the hypothesis may be
1792 * with a real local context *)
1793 ProofEngineReduction.replace_lifting
1794 ~equality:(fun x y ->
1795 match x,y with Cic.Meta(i,_),Cic.Meta(j,_) -> i=j | _-> false)
1796 ~what ~with_what ~where
1798 let goal_proof = replace goal_proof in
1799 (* ok per le meta libere... ma per quelle che c'erano e sono rimaste?
1800 * what mi pare buono, sostituisce solo le meta farlocche *)
1801 let side_effects_t = List.map replace side_effects_t in
1803 List.filter (fun i -> i <> goalno)
1804 (ProofEngineHelpers.compare_metasenvs
1805 ~oldmetasenv:metasenv ~newmetasenv:goal_proof_menv)
1807 prerr_endline ("freemetas: " ^ String.concat "," (List.map string_of_int free_metas) );
1808 (* check/refine/... build the new proof *)
1810 ProofEngineReduction.replace
1811 ~what:side_effects ~with_what:side_effects_t
1812 ~equality:(fun i t -> match t with Cic.Meta(j,_)->j=i|_->false)
1815 let subst_side_effects,real_menv,_ =
1816 let fail t s = raise (ProofEngineTypes.Fail (lazy (t^Lazy.force s))) in
1817 let free_metas_menv =
1818 List.map (fun i -> CicUtil.lookup_meta i goal_proof_menv) free_metas
1821 CicUnification.fo_unif_subst [] context (metasenv @ free_metas_menv)
1822 replaced_goal type_of_goal CicUniv.empty_ugraph
1824 | CicUnification.UnificationFailure s
1825 | CicUnification.Uncertain s
1826 | CicUnification.AssertFailure s ->
1827 fail "Maybe the local context of metas in the goal was not an IRL" s
1830 (goalno,(context,goal_proof,type_of_goal))::subst_side_effects
1832 prerr_endline ("MENVreal_menv: " ^ CicMetaSubst.ppmetasenv [] real_menv);
1835 CicTypeChecker.type_of_aux' real_menv context goal_proof
1836 CicUniv.empty_ugraph
1838 | CicUtil.Meta_not_found _
1839 | CicTypeChecker.TypeCheckerFailure _
1840 | CicTypeChecker.AssertFailure _
1841 | Invalid_argument "list_fold_left2" as exn ->
1842 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1843 prerr_endline (CicPp.pp goal_proof names);
1844 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1847 let proof, real_metasenv =
1848 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1849 proof goalno (CicMetaSubst.apply_subst final_subst) real_menv
1852 match free_meta with Some(Cic.Meta(m,_)) when m<>goalno ->[m] | _ ->[]
1855 "GOALS APERTI: %s\nMETASENV PRIMA:\n%s\nMETASENV DOPO:\n%s\n"
1856 (String.concat ", " (List.map string_of_int open_goals))
1857 (CicMetaSubst.ppmetasenv [] metasenv)
1858 (CicMetaSubst.ppmetasenv [] real_metasenv);
1859 prerr_endline (Printf.sprintf "\nTIME NEEDED: %8.2f" time);
1863 let retrieve_and_print dbd term metasenv ugraph =
1864 let module C = Cic in
1865 let module T = CicTypeChecker in
1866 let module PET = ProofEngineTypes in
1867 let module PP = CicPp in
1868 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1869 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1870 let proof, goals = status in
1871 let goal' = List.nth goals 0 in
1872 let uri, metasenv, meta_proof, term_to_prove = proof in
1873 let _, context, type_of_goal = CicUtil.lookup_meta goal' metasenv in
1874 let eq_indexes, equalities, maxm = find_equalities context proof in
1875 let ugraph = CicUniv.empty_ugraph in
1876 let env = (metasenv, context, ugraph) in
1877 let t1 = Unix.gettimeofday () in
1878 let lib_eq_uris, library_equalities, maxm =
1879 find_library_equalities dbd context (proof, goal') (maxm+2) in
1880 let t2 = Unix.gettimeofday () in
1882 let equalities = (* equalities @ *) library_equalities in
1885 (Printf.sprintf "\n\nequalities:\n%s\n"
1889 (* Printf.sprintf "%s: %s" *)
1890 (UriManager.string_of_uri u)
1891 (* (string_of_equality e) *)
1894 debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
1895 let rec simpl e others others_simpl =
1897 let active = List.map (fun (u, e) -> (Positive, e))
1898 (others @ others_simpl) in
1901 (fun t (_, e) -> Indexing.index t e)
1902 Indexing.empty active
1904 let res = forward_simplify env (Positive, e) (active, tbl) in
1908 | None -> simpl hd tl others_simpl
1909 | Some e -> simpl hd tl ((u, e)::others_simpl)
1913 | None -> others_simpl
1914 | Some e -> (u, e)::others_simpl
1918 match equalities with
1921 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
1923 List.rev (simpl (*(Positive,*) hd others [])
1927 (Printf.sprintf "\nequalities AFTER:\n%s\n"
1931 Printf.sprintf "%s: %s"
1932 (UriManager.string_of_uri u)
1933 (Equality.string_of_equality e)
1939 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
1943 let main_demod_equalities dbd term metasenv ugraph =
1944 let module C = Cic in
1945 let module T = CicTypeChecker in
1946 let module PET = ProofEngineTypes in
1947 let module PP = CicPp in
1948 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1949 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1950 let proof, goals = status in
1951 let goal' = List.nth goals 0 in
1952 let _, metasenv, meta_proof, _ = proof in
1953 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1954 let eq_indexes, equalities, maxm = find_equalities context proof in
1955 let lib_eq_uris, library_equalities, maxm =
1956 find_library_equalities dbd context (proof, goal') (maxm+2)
1958 let library_equalities = List.map snd library_equalities in
1959 maxmeta := maxm+2; (* TODO ugly!! *)
1960 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1961 let new_meta_goal, metasenv, type_of_goal =
1962 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1965 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
1966 (CicPp.ppterm ty)));
1967 Cic.Meta (maxm+1, irl),
1968 (maxm+1, context, ty)::metasenv,
1971 let env = (metasenv, context, ugraph) in
1973 let goal = [], [], goal
1975 let equalities = simplify_equalities env (equalities@library_equalities) in
1976 let active = make_active () in
1977 let passive = make_passive equalities in
1978 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1979 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1980 Printf.printf "\nequalities:\n%s\n"
1983 (Equality.string_of_equality ~env) equalities));
1984 print_endline "--------------------------------------------------";
1985 print_endline "GO!";
1986 start_time := Unix.gettimeofday ();
1987 if !time_limit < 1. then time_limit := 60.;
1989 saturate_equations env goal (fun e -> true) passive active
1993 List.fold_left (fun s e -> EqualitySet.add e s)
1994 EqualitySet.empty equalities
1997 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2003 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2007 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2009 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2010 (String.concat "\n" (List.map (Equality.string_of_equality ~env) active))
2011 (* (String.concat "\n"
2012 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2013 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2015 (List.map (fun e -> CicPp.ppterm (Equality.term_of_equality e)) passive));
2019 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2023 let demodulate_tac ~dbd ~pattern ((proof,goal)(*s initialstatus*)) =
2024 let module I = Inference in
2025 let curi,metasenv,pbo,pty = proof in
2026 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2027 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2028 let lib_eq_uris, library_equalities, maxm =
2029 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2030 if library_equalities = [] then prerr_endline "VUOTA!!!";
2031 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2032 let library_equalities = List.map snd library_equalities in
2033 let initgoal = [], [], ty in
2034 let env = (metasenv, context, CicUniv.empty_ugraph) in
2035 let equalities = simplify_equalities env (equalities@library_equalities) in
2038 (fun tbl eq -> Indexing.index tbl eq)
2039 Indexing.empty equalities
2041 let changed,(newproof,newmetasenv, newty) =
2042 Indexing.demodulation_goal
2043 (metasenv,context,CicUniv.empty_ugraph) table initgoal
2047 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
2049 Equality.build_goal_proof newproof opengoal ty [] in
2050 let extended_metasenv = (maxm,context,newty)::metasenv in
2051 let extended_status =
2052 (curi,extended_metasenv,pbo,pty),goal in
2053 let (status,newgoals) =
2054 ProofEngineTypes.apply_tactic
2055 (PrimitiveTactics.apply_tac ~term:proofterm)
2057 (status,maxm::newgoals)
2059 else (* if newty = ty then *)
2060 raise (ProofEngineTypes.Fail (lazy "no progress"))
2061 (*else ProofEngineTypes.apply_tactic
2062 (ReductionTactics.simpl_tac ~pattern)
2066 let demodulate_tac ~dbd ~pattern =
2067 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)
2071 <:show<Saturation.>> ^ Indexing.get_stats () ^ Inference.get_stats ();;