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>>;; *)
30 (* set to false to disable paramodulation inside auto_tac *)
32 let connect_to_auto = true;;
35 (* profiling statistics... *)
36 let infer_time = ref 0.;;
37 let forward_simpl_time = ref 0.;;
38 let forward_simpl_new_time = ref 0.;;
39 let backward_simpl_time = ref 0.;;
40 let passive_maintainance_time = ref 0.;;
42 (* limited-resource-strategy related globals *)
43 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
44 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
45 let start_time = ref 0.;; (* time at which the execution started *)
46 let elapsed_time = ref 0.;;
47 (* let maximal_weight = ref None;; *)
48 let maximal_retained_equality = ref None;;
50 (* equality-selection related globals *)
51 let use_fullred = ref true;;
52 let weight_age_ratio = ref 6 (* 5 *);; (* settable by the user *)
53 let weight_age_counter = ref !weight_age_ratio ;;
54 let symbols_ratio = ref 0 (* 3 *);;
55 let symbols_counter = ref 0;;
57 (* non-recursive Knuth-Bendix term ordering by default *)
58 (* Utils.compare_terms := Utils.rpo;; *)
59 (* Utils.compare_terms := Utils.nonrec_kbo;; *)
60 (* Utils.compare_terms := Utils.ao;; *)
63 let derived_clauses = ref 0;;
64 let kept_clauses = ref 0;;
66 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
69 (* varbiables controlling the search-space *)
70 let maxdepth = ref 3;;
71 let maxwidth = ref 3;;
73 type theorem = Cic.term * Cic.term * Cic.metasenv;;
75 let symbols_of_equality equality =
76 let (_, _, (_, left, right, _), _,_) = Equality.open_equality equality in
77 let m1 = Utils.symbols_of_term left in
82 let c = Utils.TermMap.find k res in
83 Utils.TermMap.add k (c+v) res
85 Utils.TermMap.add k v res)
86 (Utils.symbols_of_term right) m1
92 module OrderedEquality = struct
93 type t = Equality.equality
96 match Equality.meta_convertibility_eq eq1 eq2 with
99 let w1, _, (ty,left, right, _), m1,_ = Equality.open_equality eq1 in
100 let w2, _, (ty',left', right', _), m2,_ = Equality.open_equality eq2 in
101 match Pervasives.compare w1 w2 with
103 let res = (List.length m1) - (List.length m2) in
104 if res <> 0 then res else
105 Equality.compare eq1 eq2
109 module EqualitySet = Set.Make(OrderedEquality);;
111 type passive_table = Equality.equality list * EqualitySet.t
112 type active_table = Equality.equality list * Indexing.Index.t
114 Equality.goal_proof * Equality.proof * int * Subst.substitution * Cic.metasenv
116 | ParamodulationFailure of string * active_table * passive_table
117 | ParamodulationSuccess of new_proof * active_table * passive_table
119 let make_passive eq_list =
121 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty eq_list
123 (*EqualitySet.elements set*) eq_list, set
125 let make_empty_active () = [], Indexing.empty ;;
126 let make_active eq_list =
127 eq_list, List.fold_left Indexing.index Indexing.empty eq_list
130 let size_of_passive (passive_list, _) = List.length passive_list;;
131 let size_of_active (active_list, _) = List.length active_list;;
132 let passive_is_empty = function
133 | [], s when EqualitySet.is_empty s -> true
134 | [], s -> assert false (* the set and the list should be in sync *)
138 type goals = Equality.goal list * Equality.goal list
140 let no_more_passive_goals g = match g with | _,[] -> true | _ -> false;;
143 let age_factor = 0.01;;
146 selects one equality from passive. The selection strategy is a combination
147 of weight, age and goal-similarity
150 let rec select env g passive =
151 processed_clauses := !processed_clauses + 1;
154 match (List.rev goals) with goal::_ -> goal | _ -> assert false
157 let pos_list, pos_set = passive in
158 let remove eq l = List.filter (fun e -> Equality.compare e eq <> 0) l in
159 if !weight_age_ratio > 0 then
160 weight_age_counter := !weight_age_counter - 1;
161 match !weight_age_counter with
163 weight_age_counter := !weight_age_ratio;
164 let skip_giant pos_list pos_set =
166 | (hd:EqualitySet.elt)::tl ->
167 let w,_,_,_,_ = Equality.open_equality hd in
169 hd, (tl, EqualitySet.remove hd pos_set)
173 ("+++ skipping giant of size "^string_of_int w^" +++");
175 select env g (tl@[hd],pos_set)
178 skip_giant pos_list pos_set)
181 let rec skip_giant pos_list pos_set =
183 | (hd:EqualitySet.elt)::tl ->
184 let w,_,_,_,_ = Equality.open_equality hd in
185 let pos_set = EqualitySet.remove hd pos_set in
190 ("+++ skipping giant of size "^string_of_int w^" +++");
191 skip_giant tl pos_set)
194 skip_giant pos_list pos_set)
198 | _ when (!symbols_counter > 0) ->
199 (symbols_counter := !symbols_counter - 1;
200 let cardinality map =
201 Utils.TermMap.fold (fun k v res -> res + v) map 0
204 let _, _, term = goal in
205 Utils.symbols_of_term term
207 let card = cardinality symbols in
208 let foldfun k v (r1, r2) =
209 if Utils.TermMap.mem k symbols then
210 let c = Utils.TermMap.find k symbols in
211 let c1 = abs (c - v) in
217 let f equality (i, e) =
219 Utils.TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
221 let c = others + (abs (common - card)) in
222 if c < i then (c, equality)
225 let e1 = EqualitySet.min_elt pos_set in
228 Utils.TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
230 (others + (abs (common - card))), e1
232 let _, current = EqualitySet.fold f pos_set initial in
234 (remove current pos_list, EqualitySet.remove current pos_set))
237 symbols_counter := !symbols_ratio;
239 let w1,_,_,_,_ = Equality.open_equality e1 in
240 let w2,_,_,_,_ = Equality.open_equality e2 in
241 if w1 < w2 then e1 else e2
243 let rec my_min_elt min = function
245 | hd::tl -> my_min_elt (my_min hd min) tl
247 (* let current = EqualitySet.min_elt pos_set in *)
248 let current = my_min_elt (List.hd pos_list) (List.tl pos_list) in
249 current,(remove current pos_list, EqualitySet.remove current pos_set)
253 let filter_dependent bag passive id =
254 let pos_list, pos_set = passive in
255 let passive,no_pruned =
257 (fun eq ((list,set),no) ->
258 if Equality.depend bag eq id then
259 (list, EqualitySet.remove eq set), no + 1
262 pos_list (([],pos_set),0)
265 if no_pruned > 0 then
266 prerr_endline ("+++ pruning "^ string_of_int no_pruned ^" passives +++");
272 (* adds to passive a list of equalities new_pos *)
273 let add_to_passive passive new_pos preferred =
274 let pos_list, pos_set = passive in
275 let ok set equality = not (EqualitySet.mem equality set) in
276 let pos = List.filter (ok pos_set) new_pos in
277 let add set equalities =
278 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
280 let pos_head, pos_tail =
282 (fun e -> List.exists (fun x -> Equality.compare x e = 0) preferred)
285 pos_head @ pos_list @ pos_tail, add pos_set pos
289 (* removes from passive equalities that are estimated impossible to activate
290 within the current time limit *)
291 let prune_passive howmany (active, _) passive =
292 let (pl, ps), tbl = passive in
293 let howmany = float_of_int howmany
294 and ratio = float_of_int !weight_age_ratio in
297 int_of_float (if t -. v < 0.5 then t else v)
299 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
300 and in_age = round (howmany /. (ratio +. 1.)) in
302 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
303 let counter = ref !symbols_ratio in
308 counter := !counter - 1;
309 if !counter = 0 then counter := !symbols_ratio in
310 let e = EqualitySet.min_elt ps in
311 let ps' = pickw (w-1) (EqualitySet.remove e ps) in
312 EqualitySet.add e ps'
314 let e = EqualitySet.min_elt ps in
315 let ps' = pickw (w-1) (EqualitySet.remove e ps) in
316 EqualitySet.add e ps'
320 let ps = pickw in_weight ps in
321 let rec picka w s l =
325 | hd::tl when not (EqualitySet.mem hd s) ->
326 let w, s, l = picka (w-1) s tl in
327 w, EqualitySet.add hd s, hd::l
329 let w, s, l = picka w s tl in
334 let _, ps, pl = picka in_age ps pl in
335 if not (EqualitySet.is_empty ps) then
336 maximal_retained_equality := Some (EqualitySet.max_elt ps);
339 (fun e tbl -> Indexing.index tbl e) ps Indexing.empty
345 (** inference of new equalities between current and some in active *)
346 let infer bag eq_uri env current (active_list, active_table) =
348 if Utils.debug_metas then
349 (ignore(Indexing.check_target bag c current "infer1");
350 ignore(List.map (function current -> Indexing.check_target bag c current "infer2") active_list));
352 let maxm, copy_of_current = Equality.fix_metas bag !maxmeta current in
354 let active_table = Indexing.index active_table copy_of_current in
355 (* let _ = <:start<current contro active>> in *)
357 Indexing.superposition_right bag eq_uri !maxmeta env active_table current
359 (* let _ = <:stop<current contro active>> in *)
360 if Utils.debug_metas then
363 Indexing.check_target bag c current "sup0") res);
365 let rec infer_positive table = function
369 Indexing.superposition_right bag
370 ~subterms_only:true eq_uri !maxmeta env table equality
373 if Utils.debug_metas then
377 Indexing.check_target bag c current "sup2") res);
378 let pos = infer_positive table tl in
382 let maxm, copy_of_current = Equality.fix_metas !maxmeta current in
385 let curr_table = Indexing.index Indexing.empty current in
386 (* let _ = <:start<active contro current>> in *)
387 let pos = infer_positive curr_table ((*copy_of_current::*)active_list) in
388 (* let _ = <:stop<active contro current>> in *)
389 if Utils.debug_metas then
392 Indexing.check_target bag c current "sup3") pos);
395 derived_clauses := !derived_clauses + (List.length new_pos);
396 match !maximal_retained_equality with
399 ignore(assert false);
400 (* if we have a maximal_retained_equality, we can discard all equalities
401 "greater" than it, as they will never be reached... An equality is
402 greater than maximal_retained_equality if it is bigger
403 wrt. OrderedEquality.compare and it is less similar than
404 maximal_retained_equality to the current goal *)
405 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
408 let check_for_deep_subsumption env active_table eq =
409 let _,_,(eq_ty, left, right, order),metas,id = Equality.open_equality eq in
410 let check_subsumed deep l r =
412 Equality.mk_tmp_equality(0,(eq_ty,l,r,Utils.Incomparable),metas)in
413 match Indexing.subsumption env active_table eqtmp with
417 let rec aux b (ok_so_far, subsumption_used) t1 t2 =
419 | t1, t2 when not ok_so_far -> ok_so_far, subsumption_used
420 | t1, t2 when subsumption_used -> t1 = t2, subsumption_used
421 | Cic.Appl (h1::l),Cic.Appl (h2::l') ->
422 let rc = check_subsumed b t1 t2 in
428 (fun (ok_so_far, subsumption_used) t t' ->
429 aux true (ok_so_far, subsumption_used) t t')
430 (ok_so_far, subsumption_used) l l'
431 with Invalid_argument _ -> false,subsumption_used)
433 false, subsumption_used
434 | _ -> false, subsumption_used
436 fst (aux false (true,false) left right)
439 (** simplifies current using active and passive *)
440 let forward_simplify bag eq_uri env current (active_list, active_table) =
441 prerr_endline (Equality.string_of_equality ~env current);
442 let _, context, _ = env in
443 let demodulate table current =
444 let newmeta, newcurrent =
445 Indexing.demodulation_equality bag eq_uri !maxmeta env table current
448 if Equality.is_weak_identity newcurrent then None else Some newcurrent
451 if Utils.debug_metas then
452 ignore (Indexing.check_target bag context current "demod0");
453 let res = demodulate active_table current in
454 if Utils.debug_metas then
455 ignore ((function None -> () | Some x ->
456 ignore (Indexing.check_target bag context x "demod1");()) res);
459 let res = demod current in
463 if Indexing.in_index active_table c ||
464 check_for_deep_subsumption env active_table c
471 (** simplifies new using active and passive *)
472 let forward_simplify_new bag eq_uri env new_pos active =
473 if Utils.debug_metas then
477 (fun current -> Indexing.check_target bag c current "forward new pos")
480 let active_list, active_table = active in
481 let demodulate table target =
482 let newmeta, newtarget =
483 Indexing.demodulation_equality bag eq_uri !maxmeta env table target
488 (* we could also demodulate using passive. Currently we don't *)
489 let new_pos = List.map (demodulate active_table) new_pos in
493 if not (Equality.is_identity env e) then
496 EqualitySet.empty new_pos
498 let new_pos = EqualitySet.elements new_pos_set in
500 let subs e = Indexing.subsumption env active_table e = None in
501 let is_duplicate e = not (Indexing.in_index active_table e) in
502 List.filter subs (List.filter is_duplicate new_pos)
506 (** simplifies a goal with equalities in active and passive *)
507 let rec simplify_goal bag env goal (active_list, active_table) =
508 let demodulate table goal = Indexing.demodulation_goal bag env table goal in
509 let changed, goal = demodulate active_table goal in
514 snd (simplify_goal bag env goal (active_list, active_table))
518 let simplify_goals bag env goals active =
519 let a_goals, p_goals = goals in
520 let p_goals = List.map (fun g -> snd (simplify_goal bag env g active)) p_goals in
521 let a_goals = List.map (fun g -> snd (simplify_goal bag env g active)) a_goals in
526 (** simplifies active usign new *)
527 let backward_simplify_active
528 bag eq_uri env new_pos new_table min_weight active
530 let active_list, active_table = active in
531 let active_list, newa, pruned =
533 (fun equality (res, newn,pruned) ->
534 let ew, _, _, _,id = Equality.open_equality equality in
535 if ew < min_weight then
536 equality::res, newn,pruned
539 forward_simplify bag eq_uri env equality (new_pos, new_table)
541 | None -> res, newn, id::pruned
543 if Equality.compare equality e = 0 then
546 res, e::newn, pruned)
547 active_list ([], [],[])
550 List.exists (Equality.meta_convertibility_eq eq1) where
553 let _, _, _, _,id = Equality.open_equality eq in id
555 let ((active1,pruned),tbl), newa =
557 (fun eq ((res,pruned), tbl) ->
558 if List.mem eq res then
559 (res, (id_of_eq eq)::pruned),tbl
560 else if (Equality.is_weak_identity eq) || (find eq res) then (
561 (res, (id_of_eq eq)::pruned),tbl
564 (eq::res,pruned), Indexing.index tbl eq)
565 active_list (([],pruned), Indexing.empty),
568 if (Equality.is_identity env eq) then p
573 | [] -> (active1,tbl), None, pruned
574 | _ -> (active1,tbl), Some newa, pruned
578 (** simplifies passive using new *)
579 let backward_simplify_passive
580 bag eq_uri env new_pos new_table min_weight passive
582 let (pl, ps), passive_table = passive in
583 let f equality (resl, ress, newn) =
584 let ew, _, _, _ , _ = Equality.open_equality equality in
585 if ew < min_weight then
586 equality::resl, ress, newn
589 forward_simplify bag eq_uri env equality (new_pos, new_table)
591 | None -> resl, EqualitySet.remove equality ress, newn
594 equality::resl, ress, newn
596 let ress = EqualitySet.remove equality ress in
599 let pl, ps, newp = List.fold_right f pl ([], ps, []) in
602 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
605 | [] -> ((pl, ps), passive_table), None
606 | _ -> ((pl, ps), passive_table), Some (newp)
609 let build_table equations =
612 let ew, _, _, _ , _ = Equality.open_equality e in
613 e::l, Indexing.index t e, min ew w)
614 ([], Indexing.empty, 1000000) equations
618 let backward_simplify bag eq_uri env new' active =
619 let new_pos, new_table, min_weight = build_table new' in
620 let active, newa, pruned =
621 backward_simplify_active bag eq_uri env new_pos new_table min_weight active
626 let close bag eq_uri env new' given =
627 let new_pos, new_table, min_weight =
630 let ew, _, _, _ , _ = Equality.open_equality e in
631 e::l, Indexing.index t e, min ew w)
632 ([], Indexing.empty, 1000000) (snd new')
636 let pos = infer bag eq_uri env c (new_pos,new_table) in
641 let is_commutative_law eq =
642 let w, proof, (eq_ty, left, right, order), metas , _ =
643 Equality.open_equality eq
645 match left,right with
646 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
647 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
648 f1 = f2 && a1 = b2 && a2 = b1
652 let prova bag eq_uri env new' active =
653 let given = List.filter is_commutative_law (fst active) in
657 (Printf.sprintf "symmetric:\n%s\n"
660 (fun e -> Equality.string_of_equality ~env e)
662 close bag eq_uri env new' given
665 (* returns an estimation of how many equalities in passive can be activated
666 within the current time limit *)
667 let get_selection_estimate () =
668 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
669 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
671 ceil ((float_of_int !processed_clauses) *.
672 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
676 (** initializes the set of goals *)
677 let make_goals goal =
679 and passive = [0, [goal]] in
683 let make_goal_set goal =
687 (** initializes the set of theorems *)
688 let make_theorems theorems =
693 let activate_goal (active, passive) =
696 | goal_conj::tl -> true, (goal_conj::active, tl)
697 | [] -> false, (active, passive)
699 true, (active,passive)
703 let activate_theorem (active, passive) =
705 | theorem::tl -> true, (theorem::active, tl)
706 | [] -> false, (active, passive)
711 let simplify_theorems bag env theorems ?passive (active_list, active_table) =
712 let pl, passive_table =
715 | Some ((pn, _), (pp, _), pt) -> pn @ pp, Some pt
717 let a_theorems, p_theorems = theorems in
718 let demodulate table theorem =
719 let newmeta, newthm =
720 Indexing.demodulation_theorem bag !maxmeta env table theorem in
722 theorem != newthm, newthm
724 let foldfun table (a, p) theorem =
725 let changed, theorem = demodulate table theorem in
726 if changed then (a, theorem::p) else (theorem::a, p)
728 let mapfun table theorem = snd (demodulate table theorem) in
729 match passive_table with
731 let p_theorems = List.map (mapfun active_table) p_theorems in
732 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
733 | Some passive_table ->
734 let p_theorems = List.map (mapfun active_table) p_theorems in
735 let p_theorems, a_theorems =
736 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
737 let p_theorems = List.map (mapfun passive_table) p_theorems in
738 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
742 let rec simpl bag eq_uri env e others others_simpl =
743 let active = others @ others_simpl in
747 if Equality.is_weak_identity e then t else Indexing.index t e)
748 Indexing.empty active
751 if Equality.is_weak_identity e then None else
752 forward_simplify bag eq_uri env e (active, tbl)
757 | None -> simpl bag eq_uri env hd tl others_simpl
758 | Some e -> simpl bag eq_uri env hd tl (e::others_simpl)
762 | None -> others_simpl
763 | Some e -> e::others_simpl
767 let simplify_equalities bag eq_uri env equalities =
770 (Printf.sprintf "equalities:\n%s\n"
772 (List.map Equality.string_of_equality equalities))));
773 Utils.debug_print (lazy "SIMPLYFYING EQUALITIES...");
774 match equalities with
778 List.rev (simpl bag eq_uri env hd tl [])
782 (Printf.sprintf "equalities AFTER:\n%s\n"
784 (List.map Equality.string_of_equality res))));
788 let print_goals goals =
795 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
797 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
800 let pp_goal_set msg goals names =
801 let active_goals, passive_goals = goals in
802 prerr_endline ("////" ^ msg);
803 prerr_endline ("ACTIVE G: " ^
804 (String.concat "\n " (List.map (fun (_,_,g) -> CicPp.pp g names)
806 prerr_endline ("PASSIVE G: " ^
807 (String.concat "\n " (List.map (fun (_,_,g) -> CicPp.pp g names)
811 let check_if_goal_is_subsumed bag ((_,ctx,_) as env) table (goalproof,menv,ty) =
812 (* let names = Utils.names_of_context ctx in *)
814 | Cic.Appl[Cic.MutInd(uri,_,_);eq_ty;left;right]
815 when LibraryObjects.is_eq_URI uri ->
817 Equality.mk_equality bag
818 (0,Equality.Exact (Cic.Implicit None),(eq_ty,left,right,Utils.Eq),menv)
820 (* match Indexing.subsumption env table goal_equation with*)
821 match Indexing.unification env table goal_equation with
822 | Some (subst, equality, swapped ) ->
825 ("GOAL SUBSUMED IS: "^Equality.string_of_equality goal_equation ~env);
827 ("GOAL IS SUBSUMED BY: "^Equality.string_of_equality equality ~env);
828 prerr_endline ("SUBST:"^Subst.ppsubst ~names subst);
830 let (_,p,(ty,l,r,_),m,id) = Equality.open_equality equality in
831 let cicmenv = Subst.apply_subst_metasenv subst (m @ menv) in
834 Equality.symmetric bag eq_ty l id uri m
838 Some (goalproof, p, id, subst, cicmenv)
843 let check_if_goal_is_identity env = function
844 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
845 when left = right && LibraryObjects.is_eq_URI uri ->
846 let reflproof = Equality.Exact (Equality.refl_proof uri eq_ty left) in
847 Some (goalproof, reflproof, 0, Subst.empty_subst,m)
848 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right])
849 when LibraryObjects.is_eq_URI uri ->
850 (let _,context,_ = env in
853 Inference.unification m m context left right CicUniv.empty_ugraph
855 let reflproof = Equality.Exact (Equality.refl_proof uri eq_ty left) in
856 let m = Subst.apply_subst_metasenv s m in
857 Some (goalproof, reflproof, 0, s,m)
862 let rec check goal = function
866 | None -> check goal tl
867 | (Some p) as ok -> ok
870 let simplify_goal_set bag env goals active =
871 let active_goals, passive_goals = goals in
872 let find (_,_,g) where =
873 List.exists (fun (_,_,g1) -> Equality.meta_convertibility g g1) where
875 (* prova:tengo le passive semplificate
877 List.map (fun g -> snd (simplify_goal env g active)) passive_goals
880 (fun (acc_a,acc_p) goal ->
881 match simplify_goal bag env goal active with
884 if find g acc_p then acc_a,acc_p else acc_a,g::acc_p
886 if find g acc_a then acc_a,acc_p else g::acc_a,acc_p)
887 ([],passive_goals) active_goals
890 let check_if_goals_set_is_solved bag env active goals =
891 let active_goals, passive_goals = goals in
898 check_if_goal_is_identity env;
899 check_if_goal_is_subsumed bag env (snd active)])
900 (* provare active and passive?*)
904 let infer_goal_set bag env active goals =
905 let active_goals, passive_goals = goals in
906 let rec aux = function
907 | [] -> active_goals, []
909 let changed,selected = simplify_goal bag env hd active in
912 prerr_endline ("--------------- goal semplificato");
914 let (_,_,t1) = selected in
916 List.exists (fun (_,_,t) -> Equality.meta_convertibility t t1)
922 let passive_goals = tl in
923 let new_passive_goals =
924 if Utils.metas_of_term t1 = [] then passive_goals
926 let newmaxmeta,new' =
927 Indexing.superposition_left bag env (snd active) selected
930 maxmeta := newmaxmeta;
933 selected::active_goals, new_passive_goals
938 let infer_goal_set_with_current bag env current goals active =
939 let active_goals, passive_goals = simplify_goal_set bag env goals active in
940 let l,table,_ = build_table [current] in
944 let newmaxmeta, new' = Indexing.superposition_left bag env table g !maxmeta in
945 maxmeta := newmaxmeta;
947 passive_goals active_goals
952 let ids = List.map (fun _,_,i,_,_ -> i) p in
956 let ids_of_goal_set (ga,gp) =
957 List.flatten (List.map ids_of_goal ga) @
958 List.flatten (List.map ids_of_goal gp)
961 let size_of_goal_set_a (l,_) = List.length l;;
962 let size_of_goal_set_p (_,l) = List.length l;;
964 let pp_goals label goals context =
965 let names = Utils.names_of_context context in
969 (Printf.sprintf "Current goal: %s = %s\n" label (CicPp.pp g names)))
974 (Printf.sprintf "PASSIVE goal: %s = %s\n" label (CicPp.pp g names)))
978 let print_status iterno goals active passive =
980 (Printf.sprintf "\n%d #ACTIVES: %d #PASSIVES: %d #GOALSET: %d(%d)"
981 iterno (size_of_active active) (size_of_passive passive)
982 (size_of_goal_set_a goals) (size_of_goal_set_p goals))
985 (** given-clause algorithm with full reduction strategy: NEW implementation *)
986 (* here goals is a set of goals in OR *)
988 bag eq_uri ((_,context,_) as env) goals passive active
989 goal_steps saturation_steps max_time
991 let initial_time = Unix.gettimeofday () in
992 let iterations_left iterno =
993 let now = Unix.gettimeofday () in
994 let time_left = max_time -. now in
995 let time_spent_until_now = now -. initial_time in
996 let iteration_medium_cost =
997 time_spent_until_now /. (float_of_int iterno)
999 let iterations_left = time_left /. iteration_medium_cost in
1000 int_of_float iterations_left
1002 let rec step goals passive active g_iterno s_iterno =
1003 if g_iterno > goal_steps && s_iterno > saturation_steps then
1004 (ParamodulationFailure ("No more iterations to spend",active,passive))
1005 else if Unix.gettimeofday () > max_time then
1006 (ParamodulationFailure ("No more time to spend",active,passive))
1009 print_status (max g_iterno s_iterno) goals active passive
1010 (* Printf.eprintf ".%!"; *)
1012 (* PRUNING OF PASSIVE THAT WILL NEVER BE PROCESSED *)
1014 let selection_estimate = iterations_left (max g_iterno s_iterno) in
1015 let kept = size_of_passive passive in
1016 if kept > selection_estimate then
1018 (*Printf.eprintf "Too many passive equalities: pruning...";
1019 prune_passive selection_estimate active*) passive
1024 kept_clauses := (size_of_passive passive) + (size_of_active active);
1026 if g_iterno < goal_steps then
1027 infer_goal_set bag env active goals
1031 match check_if_goals_set_is_solved bag env active goals with
1034 (Printf.sprintf "\nFound a proof in: %f\n"
1035 (Unix.gettimeofday() -. initial_time));
1036 ParamodulationSuccess (p,active,passive)
1039 if passive_is_empty passive then
1040 if no_more_passive_goals goals then
1041 ParamodulationFailure
1042 ("No more passive equations/goals",active,passive)
1043 (*maybe this is a success! *)
1045 step goals passive active (g_iterno+1) (s_iterno+1)
1048 (* COLLECTION OF GARBAGED EQUALITIES *)
1049 if max g_iterno s_iterno mod 40 = 0 then
1051 print_status (max g_iterno s_iterno) goals active passive;
1052 let active = List.map Equality.id_of (fst active) in
1053 let passive = List.map Equality.id_of (fst passive) in
1054 let goal = ids_of_goal_set goals in
1055 Equality.collect bag active passive goal
1058 if s_iterno < saturation_steps then
1059 let current, passive = select env goals passive in
1060 (* SIMPLIFICATION OF CURRENT *)
1063 Equality.string_of_equality ~env current);
1064 forward_simplify bag eq_uri env current active, passive
1069 | None -> step goals passive active (g_iterno+1) (s_iterno+1)
1071 (* GENERATION OF NEW EQUATIONS *)
1072 (* prerr_endline "infer"; *)
1073 let new' = infer bag eq_uri env current active in
1074 (* prerr_endline "infer goal"; *)
1076 match check_if_goals_set_is_solved env active goals with
1079 (Printf.sprintf "Found a proof in: %f\n"
1080 (Unix.gettimeofday() -. initial_time));
1081 ParamodulationSuccess p
1086 let al, tbl = active in
1087 al @ [current], Indexing.index tbl current
1090 infer_goal_set_with_current bag env current goals active
1093 (* FORWARD AND BACKWARD SIMPLIFICATION *)
1094 (* prerr_endline "fwd/back simpl"; *)
1095 let rec simplify new' active passive =
1097 forward_simplify_new bag eq_uri env new' active
1099 let active, newa, pruned =
1100 backward_simplify bag eq_uri env new' active
1103 List.fold_left (filter_dependent bag) passive pruned
1106 | None -> active, passive, new'
1107 | Some p -> simplify (new' @ p) active passive
1109 let active, passive, new' =
1110 simplify new' active passive
1113 (* prerr_endline "simpl goal with new"; *)
1115 let a,b,_ = build_table new' in
1116 (* let _ = <:start<simplify_goal_set new>> in *)
1117 let rc = simplify_goal_set bag env goals (a,b) in
1118 (* let _ = <:stop<simplify_goal_set new>> in *)
1121 let passive = add_to_passive passive new' [] in
1122 step goals passive active (g_iterno+1) (s_iterno+1)
1125 step goals passive active 1 1
1128 let rec saturate_equations bag eq_uri env goal accept_fun passive active =
1129 elapsed_time := Unix.gettimeofday () -. !start_time;
1130 if !elapsed_time > !time_limit then
1133 let current, passive = select env ([goal],[]) passive in
1134 let res = forward_simplify bag eq_uri env current active in
1137 saturate_equations bag eq_uri env goal accept_fun passive active
1139 Utils.debug_print (lazy (Printf.sprintf "selected: %s"
1140 (Equality.string_of_equality ~env current)));
1141 let new' = infer bag eq_uri env current active in
1143 if Equality.is_identity env current then active
1145 let al, tbl = active in
1146 al @ [current], Indexing.index tbl current
1148 (* alla fine new' contiene anche le attive semplificate!
1149 * quindi le aggiungo alle passive insieme alle new *)
1150 let rec simplify new' active passive =
1151 let new' = forward_simplify_new bag eq_uri env new' active in
1152 let active, newa, pruned =
1153 backward_simplify bag eq_uri env new' active in
1155 List.fold_left (filter_dependent bag) passive pruned in
1157 | None -> active, passive, new'
1158 | Some p -> simplify (new' @ p) active passive
1160 let active, passive, new' = simplify new' active passive in
1164 (Printf.sprintf "active:\n%s\n"
1167 (fun e -> Equality.string_of_equality ~env e)
1173 (Printf.sprintf "new':\n%s\n"
1176 (fun e -> "Negative " ^
1177 (Equality.string_of_equality ~env e)) new'))))
1179 let new' = List.filter accept_fun new' in
1180 let passive = add_to_passive passive new' [] in
1181 saturate_equations bag eq_uri env goal accept_fun passive active
1184 let default_depth = !maxdepth
1185 and default_width = !maxwidth;;
1189 symbols_counter := 0;
1190 weight_age_counter := !weight_age_ratio;
1191 processed_clauses := 0;
1194 maximal_retained_equality := None;
1196 forward_simpl_time := 0.;
1197 forward_simpl_new_time := 0.;
1198 backward_simpl_time := 0.;
1199 passive_maintainance_time := 0.;
1200 derived_clauses := 0;
1204 let eq_of_goal = function
1205 | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
1207 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1210 let eq_and_ty_of_goal = function
1211 | Cic.Appl [Cic.MutInd(uri,0,_);t;_;_] when LibraryObjects.is_eq_URI uri ->
1213 | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
1216 (* status: input proof status
1217 * goalproof: forward steps on goal
1218 * newproof: backward steps
1219 * subsumption_id: the equation used if goal is closed by subsumption
1220 * (0 if not closed by subsumption) (DEBUGGING: can be safely removed)
1221 * subsumption_subst: subst to make newproof and goalproof match
1222 * proof_menv: final metasenv
1226 goalproof newproof subsumption_id subsumption_subst proof_menv
1228 let proof, goalno = status in
1229 let uri, metasenv, meta_proof, term_to_prove = proof in
1230 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1231 let eq_uri = eq_of_goal type_of_goal in
1232 let names = Utils.names_of_context context in
1233 prerr_endline "Proof:";
1235 (Equality.pp_proof bag names goalproof newproof subsumption_subst
1236 subsumption_id type_of_goal);
1237 (* prerr_endline "ENDOFPROOFS"; *)
1239 prerr_endline ("max weight: " ^
1240 (string_of_int (Equality.max_weight goalproof newproof)));
1242 (* generation of the CIC proof *)
1244 List.filter (fun i -> i <> goalno)
1245 (ProofEngineHelpers.compare_metasenvs
1246 ~newmetasenv:metasenv ~oldmetasenv:proof_menv)
1248 let goal_proof, side_effects_t =
1249 let initial = Equality.add_subst subsumption_subst newproof in
1250 Equality.build_goal_proof bag
1251 eq_uri goalproof initial type_of_goal side_effects
1254 (* prerr_endline ("PROOF: " ^ CicPp.pp goal_proof names); *)
1255 let goal_proof = Subst.apply_subst subsumption_subst goal_proof in
1256 let metas_still_open_in_proof = Utils.metas_of_term goal_proof in
1257 (*prerr_endline (CicPp.pp goal_proof names);*)
1259 let goal_proof = (* Subst.apply_subst subsumption_subst *) goal_proof in
1260 let side_effects_t =
1261 List.map (Subst.apply_subst subsumption_subst) side_effects_t
1263 (* replacing fake mets with real ones *)
1264 (* prerr_endline "replacing metas..."; *)
1265 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1266 let goal_proof_menv, what, with_what,free_meta =
1268 (fun (acc1,acc2,acc3,uniq) (i,_,ty) ->
1271 (* acc1, (Cic.Meta(i,[]))::acc2, m::acc3, uniq *)
1272 (i,context,ty)::acc1, (Cic.Meta(i,[]))::acc2, (Cic.Meta(i,irl))::acc3, uniq
1274 [i,context,ty], (Cic.Meta(i,[]))::acc2,
1275 (Cic.Meta(i,irl)) ::acc3,Some (Cic.Meta(i,irl)))
1278 (fun (i,_,_) -> List.mem i metas_still_open_in_proof)
1282 (* we need this fake equality since the metas of the hypothesis may be
1283 * with a real local context *)
1284 ProofEngineReduction.replace_lifting
1285 ~equality:(fun x y ->
1286 match x,y with Cic.Meta(i,_),Cic.Meta(j,_) -> i=j | _-> false)
1287 ~what ~with_what ~where
1289 let goal_proof = replace goal_proof in
1290 (* ok per le meta libere... ma per quelle che c'erano e sono rimaste?
1291 * what mi pare buono, sostituisce solo le meta farlocche *)
1292 let side_effects_t = List.map replace side_effects_t in
1294 List.filter (fun i -> i <> goalno)
1295 (ProofEngineHelpers.compare_metasenvs
1296 ~oldmetasenv:metasenv ~newmetasenv:goal_proof_menv)
1298 (* prerr_endline ("freemetas: " ^ String.concat "," (List.map string_of_int
1300 (* check/refine/... build the new proof *)
1302 ProofEngineReduction.replace
1303 ~what:side_effects ~with_what:side_effects_t
1304 ~equality:(fun i t -> match t with Cic.Meta(j,_)->j=i|_->false)
1307 let subst_side_effects,real_menv,_ =
1308 let fail t s = raise (ProofEngineTypes.Fail (lazy (t^Lazy.force s))) in
1309 let free_metas_menv =
1310 List.map (fun i -> CicUtil.lookup_meta i goal_proof_menv) free_metas
1313 prerr_endline ("XX type_of_goal " ^ CicPp.ppterm type_of_goal);
1314 prerr_endline ("XX replaced_goal " ^ CicPp.ppterm replaced_goal);
1315 prerr_endline ("XX metasenv " ^
1316 CicMetaSubst.ppmetasenv [] (metasenv @ free_metas_menv));
1319 CicUnification.fo_unif_subst [] context (metasenv @ free_metas_menv)
1320 replaced_goal type_of_goal CicUniv.empty_ugraph
1322 | CicUnification.UnificationFailure s
1323 | CicUnification.Uncertain s
1324 | CicUnification.AssertFailure s ->
1325 fail "Maybe the local context of metas in the goal was not an IRL" s
1328 (goalno,(context,goal_proof,type_of_goal))::subst_side_effects
1330 (* prerr_endline ("MENVreal_menv: " ^ CicMetaSubst.ppmetasenv [] real_menv); *)
1333 CicTypeChecker.type_of_aux' real_menv context goal_proof
1334 CicUniv.empty_ugraph
1336 | CicUtil.Meta_not_found _
1337 | CicTypeChecker.TypeCheckerFailure _
1338 | CicTypeChecker.AssertFailure _
1339 | Invalid_argument "list_fold_left2" as exn ->
1340 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1341 prerr_endline (CicPp.pp goal_proof names);
1342 prerr_endline "THE PROOF DOES NOT TYPECHECK!";
1345 let proof, real_metasenv =
1346 ProofEngineHelpers.subst_meta_and_metasenv_in_proof
1347 proof goalno (CicMetaSubst.apply_subst final_subst) real_menv
1350 match free_meta with Some(Cic.Meta(m,_)) when m<>goalno ->[m] | _ ->[]
1354 "GOALS APERTI: %s\nMETASENV PRIMA:\n%s\nMETASENV DOPO:\n%s\n"
1355 (String.concat ", " (List.map string_of_int open_goals))
1356 (CicMetaSubst.ppmetasenv [] metasenv)
1357 (CicMetaSubst.ppmetasenv [] real_metasenv);
1359 final_subst, proof, open_goals
1362 let find_equalities dbd status smart_flag ?auto cache =
1363 let proof, goalno = status in
1364 let _, metasenv,_,_ = proof in
1365 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1366 let eq_uri = eq_of_goal type_of_goal in
1367 let env = (metasenv, context, CicUniv.empty_ugraph) in
1368 let bag = Equality.mk_equality_bag () in
1369 let eq_indexes, equalities, maxm, cache =
1370 Equality_retrieval.find_context_equalities 0 bag ?auto context proof cache
1372 prerr_endline ">>>>>>>>>> gained from the context >>>>>>>>>>>>";
1373 List.iter (fun e -> prerr_endline (Equality.string_of_equality e)) equalities;
1374 prerr_endline ">>>>>>>>>>>>>>>>>>>>>>";
1375 let lib_eq_uris, library_equalities, maxm, cache =
1376 Equality_retrieval.find_library_equalities bag
1377 ?auto smart_flag dbd context (proof, goalno) (maxm+2)
1380 prerr_endline (">>>>>>>>>> gained from the library >>>>>>>>>>>>" ^
1381 string_of_int maxm);
1383 (fun (_,e) -> prerr_endline (Equality.string_of_equality e))
1385 prerr_endline ">>>>>>>>>>>>>>>>>>>>>>";
1386 let equalities = List.map snd library_equalities @ equalities in
1388 simplify_equalities bag eq_uri env equalities
1390 prerr_endline ">>>>>>>>>> after simplify >>>>>>>>>>>>";
1392 (fun e -> prerr_endline (Equality.string_of_equality e)) equalities;
1393 prerr_endline (">>>>>>>>>>>>>>>>>>>>>>" ^ string_of_int maxm);
1394 bag, equalities, cache, maxm
1397 let saturate_more bag active maxmeta status smart_flag ?auto cache =
1398 let proof, goalno = status in
1399 let _, metasenv,_,_ = proof in
1400 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1401 let eq_uri = eq_of_goal type_of_goal in
1402 let env = (metasenv, context, CicUniv.empty_ugraph) in
1403 let eq_indexes, equalities, maxm, cache =
1404 Equality_retrieval.find_context_equalities maxmeta bag ?auto context proof cache
1406 prerr_endline (">>>>>>> gained from a new context saturation >>>>>>>>>" ^
1407 string_of_int maxm);
1409 (fun e -> prerr_endline (Equality.string_of_equality ~env e))
1411 prerr_endline ">>>>>>>>>>>>>>>>>>>>>>";
1414 (fun e -> forward_simplify bag eq_uri env e active)
1417 prerr_endline ">>>>>>>>>> after simplify >>>>>>>>>>>>";
1419 (fun e -> prerr_endline (Equality.string_of_equality ~env e)) equalities;
1420 prerr_endline (">>>>>>>>>>>>>>>>>>>>>>" ^ string_of_int maxm);
1421 bag, equalities, cache, maxm
1425 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width)
1426 ?(timeout=600.) ?auto status =
1427 let module C = Cic in
1431 (* CicUnification.unif_ty := false;*)
1432 let proof, goalno = status in
1433 let uri, metasenv, meta_proof, term_to_prove = proof in
1434 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1435 let eq_uri = eq_of_goal type_of_goal in
1436 let cleaned_goal = Utils.remove_local_context type_of_goal in
1437 Utils.set_goal_symbols cleaned_goal; (* DISACTIVATED *)
1438 let ugraph = CicUniv.empty_ugraph in
1439 let env = (metasenv, context, ugraph) in
1440 let goal = [], List.filter (fun (i,_,_)->i<>goalno) metasenv, cleaned_goal in
1441 let bag, equalities, cache, maxm =
1442 find_equalities dbd status smart_flag ?auto AutoCache.cache_empty
1446 let t1 = Unix.gettimeofday () in
1448 let refl_equal = LibraryObjects.eq_refl_URI ~eq:eq_uri in
1449 let t = CicUtil.term_of_uri refl_equal in
1450 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1453 let t2 = Unix.gettimeofday () in
1458 "Theorems:\n-------------------------------------\n%s\n"
1463 "Term: %s, type: %s"
1464 (CicPp.ppterm t) (CicPp.ppterm ty))
1468 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1470 let active = make_empty_active () in
1471 let passive = make_passive equalities in
1472 let start = Unix.gettimeofday () in
1475 let goals = make_goals goal in
1476 given_clause_fullred dbd env goals theorems passive active
1478 let goals = make_goal_set goal in
1479 let max_iterations = 10000 in
1480 let max_time = Unix.gettimeofday () +. timeout (* minutes *) in
1482 eq_uri env goals passive active max_iterations max_iterations max_time
1484 let finish = Unix.gettimeofday () in
1485 (res, finish -. start)
1488 | ParamodulationFailure (s,_,_) ->
1489 raise (ProofEngineTypes.Fail (lazy ("NO proof found: " ^ s)))
1490 | ParamodulationSuccess
1491 ((goalproof,newproof,subsumption_id,subsumption_subst, proof_menv),_,_) ->
1492 prerr_endline (Printf.sprintf "\nTIME NEEDED: %8.2f" time);
1495 status goalproof newproof subsumption_id subsumption_subst proof_menv
1499 (* **************** HERE ENDS THE PARAMODULATION STUFF ******************** *)
1501 (* exported version of given_clause *)
1503 bag maxm status active passive goal_steps saturation_steps max_time
1509 (fun acc e -> let _,_,_,menv,_ = Equality.open_equality e in
1510 List.fold_left (fun acc (i,_,_) -> max i acc) acc menv)
1513 let active_l = fst active in
1514 let passive_l = fst passive in
1515 let ma = max_l active_l in
1516 let mp = max_l passive_l in
1517 assert (maxm > max ma mp);
1518 let proof, goalno = status in
1519 let uri, metasenv, meta_proof, term_to_prove = proof in
1520 let _, context, type_of_goal = CicUtil.lookup_meta goalno metasenv in
1521 let eq_uri = eq_of_goal type_of_goal in
1522 let cleaned_goal = Utils.remove_local_context type_of_goal in
1523 Utils.set_goal_symbols cleaned_goal; (* DISACTIVATED *)
1524 let goal = [], List.filter (fun (i,_,_)->i<>goalno) metasenv, cleaned_goal in
1525 let env = metasenv,context,CicUniv.empty_ugraph in
1526 prerr_endline ">>>>>> ACTIVES >>>>>>>>";
1527 List.iter (fun e -> prerr_endline (Equality.string_of_equality ~env e))
1529 prerr_endline ">>>>>>>>>>>>>>";
1530 let goals = make_goal_set goal in
1532 given_clause bag eq_uri env goals passive active
1533 goal_steps saturation_steps max_time
1535 | ParamodulationFailure (_,a,p) ->
1536 None, a, p, !maxmeta
1537 | ParamodulationSuccess
1538 ((goalproof,newproof,subsumption_id,subsumption_subst, proof_menv),a,p) ->
1539 let subst, proof, gl =
1541 status goalproof newproof subsumption_id subsumption_subst proof_menv
1543 Some (subst, proof,gl),a,p, !maxmeta
1546 let demodulate_tac ~dbd ((proof,goal)(*s initialstatus*)) =
1547 let curi,metasenv,pbo,pty = proof in
1548 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
1549 let eq_uri = eq_of_goal ty in
1550 let bag = Equality.mk_equality_bag () in
1551 let eq_indexes, equalities, maxm, cache =
1552 Equality_retrieval.find_context_equalities 0 bag context proof AutoCache.cache_empty
1554 let lib_eq_uris, library_equalities, maxm, cache =
1555 Equality_retrieval.find_library_equalities bag
1556 false dbd context (proof, goal) (maxm+2) cache
1558 if library_equalities = [] then prerr_endline "VUOTA!!!";
1559 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1560 let library_equalities = List.map snd library_equalities in
1561 let initgoal = [], [], ty in
1562 let env = (metasenv, context, CicUniv.empty_ugraph) in
1564 simplify_equalities bag eq_uri env (equalities@library_equalities)
1568 (fun tbl eq -> Indexing.index tbl eq)
1569 Indexing.empty equalities
1571 let changed,(newproof,newmetasenv, newty) =
1572 Indexing.demodulation_goal bag
1573 (metasenv,context,CicUniv.empty_ugraph) table initgoal
1577 let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
1579 Equality.build_goal_proof bag
1580 eq_uri newproof opengoal ty [] context metasenv
1582 let extended_metasenv = (maxm,context,newty)::metasenv in
1583 let extended_status =
1584 (curi,extended_metasenv,pbo,pty),goal in
1585 let (status,newgoals) =
1586 ProofEngineTypes.apply_tactic
1587 (PrimitiveTactics.apply_tac ~term:proofterm)
1589 (status,maxm::newgoals)
1591 else (* if newty = ty then *)
1592 raise (ProofEngineTypes.Fail (lazy "no progress"))
1593 (*else ProofEngineTypes.apply_tactic
1594 (ReductionTactics.simpl_tac
1595 ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
1598 let demodulate_tac ~dbd = ProofEngineTypes.mk_tactic (demodulate_tac ~dbd);;
1600 let rec find_in_ctx i name = function
1601 | [] -> raise (ProofEngineTypes.Fail (lazy ("Hypothesis not found: " ^ name)))
1602 | Some (Cic.Name name', _)::tl when name = name' -> i
1603 | _::tl -> find_in_ctx (i+1) name tl
1606 let rec position_of i x = function
1607 | [] -> assert false
1608 | j::tl when j <> x -> position_of (i+1) x tl
1613 * auto superposition target = NAME
1614 * [table = NAME_LIST] [demod_table = NAME_LIST] [subterms_only]
1616 * - if table is omitted no superposition will be performed
1617 * - if demod_table is omitted no demodulation will be prformed
1618 * - subterms_only is passed to Indexing.superposition_right
1620 * lists are coded using _ (example: H_H1_H2)
1623 let superposition_tac ~target ~table ~subterms_only ~demod_table status =
1625 let proof,goalno = status in
1626 let curi,metasenv,pbo,pty = proof in
1627 let metano,context,ty = CicUtil.lookup_meta goalno metasenv in
1628 let eq_uri,tty = eq_and_ty_of_goal ty in
1629 let env = (metasenv, context, CicUniv.empty_ugraph) in
1630 let names = Utils.names_of_context context in
1631 let bag = Equality.mk_equality_bag () in
1632 let eq_index, equalities, maxm,cache =
1633 Equality_retrieval.find_context_equalities 0 bag context proof AutoCache.cache_empty
1636 let what = find_in_ctx 1 target context in
1637 List.nth equalities (position_of 0 what eq_index)
1642 let others = Str.split (Str.regexp "_") table in
1643 List.map (fun other -> find_in_ctx 1 other context) others
1646 (fun other -> List.nth equalities (position_of 0 other eq_index))
1651 let index = List.fold_left Indexing.index Indexing.empty eq_other in
1653 if table = "" then maxm,[eq_what] else
1654 Indexing.superposition_right bag
1655 ~subterms_only eq_uri maxm env index eq_what
1657 prerr_endline ("Superposition right:");
1658 prerr_endline ("\n eq: " ^ Equality.string_of_equality eq_what ~env);
1659 prerr_endline ("\n table: ");
1660 List.iter (fun e -> prerr_endline (" " ^ Equality.string_of_equality e ~env)) eq_other;
1661 prerr_endline ("\n result: ");
1662 List.iter (fun e -> prerr_endline (Equality.string_of_equality e ~env)) eql;
1663 prerr_endline ("\n result (cut&paste): ");
1666 let t = Equality.term_of_equality eq_uri e in
1667 prerr_endline (CicPp.pp t names))
1669 prerr_endline ("\n result proofs: ");
1671 prerr_endline (let _,p,_,_,_ = Equality.open_equality e in
1672 let s = match p with Equality.Exact _ -> Subst.empty_subst | Equality.Step (s,_) -> s in
1673 Subst.ppsubst s ^ "\n" ^
1674 CicPp.pp (Equality.build_proof_term bag eq_uri [] 0 p) names)) eql;
1675 if demod_table <> "" then
1678 if eql = [] then [eq_what] else eql
1681 let demod = Str.split (Str.regexp "_") demod_table in
1682 List.map (fun other -> find_in_ctx 1 other context) demod
1686 (fun demod -> List.nth equalities (position_of 0 demod eq_index))
1689 let table = List.fold_left Indexing.index Indexing.empty eq_demod in
1692 (fun (maxm,acc) e ->
1694 Indexing.demodulation_equality bag eq_uri maxm env table e
1699 let eql = List.rev eql in
1700 prerr_endline ("\n result [demod]: ");
1702 (fun e -> prerr_endline (Equality.string_of_equality e ~env)) eql;
1703 prerr_endline ("\n result [demod] (cut&paste): ");
1706 let t = Equality.term_of_equality eq_uri e in
1707 prerr_endline (CicPp.pp t names))
1713 let get_stats () = ""
1715 <:show<Saturation.>> ^ Indexing.get_stats () ^ Inference.get_stats () ^
1716 Equality.get_stats ()
1720 (* THINGS USED ONLY BY saturate_main.ml *)
1722 let main _ _ _ _ _ = () ;;
1724 let retrieve_and_print dbd term metasenv ugraph =
1725 let module C = Cic in
1726 let module T = CicTypeChecker in
1727 let module PET = ProofEngineTypes in
1728 let module PP = CicPp in
1729 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1730 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1731 let proof, goals = status in
1732 let goal' = List.nth goals 0 in
1733 let uri, metasenv, meta_proof, term_to_prove = proof in
1734 let _, context, type_of_goal = CicUtil.lookup_meta goal' metasenv in
1735 let eq_uri = eq_of_goal type_of_goal in
1736 let bag = Equality.mk_equality_bag () in
1737 let eq_indexes, equalities, maxm,cache =
1738 Equality_retrieval.find_context_equalities 0 bag context proof AutoCache.cache_empty in
1739 let ugraph = CicUniv.empty_ugraph in
1740 let env = (metasenv, context, ugraph) in
1741 let t1 = Unix.gettimeofday () in
1742 let lib_eq_uris, library_equalities, maxm, cache =
1743 Equality_retrieval.find_library_equalities bag
1744 false dbd context (proof, goal') (maxm+2) cache
1746 let t2 = Unix.gettimeofday () in
1748 let equalities = (* equalities @ *) library_equalities in
1751 (Printf.sprintf "\n\nequalities:\n%s\n"
1755 (* Printf.sprintf "%s: %s" *)
1756 (UriManager.string_of_uri u)
1757 (* (string_of_equality e) *)
1760 Utils.debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
1761 let rec simpl e others others_simpl =
1763 let active = (others @ others_simpl) in
1766 (fun t (_, e) -> Indexing.index t e)
1767 Indexing.empty active
1769 let res = forward_simplify bag eq_uri env e (active, tbl) in
1773 | None -> simpl hd tl others_simpl
1774 | Some e -> simpl hd tl ((u, e)::others_simpl)
1778 | None -> others_simpl
1779 | Some e -> (u, e)::others_simpl
1783 match equalities with
1786 let others = tl in (* List.map (fun e -> (Utils.Positive, e)) tl in *)
1788 List.rev (simpl (*(Positive,*) hd others [])
1792 (Printf.sprintf "\nequalities AFTER:\n%s\n"
1796 Printf.sprintf "%s: %s"
1797 (UriManager.string_of_uri u)
1798 (Equality.string_of_equality e)
1804 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
1808 let main_demod_equalities dbd term metasenv ugraph =
1809 let module C = Cic in
1810 let module T = CicTypeChecker in
1811 let module PET = ProofEngineTypes in
1812 let module PP = CicPp in
1813 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1814 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1815 let proof, goals = status in
1816 let goal' = List.nth goals 0 in
1817 let _, metasenv, meta_proof, _ = proof in
1818 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1819 let eq_uri = eq_of_goal goal in
1820 let bag = Equality.mk_equality_bag () in
1821 let eq_indexes, equalities, maxm, cache =
1822 Equality_retrieval.find_context_equalities 0 bag context proof AutoCache.cache_empty in
1823 let lib_eq_uris, library_equalities, maxm,cache =
1824 Equality_retrieval.find_library_equalities bag
1825 false dbd context (proof, goal') (maxm+2) cache
1827 let library_equalities = List.map snd library_equalities in
1828 maxmeta := maxm+2; (* TODO ugly!! *)
1829 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1830 let new_meta_goal, metasenv, type_of_goal =
1831 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1834 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
1835 (CicPp.ppterm ty)));
1836 Cic.Meta (maxm+1, irl),
1837 (maxm+1, context, ty)::metasenv,
1840 let env = (metasenv, context, ugraph) in
1842 let goal = [], [], goal
1845 simplify_equalities bag eq_uri env (equalities@library_equalities)
1847 let active = make_empty_active () in
1848 let passive = make_passive equalities in
1849 Printf.eprintf "\ncontext:\n%s\n" (PP.ppcontext context);
1850 Printf.eprintf "\nmetasenv:\n%s\n" (Utils.print_metasenv metasenv);
1851 Printf.eprintf "\nequalities:\n%s\n"
1854 (Equality.string_of_equality ~env) equalities));
1855 prerr_endline "--------------------------------------------------";
1856 prerr_endline "GO!";
1857 start_time := Unix.gettimeofday ();
1858 if !time_limit < 1. then time_limit := 60.;
1860 saturate_equations bag eq_uri env goal (fun e -> true) passive active
1864 List.fold_left (fun s e -> EqualitySet.add e s)
1865 EqualitySet.empty equalities
1868 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
1874 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
1878 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
1880 Printf.eprintf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
1881 (String.concat "\n" (List.map (Equality.string_of_equality ~env) active))
1882 (* (String.concat "\n"
1883 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
1884 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
1887 (fun e -> CicPp.ppterm (Equality.term_of_equality eq_uri e))
1892 Utils.debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
1895 let saturate_equations eq_uri env goal accept_fun passive active =
1896 let bag = Equality.mk_equality_bag () in
1897 saturate_equations bag eq_uri env goal accept_fun passive active
1900 let add_to_passive eql passives =
1901 add_to_passive passives eql eql