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 =
486 Indexing.demodulation_equality !maxmeta env table sign current in
488 if is_identity env newcurrent then
489 if sign = Negative then Some (sign, newcurrent)
493 (* (Printf.sprintf "\ncurrent was: %s\nnewcurrent is: %s\n" *)
494 (* (string_of_equality current) *)
495 (* (string_of_equality newcurrent))); *)
498 (* (Printf.sprintf "active is: %s" *)
499 (* (String.concat "\n" *)
500 (* (List.map (fun (_, e) -> (string_of_equality e)) active_list)))); *)
504 Some (sign, newcurrent)
507 let res = demodulate active_table current in
510 | Some (sign, newcurrent) ->
511 match passive_table with
513 | Some passive_table -> demodulate passive_table newcurrent
517 | Some (Negative, c) ->
520 (fun (s, eq) -> s = Negative && meta_convertibility_eq eq c)
523 if ok then res else None
524 | Some (Positive, c) ->
525 if Indexing.in_index active_table c then
528 match passive_table with
530 if fst (Indexing.subsumption env active_table c) then
534 | Some passive_table ->
535 if Indexing.in_index passive_table c then None
537 let r1, _ = Indexing.subsumption env active_table c in
539 let r2, _ = Indexing.subsumption env passive_table c in
540 if r2 then None else res
543 type fs_time_info_t = {
544 mutable build_all: float;
545 mutable demodulate: float;
546 mutable subsumption: float;
549 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
552 (** simplifies new using active and passive *)
553 let forward_simplify_new env (new_neg, new_pos) ?passive active =
554 let t1 = Unix.gettimeofday () in
556 let active_list, active_table = active in
557 let pl, passive_table =
560 | Some ((pn, _), (pp, _), pt) ->
561 let pn = List.map (fun e -> (Negative, e)) pn
562 and pp = List.map (fun e -> (Positive, e)) pp in
566 let t2 = Unix.gettimeofday () in
567 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
569 let demodulate sign table target =
570 let newmeta, newtarget =
571 Indexing.demodulation_equality !maxmeta env table sign target in
575 let t1 = Unix.gettimeofday () in
577 let new_neg, new_pos =
578 let new_neg = List.map (demodulate Negative active_table) new_neg
579 and new_pos = List.map (demodulate Positive active_table) new_pos in
580 match passive_table with
581 | None -> new_neg, new_pos
582 | Some passive_table ->
583 List.map (demodulate Negative passive_table) new_neg,
584 List.map (demodulate Positive passive_table) new_pos
587 let t2 = Unix.gettimeofday () in
588 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
593 if not (Inference.is_identity env e) then
594 if EqualitySet.mem e s then s
595 else EqualitySet.add e s
597 EqualitySet.empty new_pos
599 let new_pos = EqualitySet.elements new_pos_set in
602 match passive_table with
604 (fun e -> not (fst (Indexing.subsumption env active_table e)))
605 | Some passive_table ->
606 (fun e -> not ((fst (Indexing.subsumption env active_table e)) ||
607 (fst (Indexing.subsumption env passive_table e))))
609 (* let t1 = Unix.gettimeofday () in *)
610 (* let t2 = Unix.gettimeofday () in *)
611 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
613 match passive_table with
615 (fun e -> not (Indexing.in_index active_table e))
616 | Some passive_table ->
618 not ((Indexing.in_index active_table e) ||
619 (Indexing.in_index passive_table e)))
621 new_neg, List.filter subs (List.filter is_duplicate new_pos)
625 (** simplifies active usign new *)
626 let backward_simplify_active env new_pos new_table min_weight active =
627 let active_list, active_table = active in
628 let active_list, newa =
630 (fun (s, equality) (res, newn) ->
631 let ew, _, _, _, _ = equality in
632 if ew < min_weight then
633 (s, equality)::res, newn
635 match forward_simplify env (s, equality) (new_pos, new_table) with
645 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
649 (fun (s, eq) (res, tbl) ->
650 if List.mem (s, eq) res then
652 else if (is_identity env eq) || (find eq res) then (
656 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
657 active_list ([], Indexing.empty),
659 (fun (s, eq) (n, p) ->
660 if (s <> Negative) && (is_identity env eq) then (
663 if s = Negative then eq::n, p
668 | [], [] -> active, None
669 | _ -> active, Some newa
673 (** simplifies passive using new *)
674 let backward_simplify_passive env new_pos new_table min_weight passive =
675 let (nl, ns), (pl, ps), passive_table = passive in
676 let f sign equality (resl, ress, newn) =
677 let ew, _, _, _, _ = equality in
678 if ew < min_weight then
679 equality::resl, ress, newn
681 match forward_simplify env (sign, equality) (new_pos, new_table) with
682 | None -> resl, EqualitySet.remove equality ress, newn
685 equality::resl, ress, newn
687 let ress = EqualitySet.remove equality ress in
690 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
691 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
694 (fun tbl e -> Indexing.index tbl e) Indexing.empty pl
696 match newn, newp with
697 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
698 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
702 let backward_simplify env new' ?passive active =
703 let new_pos, new_table, min_weight =
706 let ew, _, _, _, _ = e in
707 (Positive, e)::l, Indexing.index t e, min ew w)
708 ([], Indexing.empty, 1000000) (snd new')
711 backward_simplify_active env new_pos new_table min_weight active in
714 active, (make_passive [] []), newa, None
717 backward_simplify_passive env new_pos new_table min_weight passive in
718 active, passive, newa, newp
722 (* returns an estimation of how many equalities in passive can be activated
723 within the current time limit *)
724 let get_selection_estimate () =
725 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
726 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
728 ceil ((float_of_int !processed_clauses) *.
729 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
733 (** initializes the set of goals *)
734 let make_goals goal =
736 and passive = [0, [goal]] in
741 (** initializes the set of theorems *)
742 let make_theorems theorems =
747 let activate_goal (active, passive) =
749 | goal_conj::tl -> true, (goal_conj::active, tl)
750 | [] -> false, (active, passive)
754 let activate_theorem (active, passive) =
756 | theorem::tl -> true, (theorem::active, tl)
757 | [] -> false, (active, passive)
761 (** simplifies a goal with equalities in active and passive *)
762 let simplify_goal env goal ?passive (active_list, active_table) =
763 let pl, passive_table =
766 | Some ((pn, _), (pp, _), pt) ->
767 let pn = List.map (fun e -> (Negative, e)) pn
768 and pp = List.map (fun e -> (Positive, e)) pp in
772 let demodulate table goal =
773 let newmeta, newgoal =
774 Indexing.demodulation_goal !maxmeta env table goal in
776 goal != newgoal, newgoal
779 match passive_table with
780 | None -> demodulate active_table goal
781 | Some passive_table ->
782 let changed, goal = demodulate active_table goal in
783 let changed', goal = demodulate passive_table goal in
784 (changed || changed'), goal
790 let simplify_goals env goals ?passive active =
791 let a_goals, p_goals = goals in
796 List.map (fun g -> snd (simplify_goal env g ?passive active)) gl in
802 (fun (a, p) (d, gl) ->
803 let changed = ref false in
807 let c, g = simplify_goal env g ?passive active in
808 changed := !changed || c; g) gl in
809 if !changed then (a, (d, gl)::p) else ((d, gl)::a, p))
810 ([], p_goals) a_goals
816 let simplify_theorems env theorems ?passive (active_list, active_table) =
817 let pl, passive_table =
820 | Some ((pn, _), (pp, _), pt) ->
821 let pn = List.map (fun e -> (Negative, e)) pn
822 and pp = List.map (fun e -> (Positive, e)) pp in
825 let a_theorems, p_theorems = theorems in
826 let demodulate table theorem =
827 let newmeta, newthm =
828 Indexing.demodulation_theorem !maxmeta env table theorem in
830 theorem != newthm, newthm
832 let foldfun table (a, p) theorem =
833 let changed, theorem = demodulate table theorem in
834 if changed then (a, theorem::p) else (theorem::a, p)
836 let mapfun table theorem = snd (demodulate table theorem) in
837 match passive_table with
839 let p_theorems = List.map (mapfun active_table) p_theorems in
840 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
841 | Some passive_table ->
842 let p_theorems = List.map (mapfun active_table) p_theorems in
843 let p_theorems, a_theorems =
844 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
845 let p_theorems = List.map (mapfun passive_table) p_theorems in
846 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
850 let rec simpl env e others others_simpl =
851 let active = others @ others_simpl in
854 (fun t (_, e) -> Indexing.index t e)
855 Indexing.empty active
857 let res = forward_simplify env e (active, tbl) in
861 | None -> simpl env hd tl others_simpl
862 | Some e -> simpl env hd tl (e::others_simpl)
866 | None -> others_simpl
867 | Some e -> e::others_simpl
871 let simplify_equalities env equalities =
874 (Printf.sprintf "equalities:\n%s\n"
876 (List.map string_of_equality equalities))));
877 debug_print (lazy "SIMPLYFYING EQUALITIES...");
878 match equalities with
881 let others = List.map (fun e -> (Positive, e)) tl in
883 List.rev (List.map snd (simpl env (Positive, hd) others []))
887 (Printf.sprintf "equalities AFTER:\n%s\n"
889 (List.map string_of_equality res))));
893 (* applies equality to goal to see if the goal can be closed *)
894 let apply_equality_to_goal env equality goal =
895 let module C = Cic in
896 let module HL = HelmLibraryObjects in
897 let module I = Inference in
898 let metasenv, context, ugraph = env in
899 let _, proof, (ty, left, right, _), metas, args = equality in
901 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
902 let gproof, gmetas, gterm = goal in
905 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
906 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
908 let subst, metasenv', _ =
909 let menv = metasenv @ metas @ gmetas in
910 Inference.unification menv context eqterm gterm ugraph
914 | I.BasicProof t -> I.BasicProof (CicMetaSubst.apply_subst subst t)
915 | I.ProofBlock (s, uri, nt, t, pe, p) ->
916 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
920 let rec repl = function
921 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
922 | I.NoProof -> newproof
923 | I.BasicProof p -> newproof
924 | I.SubProof (t, i, p) -> I.SubProof (t, i, repl p)
929 true, subst, newgproof
930 with CicUnification.UnificationFailure _ ->
936 let new_meta metasenv =
937 let m = CicMkImplicit.new_meta metasenv [] in
939 while !maxmeta <= m do incr maxmeta done;
944 (* applies a theorem or an equality to goal, returning a list of subgoals or
945 an indication of failure *)
946 let apply_to_goal env theorems ?passive active goal =
947 let metasenv, context, ugraph = env in
948 let proof, metas, term = goal in
951 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
952 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
955 CicMkImplicit.identity_relocation_list_for_metavariable context in
956 let proof', newmeta =
957 let rec get_meta = function
958 | SubProof (t, i, p) ->
959 let t', i' = get_meta p in
960 if i' = -1 then t, i else t', i'
961 | ProofGoalBlock (_, p) -> get_meta p
962 | _ -> Cic.Implicit None, -1
964 let p, m = get_meta proof in
966 let n = new_meta (metasenv @ metas) in
971 let metasenv = (newmeta, context, term)::metasenv @ metas in
972 let bit = new_meta metasenv, context, term in
973 let metasenv' = bit::metasenv in
974 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
976 let rec aux = function
978 | (theorem, thmty, _)::tl ->
980 let subst, (newproof, newgoals) =
981 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
983 if newgoals = [] then
984 let _, _, p, _ = newproof in
986 let rec repl = function
987 | Inference.ProofGoalBlock (_, gp) ->
988 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
989 | Inference.NoProof -> Inference.BasicProof p
990 | Inference.BasicProof _ -> Inference.BasicProof p
991 | Inference.SubProof (t, i, p2) ->
992 Inference.SubProof (t, i, repl p2)
998 let subst = List.filter (fun (i, _) -> i = m) subst in
999 `Ok (subst, [newp, metas, term])
1001 let _, menv, p, _ = newproof in
1003 CicMkImplicit.identity_relocation_list_for_metavariable context
1008 let _, _, ty = CicUtil.lookup_meta i menv in
1010 let rec gp = function
1011 | SubProof (t, i, p) ->
1012 SubProof (t, i, gp p)
1013 | ProofGoalBlock (sp1, sp2) ->
1014 ProofGoalBlock (sp1, gp sp2)
1017 SubProof (p, i, BasicProof (Cic.Meta (i, irl)))
1018 | ProofSymBlock (s, sp) ->
1019 ProofSymBlock (s, gp sp)
1020 | ProofBlock (s, u, nt, t, pe, sp) ->
1021 ProofBlock (s, u, nt, t, pe, gp sp)
1029 let w, m = weight_of_term t in
1030 w + 2 * (List.length m)
1033 (fun (_, _, t1) (_, _, t2) ->
1034 Pervasives.compare (weight t1) (weight t2))
1037 let best = aux tl in
1039 | `Ok (_, _) -> best
1040 | `No -> `GoOn ([subst, goals])
1041 | `GoOn sl -> `GoOn ((subst, goals)::sl)
1042 with ProofEngineTypes.Fail msg ->
1046 if Inference.term_is_equality term then
1047 let rec appleq_a = function
1048 | [] -> false, [], []
1049 | (Positive, equality)::tl ->
1050 let ok, s, newproof = apply_equality_to_goal env equality goal in
1051 if ok then true, s, [newproof, metas, term] else appleq_a tl
1052 | _::tl -> appleq_a tl
1054 let rec appleq_p = function
1055 | [] -> false, [], []
1057 let ok, s, newproof = apply_equality_to_goal env equality goal in
1058 if ok then true, s, [newproof, metas, term] else appleq_p tl
1060 let al, _ = active in
1062 | None -> appleq_a al
1063 | Some (_, (pl, _), _) ->
1064 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1068 if r = true then `Ok (s, l) else aux theorems
1072 (* sorts a conjunction of goals in order to detect earlier if it is
1073 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1074 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1077 (fun (_, e1, g1) (_, e2, g2) ->
1079 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1081 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1085 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1090 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1094 if prop1 = 0 && prop2 = 0 then
1095 let e1 = if Inference.term_is_equality g1 then 0 else 1
1096 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1106 let is_meta_closed goals =
1107 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1111 (* applies a series of theorems/equalities to a conjunction of goals *)
1112 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1113 let aux (goal, r) tl =
1114 let propagate_subst subst (proof, metas, term) =
1115 let rec repl = function
1116 | NoProof -> NoProof
1118 BasicProof (CicMetaSubst.apply_subst subst t)
1119 | ProofGoalBlock (p, pb) ->
1120 let pb' = repl pb in
1121 ProofGoalBlock (p, pb')
1122 | SubProof (t, i, p) ->
1123 let t' = CicMetaSubst.apply_subst subst t in
1126 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1127 | ProofBlock (s, u, nty, t, pe, p) ->
1128 ProofBlock (subst @ s, u, nty, t, pe, p)
1129 in (repl proof, metas, term)
1131 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1133 | `No -> `No (depth, goals)
1138 let tl = List.map (propagate_subst s) tl in
1139 sort_goal_conj env (depth+1, gl @ tl)) sl
1142 | `Ok (subst, gl) ->
1146 let p, _, _ = List.hd gl in
1148 let rec repl = function
1149 | SubProof (_, _, p) -> repl p
1150 | ProofGoalBlock (p1, p2) ->
1151 ProofGoalBlock (repl p1, repl p2)
1154 build_proof_term (repl p)
1157 let rec get_meta = function
1158 | SubProof (_, i, p) ->
1159 let i' = get_meta p in
1160 if i' = -1 then i else i'
1161 (* max i (get_meta p) *)
1162 | ProofGoalBlock (_, p) -> get_meta p
1168 let _, (context, _, _) = List.hd subst in
1169 [i, (context, subproof, Cic.Implicit None)]
1171 let tl = List.map (propagate_subst subst) tl in
1172 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1176 if depth > !maxdepth || (List.length goals) > !maxwidth then
1179 let rec search_best res = function
1182 let r = apply_to_goal env theorems ?passive active goal in
1184 | `Ok _ -> (goal, r)
1185 | `No -> search_best res tl
1189 | _, `Ok _ -> assert false
1192 if (List.length l) < (List.length l2) then goal, r else res
1194 search_best newres tl
1196 let hd = List.hd goals in
1197 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1201 | _, _ -> search_best res (List.tl goals)
1203 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1205 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1206 (List.length (snd conj)) < (List.length goals)->
1207 apply_to_goal_conj env theorems ?passive active conj
1213 module OrderedGoals = struct
1214 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1221 else let r = (List.length l1) - (List.length l2) in
1227 (fun (_, _, t1) (_, _, t2) ->
1228 let r = Pervasives.compare t1 t2 in
1237 module GoalsSet = Set.Make(OrderedGoals);;
1240 exception SearchSpaceOver;;
1245 let apply_to_goals env is_passive_empty theorems active goals =
1246 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1247 let add_to set goals =
1248 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1250 let rec aux set = function
1252 debug_print (lazy "HERE!!!");
1253 if is_passive_empty then raise SearchSpaceOver else false, set
1255 let res = apply_to_goal_conj env theorems active goals in
1261 | (d, (p, _, t)::_) -> d, p, t
1266 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1267 d (string_of_proof p) (CicPp.ppterm t)))
1269 true, GoalsSet.singleton newgoals
1271 let set' = add_to set (goals::tl) in
1272 let set' = add_to set' newgoals in
1277 let n = List.length goals in
1278 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1279 let goals = GoalsSet.elements goals in
1280 debug_print (lazy "\n\tapply_to_goals end\n");
1281 let m = List.length goals in
1282 if m = n && is_passive_empty then
1283 raise SearchSpaceOver
1290 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1291 work that well yet...) *)
1292 let sort_passive_goals goals =
1294 (fun (d1, l1) (d2, l2) ->
1296 and r2 = (List.length l1) - (List.length l2) in
1297 let foldfun ht (_, _, t) =
1298 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1301 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1302 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1303 in let r3 = m1 - m2 in
1305 else if r2 <> 0 then r2
1307 (* let _, _, g1 = List.hd l1 *)
1308 (* and _, _, g2 = List.hd l2 in *)
1309 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1310 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1311 (* in let r4 = e1 - e2 in *)
1312 (* if r4 <> 0 then r3 else r1) *)
1317 let print_goals goals =
1324 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1326 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1330 (* tries to prove the first conjunction in goals with applications of
1331 theorems/equalities, returning new sub-goals or an indication of success *)
1332 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1333 let theorems, _ = theorems in
1334 let a_goals, p_goals = goals in
1335 let goal = List.hd a_goals in
1336 let not_in_active gl =
1340 if (List.length gl) = (List.length gl') then
1341 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1347 let res = apply_to_goal_conj env theorems ?passive active goal in
1350 true, ([newgoals], [])
1352 false, (a_goals, p_goals)
1357 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1360 let p_goals = newgoals @ p_goals in
1361 let p_goals = sort_passive_goals p_goals in
1362 false, (a_goals, p_goals)
1368 let apply_theorem_to_goals env theorems active goals =
1369 let a_goals, p_goals = goals in
1370 let theorem = List.hd (fst theorems) in
1371 let theorems = [theorem] in
1372 let rec aux p = function
1373 | [] -> false, ([], p)
1375 let res = apply_to_goal_conj env theorems active goal in
1377 | `Ok newgoals -> true, ([newgoals], [])
1379 | `GoOn newgoals -> aux (newgoals @ p) tl
1381 let ok, (a, p) = aux p_goals a_goals in
1387 (fun (d1, l1) (d2, l2) ->
1390 else let r = (List.length l1) - (List.length l2) in
1396 (fun (_, _, t1) (_, _, t2) ->
1397 let r = Pervasives.compare t1 t2 in
1398 if r <> 0 then (res := r; true) else false) l1 l2
1402 ok, (a_goals, p_goals)
1406 (* given-clause algorithm with lazy reduction strategy *)
1407 let rec given_clause dbd env goals theorems passive active =
1408 let goals = simplify_goals env goals active in
1409 let ok, goals = activate_goal goals in
1410 (* let theorems = simplify_theorems env theorems active in *)
1412 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1415 match (fst goals) with
1416 | (_, [proof, _, _])::_ -> Some proof
1419 ParamodulationSuccess (proof, env)
1421 given_clause_aux dbd env goals theorems passive active
1423 (* let ok', theorems = activate_theorem theorems in *)
1424 let ok', theorems = false, theorems in
1426 let ok, goals = apply_theorem_to_goals env theorems active goals in
1429 match (fst goals) with
1430 | (_, [proof, _, _])::_ -> Some proof
1433 ParamodulationSuccess (proof, env)
1435 given_clause_aux dbd env goals theorems passive active
1437 if (passive_is_empty passive) then ParamodulationFailure
1438 else given_clause_aux dbd env goals theorems passive active
1440 and given_clause_aux dbd env goals theorems passive active =
1441 let time1 = Unix.gettimeofday () in
1443 let selection_estimate = get_selection_estimate () in
1444 let kept = size_of_passive passive in
1446 if !time_limit = 0. || !processed_clauses = 0 then
1448 else if !elapsed_time > !time_limit then (
1449 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1450 !time_limit !elapsed_time));
1452 ) else if kept > selection_estimate then (
1454 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1455 "(kept: %d, selection_estimate: %d)\n")
1456 kept selection_estimate));
1457 prune_passive selection_estimate active passive
1462 let time2 = Unix.gettimeofday () in
1463 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1465 kept_clauses := (size_of_passive passive) + (size_of_active active);
1466 match passive_is_empty passive with
1467 | true -> (* ParamodulationFailure *)
1468 given_clause dbd env goals theorems passive active
1470 let (sign, current), passive = select env (fst goals) passive active in
1471 let time1 = Unix.gettimeofday () in
1472 let res = forward_simplify env (sign, current) ~passive active in
1473 let time2 = Unix.gettimeofday () in
1474 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1477 given_clause dbd env goals theorems passive active
1478 | Some (sign, current) ->
1479 if (sign = Negative) && (is_identity env current) then (
1481 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1482 (string_of_equality ~env current)));
1483 let _, proof, _, _, _ = current in
1484 ParamodulationSuccess (Some proof, env)
1487 (lazy "\n================================================");
1488 debug_print (lazy (Printf.sprintf "selected: %s %s"
1489 (string_of_sign sign)
1490 (string_of_equality ~env current)));
1492 let t1 = Unix.gettimeofday () in
1493 let new' = infer env sign current active in
1494 let t2 = Unix.gettimeofday () in
1495 infer_time := !infer_time +. (t2 -. t1);
1497 let res, goal' = contains_empty env new' in
1501 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1504 ParamodulationSuccess (proof, env)
1506 let t1 = Unix.gettimeofday () in
1507 let new' = forward_simplify_new env new' active in
1508 let t2 = Unix.gettimeofday () in
1510 forward_simpl_new_time :=
1511 !forward_simpl_new_time +. (t2 -. t1)
1515 | Negative -> active
1517 let t1 = Unix.gettimeofday () in
1518 let active, _, newa, _ =
1519 backward_simplify env ([], [current]) active
1521 let t2 = Unix.gettimeofday () in
1522 backward_simpl_time :=
1523 !backward_simpl_time +. (t2 -. t1);
1527 let al, tbl = active in
1528 let nn = List.map (fun e -> Negative, e) n in
1533 Indexing.index tbl e)
1538 match contains_empty env new' with
1541 let al, tbl = active in
1543 | Negative -> (sign, current)::al, tbl
1545 al @ [(sign, current)], Indexing.index tbl current
1547 let passive = add_to_passive passive new' in
1548 given_clause dbd env goals theorems passive active
1553 let _, proof, _, _, _ = goal in Some proof
1556 ParamodulationSuccess (proof, env)
1561 (** given-clause algorithm with full reduction strategy *)
1562 let rec given_clause_fullred dbd env goals theorems passive active =
1563 let goals = simplify_goals env goals ~passive active in
1564 let ok, goals = activate_goal goals in
1565 (* let theorems = simplify_theorems env theorems ~passive active in *)
1570 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1571 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1572 (* let current = List.hd (fst goals) in *)
1573 (* let p, _, t = List.hd (snd current) in *)
1576 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1577 (* (CicPp.ppterm t) (string_of_proof p))); *)
1580 apply_goal_to_theorems dbd env theorems ~passive active goals
1584 match (fst goals) with
1585 | (_, [proof, _, _])::_ -> Some proof
1588 ParamodulationSuccess (proof, env)
1590 given_clause_fullred_aux dbd env goals theorems passive active
1592 (* let ok', theorems = activate_theorem theorems in *)
1594 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1597 (* match (fst goals) with *)
1598 (* | (_, [proof, _, _])::_ -> Some proof *)
1599 (* | _ -> assert false *)
1601 (* ParamodulationSuccess (proof, env) *)
1603 (* given_clause_fullred_aux env goals theorems passive active *)
1605 if (passive_is_empty passive) then ParamodulationFailure
1606 else given_clause_fullred_aux dbd env goals theorems passive active
1608 and given_clause_fullred_aux dbd env goals theorems passive active =
1609 let time1 = Unix.gettimeofday () in
1611 let selection_estimate = get_selection_estimate () in
1612 let kept = size_of_passive passive in
1614 if !time_limit = 0. || !processed_clauses = 0 then
1616 else if !elapsed_time > !time_limit then (
1617 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1618 !time_limit !elapsed_time));
1620 ) else if kept > selection_estimate then (
1622 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1623 "(kept: %d, selection_estimate: %d)\n")
1624 kept selection_estimate));
1625 prune_passive selection_estimate active passive
1630 let time2 = Unix.gettimeofday () in
1631 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1633 kept_clauses := (size_of_passive passive) + (size_of_active active);
1634 match passive_is_empty passive with
1635 | true -> (* ParamodulationFailure *)
1636 given_clause_fullred dbd env goals theorems passive active
1638 let (sign, current), passive = select env (fst goals) passive active in
1639 let time1 = Unix.gettimeofday () in
1640 let res = forward_simplify env (sign, current) ~passive active in
1641 let time2 = Unix.gettimeofday () in
1642 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1645 given_clause_fullred dbd env goals theorems passive active
1646 | Some (sign, current) ->
1647 if (sign = Negative) && (is_identity env current) then (
1649 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1650 (string_of_equality ~env current)));
1651 let _, proof, _, _, _ = current in
1652 ParamodulationSuccess (Some proof, env)
1655 (lazy "\n================================================");
1656 debug_print (lazy (Printf.sprintf "selected: %s %s"
1657 (string_of_sign sign)
1658 (string_of_equality ~env current)));
1660 let t1 = Unix.gettimeofday () in
1661 let new' = infer env sign current active in
1662 let t2 = Unix.gettimeofday () in
1663 infer_time := !infer_time +. (t2 -. t1);
1666 if is_identity env current then active
1668 let al, tbl = active in
1670 | Negative -> (sign, current)::al, tbl
1672 al @ [(sign, current)], Indexing.index tbl current
1674 let rec simplify new' active passive =
1675 let t1 = Unix.gettimeofday () in
1676 let new' = forward_simplify_new env new' ~passive active in
1677 let t2 = Unix.gettimeofday () in
1678 forward_simpl_new_time :=
1679 !forward_simpl_new_time +. (t2 -. t1);
1680 let t1 = Unix.gettimeofday () in
1681 let active, passive, newa, retained =
1682 backward_simplify env new' ~passive active in
1683 let t2 = Unix.gettimeofday () in
1684 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1685 match newa, retained with
1686 | None, None -> active, passive, new'
1688 | None, Some (n, p) ->
1689 let nn, np = new' in
1690 simplify (nn @ n, np @ p) active passive
1691 | Some (n, p), Some (rn, rp) ->
1692 let nn, np = new' in
1693 simplify (nn @ n @ rn, np @ p @ rp) active passive
1695 let active, passive, new' = simplify new' active passive in
1697 let k = size_of_passive passive in
1698 if k < (kept - 1) then
1699 processed_clauses := !processed_clauses + (kept - 1 - k);
1704 (Printf.sprintf "active:\n%s\n"
1707 (fun (s, e) -> (string_of_sign s) ^ " " ^
1708 (string_of_equality ~env e))
1716 (Printf.sprintf "new':\n%s\n"
1719 (fun e -> "Negative " ^
1720 (string_of_equality ~env e)) neg) @
1722 (fun e -> "Positive " ^
1723 (string_of_equality ~env e)) pos)))))
1725 match contains_empty env new' with
1727 let passive = add_to_passive passive new' in
1728 given_clause_fullred dbd env goals theorems passive active
1732 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1735 ParamodulationSuccess (proof, env)
1740 let rec saturate_equations env goal accept_fun passive active =
1741 elapsed_time := Unix.gettimeofday () -. !start_time;
1742 if !elapsed_time > !time_limit then
1745 let (sign, current), passive = select env [1, [goal]] passive active in
1746 let res = forward_simplify env (sign, current) ~passive active in
1749 saturate_equations env goal accept_fun passive active
1750 | Some (sign, current) ->
1751 assert (sign = Positive);
1753 (lazy "\n================================================");
1754 debug_print (lazy (Printf.sprintf "selected: %s %s"
1755 (string_of_sign sign)
1756 (string_of_equality ~env current)));
1757 let new' = infer env sign current active in
1759 if is_identity env current then active
1761 let al, tbl = active in
1762 al @ [(sign, current)], Indexing.index tbl current
1764 let rec simplify new' active passive =
1765 let new' = forward_simplify_new env new' ~passive active in
1766 let active, passive, newa, retained =
1767 backward_simplify env new' ~passive active in
1768 match newa, retained with
1769 | None, None -> active, passive, new'
1771 | None, Some (n, p) ->
1772 let nn, np = new' in
1773 simplify (nn @ n, np @ p) active passive
1774 | Some (n, p), Some (rn, rp) ->
1775 let nn, np = new' in
1776 simplify (nn @ n @ rn, np @ p @ rp) active passive
1778 let active, passive, new' = simplify new' active passive in
1782 (Printf.sprintf "active:\n%s\n"
1785 (fun (s, e) -> (string_of_sign s) ^ " " ^
1786 (string_of_equality ~env e))
1794 (Printf.sprintf "new':\n%s\n"
1797 (fun e -> "Negative " ^
1798 (string_of_equality ~env e)) neg) @
1800 (fun e -> "Positive " ^
1801 (string_of_equality ~env e)) pos)))))
1803 let new' = match new' with _, pos -> [], List.filter accept_fun pos in
1804 let passive = add_to_passive passive new' in
1805 saturate_equations env goal accept_fun passive active
1811 let main dbd full term metasenv ugraph =
1812 let module C = Cic in
1813 let module T = CicTypeChecker in
1814 let module PET = ProofEngineTypes in
1815 let module PP = CicPp in
1816 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1817 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1818 let proof, goals = status in
1819 let goal' = List.nth goals 0 in
1820 let _, metasenv, meta_proof, _ = proof in
1821 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1822 let eq_indexes, equalities, maxm = find_equalities context proof in
1823 let lib_eq_uris, library_equalities, maxm =
1824 find_library_equalities dbd context (proof, goal') (maxm+2)
1826 let library_equalities = List.map snd library_equalities in
1827 maxmeta := maxm+2; (* TODO ugly!! *)
1828 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1829 let new_meta_goal, metasenv, type_of_goal =
1830 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1833 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1834 Cic.Meta (maxm+1, irl),
1835 (maxm+1, context, ty)::metasenv,
1838 let env = (metasenv, context, ugraph) in
1839 let t1 = Unix.gettimeofday () in
1842 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1843 let context_hyp = find_context_hypotheses env eq_indexes in
1844 context_hyp @ theorems, []
1847 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1848 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1850 let t = CicUtil.term_of_uri refl_equal in
1851 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1854 let t2 = Unix.gettimeofday () in
1857 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1862 "Theorems:\n-------------------------------------\n%s\n"
1867 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1871 let goal = Inference.BasicProof new_meta_goal, [], goal in
1872 let equalities = simplify_equalities env (equalities@library_equalities) in
1873 let active = make_active () in
1874 let passive = make_passive [] equalities in
1875 Printf.printf "\ncurrent goal: %s\n"
1876 (let _, _, g = goal in CicPp.ppterm g);
1877 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1878 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1879 Printf.printf "\nequalities:\n%s\n"
1882 (string_of_equality ~env) equalities));
1883 (* (equalities @ library_equalities))); *)
1884 print_endline "--------------------------------------------------";
1885 let start = Unix.gettimeofday () in
1886 print_endline "GO!";
1887 start_time := Unix.gettimeofday ();
1889 let goals = make_goals goal in
1890 (if !use_fullred then given_clause_fullred else given_clause)
1891 dbd env goals theorems passive active
1893 let finish = Unix.gettimeofday () in
1896 | ParamodulationFailure ->
1897 Printf.printf "NO proof found! :-(\n\n"
1898 | ParamodulationSuccess (Some proof, env) ->
1899 let proof = Inference.build_proof_term proof in
1900 Printf.printf "OK, found a proof!\n";
1901 (* REMEMBER: we have to instantiate meta_proof, we should use
1902 apply the "apply" tactic to proof and status
1904 let names = names_of_context context in
1905 print_endline (PP.pp proof names);
1908 (fun m (_, _, _, menv, _) -> m @ menv) metasenv equalities
1913 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1915 print_endline (string_of_float (finish -. start));
1917 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1918 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1920 (fst (CicReduction.are_convertible
1921 context type_of_goal ty ug)));
1923 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1924 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1925 print_endline (string_of_float (finish -. start));*)
1929 | ParamodulationSuccess (None, env) ->
1930 Printf.printf "Success, but no proof?!?\n\n"
1932 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1933 "forward_simpl_new_time: %.9f\n" ^^
1934 "backward_simpl_time: %.9f\n")
1935 !infer_time !forward_simpl_time !forward_simpl_new_time
1936 !backward_simpl_time;
1937 Printf.printf "passive_maintainance_time: %.9f\n"
1938 !passive_maintainance_time;
1939 Printf.printf " successful unification/matching time: %.9f\n"
1940 !Indexing.match_unif_time_ok;
1941 Printf.printf " failed unification/matching time: %.9f\n"
1942 !Indexing.match_unif_time_no;
1943 Printf.printf " indexing retrieval time: %.9f\n"
1944 !Indexing.indexing_retrieval_time;
1945 Printf.printf " demodulate_term.build_newtarget_time: %.9f\n"
1946 !Indexing.build_newtarget_time;
1947 Printf.printf "derived %d clauses, kept %d clauses.\n"
1948 !derived_clauses !kept_clauses;
1951 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1957 let default_depth = !maxdepth
1958 and default_width = !maxwidth;;
1962 symbols_counter := 0;
1963 weight_age_counter := !weight_age_ratio;
1964 processed_clauses := 0;
1967 maximal_retained_equality := None;
1969 forward_simpl_time := 0.;
1970 forward_simpl_new_time := 0.;
1971 backward_simpl_time := 0.;
1972 passive_maintainance_time := 0.;
1973 derived_clauses := 0;
1978 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1979 let module C = Cic in
1981 Indexing.init_index ();
1984 let proof, goal = status in
1986 let uri, metasenv, meta_proof, term_to_prove = proof in
1987 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1988 let eq_indexes, equalities, maxm = find_equalities context proof in
1989 let new_meta_goal, metasenv, type_of_goal =
1991 CicMkImplicit.identity_relocation_list_for_metavariable context in
1992 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1994 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
1995 Cic.Meta (maxm+1, irl),
1996 (maxm+1, context, ty)::metasenv,
1999 let ugraph = CicUniv.empty_ugraph in
2000 let env = (metasenv, context, ugraph) in
2001 let goal = Inference.BasicProof new_meta_goal, [], goal in
2003 let t1 = Unix.gettimeofday () in
2004 let lib_eq_uris, library_equalities, maxm =
2005 find_library_equalities dbd context (proof, goal') (maxm+2)
2007 let library_equalities = List.map snd library_equalities in
2008 let t2 = Unix.gettimeofday () in
2010 let equalities = simplify_equalities env (equalities@library_equalities) in
2013 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
2014 let t1 = Unix.gettimeofday () in
2017 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2018 let context_hyp = find_context_hypotheses env eq_indexes in
2019 context_hyp @ thms, []
2022 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2023 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2025 let t = CicUtil.term_of_uri refl_equal in
2026 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2029 let t2 = Unix.gettimeofday () in
2034 "Theorems:\n-------------------------------------\n%s\n"
2039 "Term: %s, type: %s"
2040 (CicPp.ppterm t) (CicPp.ppterm ty))
2044 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2046 let active = make_active () in
2047 let passive = make_passive [] equalities in
2048 let start = Unix.gettimeofday () in
2050 let goals = make_goals goal in
2051 given_clause_fullred dbd env goals theorems passive active
2053 let finish = Unix.gettimeofday () in
2054 (res, finish -. start)
2057 | ParamodulationSuccess (Some proof, env) ->
2058 debug_print (lazy "OK, found a proof!");
2059 let proof = Inference.build_proof_term proof in
2060 let names = names_of_context context in
2063 match new_meta_goal with
2064 | C.Meta (i, _) -> i | _ -> assert false
2066 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2071 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2073 debug_print (lazy (CicPp.pp proof [](* names *)));
2077 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2078 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2080 (fst (CicReduction.are_convertible
2081 context type_of_goal ty ug)))));
2082 let equality_for_replace i t1 =
2084 | C.Meta (n, _) -> n = i
2088 ProofEngineReduction.replace
2089 ~equality:equality_for_replace
2090 ~what:[goal'] ~with_what:[proof]
2095 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2096 (match uri with Some uri -> UriManager.string_of_uri uri
2098 (print_metasenv newmetasenv)
2099 (CicPp.pp real_proof [](* names *))
2100 (CicPp.pp term_to_prove names)));
2101 ((uri, newmetasenv, real_proof, term_to_prove), [])
2102 with CicTypeChecker.TypeCheckerFailure _ ->
2103 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2104 debug_print (lazy (CicPp.pp proof names));
2105 raise (ProofEngineTypes.Fail
2106 (lazy "Found a proof, but it doesn't typecheck"))
2108 debug_print (lazy (Printf.sprintf "\nTIME NEEDED: %.9f" time));
2111 raise (ProofEngineTypes.Fail (lazy "NO proof found"))
2114 (* dummy function called within matita to trigger linkage *)
2118 let retrieve_and_print dbd term metasenv ugraph =
2119 let module C = Cic in
2120 let module T = CicTypeChecker in
2121 let module PET = ProofEngineTypes in
2122 let module PP = CicPp in
2123 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2124 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2125 let proof, goals = status in
2126 let goal' = List.nth goals 0 in
2127 let uri, metasenv, meta_proof, term_to_prove = proof in
2128 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2129 let eq_indexes, equalities, maxm = find_equalities context proof in
2130 let new_meta_goal, metasenv, type_of_goal =
2132 CicMkImplicit.identity_relocation_list_for_metavariable context in
2133 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2135 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2136 Cic.Meta (maxm+1, irl),
2137 (maxm+1, context, ty)::metasenv,
2140 let ugraph = CicUniv.empty_ugraph in
2141 let env = (metasenv, context, ugraph) in
2142 let t1 = Unix.gettimeofday () in
2143 let lib_eq_uris, library_equalities, maxm =
2144 find_library_equalities dbd context (proof, goal') (maxm+2) in
2145 let t2 = Unix.gettimeofday () in
2147 let equalities = (* equalities @ *) library_equalities in
2150 (Printf.sprintf "\n\nequalities:\n%s\n"
2154 (* Printf.sprintf "%s: %s" *)
2155 (UriManager.string_of_uri u)
2156 (* (string_of_equality e) *)
2159 debug_print (lazy "SIMPLYFYING EQUALITIES...");
2160 let rec simpl e others others_simpl =
2162 let active = List.map (fun (u, e) -> (Positive, e))
2163 (others @ others_simpl) in
2166 (fun t (_, e) -> Indexing.index t e)
2167 Indexing.empty active
2169 let res = forward_simplify env (Positive, e) (active, tbl) in
2173 | None -> simpl hd tl others_simpl
2174 | Some e -> simpl hd tl ((u, (snd e))::others_simpl)
2178 | None -> others_simpl
2179 | Some e -> (u, (snd e))::others_simpl
2183 match equalities with
2186 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
2188 List.rev (simpl (*(Positive,*) hd others [])
2192 (Printf.sprintf "\nequalities AFTER:\n%s\n"
2196 Printf.sprintf "%s: %s"
2197 (UriManager.string_of_uri u)
2198 (string_of_equality e)
2204 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
2208 let main_demod_equalities dbd term metasenv ugraph =
2209 let module C = Cic in
2210 let module T = CicTypeChecker in
2211 let module PET = ProofEngineTypes in
2212 let module PP = CicPp in
2213 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2214 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2215 let proof, goals = status in
2216 let goal' = List.nth goals 0 in
2217 let _, metasenv, meta_proof, _ = proof in
2218 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2219 let eq_indexes, equalities, maxm = find_equalities context proof in
2220 let lib_eq_uris, library_equalities, maxm =
2221 find_library_equalities dbd context (proof, goal') (maxm+2)
2223 let library_equalities = List.map snd library_equalities in
2224 maxmeta := maxm+2; (* TODO ugly!! *)
2225 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2226 let new_meta_goal, metasenv, type_of_goal =
2227 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2230 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
2231 (CicPp.ppterm ty)));
2232 Cic.Meta (maxm+1, irl),
2233 (maxm+1, context, ty)::metasenv,
2236 let env = (metasenv, context, ugraph) in
2238 let goal = Inference.BasicProof new_meta_goal, [], goal in
2239 let equalities = simplify_equalities env (equalities@library_equalities) in
2240 let active = make_active () in
2241 let passive = make_passive [] equalities in
2242 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2243 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2244 Printf.printf "\nequalities:\n%s\n"
2247 (string_of_equality ~env) equalities));
2248 print_endline "--------------------------------------------------";
2249 print_endline "GO!";
2250 start_time := Unix.gettimeofday ();
2251 if !time_limit < 1. then time_limit := 60.;
2253 saturate_equations env goal (fun e -> true) passive active
2257 List.fold_left (fun s e -> EqualitySet.add e s)
2258 EqualitySet.empty equalities
2261 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2266 | (n, _), (p, _), _ ->
2267 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2270 let l = List.map snd (fst ra) in
2271 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2273 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2274 (String.concat "\n" (List.map (string_of_equality ~env) active))
2275 (* (String.concat "\n"
2276 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2277 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2279 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) passive));
2283 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))
2287 let demodulate_tac ~dbd ~pattern ((proof,goal) as initialstatus) =
2288 let module I = Inference in
2289 let curi,metasenv,pbo,pty = proof in
2290 let metano,context,ty = CicUtil.lookup_meta goal metasenv in
2291 let eq_indexes, equalities, maxm = I.find_equalities context proof in
2292 let lib_eq_uris, library_equalities, maxm =
2293 I.find_library_equalities dbd context (proof, goal) (maxm+2) in
2294 if library_equalities = [] then prerr_endline "VUOTA!!!";
2295 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2296 let library_equalities = List.map snd library_equalities in
2297 let goalterm = Cic.Meta (metano,irl) in
2298 let initgoal = Inference.BasicProof goalterm, [], ty in
2299 let env = (metasenv, context, CicUniv.empty_ugraph) in
2300 let equalities = simplify_equalities env (equalities@library_equalities) in
2303 (fun tbl eq -> Indexing.index tbl eq)
2304 Indexing.empty equalities
2306 let newmeta,(newproof,newmetasenv, newty) = Indexing.demodulation_goal
2307 maxm (metasenv,context,CicUniv.empty_ugraph) table initgoal
2309 if newmeta != maxm then
2311 let opengoal = Cic.Meta(maxm,irl) in
2313 Inference.build_proof_term ~noproof:opengoal newproof in
2314 let extended_metasenv = (maxm,context,newty)::metasenv in
2315 let extended_status =
2316 (curi,extended_metasenv,pbo,pty),goal in
2317 let (status,newgoals) =
2318 ProofEngineTypes.apply_tactic
2319 (PrimitiveTactics.apply_tac ~term:proofterm)
2321 (status,maxm::newgoals)
2323 else if newty = ty then
2324 raise (ProofEngineTypes.Fail (lazy "no progress"))
2325 else ProofEngineTypes.apply_tactic
2326 (ReductionTactics.simpl_tac ~pattern)
2330 let demodulate_tac ~dbd ~pattern =
2331 ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~pattern)