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 * Cic.metasenv) option
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 Indexing.subsumption env active_table c = None then
657 | Some passive_table ->
658 if Indexing.in_index passive_table c then None
660 if Indexing.subsumption env active_table c = None then
661 if Indexing.subsumption env passive_table c = None then
669 type fs_time_info_t = {
670 mutable build_all: float;
671 mutable demodulate: float;
672 mutable subsumption: float;
675 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
678 (** simplifies new using active and passive *)
679 let forward_simplify_new env (new_neg, new_pos) ?passive active =
680 if Utils.debug_metas then
685 Indexing.check_target c current "forward new neg") new_neg);
687 (fun current -> Indexing.check_target c current "forward new pos")
690 let t1 = Unix.gettimeofday () in
692 let active_list, active_table = active in
693 let pl, passive_table =
696 | Some ((pn, _), (pp, _), pt) ->
697 let pn = List.map (fun e -> (Negative, e)) pn
698 and pp = List.map (fun e -> (Positive, e)) pp in
702 let t2 = Unix.gettimeofday () in
703 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
705 let demodulate sign table target =
706 let newmeta, newtarget =
707 Indexing.demodulation_equality !maxmeta env table sign target in
711 let t1 = Unix.gettimeofday () in
713 let new_neg, new_pos =
714 let new_neg = List.map (demodulate Negative active_table) new_neg
715 and new_pos = List.map (demodulate Positive active_table) new_pos in
718 match passive_table with
719 | None -> new_neg, new_pos
720 | Some passive_table ->
721 List.map (demodulate Negative passive_table) new_neg,
722 List.map (demodulate Positive passive_table) new_pos *)
725 let t2 = Unix.gettimeofday () in
726 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
731 if not (Inference.is_identity env e) then
732 if EqualitySet.mem e s then s
733 else EqualitySet.add e s
735 EqualitySet.empty new_pos
737 let new_pos = EqualitySet.elements new_pos_set in
740 match passive_table with
742 (fun e -> (Indexing.subsumption env active_table e = None))
743 | Some passive_table ->
744 (fun e -> ((Indexing.subsumption env active_table e = None) &&
745 (Indexing.subsumption env passive_table e = None)))
747 (* let t1 = Unix.gettimeofday () in *)
748 (* let t2 = Unix.gettimeofday () in *)
749 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
751 match passive_table with
753 (fun e -> not (Indexing.in_index active_table e))
754 | Some passive_table ->
756 not ((Indexing.in_index active_table e) ||
757 (Indexing.in_index passive_table e)))
759 new_neg, List.filter subs (List.filter is_duplicate new_pos)
763 (** simplifies a goal with equalities in active and passive *)
764 let rec simplify_goal env goal ?passive (active_list, active_table) =
765 let pl, passive_table =
768 | Some ((pn, _), (pp, _), pt) ->
769 let pn = List.map (fun e -> (Negative, e)) pn
770 and pp = List.map (fun e -> (Positive, e)) pp in
774 let demodulate table goal =
775 let newmeta, newgoal =
776 Indexing.demodulation_goal !maxmeta env table goal in
778 goal <> newgoal, newgoal
781 match passive_table with
782 | None -> demodulate active_table goal
783 | Some passive_table ->
784 let changed, goal = demodulate active_table goal in
785 let changed', goal = demodulate passive_table goal in
786 (changed || changed'), goal
792 snd (simplify_goal env goal ?passive (active_list, active_table))
796 let simplify_goals env goals ?passive active =
797 let a_goals, p_goals = goals in
802 List.map (fun g -> snd (simplify_goal env g ?passive active)) gl in
808 (fun (a, p) (d, gl) ->
809 let changed = ref false in
813 let c, g = simplify_goal env g ?passive active in
814 changed := !changed || c; g) gl in
815 if !changed then (a, (d, gl)::p) else ((d, gl)::a, p))
816 ([], p_goals) a_goals
822 (** simplifies active usign new *)
823 let backward_simplify_active env new_pos new_table min_weight active =
824 let active_list, active_table = active in
825 let active_list, newa =
827 (fun (s, equality) (res, newn) ->
828 let ew, _, _, _ = equality in
829 if ew < min_weight then
830 (s, equality)::res, newn
832 match forward_simplify env (s, equality) (new_pos, new_table) with
842 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
846 (fun (s, eq) (res, tbl) ->
847 if List.mem (s, eq) res then
849 else if (is_identity env eq) || (find eq res) then (
853 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
854 active_list ([], Indexing.empty),
856 (fun (s, eq) (n, p) ->
857 if (s <> Negative) && (is_identity env eq) then (
860 if s = Negative then eq::n, p
865 | [], [] -> active, None
866 | _ -> active, Some newa
870 (** simplifies passive using new *)
871 let backward_simplify_passive env new_pos new_table min_weight passive =
872 let (nl, ns), (pl, ps), passive_table = passive in
873 let f sign equality (resl, ress, newn) =
874 let ew, _, _, _ = equality in
875 if ew < min_weight then
876 equality::resl, ress, newn
878 match forward_simplify env (sign, equality) (new_pos, new_table) with
879 | None -> resl, EqualitySet.remove equality ress, newn
882 equality::resl, ress, newn
884 let ress = EqualitySet.remove equality ress in
887 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
888 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
891 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
893 match newn, newp with
894 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
895 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
899 let backward_simplify env new' ?passive active =
900 let new_pos, new_table, min_weight =
903 let ew, _, _, _ = e in
904 (Positive, e)::l, Indexing.index t e, min ew w)
905 ([], Indexing.empty, 1000000) (snd new')
908 backward_simplify_active env new_pos new_table min_weight active in
911 active, (make_passive [] []), newa, None
913 active, passive, newa, None
916 backward_simplify_passive env new_pos new_table min_weight passive in
917 active, passive, newa, newp *)
921 let close env new' given =
922 let new_pos, new_table, min_weight =
925 let ew, _, _, _ = e in
926 (Positive, e)::l, Indexing.index t e, min ew w)
927 ([], Indexing.empty, 1000000) (snd new')
931 let neg,pos = infer env s c (new_pos,new_table) in
936 let is_commutative_law eq =
937 let w, proof, (eq_ty, left, right, order), metas = snd eq in
938 match left,right with
939 Cic.Appl[f1;Cic.Meta _ as a1;Cic.Meta _ as b1],
940 Cic.Appl[f2;Cic.Meta _ as a2;Cic.Meta _ as b2] ->
941 f1 = f2 && a1 = b2 && a2 = b1
945 let prova env new' active =
946 let given = List.filter is_commutative_law (fst active) in
950 (Printf.sprintf "symmetric:\n%s\n"
953 (fun (s, e) -> (string_of_sign s) ^ " " ^
954 (string_of_equality ~env e))
959 (* returns an estimation of how many equalities in passive can be activated
960 within the current time limit *)
961 let get_selection_estimate () =
962 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
963 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
965 ceil ((float_of_int !processed_clauses) *.
966 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
970 (** initializes the set of goals *)
971 let make_goals goal =
973 and passive = [0, [goal]] in
978 (** initializes the set of theorems *)
979 let make_theorems theorems =
984 let activate_goal (active, passive) =
987 | goal_conj::tl -> true, (goal_conj::active, tl)
988 | [] -> false, (active, passive)
990 true, (active,passive)
994 let activate_theorem (active, passive) =
996 | theorem::tl -> true, (theorem::active, tl)
997 | [] -> false, (active, passive)
1002 let simplify_theorems env theorems ?passive (active_list, active_table) =
1003 let pl, passive_table =
1006 | Some ((pn, _), (pp, _), pt) ->
1007 let pn = List.map (fun e -> (Negative, e)) pn
1008 and pp = List.map (fun e -> (Positive, e)) pp in
1011 let a_theorems, p_theorems = theorems in
1012 let demodulate table theorem =
1013 let newmeta, newthm =
1014 Indexing.demodulation_theorem !maxmeta env table theorem in
1016 theorem != newthm, newthm
1018 let foldfun table (a, p) theorem =
1019 let changed, theorem = demodulate table theorem in
1020 if changed then (a, theorem::p) else (theorem::a, p)
1022 let mapfun table theorem = snd (demodulate table theorem) in
1023 match passive_table with
1025 let p_theorems = List.map (mapfun active_table) p_theorems in
1026 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
1027 | Some passive_table ->
1028 let p_theorems = List.map (mapfun active_table) p_theorems in
1029 let p_theorems, a_theorems =
1030 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
1031 let p_theorems = List.map (mapfun passive_table) p_theorems in
1032 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
1036 let rec simpl env e others others_simpl =
1037 let active = others @ others_simpl in
1040 (fun t (_, e) -> Indexing.index t e)
1041 Indexing.empty active
1043 let res = forward_simplify env e (active, tbl) in
1047 | None -> simpl env hd tl others_simpl
1048 | Some e -> simpl env hd tl (e::others_simpl)
1052 | None -> others_simpl
1053 | Some e -> e::others_simpl
1057 let simplify_equalities env equalities =
1060 (Printf.sprintf "equalities:\n%s\n"
1062 (List.map string_of_equality equalities))));
1063 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1064 match equalities with
1067 let others = List.map (fun e -> (Positive, e)) tl in
1069 List.rev (List.map snd (simpl env (Positive, hd) others []))
1073 (Printf.sprintf "equalities AFTER:\n%s\n"
1075 (List.map string_of_equality res))));
1080 (* applies equality to goal to see if the goal can be closed *)
1081 let apply_equality_to_goal env equality goal =
1082 let module C = Cic in
1083 let module HL = HelmLibraryObjects in
1084 let module I = Inference in
1085 let metasenv, context, ugraph = env in
1086 let _, proof, (ty, left, right, _), metas = equality in
1088 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
1089 let gproof, gmetas, gterm = goal in
1092 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
1093 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
1095 let subst, metasenv', _ =
1096 Inference.unification metas gmetas context eqterm gterm ugraph
1100 | I.BasicProof (subst',t) -> I.BasicProof (subst@subst',t)
1101 | I.ProofBlock (s, uri, nt, t, pe, p) ->
1102 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
1106 let rec repl = function
1107 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
1108 | I.NoProof -> newproof
1109 | I.BasicProof _ -> newproof
1110 | I.SubProof (t, i, p) ->
1111 prerr_endline "SUBPROOF!";
1112 I.SubProof (t, i, repl p)
1117 true, (subst:Inference.substitution), newgproof
1118 with CicUnification.UnificationFailure _ ->
1119 false, [], I.NoProof
1124 let new_meta metasenv =
1125 let m = CicMkImplicit.new_meta metasenv [] in
1127 while !maxmeta <= m do incr maxmeta done;
1132 (* applies a theorem or an equality to goal, returning a list of subgoals or
1133 an indication of failure *)
1134 let apply_to_goal env theorems ?passive active goal =
1135 let metasenv, context, ugraph = env in
1136 let proof, metas, term = goal in
1139 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
1140 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
1143 CicMkImplicit.identity_relocation_list_for_metavariable context in
1144 let proof', newmeta =
1145 let rec get_meta = function
1146 | SubProof (t, i, p) ->
1147 let t', i' = get_meta p in
1148 if i' = -1 then t, i else t', i'
1149 | ProofGoalBlock (_, p) -> get_meta p
1150 | _ -> Cic.Implicit None, -1
1152 let p, m = get_meta proof in
1154 let n = new_meta (metasenv @ metas) in
1155 Cic.Meta (n, irl), n
1159 let metasenv = (newmeta, context, term)::metasenv @ metas in
1160 let bit = new_meta metasenv, context, term in
1161 let metasenv' = bit::metasenv in
1162 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
1164 let rec aux = function
1166 | (theorem, thmty, _)::tl ->
1168 let subst, (newproof, newgoals) =
1169 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
1171 if newgoals = [] then
1172 let _, _, p, _ = newproof in
1174 let rec repl = function
1175 | Inference.ProofGoalBlock (_, gp) ->
1176 Inference.ProofGoalBlock (Inference.BasicProof ([],p), gp)
1177 | Inference.NoProof -> Inference.BasicProof ([],p)
1178 | Inference.BasicProof _ -> Inference.BasicProof ([],p)
1179 | Inference.SubProof (t, i, p2) ->
1180 Inference.SubProof (t, i, repl p2)
1185 let _, m = status in
1186 let subst = List.filter (fun (i, _) -> i = m) subst in
1187 `Ok (subst, [newp, metas, term])
1189 let _, menv, p, _ = newproof in
1191 CicMkImplicit.identity_relocation_list_for_metavariable context
1196 let _, _, ty = CicUtil.lookup_meta i menv in
1198 let rec gp = function
1199 | SubProof (t, i, p) ->
1200 SubProof (t, i, gp p)
1201 | ProofGoalBlock (sp1, sp2) ->
1202 ProofGoalBlock (sp1, gp sp2)
1205 SubProof (p, i, BasicProof ([],Cic.Meta (i, irl)))
1206 | ProofSymBlock (s, sp) ->
1207 ProofSymBlock (s, gp sp)
1208 | ProofBlock (s, u, nt, t, pe, sp) ->
1209 prerr_endline "apply_to_goal!";
1210 ProofBlock (s, u, nt, t, pe, gp sp)
1218 let w, m = weight_of_term t in
1219 w + 2 * (List.length m)
1222 (fun (_, _, t1) (_, _, t2) ->
1223 Pervasives.compare (weight t1) (weight t2))
1226 let best = aux tl in
1228 | `Ok (_, _) -> best
1229 | `No -> `GoOn ([subst, goals])
1230 | `GoOn sl -> `GoOn ((subst, goals)::sl)
1231 with ProofEngineTypes.Fail msg ->
1235 if Inference.term_is_equality term then
1236 let rec appleq_a = function
1237 | [] -> false, [], []
1238 | (Positive, equality)::tl ->
1239 let ok, s, newproof = apply_equality_to_goal env equality goal in
1240 if ok then true, s, [newproof, metas, term] else appleq_a tl
1241 | _::tl -> appleq_a tl
1243 let rec appleq_p = function
1244 | [] -> false, [], []
1246 let ok, s, newproof = apply_equality_to_goal env equality goal in
1247 if ok then true, s, [newproof, metas, term] else appleq_p tl
1249 let al, _ = active in
1251 | None -> appleq_a al
1252 | Some (_, (pl, _), _) ->
1253 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1257 if r = true then `Ok ((s:Cic.substitution),l) else aux theorems
1261 (* sorts a conjunction of goals in order to detect earlier if it is
1262 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1263 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1266 (fun (_, e1, g1) (_, e2, g2) ->
1268 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1270 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1274 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1279 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1283 if prop1 = 0 && prop2 = 0 then
1284 let e1 = if Inference.term_is_equality g1 then 0 else 1
1285 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1295 let is_meta_closed goals =
1296 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1300 (* applies a series of theorems/equalities to a conjunction of goals *)
1301 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1302 let aux (goal, r) tl =
1303 let propagate_subst subst (proof, metas, term) =
1304 let rec repl = function
1305 | NoProof -> NoProof
1306 | BasicProof (subst',t) ->
1307 BasicProof (subst@subst',t)
1308 | ProofGoalBlock (p, pb) ->
1309 let pb' = repl pb in
1310 ProofGoalBlock (p, pb')
1311 | SubProof (t, i, p) ->
1312 let t' = Inference.apply_subst subst t in
1315 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1316 | ProofBlock (s, u, nty, t, pe, p) ->
1317 ProofBlock (subst @ s, u, nty, t, pe, p)
1318 in (repl proof, metas, term)
1320 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1322 | `No -> `No (depth, goals)
1327 let tl = List.map (propagate_subst s) tl in
1328 sort_goal_conj env (depth+1, gl @ tl)) sl
1331 | `Ok (subst, gl) ->
1335 let p, _, _ = List.hd gl in
1337 let rec repl = function
1338 | SubProof (_, _, p) -> repl p
1339 | ProofGoalBlock (p1, p2) ->
1340 ProofGoalBlock (repl p1, repl p2)
1343 build_proof_term (repl p)
1346 let rec get_meta = function
1347 | SubProof (_, i, p) ->
1348 let i' = get_meta p in
1349 if i' = -1 then i else i'
1350 (* max i (get_meta p) *)
1351 | ProofGoalBlock (_, p) -> get_meta p
1357 let _, (context, _, _) = List.hd subst in
1358 [i, (context, subproof, Cic.Implicit None)]
1360 let tl = List.map (propagate_subst subst) tl in
1361 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1365 if depth > !maxdepth || (List.length goals) > !maxwidth then
1368 let rec search_best res = function
1371 let r = apply_to_goal env theorems ?passive active goal in
1373 | `Ok _ -> (goal, r)
1374 | `No -> search_best res tl
1378 | _, `Ok _ -> assert false
1381 if (List.length l) < (List.length l2) then goal, r else res
1383 search_best newres tl
1385 let hd = List.hd goals in
1386 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1390 | _, _ -> search_best res (List.tl goals)
1392 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1394 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1395 (List.length (snd conj)) < (List.length goals)->
1396 apply_to_goal_conj env theorems ?passive active conj
1402 module OrderedGoals = struct
1403 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1410 else let r = (List.length l1) - (List.length l2) in
1416 (fun (_, _, t1) (_, _, t2) ->
1417 let r = Pervasives.compare t1 t2 in
1426 module GoalsSet = Set.Make(OrderedGoals);;
1429 exception SearchSpaceOver;;
1434 let apply_to_goals env is_passive_empty theorems active goals =
1435 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1436 let add_to set goals =
1437 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1439 let rec aux set = function
1441 debug_print (lazy "HERE!!!");
1442 if is_passive_empty then raise SearchSpaceOver else false, set
1444 let res = apply_to_goal_conj env theorems active goals in
1450 | (d, (p, _, t)::_) -> d, p, t
1455 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1456 d (string_of_proof p) (CicPp.ppterm t)))
1458 true, GoalsSet.singleton newgoals
1460 let set' = add_to set (goals::tl) in
1461 let set' = add_to set' newgoals in
1466 let n = List.length goals in
1467 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1468 let goals = GoalsSet.elements goals in
1469 debug_print (lazy "\n\tapply_to_goals end\n");
1470 let m = List.length goals in
1471 if m = n && is_passive_empty then
1472 raise SearchSpaceOver
1479 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1480 work that well yet...) *)
1481 let sort_passive_goals goals =
1483 (fun (d1, l1) (d2, l2) ->
1485 and r2 = (List.length l1) - (List.length l2) in
1486 let foldfun ht (_, _, t) =
1487 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1490 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1491 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1492 in let r3 = m1 - m2 in
1494 else if r2 <> 0 then r2
1496 (* let _, _, g1 = List.hd l1 *)
1497 (* and _, _, g2 = List.hd l2 in *)
1498 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1499 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1500 (* in let r4 = e1 - e2 in *)
1501 (* if r4 <> 0 then r3 else r1) *)
1506 let print_goals goals =
1513 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1515 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1519 (* tries to prove the first conjunction in goals with applications of
1520 theorems/equalities, returning new sub-goals or an indication of success *)
1521 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1522 let theorems, _ = theorems in
1523 let a_goals, p_goals = goals in
1524 let goal = List.hd a_goals in
1525 let not_in_active gl =
1529 if (List.length gl) = (List.length gl') then
1530 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1536 let res = apply_to_goal_conj env theorems ?passive active goal in
1539 true, ([newgoals], [])
1541 false, (a_goals, p_goals)
1546 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1549 let p_goals = newgoals @ p_goals in
1550 let p_goals = sort_passive_goals p_goals in
1551 false, (a_goals, p_goals)
1557 let apply_theorem_to_goals env theorems active goals =
1558 let a_goals, p_goals = goals in
1559 let theorem = List.hd (fst theorems) in
1560 let theorems = [theorem] in
1561 let rec aux p = function
1562 | [] -> false, ([], p)
1564 let res = apply_to_goal_conj env theorems active goal in
1566 | `Ok newgoals -> true, ([newgoals], [])
1568 | `GoOn newgoals -> aux (newgoals @ p) tl
1570 let ok, (a, p) = aux p_goals a_goals in
1576 (fun (d1, l1) (d2, l2) ->
1579 else let r = (List.length l1) - (List.length l2) in
1585 (fun (_, _, t1) (_, _, t2) ->
1586 let r = Pervasives.compare t1 t2 in
1587 if r <> 0 then (res := r; true) else false) l1 l2
1591 ok, (a_goals, p_goals)
1594 (* given-clause algorithm with lazy reduction strategy *)
1595 let rec given_clause dbd env goals theorems passive active =
1596 let goals = simplify_goals env goals active in
1597 let ok, goals = activate_goal goals in
1598 (* let theorems = simplify_theorems env theorems active in *)
1600 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1603 match (fst goals) with
1604 | (_, [proof, _, _])::_ -> Some proof
1607 ParamodulationSuccess (proof, env)
1609 given_clause_aux dbd env goals theorems passive active
1611 (* let ok', theorems = activate_theorem theorems in *)
1612 let ok', theorems = false, theorems in
1614 let ok, goals = apply_theorem_to_goals env theorems active goals in
1617 match (fst goals) with
1618 | (_, [proof, _, _])::_ -> Some proof
1621 ParamodulationSuccess (proof, env)
1623 given_clause_aux dbd env goals theorems passive active
1625 if (passive_is_empty passive) then ParamodulationFailure
1626 else given_clause_aux dbd env goals theorems passive active
1628 and given_clause_aux dbd env goals theorems passive active =
1629 let _,context,_ = env in
1630 let time1 = Unix.gettimeofday () in
1632 let selection_estimate = get_selection_estimate () in
1633 let kept = size_of_passive passive in
1635 if !time_limit = 0. || !processed_clauses = 0 then
1637 else if !elapsed_time > !time_limit then (
1638 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1639 !time_limit !elapsed_time));
1641 ) else if kept > selection_estimate then (
1643 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1644 "(kept: %d, selection_estimate: %d)\n")
1645 kept selection_estimate));
1646 prune_passive selection_estimate active passive
1651 let time2 = Unix.gettimeofday () in
1652 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1654 kept_clauses := (size_of_passive passive) + (size_of_active active);
1655 match passive_is_empty passive with
1656 | true -> (* ParamodulationFailure *)
1657 given_clause dbd env goals theorems passive active
1659 let (sign, current), passive = select env (fst goals) passive active in
1660 let names = List.map (HExtlib.map_option (fun (name,_) -> name)) context in
1661 prerr_endline ("Selected = " ^
1662 (CicPp.pp (Inference.term_of_equality current) names));
1663 let time1 = Unix.gettimeofday () in
1664 let res = forward_simplify env (sign, current) ~passive active in
1665 let time2 = Unix.gettimeofday () in
1666 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1669 given_clause dbd env goals theorems passive active
1670 | Some (sign, current) ->
1671 if (sign = Negative) && (is_identity env current) then (
1673 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1674 (string_of_equality ~env current)));
1675 let _, proof, _, _ = current in
1676 ParamodulationSuccess (Some proof, env)
1679 (lazy "\n================================================");
1680 debug_print (lazy (Printf.sprintf "selected: %s %s"
1681 (string_of_sign sign)
1682 (string_of_equality ~env current)));
1684 let t1 = Unix.gettimeofday () in
1685 let new' = infer env sign current active in
1686 let t2 = Unix.gettimeofday () in
1687 infer_time := !infer_time +. (t2 -. t1);
1689 let res, goal' = contains_empty env new' in
1693 | Some goal -> let _, proof, _, _ = goal in Some proof
1696 ParamodulationSuccess (proof, env)
1698 let t1 = Unix.gettimeofday () in
1699 let new' = forward_simplify_new env new' active in
1700 let t2 = Unix.gettimeofday () in
1702 forward_simpl_new_time :=
1703 !forward_simpl_new_time +. (t2 -. t1)
1707 | Negative -> active
1709 let t1 = Unix.gettimeofday () in
1710 let active, _, newa, _ =
1711 backward_simplify env ([], [current]) active
1713 let t2 = Unix.gettimeofday () in
1714 backward_simpl_time :=
1715 !backward_simpl_time +. (t2 -. t1);
1719 let al, tbl = active in
1720 let nn = List.map (fun e -> Negative, e) n in
1725 Indexing.index tbl e)
1730 match contains_empty env new' with
1733 let al, tbl = active in
1735 | Negative -> (sign, current)::al, tbl
1737 al @ [(sign, current)], Indexing.index tbl current
1739 let passive = add_to_passive passive new' in
1740 given_clause dbd env goals theorems passive active
1745 let _, proof, _, _ = goal in Some proof
1748 ParamodulationSuccess (proof, env)
1753 let check_if_goal_is_subsumed env (proof,menv,ty) table =
1755 | Cic.Appl[Cic.MutInd(uri,_,_);eq_ty;left;right]
1756 when UriManager.eq uri (LibraryObjects.eq_URI ()) ->
1757 (let goal_equation = 0,proof,(eq_ty,left,right,Eq),menv in
1758 match Indexing.subsumption env table goal_equation with
1759 | Some (subst, (_,p,_,m)) ->
1760 let p = Inference.apply_subst subst (Inference.build_proof_term p) in
1762 let rec repl = function
1763 | Inference.ProofGoalBlock (_, gp) ->
1764 Inference.ProofGoalBlock (Inference.BasicProof ([],p), gp)
1765 | Inference.NoProof -> Inference.BasicProof ([],p)
1766 | Inference.BasicProof _ -> Inference.BasicProof ([],p)
1767 | Inference.SubProof (t, i, p2) ->
1768 Inference.SubProof (t, i, repl p2)
1773 Some (newp,Inference.apply_subst_metasenv subst m @ menv)
1780 (** given-clause algorithm with full reduction strategy *)
1781 let rec given_clause_fullred dbd env goals theorems passive active =
1783 let table,list = active in
1784 assert (check_table list table);
1786 let goals = simplify_goals env goals ~passive active in
1787 let _,context,_ = env in
1788 let ok, goals = activate_goal goals in
1789 (* let theorems = simplify_theorems env theorems ~passive active in *)
1791 let names = List.map (HExtlib.map_option (fun (name,_) -> name)) context in
1792 let _, _, t = List.hd (snd (List.hd (fst goals))) in
1793 let _ = prerr_endline ("goal activated = " ^ (CicPp.pp t names)) in
1797 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1798 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1799 (* let current = List.hd (fst goals) in *)
1800 (* let p, _, t = List.hd (snd current) in *)
1803 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1804 (* (CicPp.ppterm t) (string_of_proof p))); *)
1807 (* apply_goal_to_theorems dbd env theorems ~passive active goals in *)
1808 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1809 match (fst goals) with
1810 | (_, [proof, m, Cic.Appl[Cic.MutInd(uri,_,ens);eq_ty;left;right]])::_
1811 when left = right && iseq uri ->
1813 Cic.Appl [Cic.MutConstruct (* reflexivity *)
1814 (LibraryObjects.eq_URI (), 0, 1, []);eq_ty; left]
1817 let rec repl = function
1818 | Inference.ProofGoalBlock (_, gp) ->
1819 Inference.ProofGoalBlock (Inference.BasicProof ([],p), gp)
1820 | Inference.NoProof -> Inference.BasicProof ([],p)
1821 | Inference.BasicProof _ -> Inference.BasicProof ([],p)
1822 | Inference.SubProof (t, i, p2) ->
1823 Inference.SubProof (t, i, repl p2)
1827 in true, Some (newp,m)
1828 | (_, [proof,m,ty])::_ ->
1829 (match check_if_goal_is_subsumed env (proof,m,ty) (snd active) with
1830 | None -> false,None
1831 | Some (newproof,m) ->
1832 prerr_endline "Proof found by subsumption!";
1833 true, Some (newproof,m))
1837 ( prerr_endline "esco qui";
1839 List.filter test (fst active) in
1840 let s = Printf.sprintf "actives:\n%s\n"
1843 (fun (s, e) -> (string_of_sign s) ^ " " ^
1844 (string_of_equality ~env e))
1849 (fun x -> test (1,x))
1850 (let x,y,_ = passive in (fst x)@(fst y)) in
1851 let p = Printf.sprintf "passives:\n%s\n"
1855 (string_of_equality ~env e))
1859 let s = Printf.sprintf "actives:\n%s\n"
1862 (fun (s, e) -> (string_of_sign s) ^ " " ^
1863 (string_of_equality ~env e))
1865 let sp = Printf.sprintf "passives:\n%s\n"
1868 (string_of_equality ~env)
1869 (let x,y,_ = passive in (fst x)@(fst y)))) in
1871 prerr_endline sp; *)
1872 ParamodulationSuccess (proof))
1874 given_clause_fullred_aux dbd env goals theorems passive active
1876 (* let ok', theorems = activate_theorem theorems in *)
1878 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1881 (* match (fst goals) with *)
1882 (* | (_, [proof, _, _])::_ -> Some proof *)
1883 (* | _ -> assert false *)
1885 (* ParamodulationSuccess (proof, env) *)
1887 (* given_clause_fullred_aux env goals theorems passive active *)
1889 if (passive_is_empty passive) then ParamodulationFailure
1890 else given_clause_fullred_aux dbd env goals theorems passive active
1892 and given_clause_fullred_aux dbd env goals theorems passive active =
1893 prerr_endline (string_of_int !counter ^
1894 " MAXMETA: " ^ string_of_int !maxmeta ^
1895 " LOCALMAX: " ^ string_of_int !Indexing.local_max ^
1896 " #ACTIVES: " ^ string_of_int (size_of_active active) ^
1897 " #PASSIVES: " ^ string_of_int (size_of_passive passive));
1899 (* if !counter mod 10 = 0 then
1901 let size = HExtlib.estimate_size (passive,active) in
1902 let sizep = HExtlib.estimate_size (passive) in
1903 let sizea = HExtlib.estimate_size (active) in
1904 let (l1,s1),(l2,s2), t = passive in
1905 let sizetbl = HExtlib.estimate_size t in
1906 let sizel = HExtlib.estimate_size (l1,l2) in
1907 let sizes = HExtlib.estimate_size (s1,s2) in
1909 prerr_endline ("SIZE: " ^ string_of_int size);
1910 prerr_endline ("SIZE P: " ^ string_of_int sizep);
1911 prerr_endline ("SIZE A: " ^ string_of_int sizea);
1912 prerr_endline ("SIZE TBL: " ^ string_of_int sizetbl ^
1913 " SIZE L: " ^ string_of_int sizel ^
1914 " SIZE S:" ^ string_of_int sizes);
1917 if (size_of_active active) mod 50 = 0 then
1918 (let s = Printf.sprintf "actives:\n%s\n"
1921 (fun (s, e) -> (string_of_sign s) ^ " " ^
1922 (string_of_equality ~env e))
1924 let sp = Printf.sprintf "passives:\n%s\n"
1927 (string_of_equality ~env)
1928 (let x,y,_ = passive in (fst x)@(fst y)))) in
1930 prerr_endline sp); *)
1931 let time1 = Unix.gettimeofday () in
1932 let (_,context,_) = env in
1933 let selection_estimate = get_selection_estimate () in
1934 let kept = size_of_passive passive in
1936 if !time_limit = 0. || !processed_clauses = 0 then
1938 else if !elapsed_time > !time_limit then (
1939 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1940 !time_limit !elapsed_time));
1942 ) else if kept > selection_estimate then (
1944 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1945 "(kept: %d, selection_estimate: %d)\n")
1946 kept selection_estimate));
1947 prune_passive selection_estimate active passive
1952 let time2 = Unix.gettimeofday () in
1953 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1955 kept_clauses := (size_of_passive passive) + (size_of_active active);
1956 match passive_is_empty passive with
1957 | true -> (* ParamodulationFailure *)
1958 given_clause_fullred dbd env goals theorems passive active
1960 let (sign, current), passive = select env (fst goals) passive active in
1962 ("Selected = " ^ (string_of_sign sign) ^ " " ^
1963 string_of_equality ~env current);
1965 (let w,p,(t,l,r,o),m = current in
1966 " size w: " ^ string_of_int (HExtlib.estimate_size w)^
1967 " size p: " ^ string_of_int (HExtlib.estimate_size p)^
1968 " size t: " ^ string_of_int (HExtlib.estimate_size t)^
1969 " size l: " ^ string_of_int (HExtlib.estimate_size l)^
1970 " size r: " ^ string_of_int (HExtlib.estimate_size r)^
1971 " size o: " ^ string_of_int (HExtlib.estimate_size o)^
1972 " size m: " ^ string_of_int (HExtlib.estimate_size m)^
1973 " size m-c: " ^ string_of_int
1974 (HExtlib.estimate_size (List.map (fun (x,_,_) -> x) m)))) *)
1975 let time1 = Unix.gettimeofday () in
1976 let res = forward_simplify env (sign, current) ~passive active in
1977 let time2 = Unix.gettimeofday () in
1978 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1981 (* weight_age_counter := !weight_age_counter + 1; *)
1982 given_clause_fullred dbd env goals theorems passive active
1983 | Some (sign, current) ->
1984 if test (sign, current) then
1986 ("Simplified = " ^ (string_of_sign sign) ^ " " ^
1987 string_of_equality ~env current);
1988 let active = fst active in
1989 let s = Printf.sprintf "actives:\n%s\n"
1992 (fun (s, e) -> (string_of_sign s) ^ " " ^
1993 (string_of_equality ~env e))
1997 if (sign = Negative) && (is_identity env current) then (
1999 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
2000 (string_of_equality ~env current)));
2001 let _, proof, _, m = current in
2002 ParamodulationSuccess (Some (proof, m))
2005 (lazy "\n================================================");
2006 debug_print (lazy (Printf.sprintf "selected: %s %s"
2007 (string_of_sign sign)
2008 (string_of_equality ~env current)));
2010 let t1 = Unix.gettimeofday () in
2011 let new' = infer env sign current active in
2017 (Printf.sprintf "new' (senza semplificare):\n%s\n"
2020 (fun e -> "Negative " ^
2021 (string_of_equality ~env e)) neg) @
2023 (fun e -> "Positive " ^
2024 (string_of_equality ~env e)) pos)))))
2026 let t2 = Unix.gettimeofday () in
2027 infer_time := !infer_time +. (t2 -. t1);
2029 if is_identity env current then active
2031 let al, tbl = active in
2033 | Negative -> (sign, current)::al, tbl
2035 al @ [(sign, current)], Indexing.index tbl current
2037 let rec simplify new' active passive =
2038 let t1 = Unix.gettimeofday () in
2039 let new' = forward_simplify_new env new'~passive active in
2040 let t2 = Unix.gettimeofday () in
2041 forward_simpl_new_time :=
2042 !forward_simpl_new_time +. (t2 -. t1);
2043 let t1 = Unix.gettimeofday () in
2044 let active, passive, newa, retained =
2045 backward_simplify env new' ~passive active in
2047 let t2 = Unix.gettimeofday () in
2048 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
2049 match newa, retained with
2050 | None, None -> active, passive, new'
2052 | None, Some (n, p) ->
2053 let nn, np = new' in
2054 if Utils.debug_metas then
2057 (fun x->Indexing.check_target context x "simplify1")
2060 (fun x->Indexing.check_target context x "simplify2")
2063 simplify (nn @ n, np @ p) active passive
2064 | Some (n, p), Some (rn, rp) ->
2065 let nn, np = new' in
2066 simplify (nn @ n @ rn, np @ p @ rp) active passive
2068 let active, _, new' = simplify new' active passive in
2070 let new1 = prova env new' active in
2071 let new' = (fst new') @ (fst new1), (snd new') @ (snd new1) in
2077 (Printf.sprintf "new1:\n%s\n"
2080 (fun e -> "Negative " ^
2081 (string_of_equality ~env e)) neg) @
2083 (fun e -> "Positive " ^
2084 (string_of_equality ~env e)) pos)))))
2087 let k = size_of_passive passive in
2088 if k < (kept - 1) then
2089 processed_clauses := !processed_clauses + (kept - 1 - k);
2094 (Printf.sprintf "active:\n%s\n"
2097 (fun (s, e) -> (string_of_sign s) ^ " " ^
2098 (string_of_equality ~env e))
2106 (Printf.sprintf "new':\n%s\n"
2109 (fun e -> "Negative " ^
2110 (string_of_equality ~env e)) neg) @
2112 (fun e -> "Positive " ^
2113 (string_of_equality ~env e)) pos)))))
2115 match contains_empty env new' with
2117 let passive = add_to_passive passive new' in
2118 given_clause_fullred dbd env goals theorems passive active
2122 | Some goal -> let _, proof, _, env = goal in Some (proof,env)
2125 ParamodulationSuccess proof
2130 let profiler0 = HExtlib.profile "P/Saturation.given_clause_fullred"
2132 let given_clause_fullred dbd env goals theorems passive active =
2133 profiler0.HExtlib.profile
2134 (given_clause_fullred dbd env goals theorems passive) active
2137 let rec saturate_equations env goal accept_fun passive active =
2138 elapsed_time := Unix.gettimeofday () -. !start_time;
2139 if !elapsed_time > !time_limit then
2142 let (sign, current), passive = select env [1, [goal]] passive active in
2143 let res = forward_simplify env (sign, current) ~passive active in
2146 saturate_equations env goal accept_fun passive active
2147 | Some (sign, current) ->
2148 assert (sign = Positive);
2150 (lazy "\n================================================");
2151 debug_print (lazy (Printf.sprintf "selected: %s %s"
2152 (string_of_sign sign)
2153 (string_of_equality ~env current)));
2154 let new' = infer env sign current active in
2156 if is_identity env current then active
2158 let al, tbl = active in
2159 al @ [(sign, current)], Indexing.index tbl current
2161 let rec simplify new' active passive =
2162 let new' = forward_simplify_new env new' ~passive active in
2163 let active, passive, newa, retained =
2164 backward_simplify env new' ~passive active in
2165 match newa, retained with
2166 | None, None -> active, passive, new'
2168 | None, Some (n, p) ->
2169 let nn, np = new' in
2170 simplify (nn @ n, np @ p) active passive
2171 | Some (n, p), Some (rn, rp) ->
2172 let nn, np = new' in
2173 simplify (nn @ n @ rn, np @ p @ rp) active passive
2175 let active, passive, new' = simplify new' active passive in
2179 (Printf.sprintf "active:\n%s\n"
2182 (fun (s, e) -> (string_of_sign s) ^ " " ^
2183 (string_of_equality ~env e))
2191 (Printf.sprintf "new':\n%s\n"
2194 (fun e -> "Negative " ^
2195 (string_of_equality ~env e)) neg) @
2197 (fun e -> "Positive " ^
2198 (string_of_equality ~env e)) pos)))))
2200 let new' = match new' with _, pos -> [], List.filter accept_fun pos in
2201 let passive = add_to_passive passive new' in
2202 saturate_equations env goal accept_fun passive active
2208 let main dbd full term metasenv ugraph =
2209 let module C = Cic in
2210 let module T = CicTypeChecker in
2211 let module PET = ProofEngineTypes in
2212 let module PP = CicPp in
2213 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2214 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2215 let proof, goals = status in
2216 let goal' = List.nth goals 0 in
2217 let _, metasenv, meta_proof, _ = proof in
2218 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2219 let eq_indexes, equalities, maxm = find_equalities context proof in
2220 let lib_eq_uris, library_equalities, maxm =
2222 find_library_equalities dbd context (proof, goal') (maxm+2)
2224 let library_equalities = List.map snd library_equalities in
2225 maxmeta := maxm+2; (* TODO ugly!! *)
2226 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2227 let new_meta_goal, metasenv, type_of_goal =
2228 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2231 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
2232 Cic.Meta (maxm+1, irl),
2233 (maxm+1, context, ty)::metasenv,
2236 let env = (metasenv, context, ugraph) in
2237 let t1 = Unix.gettimeofday () in
2240 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2241 let context_hyp = find_context_hypotheses env eq_indexes in
2242 context_hyp @ theorems, []
2245 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2246 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2248 let t = CicUtil.term_of_uri refl_equal in
2249 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2252 let t2 = Unix.gettimeofday () in
2255 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2260 "Theorems:\n-------------------------------------\n%s\n"
2265 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
2269 let goal = Inference.BasicProof ([],new_meta_goal), [], goal in
2270 let equalities = simplify_equalities env
2271 (equalities@library_equalities) in
2272 let active = make_active () in
2273 let passive = make_passive [] equalities in
2274 Printf.printf "\ncurrent goal: %s\n"
2275 (let _, _, g = goal in CicPp.ppterm g);
2276 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2277 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2278 Printf.printf "\nequalities:\n%s\n"
2281 (string_of_equality ~env) equalities));
2282 (* (equalities @ library_equalities))); *)
2283 print_endline "--------------------------------------------------";
2284 let start = Unix.gettimeofday () in
2285 print_endline "GO!";
2286 start_time := Unix.gettimeofday ();
2288 let goals = make_goals goal in
2289 (if !use_fullred then given_clause_fullred else given_clause_fullred)
2290 dbd env goals theorems passive active
2292 let finish = Unix.gettimeofday () in
2295 | ParamodulationFailure ->
2296 Printf.printf "NO proof found! :-(\n\n"
2297 | ParamodulationSuccess (Some (proof, env)) ->
2298 let proof = Inference.build_proof_term proof in
2299 Printf.printf "OK, found a proof!\n";
2300 (* REMEMBER: we have to instantiate meta_proof, we should use
2301 apply the "apply" tactic to proof and status
2303 let names = names_of_context context in
2304 print_endline (PP.pp proof names);
2307 (fun m (_, _, _, menv) -> m @ menv) metasenv equalities
2312 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2314 print_endline (string_of_float (finish -. start));
2316 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
2317 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2319 (fst (CicReduction.are_convertible
2320 context type_of_goal ty ug)));
2322 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
2323 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
2324 print_endline (string_of_float (finish -. start));*)
2328 | ParamodulationSuccess None ->
2329 Printf.printf "Success, but no proof?!?\n\n"
2334 ((Printf.sprintf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
2335 "forward_simpl_new_time: %.9f\n" ^^
2336 "backward_simpl_time: %.9f\n")
2337 !infer_time !forward_simpl_time !forward_simpl_new_time
2338 !backward_simpl_time) ^
2339 (Printf.sprintf "beta_expand_time: %.9f\n"
2340 !Indexing.beta_expand_time) ^
2341 (Printf.sprintf "passive_maintainance_time: %.9f\n"
2342 !passive_maintainance_time) ^
2343 (Printf.sprintf " successful unification/matching time: %.9f\n"
2344 !Indexing.match_unif_time_ok) ^
2345 (Printf.sprintf " failed unification/matching time: %.9f\n"
2346 !Indexing.match_unif_time_no) ^
2347 (Printf.sprintf " indexing retrieval time: %.9f\n"
2348 !Indexing.indexing_retrieval_time) ^
2349 (Printf.sprintf " demodulate_term.build_newtarget_time: %.9f\n"
2350 !Indexing.build_newtarget_time) ^
2351 (Printf.sprintf "derived %d clauses, kept %d clauses.\n"
2352 !derived_clauses !kept_clauses))
2356 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
2362 let default_depth = !maxdepth
2363 and default_width = !maxwidth;;
2367 Indexing.local_max := 100;
2368 symbols_counter := 0;
2369 weight_age_counter := !weight_age_ratio;
2370 processed_clauses := 0;
2373 maximal_retained_equality := None;
2375 forward_simpl_time := 0.;
2376 forward_simpl_new_time := 0.;
2377 backward_simpl_time := 0.;
2378 passive_maintainance_time := 0.;
2379 derived_clauses := 0;
2381 Indexing.beta_expand_time := 0.;
2382 Inference.metas_of_proof_time := 0.;
2386 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
2387 let module C = Cic in
2389 Indexing.init_index ();
2393 (* CicUnification.unif_ty := false;*)
2394 let proof, goal = status in
2396 let uri, metasenv, meta_proof, term_to_prove = proof in
2397 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2398 prerr_endline ("CTX: " ^ string_of_int (HExtlib.estimate_size context));
2399 let eq_indexes, equalities, maxm = find_equalities context proof in
2400 let new_meta_goal, metasenv, type_of_goal =
2402 CicMkImplicit.identity_relocation_list_for_metavariable context in
2403 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2405 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2406 Cic.Meta (maxm+1, irl),
2407 (maxm+1, context, ty)::metasenv,
2410 let ugraph = CicUniv.empty_ugraph in
2411 let env = (metasenv, context, ugraph) in
2412 let goal = Inference.BasicProof ([],new_meta_goal), [], goal in
2414 let t1 = Unix.gettimeofday () in
2415 let lib_eq_uris, library_equalities, maxm =
2416 find_library_equalities dbd context (proof, goal') (maxm+2)
2418 let library_equalities = List.map snd library_equalities in
2419 let t2 = Unix.gettimeofday () in
2421 let equalities = simplify_equalities env (equalities@library_equalities) in
2424 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
2425 let t1 = Unix.gettimeofday () in
2428 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2429 let context_hyp = find_context_hypotheses env eq_indexes in
2430 context_hyp @ thms, []
2433 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2434 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2436 let t = CicUtil.term_of_uri refl_equal in
2437 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2440 let t2 = Unix.gettimeofday () in
2445 "Theorems:\n-------------------------------------\n%s\n"
2450 "Term: %s, type: %s"
2451 (CicPp.ppterm t) (CicPp.ppterm ty))
2455 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2457 let active = make_active () in
2458 let passive = make_passive [] equalities in
2459 let start = Unix.gettimeofday () in
2461 let goals = make_goals goal in
2462 given_clause_fullred dbd env goals theorems passive active
2464 let finish = Unix.gettimeofday () in
2465 (res, finish -. start)
2468 | ParamodulationSuccess (Some (proof, proof_menv)) ->
2469 debug_print (lazy "OK, found a proof!");
2470 let proof = Inference.build_proof_term proof in
2471 let equality_for_replace i t1 =
2473 | C.Meta (n, _) -> n = i
2476 let proof_menv, what, with_what =
2478 CicMkImplicit.identity_relocation_list_for_metavariable context
2481 (fun (acc1,acc2,acc3) (i,_,ty) ->
2482 (i,context,ty)::acc1,
2483 (Cic.Meta(i,[]))::acc2,
2484 (Cic.Meta(i,irl)) ::acc3)
2485 ([],[],[]) proof_menv
2487 let proof = ProofEngineReduction.replace_lifting
2492 (* prerr_endline (CicPp.ppterm proof); *)
2493 let names = names_of_context context in
2496 match new_meta_goal with
2497 | C.Meta (i, _) -> i | _ -> assert false
2499 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2501 let newmetasenv = newmetasenv@proof_menv in
2505 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2507 debug_print (lazy (CicPp.pp proof [](* names *)));
2511 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2512 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2514 (fst (CicReduction.are_convertible
2515 context type_of_goal ty ug)))));
2517 ProofEngineReduction.replace
2518 ~equality:equality_for_replace
2519 ~what:[goal'] ~with_what:[proof]
2524 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2525 (match uri with Some uri -> UriManager.string_of_uri uri
2527 (print_metasenv newmetasenv)
2528 (CicPp.pp real_proof [](* names *))
2529 (CicPp.pp term_to_prove names)));
2530 ((uri, newmetasenv, real_proof, term_to_prove),
2531 List.map (fun (i,_,_) -> i) proof_menv)
2532 with CicTypeChecker.TypeCheckerFailure _ ->
2533 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2534 debug_print (lazy (CicPp.pp proof names));
2535 raise (ProofEngineTypes.Fail
2536 (lazy "Found a proof, but it doesn't typecheck"))
2538 let tall = fs_time_info.build_all in
2539 let tdemodulate = fs_time_info.demodulate in
2540 let tsubsumption = fs_time_info.subsumption in
2544 (Printf.sprintf "\nTIME NEEDED: %.9f" time) ^
2545 (Printf.sprintf "\ntall: %.9f" tall) ^
2546 (Printf.sprintf "\ntdemod: %.9f" tdemodulate) ^
2547 (Printf.sprintf "\ntsubsumption: %.9f" tsubsumption) ^
2548 (Printf.sprintf "\ninfer_time: %.9f" !infer_time) ^
2549 (Printf.sprintf "\nbeta_expand_time: %.9f\n"
2550 !Indexing.beta_expand_time) ^
2551 (Printf.sprintf "\nmetas_of_proof: %.9f\n"
2552 !Inference.metas_of_proof_time) ^
2553 (Printf.sprintf "\nforward_simpl_times: %.9f" !forward_simpl_time) ^
2554 (Printf.sprintf "\nforward_simpl_new_times: %.9f"
2555 !forward_simpl_new_time) ^
2556 (Printf.sprintf "\nbackward_simpl_times: %.9f" !backward_simpl_time) ^
2557 (Printf.sprintf "\npassive_maintainance_time: %.9f"
2558 !passive_maintainance_time))
2562 raise (ProofEngineTypes.Fail (lazy "NO proof found"))
2565 (* dummy function called within matita to trigger linkage *)
2569 let retrieve_and_print dbd term metasenv ugraph =
2570 let module C = Cic in
2571 let module T = CicTypeChecker in
2572 let module PET = ProofEngineTypes in
2573 let module PP = CicPp in
2574 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2575 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2576 let proof, goals = status in
2577 let goal' = List.nth goals 0 in
2578 let uri, metasenv, meta_proof, term_to_prove = proof in
2579 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2580 let eq_indexes, equalities, maxm = find_equalities context proof in
2581 let new_meta_goal, metasenv, type_of_goal =
2583 CicMkImplicit.identity_relocation_list_for_metavariable context in
2584 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2586 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2587 Cic.Meta (maxm+1, irl),
2588 (maxm+1, context, ty)::metasenv,
2591 let ugraph = CicUniv.empty_ugraph in
2592 let env = (metasenv, context, ugraph) in
2593 let t1 = Unix.gettimeofday () in
2594 let lib_eq_uris, library_equalities, maxm =
2595 find_library_equalities dbd context (proof, goal') (maxm+2) in
2596 let t2 = Unix.gettimeofday () in
2598 let equalities = (* equalities @ *) library_equalities in
2601 (Printf.sprintf "\n\nequalities:\n%s\n"
2605 (* Printf.sprintf "%s: %s" *)
2606 (UriManager.string_of_uri u)
2607 (* (string_of_equality e) *)
2610 debug_print (lazy "RETR: SIMPLYFYING EQUALITIES...");
2611 let rec simpl e others others_simpl =
2613 let active = List.map (fun (u, e) -> (Positive, e))
2614 (others @ others_simpl) in
2617 (fun t (_, e) -> Indexing.index t e)
2618 Indexing.empty active
2620 let res = forward_simplify env (Positive, e) (active, tbl) in
2624 | None -> simpl hd tl others_simpl
2625 | Some e -> simpl hd tl ((u, (snd e))::others_simpl)
2629 | None -> others_simpl
2630 | Some e -> (u, (snd e))::others_simpl
2634 match equalities with
2637 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
2639 List.rev (simpl (*(Positive,*) hd others [])
2643 (Printf.sprintf "\nequalities AFTER:\n%s\n"
2647 Printf.sprintf "%s: %s"
2648 (UriManager.string_of_uri u)
2649 (string_of_equality e)
2655 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
2659 let main_demod_equalities dbd term metasenv ugraph =
2660 let module C = Cic in
2661 let module T = CicTypeChecker in
2662 let module PET = ProofEngineTypes in
2663 let module PP = CicPp in
2664 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2665 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2666 let proof, goals = status in
2667 let goal' = List.nth goals 0 in
2668 let _, metasenv, meta_proof, _ = proof in
2669 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2670 let eq_indexes, equalities, maxm = find_equalities context proof in
2671 let lib_eq_uris, library_equalities, maxm =
2672 find_library_equalities dbd context (proof, goal') (maxm+2)
2674 let library_equalities = List.map snd library_equalities in
2675 maxmeta := maxm+2; (* TODO ugly!! *)
2676 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2677 let new_meta_goal, metasenv, type_of_goal =
2678 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2681 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
2682 (CicPp.ppterm ty)));
2683 Cic.Meta (maxm+1, irl),
2684 (maxm+1, context, ty)::metasenv,
2687 let env = (metasenv, context, ugraph) in
2689 let goal = Inference.BasicProof ([],new_meta_goal), [], goal in
2690 let equalities = simplify_equalities env (equalities@library_equalities) in
2691 let active = make_active () in
2692 let passive = make_passive [] equalities in
2693 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2694 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2695 Printf.printf "\nequalities:\n%s\n"
2698 (string_of_equality ~env) equalities));
2699 print_endline "--------------------------------------------------";
2700 print_endline "GO!";
2701 start_time := Unix.gettimeofday ();
2702 if !time_limit < 1. then time_limit := 60.;
2704 saturate_equations env goal (fun e -> true) passive active
2708 List.fold_left (fun s e -> EqualitySet.add e s)
2709 EqualitySet.empty equalities
2712 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2717 | (n, _), (p, _), _ ->
2718 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2721 let l = List.map snd (fst ra) in
2722 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2724 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2725 (String.concat "\n" (List.map (string_of_equality ~env) active))
2726 (* (String.concat "\n"
2727 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2728 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2730 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) passive));
2734 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2738 let demodulate_tac ~dbd ~pattern ((proof,goal) as initialstatus) =
2739 let module I = Inference in
2740 let curi,metasenv,pbo,pty = proof in
2741 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2742 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2743 let lib_eq_uris, library_equalities, maxm =
2744 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2745 if library_equalities = [] then prerr_endline "VUOTA!!!";
2746 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2747 let library_equalities = List.map snd library_equalities in
2748 let goalterm = Cic.Meta (metano,irl) in
2749 let initgoal = Inference.BasicProof ([],goalterm), [], ty in
2750 let env = (metasenv, context, CicUniv.empty_ugraph) in
2751 let equalities = simplify_equalities env (equalities@library_equalities) in
2754 (fun tbl eq -> Indexing.index tbl eq)
2755 Indexing.empty equalities
2757 let newmeta,(newproof,newmetasenv, newty) = Indexing.demodulation_goal
2758 maxm (metasenv,context,CicUniv.empty_ugraph) table initgoal
2760 if newmeta != maxm then
2762 let opengoal = Cic.Meta(maxm,irl) in
2764 Inference.build_proof_term ~noproof:opengoal newproof in
2765 let extended_metasenv = (maxm,context,newty)::metasenv in
2766 let extended_status =
2767 (curi,extended_metasenv,pbo,pty),goal in
2768 let (status,newgoals) =
2769 ProofEngineTypes.apply_tactic
2770 (PrimitiveTactics.apply_tac ~term:proofterm)
2772 (status,maxm::newgoals)
2774 else if newty = ty then
2775 raise (ProofEngineTypes.Fail (lazy "no progress"))
2776 else ProofEngineTypes.apply_tactic
2777 (ReductionTactics.simpl_tac ~pattern)
2781 let demodulate_tac ~dbd ~pattern =
2782 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)