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 eq_uri 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 eq_uri !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
363 ~subterms_only:true eq_uri !maxmeta env table equality
366 if Utils.debug_metas then
370 Indexing.check_target c current "sup2") res);
371 let pos = infer_positive table tl in
375 let maxm, copy_of_current = Equality.fix_metas !maxmeta current in
378 let curr_table = Indexing.index Indexing.empty current in
379 let _ = <:start<active contro current>> in
380 let pos = infer_positive curr_table ((*copy_of_current::*)active_list) in
381 let _ = <:stop<active contro current>> in
382 if Utils.debug_metas then
385 Indexing.check_target c current "sup3") pos);
388 derived_clauses := !derived_clauses + (List.length new_pos);
389 match !maximal_retained_equality with
392 ignore(assert false);
393 (* if we have a maximal_retained_equality, we can discard all equalities
394 "greater" than it, as they will never be reached... An equality is
395 greater than maximal_retained_equality if it is bigger
396 wrt. OrderedEquality.compare and it is less similar than
397 maximal_retained_equality to the current goal *)
398 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
401 let check_for_deep_subsumption env active_table eq =
402 let _,_,(eq_ty, left, right, order),metas,id = Equality.open_equality eq in
403 let check_subsumed deep l r =
405 Equality.mk_tmp_equality(0,(eq_ty,l,r,Utils.Incomparable),metas)in
406 match Indexing.subsumption env active_table eqtmp with
410 let rec aux b (ok_so_far, subsumption_used) t1 t2 =
412 | t1, t2 when not ok_so_far -> ok_so_far, subsumption_used
413 | t1, t2 when subsumption_used -> t1 = t2, subsumption_used
414 | Cic.Appl (h1::l),Cic.Appl (h2::l') ->
415 let rc = check_subsumed b t1 t2 in
421 (fun (ok_so_far, subsumption_used) t t' ->
422 aux true (ok_so_far, subsumption_used) t t')
423 (ok_so_far, subsumption_used) l l'
424 with Invalid_argument _ -> false,subsumption_used)
426 false, subsumption_used
427 | _ -> false, subsumption_used
429 fst (aux false (true,false) left right)
432 (* buttare via sign *)
434 (** simplifies current using active and passive *)
436 eq_uri env (sign,current) ?passive (active_list, active_table)
438 let _, context, _ = env in
442 | Some ((_, _), pt) -> Some pt
444 let demodulate table current =
445 let newmeta, newcurrent =
446 Indexing.demodulation_equality eq_uri !maxmeta env table sign current in
448 if Equality.is_identity env newcurrent then
453 let rec demod current =
454 if Utils.debug_metas then
455 ignore (Indexing.check_target context current "demod0");
456 let res = demodulate active_table current in
457 if Utils.debug_metas then
458 ignore ((function None -> () | Some x ->
459 ignore (Indexing.check_target context x "demod1");()) res);
463 match passive_table with
465 | Some passive_table ->
466 match demodulate passive_table newcurrent with
468 | Some newnewcurrent ->
469 if Equality.compare newcurrent newnewcurrent <> 0 then
471 else Some newnewcurrent
473 let res = demod current in
477 if Indexing.in_index active_table c then
480 match passive_table with
482 if check_for_deep_subsumption env active_table c then
487 if Indexing.subsumption env active_table c = None then
492 | Some passive_table ->
493 if Indexing.in_index passive_table c then None
495 if check_for_deep_subsumption env active_table c then
498 (* if Indexing.subsumption env active_table c = None then*)
499 (match Indexing.subsumption env passive_table c with
503 (*prerr_endline "\n\nPESCO DALLE PASSIVE LA PIU' GENERALE\n\n";
511 (** simplifies new using active and passive *)
512 let forward_simplify_new eq_uri env new_pos ?passive active =
513 if Utils.debug_metas then
517 (fun current -> Indexing.check_target c current "forward new pos")
520 let active_list, active_table = active in
524 | Some ((_, _), pt) -> Some pt
526 let demodulate sign table target =
527 let newmeta, newtarget =
528 Indexing.demodulation_equality eq_uri !maxmeta env table sign target
533 (* we could also demodulate using passive. Currently we don't *)
535 List.map (demodulate Positive active_table) new_pos
540 if not (Equality.is_identity env e) then
543 EqualitySet.empty new_pos
545 let new_pos = EqualitySet.elements new_pos_set in
548 match passive_table with
550 (fun e -> (Indexing.subsumption env active_table e = None))
551 | Some passive_table ->
552 (fun e -> ((Indexing.subsumption env active_table e = None) &&
553 (Indexing.subsumption env passive_table e = None)))
556 match passive_table with
558 (fun e -> not (Indexing.in_index active_table e))
559 | Some passive_table ->
561 not ((Indexing.in_index active_table e) ||
562 (Indexing.in_index passive_table e)))
564 List.filter subs (List.filter is_duplicate new_pos)
568 (** simplifies a goal with equalities in active and passive *)
569 let rec simplify_goal env goal ?passive (active_list, active_table) =
573 | Some ((_, _), pt) -> Some pt
575 let demodulate table goal = Indexing.demodulation_goal env table goal in
577 match passive_table with
578 | None -> demodulate active_table goal
579 | Some passive_table ->
580 let changed, goal = demodulate active_table goal in
581 (* let changed', goal = demodulate passive_table goal in*)
582 (changed (*|| changed'*)), goal
588 snd (simplify_goal env goal ?passive (active_list, active_table))
592 let simplify_goals env goals ?passive active =
593 let a_goals, p_goals = goals in
596 (fun g -> snd (simplify_goal env g ?passive active))
601 (fun g -> snd (simplify_goal env g ?passive active))
608 (** simplifies active usign new *)
609 let backward_simplify_active eq_uri env new_pos new_table min_weight active =
610 let active_list, active_table = active in
611 let active_list, newa, pruned =
613 (fun equality (res, newn,pruned) ->
614 let ew, _, _, _,id = Equality.open_equality equality in
615 if ew < min_weight then
616 equality::res, newn,pruned
620 eq_uri env (Utils.Positive, equality) (new_pos, new_table)
622 | None -> res, newn, id::pruned
624 if Equality.compare equality e = 0 then
627 res, e::newn, pruned)
628 active_list ([], [],[])
631 List.exists (Equality.meta_convertibility_eq eq1) where
634 let _, _, _, _,id = Equality.open_equality eq in id
636 let ((active1,pruned),tbl), newa =
638 (fun eq ((res,pruned), tbl) ->
639 if List.mem eq res then
640 (res, (id_of_eq eq)::pruned),tbl
641 else if (Equality.is_identity env eq) || (find eq res) then (
642 (res, (id_of_eq eq)::pruned),tbl
645 (eq::res,pruned), Indexing.index tbl eq)
646 active_list (([],pruned), Indexing.empty),
649 if (Equality.is_identity env eq) then p
654 | [] -> (active1,tbl), None, pruned
655 | _ -> (active1,tbl), Some newa, pruned
659 (** simplifies passive using new *)
660 let backward_simplify_passive eq_uri env new_pos new_table min_weight passive =
661 let (pl, ps), passive_table = passive in
662 let f sign equality (resl, ress, newn) =
663 let ew, _, _, _ , _ = Equality.open_equality equality in
664 if ew < min_weight then
665 equality::resl, ress, newn
668 forward_simplify eq_uri env (sign, equality) (new_pos, new_table)
670 | None -> resl, EqualitySet.remove equality ress, newn
673 equality::resl, ress, newn
675 let ress = EqualitySet.remove equality ress in
678 let pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
681 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
684 | [] -> ((pl, ps), passive_table), None
685 | _ -> ((pl, ps), passive_table), Some (newp)
688 let build_table equations =
691 let ew, _, _, _ , _ = Equality.open_equality e in
692 e::l, Indexing.index t e, min ew w)
693 ([], Indexing.empty, 1000000) equations
697 let backward_simplify eq_uri env new' ?passive active =
698 let new_pos, new_table, min_weight = build_table new' in
702 let ew, _, _, _ , _ = Equality.open_equality e in
703 e::l, Indexing.index t e, min ew w)
704 ([], Indexing.empty, 1000000) new'
707 let active, newa, pruned =
708 backward_simplify_active eq_uri env new_pos new_table min_weight active
712 active, (make_passive []), newa, None, pruned
714 active, passive, newa, None, pruned
717 backward_simplify_passive env new_pos new_table min_weight passive in
718 active, passive, newa, newp *)
721 let close eq_uri env new' given =
722 let new_pos, new_table, min_weight =
725 let ew, _, _, _ , _ = Equality.open_equality e in
726 e::l, Indexing.index t e, min ew w)
727 ([], Indexing.empty, 1000000) (snd new')
731 let pos = infer eq_uri env c (new_pos,new_table) in
736 let is_commutative_law eq =
737 let w, proof, (eq_ty, left, right, order), metas , _ =
738 Equality.open_equality eq
740 match left,right with
741 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
742 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
743 f1 = f2 && a1 = b2 && a2 = b1
747 let prova eq_uri env new' active =
748 let given = List.filter is_commutative_law (fst active) in
752 (Printf.sprintf "symmetric:\n%s\n"
755 (fun e -> Equality.string_of_equality ~env e)
757 close eq_uri env new' given
760 (* returns an estimation of how many equalities in passive can be activated
761 within the current time limit *)
762 let get_selection_estimate () =
763 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
764 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
766 ceil ((float_of_int !processed_clauses) *.
767 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
771 (** initializes the set of goals *)
772 let make_goals goal =
774 and passive = [0, [goal]] in
778 let make_goal_set goal =
782 (** initializes the set of theorems *)
783 let make_theorems theorems =
788 let activate_goal (active, passive) =
791 | goal_conj::tl -> true, (goal_conj::active, tl)
792 | [] -> false, (active, passive)
794 true, (active,passive)
798 let activate_theorem (active, passive) =
800 | theorem::tl -> true, (theorem::active, tl)
801 | [] -> false, (active, passive)
806 let simplify_theorems env theorems ?passive (active_list, active_table) =
807 let pl, passive_table =
810 | Some ((pn, _), (pp, _), pt) ->
811 let pn = List.map (fun e -> (Negative, e)) pn
812 and pp = List.map (fun e -> (Positive, e)) pp in
815 let a_theorems, p_theorems = theorems in
816 let demodulate table theorem =
817 let newmeta, newthm =
818 Indexing.demodulation_theorem !maxmeta env table theorem in
820 theorem != newthm, newthm
822 let foldfun table (a, p) theorem =
823 let changed, theorem = demodulate table theorem in
824 if changed then (a, theorem::p) else (theorem::a, p)
826 let mapfun table theorem = snd (demodulate table theorem) in
827 match passive_table with
829 let p_theorems = List.map (mapfun active_table) p_theorems in
830 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
831 | Some passive_table ->
832 let p_theorems = List.map (mapfun active_table) p_theorems in
833 let p_theorems, a_theorems =
834 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
835 let p_theorems = List.map (mapfun passive_table) p_theorems in
836 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
840 let rec simpl eq_uri env e others others_simpl =
841 let active = others @ others_simpl in
844 (fun t e -> Indexing.index t e)
845 Indexing.empty active
847 let res = forward_simplify eq_uri env (Positive,e) (active, tbl) in
851 | None -> simpl eq_uri env hd tl others_simpl
852 | Some e -> simpl eq_uri env hd tl (e::others_simpl)
856 | None -> others_simpl
857 | Some e -> e::others_simpl
861 let simplify_equalities eq_uri env equalities =
864 (Printf.sprintf "equalities:\n%s\n"
866 (List.map Equality.string_of_equality equalities))));
867 debug_print (lazy "SIMPLYFYING EQUALITIES...");
868 match equalities with
872 List.rev (simpl eq_uri env hd tl [])
876 (Printf.sprintf "equalities AFTER:\n%s\n"
878 (List.map Equality.string_of_equality res))));
882 let print_goals goals =
889 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
891 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
894 let check_if_goal_is_subsumed ((_,ctx,_) as env) table (goalproof,menv,ty) =
896 let names = names_of_context ctx in
897 Printf.eprintf "check_goal_subsumed: %s\n" (CicPp.pp ty names);
900 | Cic.Appl[Cic.MutInd(uri,_,_);eq_ty;left;right]
901 when LibraryObjects.is_eq_URI uri ->
904 (0,Equality.Exact (Cic.Implicit None),(eq_ty,left,right,Eq),menv)
906 (* match Indexing.subsumption env table goal_equation with*)
907 match Indexing.unification env table goal_equation with
908 | Some (subst, equality, swapped ) ->
910 ("GOAL SUBSUMED BY: " ^ Equality.string_of_equality equality);
911 prerr_endline ("SUBST:" ^ Subst.ppsubst subst);
912 let (_,p,(ty,l,r,_),m,id) = Equality.open_equality equality in
913 let cicmenv = Subst.apply_subst_metasenv subst (m @ menv) in
916 Equality.symmetric eq_ty l id uri m
920 Some (goalproof, p, id, subst, cicmenv)
925 let check_if_goal_is_identity env = function
926 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
927 when left = right && LibraryObjects.is_eq_URI uri ->
928 let reflproof = Equality.Exact (Equality.refl_proof uri eq_ty left) in
929 Some (goalproof, reflproof, 0, Subst.empty_subst,m)
930 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
931 when LibraryObjects.is_eq_URI uri ->
932 (let _,context,_ = env in
935 Inference.unification m m context left right CicUniv.empty_ugraph
937 let reflproof = Equality.Exact (Equality.refl_proof uri eq_ty left) in
938 let m = Subst.apply_subst_metasenv s m in
939 Some (goalproof, reflproof, 0, s,m)
944 let rec check goal = function
948 | None -> check goal tl
949 | (Some p) as ok -> ok
952 let simplify_goal_set env goals passive active =
953 let active_goals, passive_goals = goals in
954 let find (_,_,g) where =
955 List.exists (fun (_,_,g1) -> Equality.meta_convertibility g g1) where
960 match simplify_goal env goal ~passive active with
961 | _, g -> if find g acc then acc else g::acc)
962 (* active_goals active_goals *)
965 if List.length active_goals <> List.length simplified then
966 prerr_endline "SEMPLIFICANDO HO SCARTATO...";
967 (simplified,passive_goals)
969 HExtlib.list_uniq ~eq:(fun (_,_,t1) (_,_,t2) -> t1 = t2)
970 (List.sort (fun (_,_,t1) (_,_,t2) -> compare t1 t1)
971 ((*goals @*) simplified))
975 let check_if_goals_set_is_solved env active goals =
976 let active_goals, passive_goals = goals in
983 check_if_goal_is_identity env;
984 check_if_goal_is_subsumed env (snd active)])
988 let infer_goal_set env active goals =
989 let active_goals, passive_goals = goals in
990 let rec aux = function
992 | ((_,_,t1) as hd)::tl when
994 (fun (_,_,t) -> Equality.meta_convertibility t t1)
998 let passive_goals = tl in
999 let _,_,ty = selected in
1001 if CicUtil.is_meta_closed ty then
1004 Indexing.superposition_left env (snd active) selected
1006 selected::active_goals, passive_goals @ new'
1012 let infer_goal_set_with_current env current goals =
1013 let active_goals, passive_goals = goals in
1014 let _,table,_ = build_table [current] in
1018 let new' = Indexing.superposition_left env table g in
1020 passive_goals active_goals
1025 let size_of_goal_set_a (l,_) = List.length l;;
1026 let size_of_goal_set_p (_,l) = List.length l;;
1028 (** given-clause algorithm with full reduction strategy: NEW implementation *)
1029 (* here goals is a set of goals in OR *)
1031 eq_uri ((_,context,_) as env) goals theorems passive active max_iterations max_time
1033 let names = names_of_context context in
1034 let initial_time = Unix.gettimeofday () in
1035 let iterations_left iterno =
1036 let now = Unix.gettimeofday () in
1037 let time_left = max_time -. now in
1038 let time_spent_until_now = now -. initial_time in
1039 let iteration_medium_cost =
1040 time_spent_until_now /. (float_of_int iterno)
1042 let iterations_left = time_left /. iteration_medium_cost in
1043 int_of_float iterations_left
1045 let rec step goals theorems passive active iterno =
1046 if iterno > max_iterations then
1047 (ParamodulationFailure "No more iterations to spend")
1048 else if Unix.gettimeofday () > max_time then
1049 (ParamodulationFailure "No more time to spend")
1051 let _ = prerr_endline "simpl goal with active" in
1052 let _ = <:start<simplify goal set active>> in
1053 let goals = simplify_goal_set env goals passive active in
1054 let _ = <:stop<simplify goal set active>> in
1055 match check_if_goals_set_is_solved env active goals with
1058 (Printf.sprintf "Found a proof in: %f\n"
1059 (Unix.gettimeofday() -. initial_time));
1061 ParamodulationSuccess p
1064 (Printf.sprintf "%d #ACTIVES: %d #PASSIVES: %d #GOALSET: %d(%d)\n"
1065 iterno (size_of_active active) (size_of_passive passive)
1066 (size_of_goal_set_a goals) (size_of_goal_set_p goals));
1067 (* PRUNING OF PASSIVE THAT WILL NEVER BE PROCESSED *)
1069 let selection_estimate = iterations_left iterno in
1070 let kept = size_of_passive passive in
1071 if kept > selection_estimate then
1073 (*Printf.eprintf "Too many passive equalities: pruning...";
1074 prune_passive selection_estimate active*) passive
1079 kept_clauses := (size_of_passive passive) + (size_of_active active);
1081 if passive_is_empty passive then
1082 ParamodulationFailure "No more passive"(*maybe this is a success! *)
1085 let goals = infer_goal_set env active goals in
1086 let current, passive = select env goals passive in
1087 let _,_,goaltype = List.hd (fst goals) in
1088 prerr_endline (Printf.sprintf "Current goal = %s\n"
1089 (CicPp.pp goaltype names));
1090 prerr_endline (Printf.sprintf "Selected = %s\n"
1091 (Equality.string_of_equality ~env current));
1092 (* SIMPLIFICATION OF CURRENT *)
1094 forward_simplify eq_uri env (Positive, current) active
1097 | None -> step goals theorems passive active (iterno+1)
1099 (* GENERATION OF NEW EQUATIONS *)
1100 prerr_endline "infer";
1101 let new' = infer eq_uri env current active in
1102 prerr_endline "infer goal";
1103 let goals = infer_goal_set_with_current env current goals in
1105 let al, tbl = active in
1106 al @ [current], Indexing.index tbl current
1108 (* FORWARD AND BACKWARD SIMPLIFICATION *)
1109 prerr_endline "fwd/back simpl";
1110 let rec simplify new' active passive =
1112 forward_simplify_new eq_uri env new' ~passive active
1114 let active, passive, newa, retained, pruned =
1115 backward_simplify eq_uri env new' ~passive active
1118 List.fold_left filter_dependent passive pruned
1120 match newa, retained with
1121 | None, None -> active, passive, new'
1123 | None, Some p -> simplify (new' @ p) active passive
1124 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1126 let active, passive, new' = simplify new' active passive in
1127 prerr_endline "simpl goal with new";
1129 let a,b,_ = build_table new' in
1130 let _ = <:start<simplify_goal_set new>> in
1131 let rc = simplify_goal_set env goals passive (a,b) in
1132 let _ = <:stop<simplify_goal_set new>> in
1135 let passive = add_to_passive passive new' in
1136 step goals theorems passive active (iterno+1)
1139 step goals theorems passive active 1
1142 let rec saturate_equations eq_uri env goal accept_fun passive active =
1143 elapsed_time := Unix.gettimeofday () -. !start_time;
1144 if !elapsed_time > !time_limit then
1147 let current, passive = select env ([goal],[]) passive in
1148 let res = forward_simplify eq_uri env (Positive, current) ~passive active in
1151 saturate_equations eq_uri env goal accept_fun passive active
1153 debug_print (lazy (Printf.sprintf "selected: %s"
1154 (Equality.string_of_equality ~env current)));
1155 let new' = infer eq_uri env current active in
1157 if Equality.is_identity env current then active
1159 let al, tbl = active in
1160 al @ [current], Indexing.index tbl current
1162 let rec simplify new' active passive =
1163 let new' = forward_simplify_new eq_uri env new' ~passive active in
1164 let active, passive, newa, retained, pruned =
1165 backward_simplify eq_uri env new' ~passive active in
1167 List.fold_left filter_dependent passive pruned in
1168 match newa, retained with
1169 | None, None -> active, passive, new'
1171 | None, Some p -> simplify (new' @ p) active passive
1172 | Some p, Some rp -> simplify (new' @ p @ rp) active passive
1174 let active, passive, new' = simplify new' active passive in
1178 (Printf.sprintf "active:\n%s\n"
1181 (fun e -> Equality.string_of_equality ~env e)
1187 (Printf.sprintf "new':\n%s\n"
1190 (fun e -> "Negative " ^
1191 (Equality.string_of_equality ~env e)) new'))))
1193 let new' = List.filter accept_fun new' in
1194 let passive = add_to_passive passive new' in
1195 saturate_equations eq_uri env goal accept_fun passive active
1198 let main dbd full term metasenv ugraph = ()
1200 let main dbd full term metasenv ugraph =
1201 let module C = Cic in
1202 let module T = CicTypeChecker in
1203 let module PET = ProofEngineTypes in
1204 let module PP = CicPp in
1205 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1206 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1207 let proof, goals = status in
1208 let goal' = List.nth goals 0 in
1209 let _, metasenv, meta_proof, _ = proof in
1210 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1211 let eq_indexes, equalities, maxm = find_equalities context proof in
1212 let lib_eq_uris, library_equalities, maxm =
1214 find_library_equalities dbd context (proof, goal') (maxm+2)
1216 let library_equalities = List.map snd library_equalities in
1217 maxmeta := maxm+2; (* TODO ugly!! *)
1218 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1219 let new_meta_goal, metasenv, type_of_goal =
1220 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1223 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1224 Cic.Meta (maxm+1, irl),
1225 (maxm+1, context, ty)::metasenv,
1228 let env = (metasenv, context, ugraph) in
1229 let t1 = Unix.gettimeofday () in
1232 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1233 let context_hyp = find_context_hypotheses env eq_indexes in
1234 context_hyp @ theorems, []
1237 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1238 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1240 let t = CicUtil.term_of_uri refl_equal in
1241 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1244 let t2 = Unix.gettimeofday () in
1247 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1252 "Theorems:\n-------------------------------------\n%s\n"
1257 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1262 ([],Equality.BasicProof (Equality.empty_subst ,new_meta_goal)), [], goal
1264 let equalities = simplify_equalities env
1265 (equalities@library_equalities) in
1266 let active = make_active () in
1267 let passive = make_passive equalities in
1268 Printf.printf "\ncurrent goal: %s\n"
1269 (let _, _, g = goal in CicPp.ppterm g);
1270 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1271 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1272 Printf.printf "\nequalities:\n%s\n"
1275 (Equality.string_of_equality ~env) equalities));
1276 (* (equalities @ library_equalities))); *)
1277 print_endline "--------------------------------------------------";
1278 let start = Unix.gettimeofday () in
1279 print_endline "GO!";
1280 start_time := Unix.gettimeofday ();
1282 let goals = make_goals goal in
1283 (if !use_fullred then given_clause_fullred else given_clause_fullred)
1284 dbd env goals theorems passive active
1286 let finish = Unix.gettimeofday () in
1289 | ParamodulationFailure ->
1290 Printf.printf "NO proof found! :-(\n\n"
1291 | ParamodulationSuccess (Some ((cicproof,cicmenv),(proof, env))) ->
1292 Printf.printf "OK, found a proof!\n";
1293 let oldproof = Equation.build_proof_term proof in
1294 let newproof,_,newenv,_ =
1295 CicRefine.type_of_aux'
1296 cicmenv context cicproof CicUniv.empty_ugraph
1298 (* REMEMBER: we have to instantiate meta_proof, we should use
1299 apply the "apply" tactic to proof and status
1301 let names = names_of_context context in
1302 prerr_endline "OLD PROOF";
1303 print_endline (PP.pp proof names);
1304 prerr_endline "NEW PROOF";
1305 print_endline (PP.pp newproof names);
1309 let (_, _, _, menv,_) = Equality.open_equality eq in
1316 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1318 print_endline (string_of_float (finish -. start));
1320 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1321 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1323 (fst (CicReduction.are_convertible
1324 context type_of_goal ty ug)));
1326 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1327 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1328 print_endline (string_of_float (finish -. start));*)
1332 | ParamodulationSuccess None ->
1333 Printf.printf "Success, but no proof?!?\n\n"
1338 ((Printf.sprintf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1339 "forward_simpl_new_time: %.9f\n" ^^
1340 "backward_simpl_time: %.9f\n")
1341 !infer_time !forward_simpl_time !forward_simpl_new_time
1342 !backward_simpl_time) ^
1343 (Printf.sprintf "passive_maintainance_time: %.9f\n"
1344 !passive_maintainance_time) ^
1345 (Printf.sprintf " successful unification/matching time: %.9f\n"
1346 !Indexing.match_unif_time_ok) ^
1347 (Printf.sprintf " failed unification/matching time: %.9f\n"
1348 !Indexing.match_unif_time_no) ^
1349 (Printf.sprintf " indexing retrieval time: %.9f\n"
1350 !Indexing.indexing_retrieval_time) ^
1351 (Printf.sprintf " demodulate_term.build_newtarget_time: %.9f\n"
1352 !Indexing.build_newtarget_time) ^
1353 (Printf.sprintf "derived %d clauses, kept %d clauses.\n"
1354 !derived_clauses !kept_clauses))
1358 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1364 let default_depth = !maxdepth
1365 and default_width = !maxwidth;;
1369 symbols_counter := 0;
1370 weight_age_counter := !weight_age_ratio;
1371 processed_clauses := 0;
1374 maximal_retained_equality := None;
1376 forward_simpl_time := 0.;
1377 forward_simpl_new_time := 0.;
1378 backward_simpl_time := 0.;
1379 passive_maintainance_time := 0.;
1380 derived_clauses := 0;
1385 let eq_of_goal = function
1386 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1388 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1392 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1393 let module C = Cic in
1395 Indexing.init_index ();
1398 (* CicUnification.unif_ty := false;*)
1399 let proof, goalno = status in
1400 let uri, metasenv, meta_proof, term_to_prove = proof in
1401 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1402 let eq_uri = eq_of_goal type_of_goal in
1403 let names = names_of_context context in
1404 let eq_indexes, equalities, maxm = find_equalities context proof in
1405 let ugraph = CicUniv.empty_ugraph in
1406 let env = (metasenv, context, ugraph) in
1407 let goal = [], List.filter (fun (i,_,_)->i<>goalno) metasenv, type_of_goal in
1409 let t1 = Unix.gettimeofday () in
1410 let lib_eq_uris, library_equalities, maxm =
1411 find_library_equalities dbd context (proof, goalno) (maxm+2)
1413 let library_equalities = List.map snd library_equalities in
1414 let t2 = Unix.gettimeofday () in
1417 simplify_equalities eq_uri env (equalities@library_equalities)
1421 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
1422 let t1 = Unix.gettimeofday () in
1425 let thms = find_library_theorems dbd env (proof, goalno) lib_eq_uris in
1426 let context_hyp = find_context_hypotheses env eq_indexes in
1427 context_hyp @ thms, []
1429 let refl_equal = LibraryObjects.eq_refl_URI ~eq:eq_uri in
1430 let t = CicUtil.term_of_uri refl_equal in
1431 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1434 let t2 = Unix.gettimeofday () in
1439 "Theorems:\n-------------------------------------\n%s\n"
1444 "Term: %s, type: %s"
1445 (CicPp.ppterm t) (CicPp.ppterm ty))
1449 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1451 let active = make_active () in
1452 let passive = make_passive equalities in
1453 let start = Unix.gettimeofday () in
1456 let goals = make_goals goal in
1457 given_clause_fullred dbd env goals theorems passive active
1459 let goals = make_goal_set goal in
1460 let max_iterations = 10000 in
1461 let max_time = Unix.gettimeofday () +. 600. (* minutes *) in
1463 eq_uri env goals theorems passive active max_iterations max_time
1465 let finish = Unix.gettimeofday () in
1466 (res, finish -. start)
1469 | ParamodulationFailure s ->
1470 raise (ProofEngineTypes.Fail (lazy ("NO proof found: " ^ s)))
1471 | ParamodulationSuccess
1472 (goalproof,newproof,subsumption_id,subsumption_subst, proof_menv) ->
1473 prerr_endline "OK, found a proof!";
1475 (Equality.pp_proof names goalproof newproof subsumption_subst
1476 subsumption_id type_of_goal);
1477 prerr_endline (CicMetaSubst.ppmetasenv [] proof_menv);
1478 prerr_endline "ENDOFPROOFS";
1479 (* generation of the CIC proof *)
1481 List.filter (fun i -> i <> goalno)
1482 (ProofEngineHelpers.compare_metasenvs
1483 ~newmetasenv:metasenv ~oldmetasenv:proof_menv)
1485 let goal_proof, side_effects_t =
1486 let initial = Equality.add_subst subsumption_subst newproof in
1487 Equality.build_goal_proof
1488 eq_uri goalproof initial type_of_goal side_effects
1490 let goal_proof = Subst.apply_subst subsumption_subst goal_proof in
1491 let metas_still_open_in_proof = Utils.metas_of_term goal_proof in
1492 (*prerr_endline (CicPp.pp goal_proof names);*)
1494 let goal_proof = (* Subst.apply_subst subsumption_subst *) goal_proof in
1495 let side_effects_t =
1496 List.map (Subst.apply_subst subsumption_subst) side_effects_t
1498 (* replacing fake mets with real ones *)
1499 prerr_endline "replacing metas...";
1500 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1501 let goal_proof_menv, what, with_what,free_meta =
1503 (fun (acc1,acc2,acc3,uniq) (i,_,ty) ->
1506 acc1, (Cic.Meta(i,[]))::acc2, m::acc3, uniq
1508 [i,context,ty], (Cic.Meta(i,[]))::acc2,
1509 (Cic.Meta(i,irl)) ::acc3,Some (Cic.Meta(i,irl)))
1512 (fun (i,_,_) -> List.mem i metas_still_open_in_proof)
1516 (* we need this fake equality since the metas of the hypothesis may be
1517 * with a real local context *)
1518 ProofEngineReduction.replace_lifting
1519 ~equality:(fun x y ->
1520 match x,y with Cic.Meta(i,_),Cic.Meta(j,_) -> i=j | _-> false)
1521 ~what ~with_what ~where
1523 let goal_proof = replace goal_proof in
1524 (* ok per le meta libere... ma per quelle che c'erano e sono rimaste?
1525 * what mi pare buono, sostituisce solo le meta farlocche *)
1526 let side_effects_t = List.map replace side_effects_t in
1528 List.filter (fun i -> i <> goalno)
1529 (ProofEngineHelpers.compare_metasenvs
1530 ~oldmetasenv:metasenv ~newmetasenv:goal_proof_menv)
1532 prerr_endline ("freemetas: " ^ String.concat "," (List.map string_of_int free_metas) );
1533 (* check/refine/... build the new proof *)
1535 ProofEngineReduction.replace
1536 ~what:side_effects ~with_what:side_effects_t
1537 ~equality:(fun i t -> match t with Cic.Meta(j,_)->j=i|_->false)
1540 let subst_side_effects,real_menv,_ =
1541 let fail t s = raise (ProofEngineTypes.Fail (lazy (t^Lazy.force s))) in
1542 let free_metas_menv =
1543 List.map (fun i -> CicUtil.lookup_meta i goal_proof_menv) free_metas
1546 CicUnification.fo_unif_subst [] context (metasenv @ free_metas_menv)
1547 replaced_goal type_of_goal CicUniv.empty_ugraph
1549 | CicUnification.UnificationFailure s
1550 | CicUnification.Uncertain s
1551 | CicUnification.AssertFailure s ->
1552 fail "Maybe the local context of metas in the goal was not an IRL" s
1555 (goalno,(context,goal_proof,type_of_goal))::subst_side_effects
1557 prerr_endline ("MENVreal_menv: " ^ CicMetaSubst.ppmetasenv [] real_menv);
1560 CicTypeChecker.type_of_aux' real_menv context goal_proof
1561 CicUniv.empty_ugraph
1563 | CicUtil.Meta_not_found _
1564 | CicTypeChecker.TypeCheckerFailure _
1565 | CicTypeChecker.AssertFailure _
1566 | Invalid_argument "list_fold_left2" as exn ->
1567 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1568 prerr_endline (CicPp.pp goal_proof names);
1569 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1572 let proof, real_metasenv =
1573 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1574 proof goalno (CicMetaSubst.apply_subst final_subst) real_menv
1577 match free_meta with Some(Cic.Meta(m,_)) when m<>goalno ->[m] | _ ->[]
1580 "GOALS APERTI: %s\nMETASENV PRIMA:\n%s\nMETASENV DOPO:\n%s\n"
1581 (String.concat ", " (List.map string_of_int open_goals))
1582 (CicMetaSubst.ppmetasenv [] metasenv)
1583 (CicMetaSubst.ppmetasenv [] real_metasenv);
1584 prerr_endline (Printf.sprintf "\nTIME NEEDED: %8.2f" time);
1588 let retrieve_and_print dbd term metasenv ugraph =
1589 let module C = Cic in
1590 let module T = CicTypeChecker in
1591 let module PET = ProofEngineTypes in
1592 let module PP = CicPp in
1593 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1594 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1595 let proof, goals = status in
1596 let goal' = List.nth goals 0 in
1597 let uri, metasenv, meta_proof, term_to_prove = proof in
1598 let _, context, type_of_goal = CicUtil.lookup_meta goal' metasenv in
1599 let eq_uri = eq_of_goal type_of_goal in
1600 let eq_indexes, equalities, maxm = find_equalities context proof in
1601 let ugraph = CicUniv.empty_ugraph in
1602 let env = (metasenv, context, ugraph) in
1603 let t1 = Unix.gettimeofday () in
1604 let lib_eq_uris, library_equalities, maxm =
1605 find_library_equalities dbd context (proof, goal') (maxm+2) in
1606 let t2 = Unix.gettimeofday () in
1608 let equalities = (* equalities @ *) library_equalities in
1611 (Printf.sprintf "\n\nequalities:\n%s\n"
1615 (* Printf.sprintf "%s: %s" *)
1616 (UriManager.string_of_uri u)
1617 (* (string_of_equality e) *)
1620 debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
1621 let rec simpl e others others_simpl =
1623 let active = List.map (fun (u, e) -> (Positive, e))
1624 (others @ others_simpl) in
1627 (fun t (_, e) -> Indexing.index t e)
1628 Indexing.empty active
1630 let res = forward_simplify eq_uri env (Positive, e) (active, tbl) in
1634 | None -> simpl hd tl others_simpl
1635 | Some e -> simpl hd tl ((u, e)::others_simpl)
1639 | None -> others_simpl
1640 | Some e -> (u, e)::others_simpl
1644 match equalities with
1647 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
1649 List.rev (simpl (*(Positive,*) hd others [])
1653 (Printf.sprintf "\nequalities AFTER:\n%s\n"
1657 Printf.sprintf "%s: %s"
1658 (UriManager.string_of_uri u)
1659 (Equality.string_of_equality e)
1665 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
1669 let main_demod_equalities dbd term metasenv ugraph =
1670 let module C = Cic in
1671 let module T = CicTypeChecker in
1672 let module PET = ProofEngineTypes in
1673 let module PP = CicPp in
1674 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1675 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1676 let proof, goals = status in
1677 let goal' = List.nth goals 0 in
1678 let _, metasenv, meta_proof, _ = proof in
1679 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1680 let eq_uri = eq_of_goal goal in
1681 let eq_indexes, equalities, maxm = find_equalities context proof in
1682 let lib_eq_uris, library_equalities, maxm =
1683 find_library_equalities dbd context (proof, goal') (maxm+2)
1685 let library_equalities = List.map snd library_equalities in
1686 maxmeta := maxm+2; (* TODO ugly!! *)
1687 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1688 let new_meta_goal, metasenv, type_of_goal =
1689 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1692 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
1693 (CicPp.ppterm ty)));
1694 Cic.Meta (maxm+1, irl),
1695 (maxm+1, context, ty)::metasenv,
1698 let env = (metasenv, context, ugraph) in
1700 let goal = [], [], goal
1703 simplify_equalities eq_uri env (equalities@library_equalities)
1705 let active = make_active () in
1706 let passive = make_passive equalities in
1707 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1708 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1709 Printf.printf "\nequalities:\n%s\n"
1712 (Equality.string_of_equality ~env) equalities));
1713 print_endline "--------------------------------------------------";
1714 print_endline "GO!";
1715 start_time := Unix.gettimeofday ();
1716 if !time_limit < 1. then time_limit := 60.;
1718 saturate_equations eq_uri env goal (fun e -> true) passive active
1722 List.fold_left (fun s e -> EqualitySet.add e s)
1723 EqualitySet.empty equalities
1726 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
1732 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
1736 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
1738 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
1739 (String.concat "\n" (List.map (Equality.string_of_equality ~env) active))
1740 (* (String.concat "\n"
1741 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
1742 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
1745 (fun e -> CicPp.ppterm (Equality.term_of_equality eq_uri e))
1750 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
1754 let demodulate_tac ~dbd ~pattern ((proof,goal)(*s initialstatus*)) =
1755 let module I = Inference in
1756 let curi,metasenv,pbo,pty = proof in
1757 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1758 let eq_uri = eq_of_goal ty in
1759 let eq_indexes, equalities, maxm =
1760 Inference.find_equalities context proof
1762 let lib_eq_uris, library_equalities, maxm =
1763 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
1764 if library_equalities = [] then prerr_endline "VUOTA!!!";
1765 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1766 let library_equalities = List.map snd library_equalities in
1767 let initgoal = [], [], ty in
1768 let env = (metasenv, context, CicUniv.empty_ugraph) in
1770 simplify_equalities eq_uri env (equalities@library_equalities)
1774 (fun tbl eq -> Indexing.index tbl eq)
1775 Indexing.empty equalities
1777 let changed,(newproof,newmetasenv, newty) =
1778 Indexing.demodulation_goal
1779 (metasenv,context,CicUniv.empty_ugraph) table initgoal
1783 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1785 Equality.build_goal_proof eq_uri newproof opengoal ty []
1787 let extended_metasenv = (maxm,context,newty)::metasenv in
1788 let extended_status =
1789 (curi,extended_metasenv,pbo,pty),goal in
1790 let (status,newgoals) =
1791 ProofEngineTypes.apply_tactic
1792 (PrimitiveTactics.apply_tac ~term:proofterm)
1794 (status,maxm::newgoals)
1796 else (* if newty = ty then *)
1797 raise (ProofEngineTypes.Fail (lazy "no progress"))
1798 (*else ProofEngineTypes.apply_tactic
1799 (ReductionTactics.simpl_tac ~pattern)
1803 let demodulate_tac ~dbd ~pattern =
1804 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)
1808 <:show<Saturation.>> ^ Indexing.get_stats () ^ Inference.get_stats ();;