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;
217 let w1,_,_,_,_ = Equality.open_equality e1 in
218 let w2,_,_,_,_ = Equality.open_equality e2 in
219 if w1 < w2 then e1 else e2
221 let rec my_min_elt min = function
223 | hd::tl -> my_min_elt (my_min hd min) tl
225 (* let current = EqualitySet.min_elt pos_set in *)
226 let current = my_min_elt (List.hd pos_list) (List.tl pos_list) in
228 Indexing.remove_index passive_table current
231 ((remove current pos_list, EqualitySet.remove current pos_set),
235 let filter_dependent passive id =
236 prerr_endline ("+++++++++++++++passives "^
237 ( string_of_int (size_of_passive passive)));
238 let (pos_list, pos_set), passive_table = passive in
241 (fun eq ((list,set),table) ->
242 if Equality.depend eq id then
243 (let _,_,_,_,id_eq = Equality.open_equality eq in
245 prerr_endline ("\n\n--------filtering "^(string_of_int id_eq));
247 EqualitySet.remove eq set),
248 Indexing.remove_index table eq))
250 ((eq::list, set),table))
251 pos_list (([],pos_set),passive_table) in
252 prerr_endline ("+++++++++++++++passives "^
253 ( string_of_int (size_of_passive passive)));
258 (* initializes the passive set of equalities *)
259 let make_passive pos =
260 let set_of equalities =
261 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
264 List.fold_left (fun tbl e -> Indexing.index tbl e) Indexing.empty pos
276 (* adds to passive a list of equalities new_pos *)
277 let add_to_passive passive new_pos =
278 let (pos_list, pos_set), table = passive in
279 let ok set equality = not (EqualitySet.mem equality set) in
280 let pos = List.filter (ok pos_set) new_pos in
282 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
284 let add set equalities =
285 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
287 (pos_list @ pos, add pos_set pos),
292 (* removes from passive equalities that are estimated impossible to activate
293 within the current time limit *)
294 let prune_passive howmany (active, _) passive =
295 let (pl, ps), tbl = passive in
296 let howmany = float_of_int howmany
297 and ratio = float_of_int !weight_age_ratio in
300 int_of_float (if t -. v < 0.5 then t else v)
302 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
303 and in_age = round (howmany /. (ratio +. 1.)) in
305 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
306 let counter = ref !symbols_ratio in
311 counter := !counter - 1;
312 if !counter = 0 then counter := !symbols_ratio in
313 let e = EqualitySet.min_elt ps in
314 let ps' = pickw (w-1) (EqualitySet.remove e ps) in
315 EqualitySet.add e ps'
317 let e = EqualitySet.min_elt ps in
318 let ps' = pickw (w-1) (EqualitySet.remove e ps) in
319 EqualitySet.add e ps'
323 let ps = pickw in_weight ps in
324 let rec picka w s l =
328 | hd::tl when not (EqualitySet.mem hd s) ->
329 let w, s, l = picka (w-1) s tl in
330 w, EqualitySet.add hd s, hd::l
332 let w, s, l = picka w s tl in
337 let _, ps, pl = picka in_age ps pl in
338 if not (EqualitySet.is_empty ps) then
339 maximal_retained_equality := Some (EqualitySet.max_elt ps);
342 (fun e tbl -> Indexing.index tbl e) ps Indexing.empty
348 (** inference of new equalities between current and some in active *)
349 let infer eq_uri env current (active_list, active_table) =
351 if Utils.debug_metas then
352 (ignore(Indexing.check_target c current "infer1");
353 ignore(List.map (function current -> Indexing.check_target c current "infer2") active_list));
355 let maxm, copy_of_current = Equality.fix_metas !maxmeta current in
357 let active_table = Indexing.index active_table copy_of_current in
358 let _ = <:start<current contro active>> in
360 Indexing.superposition_right eq_uri !maxmeta env active_table current
362 let _ = <:stop<current contro active>> in
363 if Utils.debug_metas then
366 Indexing.check_target c current "sup0") res);
368 let rec infer_positive table = function
372 Indexing.superposition_right
373 ~subterms_only:true eq_uri !maxmeta env table equality
376 if Utils.debug_metas then
380 Indexing.check_target c current "sup2") res);
381 let pos = infer_positive table tl in
385 let maxm, copy_of_current = Equality.fix_metas !maxmeta current in
388 let curr_table = Indexing.index Indexing.empty current in
389 let _ = <:start<active contro current>> in
390 let pos = infer_positive curr_table ((*copy_of_current::*)active_list) in
391 let _ = <:stop<active contro current>> in
392 if Utils.debug_metas then
395 Indexing.check_target c current "sup3") pos);
398 derived_clauses := !derived_clauses + (List.length new_pos);
399 match !maximal_retained_equality with
402 ignore(assert false);
403 (* if we have a maximal_retained_equality, we can discard all equalities
404 "greater" than it, as they will never be reached... An equality is
405 greater than maximal_retained_equality if it is bigger
406 wrt. OrderedEquality.compare and it is less similar than
407 maximal_retained_equality to the current goal *)
408 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
411 let check_for_deep_subsumption env active_table eq =
412 let _,_,(eq_ty, left, right, order),metas,id = Equality.open_equality eq in
413 let check_subsumed deep l r =
415 Equality.mk_tmp_equality(0,(eq_ty,l,r,Utils.Incomparable),metas)in
416 match Indexing.subsumption env active_table eqtmp with
420 let rec aux b (ok_so_far, subsumption_used) t1 t2 =
422 | t1, t2 when not ok_so_far -> ok_so_far, subsumption_used
423 | t1, t2 when subsumption_used -> t1 = t2, subsumption_used
424 | Cic.Appl (h1::l),Cic.Appl (h2::l') ->
425 let rc = check_subsumed b t1 t2 in
431 (fun (ok_so_far, subsumption_used) t t' ->
432 aux true (ok_so_far, subsumption_used) t t')
433 (ok_so_far, subsumption_used) l l'
434 with Invalid_argument _ -> false,subsumption_used)
436 false, subsumption_used
437 | _ -> false, subsumption_used
439 fst (aux false (true,false) left right)
442 (* buttare via sign *)
444 (** simplifies current using active and passive *)
446 eq_uri env (sign,current) ?passive (active_list, active_table)
448 let _, context, _ = env in
452 | Some ((_, _), pt) -> Some pt
454 let demodulate table current =
455 let newmeta, newcurrent =
456 Indexing.demodulation_equality eq_uri !maxmeta env table sign current in
458 if Equality.is_identity env newcurrent then
463 let rec demod current =
464 if Utils.debug_metas then
465 ignore (Indexing.check_target context current "demod0");
466 let res = demodulate active_table current in
467 if Utils.debug_metas then
468 ignore ((function None -> () | Some x ->
469 ignore (Indexing.check_target context x "demod1");()) res);
473 match passive_table with
475 | Some passive_table ->
476 match demodulate passive_table newcurrent with
478 | Some newnewcurrent ->
479 if Equality.compare newcurrent newnewcurrent <> 0 then
481 else Some newnewcurrent
483 let res = demod current in
487 if Indexing.in_index active_table c then
490 match passive_table with
492 if check_for_deep_subsumption env active_table c then
497 if Indexing.subsumption env active_table c = None then
502 | Some passive_table ->
503 if Indexing.in_index passive_table c then None
505 if check_for_deep_subsumption env active_table c then
508 (* if Indexing.subsumption env active_table c = None then*)
509 (match Indexing.subsumption env passive_table c with
513 (*prerr_endline "\n\nPESCO DALLE PASSIVE LA PIU' GENERALE\n\n";
521 (** simplifies new using active and passive *)
522 let forward_simplify_new eq_uri env new_pos ?passive active =
523 if Utils.debug_metas then
527 (fun current -> Indexing.check_target c current "forward new pos")
530 let active_list, active_table = active in
534 | Some ((_, _), pt) -> Some pt
536 let demodulate sign table target =
537 let newmeta, newtarget =
538 Indexing.demodulation_equality eq_uri !maxmeta env table sign target
543 (* we could also demodulate using passive. Currently we don't *)
545 List.map (demodulate Positive active_table) new_pos
550 if not (Equality.is_identity env e) then
553 EqualitySet.empty new_pos
555 let new_pos = EqualitySet.elements new_pos_set in
558 match passive_table with
560 (fun e -> (Indexing.subsumption env active_table e = None))
561 | Some passive_table ->
562 (fun e -> ((Indexing.subsumption env active_table e = None) &&
563 (Indexing.subsumption env passive_table e = None)))
566 match passive_table with
568 (fun e -> not (Indexing.in_index active_table e))
569 | Some passive_table ->
571 not ((Indexing.in_index active_table e) ||
572 (Indexing.in_index passive_table e)))
574 List.filter subs (List.filter is_duplicate new_pos)
578 (** simplifies a goal with equalities in active and passive *)
579 let rec simplify_goal env goal ?passive (active_list, active_table) =
583 | Some ((_, _), pt) -> Some pt
585 let demodulate table goal = Indexing.demodulation_goal env table goal in
587 match passive_table with
588 | None -> demodulate active_table goal
589 | Some passive_table ->
590 let changed, goal = demodulate active_table goal in
591 (* let changed', goal = demodulate passive_table goal in*)
592 (changed (*|| changed'*)), goal
598 snd (simplify_goal env goal ?passive (active_list, active_table))
602 let simplify_goals env goals ?passive active =
603 let a_goals, p_goals = goals in
606 (fun g -> snd (simplify_goal env g ?passive active))
611 (fun g -> snd (simplify_goal env g ?passive active))
618 (** simplifies active usign new *)
619 let backward_simplify_active eq_uri env new_pos new_table min_weight active =
620 let active_list, active_table = active in
621 let active_list, newa, pruned =
623 (fun equality (res, newn,pruned) ->
624 let ew, _, _, _,id = Equality.open_equality equality in
625 if ew < min_weight then
626 equality::res, newn,pruned
630 eq_uri env (Utils.Positive, equality) (new_pos, new_table)
632 | None -> res, newn, id::pruned
634 if Equality.compare equality e = 0 then
637 res, e::newn, pruned)
638 active_list ([], [],[])
641 List.exists (Equality.meta_convertibility_eq eq1) where
644 let _, _, _, _,id = Equality.open_equality eq in id
646 let ((active1,pruned),tbl), newa =
648 (fun eq ((res,pruned), tbl) ->
649 if List.mem eq res then
650 (res, (id_of_eq eq)::pruned),tbl
651 else if (Equality.is_identity env eq) || (find eq res) then (
652 (res, (id_of_eq eq)::pruned),tbl
655 (eq::res,pruned), Indexing.index tbl eq)
656 active_list (([],pruned), Indexing.empty),
659 if (Equality.is_identity env eq) then p
664 | [] -> (active1,tbl), None, pruned
665 | _ -> (active1,tbl), Some newa, pruned
669 (** simplifies passive using new *)
670 let backward_simplify_passive eq_uri env new_pos new_table min_weight passive =
671 let (pl, ps), passive_table = passive in
672 let f sign equality (resl, ress, newn) =
673 let ew, _, _, _ , _ = Equality.open_equality equality in
674 if ew < min_weight then
675 equality::resl, ress, newn
678 forward_simplify eq_uri env (sign, equality) (new_pos, new_table)
680 | None -> resl, EqualitySet.remove equality ress, newn
683 equality::resl, ress, newn
685 let ress = EqualitySet.remove equality ress in
688 let pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
691 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
694 | [] -> ((pl, ps), passive_table), None
695 | _ -> ((pl, ps), passive_table), Some (newp)
698 let build_table equations =
701 let ew, _, _, _ , _ = Equality.open_equality e in
702 e::l, Indexing.index t e, min ew w)
703 ([], Indexing.empty, 1000000) equations
707 let backward_simplify eq_uri env new' ?passive active =
708 let new_pos, new_table, min_weight = build_table new' in
712 let ew, _, _, _ , _ = Equality.open_equality e in
713 e::l, Indexing.index t e, min ew w)
714 ([], Indexing.empty, 1000000) new'
717 let active, newa, pruned =
718 backward_simplify_active eq_uri env new_pos new_table min_weight active
722 active, (make_passive []), newa, None, pruned
724 active, passive, newa, None, pruned
727 backward_simplify_passive env new_pos new_table min_weight passive in
728 active, passive, newa, newp *)
731 let close eq_uri env new' given =
732 let new_pos, new_table, min_weight =
735 let ew, _, _, _ , _ = Equality.open_equality e in
736 e::l, Indexing.index t e, min ew w)
737 ([], Indexing.empty, 1000000) (snd new')
741 let pos = infer eq_uri env c (new_pos,new_table) in
746 let is_commutative_law eq =
747 let w, proof, (eq_ty, left, right, order), metas , _ =
748 Equality.open_equality eq
750 match left,right with
751 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
752 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
753 f1 = f2 && a1 = b2 && a2 = b1
757 let prova eq_uri env new' active =
758 let given = List.filter is_commutative_law (fst active) in
762 (Printf.sprintf "symmetric:\n%s\n"
765 (fun e -> Equality.string_of_equality ~env e)
767 close eq_uri env new' given
770 (* returns an estimation of how many equalities in passive can be activated
771 within the current time limit *)
772 let get_selection_estimate () =
773 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
774 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
776 ceil ((float_of_int !processed_clauses) *.
777 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
781 (** initializes the set of goals *)
782 let make_goals goal =
784 and passive = [0, [goal]] in
788 let make_goal_set goal =
792 (** initializes the set of theorems *)
793 let make_theorems theorems =
798 let activate_goal (active, passive) =
801 | goal_conj::tl -> true, (goal_conj::active, tl)
802 | [] -> false, (active, passive)
804 true, (active,passive)
808 let activate_theorem (active, passive) =
810 | theorem::tl -> true, (theorem::active, tl)
811 | [] -> false, (active, passive)
816 let simplify_theorems env theorems ?passive (active_list, active_table) =
817 let pl, passive_table =
820 | Some ((pn, _), (pp, _), pt) ->
821 let pn = List.map (fun e -> (Negative, e)) pn
822 and pp = List.map (fun e -> (Positive, e)) pp in
825 let a_theorems, p_theorems = theorems in
826 let demodulate table theorem =
827 let newmeta, newthm =
828 Indexing.demodulation_theorem !maxmeta env table theorem in
830 theorem != newthm, newthm
832 let foldfun table (a, p) theorem =
833 let changed, theorem = demodulate table theorem in
834 if changed then (a, theorem::p) else (theorem::a, p)
836 let mapfun table theorem = snd (demodulate table theorem) in
837 match passive_table with
839 let p_theorems = List.map (mapfun active_table) p_theorems in
840 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
841 | Some passive_table ->
842 let p_theorems = List.map (mapfun active_table) p_theorems in
843 let p_theorems, a_theorems =
844 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
845 let p_theorems = List.map (mapfun passive_table) p_theorems in
846 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
850 let rec simpl eq_uri env e others others_simpl =
851 let active = others @ others_simpl in
854 (fun t e -> Indexing.index t e)
855 Indexing.empty active
857 let res = forward_simplify eq_uri env (Positive,e) (active, tbl) in
861 | None -> simpl eq_uri env hd tl others_simpl
862 | Some e -> simpl eq_uri env hd tl (e::others_simpl)
866 | None -> others_simpl
867 | Some e -> e::others_simpl
871 let simplify_equalities eq_uri env equalities =
874 (Printf.sprintf "equalities:\n%s\n"
876 (List.map Equality.string_of_equality equalities))));
877 debug_print (lazy "SIMPLYFYING EQUALITIES...");
878 match equalities with
882 List.rev (simpl eq_uri env hd tl [])
886 (Printf.sprintf "equalities AFTER:\n%s\n"
888 (List.map Equality.string_of_equality res))));
892 let print_goals goals =
899 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
901 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
904 let check_if_goal_is_subsumed ((_,ctx,_) as env) table (goalproof,menv,ty) =
906 let names = names_of_context ctx in
907 Printf.eprintf "check_goal_subsumed: %s\n" (CicPp.pp ty names);
910 | Cic.Appl[Cic.MutInd(uri,_,_);eq_ty;left;right]
911 when LibraryObjects.is_eq_URI uri ->
914 (0,Equality.Exact (Cic.Implicit None),(eq_ty,left,right,Eq),menv)
916 (* match Indexing.subsumption env table goal_equation with*)
917 match Indexing.unification env table goal_equation with
918 | Some (subst, equality, swapped ) ->
920 ("GOAL SUBSUMED BY: " ^ Equality.string_of_equality equality);
921 prerr_endline ("SUBST:" ^ Subst.ppsubst subst);
922 let (_,p,(ty,l,r,_),m,id) = Equality.open_equality equality in
923 let cicmenv = Subst.apply_subst_metasenv subst (m @ menv) in
926 Equality.symmetric eq_ty l id uri m
930 Some (goalproof, p, id, subst, cicmenv)
935 let check_if_goal_is_identity env = function
936 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
937 when left = right && LibraryObjects.is_eq_URI uri ->
938 let reflproof = Equality.Exact (Equality.refl_proof uri eq_ty left) in
939 Some (goalproof, reflproof, 0, Subst.empty_subst,m)
940 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
941 when LibraryObjects.is_eq_URI uri ->
942 (let _,context,_ = env in
945 Inference.unification m m context left right CicUniv.empty_ugraph
947 let reflproof = Equality.Exact (Equality.refl_proof uri eq_ty left) in
948 let m = Subst.apply_subst_metasenv s m in
949 Some (goalproof, reflproof, 0, s,m)
954 let rec check goal = function
958 | None -> check goal tl
959 | (Some p) as ok -> ok
962 let simplify_goal_set env goals passive active =
963 let active_goals, passive_goals = goals in
964 let find (_,_,g) where =
965 List.exists (fun (_,_,g1) -> Equality.meta_convertibility g g1) where
970 match simplify_goal env goal ~passive active with
972 if changed then prerr_endline "???????????????cambiato ancora";
973 if find g acc then acc else g::acc)
974 (* active_goals active_goals *)
977 if List.length active_goals <> List.length simplified then
978 prerr_endline "SEMPLIFICANDO HO SCARTATO...";
979 (simplified,passive_goals)
981 HExtlib.list_uniq ~eq:(fun (_,_,t1) (_,_,t2) -> t1 = t2)
982 (List.sort (fun (_,_,t1) (_,_,t2) -> compare t1 t1)
983 ((*goals @*) simplified))
987 let check_if_goals_set_is_solved env active goals =
988 let active_goals, passive_goals = goals in
995 check_if_goal_is_identity env;
996 check_if_goal_is_subsumed env (snd active)])
1000 let infer_goal_set env active goals =
1001 let active_goals, passive_goals = goals in
1002 let rec aux = function
1005 let changed,selected = simplify_goal env hd active in
1007 prerr_endline ("--------------- goal semplificato");
1008 let (_,_,t1) = selected in
1011 Equality.meta_convertibility t t1)
1012 active_goals) then aux tl
1014 let passive_goals = tl in
1015 let new_passive_goals =
1016 if Utils.metas_of_term t1 = [] then passive_goals
1019 Indexing.superposition_left env (snd active) selected in
1020 passive_goals @ new'
1022 selected::active_goals, new_passive_goals
1028 let infer_goal_set env active goals =
1029 let active_goals, passive_goals = goals in
1030 let rec aux = function
1032 | ((_,_,t1) as hd)::tl when
1035 Equality.meta_convertibility t t1)
1038 let selected = hd in
1039 let passive_goals = tl in
1040 let new_passive_goals =
1041 if CicUtil.is_meta_closed t1 then
1044 let new' = Indexing.superposition_left env (snd active) selected in
1045 passive_goals @ new'
1047 selected::active_goals, new_passive_goals
1054 let infer_goal_set_with_current env current goals =
1055 let active_goals, passive_goals = goals in
1056 let _,table,_ = build_table [current] in
1060 let new' = Indexing.superposition_left env table g in
1062 passive_goals active_goals
1067 let ids = List.map (fun _,_,i,_,_ -> i) p in
1071 let ids_of_goal_set (ga,gp) =
1072 List.flatten (List.map ids_of_goal ga) @
1073 List.flatten (List.map ids_of_goal gp)
1076 let size_of_goal_set_a (l,_) = List.length l;;
1077 let size_of_goal_set_p (_,l) = List.length l;;
1079 (** given-clause algorithm with full reduction strategy: NEW implementation *)
1080 (* here goals is a set of goals in OR *)
1082 eq_uri ((_,context,_) as env) goals theorems passive active max_iterations max_time
1084 let names = names_of_context context in
1085 let initial_time = Unix.gettimeofday () in
1086 let iterations_left iterno =
1087 let now = Unix.gettimeofday () in
1088 let time_left = max_time -. now in
1089 let time_spent_until_now = now -. initial_time in
1090 let iteration_medium_cost =
1091 time_spent_until_now /. (float_of_int iterno)
1093 let iterations_left = time_left /. iteration_medium_cost in
1094 int_of_float iterations_left
1096 let rec step goals theorems passive active iterno =
1097 if iterno > max_iterations then
1098 (ParamodulationFailure "No more iterations to spend")
1099 else if Unix.gettimeofday () > max_time then
1100 (ParamodulationFailure "No more time to spend")
1103 let _ = prerr_endline "simpl goal with active" in
1104 let _ = <:start<simplify goal set active>> in
1105 let goals = simplify_goal_set env goals passive active in
1106 let _ = <:stop<simplify goal set active>> in
1110 (Printf.sprintf "%d #ACTIVES: %d #PASSIVES: %d #GOALSET: %d(%d)\n"
1111 iterno (size_of_active active) (size_of_passive passive)
1112 (size_of_goal_set_a goals) (size_of_goal_set_p goals))
1114 (* PRUNING OF PASSIVE THAT WILL NEVER BE PROCESSED *)
1116 let selection_estimate = iterations_left iterno in
1117 let kept = size_of_passive passive in
1118 if kept > selection_estimate then
1120 (*Printf.eprintf "Too many passive equalities: pruning...";
1121 prune_passive selection_estimate active*) passive
1126 kept_clauses := (size_of_passive passive) + (size_of_active active);
1127 let goals = infer_goal_set env active goals in
1128 match check_if_goals_set_is_solved env active goals with
1131 (Printf.sprintf "Found a proof in: %f\n"
1132 (Unix.gettimeofday() -. initial_time));
1133 ParamodulationSuccess p
1136 if passive_is_empty passive then
1137 ParamodulationFailure "No more passive"(*maybe this is a success! *)
1140 (* COLLECTION OF GARBAGED EQUALITIES *)
1141 if iterno mod 40 = 0 then
1143 let active = List.map Equality.id_of (fst active) in
1144 let passive = List.map Equality.id_of (fst (fst passive)) in
1145 let goal = ids_of_goal_set goals in
1146 Equality.collect active passive goal
1148 let current, passive = select env goals passive in
1152 prerr_endline (Printf.sprintf "Current goal = %s\n"
1153 (CicPp.pp g names)))
1155 prerr_endline (Printf.sprintf "Selected = %s\n"
1156 (Equality.string_of_equality ~env current))
1158 (* SIMPLIFICATION OF CURRENT *)
1160 forward_simplify eq_uri env (Positive, current) active
1163 | None -> step goals theorems passive active (iterno+1)
1165 (* GENERATION OF NEW EQUATIONS *)
1166 prerr_endline "infer";
1167 let new' = infer eq_uri env current active in
1168 prerr_endline "infer goal";
1169 let goals = infer_goal_set_with_current env current goals in
1171 let al, tbl = active in
1172 al @ [current], Indexing.index tbl current
1174 (* FORWARD AND BACKWARD SIMPLIFICATION *)
1175 prerr_endline "fwd/back simpl";
1176 let rec simplify new' active passive =
1178 forward_simplify_new eq_uri env new' ~passive active
1180 let active, passive, newa, retained, pruned =
1181 backward_simplify eq_uri env new' ~passive active
1184 List.fold_left filter_dependent passive pruned
1186 match newa, retained with
1187 | None, None -> active, passive, new'
1189 | None, Some p -> simplify (new' @ p) active passive
1190 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1192 let active, passive, new' = simplify new' active passive in
1193 prerr_endline "simpl goal with new";
1195 let a,b,_ = build_table new' in
1196 let _ = <:start<simplify_goal_set new>> in
1197 let rc = simplify_goal_set env goals passive (a,b) in
1198 let _ = <:stop<simplify_goal_set new>> in
1201 let passive = add_to_passive passive new' in
1202 step goals theorems passive active (iterno+1)
1205 step goals theorems passive active 1
1208 let rec saturate_equations eq_uri env goal accept_fun passive active =
1209 elapsed_time := Unix.gettimeofday () -. !start_time;
1210 if !elapsed_time > !time_limit then
1213 let current, passive = select env ([goal],[]) passive in
1214 let res = forward_simplify eq_uri env (Positive, current) ~passive active in
1217 saturate_equations eq_uri env goal accept_fun passive active
1219 debug_print (lazy (Printf.sprintf "selected: %s"
1220 (Equality.string_of_equality ~env current)));
1221 let new' = infer eq_uri env current active in
1223 if Equality.is_identity env current then active
1225 let al, tbl = active in
1226 al @ [current], Indexing.index tbl current
1228 let rec simplify new' active passive =
1229 let new' = forward_simplify_new eq_uri env new' ~passive active in
1230 let active, passive, newa, retained, pruned =
1231 backward_simplify eq_uri env new' ~passive active in
1233 List.fold_left filter_dependent passive pruned in
1234 match newa, retained with
1235 | None, None -> active, passive, new'
1237 | None, Some p -> simplify (new' @ p) active passive
1238 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1240 let active, passive, new' = simplify new' active passive in
1244 (Printf.sprintf "active:\n%s\n"
1247 (fun e -> Equality.string_of_equality ~env e)
1253 (Printf.sprintf "new':\n%s\n"
1256 (fun e -> "Negative " ^
1257 (Equality.string_of_equality ~env e)) new'))))
1259 let new' = List.filter accept_fun new' in
1260 let passive = add_to_passive passive new' in
1261 saturate_equations eq_uri env goal accept_fun passive active
1264 let main dbd full term metasenv ugraph = ()
1266 let main dbd full term metasenv ugraph =
1267 let module C = Cic in
1268 let module T = CicTypeChecker in
1269 let module PET = ProofEngineTypes in
1270 let module PP = CicPp in
1271 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1272 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1273 let proof, goals = status in
1274 let goal' = List.nth goals 0 in
1275 let _, metasenv, meta_proof, _ = proof in
1276 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1277 let eq_indexes, equalities, maxm = find_equalities context proof in
1278 let lib_eq_uris, library_equalities, maxm =
1280 find_library_equalities dbd context (proof, goal') (maxm+2)
1282 let library_equalities = List.map snd library_equalities in
1283 maxmeta := maxm+2; (* TODO ugly!! *)
1284 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1285 let new_meta_goal, metasenv, type_of_goal =
1286 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1289 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1290 Cic.Meta (maxm+1, irl),
1291 (maxm+1, context, ty)::metasenv,
1294 let env = (metasenv, context, ugraph) in
1295 let t1 = Unix.gettimeofday () in
1298 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1299 let context_hyp = find_context_hypotheses env eq_indexes in
1300 context_hyp @ theorems, []
1303 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1304 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1306 let t = CicUtil.term_of_uri refl_equal in
1307 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1310 let t2 = Unix.gettimeofday () in
1313 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1318 "Theorems:\n-------------------------------------\n%s\n"
1323 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1328 ([],Equality.BasicProof (Equality.empty_subst ,new_meta_goal)), [], goal
1330 let equalities = simplify_equalities env
1331 (equalities@library_equalities) in
1332 let active = make_active () in
1333 let passive = make_passive equalities in
1334 Printf.printf "\ncurrent goal: %s\n"
1335 (let _, _, g = goal in CicPp.ppterm g);
1336 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1337 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1338 Printf.printf "\nequalities:\n%s\n"
1341 (Equality.string_of_equality ~env) equalities));
1342 (* (equalities @ library_equalities))); *)
1343 print_endline "--------------------------------------------------";
1344 let start = Unix.gettimeofday () in
1345 print_endline "GO!";
1346 start_time := Unix.gettimeofday ();
1348 let goals = make_goals goal in
1349 (if !use_fullred then given_clause_fullred else given_clause_fullred)
1350 dbd env goals theorems passive active
1352 let finish = Unix.gettimeofday () in
1355 | ParamodulationFailure ->
1356 Printf.printf "NO proof found! :-(\n\n"
1357 | ParamodulationSuccess (Some ((cicproof,cicmenv),(proof, env))) ->
1358 Printf.printf "OK, found a proof!\n";
1359 let oldproof = Equation.build_proof_term proof in
1360 let newproof,_,newenv,_ =
1361 CicRefine.type_of_aux'
1362 cicmenv context cicproof CicUniv.empty_ugraph
1364 (* REMEMBER: we have to instantiate meta_proof, we should use
1365 apply the "apply" tactic to proof and status
1367 let names = names_of_context context in
1368 prerr_endline "OLD PROOF";
1369 print_endline (PP.pp proof names);
1370 prerr_endline "NEW PROOF";
1371 print_endline (PP.pp newproof names);
1375 let (_, _, _, menv,_) = Equality.open_equality eq in
1382 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1384 print_endline (string_of_float (finish -. start));
1386 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1387 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1389 (fst (CicReduction.are_convertible
1390 context type_of_goal ty ug)));
1392 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1393 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1394 print_endline (string_of_float (finish -. start));*)
1398 | ParamodulationSuccess None ->
1399 Printf.printf "Success, but no proof?!?\n\n"
1404 ((Printf.sprintf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1405 "forward_simpl_new_time: %.9f\n" ^^
1406 "backward_simpl_time: %.9f\n")
1407 !infer_time !forward_simpl_time !forward_simpl_new_time
1408 !backward_simpl_time) ^
1409 (Printf.sprintf "passive_maintainance_time: %.9f\n"
1410 !passive_maintainance_time) ^
1411 (Printf.sprintf " successful unification/matching time: %.9f\n"
1412 !Indexing.match_unif_time_ok) ^
1413 (Printf.sprintf " failed unification/matching time: %.9f\n"
1414 !Indexing.match_unif_time_no) ^
1415 (Printf.sprintf " indexing retrieval time: %.9f\n"
1416 !Indexing.indexing_retrieval_time) ^
1417 (Printf.sprintf " demodulate_term.build_newtarget_time: %.9f\n"
1418 !Indexing.build_newtarget_time) ^
1419 (Printf.sprintf "derived %d clauses, kept %d clauses.\n"
1420 !derived_clauses !kept_clauses))
1424 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1430 let default_depth = !maxdepth
1431 and default_width = !maxwidth;;
1435 symbols_counter := 0;
1436 weight_age_counter := !weight_age_ratio;
1437 processed_clauses := 0;
1440 maximal_retained_equality := None;
1442 forward_simpl_time := 0.;
1443 forward_simpl_new_time := 0.;
1444 backward_simpl_time := 0.;
1445 passive_maintainance_time := 0.;
1446 derived_clauses := 0;
1451 let eq_of_goal = function
1452 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1454 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1458 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1459 let module C = Cic in
1461 Indexing.init_index ();
1464 (* CicUnification.unif_ty := false;*)
1465 let proof, goalno = status in
1466 let uri, metasenv, meta_proof, term_to_prove = proof in
1467 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1468 let eq_uri = eq_of_goal type_of_goal in
1469 let names = names_of_context context in
1470 let eq_indexes, equalities, maxm = find_equalities context proof in
1471 let ugraph = CicUniv.empty_ugraph in
1472 let env = (metasenv, context, ugraph) in
1473 let cleaned_goal = Utils.remove_local_context type_of_goal in
1474 let goal = [], List.filter (fun (i,_,_)->i<>goalno) metasenv, cleaned_goal in
1476 let t1 = Unix.gettimeofday () in
1477 let lib_eq_uris, library_equalities, maxm =
1478 find_library_equalities dbd context (proof, goalno) (maxm+2)
1480 let library_equalities = List.map snd library_equalities in
1481 let t2 = Unix.gettimeofday () in
1484 simplify_equalities eq_uri env (equalities@library_equalities)
1488 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
1489 let t1 = Unix.gettimeofday () in
1492 let thms = find_library_theorems dbd env (proof, goalno) lib_eq_uris in
1493 let context_hyp = find_context_hypotheses env eq_indexes in
1494 context_hyp @ thms, []
1496 let refl_equal = LibraryObjects.eq_refl_URI ~eq:eq_uri in
1497 let t = CicUtil.term_of_uri refl_equal in
1498 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1501 let t2 = Unix.gettimeofday () in
1506 "Theorems:\n-------------------------------------\n%s\n"
1511 "Term: %s, type: %s"
1512 (CicPp.ppterm t) (CicPp.ppterm ty))
1516 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1518 let active = make_active () in
1519 let passive = make_passive equalities in
1520 let start = Unix.gettimeofday () in
1523 let goals = make_goals goal in
1524 given_clause_fullred dbd env goals theorems passive active
1526 let goals = make_goal_set goal in
1527 let max_iterations = 10000 in
1528 let max_time = Unix.gettimeofday () +. 600. (* minutes *) in
1530 eq_uri env goals theorems passive active max_iterations max_time
1532 let finish = Unix.gettimeofday () in
1533 (res, finish -. start)
1536 | ParamodulationFailure s ->
1537 raise (ProofEngineTypes.Fail (lazy ("NO proof found: " ^ s)))
1538 | ParamodulationSuccess
1539 (goalproof,newproof,subsumption_id,subsumption_subst, proof_menv) ->
1540 prerr_endline "OK, found a proof!";
1542 (Equality.pp_proof names goalproof newproof subsumption_subst
1543 subsumption_id type_of_goal);
1544 prerr_endline (CicMetaSubst.ppmetasenv [] proof_menv);
1545 prerr_endline "ENDOFPROOFS";
1546 (* generation of the CIC proof *)
1548 List.filter (fun i -> i <> goalno)
1549 (ProofEngineHelpers.compare_metasenvs
1550 ~newmetasenv:metasenv ~oldmetasenv:proof_menv)
1552 let goal_proof, side_effects_t =
1553 let initial = Equality.add_subst subsumption_subst newproof in
1554 Equality.build_goal_proof
1555 eq_uri goalproof initial type_of_goal side_effects
1557 let goal_proof = Subst.apply_subst subsumption_subst goal_proof in
1558 let metas_still_open_in_proof = Utils.metas_of_term goal_proof in
1559 (*prerr_endline (CicPp.pp goal_proof names);*)
1561 let goal_proof = (* Subst.apply_subst subsumption_subst *) goal_proof in
1562 let side_effects_t =
1563 List.map (Subst.apply_subst subsumption_subst) side_effects_t
1565 (* replacing fake mets with real ones *)
1566 prerr_endline "replacing metas...";
1567 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1568 let goal_proof_menv, what, with_what,free_meta =
1570 (fun (acc1,acc2,acc3,uniq) (i,_,ty) ->
1573 acc1, (Cic.Meta(i,[]))::acc2, m::acc3, uniq
1575 [i,context,ty], (Cic.Meta(i,[]))::acc2,
1576 (Cic.Meta(i,irl)) ::acc3,Some (Cic.Meta(i,irl)))
1579 (fun (i,_,_) -> List.mem i metas_still_open_in_proof)
1583 (* we need this fake equality since the metas of the hypothesis may be
1584 * with a real local context *)
1585 ProofEngineReduction.replace_lifting
1586 ~equality:(fun x y ->
1587 match x,y with Cic.Meta(i,_),Cic.Meta(j,_) -> i=j | _-> false)
1588 ~what ~with_what ~where
1590 let goal_proof = replace goal_proof in
1591 (* ok per le meta libere... ma per quelle che c'erano e sono rimaste?
1592 * what mi pare buono, sostituisce solo le meta farlocche *)
1593 let side_effects_t = List.map replace side_effects_t in
1595 List.filter (fun i -> i <> goalno)
1596 (ProofEngineHelpers.compare_metasenvs
1597 ~oldmetasenv:metasenv ~newmetasenv:goal_proof_menv)
1599 prerr_endline ("freemetas: " ^ String.concat "," (List.map string_of_int free_metas) );
1600 (* check/refine/... build the new proof *)
1602 ProofEngineReduction.replace
1603 ~what:side_effects ~with_what:side_effects_t
1604 ~equality:(fun i t -> match t with Cic.Meta(j,_)->j=i|_->false)
1607 let subst_side_effects,real_menv,_ =
1608 let fail t s = raise (ProofEngineTypes.Fail (lazy (t^Lazy.force s))) in
1609 let free_metas_menv =
1610 List.map (fun i -> CicUtil.lookup_meta i goal_proof_menv) free_metas
1613 CicUnification.fo_unif_subst [] context (metasenv @ free_metas_menv)
1614 replaced_goal type_of_goal CicUniv.empty_ugraph
1616 | CicUnification.UnificationFailure s
1617 | CicUnification.Uncertain s
1618 | CicUnification.AssertFailure s ->
1619 fail "Maybe the local context of metas in the goal was not an IRL" s
1622 (goalno,(context,goal_proof,type_of_goal))::subst_side_effects
1624 prerr_endline ("MENVreal_menv: " ^ CicMetaSubst.ppmetasenv [] real_menv);
1627 CicTypeChecker.type_of_aux' real_menv context goal_proof
1628 CicUniv.empty_ugraph
1630 | CicUtil.Meta_not_found _
1631 | CicTypeChecker.TypeCheckerFailure _
1632 | CicTypeChecker.AssertFailure _
1633 | Invalid_argument "list_fold_left2" as exn ->
1634 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1635 prerr_endline (CicPp.pp goal_proof names);
1636 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1639 let proof, real_metasenv =
1640 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1641 proof goalno (CicMetaSubst.apply_subst final_subst) real_menv
1644 match free_meta with Some(Cic.Meta(m,_)) when m<>goalno ->[m] | _ ->[]
1647 "GOALS APERTI: %s\nMETASENV PRIMA:\n%s\nMETASENV DOPO:\n%s\n"
1648 (String.concat ", " (List.map string_of_int open_goals))
1649 (CicMetaSubst.ppmetasenv [] metasenv)
1650 (CicMetaSubst.ppmetasenv [] real_metasenv);
1651 prerr_endline (Printf.sprintf "\nTIME NEEDED: %8.2f" time);
1655 let retrieve_and_print dbd term metasenv ugraph =
1656 let module C = Cic in
1657 let module T = CicTypeChecker in
1658 let module PET = ProofEngineTypes in
1659 let module PP = CicPp in
1660 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1661 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1662 let proof, goals = status in
1663 let goal' = List.nth goals 0 in
1664 let uri, metasenv, meta_proof, term_to_prove = proof in
1665 let _, context, type_of_goal = CicUtil.lookup_meta goal' metasenv in
1666 let eq_uri = eq_of_goal type_of_goal in
1667 let eq_indexes, equalities, maxm = find_equalities context proof in
1668 let ugraph = CicUniv.empty_ugraph in
1669 let env = (metasenv, context, ugraph) in
1670 let t1 = Unix.gettimeofday () in
1671 let lib_eq_uris, library_equalities, maxm =
1672 find_library_equalities dbd context (proof, goal') (maxm+2) in
1673 let t2 = Unix.gettimeofday () in
1675 let equalities = (* equalities @ *) library_equalities in
1678 (Printf.sprintf "\n\nequalities:\n%s\n"
1682 (* Printf.sprintf "%s: %s" *)
1683 (UriManager.string_of_uri u)
1684 (* (string_of_equality e) *)
1687 debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
1688 let rec simpl e others others_simpl =
1690 let active = List.map (fun (u, e) -> (Positive, e))
1691 (others @ others_simpl) in
1694 (fun t (_, e) -> Indexing.index t e)
1695 Indexing.empty active
1697 let res = forward_simplify eq_uri env (Positive, e) (active, tbl) in
1701 | None -> simpl hd tl others_simpl
1702 | Some e -> simpl hd tl ((u, e)::others_simpl)
1706 | None -> others_simpl
1707 | Some e -> (u, e)::others_simpl
1711 match equalities with
1714 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
1716 List.rev (simpl (*(Positive,*) hd others [])
1720 (Printf.sprintf "\nequalities AFTER:\n%s\n"
1724 Printf.sprintf "%s: %s"
1725 (UriManager.string_of_uri u)
1726 (Equality.string_of_equality e)
1732 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
1736 let main_demod_equalities dbd term metasenv ugraph =
1737 let module C = Cic in
1738 let module T = CicTypeChecker in
1739 let module PET = ProofEngineTypes in
1740 let module PP = CicPp in
1741 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1742 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1743 let proof, goals = status in
1744 let goal' = List.nth goals 0 in
1745 let _, metasenv, meta_proof, _ = proof in
1746 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1747 let eq_uri = eq_of_goal goal in
1748 let eq_indexes, equalities, maxm = find_equalities context proof in
1749 let lib_eq_uris, library_equalities, maxm =
1750 find_library_equalities dbd context (proof, goal') (maxm+2)
1752 let library_equalities = List.map snd library_equalities in
1753 maxmeta := maxm+2; (* TODO ugly!! *)
1754 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1755 let new_meta_goal, metasenv, type_of_goal =
1756 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1759 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
1760 (CicPp.ppterm ty)));
1761 Cic.Meta (maxm+1, irl),
1762 (maxm+1, context, ty)::metasenv,
1765 let env = (metasenv, context, ugraph) in
1767 let goal = [], [], goal
1770 simplify_equalities eq_uri env (equalities@library_equalities)
1772 let active = make_active () in
1773 let passive = make_passive equalities in
1774 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1775 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1776 Printf.printf "\nequalities:\n%s\n"
1779 (Equality.string_of_equality ~env) equalities));
1780 print_endline "--------------------------------------------------";
1781 print_endline "GO!";
1782 start_time := Unix.gettimeofday ();
1783 if !time_limit < 1. then time_limit := 60.;
1785 saturate_equations eq_uri env goal (fun e -> true) passive active
1789 List.fold_left (fun s e -> EqualitySet.add e s)
1790 EqualitySet.empty equalities
1793 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
1799 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
1803 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
1805 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
1806 (String.concat "\n" (List.map (Equality.string_of_equality ~env) active))
1807 (* (String.concat "\n"
1808 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
1809 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
1812 (fun e -> CicPp.ppterm (Equality.term_of_equality eq_uri e))
1817 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
1821 let demodulate_tac ~dbd ~pattern ((proof,goal)(*s initialstatus*)) =
1822 let module I = Inference in
1823 let curi,metasenv,pbo,pty = proof in
1824 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1825 let eq_uri = eq_of_goal ty in
1826 let eq_indexes, equalities, maxm =
1827 Inference.find_equalities context proof
1829 let lib_eq_uris, library_equalities, maxm =
1830 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
1831 if library_equalities = [] then prerr_endline "VUOTA!!!";
1832 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1833 let library_equalities = List.map snd library_equalities in
1834 let initgoal = [], [], ty in
1835 let env = (metasenv, context, CicUniv.empty_ugraph) in
1837 simplify_equalities eq_uri env (equalities@library_equalities)
1841 (fun tbl eq -> Indexing.index tbl eq)
1842 Indexing.empty equalities
1844 let changed,(newproof,newmetasenv, newty) =
1845 Indexing.demodulation_goal
1846 (metasenv,context,CicUniv.empty_ugraph) table initgoal
1850 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1852 Equality.build_goal_proof eq_uri newproof opengoal ty []
1854 let extended_metasenv = (maxm,context,newty)::metasenv in
1855 let extended_status =
1856 (curi,extended_metasenv,pbo,pty),goal in
1857 let (status,newgoals) =
1858 ProofEngineTypes.apply_tactic
1859 (PrimitiveTactics.apply_tac ~term:proofterm)
1861 (status,maxm::newgoals)
1863 else (* if newty = ty then *)
1864 raise (ProofEngineTypes.Fail (lazy "no progress"))
1865 (*else ProofEngineTypes.apply_tactic
1866 (ReductionTactics.simpl_tac ~pattern)
1870 let demodulate_tac ~dbd ~pattern =
1871 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)
1875 <:show<Saturation.>> ^ Indexing.get_stats () ^ Inference.get_stats () ^
1876 Equality.get_stats ();;