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 (* applies equality to goal to see if the goal can be closed *)
851 let apply_equality_to_goal env equality goal =
852 let module C = Cic in
853 let module HL = HelmLibraryObjects in
854 let module I = Inference in
855 let metasenv, context, ugraph = env in
856 let _, proof, (ty, left, right, _), metas, args = equality in
858 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
859 let gproof, gmetas, gterm = goal in
862 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
863 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
865 let subst, metasenv', _ =
866 let menv = metasenv @ metas @ gmetas in
867 Inference.unification menv context eqterm gterm ugraph
871 | I.BasicProof t -> I.BasicProof (CicMetaSubst.apply_subst subst t)
872 | I.ProofBlock (s, uri, nt, t, pe, p) ->
873 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
877 let rec repl = function
878 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
879 | I.NoProof -> newproof
880 | I.BasicProof p -> newproof
881 | I.SubProof (t, i, p) -> I.SubProof (t, i, repl p)
886 true, subst, newgproof
887 with CicUnification.UnificationFailure _ ->
893 let new_meta metasenv =
894 let m = CicMkImplicit.new_meta metasenv [] in
896 while !maxmeta <= m do incr maxmeta done;
901 (* applies a theorem or an equality to goal, returning a list of subgoals or
902 an indication of failure *)
903 let apply_to_goal env theorems ?passive active goal =
904 let metasenv, context, ugraph = env in
905 let proof, metas, term = goal in
908 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
909 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
912 CicMkImplicit.identity_relocation_list_for_metavariable context in
913 let proof', newmeta =
914 let rec get_meta = function
915 | SubProof (t, i, p) ->
916 let t', i' = get_meta p in
917 if i' = -1 then t, i else t', i'
918 | ProofGoalBlock (_, p) -> get_meta p
919 | _ -> Cic.Implicit None, -1
921 let p, m = get_meta proof in
923 let n = new_meta (metasenv @ metas) in
928 let metasenv = (newmeta, context, term)::metasenv @ metas in
929 let bit = new_meta metasenv, context, term in
930 let metasenv' = bit::metasenv in
931 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
933 let rec aux = function
935 | (theorem, thmty, _)::tl ->
937 let subst, (newproof, newgoals) =
938 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
940 if newgoals = [] then
941 let _, _, p, _ = newproof in
943 let rec repl = function
944 | Inference.ProofGoalBlock (_, gp) ->
945 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
946 | Inference.NoProof -> Inference.BasicProof p
947 | Inference.BasicProof _ -> Inference.BasicProof p
948 | Inference.SubProof (t, i, p2) ->
949 Inference.SubProof (t, i, repl p2)
955 let subst = List.filter (fun (i, _) -> i = m) subst in
956 `Ok (subst, [newp, metas, term])
958 let _, menv, p, _ = newproof in
960 CicMkImplicit.identity_relocation_list_for_metavariable context
965 let _, _, ty = CicUtil.lookup_meta i menv in
967 let rec gp = function
968 | SubProof (t, i, p) ->
969 SubProof (t, i, gp p)
970 | ProofGoalBlock (sp1, sp2) ->
971 ProofGoalBlock (sp1, gp sp2)
974 SubProof (p, i, BasicProof (Cic.Meta (i, irl)))
975 | ProofSymBlock (s, sp) ->
976 ProofSymBlock (s, gp sp)
977 | ProofBlock (s, u, nt, t, pe, sp) ->
978 ProofBlock (s, u, nt, t, pe, gp sp)
986 let w, m = weight_of_term t in
987 w + 2 * (List.length m)
990 (fun (_, _, t1) (_, _, t2) ->
991 Pervasives.compare (weight t1) (weight t2))
997 | `No -> `GoOn ([subst, goals])
998 | `GoOn sl -> `GoOn ((subst, goals)::sl)
999 with ProofEngineTypes.Fail msg ->
1003 if Inference.term_is_equality term then
1004 let rec appleq_a = function
1005 | [] -> false, [], []
1006 | (Positive, equality)::tl ->
1007 let ok, s, newproof = apply_equality_to_goal env equality goal in
1008 if ok then true, s, [newproof, metas, term] else appleq_a tl
1009 | _::tl -> appleq_a tl
1011 let rec appleq_p = function
1012 | [] -> false, [], []
1014 let ok, s, newproof = apply_equality_to_goal env equality goal in
1015 if ok then true, s, [newproof, metas, term] else appleq_p tl
1017 let al, _ = active in
1019 | None -> appleq_a al
1020 | Some (_, (pl, _), _) ->
1021 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1025 if r = true then `Ok (s, l) else aux theorems
1029 (* sorts a conjunction of goals in order to detect earlier if it is
1030 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1031 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1034 (fun (_, e1, g1) (_, e2, g2) ->
1036 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1038 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1042 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1047 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1051 if prop1 = 0 && prop2 = 0 then
1052 let e1 = if Inference.term_is_equality g1 then 0 else 1
1053 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1063 let is_meta_closed goals =
1064 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1068 (* applies a series of theorems/equalities to a conjunction of goals *)
1069 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1070 let aux (goal, r) tl =
1071 let propagate_subst subst (proof, metas, term) =
1072 let rec repl = function
1073 | NoProof -> NoProof
1075 BasicProof (CicMetaSubst.apply_subst subst t)
1076 | ProofGoalBlock (p, pb) ->
1077 let pb' = repl pb in
1078 ProofGoalBlock (p, pb')
1079 | SubProof (t, i, p) ->
1080 let t' = CicMetaSubst.apply_subst subst t in
1083 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1084 | ProofBlock (s, u, nty, t, pe, p) ->
1085 ProofBlock (subst @ s, u, nty, t, pe, p)
1086 in (repl proof, metas, term)
1088 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1090 | `No -> `No (depth, goals)
1095 let tl = List.map (propagate_subst s) tl in
1096 sort_goal_conj env (depth+1, gl @ tl)) sl
1099 | `Ok (subst, gl) ->
1103 let p, _, _ = List.hd gl in
1105 let rec repl = function
1106 | SubProof (_, _, p) -> repl p
1107 | ProofGoalBlock (p1, p2) ->
1108 ProofGoalBlock (repl p1, repl p2)
1111 build_proof_term (repl p)
1114 let rec get_meta = function
1115 | SubProof (_, i, p) ->
1116 let i' = get_meta p in
1117 if i' = -1 then i else i'
1118 (* max i (get_meta p) *)
1119 | ProofGoalBlock (_, p) -> get_meta p
1125 let _, (context, _, _) = List.hd subst in
1126 [i, (context, subproof, Cic.Implicit None)]
1128 let tl = List.map (propagate_subst subst) tl in
1129 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1133 if depth > !maxdepth || (List.length goals) > !maxwidth then
1136 let rec search_best res = function
1139 let r = apply_to_goal env theorems ?passive active goal in
1141 | `Ok _ -> (goal, r)
1142 | `No -> search_best res tl
1146 | _, `Ok _ -> assert false
1149 if (List.length l) < (List.length l2) then goal, r else res
1151 search_best newres tl
1153 let hd = List.hd goals in
1154 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1158 | _, _ -> search_best res (List.tl goals)
1160 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1162 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1163 (List.length (snd conj)) < (List.length goals)->
1164 apply_to_goal_conj env theorems ?passive active conj
1170 module OrderedGoals = struct
1171 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1178 else let r = (List.length l1) - (List.length l2) in
1184 (fun (_, _, t1) (_, _, t2) ->
1185 let r = Pervasives.compare t1 t2 in
1194 module GoalsSet = Set.Make(OrderedGoals);;
1197 exception SearchSpaceOver;;
1202 let apply_to_goals env is_passive_empty theorems active goals =
1203 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1204 let add_to set goals =
1205 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1207 let rec aux set = function
1209 debug_print (lazy "HERE!!!");
1210 if is_passive_empty then raise SearchSpaceOver else false, set
1212 let res = apply_to_goal_conj env theorems active goals in
1218 | (d, (p, _, t)::_) -> d, p, t
1223 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1224 d (string_of_proof p) (CicPp.ppterm t)))
1226 true, GoalsSet.singleton newgoals
1228 let set' = add_to set (goals::tl) in
1229 let set' = add_to set' newgoals in
1234 let n = List.length goals in
1235 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1236 let goals = GoalsSet.elements goals in
1237 debug_print (lazy "\n\tapply_to_goals end\n");
1238 let m = List.length goals in
1239 if m = n && is_passive_empty then
1240 raise SearchSpaceOver
1247 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1248 work that well yet...) *)
1249 let sort_passive_goals goals =
1251 (fun (d1, l1) (d2, l2) ->
1253 and r2 = (List.length l1) - (List.length l2) in
1254 let foldfun ht (_, _, t) =
1255 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1258 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1259 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1260 in let r3 = m1 - m2 in
1262 else if r2 <> 0 then r2
1264 (* let _, _, g1 = List.hd l1 *)
1265 (* and _, _, g2 = List.hd l2 in *)
1266 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1267 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1268 (* in let r4 = e1 - e2 in *)
1269 (* if r4 <> 0 then r3 else r1) *)
1274 let print_goals goals =
1281 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1283 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1287 (* tries to prove the first conjunction in goals with applications of
1288 theorems/equalities, returning new sub-goals or an indication of success *)
1289 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1290 let theorems, _ = theorems in
1291 let a_goals, p_goals = goals in
1292 let goal = List.hd a_goals in
1293 let not_in_active gl =
1297 if (List.length gl) = (List.length gl') then
1298 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1304 let res = apply_to_goal_conj env theorems ?passive active goal in
1307 true, ([newgoals], [])
1309 false, (a_goals, p_goals)
1314 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1317 let p_goals = newgoals @ p_goals in
1318 let p_goals = sort_passive_goals p_goals in
1319 false, (a_goals, p_goals)
1325 let apply_theorem_to_goals env theorems active goals =
1326 let a_goals, p_goals = goals in
1327 let theorem = List.hd (fst theorems) in
1328 let theorems = [theorem] in
1329 let rec aux p = function
1330 | [] -> false, ([], p)
1332 let res = apply_to_goal_conj env theorems active goal in
1334 | `Ok newgoals -> true, ([newgoals], [])
1336 | `GoOn newgoals -> aux (newgoals @ p) tl
1338 let ok, (a, p) = aux p_goals a_goals in
1344 (fun (d1, l1) (d2, l2) ->
1347 else let r = (List.length l1) - (List.length l2) in
1353 (fun (_, _, t1) (_, _, t2) ->
1354 let r = Pervasives.compare t1 t2 in
1355 if r <> 0 then (res := r; true) else false) l1 l2
1359 ok, (a_goals, p_goals)
1363 (* given-clause algorithm with lazy reduction strategy *)
1364 let rec given_clause dbd env goals theorems passive active =
1365 let goals = simplify_goals env goals active in
1366 let ok, goals = activate_goal goals in
1367 (* let theorems = simplify_theorems env theorems active in *)
1369 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1372 match (fst goals) with
1373 | (_, [proof, _, _])::_ -> Some proof
1376 ParamodulationSuccess (proof, env)
1378 given_clause_aux dbd env goals theorems passive active
1380 (* let ok', theorems = activate_theorem theorems in *)
1381 let ok', theorems = false, theorems in
1383 let ok, goals = apply_theorem_to_goals env theorems active goals in
1386 match (fst goals) with
1387 | (_, [proof, _, _])::_ -> Some proof
1390 ParamodulationSuccess (proof, env)
1392 given_clause_aux dbd env goals theorems passive active
1394 if (passive_is_empty passive) then ParamodulationFailure
1395 else given_clause_aux dbd env goals theorems passive active
1397 and given_clause_aux dbd env goals theorems passive active =
1398 let time1 = Unix.gettimeofday () in
1400 let selection_estimate = get_selection_estimate () in
1401 let kept = size_of_passive passive in
1403 if !time_limit = 0. || !processed_clauses = 0 then
1405 else if !elapsed_time > !time_limit then (
1406 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1407 !time_limit !elapsed_time));
1409 ) else if kept > selection_estimate then (
1411 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1412 "(kept: %d, selection_estimate: %d)\n")
1413 kept selection_estimate));
1414 prune_passive selection_estimate active passive
1419 let time2 = Unix.gettimeofday () in
1420 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1422 kept_clauses := (size_of_passive passive) + (size_of_active active);
1423 match passive_is_empty passive with
1424 | true -> (* ParamodulationFailure *)
1425 given_clause dbd env goals theorems passive active
1427 let (sign, current), passive = select env (fst goals) passive active in
1428 let time1 = Unix.gettimeofday () in
1429 let res = forward_simplify env (sign, current) ~passive active in
1430 let time2 = Unix.gettimeofday () in
1431 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1434 given_clause dbd env goals theorems passive active
1435 | Some (sign, current) ->
1436 if (sign = Negative) && (is_identity env current) then (
1438 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1439 (string_of_equality ~env current)));
1440 let _, proof, _, _, _ = current in
1441 ParamodulationSuccess (Some proof, env)
1444 (lazy "\n================================================");
1445 debug_print (lazy (Printf.sprintf "selected: %s %s"
1446 (string_of_sign sign)
1447 (string_of_equality ~env current)));
1449 let t1 = Unix.gettimeofday () in
1450 let new' = infer env sign current active in
1451 let t2 = Unix.gettimeofday () in
1452 infer_time := !infer_time +. (t2 -. t1);
1454 let res, goal' = contains_empty env new' in
1458 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1461 ParamodulationSuccess (proof, env)
1463 let t1 = Unix.gettimeofday () in
1464 let new' = forward_simplify_new env new' active in
1465 let t2 = Unix.gettimeofday () in
1467 forward_simpl_new_time :=
1468 !forward_simpl_new_time +. (t2 -. t1)
1472 | Negative -> active
1474 let t1 = Unix.gettimeofday () in
1475 let active, _, newa, _ =
1476 backward_simplify env ([], [current]) active
1478 let t2 = Unix.gettimeofday () in
1479 backward_simpl_time :=
1480 !backward_simpl_time +. (t2 -. t1);
1484 let al, tbl = active in
1485 let nn = List.map (fun e -> Negative, e) n in
1490 Indexing.index tbl e)
1495 match contains_empty env new' with
1498 let al, tbl = active in
1500 | Negative -> (sign, current)::al, tbl
1502 al @ [(sign, current)], Indexing.index tbl current
1504 let passive = add_to_passive passive new' in
1505 given_clause dbd env goals theorems passive active
1510 let _, proof, _, _, _ = goal in Some proof
1513 ParamodulationSuccess (proof, env)
1518 (** given-clause algorithm with full reduction strategy *)
1519 let rec given_clause_fullred dbd env goals theorems passive active =
1520 let goals = simplify_goals env goals ~passive active in
1521 let ok, goals = activate_goal goals in
1522 (* let theorems = simplify_theorems env theorems ~passive active in *)
1527 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1528 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1529 (* let current = List.hd (fst goals) in *)
1530 (* let p, _, t = List.hd (snd current) in *)
1533 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1534 (* (CicPp.ppterm t) (string_of_proof p))); *)
1537 apply_goal_to_theorems dbd env theorems ~passive active goals
1541 match (fst goals) with
1542 | (_, [proof, _, _])::_ -> Some proof
1545 ParamodulationSuccess (proof, env)
1547 given_clause_fullred_aux dbd env goals theorems passive active
1549 (* let ok', theorems = activate_theorem theorems in *)
1551 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1554 (* match (fst goals) with *)
1555 (* | (_, [proof, _, _])::_ -> Some proof *)
1556 (* | _ -> assert false *)
1558 (* ParamodulationSuccess (proof, env) *)
1560 (* given_clause_fullred_aux env goals theorems passive active *)
1562 if (passive_is_empty passive) then ParamodulationFailure
1563 else given_clause_fullred_aux dbd env goals theorems passive active
1565 and given_clause_fullred_aux dbd env goals theorems passive active =
1566 let time1 = Unix.gettimeofday () in
1568 let selection_estimate = get_selection_estimate () in
1569 let kept = size_of_passive passive in
1571 if !time_limit = 0. || !processed_clauses = 0 then
1573 else if !elapsed_time > !time_limit then (
1574 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1575 !time_limit !elapsed_time));
1577 ) else if kept > selection_estimate then (
1579 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1580 "(kept: %d, selection_estimate: %d)\n")
1581 kept selection_estimate));
1582 prune_passive selection_estimate active passive
1587 let time2 = Unix.gettimeofday () in
1588 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1590 kept_clauses := (size_of_passive passive) + (size_of_active active);
1591 match passive_is_empty passive with
1592 | true -> (* ParamodulationFailure *)
1593 given_clause_fullred dbd env goals theorems passive active
1595 let (sign, current), passive = select env (fst goals) passive active in
1596 let time1 = Unix.gettimeofday () in
1597 let res = forward_simplify env (sign, current) ~passive active in
1598 let time2 = Unix.gettimeofday () in
1599 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1602 given_clause_fullred dbd env goals theorems passive active
1603 | Some (sign, current) ->
1604 if (sign = Negative) && (is_identity env current) then (
1606 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1607 (string_of_equality ~env current)));
1608 let _, proof, _, _, _ = current in
1609 ParamodulationSuccess (Some proof, env)
1612 (lazy "\n================================================");
1613 debug_print (lazy (Printf.sprintf "selected: %s %s"
1614 (string_of_sign sign)
1615 (string_of_equality ~env current)));
1617 let t1 = Unix.gettimeofday () in
1618 let new' = infer env sign current active in
1619 let t2 = Unix.gettimeofday () in
1620 infer_time := !infer_time +. (t2 -. t1);
1623 if is_identity env current then active
1625 let al, tbl = active in
1627 | Negative -> (sign, current)::al, tbl
1629 al @ [(sign, current)], Indexing.index tbl current
1631 let rec simplify new' active passive =
1632 let t1 = Unix.gettimeofday () in
1633 let new' = forward_simplify_new env new' ~passive active in
1634 let t2 = Unix.gettimeofday () in
1635 forward_simpl_new_time :=
1636 !forward_simpl_new_time +. (t2 -. t1);
1637 let t1 = Unix.gettimeofday () in
1638 let active, passive, newa, retained =
1639 backward_simplify env new' ~passive active in
1640 let t2 = Unix.gettimeofday () in
1641 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1642 match newa, retained with
1643 | None, None -> active, passive, new'
1645 | None, Some (n, p) ->
1646 let nn, np = new' in
1647 simplify (nn @ n, np @ p) active passive
1648 | Some (n, p), Some (rn, rp) ->
1649 let nn, np = new' in
1650 simplify (nn @ n @ rn, np @ p @ rp) active passive
1652 let active, passive, new' = simplify new' active passive in
1654 let k = size_of_passive passive in
1655 if k < (kept - 1) then
1656 processed_clauses := !processed_clauses + (kept - 1 - k);
1661 (Printf.sprintf "active:\n%s\n"
1664 (fun (s, e) -> (string_of_sign s) ^ " " ^
1665 (string_of_equality ~env e))
1673 (Printf.sprintf "new':\n%s\n"
1676 (fun e -> "Negative " ^
1677 (string_of_equality ~env e)) neg) @
1679 (fun e -> "Positive " ^
1680 (string_of_equality ~env e)) pos)))))
1682 match contains_empty env new' with
1684 let passive = add_to_passive passive new' in
1685 given_clause_fullred dbd env goals theorems passive active
1689 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1692 ParamodulationSuccess (proof, env)
1697 let rec saturate_equations env goal accept_fun passive active =
1698 elapsed_time := Unix.gettimeofday () -. !start_time;
1699 if !elapsed_time > !time_limit then
1702 let (sign, current), passive = select env [1, [goal]] passive active in
1703 let res = forward_simplify env (sign, current) ~passive active in
1706 saturate_equations env goal accept_fun passive active
1707 | Some (sign, current) ->
1708 assert (sign = Positive);
1710 (lazy "\n================================================");
1711 debug_print (lazy (Printf.sprintf "selected: %s %s"
1712 (string_of_sign sign)
1713 (string_of_equality ~env current)));
1714 let new' = infer env sign current active in
1716 if is_identity env current then active
1718 let al, tbl = active in
1719 al @ [(sign, current)], Indexing.index tbl current
1721 let rec simplify new' active passive =
1722 let new' = forward_simplify_new env new' ~passive active in
1723 let active, passive, newa, retained =
1724 backward_simplify env new' ~passive active in
1725 match newa, retained with
1726 | None, None -> active, passive, new'
1728 | None, Some (n, p) ->
1729 let nn, np = new' in
1730 simplify (nn @ n, np @ p) active passive
1731 | Some (n, p), Some (rn, rp) ->
1732 let nn, np = new' in
1733 simplify (nn @ n @ rn, np @ p @ rp) active passive
1735 let active, passive, new' = simplify new' active passive in
1739 (Printf.sprintf "active:\n%s\n"
1742 (fun (s, e) -> (string_of_sign s) ^ " " ^
1743 (string_of_equality ~env e))
1751 (Printf.sprintf "new':\n%s\n"
1754 (fun e -> "Negative " ^
1755 (string_of_equality ~env e)) neg) @
1757 (fun e -> "Positive " ^
1758 (string_of_equality ~env e)) pos)))))
1760 let new' = match new' with _, pos -> [], List.filter accept_fun pos in
1761 let passive = add_to_passive passive new' in
1762 saturate_equations env goal accept_fun passive active
1768 let main dbd full term metasenv ugraph =
1769 let module C = Cic in
1770 let module T = CicTypeChecker in
1771 let module PET = ProofEngineTypes in
1772 let module PP = CicPp in
1773 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1774 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1775 let proof, goals = status in
1776 let goal' = List.nth goals 0 in
1777 let _, metasenv, meta_proof, _ = proof in
1778 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1779 let eq_indexes, equalities, maxm = find_equalities context proof in
1780 let lib_eq_uris, library_equalities, maxm =
1781 find_library_equalities dbd context (proof, goal') (maxm+2)
1783 let library_equalities = List.map snd library_equalities in
1784 maxmeta := maxm+2; (* TODO ugly!! *)
1785 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1786 let new_meta_goal, metasenv, type_of_goal =
1787 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1790 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1791 Cic.Meta (maxm+1, irl),
1792 (maxm+1, context, ty)::metasenv,
1795 let env = (metasenv, context, ugraph) in
1796 let t1 = Unix.gettimeofday () in
1799 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1800 let context_hyp = find_context_hypotheses env eq_indexes in
1801 context_hyp @ theorems, []
1804 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1805 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1807 let t = CicUtil.term_of_uri refl_equal in
1808 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1811 let t2 = Unix.gettimeofday () in
1814 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1819 "Theorems:\n-------------------------------------\n%s\n"
1824 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1828 let goal = Inference.BasicProof new_meta_goal, [], goal in
1830 let equalities = equalities @ library_equalities in
1833 (Printf.sprintf "equalities:\n%s\n"
1835 (List.map string_of_equality equalities))));
1836 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1837 let rec simpl e others others_simpl =
1838 let active = others @ others_simpl in
1841 (fun t (_, e) -> Indexing.index t e)
1842 Indexing.empty active
1844 let res = forward_simplify env e (active, tbl) in
1848 | None -> simpl hd tl others_simpl
1849 | Some e -> simpl hd tl (e::others_simpl)
1853 | None -> others_simpl
1854 | Some e -> e::others_simpl
1857 match equalities with
1860 let others = List.map (fun e -> (Positive, e)) tl in
1862 List.rev (List.map snd (simpl (Positive, hd) others []))
1866 (Printf.sprintf "equalities AFTER:\n%s\n"
1868 (List.map string_of_equality res))));
1871 let active = make_active () in
1872 let passive = make_passive [] equalities in
1873 Printf.printf "\ncurrent goal: %s\n"
1874 (let _, _, g = goal in CicPp.ppterm g);
1875 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1876 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1877 Printf.printf "\nequalities:\n%s\n"
1880 (string_of_equality ~env) equalities));
1881 (* (equalities @ library_equalities))); *)
1882 print_endline "--------------------------------------------------";
1883 let start = Unix.gettimeofday () in
1884 print_endline "GO!";
1885 start_time := Unix.gettimeofday ();
1887 let goals = make_goals goal in
1888 (if !use_fullred then given_clause_fullred else given_clause)
1889 dbd env goals theorems passive active
1891 let finish = Unix.gettimeofday () in
1894 | ParamodulationFailure ->
1895 Printf.printf "NO proof found! :-(\n\n"
1896 | ParamodulationSuccess (Some proof, env) ->
1897 let proof = Inference.build_proof_term proof in
1898 Printf.printf "OK, found a proof!\n";
1899 (* REMEMBER: we have to instantiate meta_proof, we should use
1900 apply the "apply" tactic to proof and status
1902 let names = names_of_context context in
1903 print_endline (PP.pp proof names);
1906 (fun m (_, _, _, menv, _) -> m @ menv) metasenv equalities
1911 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1913 print_endline (string_of_float (finish -. start));
1915 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1916 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1918 (fst (CicReduction.are_convertible
1919 context type_of_goal ty ug)));
1921 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1922 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1923 print_endline (string_of_float (finish -. start));*)
1927 | ParamodulationSuccess (None, env) ->
1928 Printf.printf "Success, but no proof?!?\n\n"
1930 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1931 "forward_simpl_new_time: %.9f\n" ^^
1932 "backward_simpl_time: %.9f\n")
1933 !infer_time !forward_simpl_time !forward_simpl_new_time
1934 !backward_simpl_time;
1935 Printf.printf "passive_maintainance_time: %.9f\n"
1936 !passive_maintainance_time;
1937 Printf.printf " successful unification/matching time: %.9f\n"
1938 !Indexing.match_unif_time_ok;
1939 Printf.printf " failed unification/matching time: %.9f\n"
1940 !Indexing.match_unif_time_no;
1941 Printf.printf " indexing retrieval time: %.9f\n"
1942 !Indexing.indexing_retrieval_time;
1943 Printf.printf " demodulate_term.build_newtarget_time: %.9f\n"
1944 !Indexing.build_newtarget_time;
1945 Printf.printf "derived %d clauses, kept %d clauses.\n"
1946 !derived_clauses !kept_clauses;
1949 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1955 let default_depth = !maxdepth
1956 and default_width = !maxwidth;;
1960 symbols_counter := 0;
1961 weight_age_counter := !weight_age_ratio;
1962 processed_clauses := 0;
1965 maximal_retained_equality := None;
1967 forward_simpl_time := 0.;
1968 forward_simpl_new_time := 0.;
1969 backward_simpl_time := 0.;
1970 passive_maintainance_time := 0.;
1971 derived_clauses := 0;
1976 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1977 let module C = Cic in
1979 Indexing.init_index ();
1982 let proof, goal = status in
1984 let uri, metasenv, meta_proof, term_to_prove = proof in
1985 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1986 let eq_indexes, equalities, maxm = find_equalities context proof in
1987 let new_meta_goal, metasenv, type_of_goal =
1989 CicMkImplicit.identity_relocation_list_for_metavariable context in
1990 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1992 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
1993 Cic.Meta (maxm+1, irl),
1994 (maxm+1, context, ty)::metasenv,
1997 let ugraph = CicUniv.empty_ugraph in
1998 let env = (metasenv, context, ugraph) in
1999 let goal = Inference.BasicProof new_meta_goal, [], goal in
2001 let t1 = Unix.gettimeofday () in
2002 let lib_eq_uris, library_equalities, maxm =
2003 find_library_equalities dbd context (proof, goal') (maxm+2)
2005 let library_equalities = List.map snd library_equalities in
2006 let t2 = Unix.gettimeofday () in
2009 let equalities = equalities @ library_equalities in
2012 (Printf.sprintf "equalities:\n%s\n"
2014 (List.map string_of_equality equalities))));
2015 debug_print (lazy "SIMPLYFYING EQUALITIES...");
2016 let rec simpl e others others_simpl =
2017 let active = others @ others_simpl in
2020 (fun t (_, e) -> Indexing.index t e)
2021 Indexing.empty active
2023 let res = forward_simplify env e (active, tbl) in
2027 | None -> simpl hd tl others_simpl
2028 | Some e -> simpl hd tl (e::others_simpl)
2032 | None -> others_simpl
2033 | Some e -> e::others_simpl
2036 match equalities with
2039 let others = List.map (fun e -> (Positive, e)) tl in
2041 List.rev (List.map snd (simpl (Positive, hd) others []))
2045 (Printf.sprintf "equalities AFTER:\n%s\n"
2047 (List.map string_of_equality res))));
2052 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
2053 let t1 = Unix.gettimeofday () in
2056 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
2057 let context_hyp = find_context_hypotheses env eq_indexes in
2058 context_hyp @ thms, []
2061 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
2062 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
2064 let t = CicUtil.term_of_uri refl_equal in
2065 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
2068 let t2 = Unix.gettimeofday () in
2073 "Theorems:\n-------------------------------------\n%s\n"
2078 "Term: %s, type: %s"
2079 (CicPp.ppterm t) (CicPp.ppterm ty))
2083 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
2085 let active = make_active () in
2086 let passive = make_passive [] equalities in
2087 let start = Unix.gettimeofday () in
2089 let goals = make_goals goal in
2090 given_clause_fullred dbd env goals theorems passive active
2092 let finish = Unix.gettimeofday () in
2093 (res, finish -. start)
2096 | ParamodulationSuccess (Some proof, env) ->
2097 debug_print (lazy "OK, found a proof!");
2098 let proof = Inference.build_proof_term proof in
2099 let names = names_of_context context in
2102 match new_meta_goal with
2103 | C.Meta (i, _) -> i | _ -> assert false
2105 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2110 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2112 debug_print (lazy (CicPp.pp proof [](* names *)));
2116 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2117 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2119 (fst (CicReduction.are_convertible
2120 context type_of_goal ty ug)))));
2121 let equality_for_replace i t1 =
2123 | C.Meta (n, _) -> n = i
2127 ProofEngineReduction.replace
2128 ~equality:equality_for_replace
2129 ~what:[goal'] ~with_what:[proof]
2134 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2135 (match uri with Some uri -> UriManager.string_of_uri uri
2137 (print_metasenv newmetasenv)
2138 (CicPp.pp real_proof [](* names *))
2139 (CicPp.pp term_to_prove names)));
2140 ((uri, newmetasenv, real_proof, term_to_prove), [])
2141 with CicTypeChecker.TypeCheckerFailure _ ->
2142 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2143 debug_print (lazy (CicPp.pp proof names));
2144 raise (ProofEngineTypes.Fail
2145 (lazy "Found a proof, but it doesn't typecheck"))
2147 debug_print (lazy (Printf.sprintf "\nTIME NEEDED: %.9f" time));
2150 raise (ProofEngineTypes.Fail (lazy "NO proof found"))
2153 (* dummy function called within matita to trigger linkage *)
2157 let retrieve_and_print dbd term metasenv ugraph =
2158 let module C = Cic in
2159 let module T = CicTypeChecker in
2160 let module PET = ProofEngineTypes in
2161 let module PP = CicPp in
2162 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2163 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2164 let proof, goals = status in
2165 let goal' = List.nth goals 0 in
2166 let uri, metasenv, meta_proof, term_to_prove = proof in
2167 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2168 let eq_indexes, equalities, maxm = find_equalities context proof in
2169 let new_meta_goal, metasenv, type_of_goal =
2171 CicMkImplicit.identity_relocation_list_for_metavariable context in
2172 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2174 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
2175 Cic.Meta (maxm+1, irl),
2176 (maxm+1, context, ty)::metasenv,
2179 let ugraph = CicUniv.empty_ugraph in
2180 let env = (metasenv, context, ugraph) in
2181 let t1 = Unix.gettimeofday () in
2182 let lib_eq_uris, library_equalities, maxm =
2183 find_library_equalities dbd context (proof, goal') (maxm+2) in
2184 let t2 = Unix.gettimeofday () in
2186 let equalities = (* equalities @ *) library_equalities in
2189 (Printf.sprintf "\n\nequalities:\n%s\n"
2193 (* Printf.sprintf "%s: %s" *)
2194 (UriManager.string_of_uri u)
2195 (* (string_of_equality e) *)
2198 debug_print (lazy "SIMPLYFYING EQUALITIES...");
2199 let rec simpl e others others_simpl =
2201 let active = List.map (fun (u, e) -> (Positive, e))
2202 (others @ others_simpl) in
2205 (fun t (_, e) -> Indexing.index t e)
2206 Indexing.empty active
2208 let res = forward_simplify env (Positive, e) (active, tbl) in
2212 | None -> simpl hd tl others_simpl
2213 | Some e -> simpl hd tl ((u, (snd e))::others_simpl)
2217 | None -> others_simpl
2218 | Some e -> (u, (snd e))::others_simpl
2222 match equalities with
2225 let others = tl in (* List.map (fun e -> (Positive, e)) tl in *)
2227 List.rev (simpl (*(Positive,*) hd others [])
2231 (Printf.sprintf "\nequalities AFTER:\n%s\n"
2235 Printf.sprintf "%s: %s"
2236 (UriManager.string_of_uri u)
2237 (string_of_equality e)
2243 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)))
2247 let main_demod_equalities dbd term metasenv ugraph =
2248 let module C = Cic in
2249 let module T = CicTypeChecker in
2250 let module PET = ProofEngineTypes in
2251 let module PP = CicPp in
2252 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
2253 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
2254 let proof, goals = status in
2255 let goal' = List.nth goals 0 in
2256 let _, metasenv, meta_proof, _ = proof in
2257 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
2258 let eq_indexes, equalities, maxm = find_equalities context proof in
2259 let lib_eq_uris, library_equalities, maxm =
2260 find_library_equalities dbd context (proof, goal') (maxm+2)
2262 let library_equalities = List.map snd library_equalities in
2263 maxmeta := maxm+2; (* TODO ugly!! *)
2264 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
2265 let new_meta_goal, metasenv, type_of_goal =
2266 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
2269 (Printf.sprintf "\n\nTRYING TO INFER EQUALITIES MATCHING: %s\n\n"
2270 (CicPp.ppterm ty)));
2271 Cic.Meta (maxm+1, irl),
2272 (maxm+1, context, ty)::metasenv,
2275 let env = (metasenv, context, ugraph) in
2277 let goal = Inference.BasicProof new_meta_goal, [], goal in
2279 let equalities = equalities @ library_equalities in
2282 (Printf.sprintf "equalities:\n%s\n"
2284 (List.map string_of_equality equalities))));
2285 debug_print (lazy "SIMPLYFYING EQUALITIES...");
2286 let rec simpl e others others_simpl =
2287 let active = others @ others_simpl in
2290 (fun t (_, e) -> Indexing.index t e)
2291 Indexing.empty active
2293 let res = forward_simplify env e (active, tbl) in
2297 | None -> simpl hd tl others_simpl
2298 | Some e -> simpl hd tl (e::others_simpl)
2302 | None -> others_simpl
2303 | Some e -> e::others_simpl
2306 match equalities with
2309 let others = List.map (fun e -> (Positive, e)) tl in
2311 List.rev (List.map snd (simpl (Positive, hd) others []))
2315 (Printf.sprintf "equalities AFTER:\n%s\n"
2317 (List.map string_of_equality res))));
2320 let active = make_active () in
2321 let passive = make_passive [] equalities in
2322 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
2323 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
2324 Printf.printf "\nequalities:\n%s\n"
2327 (string_of_equality ~env) equalities));
2328 print_endline "--------------------------------------------------";
2329 print_endline "GO!";
2330 start_time := Unix.gettimeofday ();
2331 if !time_limit < 1. then time_limit := 60.;
2333 saturate_equations env goal (fun e -> true) passive active
2337 List.fold_left (fun s e -> EqualitySet.add e s)
2338 EqualitySet.empty equalities
2341 if not (EqualitySet.mem e initial) then EqualitySet.add e s else s
2346 | (n, _), (p, _), _ ->
2347 EqualitySet.elements (List.fold_left addfun EqualitySet.empty p)
2350 let l = List.map snd (fst ra) in
2351 EqualitySet.elements (List.fold_left addfun EqualitySet.empty l)
2353 Printf.printf "\n\nRESULTS:\nActive:\n%s\n\nPassive:\n%s\n"
2354 (String.concat "\n" (List.map (string_of_equality ~env) active))
2355 (* (String.concat "\n"
2356 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) active)) *)
2357 (* (String.concat "\n" (List.map (string_of_equality ~env) passive)); *)
2359 (List.map (fun e -> CicPp.ppterm (term_of_equality e)) passive));
2363 debug_print (lazy ("EXCEPTION: " ^ (Printexc.to_string e)))