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/.
33 (fun b (_,eq) -> b && (Indexing.in_index t eq)) true l
36 (* set to false to disable paramodulation inside auto_tac *)
37 let connect_to_auto = true;;
40 (* profiling statistics... *)
41 let infer_time = ref 0.;;
42 let forward_simpl_time = ref 0.;;
43 let forward_simpl_new_time = ref 0.;;
44 let backward_simpl_time = ref 0.;;
45 let passive_maintainance_time = ref 0.;;
47 (* limited-resource-strategy related globals *)
48 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
49 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
50 let start_time = ref 0.;; (* time at which the execution started *)
51 let elapsed_time = ref 0.;;
52 (* let maximal_weight = ref None;; *)
53 let maximal_retained_equality = ref None;;
55 (* equality-selection related globals *)
56 let use_fullred = ref true;;
57 let weight_age_ratio = ref 4 (* 5 *);; (* settable by the user *)
58 let weight_age_counter = ref !weight_age_ratio ;;
59 let symbols_ratio = ref 0 (* 3 *);;
60 let symbols_counter = ref 0;;
62 (* non-recursive Knuth-Bendix term ordering by default *)
63 (* Utils.compare_terms := Utils.rpo;; *)
64 (* Utils.compare_terms := Utils.nonrec_kbo;; *)
65 (* Utils.compare_terms := Utils.ao;; *)
68 let derived_clauses = ref 0;;
69 let kept_clauses = ref 0;;
71 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
74 (* varbiables controlling the search-space *)
75 let maxdepth = ref 3;;
76 let maxwidth = ref 3;;
80 let (_,(_,_,(ty,left,right,_),m1)) = eq in
82 (Inference.metas_of_term left)@(Inference.metas_of_term right)
84 let m = List.filter (fun (i, _, _) -> List.mem i actual) m1 in
89 | ParamodulationFailure
90 | ParamodulationSuccess of Inference.proof option * environment
93 type goal = proof * Cic.metasenv * Cic.term;;
95 type theorem = Cic.term * Cic.term * Cic.metasenv;;
97 let symbols_of_equality (_, _, (_, left, right, _), _) =
98 let m1 = symbols_of_term left in
103 let c = TermMap.find k res in
104 TermMap.add k (c+v) res
107 (symbols_of_term right) m1
113 module OrderedEquality = struct
114 type t = Inference.equality
116 let compare eq1 eq2 =
117 match meta_convertibility_eq eq1 eq2 with
120 let w1, _, (ty, left, right, _), m1 = eq1
121 and w2, _, (ty', left', right', _), m2 = eq2 in
122 match Pervasives.compare w1 w2 with
124 let res = (List.length m1) - (List.length m2) in
125 if res <> 0 then res else Pervasives.compare eq1 eq2
130 module OrderedEquality = struct
131 type t = Inference.equality
134 let w, _, (ty, left, right, o), _ = eq in
141 | Incomparable -> None
143 let compare eq1 eq2 =
144 let w1, _, (ty, left, right, o1), m1 = eq1
145 and w2, _, (ty', left', right', o2), m2 = eq2 in
146 match Pervasives.compare w1 w2 with
148 (match minor eq1, minor eq2 with
149 | Some t1, Some t2 ->
150 fst (Utils.weight_of_term t1) - fst (Utils.weight_of_term t2)
154 (List.length m2) - (List.length m1) )
157 let compare eq1 eq2 =
158 match compare eq1 eq2 with
159 0 -> Pervasives.compare eq1 eq2
164 module EqualitySet = Set.Make(OrderedEquality);;
166 exception Empty_list;;
168 let passive_is_empty = function
169 | ([], _), ([], _), _ -> true
174 let size_of_passive ((_, ns), (_, ps), _) =
175 (EqualitySet.cardinal ns) + (EqualitySet.cardinal ps)
179 let size_of_active (active_list, _) =
180 List.length active_list
183 let age_factor = 0.01;;
185 let min_elt weight l =
188 [] -> raise Empty_list
190 let wa = float_of_int (weight a) in
193 (fun (current,w) arg ->
195 let w1 = weight arg in
196 let wa = (float_of_int w1) +. !x *. age_factor in
197 if wa < w then (arg,wa) else (current,w))
202 let compare eq1 eq2 =
203 let w1, _, (ty, left, right, _), m1, _ = eq1 in
204 let w2, _, (ty', left', right', _), m2, _ = eq2 in
205 match Pervasives.compare w1 w2 with
206 | 0 -> (List.length m1) - (List.length m2)
212 selects one equality from passive. The selection strategy is a combination
213 of weight, age and goal-similarity
215 let rec select env goals passive (active, _) =
216 processed_clauses := !processed_clauses + 1;
218 match (List.rev goals) with (_, goal::_)::_ -> goal | _ -> assert false
220 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
222 List.filter (fun e -> e <> eq) l
224 if !weight_age_ratio > 0 then
225 weight_age_counter := !weight_age_counter - 1;
226 match !weight_age_counter with
228 weight_age_counter := !weight_age_ratio;
229 match neg_list, pos_list with
231 (* Negatives aren't indexed, no need to remove them... *)
233 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
234 | [], (hd:EqualitySet.elt)::tl ->
236 Indexing.remove_index passive_table hd
238 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
239 | _, _ -> assert false
241 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) ->
242 (symbols_counter := !symbols_counter - 1;
243 let cardinality map =
244 TermMap.fold (fun k v res -> res + v) map 0
247 let _, _, term = goal in
250 let card = cardinality symbols in
251 let foldfun k v (r1, r2) =
252 if TermMap.mem k symbols then
253 let c = TermMap.find k symbols in
254 let c1 = abs (c - v) in
260 let f equality (i, e) =
262 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
264 let c = others + (abs (common - card)) in
265 if c < i then (c, equality)
268 let e1 = EqualitySet.min_elt pos_set in
271 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
273 (others + (abs (common - card))), e1
275 let _, current = EqualitySet.fold f pos_set initial in
277 Indexing.remove_index passive_table current
281 (remove current pos_list, EqualitySet.remove current pos_set),
285 symbols_counter := !symbols_ratio;
286 let set_selection set = EqualitySet.min_elt set in
287 (* let set_selection l = min_elt (fun (w,_,_,_) -> w) l in *)
288 if EqualitySet.is_empty neg_set then
289 let current = set_selection pos_set in
292 (remove current pos_list, EqualitySet.remove current pos_set),
293 Indexing.remove_index passive_table current
295 (Positive, current), passive
297 let current = set_selection neg_set in
299 (remove current neg_list, EqualitySet.remove current neg_set),
303 (Negative, current), passive
307 (* initializes the passive set of equalities *)
308 let make_passive neg pos =
309 let set_of equalities =
310 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
313 List.fold_left (fun tbl e -> Indexing.index tbl e) Indexing.empty pos
326 (* adds to passive a list of equalities: new_neg is a list of negative
327 equalities, new_pos a list of positive equalities *)
328 let add_to_passive passive (new_neg, new_pos) =
329 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
330 let ok set equality = not (EqualitySet.mem equality set) in
331 let neg = List.filter (ok neg_set) new_neg
332 and pos = List.filter (ok pos_set) new_pos in
334 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
336 let add set equalities =
337 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
339 (neg @ neg_list, add neg_set neg),
340 (pos_list @ pos, add pos_set pos),
345 (* removes from passive equalities that are estimated impossible to activate
346 within the current time limit *)
347 let prune_passive howmany (active, _) passive =
348 let (nl, ns), (pl, ps), tbl = passive in
349 let howmany = float_of_int howmany
350 and ratio = float_of_int !weight_age_ratio in
353 int_of_float (if t -. v < 0.5 then t else v)
355 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
356 and in_age = round (howmany /. (ratio +. 1.)) in
358 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
361 | (Negative, e)::_ ->
362 let symbols = symbols_of_equality e in
363 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
367 let counter = ref !symbols_ratio in
368 let rec pickw w ns ps =
370 if not (EqualitySet.is_empty ns) then
371 let e = EqualitySet.min_elt ns in
372 let ns', ps = pickw (w-1) (EqualitySet.remove e ns) ps in
373 EqualitySet.add e ns', ps
374 else if !counter > 0 then
376 counter := !counter - 1;
377 if !counter = 0 then counter := !symbols_ratio
381 let e = EqualitySet.min_elt ps in
382 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
383 ns, EqualitySet.add e ps'
385 let foldfun k v (r1, r2) =
386 if TermMap.mem k symbols then
387 let c = TermMap.find k symbols in
388 let c1 = abs (c - v) in
394 let f equality (i, e) =
396 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
398 let c = others + (abs (common - card)) in
399 if c < i then (c, equality)
402 let e1 = EqualitySet.min_elt ps in
405 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
407 (others + (abs (common - card))), e1
409 let _, e = EqualitySet.fold f ps initial in
410 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
411 ns, EqualitySet.add e ps'
413 let e = EqualitySet.min_elt ps in
414 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
415 ns, EqualitySet.add e ps'
417 EqualitySet.empty, EqualitySet.empty
419 let ns, ps = pickw in_weight ns ps in
420 let rec picka w s l =
424 | hd::tl when not (EqualitySet.mem hd s) ->
425 let w, s, l = picka (w-1) s tl in
426 w, EqualitySet.add hd s, hd::l
428 let w, s, l = picka w s tl in
433 let in_age, ns, nl = picka in_age ns nl in
434 let _, ps, pl = picka in_age ps pl in
435 if not (EqualitySet.is_empty ps) then
436 maximal_retained_equality := Some (EqualitySet.max_elt ps);
439 (fun e tbl -> Indexing.index tbl e) ps Indexing.empty
441 (nl, ns), (pl, ps), tbl
445 (** inference of new equalities between current and some in active *)
446 let infer env sign current (active_list, active_table) =
448 if Utils.debug_metas then
449 (ignore(Indexing.check_target c current "infer1");
450 ignore(List.map (function (_,current) -> Indexing.check_target c current "infer2") active_list));
451 let new_neg, new_pos =
455 Indexing.superposition_left !maxmeta env active_table current in
456 if Utils.debug_metas then
459 Indexing.check_target c current "sup-1") res);
464 Indexing.superposition_right !maxmeta env active_table current in
465 if Utils.debug_metas then
468 Indexing.check_target c current "sup0") res);
470 let rec infer_positive table = function
472 | (Negative, equality)::tl ->
474 Indexing.superposition_left !maxmeta env table equality in
476 if Utils.debug_metas then
479 Indexing.check_target c current "supl") res);
480 let neg, pos = infer_positive table tl in
482 | (Positive, equality)::tl ->
484 Indexing.superposition_right !maxmeta env table equality in
486 if Utils.debug_metas then
490 Indexing.check_target c current "sup2") res);
491 let neg, pos = infer_positive table tl in
494 let maxm, copy_of_current = Inference.fix_metas !maxmeta current in
496 let curr_table = Indexing.index Indexing.empty current in
498 infer_positive curr_table ((sign,copy_of_current)::active_list)
500 if Utils.debug_metas then
503 Indexing.check_target c current "sup3") pos);
506 derived_clauses := !derived_clauses + (List.length new_neg) +
507 (List.length new_pos);
508 match !maximal_retained_equality with
510 if Utils.debug_metas then
513 Indexing.check_target c current "sup4") new_pos);
516 Indexing.check_target c current "sup5") new_neg));
519 ignore(assert false);
520 (* if we have a maximal_retained_equality, we can discard all equalities
521 "greater" than it, as they will never be reached... An equality is
522 greater than maximal_retained_equality if it is bigger
523 wrt. OrderedEquality.compare and it is less similar than
524 maximal_retained_equality to the current goal *)
526 match active_list with
527 | (Negative, e)::_ ->
528 let symbols = symbols_of_equality e in
529 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
536 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
539 if OrderedEquality.compare e eq <= 0 then
542 let foldfun k v (r1, r2) =
543 if TermMap.mem k symbols then
544 let c = TermMap.find k symbols in
545 let c1 = abs (c - v) in
553 TermMap.fold foldfun (symbols_of_equality eq) (0, 0) in
554 others + (abs (common - card))
557 TermMap.fold foldfun (symbols_of_equality e) (0, 0) in
558 let c = others + (abs (common - card)) in
559 if c < initial then true else false
561 List.filter filterfun new_pos
567 let contains_empty env (negative, positive) =
568 let metasenv, context, ugraph = env in
572 (fun (w, proof, (ty, left, right, ordering), m) ->
573 fst (CicReduction.are_convertible context left right ugraph))
582 (** simplifies current using active and passive *)
583 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
584 let _, context, _ = env in
585 let pl, passive_table =
588 | Some ((pn, _), (pp, _), pt) ->
589 let pn = List.map (fun e -> (Negative, e)) pn
590 and pp = List.map (fun e -> (Positive, e)) pp in
593 let all = if pl = [] then active_list else active_list @ pl in
595 let demodulate table current =
596 let newmeta, newcurrent =
597 Indexing.demodulation_equality !maxmeta env table sign current in
599 if is_identity env newcurrent then
600 if sign = Negative then Some (sign, newcurrent)
604 (* (Printf.sprintf "\ncurrent was: %s\nnewcurrent is: %s\n" *)
605 (* (string_of_equality current) *)
606 (* (string_of_equality newcurrent))); *)
609 (* (Printf.sprintf "active is: %s" *)
610 (* (String.concat "\n" *)
611 (* (List.map (fun (_, e) -> (string_of_equality e)) active_list)))); *)
615 Some (sign, newcurrent)
617 let rec demod current =
618 if Utils.debug_metas then
619 ignore (Indexing.check_target context current "demod0");
620 let res = demodulate active_table current in
621 if Utils.debug_metas then
622 ignore ((function None -> () | Some (_,x) ->
623 ignore (Indexing.check_target context x "demod1");()) res);
626 | Some (sign, newcurrent) ->
627 match passive_table with
629 | Some passive_table ->
630 match demodulate passive_table newcurrent with
632 | Some (sign,newnewcurrent) ->
633 if newcurrent <> newnewcurrent then
635 else Some (sign,newnewcurrent)
637 let res = demod current in
640 | Some (Negative, c) ->
643 (fun (s, eq) -> s = Negative && meta_convertibility_eq eq c)
646 if ok then res else None
647 | Some (Positive, c) ->
648 if Indexing.in_index active_table c then
651 match passive_table with
653 if fst (Indexing.subsumption env active_table c) then
657 | Some passive_table ->
658 if Indexing.in_index passive_table c then None
660 let r1, _ = Indexing.subsumption env active_table c in
662 let r2, _ = Indexing.subsumption env passive_table c in
663 if r2 then None else res
666 type fs_time_info_t = {
667 mutable build_all: float;
668 mutable demodulate: float;
669 mutable subsumption: float;
672 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
675 (** simplifies new using active and passive *)
676 let forward_simplify_new env (new_neg, new_pos) ?passive active =
677 if Utils.debug_metas then
682 Indexing.check_target c current "forward new neg") new_neg);
684 (fun current -> Indexing.check_target c current "forward new pos")
687 let t1 = Unix.gettimeofday () in
689 let active_list, active_table = active in
690 let pl, passive_table =
693 | Some ((pn, _), (pp, _), pt) ->
694 let pn = List.map (fun e -> (Negative, e)) pn
695 and pp = List.map (fun e -> (Positive, e)) pp in
699 let t2 = Unix.gettimeofday () in
700 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
702 let demodulate sign table target =
703 let newmeta, newtarget =
704 Indexing.demodulation_equality !maxmeta env table sign target in
708 let t1 = Unix.gettimeofday () in
710 let new_neg, new_pos =
711 let new_neg = List.map (demodulate Negative active_table) new_neg
712 and new_pos = List.map (demodulate Positive active_table) new_pos in
715 match passive_table with
716 | None -> new_neg, new_pos
717 | Some passive_table ->
718 List.map (demodulate Negative passive_table) new_neg,
719 List.map (demodulate Positive passive_table) new_pos *)
722 let t2 = Unix.gettimeofday () in
723 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
728 if not (Inference.is_identity env e) then
729 if EqualitySet.mem e s then s
730 else EqualitySet.add e s
732 EqualitySet.empty new_pos
734 let new_pos = EqualitySet.elements new_pos_set in
737 match passive_table with
739 (fun e -> not (fst (Indexing.subsumption env active_table e)))
740 | Some passive_table ->
741 (fun e -> not ((fst (Indexing.subsumption env active_table e)) ||
742 (fst (Indexing.subsumption env passive_table e))))
744 (* let t1 = Unix.gettimeofday () in *)
745 (* let t2 = Unix.gettimeofday () in *)
746 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
748 match passive_table with
750 (fun e -> not (Indexing.in_index active_table e))
751 | Some passive_table ->
753 not ((Indexing.in_index active_table e) ||
754 (Indexing.in_index passive_table e)))
756 new_neg, List.filter subs (List.filter is_duplicate new_pos)
760 (** simplifies a goal with equalities in active and passive *)
761 let rec simplify_goal env goal ?passive (active_list, active_table) =
762 let pl, passive_table =
765 | Some ((pn, _), (pp, _), pt) ->
766 let pn = List.map (fun e -> (Negative, e)) pn
767 and pp = List.map (fun e -> (Positive, e)) pp in
771 let demodulate table goal =
772 let newmeta, newgoal =
773 Indexing.demodulation_goal !maxmeta env table goal in
775 goal <> newgoal, newgoal
778 match passive_table with
779 | None -> demodulate active_table goal
780 | Some passive_table ->
781 let changed, goal = demodulate active_table goal in
782 let changed', goal = demodulate passive_table goal in
783 (changed || changed'), goal
785 changed, if not changed then goal
786 else snd (simplify_goal env goal ?passive (active_list, active_table))
790 let simplify_goals env goals ?passive active =
791 let a_goals, p_goals = goals in
796 List.map (fun g -> snd (simplify_goal env g ?passive active)) gl in
802 (fun (a, p) (d, gl) ->
803 let changed = ref false in
807 let c, g = simplify_goal env g ?passive active in
808 changed := !changed || c; g) gl in
809 if !changed then (a, (d, gl)::p) else ((d, gl)::a, p))
810 ([], p_goals) a_goals
816 (** simplifies active usign new *)
817 let backward_simplify_active env new_pos new_table min_weight active =
818 let active_list, active_table = active in
819 let active_list, newa =
821 (fun (s, equality) (res, newn) ->
822 let ew, _, _, _ = equality in
823 if ew < min_weight then
824 (s, equality)::res, newn
826 match forward_simplify env (s, equality) (new_pos, new_table) with
836 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
840 (fun (s, eq) (res, tbl) ->
841 if List.mem (s, eq) res then
843 else if (is_identity env eq) || (find eq res) then (
847 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
848 active_list ([], Indexing.empty),
850 (fun (s, eq) (n, p) ->
851 if (s <> Negative) && (is_identity env eq) then (
854 if s = Negative then eq::n, p
859 | [], [] -> active, None
860 | _ -> active, Some newa
864 (** simplifies passive using new *)
865 let backward_simplify_passive env new_pos new_table min_weight passive =
866 let (nl, ns), (pl, ps), passive_table = passive in
867 let f sign equality (resl, ress, newn) =
868 let ew, _, _, _ = equality in
869 if ew < min_weight then
870 equality::resl, ress, newn
872 match forward_simplify env (sign, equality) (new_pos, new_table) with
873 | None -> resl, EqualitySet.remove equality ress, newn
876 equality::resl, ress, newn
878 let ress = EqualitySet.remove equality ress in
881 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
882 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
885 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
887 match newn, newp with
888 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
889 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
893 let backward_simplify env new' ?passive active =
894 let new_pos, new_table, min_weight =
897 let ew, _, _, _ = e in
898 (Positive, e)::l, Indexing.index t e, min ew w)
899 ([], Indexing.empty, 1000000) (snd new')
902 backward_simplify_active env new_pos new_table min_weight active in
905 active, (make_passive [] []), newa, None
907 active, passive, newa, None
910 backward_simplify_passive env new_pos new_table min_weight passive in
911 active, passive, newa, newp *)
915 let close env new' given =
916 let new_pos, new_table, min_weight =
919 let ew, _, _, _ = e in
920 (Positive, e)::l, Indexing.index t e, min ew w)
921 ([], Indexing.empty, 1000000) (snd new')
925 let neg,pos = infer env s c (new_pos,new_table) in
930 let is_commutative_law eq =
931 let w, proof, (eq_ty, left, right, order), metas = snd eq in
932 match left,right with
933 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
934 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
935 f1 = f2 && a1 = b2 && a2 = b1
939 let prova env new' active =
940 let given = List.filter is_commutative_law (fst active) in
944 (Printf.sprintf "symmetric:\n%s\n"
947 (fun (s, e) -> (string_of_sign s) ^ " " ^
948 (string_of_equality ~env e))
953 (* returns an estimation of how many equalities in passive can be activated
954 within the current time limit *)
955 let get_selection_estimate () =
956 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
957 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
959 ceil ((float_of_int !processed_clauses) *.
960 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
964 (** initializes the set of goals *)
965 let make_goals goal =
967 and passive = [0, [goal]] in
972 (** initializes the set of theorems *)
973 let make_theorems theorems =
978 let activate_goal (active, passive) =
980 | goal_conj::tl -> true, (goal_conj::active, tl)
981 | [] -> false, (active, passive)
985 let activate_theorem (active, passive) =
987 | theorem::tl -> true, (theorem::active, tl)
988 | [] -> false, (active, passive)
993 let simplify_theorems env theorems ?passive (active_list, active_table) =
994 let pl, passive_table =
997 | Some ((pn, _), (pp, _), pt) ->
998 let pn = List.map (fun e -> (Negative, e)) pn
999 and pp = List.map (fun e -> (Positive, e)) pp in
1002 let a_theorems, p_theorems = theorems in
1003 let demodulate table theorem =
1004 let newmeta, newthm =
1005 Indexing.demodulation_theorem !maxmeta env table theorem in
1007 theorem != newthm, newthm
1009 let foldfun table (a, p) theorem =
1010 let changed, theorem = demodulate table theorem in
1011 if changed then (a, theorem::p) else (theorem::a, p)
1013 let mapfun table theorem = snd (demodulate table theorem) in
1014 match passive_table with
1016 let p_theorems = List.map (mapfun active_table) p_theorems in
1017 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
1018 | Some passive_table ->
1019 let p_theorems = List.map (mapfun active_table) p_theorems in
1020 let p_theorems, a_theorems =
1021 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
1022 let p_theorems = List.map (mapfun passive_table) p_theorems in
1023 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
1027 let rec simpl env e others others_simpl =
1028 let active = others @ others_simpl in
1031 (fun t (_, e) -> Indexing.index t e)
1032 Indexing.empty active
1034 let res = forward_simplify env e (active, tbl) in
1038 | None -> simpl env hd tl others_simpl
1039 | Some e -> simpl env hd tl (e::others_simpl)
1043 | None -> others_simpl
1044 | Some e -> e::others_simpl
1048 let simplify_equalities env equalities =
1051 (Printf.sprintf "equalities:\n%s\n"
1053 (List.map string_of_equality equalities))));
1054 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1055 match equalities with
1058 let others = List.map (fun e -> (Positive, e)) tl in
1060 List.rev (List.map snd (simpl env (Positive, hd) others []))
1064 (Printf.sprintf "equalities AFTER:\n%s\n"
1066 (List.map string_of_equality res))));
1071 (* applies equality to goal to see if the goal can be closed *)
1072 let apply_equality_to_goal env equality goal =
1073 let module C = Cic in
1074 let module HL = HelmLibraryObjects in
1075 let module I = Inference in
1076 let metasenv, context, ugraph = env in
1077 let _, proof, (ty, left, right, _), metas = equality in
1079 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
1080 let gproof, gmetas, gterm = goal in
1083 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
1084 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
1086 let subst, metasenv', _ =
1087 Inference.unification metas gmetas context eqterm gterm ugraph
1091 | I.BasicProof (subst',t) -> I.BasicProof (subst@subst',t)
1092 | I.ProofBlock (s, uri, nt, t, pe, p) ->
1093 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
1097 let rec repl = function
1098 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
1099 | I.NoProof -> newproof
1100 | I.BasicProof _ -> newproof
1101 | I.SubProof (t, i, p) ->
1102 prerr_endline "SUBPROOF!";
1103 I.SubProof (t, i, repl p)
1108 true, (subst:Inference.substitution), newgproof
1109 with CicUnification.UnificationFailure _ ->
1110 false, [], I.NoProof
1115 let new_meta metasenv =
1116 let m = CicMkImplicit.new_meta metasenv [] in
1118 while !maxmeta <= m do incr maxmeta done;
1123 (* applies a theorem or an equality to goal, returning a list of subgoals or
1124 an indication of failure *)
1125 let apply_to_goal env theorems ?passive active goal =
1126 let metasenv, context, ugraph = env in
1127 let proof, metas, term = goal in
1130 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
1131 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
1134 CicMkImplicit.identity_relocation_list_for_metavariable context in
1135 let proof', newmeta =
1136 let rec get_meta = function
1137 | SubProof (t, i, p) ->
1138 let t', i' = get_meta p in
1139 if i' = -1 then t, i else t', i'
1140 | ProofGoalBlock (_, p) -> get_meta p
1141 | _ -> Cic.Implicit None, -1
1143 let p, m = get_meta proof in
1145 let n = new_meta (metasenv @ metas) in
1146 Cic.Meta (n, irl), n
1150 let metasenv = (newmeta, context, term)::metasenv @ metas in
1151 let bit = new_meta metasenv, context, term in
1152 let metasenv' = bit::metasenv in
1153 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
1155 let rec aux = function
1157 | (theorem, thmty, _)::tl ->
1159 let subst, (newproof, newgoals) =
1160 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
1162 if newgoals = [] then
1163 let _, _, p, _ = newproof in
1165 let rec repl = function
1166 | Inference.ProofGoalBlock (_, gp) ->
1167 Inference.ProofGoalBlock (Inference.BasicProof ([],p), gp)
1168 | Inference.NoProof -> Inference.BasicProof ([],p)
1169 | Inference.BasicProof _ -> Inference.BasicProof ([],p)
1170 | Inference.SubProof (t, i, p2) ->
1171 Inference.SubProof (t, i, repl p2)
1176 let _, m = status in
1177 let subst = List.filter (fun (i, _) -> i = m) subst in
1178 `Ok (subst, [newp, metas, term])
1180 let _, menv, p, _ = newproof in
1182 CicMkImplicit.identity_relocation_list_for_metavariable context
1187 let _, _, ty = CicUtil.lookup_meta i menv in
1189 let rec gp = function
1190 | SubProof (t, i, p) ->
1191 SubProof (t, i, gp p)
1192 | ProofGoalBlock (sp1, sp2) ->
1193 ProofGoalBlock (sp1, gp sp2)
1196 SubProof (p, i, BasicProof ([],Cic.Meta (i, irl)))
1197 | ProofSymBlock (s, sp) ->
1198 ProofSymBlock (s, gp sp)
1199 | ProofBlock (s, u, nt, t, pe, sp) ->
1200 prerr_endline "apply_to_goal!";
1201 ProofBlock (s, u, nt, t, pe, gp sp)
1209 let w, m = weight_of_term t in
1210 w + 2 * (List.length m)
1213 (fun (_, _, t1) (_, _, t2) ->
1214 Pervasives.compare (weight t1) (weight t2))
1217 let best = aux tl in
1219 | `Ok (_, _) -> best
1220 | `No -> `GoOn ([subst, goals])
1221 | `GoOn sl -> `GoOn ((subst, goals)::sl)
1222 with ProofEngineTypes.Fail msg ->
1226 if Inference.term_is_equality term then
1227 let rec appleq_a = function
1228 | [] -> false, [], []
1229 | (Positive, equality)::tl ->
1230 let ok, s, newproof = apply_equality_to_goal env equality goal in
1231 if ok then true, s, [newproof, metas, term] else appleq_a tl
1232 | _::tl -> appleq_a tl
1234 let rec appleq_p = function
1235 | [] -> false, [], []
1237 let ok, s, newproof = apply_equality_to_goal env equality goal in
1238 if ok then true, s, [newproof, metas, term] else appleq_p tl
1240 let al, _ = active in
1242 | None -> appleq_a al
1243 | Some (_, (pl, _), _) ->
1244 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1248 if r = true then `Ok ((s:Cic.substitution),l) else aux theorems
1252 (* sorts a conjunction of goals in order to detect earlier if it is
1253 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1254 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1257 (fun (_, e1, g1) (_, e2, g2) ->
1259 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1261 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1265 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1270 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1274 if prop1 = 0 && prop2 = 0 then
1275 let e1 = if Inference.term_is_equality g1 then 0 else 1
1276 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1286 let is_meta_closed goals =
1287 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1291 (* applies a series of theorems/equalities to a conjunction of goals *)
1292 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1293 let aux (goal, r) tl =
1294 let propagate_subst subst (proof, metas, term) =
1295 let rec repl = function
1296 | NoProof -> NoProof
1297 | BasicProof (subst',t) ->
1298 BasicProof (subst@subst',t)
1299 | ProofGoalBlock (p, pb) ->
1300 let pb' = repl pb in
1301 ProofGoalBlock (p, pb')
1302 | SubProof (t, i, p) ->
1303 let t' = Inference.apply_subst subst t in
1306 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1307 | ProofBlock (s, u, nty, t, pe, p) ->
1308 ProofBlock (subst @ s, u, nty, t, pe, p)
1309 in (repl proof, metas, term)
1311 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1313 | `No -> `No (depth, goals)
1318 let tl = List.map (propagate_subst s) tl in
1319 sort_goal_conj env (depth+1, gl @ tl)) sl
1322 | `Ok (subst, gl) ->
1326 let p, _, _ = List.hd gl in
1328 let rec repl = function
1329 | SubProof (_, _, p) -> repl p
1330 | ProofGoalBlock (p1, p2) ->
1331 ProofGoalBlock (repl p1, repl p2)
1334 build_proof_term (repl p)
1337 let rec get_meta = function
1338 | SubProof (_, i, p) ->
1339 let i' = get_meta p in
1340 if i' = -1 then i else i'
1341 (* max i (get_meta p) *)
1342 | ProofGoalBlock (_, p) -> get_meta p
1348 let _, (context, _, _) = List.hd subst in
1349 [i, (context, subproof, Cic.Implicit None)]
1351 let tl = List.map (propagate_subst subst) tl in
1352 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1356 if depth > !maxdepth || (List.length goals) > !maxwidth then
1359 let rec search_best res = function
1362 let r = apply_to_goal env theorems ?passive active goal in
1364 | `Ok _ -> (goal, r)
1365 | `No -> search_best res tl
1369 | _, `Ok _ -> assert false
1372 if (List.length l) < (List.length l2) then goal, r else res
1374 search_best newres tl
1376 let hd = List.hd goals in
1377 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1381 | _, _ -> search_best res (List.tl goals)
1383 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1385 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1386 (List.length (snd conj)) < (List.length goals)->
1387 apply_to_goal_conj env theorems ?passive active conj
1393 module OrderedGoals = struct
1394 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1401 else let r = (List.length l1) - (List.length l2) in
1407 (fun (_, _, t1) (_, _, t2) ->
1408 let r = Pervasives.compare t1 t2 in
1417 module GoalsSet = Set.Make(OrderedGoals);;
1420 exception SearchSpaceOver;;
1425 let apply_to_goals env is_passive_empty theorems active goals =
1426 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1427 let add_to set goals =
1428 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1430 let rec aux set = function
1432 debug_print (lazy "HERE!!!");
1433 if is_passive_empty then raise SearchSpaceOver else false, set
1435 let res = apply_to_goal_conj env theorems active goals in
1441 | (d, (p, _, t)::_) -> d, p, t
1446 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1447 d (string_of_proof p) (CicPp.ppterm t)))
1449 true, GoalsSet.singleton newgoals
1451 let set' = add_to set (goals::tl) in
1452 let set' = add_to set' newgoals in
1457 let n = List.length goals in
1458 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1459 let goals = GoalsSet.elements goals in
1460 debug_print (lazy "\n\tapply_to_goals end\n");
1461 let m = List.length goals in
1462 if m = n && is_passive_empty then
1463 raise SearchSpaceOver
1470 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1471 work that well yet...) *)
1472 let sort_passive_goals goals =
1474 (fun (d1, l1) (d2, l2) ->
1476 and r2 = (List.length l1) - (List.length l2) in
1477 let foldfun ht (_, _, t) =
1478 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1481 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1482 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1483 in let r3 = m1 - m2 in
1485 else if r2 <> 0 then r2
1487 (* let _, _, g1 = List.hd l1 *)
1488 (* and _, _, g2 = List.hd l2 in *)
1489 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1490 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1491 (* in let r4 = e1 - e2 in *)
1492 (* if r4 <> 0 then r3 else r1) *)
1497 let print_goals goals =
1504 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1506 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1510 (* tries to prove the first conjunction in goals with applications of
1511 theorems/equalities, returning new sub-goals or an indication of success *)
1512 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1513 let theorems, _ = theorems in
1514 let a_goals, p_goals = goals in
1515 let goal = List.hd a_goals in
1516 let not_in_active gl =
1520 if (List.length gl) = (List.length gl') then
1521 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1527 let res = apply_to_goal_conj env theorems ?passive active goal in
1530 true, ([newgoals], [])
1532 false, (a_goals, p_goals)
1537 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1540 let p_goals = newgoals @ p_goals in
1541 let p_goals = sort_passive_goals p_goals in
1542 false, (a_goals, p_goals)
1548 let apply_theorem_to_goals env theorems active goals =
1549 let a_goals, p_goals = goals in
1550 let theorem = List.hd (fst theorems) in
1551 let theorems = [theorem] in
1552 let rec aux p = function
1553 | [] -> false, ([], p)
1555 let res = apply_to_goal_conj env theorems active goal in
1557 | `Ok newgoals -> true, ([newgoals], [])
1559 | `GoOn newgoals -> aux (newgoals @ p) tl
1561 let ok, (a, p) = aux p_goals a_goals in
1567 (fun (d1, l1) (d2, l2) ->
1570 else let r = (List.length l1) - (List.length l2) in
1576 (fun (_, _, t1) (_, _, t2) ->
1577 let r = Pervasives.compare t1 t2 in
1578 if r <> 0 then (res := r; true) else false) l1 l2
1582 ok, (a_goals, p_goals)
1585 (* given-clause algorithm with lazy reduction strategy *)
1586 let rec given_clause dbd env goals theorems passive active =
1587 let goals = simplify_goals env goals active in
1588 let ok, goals = activate_goal goals in
1589 (* let theorems = simplify_theorems env theorems active in *)
1591 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1594 match (fst goals) with
1595 | (_, [proof, _, _])::_ -> Some proof
1598 ParamodulationSuccess (proof, env)
1600 given_clause_aux dbd env goals theorems passive active
1602 (* let ok', theorems = activate_theorem theorems in *)
1603 let ok', theorems = false, theorems in
1605 let ok, goals = apply_theorem_to_goals env theorems active goals in
1608 match (fst goals) with
1609 | (_, [proof, _, _])::_ -> Some proof
1612 ParamodulationSuccess (proof, env)
1614 given_clause_aux dbd env goals theorems passive active
1616 if (passive_is_empty passive) then ParamodulationFailure
1617 else given_clause_aux dbd env goals theorems passive active
1619 and given_clause_aux dbd env goals theorems passive active =
1620 let _,context,_ = env in
1621 let time1 = Unix.gettimeofday () in
1623 let selection_estimate = get_selection_estimate () in
1624 let kept = size_of_passive passive in
1626 if !time_limit = 0. || !processed_clauses = 0 then
1628 else if !elapsed_time > !time_limit then (
1629 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1630 !time_limit !elapsed_time));
1632 ) else if kept > selection_estimate then (
1634 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1635 "(kept: %d, selection_estimate: %d)\n")
1636 kept selection_estimate));
1637 prune_passive selection_estimate active passive
1642 let time2 = Unix.gettimeofday () in
1643 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1645 kept_clauses := (size_of_passive passive) + (size_of_active active);
1646 match passive_is_empty passive with
1647 | true -> (* ParamodulationFailure *)
1648 given_clause dbd env goals theorems passive active
1650 let (sign, current), passive = select env (fst goals) passive active in
1651 let names = List.map (HExtlib.map_option (fun (name,_) -> name)) context in
1652 prerr_endline ("Selected = " ^
1653 (CicPp.pp (Inference.term_of_equality current) names));
1654 let time1 = Unix.gettimeofday () in
1655 let res = forward_simplify env (sign, current) ~passive active in
1656 let time2 = Unix.gettimeofday () in
1657 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1660 given_clause dbd env goals theorems passive active
1661 | Some (sign, current) ->
1662 if (sign = Negative) && (is_identity env current) then (
1664 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1665 (string_of_equality ~env current)));
1666 let _, proof, _, _ = current in
1667 ParamodulationSuccess (Some proof, env)
1670 (lazy "\n================================================");
1671 debug_print (lazy (Printf.sprintf "selected: %s %s"
1672 (string_of_sign sign)
1673 (string_of_equality ~env current)));
1675 let t1 = Unix.gettimeofday () in
1676 let new' = infer env sign current active in
1677 let t2 = Unix.gettimeofday () in
1678 infer_time := !infer_time +. (t2 -. t1);
1680 let res, goal' = contains_empty env new' in
1684 | Some goal -> let _, proof, _, _ = goal in Some proof
1687 ParamodulationSuccess (proof, env)
1689 let t1 = Unix.gettimeofday () in
1690 let new' = forward_simplify_new env new' active in
1691 let t2 = Unix.gettimeofday () in
1693 forward_simpl_new_time :=
1694 !forward_simpl_new_time +. (t2 -. t1)
1698 | Negative -> active
1700 let t1 = Unix.gettimeofday () in
1701 let active, _, newa, _ =
1702 backward_simplify env ([], [current]) active
1704 let t2 = Unix.gettimeofday () in
1705 backward_simpl_time :=
1706 !backward_simpl_time +. (t2 -. t1);
1710 let al, tbl = active in
1711 let nn = List.map (fun e -> Negative, e) n in
1716 Indexing.index tbl e)
1721 match contains_empty env new' with
1724 let al, tbl = active in
1726 | Negative -> (sign, current)::al, tbl
1728 al @ [(sign, current)], Indexing.index tbl current
1730 let passive = add_to_passive passive new' in
1731 given_clause dbd env goals theorems passive active
1736 let _, proof, _, _ = goal in Some proof
1739 ParamodulationSuccess (proof, env)
1746 (** given-clause algorithm with full reduction strategy *)
1747 let rec given_clause_fullred dbd env goals theorems passive active =
1749 let table,list = active in
1750 assert (check_table list table);
1752 let goals = simplify_goals env goals ~passive active in
1753 let _,context,_ = env in
1754 let ok, goals = activate_goal goals in
1755 (* let theorems = simplify_theorems env theorems ~passive active in *)
1757 let names = List.map (HExtlib.map_option (fun (name,_) -> name)) context in
1758 let _, _, t = List.hd (snd (List.hd (fst goals))) in
1759 let _ = prerr_endline ("goal activated = " ^ (CicPp.pp t names)) in
1763 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1764 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1765 (* let current = List.hd (fst goals) in *)
1766 (* let p, _, t = List.hd (snd current) in *)
1769 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1770 (* (CicPp.ppterm t) (string_of_proof p))); *)
1773 (* apply_goal_to_theorems dbd env theorems ~passive active goals in *)
1774 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1775 match (fst goals) with
1776 | (_, [proof, m, Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right]])::_
1777 when left = right && iseq uri ->
1779 Cic.Appl [Cic.MutConstruct (* reflexivity *)
1780 (LibraryObjects.eq_URI (), 0, 1, []);eq_ty; left]
1783 let rec repl = function
1784 | Inference.ProofGoalBlock (_, gp) ->
1785 Inference.ProofGoalBlock (Inference.BasicProof ([],p), gp)
1786 | Inference.NoProof -> Inference.BasicProof ([],p)
1787 | Inference.BasicProof _ -> Inference.BasicProof ([],p)
1788 | Inference.SubProof (t, i, p2) ->
1789 Inference.SubProof (t, i, repl p2)
1794 | (_, [proof,m,Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right]])::_->
1795 (* here we check if the goal is subsumed by an active *)
1797 (* the first m is unused *)
1798 Indexing.subsumption (m,context,CicUniv.empty_ugraph)
1800 (0,proof,(eq_ty,left,right,Eq),m)
1804 prerr_endline "The goal is subsumed by an active";
1812 ( prerr_endline "esco qui";
1814 List.filter test (fst active) in
1815 let s = Printf.sprintf "actives:\n%s\n"
1818 (fun (s, e) -> (string_of_sign s) ^ " " ^
1819 (string_of_equality ~env e))
1824 (fun x -> test (1,x))
1825 (let x,y,_ = passive in (fst x)@(fst y)) in
1826 let p = Printf.sprintf "passives:\n%s\n"
1830 (string_of_equality ~env e))
1834 let s = Printf.sprintf "actives:\n%s\n"
1837 (fun (s, e) -> (string_of_sign s) ^ " " ^
1838 (string_of_equality ~env e))
1840 let sp = Printf.sprintf "passives:\n%s\n"
1843 (string_of_equality ~env)
1844 (let x,y,_ = passive in (fst x)@(fst y)))) in
1846 prerr_endline sp; *)
1847 ParamodulationSuccess (proof, env))
1849 given_clause_fullred_aux dbd env goals theorems passive active
1851 (* let ok', theorems = activate_theorem theorems in *)
1853 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1856 (* match (fst goals) with *)
1857 (* | (_, [proof, _, _])::_ -> Some proof *)
1858 (* | _ -> assert false *)
1860 (* ParamodulationSuccess (proof, env) *)
1862 (* given_clause_fullred_aux env goals theorems passive active *)
1864 if (passive_is_empty passive) then ParamodulationFailure
1865 else given_clause_fullred_aux dbd env goals theorems passive active
1867 and given_clause_fullred_aux dbd env goals theorems passive active =
1868 prerr_endline (string_of_int !counter ^
1869 " MAXMETA: " ^ string_of_int !maxmeta ^
1870 " LOCALMAX: " ^ string_of_int !Indexing.local_max ^
1871 " #ACTIVES: " ^ string_of_int (size_of_active active) ^
1872 " #PASSIVES: " ^ string_of_int (size_of_passive passive));
1874 (* if !counter mod 10 = 0 then
1876 let size = HExtlib.estimate_size (passive,active) in
1877 let sizep = HExtlib.estimate_size (passive) in
1878 let sizea = HExtlib.estimate_size (active) in
1879 let (l1,s1),(l2,s2), t = passive in
1880 let sizetbl = HExtlib.estimate_size t in
1881 let sizel = HExtlib.estimate_size (l1,l2) in
1882 let sizes = HExtlib.estimate_size (s1,s2) in
1884 prerr_endline ("SIZE: " ^ string_of_int size);
1885 prerr_endline ("SIZE P: " ^ string_of_int sizep);
1886 prerr_endline ("SIZE A: " ^ string_of_int sizea);
1887 prerr_endline ("SIZE TBL: " ^ string_of_int sizetbl ^
1888 " SIZE L: " ^ string_of_int sizel ^
1889 " SIZE S:" ^ string_of_int sizes);
1892 if (size_of_active active) mod 50 = 0 then
1893 (let s = Printf.sprintf "actives:\n%s\n"
1896 (fun (s, e) -> (string_of_sign s) ^ " " ^
1897 (string_of_equality ~env e))
1899 let sp = Printf.sprintf "passives:\n%s\n"
1902 (string_of_equality ~env)
1903 (let x,y,_ = passive in (fst x)@(fst y)))) in
1905 prerr_endline sp); *)
1906 let time1 = Unix.gettimeofday () in
1907 let (_,context,_) = env in
1908 let selection_estimate = get_selection_estimate () in
1909 let kept = size_of_passive passive in
1911 if !time_limit = 0. || !processed_clauses = 0 then
1913 else if !elapsed_time > !time_limit then (
1914 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1915 !time_limit !elapsed_time));
1917 ) else if kept > selection_estimate then (
1919 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1920 "(kept: %d, selection_estimate: %d)\n")
1921 kept selection_estimate));
1922 prune_passive selection_estimate active passive
1927 let time2 = Unix.gettimeofday () in
1928 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1930 kept_clauses := (size_of_passive passive) + (size_of_active active);
1931 match passive_is_empty passive with
1932 | true -> (* ParamodulationFailure *)
1933 given_clause_fullred dbd env goals theorems passive active
1935 let (sign, current), passive = select env (fst goals) passive active in
1937 ("Selected = " ^ (string_of_sign sign) ^ " " ^
1938 string_of_equality ~env current);
1940 (let w,p,(t,l,r,o),m = current in
1941 " size w: " ^ string_of_int (HExtlib.estimate_size w)^
1942 " size p: " ^ string_of_int (HExtlib.estimate_size p)^
1943 " size t: " ^ string_of_int (HExtlib.estimate_size t)^
1944 " size l: " ^ string_of_int (HExtlib.estimate_size l)^
1945 " size r: " ^ string_of_int (HExtlib.estimate_size r)^
1946 " size o: " ^ string_of_int (HExtlib.estimate_size o)^
1947 " size m: " ^ string_of_int (HExtlib.estimate_size m)^
1948 " size m-c: " ^ string_of_int
1949 (HExtlib.estimate_size (List.map (fun (x,_,_) -> x) m)))) *)
1950 let time1 = Unix.gettimeofday () in
1951 let res = forward_simplify env (sign, current) ~passive active in
1952 let time2 = Unix.gettimeofday () in
1953 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1956 (* weight_age_counter := !weight_age_counter + 1; *)
1957 given_clause_fullred dbd env goals theorems passive active
1958 | Some (sign, current) ->
1959 if test (sign, current) then
1961 ("Simplified = " ^ (string_of_sign sign) ^ " " ^
1962 string_of_equality ~env current);
1963 let active = fst active in
1964 let s = Printf.sprintf "actives:\n%s\n"
1967 (fun (s, e) -> (string_of_sign s) ^ " " ^
1968 (string_of_equality ~env e))
1972 if (sign = Negative) && (is_identity env current) then (
1974 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1975 (string_of_equality ~env current)));
1976 let _, proof, _, m = current in
1977 ParamodulationSuccess (Some proof, env)
1980 (lazy "\n================================================");
1981 debug_print (lazy (Printf.sprintf "selected: %s %s"
1982 (string_of_sign sign)
1983 (string_of_equality ~env current)));
1985 let t1 = Unix.gettimeofday () in
1986 let new' = infer env sign current active in
1992 (Printf.sprintf "new' (senza semplificare):\n%s\n"
1995 (fun e -> "Negative " ^
1996 (string_of_equality ~env e)) neg) @
1998 (fun e -> "Positive " ^
1999 (string_of_equality ~env e)) pos)))))
2001 let t2 = Unix.gettimeofday () in
2002 infer_time := !infer_time +. (t2 -. t1);
2004 if is_identity env current then active
2006 let al, tbl = active in
2008 | Negative -> (sign, current)::al, tbl
2010 al @ [(sign, current)], Indexing.index tbl current
2012 let rec simplify new' active passive =
2013 let t1 = Unix.gettimeofday () in
2014 let new' = forward_simplify_new env new'~passive active in
2015 let t2 = Unix.gettimeofday () in
2016 forward_simpl_new_time :=
2017 !forward_simpl_new_time +. (t2 -. t1);
2018 let t1 = Unix.gettimeofday () in
2019 let active, passive, newa, retained =
2020 backward_simplify env new' ~passive active in
2022 let t2 = Unix.gettimeofday () in
2023 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
2024 match newa, retained with
2025 | None, None -> active, passive, new'
2027 | None, Some (n, p) ->
2028 let nn, np = new' in
2029 if Utils.debug_metas then
2032 (fun x->Indexing.check_target context x "simplify1")
2035 (fun x->Indexing.check_target context x "simplify2")
2038 simplify (nn @ n, np @ p) active passive
2039 | Some (n, p), Some (rn, rp) ->
2040 let nn, np = new' in
2041 simplify (nn @ n @ rn, np @ p @ rp) active passive
2043 let active, _, new' = simplify new' active passive in
2045 let new1 = prova env new' active in
2046 let new' = (fst new') @ (fst new1), (snd new') @ (snd new1) in
2052 (Printf.sprintf "new1:\n%s\n"
2055 (fun e -> "Negative " ^
2056 (string_of_equality ~env e)) neg) @
2058 (fun e -> "Positive " ^
2059 (string_of_equality ~env e)) pos)))))
2062 let k = size_of_passive passive in
2063 if k < (kept - 1) then
2064 processed_clauses := !processed_clauses + (kept - 1 - k);
2069 (Printf.sprintf "active:\n%s\n"
2072 (fun (s, e) -> (string_of_sign s) ^ " " ^
2073 (string_of_equality ~env e))
2081 (Printf.sprintf "new':\n%s\n"
2084 (fun e -> "Negative " ^
2085 (string_of_equality ~env e)) neg) @
2087 (fun e -> "Positive " ^
2088 (string_of_equality ~env e)) pos)))))
2090 match contains_empty env new' with
2092 let passive = add_to_passive passive new' in
2093 given_clause_fullred dbd env goals theorems passive active
2097 | Some goal -> let _, proof, _, _ = goal in Some proof
2100 ParamodulationSuccess (proof, env)
2105 let profiler0 = HExtlib.profile "P/Saturation.given_clause_fullred"
2107 let given_clause_fullred dbd env goals theorems passive active =
2108 profiler0.HExtlib.profile
2109 (given_clause_fullred dbd env goals theorems passive) active
2112 let rec saturate_equations env goal accept_fun passive active =
2113 elapsed_time := Unix.gettimeofday () -. !start_time;
2114 if !elapsed_time > !time_limit then
2117 let (sign, current), passive = select env [1, [goal]] passive active in
2118 let res = forward_simplify env (sign, current) ~passive active in
2121 saturate_equations env goal accept_fun passive active
2122 | Some (sign, current) ->
2123 assert (sign = Positive);
2125 (lazy "\n================================================");
2126 debug_print (lazy (Printf.sprintf "selected: %s %s"
2127 (string_of_sign sign)
2128 (string_of_equality ~env current)));
2129 let new' = infer env sign current active in
2131 if is_identity env current then active
2133 let al, tbl = active in
2134 al @ [(sign, current)], Indexing.index tbl current
2136 let rec simplify new' active passive =
2137 let new' = forward_simplify_new env new' ~passive active in
2138 let active, passive, newa, retained =
2139 backward_simplify env new' ~passive active in
2140 match newa, retained with
2141 | None, None -> active, passive, new'
2143 | None, Some (n, p) ->
2144 let nn, np = new' in
2145 simplify (nn @ n, np @ p) active passive
2146 | Some (n, p), Some (rn, rp) ->
2147 let nn, np = new' in
2148 simplify (nn @ n @ rn, np @ p @ rp) active passive
2150 let active, passive, new' = simplify new' active passive in
2154 (Printf.sprintf "active:\n%s\n"
2157 (fun (s, e) -> (string_of_sign s) ^ " " ^
2158 (string_of_equality ~env e))
2166 (Printf.sprintf "new':\n%s\n"
2169 (fun e -> "Negative " ^
2170 (string_of_equality ~env e)) neg) @
2172 (fun e -> "Positive " ^
2173 (string_of_equality ~env e)) pos)))))
2175 let new' = match new' with _, pos -> [], List.filter accept_fun pos in
2176 let passive = add_to_passive passive new' in
2177 saturate_equations env goal accept_fun passive active
2183 let main dbd full term metasenv ugraph =
2184 let module C = Cic in
2185 let module T = CicTypeChecker in
2186 let module PET = ProofEngineTypes in
2187 let module PP = CicPp in
2188 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2189 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2190 let proof, goals = status in
2191 let goal' = List.nth goals 0 in
2192 let _, metasenv, meta_proof, _ = proof in
2193 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2194 let eq_indexes, equalities, maxm = find_equalities context proof in
2195 let lib_eq_uris, library_equalities, maxm =
2197 find_library_equalities dbd context (proof, goal') (maxm+2)
2199 let library_equalities = List.map snd library_equalities in
2200 maxmeta := maxm+2; (* TODO ugly!! *)
2201 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2202 let new_meta_goal, metasenv, type_of_goal =
2203 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2206 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
2207 Cic.Meta (maxm+1, irl),
2208 (maxm+1, context, ty)::metasenv,
2211 let env = (metasenv, context, ugraph) in
2212 let t1 = Unix.gettimeofday () in
2215 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2216 let context_hyp = find_context_hypotheses env eq_indexes in
2217 context_hyp @ theorems, []
2220 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2221 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2223 let t = CicUtil.term_of_uri refl_equal in
2224 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2227 let t2 = Unix.gettimeofday () in
2230 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2235 "Theorems:\n-------------------------------------\n%s\n"
2240 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
2244 let goal = Inference.BasicProof ([],new_meta_goal), [], goal in
2245 let equalities = simplify_equalities env
2246 (equalities@library_equalities) in
2247 let active = make_active () in
2248 let passive = make_passive [] equalities in
2249 Printf.printf "\ncurrent goal: %s\n"
2250 (let _, _, g = goal in CicPp.ppterm g);
2251 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2252 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2253 Printf.printf "\nequalities:\n%s\n"
2256 (string_of_equality ~env) equalities));
2257 (* (equalities @ library_equalities))); *)
2258 print_endline "--------------------------------------------------";
2259 let start = Unix.gettimeofday () in
2260 print_endline "GO!";
2261 start_time := Unix.gettimeofday ();
2263 let goals = make_goals goal in
2264 (if !use_fullred then given_clause_fullred else given_clause_fullred)
2265 dbd env goals theorems passive active
2267 let finish = Unix.gettimeofday () in
2270 | ParamodulationFailure ->
2271 Printf.printf "NO proof found! :-(\n\n"
2272 | ParamodulationSuccess (Some proof, env) ->
2273 let proof = Inference.build_proof_term proof in
2274 Printf.printf "OK, found a proof!\n";
2275 (* REMEMBER: we have to instantiate meta_proof, we should use
2276 apply the "apply" tactic to proof and status
2278 let names = names_of_context context in
2279 print_endline (PP.pp proof names);
2282 (fun m (_, _, _, menv) -> m @ menv) metasenv equalities
2287 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2289 print_endline (string_of_float (finish -. start));
2291 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
2292 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2294 (fst (CicReduction.are_convertible
2295 context type_of_goal ty ug)));
2297 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
2298 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
2299 print_endline (string_of_float (finish -. start));*)
2303 | ParamodulationSuccess (None, env) ->
2304 Printf.printf "Success, but no proof?!?\n\n"
2309 ((Printf.sprintf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
2310 "forward_simpl_new_time: %.9f\n" ^^
2311 "backward_simpl_time: %.9f\n")
2312 !infer_time !forward_simpl_time !forward_simpl_new_time
2313 !backward_simpl_time) ^
2314 (Printf.sprintf "beta_expand_time: %.9f\n"
2315 !Indexing.beta_expand_time) ^
2316 (Printf.sprintf "passive_maintainance_time: %.9f\n"
2317 !passive_maintainance_time) ^
2318 (Printf.sprintf " successful unification/matching time: %.9f\n"
2319 !Indexing.match_unif_time_ok) ^
2320 (Printf.sprintf " failed unification/matching time: %.9f\n"
2321 !Indexing.match_unif_time_no) ^
2322 (Printf.sprintf " indexing retrieval time: %.9f\n"
2323 !Indexing.indexing_retrieval_time) ^
2324 (Printf.sprintf " demodulate_term.build_newtarget_time: %.9f\n"
2325 !Indexing.build_newtarget_time) ^
2326 (Printf.sprintf "derived %d clauses, kept %d clauses.\n"
2327 !derived_clauses !kept_clauses))
2331 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
2337 let default_depth = !maxdepth
2338 and default_width = !maxwidth;;
2342 Indexing.local_max := 100;
2343 symbols_counter := 0;
2344 weight_age_counter := !weight_age_ratio;
2345 processed_clauses := 0;
2348 maximal_retained_equality := None;
2350 forward_simpl_time := 0.;
2351 forward_simpl_new_time := 0.;
2352 backward_simpl_time := 0.;
2353 passive_maintainance_time := 0.;
2354 derived_clauses := 0;
2356 Indexing.beta_expand_time := 0.;
2357 Inference.metas_of_proof_time := 0.;
2361 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
2362 let module C = Cic in
2364 Indexing.init_index ();
2367 (* CicUnification.unif_ty := false;*)
2368 let proof, goal = status in
2370 let uri, metasenv, meta_proof, term_to_prove = proof in
2371 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2372 prerr_endline ("CTX: " ^ string_of_int (HExtlib.estimate_size context));
2373 let eq_indexes, equalities, maxm = find_equalities context proof in
2374 let new_meta_goal, metasenv, type_of_goal =
2376 CicMkImplicit.identity_relocation_list_for_metavariable context in
2377 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2379 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2380 Cic.Meta (maxm+1, irl),
2381 (maxm+1, context, ty)::metasenv,
2384 let ugraph = CicUniv.empty_ugraph in
2385 let env = (metasenv, context, ugraph) in
2386 let goal = Inference.BasicProof ([],new_meta_goal), [], goal in
2388 let t1 = Unix.gettimeofday () in
2389 let lib_eq_uris, library_equalities, maxm =
2390 find_library_equalities dbd context (proof, goal') (maxm+2)
2392 let library_equalities = List.map snd library_equalities in
2393 let t2 = Unix.gettimeofday () in
2395 let equalities = simplify_equalities env (equalities@library_equalities) in
2398 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
2399 let t1 = Unix.gettimeofday () in
2402 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2403 let context_hyp = find_context_hypotheses env eq_indexes in
2404 context_hyp @ thms, []
2407 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2408 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2410 let t = CicUtil.term_of_uri refl_equal in
2411 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2414 let t2 = Unix.gettimeofday () in
2419 "Theorems:\n-------------------------------------\n%s\n"
2424 "Term: %s, type: %s"
2425 (CicPp.ppterm t) (CicPp.ppterm ty))
2429 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2431 let active = make_active () in
2432 let passive = make_passive [] equalities in
2433 let start = Unix.gettimeofday () in
2435 let goals = make_goals goal in
2436 given_clause_fullred dbd env goals theorems passive active
2438 let finish = Unix.gettimeofday () in
2439 (res, finish -. start)
2442 | ParamodulationSuccess (Some proof, _) ->
2443 debug_print (lazy "OK, found a proof!");
2444 let proof = Inference.build_proof_term proof in
2445 (* prerr_endline (CicPp.ppterm proof); *)
2446 let names = names_of_context context in
2449 match new_meta_goal with
2450 | C.Meta (i, _) -> i | _ -> assert false
2452 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2457 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2459 debug_print (lazy (CicPp.pp proof [](* names *)));
2463 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2464 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2466 (fst (CicReduction.are_convertible
2467 context type_of_goal ty ug)))));
2468 let equality_for_replace i t1 =
2470 | C.Meta (n, _) -> n = i
2474 ProofEngineReduction.replace
2475 ~equality:equality_for_replace
2476 ~what:[goal'] ~with_what:[proof]
2481 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2482 (match uri with Some uri -> UriManager.string_of_uri uri
2484 (print_metasenv newmetasenv)
2485 (CicPp.pp real_proof [](* names *))
2486 (CicPp.pp term_to_prove names)));
2487 ((uri, newmetasenv, real_proof, term_to_prove), [])
2488 with CicTypeChecker.TypeCheckerFailure _ ->
2489 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2490 debug_print (lazy (CicPp.pp proof names));
2491 raise (ProofEngineTypes.Fail
2492 (lazy "Found a proof, but it doesn't typecheck"))
2494 let tall = fs_time_info.build_all in
2495 let tdemodulate = fs_time_info.demodulate in
2496 let tsubsumption = fs_time_info.subsumption in
2500 (Printf.sprintf "\nTIME NEEDED: %.9f" time) ^
2501 (Printf.sprintf "\ntall: %.9f" tall) ^
2502 (Printf.sprintf "\ntdemod: %.9f" tdemodulate) ^
2503 (Printf.sprintf "\ntsubsumption: %.9f" tsubsumption) ^
2504 (Printf.sprintf "\ninfer_time: %.9f" !infer_time) ^
2505 (Printf.sprintf "\nbeta_expand_time: %.9f\n"
2506 !Indexing.beta_expand_time) ^
2507 (Printf.sprintf "\nmetas_of_proof: %.9f\n"
2508 !Inference.metas_of_proof_time) ^
2509 (Printf.sprintf "\nforward_simpl_times: %.9f" !forward_simpl_time) ^
2510 (Printf.sprintf "\nforward_simpl_new_times: %.9f"
2511 !forward_simpl_new_time) ^
2512 (Printf.sprintf "\nbackward_simpl_times: %.9f" !backward_simpl_time) ^
2513 (Printf.sprintf "\npassive_maintainance_time: %.9f"
2514 !passive_maintainance_time))
2518 raise (ProofEngineTypes.Fail (lazy "NO proof found"))
2521 (* dummy function called within matita to trigger linkage *)
2525 let retrieve_and_print dbd term metasenv ugraph =
2526 let module C = Cic in
2527 let module T = CicTypeChecker in
2528 let module PET = ProofEngineTypes in
2529 let module PP = CicPp in
2530 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2531 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2532 let proof, goals = status in
2533 let goal' = List.nth goals 0 in
2534 let uri, metasenv, meta_proof, term_to_prove = proof in
2535 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2536 let eq_indexes, equalities, maxm = find_equalities context proof in
2537 let new_meta_goal, metasenv, type_of_goal =
2539 CicMkImplicit.identity_relocation_list_for_metavariable context in
2540 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2542 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2543 Cic.Meta (maxm+1, irl),
2544 (maxm+1, context, ty)::metasenv,
2547 let ugraph = CicUniv.empty_ugraph in
2548 let env = (metasenv, context, ugraph) in
2549 let t1 = Unix.gettimeofday () in
2550 let lib_eq_uris, library_equalities, maxm =
2551 find_library_equalities dbd context (proof, goal') (maxm+2) in
2552 let t2 = Unix.gettimeofday () in
2554 let equalities = (* equalities @ *) library_equalities in
2557 (Printf.sprintf "\n\nequalities:\n%s\n"
2561 (* Printf.sprintf "%s: %s" *)
2562 (UriManager.string_of_uri u)
2563 (* (string_of_equality e) *)
2566 debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
2567 let rec simpl e others others_simpl =
2569 let active = List.map (fun (u, e) -> (Positive, e))
2570 (others @ others_simpl) in
2573 (fun t (_, e) -> Indexing.index t e)
2574 Indexing.empty active
2576 let res = forward_simplify env (Positive, e) (active, tbl) in
2580 | None -> simpl hd tl others_simpl
2581 | Some e -> simpl hd tl ((u, (snd e))::others_simpl)
2585 | None -> others_simpl
2586 | Some e -> (u, (snd e))::others_simpl
2590 match equalities with
2593 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
2595 List.rev (simpl (*(Positive,*) hd others [])
2599 (Printf.sprintf "\nequalities AFTER:\n%s\n"
2603 Printf.sprintf "%s: %s"
2604 (UriManager.string_of_uri u)
2605 (string_of_equality e)
2611 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
2615 let main_demod_equalities dbd term metasenv ugraph =
2616 let module C = Cic in
2617 let module T = CicTypeChecker in
2618 let module PET = ProofEngineTypes in
2619 let module PP = CicPp in
2620 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2621 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2622 let proof, goals = status in
2623 let goal' = List.nth goals 0 in
2624 let _, metasenv, meta_proof, _ = proof in
2625 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2626 let eq_indexes, equalities, maxm = find_equalities context proof in
2627 let lib_eq_uris, library_equalities, maxm =
2628 find_library_equalities dbd context (proof, goal') (maxm+2)
2630 let library_equalities = List.map snd library_equalities in
2631 maxmeta := maxm+2; (* TODO ugly!! *)
2632 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2633 let new_meta_goal, metasenv, type_of_goal =
2634 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2637 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
2638 (CicPp.ppterm ty)));
2639 Cic.Meta (maxm+1, irl),
2640 (maxm+1, context, ty)::metasenv,
2643 let env = (metasenv, context, ugraph) in
2645 let goal = Inference.BasicProof ([],new_meta_goal), [], goal in
2646 let equalities = simplify_equalities env (equalities@library_equalities) in
2647 let active = make_active () in
2648 let passive = make_passive [] equalities in
2649 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2650 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2651 Printf.printf "\nequalities:\n%s\n"
2654 (string_of_equality ~env) equalities));
2655 print_endline "--------------------------------------------------";
2656 print_endline "GO!";
2657 start_time := Unix.gettimeofday ();
2658 if !time_limit < 1. then time_limit := 60.;
2660 saturate_equations env goal (fun e -> true) passive active
2664 List.fold_left (fun s e -> EqualitySet.add e s)
2665 EqualitySet.empty equalities
2668 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2673 | (n, _), (p, _), _ ->
2674 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2677 let l = List.map snd (fst ra) in
2678 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2680 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2681 (String.concat "\n" (List.map (string_of_equality ~env) active))
2682 (* (String.concat "\n"
2683 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2684 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2686 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) passive));
2690 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2694 let demodulate_tac ~dbd ~pattern ((proof,goal) as initialstatus) =
2695 let module I = Inference in
2696 let curi,metasenv,pbo,pty = proof in
2697 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2698 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2699 let lib_eq_uris, library_equalities, maxm =
2700 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2701 if library_equalities = [] then prerr_endline "VUOTA!!!";
2702 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2703 let library_equalities = List.map snd library_equalities in
2704 let goalterm = Cic.Meta (metano,irl) in
2705 let initgoal = Inference.BasicProof ([],goalterm), [], ty in
2706 let env = (metasenv, context, CicUniv.empty_ugraph) in
2707 let equalities = simplify_equalities env (equalities@library_equalities) in
2710 (fun tbl eq -> Indexing.index tbl eq)
2711 Indexing.empty equalities
2713 let newmeta,(newproof,newmetasenv, newty) = Indexing.demodulation_goal
2714 maxm (metasenv,context,CicUniv.empty_ugraph) table initgoal
2716 if newmeta != maxm then
2718 let opengoal = Cic.Meta(maxm,irl) in
2720 Inference.build_proof_term ~noproof:opengoal newproof in
2721 let extended_metasenv = (maxm,context,newty)::metasenv in
2722 let extended_status =
2723 (curi,extended_metasenv,pbo,pty),goal in
2724 let (status,newgoals) =
2725 ProofEngineTypes.apply_tactic
2726 (PrimitiveTactics.apply_tac ~term:proofterm)
2728 (status,maxm::newgoals)
2730 else if newty = ty then
2731 raise (ProofEngineTypes.Fail (lazy "no progress"))
2732 else ProofEngineTypes.apply_tactic
2733 (ReductionTactics.simpl_tac ~pattern)
2737 let demodulate_tac ~dbd ~pattern =
2738 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)