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/.
30 (* set to false to disable paramodulation inside auto_tac *)
31 let connect_to_auto = true;;
34 (* profiling statistics... *)
35 let infer_time = ref 0.;;
36 let forward_simpl_time = ref 0.;;
37 let forward_simpl_new_time = ref 0.;;
38 let backward_simpl_time = ref 0.;;
39 let passive_maintainance_time = ref 0.;;
41 (* limited-resource-strategy related globals *)
42 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
43 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
44 let start_time = ref 0.;; (* time at which the execution started *)
45 let elapsed_time = ref 0.;;
46 (* let maximal_weight = ref None;; *)
47 let maximal_retained_equality = ref None;;
49 (* equality-selection related globals *)
50 let use_fullred = ref true;;
51 let weight_age_ratio = ref (* 5 *) 4;; (* settable by the user *)
52 let weight_age_counter = ref !weight_age_ratio;;
53 let symbols_ratio = ref (* 0 *) 3;;
54 let symbols_counter = ref 0;;
56 (* non-recursive Knuth-Bendix term ordering by default *)
57 Utils.compare_terms := Utils.nonrec_kbo;;
60 let derived_clauses = ref 0;;
61 let kept_clauses = ref 0;;
63 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
66 (* varbiables controlling the search-space *)
67 let maxdepth = ref 3;;
68 let maxwidth = ref 3;;
72 | ParamodulationFailure
73 | ParamodulationSuccess of Inference.proof option * environment
76 type goal = proof * Cic.metasenv * Cic.term;;
78 type theorem = Cic.term * Cic.term * Cic.metasenv;;
81 let symbols_of_equality ((_, _, (_, left, right, _), _, _) as equality) =
82 let m1 = symbols_of_term left in
87 let c = TermMap.find k res in
88 TermMap.add k (c+v) res
91 (symbols_of_term right) m1
97 module OrderedEquality = struct
98 type t = Inference.equality
100 let compare eq1 eq2 =
101 match meta_convertibility_eq eq1 eq2 with
104 let w1, _, (ty, left, right, _), _, a = eq1
105 and w2, _, (ty', left', right', _), _, a' = eq2 in
106 match Pervasives.compare w1 w2 with
108 let res = (List.length a) - (List.length a') in
109 if res <> 0 then res else (
111 let res = Pervasives.compare (List.hd a) (List.hd a') in
112 if res <> 0 then res else Pervasives.compare eq1 eq2
113 with Failure "hd" -> Pervasives.compare eq1 eq2
118 module EqualitySet = Set.Make(OrderedEquality);;
122 selects one equality from passive. The selection strategy is a combination
123 of weight, age and goal-similarity
125 let select env goals passive (active, _) =
126 processed_clauses := !processed_clauses + 1;
128 match (List.rev goals) with (_, goal::_)::_ -> goal | _ -> assert false
130 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
132 List.filter (fun e -> e <> eq) l
134 if !weight_age_ratio > 0 then
135 weight_age_counter := !weight_age_counter - 1;
136 match !weight_age_counter with
138 weight_age_counter := !weight_age_ratio;
139 match neg_list, pos_list with
141 (* Negatives aren't indexed, no need to remove them... *)
143 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
146 Indexing.remove_index passive_table hd
149 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
150 | _, _ -> assert false
152 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) -> (
153 symbols_counter := !symbols_counter - 1;
154 let cardinality map =
155 TermMap.fold (fun k v res -> res + v) map 0
158 let _, _, term = goal in
161 let card = cardinality symbols in
162 let foldfun k v (r1, r2) =
163 if TermMap.mem k symbols then
164 let c = TermMap.find k symbols in
165 let c1 = abs (c - v) in
171 let f equality (i, e) =
173 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
175 let c = others + (abs (common - card)) in
176 if c < i then (c, equality)
179 let e1 = EqualitySet.min_elt pos_set in
182 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
184 (others + (abs (common - card))), e1
186 let _, current = EqualitySet.fold f pos_set initial in
188 Indexing.remove_index passive_table current
192 (remove current pos_list, EqualitySet.remove current pos_set),
196 symbols_counter := !symbols_ratio;
197 let set_selection set = EqualitySet.min_elt set in
198 if EqualitySet.is_empty neg_set then
199 let current = set_selection pos_set in
202 (remove current pos_list, EqualitySet.remove current pos_set),
203 Indexing.remove_index passive_table current
205 (Positive, current), passive
207 let current = set_selection neg_set in
209 (remove current neg_list, EqualitySet.remove current neg_set),
213 (Negative, current), passive
217 (* initializes the passive set of equalities *)
218 let make_passive neg pos =
219 let set_of equalities =
220 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
223 List.fold_left (fun tbl e -> Indexing.index tbl e)
224 (Indexing.empty_table ()) pos
233 [], Indexing.empty_table ()
237 (* adds to passive a list of equalities: new_neg is a list of negative
238 equalities, new_pos a list of positive equalities *)
239 let add_to_passive passive (new_neg, new_pos) =
240 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
241 let ok set equality = not (EqualitySet.mem equality set) in
242 let neg = List.filter (ok neg_set) new_neg
243 and pos = List.filter (ok pos_set) new_pos in
245 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
247 let add set equalities =
248 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
250 (neg @ neg_list, add neg_set neg),
251 (pos_list @ pos, add pos_set pos),
256 let passive_is_empty = function
257 | ([], _), ([], _), _ -> true
262 let size_of_passive ((_, ns), (_, ps), _) =
263 (EqualitySet.cardinal ns) + (EqualitySet.cardinal ps)
267 let size_of_active (active_list, _) =
268 List.length active_list
272 (* removes from passive equalities that are estimated impossible to activate
273 within the current time limit *)
274 let prune_passive howmany (active, _) passive =
275 let (nl, ns), (pl, ps), tbl = passive in
276 let howmany = float_of_int howmany
277 and ratio = float_of_int !weight_age_ratio in
280 int_of_float (if t -. v < 0.5 then t else v)
282 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
283 and in_age = round (howmany /. (ratio +. 1.)) in
285 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
288 | (Negative, e)::_ ->
289 let symbols = symbols_of_equality e in
290 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
294 let counter = ref !symbols_ratio in
295 let rec pickw w ns ps =
297 if not (EqualitySet.is_empty ns) then
298 let e = EqualitySet.min_elt ns in
299 let ns', ps = pickw (w-1) (EqualitySet.remove e ns) ps in
300 EqualitySet.add e ns', ps
301 else if !counter > 0 then
303 counter := !counter - 1;
304 if !counter = 0 then counter := !symbols_ratio
308 let e = EqualitySet.min_elt ps in
309 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
310 ns, EqualitySet.add e ps'
312 let foldfun k v (r1, r2) =
313 if TermMap.mem k symbols then
314 let c = TermMap.find k symbols in
315 let c1 = abs (c - v) in
321 let f equality (i, e) =
323 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
325 let c = others + (abs (common - card)) in
326 if c < i then (c, equality)
329 let e1 = EqualitySet.min_elt ps in
332 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
334 (others + (abs (common - card))), e1
336 let _, e = EqualitySet.fold f ps initial in
337 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
338 ns, EqualitySet.add e ps'
340 let e = EqualitySet.min_elt ps in
341 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
342 ns, EqualitySet.add e ps'
344 EqualitySet.empty, EqualitySet.empty
346 let ns, ps = pickw in_weight ns ps in
347 let rec picka w s l =
351 | hd::tl when not (EqualitySet.mem hd s) ->
352 let w, s, l = picka (w-1) s tl in
353 w, EqualitySet.add hd s, hd::l
355 let w, s, l = picka w s tl in
360 let in_age, ns, nl = picka in_age ns nl in
361 let _, ps, pl = picka in_age ps pl in
362 if not (EqualitySet.is_empty ps) then
363 maximal_retained_equality := Some (EqualitySet.max_elt ps);
366 (fun e tbl -> Indexing.index tbl e) ps (Indexing.empty_table ())
368 (nl, ns), (pl, ps), tbl
372 (** inference of new equalities between current and some in active *)
373 let infer env sign current (active_list, active_table) =
374 let new_neg, new_pos =
378 Indexing.superposition_left !maxmeta env active_table current in
383 Indexing.superposition_right !maxmeta env active_table current in
385 let rec infer_positive table = function
387 | (Negative, equality)::tl ->
389 Indexing.superposition_left !maxmeta env table equality in
391 let neg, pos = infer_positive table tl in
393 | (Positive, equality)::tl ->
395 Indexing.superposition_right !maxmeta env table equality in
397 let neg, pos = infer_positive table tl in
400 let curr_table = Indexing.index (Indexing.empty_table ()) current in
401 let neg, pos = infer_positive curr_table active_list in
404 derived_clauses := !derived_clauses + (List.length new_neg) +
405 (List.length new_pos);
406 match !maximal_retained_equality with
407 | None -> new_neg, new_pos
409 (* if we have a maximal_retained_equality, we can discard all equalities
410 "greater" than it, as they will never be reached... An equality is
411 greater than maximal_retained_equality if it is bigger
412 wrt. OrderedEquality.compare and it is less similar than
413 maximal_retained_equality to the current goal *)
415 match active_list with
416 | (Negative, e)::_ ->
417 let symbols = symbols_of_equality e in
418 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
425 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
428 if OrderedEquality.compare e eq <= 0 then
431 let foldfun k v (r1, r2) =
432 if TermMap.mem k symbols then
433 let c = TermMap.find k symbols in
434 let c1 = abs (c - v) in
442 TermMap.fold foldfun (symbols_of_equality eq) (0, 0) in
443 others + (abs (common - card))
446 TermMap.fold foldfun (symbols_of_equality e) (0, 0) in
447 let c = others + (abs (common - card)) in
448 if c < initial then true else false
450 List.filter filterfun new_pos
456 let contains_empty env (negative, positive) =
457 let metasenv, context, ugraph = env in
461 (fun (w, proof, (ty, left, right, ordering), m, a) ->
462 fst (CicReduction.are_convertible context left right ugraph))
471 (** simplifies current using active and passive *)
472 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
473 let pl, passive_table =
476 | Some ((pn, _), (pp, _), pt) ->
477 let pn = List.map (fun e -> (Negative, e)) pn
478 and pp = List.map (fun e -> (Positive, e)) pp in
481 let all = if pl = [] then active_list else active_list @ pl in
483 let demodulate table current =
484 let newmeta, newcurrent =
485 Indexing.demodulation_equality !maxmeta env table sign current in
487 if is_identity env newcurrent then
488 if sign = Negative then Some (sign, newcurrent)
491 Some (sign, newcurrent)
494 let res = demodulate active_table current in
497 | Some (sign, newcurrent) ->
498 match passive_table with
500 | Some passive_table -> demodulate passive_table newcurrent
504 | Some (Negative, c) ->
507 (fun (s, eq) -> s = Negative && meta_convertibility_eq eq c)
510 if ok then res else None
511 | Some (Positive, c) ->
512 if Indexing.in_index active_table c then
515 match passive_table with
517 | Some passive_table ->
518 if Indexing.in_index passive_table c then None
522 type fs_time_info_t = {
523 mutable build_all: float;
524 mutable demodulate: float;
525 mutable subsumption: float;
528 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
531 (** simplifies new using active and passive *)
532 let forward_simplify_new env (new_neg, new_pos) ?passive active =
533 let t1 = Unix.gettimeofday () in
535 let active_list, active_table = active in
536 let pl, passive_table =
539 | Some ((pn, _), (pp, _), pt) ->
540 let pn = List.map (fun e -> (Negative, e)) pn
541 and pp = List.map (fun e -> (Positive, e)) pp in
544 let all = active_list @ pl in
546 let t2 = Unix.gettimeofday () in
547 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
549 let demodulate sign table target =
550 let newmeta, newtarget =
551 Indexing.demodulation_equality !maxmeta env table sign target in
555 let t1 = Unix.gettimeofday () in
557 let new_neg, new_pos =
558 let new_neg = List.map (demodulate Negative active_table) new_neg
559 and new_pos = List.map (demodulate Positive active_table) new_pos in
560 match passive_table with
561 | None -> new_neg, new_pos
562 | Some passive_table ->
563 List.map (demodulate Negative passive_table) new_neg,
564 List.map (demodulate Positive passive_table) new_pos
567 let t2 = Unix.gettimeofday () in
568 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
573 if not (Inference.is_identity env e) then
574 if EqualitySet.mem e s then s
575 else EqualitySet.add e s
577 EqualitySet.empty new_pos
579 let new_pos = EqualitySet.elements new_pos_set in
582 match passive_table with
584 (fun e -> not (fst (Indexing.subsumption env active_table e)))
585 | Some passive_table ->
586 (fun e -> not ((fst (Indexing.subsumption env active_table e)) ||
587 (fst (Indexing.subsumption env passive_table e))))
589 (* let t1 = Unix.gettimeofday () in *)
590 (* let t2 = Unix.gettimeofday () in *)
591 (* fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1); *)
593 match passive_table with
595 (fun e -> not (Indexing.in_index active_table e))
596 | Some passive_table ->
598 not ((Indexing.in_index active_table e) ||
599 (Indexing.in_index passive_table e)))
601 new_neg, List.filter is_duplicate new_pos
605 (** simplifies active usign new *)
606 let backward_simplify_active env new_pos new_table min_weight active =
607 let active_list, active_table = active in
608 let active_list, newa =
610 (fun (s, equality) (res, newn) ->
611 let ew, _, _, _, _ = equality in
612 if ew < min_weight then
613 (s, equality)::res, newn
615 match forward_simplify env (s, equality) (new_pos, new_table) with
625 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
629 (fun (s, eq) (res, tbl) ->
630 if List.mem (s, eq) res then
632 else if (is_identity env eq) || (find eq res) then (
636 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
637 active_list ([], Indexing.empty_table ()),
639 (fun (s, eq) (n, p) ->
640 if (s <> Negative) && (is_identity env eq) then (
643 if s = Negative then eq::n, p
648 | [], [] -> active, None
649 | _ -> active, Some newa
653 (** simplifies passive using new *)
654 let backward_simplify_passive env new_pos new_table min_weight passive =
655 let (nl, ns), (pl, ps), passive_table = passive in
656 let f sign equality (resl, ress, newn) =
657 let ew, _, _, _, _ = equality in
658 if ew < min_weight then
659 equality::resl, ress, newn
661 match forward_simplify env (sign, equality) (new_pos, new_table) with
662 | None -> resl, EqualitySet.remove equality ress, newn
665 equality::resl, ress, newn
667 let ress = EqualitySet.remove equality ress in
670 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
671 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
674 (fun tbl e -> Indexing.index tbl e) (Indexing.empty_table ()) pl
676 match newn, newp with
677 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
678 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
682 let backward_simplify env new' ?passive active =
683 let new_pos, new_table, min_weight =
686 let ew, _, _, _, _ = e in
687 (Positive, e)::l, Indexing.index t e, min ew w)
688 ([], Indexing.empty_table (), 1000000) (snd new')
691 backward_simplify_active env new_pos new_table min_weight active in
694 active, (make_passive [] []), newa, None
697 backward_simplify_passive env new_pos new_table min_weight passive in
698 active, passive, newa, newp
702 (* returns an estimation of how many equalities in passive can be activated
703 within the current time limit *)
704 let get_selection_estimate () =
705 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
706 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
708 ceil ((float_of_int !processed_clauses) *.
709 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
713 (** initializes the set of goals *)
714 let make_goals goal =
716 and passive = [0, [goal]] in
721 (** initializes the set of theorems *)
722 let make_theorems theorems =
727 let activate_goal (active, passive) =
729 | goal_conj::tl -> true, (goal_conj::active, tl)
730 | [] -> false, (active, passive)
734 let activate_theorem (active, passive) =
736 | theorem::tl -> true, (theorem::active, tl)
737 | [] -> false, (active, passive)
741 (** simplifies a goal with equalities in active and passive *)
742 let simplify_goal env goal ?passive (active_list, active_table) =
743 let pl, passive_table =
746 | Some ((pn, _), (pp, _), pt) ->
747 let pn = List.map (fun e -> (Negative, e)) pn
748 and pp = List.map (fun e -> (Positive, e)) pp in
751 let all = if pl = [] then active_list else active_list @ pl in
753 let demodulate table goal =
754 let newmeta, newgoal =
755 Indexing.demodulation_goal !maxmeta env table goal in
757 goal != newgoal, newgoal
760 match passive_table with
761 | None -> demodulate active_table goal
762 | Some passive_table ->
763 let changed, goal = demodulate active_table goal in
764 let changed', goal = demodulate passive_table goal in
765 (changed || changed'), goal
771 let simplify_goals env goals ?passive active =
772 let a_goals, p_goals = goals in
777 List.map (fun g -> snd (simplify_goal env g ?passive active)) gl in
783 (fun (a, p) (d, gl) ->
784 let changed = ref false in
788 let c, g = simplify_goal env g ?passive active in
789 changed := !changed || c; g) gl in
790 if !changed then (a, (d, gl)::p) else ((d, gl)::a, p))
791 ([], p_goals) a_goals
797 let simplify_theorems env theorems ?passive (active_list, active_table) =
798 let pl, passive_table =
801 | Some ((pn, _), (pp, _), pt) ->
802 let pn = List.map (fun e -> (Negative, e)) pn
803 and pp = List.map (fun e -> (Positive, e)) pp in
806 let all = if pl = [] then active_list else active_list @ pl in
807 let a_theorems, p_theorems = theorems in
808 let demodulate table theorem =
809 let newmeta, newthm =
810 Indexing.demodulation_theorem !maxmeta env table theorem in
812 theorem != newthm, newthm
814 let foldfun table (a, p) theorem =
815 let changed, theorem = demodulate table theorem in
816 if changed then (a, theorem::p) else (theorem::a, p)
818 let mapfun table theorem = snd (demodulate table theorem) in
819 match passive_table with
821 let p_theorems = List.map (mapfun active_table) p_theorems in
822 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems
823 | Some passive_table ->
824 let p_theorems = List.map (mapfun active_table) p_theorems in
825 let p_theorems, a_theorems =
826 List.fold_left (foldfun active_table) ([], p_theorems) a_theorems in
827 let p_theorems = List.map (mapfun passive_table) p_theorems in
828 List.fold_left (foldfun passive_table) ([], p_theorems) a_theorems
832 (* applies equality to goal to see if the goal can be closed *)
833 let apply_equality_to_goal env equality goal =
834 let module C = Cic in
835 let module HL = HelmLibraryObjects in
836 let module I = Inference in
837 let metasenv, context, ugraph = env in
838 let _, proof, (ty, left, right, _), metas, args = equality in
840 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
841 let gproof, gmetas, gterm = goal in
844 (* (Printf.sprintf "APPLY EQUALITY TO GOAL: %s, %s" *)
845 (* (string_of_equality equality) (CicPp.ppterm gterm))); *)
847 let subst, metasenv', _ =
848 let menv = metasenv @ metas @ gmetas in
849 Inference.unification menv context eqterm gterm ugraph
853 | I.BasicProof t -> I.BasicProof (CicMetaSubst.apply_subst subst t)
854 | I.ProofBlock (s, uri, nt, t, pe, p) ->
855 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
859 let rec repl = function
860 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
861 | I.NoProof -> newproof
862 | I.BasicProof p -> newproof
863 | I.SubProof (t, i, p) -> I.SubProof (t, i, repl p)
868 true, subst, newgproof
869 with CicUnification.UnificationFailure _ ->
875 let new_meta metasenv =
876 let m = CicMkImplicit.new_meta metasenv [] in
878 while !maxmeta <= m do incr maxmeta done;
883 (* applies a theorem or an equality to goal, returning a list of subgoals or
884 an indication of failure *)
885 let apply_to_goal env theorems ?passive active goal =
886 let metasenv, context, ugraph = env in
887 let proof, metas, term = goal in
890 (* (Printf.sprintf "apply_to_goal with goal: %s" *)
891 (* (\* (string_of_proof proof) *\)(CicPp.ppterm term))); *)
894 CicMkImplicit.identity_relocation_list_for_metavariable context in
895 let proof', newmeta =
896 let rec get_meta = function
897 | SubProof (t, i, p) ->
898 let t', i' = get_meta p in
899 if i' = -1 then t, i else t', i'
900 | ProofGoalBlock (_, p) -> get_meta p
901 | _ -> Cic.Implicit None, -1
903 let p, m = get_meta proof in
905 let n = new_meta (metasenv @ metas) in
910 let metasenv = (newmeta, context, term)::metasenv @ metas in
911 let bit = new_meta metasenv, context, term in
912 let metasenv' = bit::metasenv in
913 ((None, metasenv', Cic.Meta (newmeta, irl), term), newmeta)
915 let rec aux = function
917 | (theorem, thmty, _)::tl ->
919 let subst, (newproof, newgoals) =
920 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
922 if newgoals = [] then
923 let _, _, p, _ = newproof in
925 let rec repl = function
926 | Inference.ProofGoalBlock (_, gp) ->
927 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
928 | Inference.NoProof -> Inference.BasicProof p
929 | Inference.BasicProof _ -> Inference.BasicProof p
930 | Inference.SubProof (t, i, p2) ->
931 Inference.SubProof (t, i, repl p2)
937 let subst = List.filter (fun (i, _) -> i = m) subst in
938 `Ok (subst, [newp, metas, term])
940 let _, menv, p, _ = newproof in
942 CicMkImplicit.identity_relocation_list_for_metavariable context
947 let _, _, ty = CicUtil.lookup_meta i menv in
949 let rec gp = function
950 | SubProof (t, i, p) ->
951 SubProof (t, i, gp p)
952 | ProofGoalBlock (sp1, sp2) ->
953 ProofGoalBlock (sp1, gp sp2)
956 SubProof (p, i, BasicProof (Cic.Meta (i, irl)))
957 | ProofSymBlock (s, sp) ->
958 ProofSymBlock (s, gp sp)
959 | ProofBlock (s, u, nt, t, pe, sp) ->
960 ProofBlock (s, u, nt, t, pe, gp sp)
968 let w, m = weight_of_term t in
969 w + 2 * (List.length m)
972 (fun (_, _, t1) (_, _, t2) ->
973 Pervasives.compare (weight t1) (weight t2))
979 | `No -> `GoOn ([subst, goals])
980 | `GoOn sl -> `GoOn ((subst, goals)::sl)
981 with ProofEngineTypes.Fail msg ->
985 if Inference.term_is_equality term then
986 let rec appleq_a = function
987 | [] -> false, [], []
988 | (Positive, equality)::tl ->
989 let ok, s, newproof = apply_equality_to_goal env equality goal in
990 if ok then true, s, [newproof, metas, term] else appleq_a tl
991 | _::tl -> appleq_a tl
993 let rec appleq_p = function
994 | [] -> false, [], []
996 let ok, s, newproof = apply_equality_to_goal env equality goal in
997 if ok then true, s, [newproof, metas, term] else appleq_p tl
999 let al, _ = active in
1001 | None -> appleq_a al
1002 | Some (_, (pl, _), _) ->
1003 let r, s, l = appleq_a al in if r then r, s, l else appleq_p pl
1007 if r = true then `Ok (s, l) else aux theorems
1011 (* sorts a conjunction of goals in order to detect earlier if it is
1012 unsatisfiable. Non-predicate goals are placed at the end of the list *)
1013 let sort_goal_conj (metasenv, context, ugraph) (depth, gl) =
1016 (fun (_, e1, g1) (_, e2, g2) ->
1018 CicTypeChecker.type_of_aux' (e1 @ metasenv) context g1 ugraph
1020 CicTypeChecker.type_of_aux' (e2 @ metasenv) context g2 ugraph
1024 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty1 ugraph
1029 CicReduction.are_convertible context (Cic.Sort Cic.Prop) ty2 ugraph
1033 if prop1 = 0 && prop2 = 0 then
1034 let e1 = if Inference.term_is_equality g1 then 0 else 1
1035 and e2 = if Inference.term_is_equality g2 then 0 else 1 in
1045 let is_meta_closed goals =
1046 List.for_all (fun (_, _, g) -> CicUtil.is_meta_closed g) goals
1050 (* applies a series of theorems/equalities to a conjunction of goals *)
1051 let rec apply_to_goal_conj env theorems ?passive active (depth, goals) =
1052 let aux (goal, r) tl =
1053 let propagate_subst subst (proof, metas, term) =
1054 let rec repl = function
1055 | NoProof -> NoProof
1057 BasicProof (CicMetaSubst.apply_subst subst t)
1058 | ProofGoalBlock (p, pb) ->
1059 let pb' = repl pb in
1060 ProofGoalBlock (p, pb')
1061 | SubProof (t, i, p) ->
1062 let t' = CicMetaSubst.apply_subst subst t in
1065 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1066 | ProofBlock (s, u, nty, t, pe, p) ->
1067 ProofBlock (subst @ s, u, nty, t, pe, p)
1068 in (repl proof, metas, term)
1070 (* let r = apply_to_goal env theorems ?passive active goal in *) (
1072 | `No -> `No (depth, goals)
1077 let tl = List.map (propagate_subst s) tl in
1078 sort_goal_conj env (depth+1, gl @ tl)) sl
1081 | `Ok (subst, gl) ->
1085 let p, _, _ = List.hd gl in
1087 let rec repl = function
1088 | SubProof (_, _, p) -> repl p
1089 | ProofGoalBlock (p1, p2) ->
1090 ProofGoalBlock (repl p1, repl p2)
1093 build_proof_term (repl p)
1096 let rec get_meta = function
1097 | SubProof (_, i, p) ->
1098 let i' = get_meta p in
1099 if i' = -1 then i else i'
1100 (* max i (get_meta p) *)
1101 | ProofGoalBlock (_, p) -> get_meta p
1107 let _, (context, _, _) = List.hd subst in
1108 [i, (context, subproof, Cic.Implicit None)]
1110 let tl = List.map (propagate_subst subst) tl in
1111 let conj = sort_goal_conj env (depth(* +1 *), tl) in
1115 if depth > !maxdepth || (List.length goals) > !maxwidth then
1118 let rec search_best res = function
1121 let r = apply_to_goal env theorems ?passive active goal in
1123 | `Ok _ -> (goal, r)
1124 | `No -> search_best res tl
1128 | _, `Ok _ -> assert false
1131 if (List.length l) < (List.length l2) then goal, r else res
1133 search_best newres tl
1135 let hd = List.hd goals in
1136 let res = hd, (apply_to_goal env theorems ?passive active hd) in
1140 | _, _ -> search_best res (List.tl goals)
1142 let res = aux best (List.filter (fun g -> g != (fst best)) goals) in
1144 | `GoOn ([conj]) when is_meta_closed (snd conj) &&
1145 (List.length (snd conj)) < (List.length goals)->
1146 apply_to_goal_conj env theorems ?passive active conj
1152 module OrderedGoals = struct
1153 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1160 else let r = (List.length l1) - (List.length l2) in
1166 (fun (_, _, t1) (_, _, t2) ->
1167 let r = Pervasives.compare t1 t2 in
1176 module GoalsSet = Set.Make(OrderedGoals);;
1179 exception SearchSpaceOver;;
1184 let apply_to_goals env is_passive_empty theorems active goals =
1185 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1186 let add_to set goals =
1187 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1189 let rec aux set = function
1191 debug_print (lazy "HERE!!!");
1192 if is_passive_empty then raise SearchSpaceOver else false, set
1194 let res = apply_to_goal_conj env theorems active goals in
1200 | (d, (p, _, t)::_) -> d, p, t
1205 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1206 d (string_of_proof p) (CicPp.ppterm t)))
1208 true, GoalsSet.singleton newgoals
1210 let set' = add_to set (goals::tl) in
1211 let set' = add_to set' newgoals in
1216 let n = List.length goals in
1217 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1218 let goals = GoalsSet.elements goals in
1219 debug_print (lazy "\n\tapply_to_goals end\n");
1220 let m = List.length goals in
1221 if m = n && is_passive_empty then
1222 raise SearchSpaceOver
1229 (* sorts the list of passive goals to minimize the search for a proof (doesn't
1230 work that well yet...) *)
1231 let sort_passive_goals goals =
1233 (fun (d1, l1) (d2, l2) ->
1235 and r2 = (List.length l1) - (List.length l2) in
1236 let foldfun ht (_, _, t) =
1237 let _ = List.map (fun i -> Hashtbl.replace ht i 1) (metas_of_term t)
1240 let m1 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l1)
1241 and m2 = Hashtbl.length (List.fold_left foldfun (Hashtbl.create 3) l2)
1242 in let r3 = m1 - m2 in
1244 else if r2 <> 0 then r2
1246 (* let _, _, g1 = List.hd l1 *)
1247 (* and _, _, g2 = List.hd l2 in *)
1248 (* let e1 = if Inference.term_is_equality g1 then 0 else 1 *)
1249 (* and e2 = if Inference.term_is_equality g2 then 0 else 1 *)
1250 (* in let r4 = e1 - e2 in *)
1251 (* if r4 <> 0 then r3 else r1) *)
1256 let print_goals goals =
1263 (* (string_of_proof p) ^ ", " ^ *) (CicPp.ppterm t)) gl
1265 Printf.sprintf "%d: %s" d (String.concat "; " gl')) goals))
1269 (* tries to prove the first conjunction in goals with applications of
1270 theorems/equalities, returning new sub-goals or an indication of success *)
1271 let apply_goal_to_theorems dbd env theorems ?passive active goals =
1272 let theorems, _ = theorems in
1273 let a_goals, p_goals = goals in
1274 let goal = List.hd a_goals in
1275 let not_in_active gl =
1279 if (List.length gl) = (List.length gl') then
1280 List.for_all2 (fun (_, _, g1) (_, _, g2) -> g1 = g2) gl gl'
1286 let res = apply_to_goal_conj env theorems ?passive active goal in
1289 true, ([newgoals], [])
1291 false, (a_goals, p_goals)
1296 (d <= !maxdepth) && (List.length gl) <= !maxwidth &&
1299 let p_goals = newgoals @ p_goals in
1300 let p_goals = sort_passive_goals p_goals in
1301 false, (a_goals, p_goals)
1307 let apply_theorem_to_goals env theorems active goals =
1308 let a_goals, p_goals = goals in
1309 let theorem = List.hd (fst theorems) in
1310 let theorems = [theorem] in
1311 let rec aux p = function
1312 | [] -> false, ([], p)
1314 let res = apply_to_goal_conj env theorems active goal in
1316 | `Ok newgoals -> true, ([newgoals], [])
1318 | `GoOn newgoals -> aux (newgoals @ p) tl
1320 let ok, (a, p) = aux p_goals a_goals in
1326 (fun (d1, l1) (d2, l2) ->
1329 else let r = (List.length l1) - (List.length l2) in
1335 (fun (_, _, t1) (_, _, t2) ->
1336 let r = Pervasives.compare t1 t2 in
1337 if r <> 0 then (res := r; true) else false) l1 l2
1341 ok, (a_goals, p_goals)
1345 (* given-clause algorithm with lazy reduction strategy *)
1346 let rec given_clause dbd env goals theorems passive active =
1347 let goals = simplify_goals env goals active in
1348 let ok, goals = activate_goal goals in
1349 (* let theorems = simplify_theorems env theorems active in *)
1351 let ok, goals = apply_goal_to_theorems dbd env theorems active goals in
1354 match (fst goals) with
1355 | (_, [proof, _, _])::_ -> Some proof
1358 ParamodulationSuccess (proof, env)
1360 given_clause_aux dbd env goals theorems passive active
1362 (* let ok', theorems = activate_theorem theorems in *)
1363 let ok', theorems = false, theorems in
1365 let ok, goals = apply_theorem_to_goals env theorems active goals in
1368 match (fst goals) with
1369 | (_, [proof, _, _])::_ -> Some proof
1372 ParamodulationSuccess (proof, env)
1374 given_clause_aux dbd env goals theorems passive active
1376 if (passive_is_empty passive) then ParamodulationFailure
1377 else given_clause_aux dbd env goals theorems passive active
1379 and given_clause_aux dbd env goals theorems passive active =
1380 let time1 = Unix.gettimeofday () in
1382 let selection_estimate = get_selection_estimate () in
1383 let kept = size_of_passive passive in
1385 if !time_limit = 0. || !processed_clauses = 0 then
1387 else if !elapsed_time > !time_limit then (
1388 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1389 !time_limit !elapsed_time));
1391 ) else if kept > selection_estimate then (
1393 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1394 "(kept: %d, selection_estimate: %d)\n")
1395 kept selection_estimate));
1396 prune_passive selection_estimate active passive
1401 let time2 = Unix.gettimeofday () in
1402 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1404 kept_clauses := (size_of_passive passive) + (size_of_active active);
1405 match passive_is_empty passive with
1406 | true -> (* ParamodulationFailure *)
1407 given_clause dbd env goals theorems passive active
1409 let (sign, current), passive = select env (fst goals) passive active in
1410 let time1 = Unix.gettimeofday () in
1411 let res = forward_simplify env (sign, current) ~passive active in
1412 let time2 = Unix.gettimeofday () in
1413 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1416 given_clause dbd env goals theorems passive active
1417 | Some (sign, current) ->
1418 if (sign = Negative) && (is_identity env current) then (
1420 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1421 (string_of_equality ~env current)));
1422 let _, proof, _, _, _ = current in
1423 ParamodulationSuccess (Some proof, env)
1426 (lazy "\n================================================");
1427 debug_print (lazy (Printf.sprintf "selected: %s %s"
1428 (string_of_sign sign)
1429 (string_of_equality ~env current)));
1431 let t1 = Unix.gettimeofday () in
1432 let new' = infer env sign current active in
1433 let t2 = Unix.gettimeofday () in
1434 infer_time := !infer_time +. (t2 -. t1);
1436 let res, goal' = contains_empty env new' in
1440 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1443 ParamodulationSuccess (proof, env)
1445 let t1 = Unix.gettimeofday () in
1446 let new' = forward_simplify_new env new' active in
1447 let t2 = Unix.gettimeofday () in
1449 forward_simpl_new_time :=
1450 !forward_simpl_new_time +. (t2 -. t1)
1454 | Negative -> active
1456 let t1 = Unix.gettimeofday () in
1457 let active, _, newa, _ =
1458 backward_simplify env ([], [current]) active
1460 let t2 = Unix.gettimeofday () in
1461 backward_simpl_time :=
1462 !backward_simpl_time +. (t2 -. t1);
1466 let al, tbl = active in
1467 let nn = List.map (fun e -> Negative, e) n in
1472 Indexing.index tbl e)
1477 match contains_empty env new' with
1480 let al, tbl = active in
1482 | Negative -> (sign, current)::al, tbl
1484 al @ [(sign, current)], Indexing.index tbl current
1486 let passive = add_to_passive passive new' in
1487 let (_, ns), (_, ps), _ = passive in
1488 given_clause dbd env goals theorems passive active
1493 let _, proof, _, _, _ = goal in Some proof
1496 ParamodulationSuccess (proof, env)
1501 (** given-clause algorithm with full reduction strategy *)
1502 let rec given_clause_fullred dbd env goals theorems passive active =
1503 let goals = simplify_goals env goals ~passive active in
1504 let ok, goals = activate_goal goals in
1505 (* let theorems = simplify_theorems env theorems ~passive active in *)
1510 (* (Printf.sprintf "\ngoals = \nactive\n%s\npassive\n%s\n" *)
1511 (* (print_goals (fst goals)) (print_goals (snd goals)))); *)
1512 (* let current = List.hd (fst goals) in *)
1513 (* let p, _, t = List.hd (snd current) in *)
1516 (* (Printf.sprintf "goal activated:\n%s\n%s\n" *)
1517 (* (CicPp.ppterm t) (string_of_proof p))); *)
1520 apply_goal_to_theorems dbd env theorems ~passive active goals
1524 match (fst goals) with
1525 | (_, [proof, _, _])::_ -> Some proof
1528 ParamodulationSuccess (proof, env)
1530 given_clause_fullred_aux dbd env goals theorems passive active
1532 (* let ok', theorems = activate_theorem theorems in *)
1534 (* let ok, goals = apply_theorem_to_goals env theorems active goals in *)
1537 (* match (fst goals) with *)
1538 (* | (_, [proof, _, _])::_ -> Some proof *)
1539 (* | _ -> assert false *)
1541 (* ParamodulationSuccess (proof, env) *)
1543 (* given_clause_fullred_aux env goals theorems passive active *)
1545 if (passive_is_empty passive) then ParamodulationFailure
1546 else given_clause_fullred_aux dbd env goals theorems passive active
1548 and given_clause_fullred_aux dbd env goals theorems passive active =
1549 let time1 = Unix.gettimeofday () in
1551 let selection_estimate = get_selection_estimate () in
1552 let kept = size_of_passive passive in
1554 if !time_limit = 0. || !processed_clauses = 0 then
1556 else if !elapsed_time > !time_limit then (
1557 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1558 !time_limit !elapsed_time));
1560 ) else if kept > selection_estimate then (
1562 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1563 "(kept: %d, selection_estimate: %d)\n")
1564 kept selection_estimate));
1565 prune_passive selection_estimate active passive
1570 let time2 = Unix.gettimeofday () in
1571 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1573 kept_clauses := (size_of_passive passive) + (size_of_active active);
1574 match passive_is_empty passive with
1575 | true -> (* ParamodulationFailure *)
1576 given_clause_fullred dbd env goals theorems passive active
1578 let (sign, current), passive = select env (fst goals) passive active in
1579 let time1 = Unix.gettimeofday () in
1580 let res = forward_simplify env (sign, current) ~passive active in
1581 let time2 = Unix.gettimeofday () in
1582 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1585 given_clause_fullred dbd env goals theorems passive active
1586 | Some (sign, current) ->
1587 if (sign = Negative) && (is_identity env current) then (
1589 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1590 (string_of_equality ~env current)));
1591 let _, proof, _, _, _ = current in
1592 ParamodulationSuccess (Some proof, env)
1595 (lazy "\n================================================");
1596 debug_print (lazy (Printf.sprintf "selected: %s %s"
1597 (string_of_sign sign)
1598 (string_of_equality ~env current)));
1600 let t1 = Unix.gettimeofday () in
1601 let new' = infer env sign current active in
1602 let t2 = Unix.gettimeofday () in
1603 infer_time := !infer_time +. (t2 -. t1);
1606 if is_identity env current then active
1608 let al, tbl = active in
1610 | Negative -> (sign, current)::al, tbl
1612 al @ [(sign, current)], Indexing.index tbl current
1614 let rec simplify new' active passive =
1615 let t1 = Unix.gettimeofday () in
1616 let new' = forward_simplify_new env new' ~passive active in
1617 let t2 = Unix.gettimeofday () in
1618 forward_simpl_new_time :=
1619 !forward_simpl_new_time +. (t2 -. t1);
1620 let t1 = Unix.gettimeofday () in
1621 let active, passive, newa, retained =
1622 backward_simplify env new' ~passive active in
1623 let t2 = Unix.gettimeofday () in
1624 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
1625 match newa, retained with
1626 | None, None -> active, passive, new'
1628 | None, Some (n, p) ->
1629 let nn, np = new' in
1630 simplify (nn @ n, np @ p) active passive
1631 | Some (n, p), Some (rn, rp) ->
1632 let nn, np = new' in
1633 simplify (nn @ n @ rn, np @ p @ rp) active passive
1635 let active, passive, new' = simplify new' active passive in
1637 let k = size_of_passive passive in
1638 if k < (kept - 1) then
1639 processed_clauses := !processed_clauses + (kept - 1 - k);
1644 (Printf.sprintf "active:\n%s\n"
1647 (fun (s, e) -> (string_of_sign s) ^ " " ^
1648 (string_of_equality ~env e))
1656 (Printf.sprintf "new':\n%s\n"
1659 (fun e -> "Negative " ^
1660 (string_of_equality ~env e)) neg) @
1662 (fun e -> "Positive " ^
1663 (string_of_equality ~env e)) pos)))))
1665 match contains_empty env new' with
1667 let passive = add_to_passive passive new' in
1668 given_clause_fullred dbd env goals theorems passive active
1672 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1675 ParamodulationSuccess (proof, env)
1681 let main dbd full term metasenv ugraph =
1682 let module C = Cic in
1683 let module T = CicTypeChecker in
1684 let module PET = ProofEngineTypes in
1685 let module PP = CicPp in
1686 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1687 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1688 let proof, goals = status in
1689 let goal' = List.nth goals 0 in
1690 let _, metasenv, meta_proof, _ = proof in
1691 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1692 let eq_indexes, equalities, maxm = find_equalities context proof in
1693 let lib_eq_uris, library_equalities, maxm =
1694 find_library_equalities dbd context (proof, goal') (maxm+2)
1696 maxmeta := maxm+2; (* TODO ugly!! *)
1697 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1698 let new_meta_goal, metasenv, type_of_goal =
1699 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1702 (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n\n" (CicPp.ppterm ty)));
1703 Cic.Meta (maxm+1, irl),
1704 (maxm+1, context, ty)::metasenv,
1707 let env = (metasenv, context, ugraph) in
1708 let t1 = Unix.gettimeofday () in
1711 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1712 let context_hyp = find_context_hypotheses env eq_indexes in
1713 context_hyp @ theorems, []
1716 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1717 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1719 let t = CicUtil.term_of_uri refl_equal in
1720 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1723 let t2 = Unix.gettimeofday () in
1726 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1731 "Theorems:\n-------------------------------------\n%s\n"
1736 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1740 let goal = Inference.BasicProof new_meta_goal, [], goal in
1742 let equalities = equalities @ library_equalities in
1745 (Printf.sprintf "equalities:\n%s\n"
1747 (List.map string_of_equality equalities))));
1748 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1749 let rec simpl e others others_simpl =
1750 let active = others @ others_simpl in
1753 (fun t (_, e) -> Indexing.index t e)
1754 (Indexing.empty_table ()) active
1756 let res = forward_simplify env e (active, tbl) in
1760 | None -> simpl hd tl others_simpl
1761 | Some e -> simpl hd tl (e::others_simpl)
1765 | None -> others_simpl
1766 | Some e -> e::others_simpl
1769 match equalities with
1772 let others = List.map (fun e -> (Positive, e)) tl in
1774 List.rev (List.map snd (simpl (Positive, hd) others []))
1778 (Printf.sprintf "equalities AFTER:\n%s\n"
1780 (List.map string_of_equality res))));
1783 let active = make_active () in
1784 let passive = make_passive [] equalities in
1785 Printf.printf "\ncurrent goal: %s\n"
1786 (let _, _, g = goal in CicPp.ppterm g);
1787 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1788 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1789 Printf.printf "\nequalities:\n%s\n"
1792 (string_of_equality ~env)
1793 (equalities @ library_equalities)));
1794 print_endline "--------------------------------------------------";
1795 let start = Unix.gettimeofday () in
1796 print_endline "GO!";
1797 start_time := Unix.gettimeofday ();
1799 let goals = make_goals goal in
1800 (if !use_fullred then given_clause_fullred else given_clause)
1801 dbd env goals theorems passive active
1803 let finish = Unix.gettimeofday () in
1806 | ParamodulationFailure ->
1807 Printf.printf "NO proof found! :-(\n\n"
1808 | ParamodulationSuccess (Some proof, env) ->
1809 let proof = Inference.build_proof_term proof in
1810 Printf.printf "OK, found a proof!\n";
1811 (* REMEMBER: we have to instantiate meta_proof, we should use
1812 apply the "apply" tactic to proof and status
1814 let names = names_of_context context in
1815 print_endline (PP.pp proof names);
1818 (fun m (_, _, _, menv, _) -> m @ menv) metasenv equalities
1823 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1825 print_endline (string_of_float (finish -. start));
1827 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1828 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1830 (fst (CicReduction.are_convertible
1831 context type_of_goal ty ug)));
1833 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1834 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1835 print_endline (string_of_float (finish -. start));
1839 | ParamodulationSuccess (None, env) ->
1840 Printf.printf "Success, but no proof?!?\n\n"
1842 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1843 "forward_simpl_new_time: %.9f\n" ^^
1844 "backward_simpl_time: %.9f\n")
1845 !infer_time !forward_simpl_time !forward_simpl_new_time
1846 !backward_simpl_time;
1847 Printf.printf "passive_maintainance_time: %.9f\n"
1848 !passive_maintainance_time;
1849 Printf.printf " successful unification/matching time: %.9f\n"
1850 !Indexing.match_unif_time_ok;
1851 Printf.printf " failed unification/matching time: %.9f\n"
1852 !Indexing.match_unif_time_no;
1853 Printf.printf " indexing retrieval time: %.9f\n"
1854 !Indexing.indexing_retrieval_time;
1855 Printf.printf " demodulate_term.build_newtarget_time: %.9f\n"
1856 !Indexing.build_newtarget_time;
1857 Printf.printf "derived %d clauses, kept %d clauses.\n"
1858 !derived_clauses !kept_clauses;
1860 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1865 let default_depth = !maxdepth
1866 and default_width = !maxwidth;;
1870 symbols_counter := 0;
1871 weight_age_counter := !weight_age_ratio;
1872 processed_clauses := 0;
1875 maximal_retained_equality := None;
1877 forward_simpl_time := 0.;
1878 forward_simpl_new_time := 0.;
1879 backward_simpl_time := 0.;
1880 passive_maintainance_time := 0.;
1881 derived_clauses := 0;
1886 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1887 let module C = Cic in
1889 Indexing.init_index ();
1892 let proof, goal = status in
1894 let uri, metasenv, meta_proof, term_to_prove = proof in
1895 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1896 let eq_indexes, equalities, maxm = find_equalities context proof in
1897 let new_meta_goal, metasenv, type_of_goal =
1899 CicMkImplicit.identity_relocation_list_for_metavariable context in
1900 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1902 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
1903 Cic.Meta (maxm+1, irl),
1904 (maxm+1, context, ty)::metasenv,
1907 let ugraph = CicUniv.empty_ugraph in
1908 let env = (metasenv, context, ugraph) in
1909 let goal = Inference.BasicProof new_meta_goal, [], goal in
1911 let t1 = Unix.gettimeofday () in
1912 let lib_eq_uris, library_equalities, maxm =
1913 find_library_equalities dbd context (proof, goal') (maxm+2)
1915 let t2 = Unix.gettimeofday () in
1918 let equalities = equalities @ library_equalities in
1921 (Printf.sprintf "equalities:\n%s\n"
1923 (List.map string_of_equality equalities))));
1924 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1925 let rec simpl e others others_simpl =
1926 let active = others @ others_simpl in
1929 (fun t (_, e) -> Indexing.index t e)
1930 (Indexing.empty_table ()) active
1932 let res = forward_simplify env e (active, tbl) in
1936 | None -> simpl hd tl others_simpl
1937 | Some e -> simpl hd tl (e::others_simpl)
1941 | None -> others_simpl
1942 | Some e -> e::others_simpl
1945 match equalities with
1948 let others = List.map (fun e -> (Positive, e)) tl in
1950 List.rev (List.map snd (simpl (Positive, hd) others []))
1954 (Printf.sprintf "equalities AFTER:\n%s\n"
1956 (List.map string_of_equality res))));
1961 (Printf.sprintf "Time to retrieve equalities: %.9f\n" (t2 -. t1)));
1962 let t1 = Unix.gettimeofday () in
1965 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1966 let context_hyp = find_context_hypotheses env eq_indexes in
1967 context_hyp @ thms, []
1970 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1971 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1973 let t = CicUtil.term_of_uri refl_equal in
1974 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1977 let t2 = Unix.gettimeofday () in
1982 "Theorems:\n-------------------------------------\n%s\n"
1987 "Term: %s, type: %s"
1988 (CicPp.ppterm t) (CicPp.ppterm ty))
1992 (Printf.sprintf "Time to retrieve theorems: %.9f\n" (t2 -. t1)));
1994 let active = make_active () in
1995 let passive = make_passive [] equalities in
1996 let start = Unix.gettimeofday () in
1998 let goals = make_goals goal in
1999 given_clause_fullred dbd env goals theorems passive active
2001 let finish = Unix.gettimeofday () in
2002 (res, finish -. start)
2005 | ParamodulationSuccess (Some proof, env) ->
2006 debug_print (lazy "OK, found a proof!");
2007 let proof = Inference.build_proof_term proof in
2008 let names = names_of_context context in
2011 match new_meta_goal with
2012 | C.Meta (i, _) -> i | _ -> assert false
2014 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2019 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2021 debug_print (lazy (CicPp.pp proof [](* names *)));
2025 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2026 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2028 (fst (CicReduction.are_convertible
2029 context type_of_goal ty ug)))));
2030 let equality_for_replace i t1 =
2032 | C.Meta (n, _) -> n = i
2036 ProofEngineReduction.replace
2037 ~equality:equality_for_replace
2038 ~what:[goal'] ~with_what:[proof]
2043 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2044 (match uri with Some uri -> UriManager.string_of_uri uri
2046 (print_metasenv newmetasenv)
2047 (CicPp.pp real_proof [](* names *))
2048 (CicPp.pp term_to_prove names)));
2049 ((uri, newmetasenv, real_proof, term_to_prove), [])
2050 with CicTypeChecker.TypeCheckerFailure _ ->
2051 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2052 debug_print (lazy (CicPp.pp proof names));
2053 raise (ProofEngineTypes.Fail
2054 "Found a proof, but it doesn't typecheck")
2056 debug_print (lazy (Printf.sprintf "\nTIME NEEDED: %.9f" time));
2059 raise (ProofEngineTypes.Fail "NO proof found")
2062 (* dummy function called within matita to trigger linkage *)
2066 (* UGLY SIDE EFFECT... *)
2067 if connect_to_auto then (
2068 AutoTactic.paramodulation_tactic := saturate;
2069 AutoTactic.term_is_equality := Inference.term_is_equality;