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 let check_equation env equation msg =
34 let w, proof, (eq_ty, left, right, order), metas, args = equation in
35 let metasenv, context, ugraph = env in
36 let metasenv' = metasenv @ metas in
38 CicTypeChecker.type_of_aux' metasenv' context left ugraph;
39 CicTypeChecker.type_of_aux' metasenv' context right ugraph;
42 CicUtil.Meta_not_found _ as exn ->
45 prerr_endline (CicPp.ppterm left);
46 prerr_endline (CicPp.ppterm right);
51 (* set to false to disable paramodulation inside auto_tac *)
52 let connect_to_auto = true;;
55 (* profiling statistics... *)
56 let infer_time = ref 0.;;
57 let forward_simpl_time = ref 0.;;
58 let forward_simpl_new_time = ref 0.;;
59 let backward_simpl_time = ref 0.;;
60 let passive_maintainance_time = ref 0.;;
62 (* limited-resource-strategy related globals *)
63 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
64 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
65 let start_time = ref 0.;; (* time at which the execution started *)
66 let elapsed_time = ref 0.;;
67 (* let maximal_weight = ref None;; *)
68 let maximal_retained_equality = ref None;;
70 (* equality-selection related globals *)
71 let use_fullred = ref true;;
72 let weight_age_ratio = ref (* 5 *) 4;; (* settable by the user *)
73 let weight_age_counter = ref !weight_age_ratio;;
74 let symbols_ratio = ref (* 0 *) 3;;
75 let symbols_counter = ref 0;;
77 (* non-recursive Knuth-Bendix term ordering by default *)
78 (* Utils.compare_terms := Utils.rpo;; *)
79 (* Utils.compare_terms := Utils.nonrec_kbo;; *)
80 (* Utils.compare_terms := Utils.ao;; *)
83 let derived_clauses = ref 0;;
84 let kept_clauses = ref 0;;
86 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
89 (* varbiables controlling the search-space *)
90 let maxdepth = ref 3;;
91 let maxwidth = ref 3;;
95 | ParamodulationFailure
96 | ParamodulationSuccess of Inference.proof option * environment
99 type goal = proof * Cic.metasenv * Cic.term;;
101 type theorem = Cic.term * Cic.term * Cic.metasenv;;
103 let symbols_of_equality (_, _, (_, left, right, _), _, _) =
104 let m1 = symbols_of_term left in
109 let c = TermMap.find k res in
110 TermMap.add k (c+v) res
113 (symbols_of_term right) m1
118 module OrderedEquality = struct
119 type t = Inference.equality
121 let compare eq1 eq2 =
122 match meta_convertibility_eq eq1 eq2 with
125 let w1, _, (ty, left, right, _), _, a = eq1
126 and w2, _, (ty', left', right', _), _, a' = eq2 in
127 match Pervasives.compare w1 w2 with
129 let res = (List.length a) - (List.length a') in
130 if res <> 0 then res else (
132 let res = Pervasives.compare (List.hd a) (List.hd a') in
133 if res <> 0 then res else Pervasives.compare eq1 eq2
134 with Failure "hd" -> Pervasives.compare eq1 eq2
139 module EqualitySet = Set.Make(OrderedEquality);;
143 selects one equality from passive. The selection strategy is a combination
144 of weight, age and goal-similarity
146 let select env goals passive (active, _) =
147 processed_clauses := !processed_clauses + 1;
149 match (List.rev goals) with (_, goal::_)::_ -> goal | _ -> assert false
151 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
153 List.filter (fun e -> e <> eq) l
155 if !weight_age_ratio > 0 then
156 weight_age_counter := !weight_age_counter - 1;
157 match !weight_age_counter with
159 weight_age_counter := !weight_age_ratio;
160 match neg_list, pos_list with
162 (* Negatives aren't indexed, no need to remove them... *)
164 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
165 | [], (hd:EqualitySet.elt)::tl ->
167 Indexing.remove_index passive_table hd
170 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
171 | _, _ -> assert false
173 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) -> (
174 symbols_counter := !symbols_counter - 1;
175 let cardinality map =
176 TermMap.fold (fun k v res -> res + v) map 0
179 let _, _, term = goal in
182 let card = cardinality symbols in
183 let foldfun k v (r1, r2) =
184 if TermMap.mem k symbols then
185 let c = TermMap.find k symbols in
186 let c1 = abs (c - v) in
192 let f equality (i, e) =
194 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
196 let c = others + (abs (common - card)) in
197 if c < i then (c, equality)
200 let e1 = EqualitySet.min_elt pos_set in
203 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
205 (others + (abs (common - card))), e1
207 let _, current = EqualitySet.fold f pos_set initial in
209 Indexing.remove_index passive_table current
213 (remove current pos_list, EqualitySet.remove current pos_set),
217 symbols_counter := !symbols_ratio;
218 let set_selection set = EqualitySet.min_elt set in
219 if EqualitySet.is_empty neg_set then
220 let current = set_selection pos_set in
223 (remove current pos_list, EqualitySet.remove current pos_set),
224 Indexing.remove_index passive_table current
226 (Positive, current), passive
228 let current = set_selection neg_set in
230 (remove current neg_list, EqualitySet.remove current neg_set),
234 (Negative, current), passive
238 (* initializes the passive set of equalities *)
239 let make_passive neg pos =
240 let set_of equalities =
241 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
244 List.fold_left (fun tbl e -> Indexing.index tbl e) Indexing.empty pos
257 (* adds to passive a list of equalities: new_neg is a list of negative
258 equalities, new_pos a list of positive equalities *)
259 let add_to_passive passive (new_neg, new_pos) =
260 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
261 let ok set equality = not (EqualitySet.mem equality set) in
262 let neg = List.filter (ok neg_set) new_neg
263 and pos = List.filter (ok pos_set) new_pos in
265 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
267 let add set equalities =
268 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
270 (neg @ neg_list, add neg_set neg),
271 (pos_list @ pos, add pos_set pos),
276 let passive_is_empty = function
277 | ([], _), ([], _), _ -> true
282 let size_of_passive ((_, ns), (_, ps), _) =
283 (EqualitySet.cardinal ns) + (EqualitySet.cardinal ps)
287 let size_of_active (active_list, _) =
288 List.length active_list
292 (* removes from passive equalities that are estimated impossible to activate
293 within the current time limit *)
294 let prune_passive howmany (active, _) passive =
295 let (nl, ns), (pl, ps), tbl = passive in
296 let howmany = float_of_int howmany
297 and ratio = float_of_int !weight_age_ratio in
300 int_of_float (if t -. v < 0.5 then t else v)
302 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
303 and in_age = round (howmany /. (ratio +. 1.)) in
305 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
308 | (Negative, e)::_ ->
309 let symbols = symbols_of_equality e in
310 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
314 let counter = ref !symbols_ratio in
315 let rec pickw w ns ps =
317 if not (EqualitySet.is_empty ns) then
318 let e = EqualitySet.min_elt ns in
319 let ns', ps = pickw (w-1) (EqualitySet.remove e ns) ps in
320 EqualitySet.add e ns', ps
321 else if !counter > 0 then
323 counter := !counter - 1;
324 if !counter = 0 then counter := !symbols_ratio
328 let e = EqualitySet.min_elt ps in
329 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
330 ns, EqualitySet.add e ps'
332 let foldfun k v (r1, r2) =
333 if TermMap.mem k symbols then
334 let c = TermMap.find k symbols in
335 let c1 = abs (c - v) in
341 let f equality (i, e) =
343 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
345 let c = others + (abs (common - card)) in
346 if c < i then (c, equality)
349 let e1 = EqualitySet.min_elt ps in
352 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
354 (others + (abs (common - card))), e1
356 let _, e = EqualitySet.fold f ps initial in
357 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
358 ns, EqualitySet.add e ps'
360 let e = EqualitySet.min_elt ps in
361 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
362 ns, EqualitySet.add e ps'
364 EqualitySet.empty, EqualitySet.empty
366 let ns, ps = pickw in_weight ns ps in
367 let rec picka w s l =
371 | hd::tl when not (EqualitySet.mem hd s) ->
372 let w, s, l = picka (w-1) s tl in
373 w, EqualitySet.add hd s, hd::l
375 let w, s, l = picka w s tl in
380 let in_age, ns, nl = picka in_age ns nl in
381 let _, ps, pl = picka in_age ps pl in
382 if not (EqualitySet.is_empty ps) then
383 maximal_retained_equality := Some (EqualitySet.max_elt ps);
386 (fun e tbl -> Indexing.index tbl e) ps Indexing.empty
388 (nl, ns), (pl, ps), tbl
392 (** inference of new equalities between current and some in active *)
393 let infer env sign current (active_list, active_table) =
394 let new_neg, new_pos =
398 Indexing.superposition_left !maxmeta env active_table current in
403 Indexing.superposition_right !maxmeta env active_table current in
405 let rec infer_positive table = function
407 | (Negative, equality)::tl ->
409 Indexing.superposition_left !maxmeta env table equality in
411 let neg, pos = infer_positive table tl in
413 | (Positive, equality)::tl ->
415 Indexing.superposition_right !maxmeta env table equality in
417 let neg, pos = infer_positive table tl in
420 let curr_table = Indexing.index Indexing.empty current in
421 let neg, pos = infer_positive curr_table active_list in
424 derived_clauses := !derived_clauses + (List.length new_neg) +
425 (List.length new_pos);
426 match !maximal_retained_equality with
427 | None -> new_neg, new_pos
429 (* if we have a maximal_retained_equality, we can discard all equalities
430 "greater" than it, as they will never be reached... An equality is
431 greater than maximal_retained_equality if it is bigger
432 wrt. OrderedEquality.compare and it is less similar than
433 maximal_retained_equality to the current goal *)
435 match active_list with
436 | (Negative, e)::_ ->
437 let symbols = symbols_of_equality e in
438 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
445 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
448 if OrderedEquality.compare e eq <= 0 then
451 let foldfun k v (r1, r2) =
452 if TermMap.mem k symbols then
453 let c = TermMap.find k symbols in
454 let c1 = abs (c - v) in
462 TermMap.fold foldfun (symbols_of_equality eq) (0, 0) in
463 others + (abs (common - card))
466 TermMap.fold foldfun (symbols_of_equality e) (0, 0) in
467 let c = others + (abs (common - card)) in
468 if c < initial then true else false
470 List.filter filterfun new_pos
476 let contains_empty env (negative, positive) =
477 let metasenv, context, ugraph = env in
481 (fun (w, proof, (ty, left, right, ordering), m, a) ->
482 fst (CicReduction.are_convertible context left right ugraph))
491 (** simplifies current using active and passive *)
492 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
493 let pl, passive_table =
496 | Some ((pn, _), (pp, _), pt) ->
497 let pn = List.map (fun e -> (Negative, e)) pn
498 and pp = List.map (fun e -> (Positive, e)) pp in
501 let all = if pl = [] then active_list else active_list @ pl in
503 let demodulate table current =
504 let newmeta, newcurrent =
505 Indexing.demodulation_equality !maxmeta env table sign current in
507 if is_identity env newcurrent then
508 if sign = Negative then Some (sign, newcurrent)
512 (* (Printf.sprintf "\ncurrent was: %s\nnewcurrent is: %s\n" *)
513 (* (string_of_equality current) *)
514 (* (string_of_equality newcurrent))); *)
517 (* (Printf.sprintf "active is: %s" *)
518 (* (String.concat "\n" *)
519 (* (List.map (fun (_, e) -> (string_of_equality e)) active_list)))); *)
523 Some (sign, newcurrent)
526 let res = demodulate active_table current in
529 | Some (sign, newcurrent) ->
530 match passive_table with
532 | Some passive_table -> demodulate passive_table newcurrent
536 | Some (Negative, c) ->
539 (fun (s, eq) -> s = Negative && meta_convertibility_eq eq c)
542 if ok then res else None
543 | Some (Positive, c) ->
544 if Indexing.in_index active_table c then
547 match passive_table with
549 if fst (Indexing.subsumption env active_table c) then
553 | Some passive_table ->
554 if Indexing.in_index passive_table c then None
556 let r1, _ = Indexing.subsumption env active_table c in
558 let r2, _ = Indexing.subsumption env passive_table c in
559 if r2 then None else res
562 type fs_time_info_t = {
563 mutable build_all: float;
564 mutable demodulate: float;
565 mutable subsumption: float;
568 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
571 (** simplifies new using active and passive *)
572 let forward_simplify_new env (new_neg, new_pos) ?passive active =
573 let t1 = Unix.gettimeofday () in
575 let active_list, active_table = active in
576 let pl, passive_table =
579 | Some ((pn, _), (pp, _), pt) ->
580 let pn = List.map (fun e -> (Negative, e)) pn
581 and pp = List.map (fun e -> (Positive, e)) pp in
585 let t2 = Unix.gettimeofday () in
586 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
588 let demodulate sign table target =
589 let newmeta, newtarget =
590 Indexing.demodulation_equality !maxmeta env table sign target in
594 let t1 = Unix.gettimeofday () in
596 let new_neg, new_pos =
597 let new_neg = List.map (demodulate Negative active_table) new_neg
598 and new_pos = List.map (demodulate Positive active_table) new_pos in
602 match passive_table with
603 | None -> new_neg, new_pos
604 | Some passive_table ->
605 List.map (demodulate Negative passive_table) new_neg,
606 List.map (demodulate Positive passive_table) new_pos *)
609 let t2 = Unix.gettimeofday () in
610 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
615 if not (Inference.is_identity env e) then
616 if EqualitySet.mem e s then s
617 else EqualitySet.add e s
619 EqualitySet.empty new_pos
621 let new_pos = EqualitySet.elements new_pos_set in
624 match passive_table with
626 (fun e -> not (fst (Indexing.subsumption env active_table e)))
627 | Some passive_table ->
628 (fun e -> not ((fst (Indexing.subsumption env active_table e)) ||
629 (fst (Indexing.subsumption env passive_table e))))
631 (* let t1 = Unix.gettimeofday () in *)
632 (* let t2 = Unix.gettimeofday () in *)
633 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
635 match passive_table with
637 (fun e -> not (Indexing.in_index active_table e))
638 | Some passive_table ->
640 not ((Indexing.in_index active_table e) ||
641 (Indexing.in_index passive_table e)))
643 new_neg, List.filter subs (List.filter is_duplicate new_pos)
647 (** simplifies active usign new *)
648 let backward_simplify_active env new_pos new_table min_weight active =
649 let active_list, active_table = active in
650 let active_list, newa =
652 (fun (s, equality) (res, newn) ->
653 let ew, _, _, _, _ = equality in
654 if ew < min_weight then
655 (s, equality)::res, newn
657 match forward_simplify env (s, equality) (new_pos, new_table) with
667 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
671 (fun (s, eq) (res, tbl) ->
672 if List.mem (s, eq) res then
674 else if (is_identity env eq) || (find eq res) then (
678 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
679 active_list ([], Indexing.empty),
681 (fun (s, eq) (n, p) ->
682 if (s <> Negative) && (is_identity env eq) then (
685 if s = Negative then eq::n, p
690 | [], [] -> active, None
691 | _ -> active, Some newa
695 (** simplifies passive using new *)
696 let backward_simplify_passive env new_pos new_table min_weight passive =
697 let (nl, ns), (pl, ps), passive_table = passive in
698 let f sign equality (resl, ress, newn) =
699 let ew, _, _, _, _ = equality in
700 if ew < min_weight then
701 equality::resl, ress, newn
703 match forward_simplify env (sign, equality) (new_pos, new_table) with
704 | None -> resl, EqualitySet.remove equality ress, newn
707 equality::resl, ress, newn
709 let ress = EqualitySet.remove equality ress in
712 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
713 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
716 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
718 match newn, newp with
719 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
720 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
724 let backward_simplify env new' ?passive active =
725 let new_pos, new_table, min_weight =
728 let ew, _, _, _, _ = e in
729 (Positive, e)::l, Indexing.index t e, min ew w)
730 ([], Indexing.empty, 1000000) (snd new')
733 backward_simplify_active env new_pos new_table min_weight active in
736 active, (make_passive [] []), newa, None
739 backward_simplify_passive env new_pos new_table min_weight passive in
740 active, passive, newa, newp
744 (* returns an estimation of how many equalities in passive can be activated
745 within the current time limit *)
746 let get_selection_estimate () =
747 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
748 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
750 ceil ((float_of_int !processed_clauses) *.
751 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
755 (** initializes the set of goals *)
756 let make_goals goal =
758 and passive = [0, [goal]] in
763 (** initializes the set of theorems *)
764 let make_theorems theorems =
769 let activate_goal (active, passive) =
771 | goal_conj::tl -> true, (goal_conj::active, tl)
772 | [] -> false, (active, passive)
776 let activate_theorem (active, passive) =
778 | theorem::tl -> true, (theorem::active, tl)
779 | [] -> false, (active, passive)
783 (** simplifies a goal with equalities in active and passive *)
784 let simplify_goal env goal ?passive (active_list, active_table) =
785 let pl, passive_table =
788 | Some ((pn, _), (pp, _), pt) ->
789 let pn = List.map (fun e -> (Negative, e)) pn
790 and pp = List.map (fun e -> (Positive, e)) pp in
794 let demodulate table goal =
795 let newmeta, newgoal =
796 Indexing.demodulation_goal !maxmeta env table goal in
798 goal != newgoal, newgoal
801 match passive_table with
802 | None -> demodulate active_table goal
803 | Some passive_table ->
804 let changed, goal = demodulate active_table goal in
805 let changed', goal = demodulate passive_table goal in
806 (changed || changed'), goal
812 let simplify_goals env goals ?passive active =
813 let a_goals, p_goals = goals in
818 List.map (fun g -> snd (simplify_goal env g ?passive active)) gl in
824 (fun (a, p) (d, gl) ->
825 let changed = ref false in
829 let c, g = simplify_goal env g ?passive active in
830 changed := !changed || c; g) gl in
831 if !changed then (a, (d, gl)::p) else ((d, gl)::a, p))
832 ([], p_goals) a_goals
838 let simplify_theorems env theorems ?passive (active_list, active_table) =
839 let pl, passive_table =
842 | Some ((pn, _), (pp, _), pt) ->
843 let pn = List.map (fun e -> (Negative, e)) pn
844 and pp = List.map (fun e -> (Positive, e)) pp in
847 let a_theorems, p_theorems = theorems in
848 let demodulate table theorem =
849 let newmeta, newthm =
850 Indexing.demodulation_theorem !maxmeta env table theorem in
852 theorem != newthm, newthm
854 let foldfun table (a, p) theorem =
855 let changed, theorem = demodulate table theorem in
856 if changed then (a, theorem::p) else (theorem::a, p)
858 let mapfun table theorem = snd (demodulate table theorem) in
859 match passive_table with
861 let p_theorems = List.map (mapfun active_table) p_theorems in
862 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
863 | Some passive_table ->
864 let p_theorems = List.map (mapfun active_table) p_theorems in
865 let p_theorems, a_theorems =
866 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
867 let p_theorems = List.map (mapfun passive_table) p_theorems in
868 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
872 let rec simpl env e others others_simpl =
873 let active = others @ others_simpl in
876 (fun t (_, e) -> Indexing.index t e)
877 Indexing.empty active
879 let res = forward_simplify env e (active, tbl) in
883 | None -> simpl env hd tl others_simpl
884 | Some e -> simpl env hd tl (e::others_simpl)
888 | None -> others_simpl
889 | Some e -> e::others_simpl
893 let simplify_equalities env equalities =
896 (Printf.sprintf "equalities:\n%s\n"
898 (List.map string_of_equality equalities))));
899 debug_print (lazy "SIMPLYFYING EQUALITIES...");
900 match equalities with
903 let others = List.map (fun e -> (Positive, e)) tl in
905 List.rev (List.map snd (simpl env (Positive, hd) others []))
909 (Printf.sprintf "equalities AFTER:\n%s\n"
911 (List.map string_of_equality res))));
915 (* applies equality to goal to see if the goal can be closed *)
916 let apply_equality_to_goal env equality goal =
917 let module C = Cic in
918 let module HL = HelmLibraryObjects in
919 let module I = Inference in
920 let metasenv, context, ugraph = env in
921 let _, proof, (ty, left, right, _), metas, args = equality in
923 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
924 let gproof, gmetas, gterm = goal in
927 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
928 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
930 let subst, metasenv', _ =
931 let menv = metasenv @ metas @ gmetas in
932 Inference.unification menv context eqterm gterm ugraph
936 | I.BasicProof t -> I.BasicProof (CicMetaSubst.apply_subst subst t)
937 | I.ProofBlock (s, uri, nt, t, pe, p) ->
938 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
942 let rec repl = function
943 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
944 | I.NoProof -> newproof
945 | I.BasicProof p -> newproof
946 | I.SubProof (t, i, p) -> I.SubProof (t, i, repl p)
951 true, subst, newgproof
952 with CicUnification.UnificationFailure _ ->
958 let new_meta metasenv =
959 let m = CicMkImplicit.new_meta metasenv [] in
961 while !maxmeta <= m do incr maxmeta done;
966 (* applies a theorem or an equality to goal, returning a list of subgoals or
967 an indication of failure *)
968 let apply_to_goal env theorems ?passive active goal =
969 let metasenv, context, ugraph = env in
970 let proof, metas, term = goal in
973 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
974 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
977 CicMkImplicit.identity_relocation_list_for_metavariable context in
978 let proof', newmeta =
979 let rec get_meta = function
980 | SubProof (t, i, p) ->
981 let t', i' = get_meta p in
982 if i' = -1 then t, i else t', i'
983 | ProofGoalBlock (_, p) -> get_meta p
984 | _ -> Cic.Implicit None, -1
986 let p, m = get_meta proof in
988 let n = new_meta (metasenv @ metas) in
993 let metasenv = (newmeta, context, term)::metasenv @ metas in
994 let bit = new_meta metasenv, context, term in
995 let metasenv' = bit::metasenv in
996 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
998 let rec aux = function
1000 | (theorem, thmty, _)::tl ->
1002 let subst, (newproof, newgoals) =
1003 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
1005 if newgoals = [] then
1006 let _, _, p, _ = newproof in
1008 let rec repl = function
1009 | Inference.ProofGoalBlock (_, gp) ->
1010 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
1011 | Inference.NoProof -> Inference.BasicProof p
1012 | Inference.BasicProof _ -> Inference.BasicProof p
1013 | Inference.SubProof (t, i, p2) ->
1014 Inference.SubProof (t, i, repl p2)
1019 let _, m = status in
1020 let subst = List.filter (fun (i, _) -> i = m) subst in
1021 `Ok (subst, [newp, metas, term])
1023 let _, menv, p, _ = newproof in
1025 CicMkImplicit.identity_relocation_list_for_metavariable context
1030 let _, _, ty = CicUtil.lookup_meta i menv in
1032 let rec gp = function
1033 | SubProof (t, i, p) ->
1034 SubProof (t, i, gp p)
1035 | ProofGoalBlock (sp1, sp2) ->
1036 ProofGoalBlock (sp1, gp sp2)
1039 SubProof (p, i, BasicProof (Cic.Meta (i, irl)))
1040 | ProofSymBlock (s, sp) ->
1041 ProofSymBlock (s, gp sp)
1042 | ProofBlock (s, u, nt, t, pe, sp) ->
1043 ProofBlock (s, u, nt, t, pe, gp sp)
1051 let w, m = weight_of_term t in
1052 w + 2 * (List.length m)
1055 (fun (_, _, t1) (_, _, t2) ->
1056 Pervasives.compare (weight t1) (weight t2))
1059 let best = aux tl in
1061 | `Ok (_, _) -> best
1062 | `No -> `GoOn ([subst, goals])
1063 | `GoOn sl -> `GoOn ((subst, goals)::sl)
1064 with ProofEngineTypes.Fail msg ->
1068 if Inference.term_is_equality term then
1069 let rec appleq_a = function
1070 | [] -> false, [], []
1071 | (Positive, equality)::tl ->
1072 let ok, s, newproof = apply_equality_to_goal env equality goal in
1073 if ok then true, s, [newproof, metas, term] else appleq_a tl
1074 | _::tl -> appleq_a tl
1076 let rec appleq_p = function
1077 | [] -> false, [], []
1079 let ok, s, newproof = apply_equality_to_goal env equality goal in
1080 if ok then true, s, [newproof, metas, term] else appleq_p tl
1082 let al, _ = active in
1084 | None -> appleq_a al
1085 | Some (_, (pl, _), _) ->
1086 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1090 if r = true then `Ok (s, l) else aux theorems
1094 (* sorts a conjunction of goals in order to detect earlier if it is
1095 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1096 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1099 (fun (_, e1, g1) (_, e2, g2) ->
1101 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1103 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1107 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1112 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1116 if prop1 = 0 && prop2 = 0 then
1117 let e1 = if Inference.term_is_equality g1 then 0 else 1
1118 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1128 let is_meta_closed goals =
1129 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1133 (* applies a series of theorems/equalities to a conjunction of goals *)
1134 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1135 let aux (goal, r) tl =
1136 let propagate_subst subst (proof, metas, term) =
1137 let rec repl = function
1138 | NoProof -> NoProof
1140 BasicProof (CicMetaSubst.apply_subst subst t)
1141 | ProofGoalBlock (p, pb) ->
1142 let pb' = repl pb in
1143 ProofGoalBlock (p, pb')
1144 | SubProof (t, i, p) ->
1145 let t' = CicMetaSubst.apply_subst subst t in
1148 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1149 | ProofBlock (s, u, nty, t, pe, p) ->
1150 ProofBlock (subst @ s, u, nty, t, pe, p)
1151 in (repl proof, metas, term)
1153 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1155 | `No -> `No (depth, goals)
1160 let tl = List.map (propagate_subst s) tl in
1161 sort_goal_conj env (depth+1, gl @ tl)) sl
1164 | `Ok (subst, gl) ->
1168 let p, _, _ = List.hd gl in
1170 let rec repl = function
1171 | SubProof (_, _, p) -> repl p
1172 | ProofGoalBlock (p1, p2) ->
1173 ProofGoalBlock (repl p1, repl p2)
1176 build_proof_term (repl p)
1179 let rec get_meta = function
1180 | SubProof (_, i, p) ->
1181 let i' = get_meta p in
1182 if i' = -1 then i else i'
1183 (* max i (get_meta p) *)
1184 | ProofGoalBlock (_, p) -> get_meta p
1190 let _, (context, _, _) = List.hd subst in
1191 [i, (context, subproof, Cic.Implicit None)]
1193 let tl = List.map (propagate_subst subst) tl in
1194 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1198 if depth > !maxdepth || (List.length goals) > !maxwidth then
1201 let rec search_best res = function
1204 let r = apply_to_goal env theorems ?passive active goal in
1206 | `Ok _ -> (goal, r)
1207 | `No -> search_best res tl
1211 | _, `Ok _ -> assert false
1214 if (List.length l) < (List.length l2) then goal, r else res
1216 search_best newres tl
1218 let hd = List.hd goals in
1219 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1223 | _, _ -> search_best res (List.tl goals)
1225 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1227 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1228 (List.length (snd conj)) < (List.length goals)->
1229 apply_to_goal_conj env theorems ?passive active conj
1235 module OrderedGoals = struct
1236 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1243 else let r = (List.length l1) - (List.length l2) in
1249 (fun (_, _, t1) (_, _, t2) ->
1250 let r = Pervasives.compare t1 t2 in
1259 module GoalsSet = Set.Make(OrderedGoals);;
1262 exception SearchSpaceOver;;
1267 let apply_to_goals env is_passive_empty theorems active goals =
1268 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1269 let add_to set goals =
1270 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1272 let rec aux set = function
1274 debug_print (lazy "HERE!!!");
1275 if is_passive_empty then raise SearchSpaceOver else false, set
1277 let res = apply_to_goal_conj env theorems active goals in
1283 | (d, (p, _, t)::_) -> d, p, t
1288 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1289 d (string_of_proof p) (CicPp.ppterm t)))
1291 true, GoalsSet.singleton newgoals
1293 let set' = add_to set (goals::tl) in
1294 let set' = add_to set' newgoals in
1299 let n = List.length goals in
1300 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1301 let goals = GoalsSet.elements goals in
1302 debug_print (lazy "\n\tapply_to_goals end\n");
1303 let m = List.length goals in
1304 if m = n && is_passive_empty then
1305 raise SearchSpaceOver
1312 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1313 work that well yet...) *)
1314 let sort_passive_goals goals =
1316 (fun (d1, l1) (d2, l2) ->
1318 and r2 = (List.length l1) - (List.length l2) in
1319 let foldfun ht (_, _, t) =
1320 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1323 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1324 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1325 in let r3 = m1 - m2 in
1327 else if r2 <> 0 then r2
1329 (* let _, _, g1 = List.hd l1 *)
1330 (* and _, _, g2 = List.hd l2 in *)
1331 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1332 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1333 (* in let r4 = e1 - e2 in *)
1334 (* if r4 <> 0 then r3 else r1) *)
1339 let print_goals goals =
1346 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1348 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1352 (* tries to prove the first conjunction in goals with applications of
1353 theorems/equalities, returning new sub-goals or an indication of success *)
1354 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1355 let theorems, _ = theorems in
1356 let a_goals, p_goals = goals in
1357 let goal = List.hd a_goals in
1358 let not_in_active gl =
1362 if (List.length gl) = (List.length gl') then
1363 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1369 let res = apply_to_goal_conj env theorems ?passive active goal in
1372 true, ([newgoals], [])
1374 false, (a_goals, p_goals)
1379 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1382 let p_goals = newgoals @ p_goals in
1383 let p_goals = sort_passive_goals p_goals in
1384 false, (a_goals, p_goals)
1390 let apply_theorem_to_goals env theorems active goals =
1391 let a_goals, p_goals = goals in
1392 let theorem = List.hd (fst theorems) in
1393 let theorems = [theorem] in
1394 let rec aux p = function
1395 | [] -> false, ([], p)
1397 let res = apply_to_goal_conj env theorems active goal in
1399 | `Ok newgoals -> true, ([newgoals], [])
1401 | `GoOn newgoals -> aux (newgoals @ p) tl
1403 let ok, (a, p) = aux p_goals a_goals in
1409 (fun (d1, l1) (d2, l2) ->
1412 else let r = (List.length l1) - (List.length l2) in
1418 (fun (_, _, t1) (_, _, t2) ->
1419 let r = Pervasives.compare t1 t2 in
1420 if r <> 0 then (res := r; true) else false) l1 l2
1424 ok, (a_goals, p_goals)
1428 (* given-clause algorithm with lazy reduction strategy *)
1429 let rec given_clause dbd env goals theorems passive active =
1430 let goals = simplify_goals env goals active in
1431 let ok, goals = activate_goal goals in
1432 (* let theorems = simplify_theorems env theorems active in *)
1434 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1437 match (fst goals) with
1438 | (_, [proof, _, _])::_ -> Some proof
1441 ParamodulationSuccess (proof, env)
1443 given_clause_aux dbd env goals theorems passive active
1445 (* let ok', theorems = activate_theorem theorems in *)
1446 let ok', theorems = false, theorems in
1448 let ok, goals = apply_theorem_to_goals env theorems active goals in
1451 match (fst goals) with
1452 | (_, [proof, _, _])::_ -> Some proof
1455 ParamodulationSuccess (proof, env)
1457 given_clause_aux dbd env goals theorems passive active
1459 if (passive_is_empty passive) then ParamodulationFailure
1460 else given_clause_aux dbd env goals theorems passive active
1462 and given_clause_aux dbd env goals theorems passive active =
1463 let time1 = Unix.gettimeofday () in
1465 let selection_estimate = get_selection_estimate () in
1466 let kept = size_of_passive passive in
1468 if !time_limit = 0. || !processed_clauses = 0 then
1470 else if !elapsed_time > !time_limit then (
1471 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1472 !time_limit !elapsed_time));
1474 ) else if kept > selection_estimate then (
1476 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1477 "(kept: %d, selection_estimate: %d)\n")
1478 kept selection_estimate));
1479 prune_passive selection_estimate active passive
1484 let time2 = Unix.gettimeofday () in
1485 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1487 kept_clauses := (size_of_passive passive) + (size_of_active active);
1488 match passive_is_empty passive with
1489 | true -> (* ParamodulationFailure *)
1490 given_clause dbd env goals theorems passive active
1492 let (sign, current), passive = select env (fst goals) passive active in
1493 let time1 = Unix.gettimeofday () in
1494 let res = forward_simplify env (sign, current) ~passive active in
1495 let time2 = Unix.gettimeofday () in
1496 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1499 given_clause dbd env goals theorems passive active
1500 | Some (sign, current) ->
1501 if (sign = Negative) && (is_identity env current) then (
1503 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1504 (string_of_equality ~env current)));
1505 let _, proof, _, _, _ = current in
1506 ParamodulationSuccess (Some proof, env)
1509 (lazy "\n================================================");
1510 debug_print (lazy (Printf.sprintf "selected: %s %s"
1511 (string_of_sign sign)
1512 (string_of_equality ~env current)));
1514 let t1 = Unix.gettimeofday () in
1515 let new' = infer env sign current active in
1516 let t2 = Unix.gettimeofday () in
1517 infer_time := !infer_time +. (t2 -. t1);
1519 let res, goal' = contains_empty env new' in
1523 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1526 ParamodulationSuccess (proof, env)
1528 let t1 = Unix.gettimeofday () in
1529 let new' = forward_simplify_new env new' active in
1530 let t2 = Unix.gettimeofday () in
1532 forward_simpl_new_time :=
1533 !forward_simpl_new_time +. (t2 -. t1)
1537 | Negative -> active
1539 let t1 = Unix.gettimeofday () in
1540 let active, _, newa, _ =
1541 backward_simplify env ([], [current]) active
1543 let t2 = Unix.gettimeofday () in
1544 backward_simpl_time :=
1545 !backward_simpl_time +. (t2 -. t1);
1549 let al, tbl = active in
1550 let nn = List.map (fun e -> Negative, e) n in
1555 Indexing.index tbl e)
1560 match contains_empty env new' with
1563 let al, tbl = active in
1565 | Negative -> (sign, current)::al, tbl
1567 al @ [(sign, current)], Indexing.index tbl current
1569 let passive = add_to_passive passive new' in
1570 given_clause dbd env goals theorems passive active
1575 let _, proof, _, _, _ = goal in Some proof
1578 ParamodulationSuccess (proof, env)
1583 (** given-clause algorithm with full reduction strategy *)
1584 let rec given_clause_fullred dbd env goals theorems passive active =
1585 let goals = simplify_goals env goals ~passive active in
1586 let ok, goals = activate_goal goals in
1587 (* let theorems = simplify_theorems env theorems ~passive active in *)
1592 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1593 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1594 (* let current = List.hd (fst goals) in *)
1595 (* let p, _, t = List.hd (snd current) in *)
1598 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1599 (* (CicPp.ppterm t) (string_of_proof p))); *)
1602 apply_goal_to_theorems dbd env theorems ~passive active goals
1606 match (fst goals) with
1607 | (_, [proof, _, _])::_ -> Some proof
1610 ParamodulationSuccess (proof, env)
1612 given_clause_fullred_aux dbd env goals theorems passive active
1614 (* let ok', theorems = activate_theorem theorems in *)
1616 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1619 (* match (fst goals) with *)
1620 (* | (_, [proof, _, _])::_ -> Some proof *)
1621 (* | _ -> assert false *)
1623 (* ParamodulationSuccess (proof, env) *)
1625 (* given_clause_fullred_aux env goals theorems passive active *)
1627 if (passive_is_empty passive) then ParamodulationFailure
1628 else given_clause_fullred_aux dbd env goals theorems passive active
1630 and given_clause_fullred_aux dbd env goals theorems passive active =
1631 let time1 = Unix.gettimeofday () in
1633 let selection_estimate = get_selection_estimate () in
1634 let kept = size_of_passive passive in
1636 if !time_limit = 0. || !processed_clauses = 0 then
1638 else if !elapsed_time > !time_limit then (
1639 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1640 !time_limit !elapsed_time));
1642 ) else if kept > selection_estimate then (
1644 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1645 "(kept: %d, selection_estimate: %d)\n")
1646 kept selection_estimate));
1647 prune_passive selection_estimate active passive
1652 let time2 = Unix.gettimeofday () in
1653 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1655 kept_clauses := (size_of_passive passive) + (size_of_active active);
1656 match passive_is_empty passive with
1657 | true -> (* ParamodulationFailure *)
1658 given_clause_fullred dbd env goals theorems passive active
1660 let (sign, current), passive = select env (fst goals) passive active in
1661 let time1 = Unix.gettimeofday () in
1662 let res = forward_simplify env (sign, current) ~passive active in
1663 let time2 = Unix.gettimeofday () in
1664 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1667 given_clause_fullred dbd env goals theorems passive active
1668 | Some (sign, current) ->
1669 if (sign = Negative) && (is_identity env current) then (
1671 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1672 (string_of_equality ~env current)));
1673 let _, proof, _, _, _ = current in
1674 ParamodulationSuccess (Some proof, env)
1677 (lazy "\n================================================");
1678 debug_print (lazy (Printf.sprintf "selected: %s %s"
1679 (string_of_sign sign)
1680 (string_of_equality ~env current)));
1682 let t1 = Unix.gettimeofday () in
1683 let new' = infer env sign current active in
1684 let t2 = Unix.gettimeofday () in
1685 infer_time := !infer_time +. (t2 -. t1);
1688 if is_identity env current then active
1690 let al, tbl = active in
1692 | Negative -> (sign, current)::al, tbl
1694 al @ [(sign, current)], Indexing.index tbl current
1696 let rec simplify new' active passive =
1697 let t1 = Unix.gettimeofday () in
1698 let new' = forward_simplify_new env new' ~passive active in
1699 let t2 = Unix.gettimeofday () in
1700 forward_simpl_new_time :=
1701 !forward_simpl_new_time +. (t2 -. t1);
1702 let t1 = Unix.gettimeofday () in
1703 let active, passive, newa, retained =
1704 backward_simplify env new' ~passive active in
1705 let t2 = Unix.gettimeofday () in
1706 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1707 match newa, retained with
1708 | None, None -> active, passive, new'
1710 | None, Some (n, p) ->
1711 let nn, np = new' in
1712 simplify (nn @ n, np @ p) active passive
1713 | Some (n, p), Some (rn, rp) ->
1714 let nn, np = new' in
1715 simplify (nn @ n @ rn, np @ p @ rp) active passive
1717 let active, passive, new' = simplify new' active passive in
1719 let k = size_of_passive passive in
1720 if k < (kept - 1) then
1721 processed_clauses := !processed_clauses + (kept - 1 - k);
1726 (Printf.sprintf "active:\n%s\n"
1729 (fun (s, e) -> (string_of_sign s) ^ " " ^
1730 (string_of_equality ~env e))
1738 (Printf.sprintf "new':\n%s\n"
1741 (fun e -> "Negative " ^
1742 (string_of_equality ~env e)) neg) @
1744 (fun e -> "Positive " ^
1745 (string_of_equality ~env e)) pos)))))
1747 match contains_empty env new' with
1749 let passive = add_to_passive passive new' in
1750 given_clause_fullred dbd env goals theorems passive active
1754 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1757 ParamodulationSuccess (proof, env)
1762 let rec saturate_equations env goal accept_fun passive active =
1763 elapsed_time := Unix.gettimeofday () -. !start_time;
1764 if !elapsed_time > !time_limit then
1767 let (sign, current), passive = select env [1, [goal]] passive active in
1768 let res = forward_simplify env (sign, current) ~passive active in
1771 saturate_equations env goal accept_fun passive active
1772 | Some (sign, current) ->
1773 assert (sign = Positive);
1775 (lazy "\n================================================");
1776 debug_print (lazy (Printf.sprintf "selected: %s %s"
1777 (string_of_sign sign)
1778 (string_of_equality ~env current)));
1779 let new' = infer env sign current active in
1781 if is_identity env current then active
1783 let al, tbl = active in
1784 al @ [(sign, current)], Indexing.index tbl current
1786 let rec simplify new' active passive =
1787 let new' = forward_simplify_new env new' ~passive active in
1788 let active, passive, newa, retained =
1789 backward_simplify env new' ~passive active in
1790 match newa, retained with
1791 | None, None -> active, passive, new'
1793 | None, Some (n, p) ->
1794 let nn, np = new' in
1795 simplify (nn @ n, np @ p) active passive
1796 | Some (n, p), Some (rn, rp) ->
1797 let nn, np = new' in
1798 simplify (nn @ n @ rn, np @ p @ rp) active passive
1800 let active, passive, new' = simplify new' active passive in
1804 (Printf.sprintf "active:\n%s\n"
1807 (fun (s, e) -> (string_of_sign s) ^ " " ^
1808 (string_of_equality ~env e))
1816 (Printf.sprintf "new':\n%s\n"
1819 (fun e -> "Negative " ^
1820 (string_of_equality ~env e)) neg) @
1822 (fun e -> "Positive " ^
1823 (string_of_equality ~env e)) pos)))))
1825 let new' = match new' with _, pos -> [], List.filter accept_fun pos in
1826 let passive = add_to_passive passive new' in
1827 saturate_equations env goal accept_fun passive active
1833 let main dbd full term metasenv ugraph =
1834 let module C = Cic in
1835 let module T = CicTypeChecker in
1836 let module PET = ProofEngineTypes in
1837 let module PP = CicPp in
1838 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1839 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1840 let proof, goals = status in
1841 let goal' = List.nth goals 0 in
1842 let _, metasenv, meta_proof, _ = proof in
1843 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1844 let eq_indexes, equalities, maxm = find_equalities context proof in
1845 let lib_eq_uris, library_equalities, maxm =
1847 find_library_equalities dbd context (proof, goal') (maxm+2)
1849 let library_equalities = List.map snd library_equalities in
1850 maxmeta := maxm+2; (* TODO ugly!! *)
1851 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1852 let new_meta_goal, metasenv, type_of_goal =
1853 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1856 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1857 Cic.Meta (maxm+1, irl),
1858 (maxm+1, context, ty)::metasenv,
1861 let env = (metasenv, context, ugraph) in
1862 let t1 = Unix.gettimeofday () in
1865 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1866 let context_hyp = find_context_hypotheses env eq_indexes in
1867 context_hyp @ theorems, []
1870 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1871 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1873 let t = CicUtil.term_of_uri refl_equal in
1874 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1877 let t2 = Unix.gettimeofday () in
1880 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1885 "Theorems:\n-------------------------------------\n%s\n"
1890 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1894 let goal = Inference.BasicProof new_meta_goal, [], goal in
1895 let equalities = simplify_equalities env (equalities@library_equalities) in
1896 let active = make_active () in
1897 let passive = make_passive [] equalities in
1898 Printf.printf "\ncurrent goal: %s\n"
1899 (let _, _, g = goal in CicPp.ppterm g);
1900 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1901 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1902 Printf.printf "\nequalities:\n%s\n"
1905 (string_of_equality ~env) equalities));
1906 (* (equalities @ library_equalities))); *)
1907 print_endline "--------------------------------------------------";
1908 let start = Unix.gettimeofday () in
1909 print_endline "GO!";
1910 start_time := Unix.gettimeofday ();
1912 let goals = make_goals goal in
1913 (if !use_fullred then given_clause_fullred else given_clause)
1914 dbd env goals theorems passive active
1916 let finish = Unix.gettimeofday () in
1919 | ParamodulationFailure ->
1920 Printf.printf "NO proof found! :-(\n\n"
1921 | ParamodulationSuccess (Some proof, env) ->
1922 let proof = Inference.build_proof_term proof in
1923 Printf.printf "OK, found a proof!\n";
1924 (* REMEMBER: we have to instantiate meta_proof, we should use
1925 apply the "apply" tactic to proof and status
1927 let names = names_of_context context in
1928 print_endline (PP.pp proof names);
1931 (fun m (_, _, _, menv, _) -> m @ menv) metasenv equalities
1936 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1938 print_endline (string_of_float (finish -. start));
1940 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1941 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1943 (fst (CicReduction.are_convertible
1944 context type_of_goal ty ug)));
1946 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1947 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1948 print_endline (string_of_float (finish -. start));*)
1952 | ParamodulationSuccess (None, env) ->
1953 Printf.printf "Success, but no proof?!?\n\n"
1955 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1956 "forward_simpl_new_time: %.9f\n" ^^
1957 "backward_simpl_time: %.9f\n")
1958 !infer_time !forward_simpl_time !forward_simpl_new_time
1959 !backward_simpl_time;
1960 Printf.printf "passive_maintainance_time: %.9f\n"
1961 !passive_maintainance_time;
1962 Printf.printf " successful unification/matching time: %.9f\n"
1963 !Indexing.match_unif_time_ok;
1964 Printf.printf " failed unification/matching time: %.9f\n"
1965 !Indexing.match_unif_time_no;
1966 Printf.printf " indexing retrieval time: %.9f\n"
1967 !Indexing.indexing_retrieval_time;
1968 Printf.printf " demodulate_term.build_newtarget_time: %.9f\n"
1969 !Indexing.build_newtarget_time;
1970 Printf.printf "derived %d clauses, kept %d clauses.\n"
1971 !derived_clauses !kept_clauses;
1974 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1980 let default_depth = !maxdepth
1981 and default_width = !maxwidth;;
1985 symbols_counter := 0;
1986 weight_age_counter := !weight_age_ratio;
1987 processed_clauses := 0;
1990 maximal_retained_equality := None;
1992 forward_simpl_time := 0.;
1993 forward_simpl_new_time := 0.;
1994 backward_simpl_time := 0.;
1995 passive_maintainance_time := 0.;
1996 derived_clauses := 0;
2001 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
2002 let module C = Cic in
2004 Indexing.init_index ();
2007 let proof, goal = status in
2009 let uri, metasenv, meta_proof, term_to_prove = proof in
2010 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2011 let eq_indexes, equalities, maxm = find_equalities context proof in
2012 let new_meta_goal, metasenv, type_of_goal =
2014 CicMkImplicit.identity_relocation_list_for_metavariable context in
2015 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2017 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2018 Cic.Meta (maxm+1, irl),
2019 (maxm+1, context, ty)::metasenv,
2022 let ugraph = CicUniv.empty_ugraph in
2023 let env = (metasenv, context, ugraph) in
2024 let goal = Inference.BasicProof new_meta_goal, [], goal in
2026 let t1 = Unix.gettimeofday () in
2027 let lib_eq_uris, library_equalities, maxm =
2028 find_library_equalities dbd context (proof, goal') (maxm+2)
2030 let library_equalities = List.map snd library_equalities in
2031 let t2 = Unix.gettimeofday () in
2033 let equalities = simplify_equalities env (equalities@library_equalities) in
2036 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
2037 let t1 = Unix.gettimeofday () in
2040 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2041 let context_hyp = find_context_hypotheses env eq_indexes in
2042 context_hyp @ thms, []
2045 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2046 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2048 let t = CicUtil.term_of_uri refl_equal in
2049 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2052 let t2 = Unix.gettimeofday () in
2057 "Theorems:\n-------------------------------------\n%s\n"
2062 "Term: %s, type: %s"
2063 (CicPp.ppterm t) (CicPp.ppterm ty))
2067 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2069 let active = make_active () in
2070 let passive = make_passive [] equalities in
2071 let start = Unix.gettimeofday () in
2073 let goals = make_goals goal in
2074 given_clause_fullred dbd env goals theorems passive active
2076 let finish = Unix.gettimeofday () in
2077 (res, finish -. start)
2080 | ParamodulationSuccess (Some proof, env) ->
2081 debug_print (lazy "OK, found a proof!");
2082 let proof = Inference.build_proof_term proof in
2083 let names = names_of_context context in
2086 match new_meta_goal with
2087 | C.Meta (i, _) -> i | _ -> assert false
2089 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2094 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2096 debug_print (lazy (CicPp.pp proof [](* names *)));
2100 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2101 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2103 (fst (CicReduction.are_convertible
2104 context type_of_goal ty ug)))));
2105 let equality_for_replace i t1 =
2107 | C.Meta (n, _) -> n = i
2111 ProofEngineReduction.replace
2112 ~equality:equality_for_replace
2113 ~what:[goal'] ~with_what:[proof]
2118 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2119 (match uri with Some uri -> UriManager.string_of_uri uri
2121 (print_metasenv newmetasenv)
2122 (CicPp.pp real_proof [](* names *))
2123 (CicPp.pp term_to_prove names)));
2124 ((uri, newmetasenv, real_proof, term_to_prove), [])
2125 with CicTypeChecker.TypeCheckerFailure _ ->
2126 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2127 debug_print (lazy (CicPp.pp proof names));
2128 raise (ProofEngineTypes.Fail
2129 (lazy "Found a proof, but it doesn't typecheck"))
2131 let tall = fs_time_info.build_all in
2132 let tdemodulate = fs_time_info.demodulate in
2133 let tsubsumption = fs_time_info.subsumption in
2134 debug_print (lazy (Printf.sprintf "\nTIME NEEDED: %.9f" time));
2135 debug_print (lazy (Printf.sprintf "\ntall: %.9f" tall));
2136 debug_print (lazy (Printf.sprintf "\ntdemod: %.9f" tdemodulate));
2137 debug_print (lazy (Printf.sprintf "\ntsubsumption: %.9f" tsubsumption));
2138 debug_print (lazy (Printf.sprintf "\ninfer_time: %.9f" !infer_time));
2139 debug_print (lazy (Printf.sprintf "\nforward_simpl_times: %.9f" !forward_simpl_time));
2140 debug_print (lazy (Printf.sprintf "\nforward_simpl_new_times: %.9f" !forward_simpl_new_time));
2141 debug_print (lazy (Printf.sprintf "\nbackward_simpl_times: %.9f" !backward_simpl_time));
2142 debug_print (lazy (Printf.sprintf "\npassive_maintainance_time: %.9f" !passive_maintainance_time));
2145 raise (ProofEngineTypes.Fail (lazy "NO proof found"))
2148 (* dummy function called within matita to trigger linkage *)
2152 let retrieve_and_print dbd term metasenv ugraph =
2153 let module C = Cic in
2154 let module T = CicTypeChecker in
2155 let module PET = ProofEngineTypes in
2156 let module PP = CicPp in
2157 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2158 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2159 let proof, goals = status in
2160 let goal' = List.nth goals 0 in
2161 let uri, metasenv, meta_proof, term_to_prove = proof in
2162 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2163 let eq_indexes, equalities, maxm = find_equalities context proof in
2164 let new_meta_goal, metasenv, type_of_goal =
2166 CicMkImplicit.identity_relocation_list_for_metavariable context in
2167 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2169 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2170 Cic.Meta (maxm+1, irl),
2171 (maxm+1, context, ty)::metasenv,
2174 let ugraph = CicUniv.empty_ugraph in
2175 let env = (metasenv, context, ugraph) in
2176 let t1 = Unix.gettimeofday () in
2177 let lib_eq_uris, library_equalities, maxm =
2178 find_library_equalities dbd context (proof, goal') (maxm+2) in
2179 let t2 = Unix.gettimeofday () in
2181 let equalities = (* equalities @ *) library_equalities in
2184 (Printf.sprintf "\n\nequalities:\n%s\n"
2188 (* Printf.sprintf "%s: %s" *)
2189 (UriManager.string_of_uri u)
2190 (* (string_of_equality e) *)
2193 debug_print (lazy "SIMPLYFYING EQUALITIES...");
2194 let rec simpl e others others_simpl =
2196 let active = List.map (fun (u, e) -> (Positive, e))
2197 (others @ others_simpl) in
2200 (fun t (_, e) -> Indexing.index t e)
2201 Indexing.empty active
2203 let res = forward_simplify env (Positive, e) (active, tbl) in
2207 | None -> simpl hd tl others_simpl
2208 | Some e -> simpl hd tl ((u, (snd e))::others_simpl)
2212 | None -> others_simpl
2213 | Some e -> (u, (snd e))::others_simpl
2217 match equalities with
2220 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
2222 List.rev (simpl (*(Positive,*) hd others [])
2226 (Printf.sprintf "\nequalities AFTER:\n%s\n"
2230 Printf.sprintf "%s: %s"
2231 (UriManager.string_of_uri u)
2232 (string_of_equality e)
2238 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
2242 let main_demod_equalities dbd term metasenv ugraph =
2243 let module C = Cic in
2244 let module T = CicTypeChecker in
2245 let module PET = ProofEngineTypes in
2246 let module PP = CicPp in
2247 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2248 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2249 let proof, goals = status in
2250 let goal' = List.nth goals 0 in
2251 let _, metasenv, meta_proof, _ = proof in
2252 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2253 let eq_indexes, equalities, maxm = find_equalities context proof in
2254 let lib_eq_uris, library_equalities, maxm =
2255 find_library_equalities dbd context (proof, goal') (maxm+2)
2257 let library_equalities = List.map snd library_equalities in
2258 maxmeta := maxm+2; (* TODO ugly!! *)
2259 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2260 let new_meta_goal, metasenv, type_of_goal =
2261 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2264 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
2265 (CicPp.ppterm ty)));
2266 Cic.Meta (maxm+1, irl),
2267 (maxm+1, context, ty)::metasenv,
2270 let env = (metasenv, context, ugraph) in
2272 let goal = Inference.BasicProof new_meta_goal, [], goal in
2273 let equalities = simplify_equalities env (equalities@library_equalities) in
2274 let active = make_active () in
2275 let passive = make_passive [] equalities in
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 print_endline "--------------------------------------------------";
2283 print_endline "GO!";
2284 start_time := Unix.gettimeofday ();
2285 if !time_limit < 1. then time_limit := 60.;
2287 saturate_equations env goal (fun e -> true) passive active
2291 List.fold_left (fun s e -> EqualitySet.add e s)
2292 EqualitySet.empty equalities
2295 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2300 | (n, _), (p, _), _ ->
2301 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2304 let l = List.map snd (fst ra) in
2305 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2307 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2308 (String.concat "\n" (List.map (string_of_equality ~env) active))
2309 (* (String.concat "\n"
2310 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2311 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2313 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) passive));
2317 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2321 let demodulate_tac ~dbd ~pattern ((proof,goal) as initialstatus) =
2322 let module I = Inference in
2323 let curi,metasenv,pbo,pty = proof in
2324 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2325 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2326 let lib_eq_uris, library_equalities, maxm =
2327 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2328 if library_equalities = [] then prerr_endline "VUOTA!!!";
2329 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2330 let library_equalities = List.map snd library_equalities in
2331 let goalterm = Cic.Meta (metano,irl) in
2332 let initgoal = Inference.BasicProof goalterm, [], ty in
2333 let env = (metasenv, context, CicUniv.empty_ugraph) in
2334 let equalities = simplify_equalities env (equalities@library_equalities) in
2337 (fun tbl eq -> Indexing.index tbl eq)
2338 Indexing.empty equalities
2340 let newmeta,(newproof,newmetasenv, newty) = Indexing.demodulation_goal
2341 maxm (metasenv,context,CicUniv.empty_ugraph) table initgoal
2343 if newmeta != maxm then
2345 let opengoal = Cic.Meta(maxm,irl) in
2347 Inference.build_proof_term ~noproof:opengoal newproof in
2348 let extended_metasenv = (maxm,context,newty)::metasenv in
2349 let extended_status =
2350 (curi,extended_metasenv,pbo,pty),goal in
2351 let (status,newgoals) =
2352 ProofEngineTypes.apply_tactic
2353 (PrimitiveTactics.apply_tac ~term:proofterm)
2355 (status,maxm::newgoals)
2357 else if newty = ty then
2358 raise (ProofEngineTypes.Fail (lazy "no progress"))
2359 else ProofEngineTypes.apply_tactic
2360 (ReductionTactics.simpl_tac ~pattern)
2364 let demodulate_tac ~dbd ~pattern =
2365 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)