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/.
32 (* set to false to disable paramodulation inside auto_tac *)
33 let connect_to_auto = true;;
36 (* profiling statistics... *)
37 let infer_time = ref 0.;;
38 let forward_simpl_time = ref 0.;;
39 let forward_simpl_new_time = ref 0.;;
40 let backward_simpl_time = ref 0.;;
41 let passive_maintainance_time = ref 0.;;
43 (* limited-resource-strategy related globals *)
44 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
45 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
46 let start_time = ref 0.;; (* time at which the execution started *)
47 let elapsed_time = ref 0.;;
48 (* let maximal_weight = ref None;; *)
49 let maximal_retained_equality = ref None;;
51 (* equality-selection related globals *)
52 let use_fullred = ref true;;
53 let weight_age_ratio = ref (* 5 *) 4;; (* settable by the user *)
54 let weight_age_counter = ref !weight_age_ratio;;
55 let symbols_ratio = ref (* 0 *) 3;;
56 let symbols_counter = ref 0;;
58 (* non-recursive Knuth-Bendix term ordering by default *)
59 (* Utils.compare_terms := Utils.rpo;; *)
60 (* Utils.compare_terms := Utils.nonrec_kbo;; *)
61 (* Utils.compare_terms := Utils.ao;; *)
64 let derived_clauses = ref 0;;
65 let kept_clauses = ref 0;;
67 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
70 (* varbiables controlling the search-space *)
71 let maxdepth = ref 3;;
72 let maxwidth = ref 3;;
76 | ParamodulationFailure
77 | ParamodulationSuccess of Inference.proof option * environment
80 type goal = proof * Cic.metasenv * Cic.term;;
82 type theorem = Cic.term * Cic.term * Cic.metasenv;;
84 let symbols_of_equality (_, _, (_, left, right, _), _, _) =
85 let m1 = symbols_of_term left in
90 let c = TermMap.find k res in
91 TermMap.add k (c+v) res
94 (symbols_of_term right) m1
99 module OrderedEquality = struct
100 type t = Inference.equality
102 let compare eq1 eq2 =
103 match meta_convertibility_eq eq1 eq2 with
106 let w1, _, (ty, left, right, _), _, a = eq1
107 and w2, _, (ty', left', right', _), _, a' = eq2 in
108 match Pervasives.compare w1 w2 with
110 let res = (List.length a) - (List.length a') in
111 if res <> 0 then res else (
113 let res = Pervasives.compare (List.hd a) (List.hd a') in
114 if res <> 0 then res else Pervasives.compare eq1 eq2
115 with Failure "hd" -> Pervasives.compare eq1 eq2
120 module EqualitySet = Set.Make(OrderedEquality);;
124 selects one equality from passive. The selection strategy is a combination
125 of weight, age and goal-similarity
127 let select env goals passive (active, _) =
128 processed_clauses := !processed_clauses + 1;
130 match (List.rev goals) with (_, goal::_)::_ -> goal | _ -> assert false
132 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
134 List.filter (fun e -> e <> eq) l
136 if !weight_age_ratio > 0 then
137 weight_age_counter := !weight_age_counter - 1;
138 match !weight_age_counter with
140 weight_age_counter := !weight_age_ratio;
141 match neg_list, pos_list with
143 (* Negatives aren't indexed, no need to remove them... *)
145 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
146 | [], (hd:EqualitySet.elt)::tl ->
148 Indexing.remove_index passive_table hd
151 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
152 | _, _ -> assert false
154 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) -> (
155 symbols_counter := !symbols_counter - 1;
156 let cardinality map =
157 TermMap.fold (fun k v res -> res + v) map 0
160 let _, _, term = goal in
163 let card = cardinality symbols in
164 let foldfun k v (r1, r2) =
165 if TermMap.mem k symbols then
166 let c = TermMap.find k symbols in
167 let c1 = abs (c - v) in
173 let f equality (i, e) =
175 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
177 let c = others + (abs (common - card)) in
178 if c < i then (c, equality)
181 let e1 = EqualitySet.min_elt pos_set in
184 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
186 (others + (abs (common - card))), e1
188 let _, current = EqualitySet.fold f pos_set initial in
190 Indexing.remove_index passive_table current
194 (remove current pos_list, EqualitySet.remove current pos_set),
198 symbols_counter := !symbols_ratio;
199 let set_selection set = EqualitySet.min_elt set in
200 if EqualitySet.is_empty neg_set then
201 let current = set_selection pos_set in
204 (remove current pos_list, EqualitySet.remove current pos_set),
205 Indexing.remove_index passive_table current
207 (Positive, current), passive
209 let current = set_selection neg_set in
211 (remove current neg_list, EqualitySet.remove current neg_set),
215 (Negative, current), passive
219 (* initializes the passive set of equalities *)
220 let make_passive neg pos =
221 let set_of equalities =
222 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
225 List.fold_left (fun tbl e -> Indexing.index tbl e) Indexing.empty pos
238 (* adds to passive a list of equalities: new_neg is a list of negative
239 equalities, new_pos a list of positive equalities *)
240 let add_to_passive passive (new_neg, new_pos) =
241 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
242 let ok set equality = not (EqualitySet.mem equality set) in
243 let neg = List.filter (ok neg_set) new_neg
244 and pos = List.filter (ok pos_set) new_pos in
246 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
248 let add set equalities =
249 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
251 (neg @ neg_list, add neg_set neg),
252 (pos_list @ pos, add pos_set pos),
257 let passive_is_empty = function
258 | ([], _), ([], _), _ -> true
263 let size_of_passive ((_, ns), (_, ps), _) =
264 (EqualitySet.cardinal ns) + (EqualitySet.cardinal ps)
268 let size_of_active (active_list, _) =
269 List.length active_list
273 (* removes from passive equalities that are estimated impossible to activate
274 within the current time limit *)
275 let prune_passive howmany (active, _) passive =
276 let (nl, ns), (pl, ps), tbl = passive in
277 let howmany = float_of_int howmany
278 and ratio = float_of_int !weight_age_ratio in
281 int_of_float (if t -. v < 0.5 then t else v)
283 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
284 and in_age = round (howmany /. (ratio +. 1.)) in
286 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
289 | (Negative, e)::_ ->
290 let symbols = symbols_of_equality e in
291 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
295 let counter = ref !symbols_ratio in
296 let rec pickw w ns ps =
298 if not (EqualitySet.is_empty ns) then
299 let e = EqualitySet.min_elt ns in
300 let ns', ps = pickw (w-1) (EqualitySet.remove e ns) ps in
301 EqualitySet.add e ns', ps
302 else if !counter > 0 then
304 counter := !counter - 1;
305 if !counter = 0 then counter := !symbols_ratio
309 let e = EqualitySet.min_elt ps in
310 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
311 ns, EqualitySet.add e ps'
313 let foldfun k v (r1, r2) =
314 if TermMap.mem k symbols then
315 let c = TermMap.find k symbols in
316 let c1 = abs (c - v) in
322 let f equality (i, e) =
324 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
326 let c = others + (abs (common - card)) in
327 if c < i then (c, equality)
330 let e1 = EqualitySet.min_elt ps in
333 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
335 (others + (abs (common - card))), e1
337 let _, e = EqualitySet.fold f ps initial in
338 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
339 ns, EqualitySet.add e ps'
341 let e = EqualitySet.min_elt ps in
342 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
343 ns, EqualitySet.add e ps'
345 EqualitySet.empty, EqualitySet.empty
347 let ns, ps = pickw in_weight ns ps in
348 let rec picka w s l =
352 | hd::tl when not (EqualitySet.mem hd s) ->
353 let w, s, l = picka (w-1) s tl in
354 w, EqualitySet.add hd s, hd::l
356 let w, s, l = picka w s tl in
361 let in_age, ns, nl = picka in_age ns nl in
362 let _, ps, pl = picka in_age ps pl in
363 if not (EqualitySet.is_empty ps) then
364 maximal_retained_equality := Some (EqualitySet.max_elt ps);
367 (fun e tbl -> Indexing.index tbl e) ps Indexing.empty
369 (nl, ns), (pl, ps), tbl
373 (** inference of new equalities between current and some in active *)
374 let infer env sign current (active_list, active_table) =
375 let new_neg, new_pos =
379 Indexing.superposition_left !maxmeta env active_table current in
384 Indexing.superposition_right !maxmeta env active_table current in
386 let rec infer_positive table = function
388 | (Negative, equality)::tl ->
390 Indexing.superposition_left !maxmeta env table equality in
392 let neg, pos = infer_positive table tl in
394 | (Positive, equality)::tl ->
396 Indexing.superposition_right !maxmeta env table equality in
398 let neg, pos = infer_positive table tl in
401 let curr_table = Indexing.index Indexing.empty current in
402 let neg, pos = infer_positive curr_table active_list in
405 derived_clauses := !derived_clauses + (List.length new_neg) +
406 (List.length new_pos);
407 match !maximal_retained_equality with
408 | None -> new_neg, new_pos
410 (* if we have a maximal_retained_equality, we can discard all equalities
411 "greater" than it, as they will never be reached... An equality is
412 greater than maximal_retained_equality if it is bigger
413 wrt. OrderedEquality.compare and it is less similar than
414 maximal_retained_equality to the current goal *)
416 match active_list with
417 | (Negative, e)::_ ->
418 let symbols = symbols_of_equality e in
419 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
426 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
429 if OrderedEquality.compare e eq <= 0 then
432 let foldfun k v (r1, r2) =
433 if TermMap.mem k symbols then
434 let c = TermMap.find k symbols in
435 let c1 = abs (c - v) in
443 TermMap.fold foldfun (symbols_of_equality eq) (0, 0) in
444 others + (abs (common - card))
447 TermMap.fold foldfun (symbols_of_equality e) (0, 0) in
448 let c = others + (abs (common - card)) in
449 if c < initial then true else false
451 List.filter filterfun new_pos
457 let contains_empty env (negative, positive) =
458 let metasenv, context, ugraph = env in
462 (fun (w, proof, (ty, left, right, ordering), m, a) ->
463 fst (CicReduction.are_convertible context left right ugraph))
472 (** simplifies current using active and passive *)
473 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
474 let pl, passive_table =
477 | Some ((pn, _), (pp, _), pt) ->
478 let pn = List.map (fun e -> (Negative, e)) pn
479 and pp = List.map (fun e -> (Positive, e)) pp in
482 let all = if pl = [] then active_list else active_list @ pl in
484 let demodulate table current =
485 let newmeta, newcurrent =
487 let w, proof, (eq_ty, left, right, order), metas, args = current in
488 let metasenv, context, ugraph = env in
489 let metasenv' = metasenv @ metas in
491 CicTypeChecker.type_of_aux' metasenv' context left ugraph;
492 CicTypeChecker.type_of_aux' metasenv' context right ugraph;
494 CicUtil.Meta_not_found _ as exn ->
496 prerr_endline "siamo in forward_simplify";
497 prerr_endline (CicPp.ppterm left);
498 prerr_endline (CicPp.ppterm right);
502 Indexing.demodulation_equality !maxmeta env table sign current in
504 if is_identity env newcurrent then
505 if sign = Negative then Some (sign, newcurrent)
509 (* (Printf.sprintf "\ncurrent was: %s\nnewcurrent is: %s\n" *)
510 (* (string_of_equality current) *)
511 (* (string_of_equality newcurrent))); *)
514 (* (Printf.sprintf "active is: %s" *)
515 (* (String.concat "\n" *)
516 (* (List.map (fun (_, e) -> (string_of_equality e)) active_list)))); *)
520 Some (sign, newcurrent)
523 let res = demodulate active_table current in
526 | Some (sign, newcurrent) ->
527 match passive_table with
529 | Some passive_table -> demodulate passive_table newcurrent
533 | Some (Negative, c) ->
536 (fun (s, eq) -> s = Negative && meta_convertibility_eq eq c)
539 if ok then res else None
540 | Some (Positive, c) ->
541 if Indexing.in_index active_table c then
544 match passive_table with
546 if fst (Indexing.subsumption env active_table c) then
550 | Some passive_table ->
551 if Indexing.in_index passive_table c then None
553 let r1, _ = Indexing.subsumption env active_table c in
555 let r2, _ = Indexing.subsumption env passive_table c in
556 if r2 then None else res
559 type fs_time_info_t = {
560 mutable build_all: float;
561 mutable demodulate: float;
562 mutable subsumption: float;
565 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
568 (** simplifies new using active and passive *)
569 let forward_simplify_new env (new_neg, new_pos) ?passive active =
570 let t1 = Unix.gettimeofday () in
572 let active_list, active_table = active in
573 let pl, passive_table =
576 | Some ((pn, _), (pp, _), pt) ->
577 let pn = List.map (fun e -> (Negative, e)) pn
578 and pp = List.map (fun e -> (Positive, e)) pp in
582 let t2 = Unix.gettimeofday () in
583 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
585 let demodulate sign table target =
586 let newmeta, newtarget =
588 let w, proof, (eq_ty, left, right, order), metas, args = target in
589 let metasenv, context, ugraph = env in
590 let metasenv' = metasenv @ metas in
592 CicTypeChecker.type_of_aux' metasenv' context left ugraph;
593 CicTypeChecker.type_of_aux' metasenv' context right ugraph;
595 CicUtil.Meta_not_found _ as exn ->
597 prerr_endline "siamo in forward_simplify_new";
598 prerr_endline (CicPp.ppterm left);
599 prerr_endline (CicPp.ppterm right);
603 Indexing.demodulation_equality !maxmeta env table sign target in
607 let t1 = Unix.gettimeofday () in
609 let new_neg, new_pos =
610 let new_neg = List.map (demodulate Negative active_table) new_neg
611 and new_pos = List.map (demodulate Positive active_table) new_pos in
612 match passive_table with
613 | None -> new_neg, new_pos
614 | Some passive_table ->
615 List.map (demodulate Negative passive_table) new_neg,
616 List.map (demodulate Positive passive_table) new_pos
619 let t2 = Unix.gettimeofday () in
620 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
625 if not (Inference.is_identity env e) then
626 if EqualitySet.mem e s then s
627 else EqualitySet.add e s
629 EqualitySet.empty new_pos
631 let new_pos = EqualitySet.elements new_pos_set in
634 match passive_table with
636 (fun e -> not (fst (Indexing.subsumption env active_table e)))
637 | Some passive_table ->
638 (fun e -> not ((fst (Indexing.subsumption env active_table e)) ||
639 (fst (Indexing.subsumption env passive_table e))))
641 (* let t1 = Unix.gettimeofday () in *)
642 (* let t2 = Unix.gettimeofday () in *)
643 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
645 match passive_table with
647 (fun e -> not (Indexing.in_index active_table e))
648 | Some passive_table ->
650 not ((Indexing.in_index active_table e) ||
651 (Indexing.in_index passive_table e)))
653 new_neg, List.filter subs (List.filter is_duplicate new_pos)
657 (** simplifies active usign new *)
658 let backward_simplify_active env new_pos new_table min_weight active =
659 let active_list, active_table = active in
660 let active_list, newa =
662 (fun (s, equality) (res, newn) ->
663 let ew, _, _, _, _ = equality in
664 if ew < min_weight then
665 (s, equality)::res, newn
667 match forward_simplify env (s, equality) (new_pos, new_table) with
677 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
681 (fun (s, eq) (res, tbl) ->
682 if List.mem (s, eq) res then
684 else if (is_identity env eq) || (find eq res) then (
688 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
689 active_list ([], Indexing.empty),
691 (fun (s, eq) (n, p) ->
692 if (s <> Negative) && (is_identity env eq) then (
695 if s = Negative then eq::n, p
700 | [], [] -> active, None
701 | _ -> active, Some newa
705 (** simplifies passive using new *)
706 let backward_simplify_passive env new_pos new_table min_weight passive =
707 let (nl, ns), (pl, ps), passive_table = passive in
708 let f sign equality (resl, ress, newn) =
709 let ew, _, _, _, _ = equality in
710 if ew < min_weight then
711 equality::resl, ress, newn
713 match forward_simplify env (sign, equality) (new_pos, new_table) with
714 | None -> resl, EqualitySet.remove equality ress, newn
717 equality::resl, ress, newn
719 let ress = EqualitySet.remove equality ress in
722 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
723 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
726 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
728 match newn, newp with
729 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
730 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
734 let backward_simplify env new' ?passive active =
735 let new_pos, new_table, min_weight =
738 let ew, _, _, _, _ = e in
739 (Positive, e)::l, Indexing.index t e, min ew w)
740 ([], Indexing.empty, 1000000) (snd new')
743 backward_simplify_active env new_pos new_table min_weight active in
746 active, (make_passive [] []), newa, None
749 backward_simplify_passive env new_pos new_table min_weight passive in
750 active, passive, newa, newp
754 (* returns an estimation of how many equalities in passive can be activated
755 within the current time limit *)
756 let get_selection_estimate () =
757 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
758 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
760 ceil ((float_of_int !processed_clauses) *.
761 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
765 (** initializes the set of goals *)
766 let make_goals goal =
768 and passive = [0, [goal]] in
773 (** initializes the set of theorems *)
774 let make_theorems theorems =
779 let activate_goal (active, passive) =
781 | goal_conj::tl -> true, (goal_conj::active, tl)
782 | [] -> false, (active, passive)
786 let activate_theorem (active, passive) =
788 | theorem::tl -> true, (theorem::active, tl)
789 | [] -> false, (active, passive)
793 (** simplifies a goal with equalities in active and passive *)
794 let simplify_goal env goal ?passive (active_list, active_table) =
795 let pl, passive_table =
798 | Some ((pn, _), (pp, _), pt) ->
799 let pn = List.map (fun e -> (Negative, e)) pn
800 and pp = List.map (fun e -> (Positive, e)) pp in
804 let demodulate table goal =
805 let newmeta, newgoal =
806 Indexing.demodulation_goal !maxmeta env table goal in
808 goal != newgoal, newgoal
811 match passive_table with
812 | None -> demodulate active_table goal
813 | Some passive_table ->
814 let changed, goal = demodulate active_table goal in
815 let changed', goal = demodulate passive_table goal in
816 (changed || changed'), goal
822 let simplify_goals env goals ?passive active =
823 let a_goals, p_goals = goals in
828 List.map (fun g -> snd (simplify_goal env g ?passive active)) gl in
834 (fun (a, p) (d, gl) ->
835 let changed = ref false in
839 let c, g = simplify_goal env g ?passive active in
840 changed := !changed || c; g) gl in
841 if !changed then (a, (d, gl)::p) else ((d, gl)::a, p))
842 ([], p_goals) a_goals
848 let simplify_theorems env theorems ?passive (active_list, active_table) =
849 let pl, passive_table =
852 | Some ((pn, _), (pp, _), pt) ->
853 let pn = List.map (fun e -> (Negative, e)) pn
854 and pp = List.map (fun e -> (Positive, e)) pp in
857 let a_theorems, p_theorems = theorems in
858 let demodulate table theorem =
859 let newmeta, newthm =
860 Indexing.demodulation_theorem !maxmeta env table theorem in
862 theorem != newthm, newthm
864 let foldfun table (a, p) theorem =
865 let changed, theorem = demodulate table theorem in
866 if changed then (a, theorem::p) else (theorem::a, p)
868 let mapfun table theorem = snd (demodulate table theorem) in
869 match passive_table with
871 let p_theorems = List.map (mapfun active_table) p_theorems in
872 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
873 | Some passive_table ->
874 let p_theorems = List.map (mapfun active_table) p_theorems in
875 let p_theorems, a_theorems =
876 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
877 let p_theorems = List.map (mapfun passive_table) p_theorems in
878 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
882 let rec simpl env e others others_simpl =
883 let active = others @ others_simpl in
886 (fun t (_, e) -> Indexing.index t e)
887 Indexing.empty active
889 let res = forward_simplify env e (active, tbl) in
893 | None -> simpl env hd tl others_simpl
894 | Some e -> simpl env hd tl (e::others_simpl)
898 | None -> others_simpl
899 | Some e -> e::others_simpl
903 let simplify_equalities env equalities =
906 (Printf.sprintf "equalities:\n%s\n"
908 (List.map string_of_equality equalities))));
909 debug_print (lazy "SIMPLYFYING EQUALITIES...");
910 match equalities with
913 let others = List.map (fun e -> (Positive, e)) tl in
915 List.rev (List.map snd (simpl env (Positive, hd) others []))
919 (Printf.sprintf "equalities AFTER:\n%s\n"
921 (List.map string_of_equality res))));
925 (* applies equality to goal to see if the goal can be closed *)
926 let apply_equality_to_goal env equality goal =
927 let module C = Cic in
928 let module HL = HelmLibraryObjects in
929 let module I = Inference in
930 let metasenv, context, ugraph = env in
931 let _, proof, (ty, left, right, _), metas, args = equality in
933 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
934 let gproof, gmetas, gterm = goal in
937 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
938 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
940 let subst, metasenv', _ =
941 let menv = metasenv @ metas @ gmetas in
942 Inference.unification menv context eqterm gterm ugraph
946 | I.BasicProof t -> I.BasicProof (CicMetaSubst.apply_subst subst t)
947 | I.ProofBlock (s, uri, nt, t, pe, p) ->
948 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
952 let rec repl = function
953 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
954 | I.NoProof -> newproof
955 | I.BasicProof p -> newproof
956 | I.SubProof (t, i, p) -> I.SubProof (t, i, repl p)
961 true, subst, newgproof
962 with CicUnification.UnificationFailure _ ->
968 let new_meta metasenv =
969 let m = CicMkImplicit.new_meta metasenv [] in
971 while !maxmeta <= m do incr maxmeta done;
976 (* applies a theorem or an equality to goal, returning a list of subgoals or
977 an indication of failure *)
978 let apply_to_goal env theorems ?passive active goal =
979 let metasenv, context, ugraph = env in
980 let proof, metas, term = goal in
983 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
984 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
987 CicMkImplicit.identity_relocation_list_for_metavariable context in
988 let proof', newmeta =
989 let rec get_meta = function
990 | SubProof (t, i, p) ->
991 let t', i' = get_meta p in
992 if i' = -1 then t, i else t', i'
993 | ProofGoalBlock (_, p) -> get_meta p
994 | _ -> Cic.Implicit None, -1
996 let p, m = get_meta proof in
998 let n = new_meta (metasenv @ metas) in
1003 let metasenv = (newmeta, context, term)::metasenv @ metas in
1004 let bit = new_meta metasenv, context, term in
1005 let metasenv' = bit::metasenv in
1006 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
1008 let rec aux = function
1010 | (theorem, thmty, _)::tl ->
1012 let subst, (newproof, newgoals) =
1013 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
1015 if newgoals = [] then
1016 let _, _, p, _ = newproof in
1018 let rec repl = function
1019 | Inference.ProofGoalBlock (_, gp) ->
1020 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
1021 | Inference.NoProof -> Inference.BasicProof p
1022 | Inference.BasicProof _ -> Inference.BasicProof p
1023 | Inference.SubProof (t, i, p2) ->
1024 Inference.SubProof (t, i, repl p2)
1029 let _, m = status in
1030 let subst = List.filter (fun (i, _) -> i = m) subst in
1031 `Ok (subst, [newp, metas, term])
1033 let _, menv, p, _ = newproof in
1035 CicMkImplicit.identity_relocation_list_for_metavariable context
1040 let _, _, ty = CicUtil.lookup_meta i menv in
1042 let rec gp = function
1043 | SubProof (t, i, p) ->
1044 SubProof (t, i, gp p)
1045 | ProofGoalBlock (sp1, sp2) ->
1046 ProofGoalBlock (sp1, gp sp2)
1049 SubProof (p, i, BasicProof (Cic.Meta (i, irl)))
1050 | ProofSymBlock (s, sp) ->
1051 ProofSymBlock (s, gp sp)
1052 | ProofBlock (s, u, nt, t, pe, sp) ->
1053 ProofBlock (s, u, nt, t, pe, gp sp)
1061 let w, m = weight_of_term t in
1062 w + 2 * (List.length m)
1065 (fun (_, _, t1) (_, _, t2) ->
1066 Pervasives.compare (weight t1) (weight t2))
1069 let best = aux tl in
1071 | `Ok (_, _) -> best
1072 | `No -> `GoOn ([subst, goals])
1073 | `GoOn sl -> `GoOn ((subst, goals)::sl)
1074 with ProofEngineTypes.Fail msg ->
1078 if Inference.term_is_equality term then
1079 let rec appleq_a = function
1080 | [] -> false, [], []
1081 | (Positive, equality)::tl ->
1082 let ok, s, newproof = apply_equality_to_goal env equality goal in
1083 if ok then true, s, [newproof, metas, term] else appleq_a tl
1084 | _::tl -> appleq_a tl
1086 let rec appleq_p = function
1087 | [] -> false, [], []
1089 let ok, s, newproof = apply_equality_to_goal env equality goal in
1090 if ok then true, s, [newproof, metas, term] else appleq_p tl
1092 let al, _ = active in
1094 | None -> appleq_a al
1095 | Some (_, (pl, _), _) ->
1096 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1100 if r = true then `Ok (s, l) else aux theorems
1104 (* sorts a conjunction of goals in order to detect earlier if it is
1105 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1106 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1109 (fun (_, e1, g1) (_, e2, g2) ->
1111 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1113 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1117 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1122 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1126 if prop1 = 0 && prop2 = 0 then
1127 let e1 = if Inference.term_is_equality g1 then 0 else 1
1128 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1138 let is_meta_closed goals =
1139 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1143 (* applies a series of theorems/equalities to a conjunction of goals *)
1144 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1145 let aux (goal, r) tl =
1146 let propagate_subst subst (proof, metas, term) =
1147 let rec repl = function
1148 | NoProof -> NoProof
1150 BasicProof (CicMetaSubst.apply_subst subst t)
1151 | ProofGoalBlock (p, pb) ->
1152 let pb' = repl pb in
1153 ProofGoalBlock (p, pb')
1154 | SubProof (t, i, p) ->
1155 let t' = CicMetaSubst.apply_subst subst t in
1158 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1159 | ProofBlock (s, u, nty, t, pe, p) ->
1160 ProofBlock (subst @ s, u, nty, t, pe, p)
1161 in (repl proof, metas, term)
1163 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1165 | `No -> `No (depth, goals)
1170 let tl = List.map (propagate_subst s) tl in
1171 sort_goal_conj env (depth+1, gl @ tl)) sl
1174 | `Ok (subst, gl) ->
1178 let p, _, _ = List.hd gl in
1180 let rec repl = function
1181 | SubProof (_, _, p) -> repl p
1182 | ProofGoalBlock (p1, p2) ->
1183 ProofGoalBlock (repl p1, repl p2)
1186 build_proof_term (repl p)
1189 let rec get_meta = function
1190 | SubProof (_, i, p) ->
1191 let i' = get_meta p in
1192 if i' = -1 then i else i'
1193 (* max i (get_meta p) *)
1194 | ProofGoalBlock (_, p) -> get_meta p
1200 let _, (context, _, _) = List.hd subst in
1201 [i, (context, subproof, Cic.Implicit None)]
1203 let tl = List.map (propagate_subst subst) tl in
1204 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1208 if depth > !maxdepth || (List.length goals) > !maxwidth then
1211 let rec search_best res = function
1214 let r = apply_to_goal env theorems ?passive active goal in
1216 | `Ok _ -> (goal, r)
1217 | `No -> search_best res tl
1221 | _, `Ok _ -> assert false
1224 if (List.length l) < (List.length l2) then goal, r else res
1226 search_best newres tl
1228 let hd = List.hd goals in
1229 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1233 | _, _ -> search_best res (List.tl goals)
1235 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1237 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1238 (List.length (snd conj)) < (List.length goals)->
1239 apply_to_goal_conj env theorems ?passive active conj
1245 module OrderedGoals = struct
1246 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1253 else let r = (List.length l1) - (List.length l2) in
1259 (fun (_, _, t1) (_, _, t2) ->
1260 let r = Pervasives.compare t1 t2 in
1269 module GoalsSet = Set.Make(OrderedGoals);;
1272 exception SearchSpaceOver;;
1277 let apply_to_goals env is_passive_empty theorems active goals =
1278 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1279 let add_to set goals =
1280 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1282 let rec aux set = function
1284 debug_print (lazy "HERE!!!");
1285 if is_passive_empty then raise SearchSpaceOver else false, set
1287 let res = apply_to_goal_conj env theorems active goals in
1293 | (d, (p, _, t)::_) -> d, p, t
1298 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1299 d (string_of_proof p) (CicPp.ppterm t)))
1301 true, GoalsSet.singleton newgoals
1303 let set' = add_to set (goals::tl) in
1304 let set' = add_to set' newgoals in
1309 let n = List.length goals in
1310 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1311 let goals = GoalsSet.elements goals in
1312 debug_print (lazy "\n\tapply_to_goals end\n");
1313 let m = List.length goals in
1314 if m = n && is_passive_empty then
1315 raise SearchSpaceOver
1322 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1323 work that well yet...) *)
1324 let sort_passive_goals goals =
1326 (fun (d1, l1) (d2, l2) ->
1328 and r2 = (List.length l1) - (List.length l2) in
1329 let foldfun ht (_, _, t) =
1330 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1333 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1334 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1335 in let r3 = m1 - m2 in
1337 else if r2 <> 0 then r2
1339 (* let _, _, g1 = List.hd l1 *)
1340 (* and _, _, g2 = List.hd l2 in *)
1341 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1342 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1343 (* in let r4 = e1 - e2 in *)
1344 (* if r4 <> 0 then r3 else r1) *)
1349 let print_goals goals =
1356 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1358 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1362 (* tries to prove the first conjunction in goals with applications of
1363 theorems/equalities, returning new sub-goals or an indication of success *)
1364 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1365 let theorems, _ = theorems in
1366 let a_goals, p_goals = goals in
1367 let goal = List.hd a_goals in
1368 let not_in_active gl =
1372 if (List.length gl) = (List.length gl') then
1373 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1379 let res = apply_to_goal_conj env theorems ?passive active goal in
1382 true, ([newgoals], [])
1384 false, (a_goals, p_goals)
1389 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1392 let p_goals = newgoals @ p_goals in
1393 let p_goals = sort_passive_goals p_goals in
1394 false, (a_goals, p_goals)
1400 let apply_theorem_to_goals env theorems active goals =
1401 let a_goals, p_goals = goals in
1402 let theorem = List.hd (fst theorems) in
1403 let theorems = [theorem] in
1404 let rec aux p = function
1405 | [] -> false, ([], p)
1407 let res = apply_to_goal_conj env theorems active goal in
1409 | `Ok newgoals -> true, ([newgoals], [])
1411 | `GoOn newgoals -> aux (newgoals @ p) tl
1413 let ok, (a, p) = aux p_goals a_goals in
1419 (fun (d1, l1) (d2, l2) ->
1422 else let r = (List.length l1) - (List.length l2) in
1428 (fun (_, _, t1) (_, _, t2) ->
1429 let r = Pervasives.compare t1 t2 in
1430 if r <> 0 then (res := r; true) else false) l1 l2
1434 ok, (a_goals, p_goals)
1438 (* given-clause algorithm with lazy reduction strategy *)
1439 let rec given_clause dbd env goals theorems passive active =
1440 let goals = simplify_goals env goals active in
1441 let ok, goals = activate_goal goals in
1442 (* let theorems = simplify_theorems env theorems active in *)
1444 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1447 match (fst goals) with
1448 | (_, [proof, _, _])::_ -> Some proof
1451 ParamodulationSuccess (proof, env)
1453 given_clause_aux dbd env goals theorems passive active
1455 (* let ok', theorems = activate_theorem theorems in *)
1456 let ok', theorems = false, theorems in
1458 let ok, goals = apply_theorem_to_goals env theorems active goals in
1461 match (fst goals) with
1462 | (_, [proof, _, _])::_ -> Some proof
1465 ParamodulationSuccess (proof, env)
1467 given_clause_aux dbd env goals theorems passive active
1469 if (passive_is_empty passive) then ParamodulationFailure
1470 else given_clause_aux dbd env goals theorems passive active
1472 and given_clause_aux dbd env goals theorems passive active =
1473 let time1 = Unix.gettimeofday () in
1475 let selection_estimate = get_selection_estimate () in
1476 let kept = size_of_passive passive in
1478 if !time_limit = 0. || !processed_clauses = 0 then
1480 else if !elapsed_time > !time_limit then (
1481 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1482 !time_limit !elapsed_time));
1484 ) else if kept > selection_estimate then (
1486 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1487 "(kept: %d, selection_estimate: %d)\n")
1488 kept selection_estimate));
1489 prune_passive selection_estimate active passive
1494 let time2 = Unix.gettimeofday () in
1495 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1497 kept_clauses := (size_of_passive passive) + (size_of_active active);
1498 match passive_is_empty passive with
1499 | true -> (* ParamodulationFailure *)
1500 given_clause dbd env goals theorems passive active
1502 let (sign, current), passive = select env (fst goals) passive active in
1503 let time1 = Unix.gettimeofday () in
1504 let res = forward_simplify env (sign, current) ~passive active in
1505 let time2 = Unix.gettimeofday () in
1506 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1509 given_clause dbd env goals theorems passive active
1510 | Some (sign, current) ->
1511 if (sign = Negative) && (is_identity env current) then (
1513 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1514 (string_of_equality ~env current)));
1515 let _, proof, _, _, _ = current in
1516 ParamodulationSuccess (Some proof, env)
1519 (lazy "\n================================================");
1520 debug_print (lazy (Printf.sprintf "selected: %s %s"
1521 (string_of_sign sign)
1522 (string_of_equality ~env current)));
1524 let t1 = Unix.gettimeofday () in
1525 let new' = infer env sign current active in
1526 let t2 = Unix.gettimeofday () in
1527 infer_time := !infer_time +. (t2 -. t1);
1529 let res, goal' = contains_empty env new' in
1533 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1536 ParamodulationSuccess (proof, env)
1538 let t1 = Unix.gettimeofday () in
1539 let new' = forward_simplify_new env new' active in
1540 let t2 = Unix.gettimeofday () in
1542 forward_simpl_new_time :=
1543 !forward_simpl_new_time +. (t2 -. t1)
1547 | Negative -> active
1549 let t1 = Unix.gettimeofday () in
1550 let active, _, newa, _ =
1551 backward_simplify env ([], [current]) active
1553 let t2 = Unix.gettimeofday () in
1554 backward_simpl_time :=
1555 !backward_simpl_time +. (t2 -. t1);
1559 let al, tbl = active in
1560 let nn = List.map (fun e -> Negative, e) n in
1565 Indexing.index tbl e)
1570 match contains_empty env new' with
1573 let al, tbl = active in
1575 | Negative -> (sign, current)::al, tbl
1577 al @ [(sign, current)], Indexing.index tbl current
1579 let passive = add_to_passive passive new' in
1580 given_clause dbd env goals theorems passive active
1585 let _, proof, _, _, _ = goal in Some proof
1588 ParamodulationSuccess (proof, env)
1593 (** given-clause algorithm with full reduction strategy *)
1594 let rec given_clause_fullred dbd env goals theorems passive active =
1595 let goals = simplify_goals env goals ~passive active in
1596 let ok, goals = activate_goal goals in
1597 (* let theorems = simplify_theorems env theorems ~passive active in *)
1602 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1603 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1604 (* let current = List.hd (fst goals) in *)
1605 (* let p, _, t = List.hd (snd current) in *)
1608 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1609 (* (CicPp.ppterm t) (string_of_proof p))); *)
1612 apply_goal_to_theorems dbd env theorems ~passive active goals
1616 match (fst goals) with
1617 | (_, [proof, _, _])::_ -> Some proof
1620 ParamodulationSuccess (proof, env)
1622 given_clause_fullred_aux dbd env goals theorems passive active
1624 (* let ok', theorems = activate_theorem theorems in *)
1626 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1629 (* match (fst goals) with *)
1630 (* | (_, [proof, _, _])::_ -> Some proof *)
1631 (* | _ -> assert false *)
1633 (* ParamodulationSuccess (proof, env) *)
1635 (* given_clause_fullred_aux env goals theorems passive active *)
1637 if (passive_is_empty passive) then ParamodulationFailure
1638 else given_clause_fullred_aux dbd env goals theorems passive active
1640 and given_clause_fullred_aux dbd env goals theorems passive active =
1641 let time1 = Unix.gettimeofday () in
1643 let selection_estimate = get_selection_estimate () in
1644 let kept = size_of_passive passive in
1646 if !time_limit = 0. || !processed_clauses = 0 then
1648 else if !elapsed_time > !time_limit then (
1649 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1650 !time_limit !elapsed_time));
1652 ) else if kept > selection_estimate then (
1654 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1655 "(kept: %d, selection_estimate: %d)\n")
1656 kept selection_estimate));
1657 prune_passive selection_estimate active passive
1662 let time2 = Unix.gettimeofday () in
1663 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1665 kept_clauses := (size_of_passive passive) + (size_of_active active);
1666 match passive_is_empty passive with
1667 | true -> (* ParamodulationFailure *)
1668 given_clause_fullred dbd env goals theorems passive active
1670 let (sign, current), passive = select env (fst goals) passive active in
1671 let time1 = Unix.gettimeofday () in
1672 let res = forward_simplify env (sign, current) ~passive active in
1673 let time2 = Unix.gettimeofday () in
1674 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1677 given_clause_fullred dbd env goals theorems passive active
1678 | Some (sign, current) ->
1679 if (sign = Negative) && (is_identity env current) then (
1681 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1682 (string_of_equality ~env current)));
1683 let _, proof, _, _, _ = current in
1684 ParamodulationSuccess (Some proof, env)
1687 (lazy "\n================================================");
1688 debug_print (lazy (Printf.sprintf "selected: %s %s"
1689 (string_of_sign sign)
1690 (string_of_equality ~env current)));
1692 let t1 = Unix.gettimeofday () in
1693 let new' = infer env sign current active in
1694 let t2 = Unix.gettimeofday () in
1695 infer_time := !infer_time +. (t2 -. t1);
1698 if is_identity env current then active
1700 let al, tbl = active in
1702 | Negative -> (sign, current)::al, tbl
1704 al @ [(sign, current)], Indexing.index tbl current
1706 let rec simplify new' active passive =
1707 let t1 = Unix.gettimeofday () in
1708 let new' = forward_simplify_new env new' ~passive active in
1709 let t2 = Unix.gettimeofday () in
1710 forward_simpl_new_time :=
1711 !forward_simpl_new_time +. (t2 -. t1);
1712 let t1 = Unix.gettimeofday () in
1713 let active, passive, newa, retained =
1714 backward_simplify env new' ~passive active in
1715 let t2 = Unix.gettimeofday () in
1716 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1717 match newa, retained with
1718 | None, None -> active, passive, new'
1720 | None, Some (n, p) ->
1721 let nn, np = new' in
1722 simplify (nn @ n, np @ p) active passive
1723 | Some (n, p), Some (rn, rp) ->
1724 let nn, np = new' in
1725 simplify (nn @ n @ rn, np @ p @ rp) active passive
1727 let active, passive, new' = simplify new' active passive in
1729 let k = size_of_passive passive in
1730 if k < (kept - 1) then
1731 processed_clauses := !processed_clauses + (kept - 1 - k);
1736 (Printf.sprintf "active:\n%s\n"
1739 (fun (s, e) -> (string_of_sign s) ^ " " ^
1740 (string_of_equality ~env e))
1748 (Printf.sprintf "new':\n%s\n"
1751 (fun e -> "Negative " ^
1752 (string_of_equality ~env e)) neg) @
1754 (fun e -> "Positive " ^
1755 (string_of_equality ~env e)) pos)))))
1757 match contains_empty env new' with
1759 let passive = add_to_passive passive new' in
1760 given_clause_fullred dbd env goals theorems passive active
1764 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1767 ParamodulationSuccess (proof, env)
1772 let rec saturate_equations env goal accept_fun passive active =
1773 elapsed_time := Unix.gettimeofday () -. !start_time;
1774 if !elapsed_time > !time_limit then
1777 let (sign, current), passive = select env [1, [goal]] passive active in
1778 let res = forward_simplify env (sign, current) ~passive active in
1781 saturate_equations env goal accept_fun passive active
1782 | Some (sign, current) ->
1783 assert (sign = Positive);
1785 (lazy "\n================================================");
1786 debug_print (lazy (Printf.sprintf "selected: %s %s"
1787 (string_of_sign sign)
1788 (string_of_equality ~env current)));
1789 let new' = infer env sign current active in
1791 if is_identity env current then active
1793 let al, tbl = active in
1794 al @ [(sign, current)], Indexing.index tbl current
1796 let rec simplify new' active passive =
1797 let new' = forward_simplify_new env new' ~passive active in
1798 let active, passive, newa, retained =
1799 backward_simplify env new' ~passive active in
1800 match newa, retained with
1801 | None, None -> active, passive, new'
1803 | None, Some (n, p) ->
1804 let nn, np = new' in
1805 simplify (nn @ n, np @ p) active passive
1806 | Some (n, p), Some (rn, rp) ->
1807 let nn, np = new' in
1808 simplify (nn @ n @ rn, np @ p @ rp) active passive
1810 let active, passive, new' = simplify new' active passive in
1814 (Printf.sprintf "active:\n%s\n"
1817 (fun (s, e) -> (string_of_sign s) ^ " " ^
1818 (string_of_equality ~env e))
1826 (Printf.sprintf "new':\n%s\n"
1829 (fun e -> "Negative " ^
1830 (string_of_equality ~env e)) neg) @
1832 (fun e -> "Positive " ^
1833 (string_of_equality ~env e)) pos)))))
1835 let new' = match new' with _, pos -> [], List.filter accept_fun pos in
1836 let passive = add_to_passive passive new' in
1837 saturate_equations env goal accept_fun passive active
1843 let main dbd full term metasenv ugraph =
1844 let module C = Cic in
1845 let module T = CicTypeChecker in
1846 let module PET = ProofEngineTypes in
1847 let module PP = CicPp in
1848 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1849 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1850 let proof, goals = status in
1851 let goal' = List.nth goals 0 in
1852 let _, metasenv, meta_proof, _ = proof in
1853 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1854 let eq_indexes, equalities, maxm = find_equalities context proof in
1855 let lib_eq_uris, library_equalities, maxm =
1856 find_library_equalities dbd context (proof, goal') (maxm+2)
1858 let library_equalities = List.map snd library_equalities in
1859 maxmeta := maxm+2; (* TODO ugly!! *)
1860 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1861 let new_meta_goal, metasenv, type_of_goal =
1862 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1865 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1866 Cic.Meta (maxm+1, irl),
1867 (maxm+1, context, ty)::metasenv,
1870 let env = (metasenv, context, ugraph) in
1871 let t1 = Unix.gettimeofday () in
1874 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1875 let context_hyp = find_context_hypotheses env eq_indexes in
1876 context_hyp @ theorems, []
1879 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1880 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1882 let t = CicUtil.term_of_uri refl_equal in
1883 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1886 let t2 = Unix.gettimeofday () in
1889 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1894 "Theorems:\n-------------------------------------\n%s\n"
1899 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1903 let goal = Inference.BasicProof new_meta_goal, [], goal in
1904 let equalities = simplify_equalities env (equalities@library_equalities) in
1905 let active = make_active () in
1906 let passive = make_passive [] equalities in
1907 Printf.printf "\ncurrent goal: %s\n"
1908 (let _, _, g = goal in CicPp.ppterm g);
1909 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1910 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1911 Printf.printf "\nequalities:\n%s\n"
1914 (string_of_equality ~env) equalities));
1915 (* (equalities @ library_equalities))); *)
1916 print_endline "--------------------------------------------------";
1917 let start = Unix.gettimeofday () in
1918 print_endline "GO!";
1919 start_time := Unix.gettimeofday ();
1921 let goals = make_goals goal in
1922 (if !use_fullred then given_clause_fullred else given_clause)
1923 dbd env goals theorems passive active
1925 let finish = Unix.gettimeofday () in
1928 | ParamodulationFailure ->
1929 Printf.printf "NO proof found! :-(\n\n"
1930 | ParamodulationSuccess (Some proof, env) ->
1931 let proof = Inference.build_proof_term proof in
1932 Printf.printf "OK, found a proof!\n";
1933 (* REMEMBER: we have to instantiate meta_proof, we should use
1934 apply the "apply" tactic to proof and status
1936 let names = names_of_context context in
1937 print_endline (PP.pp proof names);
1940 (fun m (_, _, _, menv, _) -> m @ menv) metasenv equalities
1945 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1947 print_endline (string_of_float (finish -. start));
1949 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1950 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1952 (fst (CicReduction.are_convertible
1953 context type_of_goal ty ug)));
1955 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1956 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1957 print_endline (string_of_float (finish -. start));*)
1961 | ParamodulationSuccess (None, env) ->
1962 Printf.printf "Success, but no proof?!?\n\n"
1964 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1965 "forward_simpl_new_time: %.9f\n" ^^
1966 "backward_simpl_time: %.9f\n")
1967 !infer_time !forward_simpl_time !forward_simpl_new_time
1968 !backward_simpl_time;
1969 Printf.printf "passive_maintainance_time: %.9f\n"
1970 !passive_maintainance_time;
1971 Printf.printf " successful unification/matching time: %.9f\n"
1972 !Indexing.match_unif_time_ok;
1973 Printf.printf " failed unification/matching time: %.9f\n"
1974 !Indexing.match_unif_time_no;
1975 Printf.printf " indexing retrieval time: %.9f\n"
1976 !Indexing.indexing_retrieval_time;
1977 Printf.printf " demodulate_term.build_newtarget_time: %.9f\n"
1978 !Indexing.build_newtarget_time;
1979 Printf.printf "derived %d clauses, kept %d clauses.\n"
1980 !derived_clauses !kept_clauses;
1983 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1989 let default_depth = !maxdepth
1990 and default_width = !maxwidth;;
1994 symbols_counter := 0;
1995 weight_age_counter := !weight_age_ratio;
1996 processed_clauses := 0;
1999 maximal_retained_equality := None;
2001 forward_simpl_time := 0.;
2002 forward_simpl_new_time := 0.;
2003 backward_simpl_time := 0.;
2004 passive_maintainance_time := 0.;
2005 derived_clauses := 0;
2010 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
2011 let module C = Cic in
2013 Indexing.init_index ();
2016 let proof, goal = status in
2018 let uri, metasenv, meta_proof, term_to_prove = proof in
2019 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2020 let eq_indexes, equalities, maxm = find_equalities context proof in
2021 let new_meta_goal, metasenv, type_of_goal =
2023 CicMkImplicit.identity_relocation_list_for_metavariable context in
2024 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2026 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2027 Cic.Meta (maxm+1, irl),
2028 (maxm+1, context, ty)::metasenv,
2031 let ugraph = CicUniv.empty_ugraph in
2032 let env = (metasenv, context, ugraph) in
2033 let goal = Inference.BasicProof new_meta_goal, [], goal in
2035 let t1 = Unix.gettimeofday () in
2036 let lib_eq_uris, library_equalities, maxm =
2037 find_library_equalities dbd context (proof, goal') (maxm+2)
2039 let library_equalities = List.map snd library_equalities in
2040 let t2 = Unix.gettimeofday () in
2042 let equalities = simplify_equalities env (equalities@library_equalities) in
2045 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
2046 let t1 = Unix.gettimeofday () in
2049 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2050 let context_hyp = find_context_hypotheses env eq_indexes in
2051 context_hyp @ thms, []
2054 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2055 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2057 let t = CicUtil.term_of_uri refl_equal in
2058 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2061 let t2 = Unix.gettimeofday () in
2066 "Theorems:\n-------------------------------------\n%s\n"
2071 "Term: %s, type: %s"
2072 (CicPp.ppterm t) (CicPp.ppterm ty))
2076 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2078 let active = make_active () in
2079 let passive = make_passive [] equalities in
2080 let start = Unix.gettimeofday () in
2082 let goals = make_goals goal in
2083 given_clause_fullred dbd env goals theorems passive active
2085 let finish = Unix.gettimeofday () in
2086 (res, finish -. start)
2089 | ParamodulationSuccess (Some proof, env) ->
2090 debug_print (lazy "OK, found a proof!");
2091 let proof = Inference.build_proof_term proof in
2092 let names = names_of_context context in
2095 match new_meta_goal with
2096 | C.Meta (i, _) -> i | _ -> assert false
2098 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2103 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2105 debug_print (lazy (CicPp.pp proof [](* names *)));
2109 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2110 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2112 (fst (CicReduction.are_convertible
2113 context type_of_goal ty ug)))));
2114 let equality_for_replace i t1 =
2116 | C.Meta (n, _) -> n = i
2120 ProofEngineReduction.replace
2121 ~equality:equality_for_replace
2122 ~what:[goal'] ~with_what:[proof]
2127 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2128 (match uri with Some uri -> UriManager.string_of_uri uri
2130 (print_metasenv newmetasenv)
2131 (CicPp.pp real_proof [](* names *))
2132 (CicPp.pp term_to_prove names)));
2133 ((uri, newmetasenv, real_proof, term_to_prove), [])
2134 with CicTypeChecker.TypeCheckerFailure _ ->
2135 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2136 debug_print (lazy (CicPp.pp proof names));
2137 raise (ProofEngineTypes.Fail
2138 (lazy "Found a proof, but it doesn't typecheck"))
2140 debug_print (lazy (Printf.sprintf "\nTIME NEEDED: %.9f" time));
2143 raise (ProofEngineTypes.Fail (lazy "NO proof found"))
2146 (* dummy function called within matita to trigger linkage *)
2150 let retrieve_and_print dbd term metasenv ugraph =
2151 let module C = Cic in
2152 let module T = CicTypeChecker in
2153 let module PET = ProofEngineTypes in
2154 let module PP = CicPp in
2155 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2156 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2157 let proof, goals = status in
2158 let goal' = List.nth goals 0 in
2159 let uri, metasenv, meta_proof, term_to_prove = proof in
2160 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2161 let eq_indexes, equalities, maxm = find_equalities context proof in
2162 let new_meta_goal, metasenv, type_of_goal =
2164 CicMkImplicit.identity_relocation_list_for_metavariable context in
2165 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2167 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2168 Cic.Meta (maxm+1, irl),
2169 (maxm+1, context, ty)::metasenv,
2172 let ugraph = CicUniv.empty_ugraph in
2173 let env = (metasenv, context, ugraph) in
2174 let t1 = Unix.gettimeofday () in
2175 let lib_eq_uris, library_equalities, maxm =
2176 find_library_equalities dbd context (proof, goal') (maxm+2) in
2177 let t2 = Unix.gettimeofday () in
2179 let equalities = (* equalities @ *) library_equalities in
2182 (Printf.sprintf "\n\nequalities:\n%s\n"
2186 (* Printf.sprintf "%s: %s" *)
2187 (UriManager.string_of_uri u)
2188 (* (string_of_equality e) *)
2191 debug_print (lazy "SIMPLYFYING EQUALITIES...");
2192 let rec simpl e others others_simpl =
2194 let active = List.map (fun (u, e) -> (Positive, e))
2195 (others @ others_simpl) in
2198 (fun t (_, e) -> Indexing.index t e)
2199 Indexing.empty active
2201 let res = forward_simplify env (Positive, e) (active, tbl) in
2205 | None -> simpl hd tl others_simpl
2206 | Some e -> simpl hd tl ((u, (snd e))::others_simpl)
2210 | None -> others_simpl
2211 | Some e -> (u, (snd e))::others_simpl
2215 match equalities with
2218 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
2220 List.rev (simpl (*(Positive,*) hd others [])
2224 (Printf.sprintf "\nequalities AFTER:\n%s\n"
2228 Printf.sprintf "%s: %s"
2229 (UriManager.string_of_uri u)
2230 (string_of_equality e)
2236 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
2240 let main_demod_equalities dbd term metasenv ugraph =
2241 let module C = Cic in
2242 let module T = CicTypeChecker in
2243 let module PET = ProofEngineTypes in
2244 let module PP = CicPp in
2245 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2246 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2247 let proof, goals = status in
2248 let goal' = List.nth goals 0 in
2249 let _, metasenv, meta_proof, _ = proof in
2250 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2251 let eq_indexes, equalities, maxm = find_equalities context proof in
2252 let lib_eq_uris, library_equalities, maxm =
2253 find_library_equalities dbd context (proof, goal') (maxm+2)
2255 let library_equalities = List.map snd library_equalities in
2256 maxmeta := maxm+2; (* TODO ugly!! *)
2257 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2258 let new_meta_goal, metasenv, type_of_goal =
2259 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2262 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
2263 (CicPp.ppterm ty)));
2264 Cic.Meta (maxm+1, irl),
2265 (maxm+1, context, ty)::metasenv,
2268 let env = (metasenv, context, ugraph) in
2270 let goal = Inference.BasicProof new_meta_goal, [], goal in
2271 let equalities = simplify_equalities env (equalities@library_equalities) in
2272 let active = make_active () in
2273 let passive = make_passive [] equalities in
2274 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2275 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2276 Printf.printf "\nequalities:\n%s\n"
2279 (string_of_equality ~env) equalities));
2280 print_endline "--------------------------------------------------";
2281 print_endline "GO!";
2282 start_time := Unix.gettimeofday ();
2283 if !time_limit < 1. then time_limit := 60.;
2285 saturate_equations env goal (fun e -> true) passive active
2289 List.fold_left (fun s e -> EqualitySet.add e s)
2290 EqualitySet.empty equalities
2293 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2298 | (n, _), (p, _), _ ->
2299 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2302 let l = List.map snd (fst ra) in
2303 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2305 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2306 (String.concat "\n" (List.map (string_of_equality ~env) active))
2307 (* (String.concat "\n"
2308 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2309 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2311 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) passive));
2315 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2319 let demodulate_tac ~dbd ~pattern ((proof,goal) as initialstatus) =
2320 let module I = Inference in
2321 let curi,metasenv,pbo,pty = proof in
2322 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2323 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2324 let lib_eq_uris, library_equalities, maxm =
2325 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2326 if library_equalities = [] then prerr_endline "VUOTA!!!";
2327 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2328 let library_equalities = List.map snd library_equalities in
2329 let goalterm = Cic.Meta (metano,irl) in
2330 let initgoal = Inference.BasicProof goalterm, [], ty in
2331 let env = (metasenv, context, CicUniv.empty_ugraph) in
2332 let equalities = simplify_equalities env (equalities@library_equalities) in
2335 (fun tbl eq -> Indexing.index tbl eq)
2336 Indexing.empty equalities
2338 let newmeta,(newproof,newmetasenv, newty) = Indexing.demodulation_goal
2339 maxm (metasenv,context,CicUniv.empty_ugraph) table initgoal
2341 if newmeta != maxm then
2343 let opengoal = Cic.Meta(maxm,irl) in
2345 Inference.build_proof_term ~noproof:opengoal newproof in
2346 let extended_metasenv = (maxm,context,newty)::metasenv in
2347 let extended_status =
2348 (curi,extended_metasenv,pbo,pty),goal in
2349 let (status,newgoals) =
2350 ProofEngineTypes.apply_tactic
2351 (PrimitiveTactics.apply_tac ~term:proofterm)
2353 (status,maxm::newgoals)
2355 else if newty = ty then
2356 raise (ProofEngineTypes.Fail (lazy "no progress"))
2357 else ProofEngineTypes.apply_tactic
2358 (ReductionTactics.simpl_tac ~pattern)
2362 let demodulate_tac ~dbd ~pattern =
2363 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)