5 (* set to false to disable paramodulation inside auto_tac *)
6 let connect_to_auto = true;;
9 (* profiling statistics... *)
10 let infer_time = ref 0.;;
11 let forward_simpl_time = ref 0.;;
12 let forward_simpl_new_time = ref 0.;;
13 let backward_simpl_time = ref 0.;;
14 let passive_maintainance_time = ref 0.;;
16 (* limited-resource-strategy related globals *)
17 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
18 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
19 let start_time = ref 0.;; (* time at which the execution started *)
20 let elapsed_time = ref 0.;;
21 (* let maximal_weight = ref None;; *)
22 let maximal_retained_equality = ref None;;
24 (* equality-selection related globals *)
25 let use_fullred = ref true;;
26 let weight_age_ratio = ref (* 5 *) 4;; (* settable by the user *)
27 let weight_age_counter = ref !weight_age_ratio;;
28 let symbols_ratio = ref (* 0 *) 3;;
29 let symbols_counter = ref 0;;
31 (* non-recursive Knuth-Bendix term ordering by default *)
32 Utils.compare_terms := Utils.nonrec_kbo;;
35 let derived_clauses = ref 0;;
36 let kept_clauses = ref 0;;
38 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
41 (* varbiables controlling the search-space *)
42 let maxdepth = ref 3;;
43 let maxwidth = ref 3;;
47 | ParamodulationFailure
48 | ParamodulationSuccess of Inference.proof option * environment
53 let symbols_of_equality (_, (_, left, right), _, _) =
54 TermSet.union (symbols_of_term left) (symbols_of_term right)
58 let symbols_of_equality ((_, _, (_, left, right, _), _, _) as equality) =
59 let m1 = symbols_of_term left in
64 let c = TermMap.find k res in
65 TermMap.add k (c+v) res
68 (symbols_of_term right) m1
70 (* Printf.printf "symbols_of_equality %s:\n" *)
71 (* (string_of_equality equality); *)
72 (* TermMap.iter (fun k v -> Printf.printf "%s: %d\n" (CicPp.ppterm k) v) m; *)
73 (* print_newline (); *)
78 module OrderedEquality = struct
79 type t = Inference.equality
82 match meta_convertibility_eq eq1 eq2 with
85 let w1, _, (ty, left, right, _), _, a = eq1
86 and w2, _, (ty', left', right', _), _, a' = eq2 in
87 (* let weight_of t = fst (weight_of_term ~consider_metas:false t) in *)
88 (* let w1 = (weight_of ty) + (weight_of left) + (weight_of right) *)
89 (* and w2 = (weight_of ty') + (weight_of left') + (weight_of right') in *)
90 match Pervasives.compare w1 w2 with
92 let res = (List.length a) - (List.length a') in
93 if res <> 0 then res else (
95 let res = Pervasives.compare (List.hd a) (List.hd a') in
96 if res <> 0 then res else Pervasives.compare eq1 eq2
97 with Failure "hd" -> Pervasives.compare eq1 eq2
98 (* match a, a' with *)
99 (* | (Cic.Meta (i, _)::_), (Cic.Meta (j, _)::_) -> *)
100 (* let res = Pervasives.compare i j in *)
101 (* if res <> 0 then res else Pervasives.compare eq1 eq2 *)
102 (* | _, _ -> Pervasives.compare eq1 eq2 *)
107 module EqualitySet = Set.Make(OrderedEquality);;
110 let select env goals passive (active, _) =
111 processed_clauses := !processed_clauses + 1;
114 match (List.rev goals) with (_, goal::_)::_ -> goal | _ -> assert false
117 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
119 List.filter (fun e -> e <> eq) l
121 if !weight_age_ratio > 0 then
122 weight_age_counter := !weight_age_counter - 1;
123 match !weight_age_counter with
125 weight_age_counter := !weight_age_ratio;
126 match neg_list, pos_list with
128 (* Negatives aren't indexed, no need to remove them... *)
130 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
133 Indexing.remove_index passive_table hd
134 (* if !use_fullred then Indexing.remove_index passive_table hd *)
135 (* else passive_table *)
138 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
139 | _, _ -> assert false
141 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) -> (
142 symbols_counter := !symbols_counter - 1;
143 let cardinality map =
144 TermMap.fold (fun k v res -> res + v) map 0
146 (* match active with *)
147 (* | (Negative, e)::_ -> *)
148 (* let symbols = symbols_of_equality e in *)
150 let _, _, term = goal in
153 let card = cardinality symbols in
154 let foldfun k v (r1, r2) =
155 if TermMap.mem k symbols then
156 let c = TermMap.find k symbols in
157 let c1 = abs (c - v) in
163 let f equality (i, e) =
165 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
167 let c = others + (abs (common - card)) in
168 if c < i then (c, equality)
169 (* else if c = i then *)
170 (* match OrderedEquality.compare equality e with *)
171 (* | -1 -> (c, equality) *)
172 (* | res -> (i, e) *)
175 let e1 = EqualitySet.min_elt pos_set in
178 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
180 (others + (abs (common - card))), e1
182 let _, current = EqualitySet.fold f pos_set initial in
183 (* Printf.printf "\nsymbols-based selection: %s\n\n" *)
184 (* (string_of_equality ~env current); *)
186 Indexing.remove_index passive_table current
187 (* if !use_fullred then Indexing.remove_index passive_table current *)
188 (* else passive_table *)
192 (remove current pos_list, EqualitySet.remove current pos_set),
195 (* let current = EqualitySet.min_elt pos_set in *)
196 (* let passive_table = *)
197 (* Indexing.remove_index passive_table current *)
198 (* (\* if !use_fullred then Indexing.remove_index passive_table current *\) *)
199 (* (\* else passive_table *\) *)
202 (* (neg_list, neg_set), *)
203 (* (remove current pos_list, EqualitySet.remove current pos_set), *)
206 (* (Positive, current), passive *)
209 symbols_counter := !symbols_ratio;
210 let set_selection set = EqualitySet.min_elt set in
211 if EqualitySet.is_empty neg_set then
212 let current = set_selection pos_set in
215 (remove current pos_list, EqualitySet.remove current pos_set),
216 Indexing.remove_index passive_table current
217 (* if !use_fullred then Indexing.remove_index passive_table current *)
218 (* else passive_table *)
220 (Positive, current), passive
222 let current = set_selection neg_set in
224 (remove current neg_list, EqualitySet.remove current neg_set),
228 (Negative, current), passive
232 let make_passive neg pos =
233 let set_of equalities =
234 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
237 List.fold_left (fun tbl e -> Indexing.index tbl e)
238 (Indexing.empty_table ()) pos
239 (* if !use_fullred then *)
240 (* List.fold_left (fun tbl e -> Indexing.index tbl e) *)
241 (* (Indexing.empty_table ()) pos *)
243 (* Indexing.empty_table () *)
252 [], Indexing.empty_table ()
256 let add_to_passive passive (new_neg, new_pos) =
257 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
258 let ok set equality = not (EqualitySet.mem equality set) in
259 let neg = List.filter (ok neg_set) new_neg
260 and pos = List.filter (ok pos_set) new_pos in
262 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
263 (* if !use_fullred then *)
264 (* List.fold_left (fun tbl e -> Indexing.index tbl e) table pos *)
268 let add set equalities =
269 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
271 (neg @ neg_list, add neg_set neg),
272 (pos_list @ pos, add pos_set pos),
277 let passive_is_empty = function
278 | ([], _), ([], _), _ -> true
283 let size_of_passive ((_, ns), (_, ps), _) =
284 (EqualitySet.cardinal ns) + (EqualitySet.cardinal ps)
288 let size_of_active (active_list, _) =
289 List.length active_list
293 let prune_passive howmany (active, _) passive =
294 let (nl, ns), (pl, ps), tbl = passive in
295 let howmany = float_of_int howmany
296 and ratio = float_of_int !weight_age_ratio in
299 int_of_float (if t -. v < 0.5 then t else v)
301 let in_weight = round (howmany *. ratio /. (ratio +. 1.))
302 and in_age = round (howmany /. (ratio +. 1.)) in
304 (lazy (Printf.sprintf "in_weight: %d, in_age: %d\n" in_weight in_age));
307 | (Negative, e)::_ ->
308 let symbols = symbols_of_equality e in
309 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
313 let counter = ref !symbols_ratio in
314 let rec pickw w ns ps =
316 if not (EqualitySet.is_empty ns) then
317 let e = EqualitySet.min_elt ns in
318 let ns', ps = pickw (w-1) (EqualitySet.remove e ns) ps in
319 EqualitySet.add e ns', ps
320 else if !counter > 0 then
322 counter := !counter - 1;
323 if !counter = 0 then counter := !symbols_ratio
327 let e = EqualitySet.min_elt ps in
328 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
329 ns, EqualitySet.add e ps'
331 let foldfun k v (r1, r2) =
332 if TermMap.mem k symbols then
333 let c = TermMap.find k symbols in
334 let c1 = abs (c - v) in
340 let f equality (i, e) =
342 TermMap.fold foldfun (symbols_of_equality equality) (0, 0)
344 let c = others + (abs (common - card)) in
345 if c < i then (c, equality)
348 let e1 = EqualitySet.min_elt ps in
351 TermMap.fold foldfun (symbols_of_equality e1) (0, 0)
353 (others + (abs (common - card))), e1
355 let _, e = EqualitySet.fold f ps initial in
356 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
357 ns, EqualitySet.add e ps'
359 let e = EqualitySet.min_elt ps in
360 let ns, ps' = pickw (w-1) ns (EqualitySet.remove e ps) in
361 ns, EqualitySet.add e ps'
363 EqualitySet.empty, EqualitySet.empty
365 (* let in_weight, ns = pickw in_weight ns in *)
366 (* let _, ps = pickw in_weight ps in *)
367 let ns, ps = pickw in_weight ns ps in
368 let rec picka w s l =
372 | hd::tl when not (EqualitySet.mem hd s) ->
373 let w, s, l = picka (w-1) s tl in
374 w, EqualitySet.add hd s, hd::l
376 let w, s, l = picka w s tl in
381 let in_age, ns, nl = picka in_age ns nl in
382 let _, ps, pl = picka in_age ps pl in
383 if not (EqualitySet.is_empty ps) then
384 (* maximal_weight := Some (weight_of_equality (EqualitySet.max_elt ps)); *)
385 maximal_retained_equality := Some (EqualitySet.max_elt ps);
388 (fun e tbl -> Indexing.index tbl e) ps (Indexing.empty_table ())
389 (* if !use_fullred then *)
390 (* EqualitySet.fold *)
391 (* (fun e tbl -> Indexing.index tbl e) ps (Indexing.empty_table ()) *)
395 (nl, ns), (pl, ps), tbl
399 let infer env sign current (active_list, active_table) =
400 let new_neg, new_pos =
404 Indexing.superposition_left !maxmeta env active_table current in
409 Indexing.superposition_right !maxmeta env active_table current in
411 let rec infer_positive table = function
413 | (Negative, equality)::tl ->
415 Indexing.superposition_left !maxmeta env table equality in
417 let neg, pos = infer_positive table tl in
419 | (Positive, equality)::tl ->
421 Indexing.superposition_right !maxmeta env table equality in
423 let neg, pos = infer_positive table tl in
426 let curr_table = Indexing.index (Indexing.empty_table ()) current in
427 let neg, pos = infer_positive curr_table active_list in
430 derived_clauses := !derived_clauses + (List.length new_neg) +
431 (List.length new_pos);
432 match !maximal_retained_equality with
433 | None -> new_neg, new_pos
435 (* if we have a maximal_retained_equality, we can discard all equalities
436 "greater" than it, as they will never be reached... An equality is
437 greater than maximal_retained_equality if it is bigger
438 wrt. OrderedEquality.compare and it is less similar than
439 maximal_retained_equality to the current goal *)
441 match active_list with
442 | (Negative, e)::_ ->
443 let symbols = symbols_of_equality e in
444 let card = TermMap.fold (fun k v res -> res + v) symbols 0 in
451 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos
454 if OrderedEquality.compare e eq <= 0 then
457 let foldfun k v (r1, r2) =
458 if TermMap.mem k symbols then
459 let c = TermMap.find k symbols in
460 let c1 = abs (c - v) in
468 TermMap.fold foldfun (symbols_of_equality eq) (0, 0) in
469 others + (abs (common - card))
472 TermMap.fold foldfun (symbols_of_equality e) (0, 0) in
473 let c = others + (abs (common - card)) in
474 if c < initial then true else false
476 List.filter filterfun new_pos
482 let contains_empty env (negative, positive) =
483 let metasenv, context, ugraph = env in
487 (fun (w, proof, (ty, left, right, ordering), m, a) ->
488 fst (CicReduction.are_convertible context left right ugraph))
497 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
498 let pl, passive_table =
501 | Some ((pn, _), (pp, _), pt) ->
502 let pn = List.map (fun e -> (Negative, e)) pn
503 and pp = List.map (fun e -> (Positive, e)) pp in
506 let all = if pl = [] then active_list else active_list @ pl in
508 (* let rec find_duplicate sign current = function *)
510 (* | (s, eq)::tl when s = sign -> *)
511 (* if meta_convertibility_eq current eq then true *)
512 (* else find_duplicate sign current tl *)
513 (* | _::tl -> find_duplicate sign current tl *)
517 (* if sign = Positive then *)
518 (* Indexing.subsumption env active_table current *)
526 let demodulate table current =
527 let newmeta, newcurrent =
528 Indexing.demodulation_equality !maxmeta env table sign current in
530 if is_identity env newcurrent then
531 if sign = Negative then Some (sign, newcurrent)
534 Some (sign, newcurrent)
537 let res = demodulate active_table current in
540 | Some (sign, newcurrent) ->
541 match passive_table with
543 | Some passive_table -> demodulate passive_table newcurrent
547 | Some (Negative, c) ->
550 (fun (s, eq) -> s = Negative && meta_convertibility_eq eq c)
553 if ok then res else None
554 | Some (Positive, c) ->
555 if Indexing.in_index active_table c then
558 match passive_table with
560 | Some passive_table ->
561 if Indexing.in_index passive_table c then None
564 (* | Some (s, c) -> if find_duplicate s c all then None else res *)
566 (* if s = Utils.Negative then *)
569 (* if Indexing.subsumption env active_table c then *)
572 (* match passive_table with *)
574 (* | Some passive_table -> *)
575 (* if Indexing.subsumption env passive_table c then *)
581 (* let pred (sign, eq) = *)
582 (* if sign <> s then false *)
583 (* else subsumption env c eq *)
585 (* if List.exists pred all then None *)
589 type fs_time_info_t = {
590 mutable build_all: float;
591 mutable demodulate: float;
592 mutable subsumption: float;
595 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
598 let forward_simplify_new env (new_neg, new_pos) ?passive active =
599 let t1 = Unix.gettimeofday () in
601 let active_list, active_table = active in
602 let pl, passive_table =
605 | Some ((pn, _), (pp, _), pt) ->
606 let pn = List.map (fun e -> (Negative, e)) pn
607 and pp = List.map (fun e -> (Positive, e)) pp in
610 let all = active_list @ pl in
612 let t2 = Unix.gettimeofday () in
613 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
615 let demodulate sign table target =
616 let newmeta, newtarget =
617 Indexing.demodulation_equality !maxmeta env table sign target in
621 (* let f sign' target (sign, eq) = *)
622 (* if sign <> sign' then false *)
623 (* else subsumption env target eq *)
626 let t1 = Unix.gettimeofday () in
628 let new_neg, new_pos =
629 let new_neg = List.map (demodulate Negative active_table) new_neg
630 and new_pos = List.map (demodulate Positive active_table) new_pos in
631 match passive_table with
632 | None -> new_neg, new_pos
633 | Some passive_table ->
634 List.map (demodulate Negative passive_table) new_neg,
635 List.map (demodulate Positive passive_table) new_pos
638 let t2 = Unix.gettimeofday () in
639 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
644 if not (Inference.is_identity env e) then
645 if EqualitySet.mem e s then s
646 else EqualitySet.add e s
648 EqualitySet.empty new_pos
650 let new_pos = EqualitySet.elements new_pos_set in
653 match passive_table with
655 (fun e -> not (fst (Indexing.subsumption env active_table e)))
656 | Some passive_table ->
657 (fun e -> not ((fst (Indexing.subsumption env active_table e)) ||
658 (fst (Indexing.subsumption env passive_table e))))
661 let t1 = Unix.gettimeofday () in
663 (* let new_neg, new_pos = *)
664 (* List.filter subs new_neg, *)
665 (* List.filter subs new_pos *)
668 (* let new_neg, new_pos = *)
669 (* (List.filter (fun e -> not (List.exists (f Negative e) all)) new_neg, *)
670 (* List.filter (fun e -> not (List.exists (f Positive e) all)) new_pos) *)
673 let t2 = Unix.gettimeofday () in
674 fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1);
677 match passive_table with
679 (fun e -> not (Indexing.in_index active_table e))
680 | Some passive_table ->
682 not ((Indexing.in_index active_table e) ||
683 (Indexing.in_index passive_table e)))
685 new_neg, List.filter is_duplicate new_pos
687 (* new_neg, new_pos *)
690 (* (List.filter (fun e -> not (List.exists (f Negative e) all)) new_neg, *)
691 (* List.filter (fun e -> not (List.exists (f Positive e) all)) new_pos) *)
697 let backward_simplify_active env new_pos new_table min_weight active =
698 let active_list, active_table = active in
699 let active_list, newa =
701 (fun (s, equality) (res, newn) ->
702 let ew, _, _, _, _ = equality in
703 if ew < min_weight then
704 (s, equality)::res, newn
706 match forward_simplify env (s, equality) (new_pos, new_table) with
716 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
720 (fun (s, eq) (res, tbl) ->
721 if List.mem (s, eq) res then
723 else if (is_identity env eq) || (find eq res) then (
725 ) (* else if (find eq res) then *)
728 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
729 active_list ([], Indexing.empty_table ()),
731 (fun (s, eq) (n, p) ->
732 if (s <> Negative) && (is_identity env eq) then (
735 if s = Negative then eq::n, p
740 | [], [] -> active, None
741 | _ -> active, Some newa
745 let backward_simplify_passive env new_pos new_table min_weight passive =
746 let (nl, ns), (pl, ps), passive_table = passive in
747 let f sign equality (resl, ress, newn) =
748 let ew, _, _, _, _ = equality in
749 if ew < min_weight then
750 (* let _ = debug_print (lazy (Printf.sprintf "OK: %d %d" ew min_weight)) in *)
751 equality::resl, ress, newn
753 match forward_simplify env (sign, equality) (new_pos, new_table) with
754 | None -> resl, EqualitySet.remove equality ress, newn
757 equality::resl, ress, newn
759 let ress = EqualitySet.remove equality ress in
762 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
763 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
766 (fun tbl e -> Indexing.index tbl e) (Indexing.empty_table ()) pl
768 match newn, newp with
769 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
770 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
774 let backward_simplify env new' ?passive active =
775 let new_pos, new_table, min_weight =
778 let ew, _, _, _, _ = e in
779 (Positive, e)::l, Indexing.index t e, min ew w)
780 ([], Indexing.empty_table (), 1000000) (snd new')
783 backward_simplify_active env new_pos new_table min_weight active in
786 active, (make_passive [] []), newa, None
789 backward_simplify_passive env new_pos new_table min_weight passive in
790 active, passive, newa, newp
794 let get_selection_estimate () =
795 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
796 (* !processed_clauses * (int_of_float (!time_limit /. !elapsed_time)) *)
798 ceil ((float_of_int !processed_clauses) *.
799 ((!time_limit (* *. 2. *)) /. !elapsed_time -. 1.)))
803 let simplify_goal env goal ?passive (active_list, active_table) =
804 let pl, passive_table =
807 | Some ((pn, _), (pp, _), pt) ->
808 let pn = List.map (fun e -> (Negative, e)) pn
809 and pp = List.map (fun e -> (Positive, e)) pp in
812 let all = if pl = [] then active_list else active_list @ pl in
814 let demodulate table goal =
815 let newmeta, newgoal =
816 Indexing.demodulation_goal !maxmeta env table goal in
821 match passive_table with
822 | None -> demodulate active_table goal
823 | Some passive_table ->
824 let goal = demodulate active_table goal in
825 demodulate passive_table goal
828 let p, _, t = goal in
831 (Printf.sprintf "Goal after demodulation: %s, %s"
832 (string_of_proof p) (CicPp.ppterm t)))
838 let simplify_goals env goals ?passive active =
841 let gl = List.map (fun g -> simplify_goal env g ?passive active) gl in
847 let simplify_theorems env theorems ?passive (active_list, active_table) =
848 let pl, passive_table =
851 | Some ((pn, _), (pp, _), pt) ->
852 let pn = List.map (fun e -> (Negative, e)) pn
853 and pp = List.map (fun e -> (Positive, e)) pp in
856 let all = if pl = [] then active_list else active_list @ pl in
858 let demodulate table theorem =
859 let newmeta, newthm =
860 Indexing.demodulation_theorem !maxmeta env table theorem in
864 match passive_table with
865 | None -> List.map (demodulate active_table) theorems
866 | Some passive_table ->
867 let theorems = List.map (demodulate active_table) theorems in
868 List.map (demodulate passive_table) theorems
872 let apply_equality_to_goal env equality goal =
873 let module C = Cic in
874 let module HL = HelmLibraryObjects in
875 let module I = Inference in
876 let metasenv, context, ugraph = env in
877 let _, proof, (ty, left, right, _), metas, args = equality in
879 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right] in
880 let gproof, gmetas, gterm = goal in
882 let subst, metasenv', _ =
883 let menv = metasenv @ metas @ gmetas in
884 Inference.unification menv context eqterm gterm ugraph
888 | I.BasicProof t -> I.BasicProof (CicMetaSubst.apply_subst subst t)
889 | I.ProofBlock (s, uri, nt, t, pe, p) ->
890 I.ProofBlock (subst @ s, uri, nt, t, pe, p)
894 let rec repl = function
895 | I.ProofGoalBlock (_, gp) -> I.ProofGoalBlock (newproof, gp)
896 | I.NoProof -> newproof
897 | I.BasicProof p -> newproof
898 | I.SubProof (t, i, p) -> I.SubProof (t, i, repl p)
903 true, subst, newgproof
904 with CicUnification.UnificationFailure _ ->
910 let apply_to_goal env theorems active (depth, goals) =
912 debug_print ("apply_to_goal: " ^ (string_of_int (List.length goals)))
914 let metasenv, context, ugraph = env in
915 let goal = List.hd goals in
916 let proof, metas, term = goal in
918 (* (Printf.sprintf "apply_to_goal with goal: %s" (CicPp.ppterm term)); *)
919 let newmeta = CicMkImplicit.new_meta metasenv [] in
920 let metasenv = (newmeta, context, term)::metasenv @ metas in
921 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
923 ((None, metasenv, Cic.Meta (newmeta, irl), term), newmeta)
925 let rec aux = function
926 | [] -> false, [] (* goals *) (* None *)
927 | (theorem, thmty, _)::tl ->
929 let subst_in, (newproof, newgoals) =
930 PrimitiveTactics.apply_tac_verbose ~term:theorem status
932 if newgoals = [] then
933 let _, _, p, _ = newproof in
935 let rec repl = function
936 | Inference.ProofGoalBlock (_, gp) ->
937 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
938 | Inference.NoProof -> Inference.BasicProof p
939 | Inference.BasicProof _ -> Inference.BasicProof p
940 | Inference.SubProof (t, i, p2) ->
941 Inference.SubProof (t, i, repl p2)
946 true, [[newp, metas, term]] (* Some newp *)
947 else if List.length newgoals = 1 then
948 let _, menv, p, _ = newproof in
950 CicMkImplicit.identity_relocation_list_for_metavariable context
955 let _, _, ty = CicUtil.lookup_meta i menv in
958 (p, i, Inference.BasicProof (Cic.Meta (i, irl)))
959 in (proof, menv, ty))
962 let res, others = aux tl in
963 if res then (true, others) else (false, goals::others)
966 with ProofEngineTypes.Fail msg ->
967 (* debug_print ("FAIL!!:" ^ msg); *)
971 if Inference.term_is_equality term then
972 let rec appleq = function
974 | (Positive, equality)::tl ->
975 let ok, _, newproof = apply_equality_to_goal env equality goal in
976 if ok then true, [(depth, [newproof, metas, term])] else appleq tl
979 let al, _ = active in
984 if r = true then r, l else
985 let r, l = aux theorems in
987 r, List.map (fun l -> (depth+1, l)) l
989 r, (depth, goals)::(List.map (fun l -> (depth+1, l)) l)
995 incr maxmeta; !maxmeta
999 let apply_to_goal env theorems active goal =
1000 let metasenv, context, ugraph = env in
1001 let proof, metas, term = goal in
1004 (Printf.sprintf "apply_to_goal with goal: %s, %s"
1005 (string_of_proof proof) (CicPp.ppterm term)));
1008 CicMkImplicit.identity_relocation_list_for_metavariable context in
1009 let proof', newmeta =
1010 let rec get_meta = function
1011 | SubProof (t, i, _) -> t, i
1012 | ProofGoalBlock (_, p) -> get_meta p
1014 let n = new_meta () in (* CicMkImplicit.new_meta metasenv [] in *)
1015 Cic.Meta (n, irl), n
1019 (* let newmeta = CicMkImplicit.new_meta metasenv [] in *)
1020 let metasenv = (newmeta, context, term)::metasenv @ metas in
1021 ((None, metasenv, Cic.Meta (newmeta, irl), term), newmeta)
1022 (* ((None, metasenv, proof', term), newmeta) *)
1024 let rec aux = function
1025 | [] -> `No (* , [], [] *)
1026 | (theorem, thmty, _)::tl ->
1028 let subst, (newproof, newgoals) =
1029 PrimitiveTactics.apply_tac_verbose_with_subst ~term:theorem status
1031 if newgoals = [] then
1032 let _, _, p, _ = newproof in
1034 let rec repl = function
1035 | Inference.ProofGoalBlock (_, gp) ->
1036 Inference.ProofGoalBlock (Inference.BasicProof p, gp)
1037 | Inference.NoProof -> Inference.BasicProof p
1038 | Inference.BasicProof _ -> Inference.BasicProof p
1039 | Inference.SubProof (t, i, p2) ->
1040 Inference.SubProof (t, i, repl p2)
1045 let _, m = status in
1046 let subst = List.filter (fun (i, _) -> i = m) subst in
1049 (* (Printf.sprintf "m = %d\nsubst = %s\n" *)
1050 (* m (print_subst subst))); *)
1051 `Ok (subst, [newp, metas, term])
1053 let _, menv, p, _ = newproof in
1055 CicMkImplicit.identity_relocation_list_for_metavariable context
1060 let _, _, ty = CicUtil.lookup_meta i menv in
1062 let rec gp = function
1063 | SubProof (t, i, p) ->
1064 SubProof (t, i, gp p)
1065 | ProofGoalBlock (sp1, sp2) ->
1066 (* SubProof (p, i, sp) *)
1067 ProofGoalBlock (sp1, gp sp2)
1071 SubProof (p, i, BasicProof (Cic.Meta (i, irl)))
1072 | ProofSymBlock (s, sp) ->
1073 ProofSymBlock (s, gp sp)
1074 | ProofBlock (s, u, nt, t, pe, sp) ->
1075 ProofBlock (s, u, nt, t, pe, gp sp)
1076 (* | _ -> assert false *)
1081 (Printf.sprintf "new sub goal: %s, %s"
1082 (string_of_proof p') (CicPp.ppterm ty)));
1088 (* (Printf.sprintf "\nGoOn with subst: %s" (print_subst subst))); *)
1089 let best = aux tl in
1091 | `Ok (_, _) -> best
1092 | `No -> `GoOn ([subst, goals])
1093 | `GoOn sl(* , subst', goals' *) ->
1094 (* if (List.length goals') < (List.length goals) then best *)
1095 (* else `GoOn, subst, goals *)
1096 `GoOn ((subst, goals)::sl)
1097 with ProofEngineTypes.Fail msg ->
1101 if Inference.term_is_equality term then
1102 let rec appleq = function
1103 | [] -> false, [], []
1104 | (Positive, equality)::tl ->
1105 let ok, s, newproof = apply_equality_to_goal env equality goal in
1106 if ok then true, s, [newproof, metas, term] else appleq tl
1107 | _::tl -> appleq tl
1109 let al, _ = active in
1114 if r = true then `Ok (s, l) else aux theorems
1118 let apply_to_goal_conj env theorems active (depth, goals) =
1119 let rec aux = function
1121 let propagate_subst subst (proof, metas, term) =
1124 (Printf.sprintf "\npropagate_subst:\n%s\n%s, %s\n"
1125 (print_subst subst) (string_of_proof proof)
1126 (CicPp.ppterm term)));
1127 let rec repl = function
1128 | NoProof -> NoProof
1130 BasicProof (CicMetaSubst.apply_subst subst t)
1131 | ProofGoalBlock (p, pb) ->
1132 debug_print (lazy "HERE");
1133 let pb' = repl pb in
1134 ProofGoalBlock (p, pb')
1135 | SubProof (t, i, p) ->
1136 let t' = CicMetaSubst.apply_subst subst t in
1140 "SubProof %d\nt = %s\nsubst = %s\nt' = %s\n"
1141 i (CicPp.ppterm t) (print_subst subst)
1142 (CicPp.ppterm t')));
1145 | ProofSymBlock (ens, p) -> ProofSymBlock (ens, repl p)
1146 | ProofBlock (s, u, nty, t, pe, p) ->
1147 ProofBlock (subst @ s, u, nty, t, pe, p)
1148 in (repl proof, metas, term)
1150 let r = apply_to_goal env theorems active goal in (
1152 | `No -> `No (depth, goals)
1153 | `GoOn sl (* (subst, gl) *) ->
1154 (* let tl = List.map (propagate_subst subst) tl in *)
1155 debug_print (lazy "GO ON!!!");
1159 (depth+1, gl @ (List.map (propagate_subst s) tl))) sl
1163 (Printf.sprintf "%s\n"
1167 (Printf.sprintf "[%s]"
1171 (Printf.sprintf "<%s, %s>"
1173 (CicPp.ppterm g))) gl)))) l))));
1174 `GoOn l (* (depth+1, gl @ tl) *)
1175 | `Ok (subst, gl) ->
1178 (* let p, _, t = List.hd gl in *)
1181 (* (Printf.sprintf "OK: %s, %s\n" *)
1182 (* (string_of_proof p) (CicPp.ppterm t))) *)
1186 let p, _, _ = List.hd gl in
1188 let rec repl = function
1189 | SubProof (_, _, p) -> repl p
1190 | ProofGoalBlock (p1, p2) ->
1191 ProofGoalBlock (repl p1, repl p2)
1194 build_proof_term (repl p)
1197 let rec get_meta = function
1198 | SubProof (_, i, p) -> max i (get_meta p)
1199 | ProofGoalBlock (_, p) -> get_meta p
1200 | _ -> -1 (* assert false *)
1205 let _, (context, _, _) = List.hd subst in
1206 [i, (context, subproof, Cic.Implicit None)]
1208 let tl = List.map (propagate_subst subst) tl in
1209 `GoOn ([depth+1, tl])
1215 (Printf.sprintf "apply_to_goal_conj (%d, [%s])"
1218 (List.map (fun (_, _, t) -> CicPp.ppterm t) goals))));
1219 if depth > !maxdepth || (List.length goals) > !maxwidth then (
1221 (lazy (Printf.sprintf "Pruning because depth = %d, width = %d"
1222 depth (List.length goals)));
1229 module OrderedGoals = struct
1230 type t = int * (Inference.proof * Cic.metasenv * Cic.term) list
1237 else let r = (List.length l1) - (List.length l2) in
1243 (fun (_, _, t1) (_, _, t2) ->
1244 let r = Pervasives.compare t1 t2 in
1251 (* let res = Pervasives.compare g1 g2 in *)
1253 (* let print_goals (d, gl) = *)
1254 (* let gl' = List.map (fun (_, _, t) -> CicPp.ppterm t) gl in *)
1255 (* Printf.sprintf "%d, [%s]" d (String.concat "; " gl') *)
1259 (* (Printf.sprintf "comparing g1:%s and g2:%s, res: %d\n" *)
1260 (* (print_goals g1) (print_goals g2) res)) *)
1265 module GoalsSet = Set.Make(OrderedGoals);;
1268 exception SearchSpaceOver;;
1271 let apply_to_goals env is_passive_empty theorems active goals =
1272 debug_print (lazy "\n\n\tapply_to_goals\n\n");
1273 let add_to set goals =
1274 List.fold_left (fun s g -> GoalsSet.add g s) set goals
1276 let rec aux set = function
1278 debug_print (lazy "HERE!!!");
1279 if is_passive_empty then raise SearchSpaceOver else false, set
1281 let res = apply_to_goal_conj env theorems active goals in
1287 | (d, (p, _, t)::_) -> d, p, t
1292 (Printf.sprintf "\nOK!!!!\ndepth: %d\nProof: %s\ngoal: %s\n"
1293 d (string_of_proof p) (CicPp.ppterm t)))
1295 true, GoalsSet.singleton newgoals
1297 let print_set set msg =
1300 (Printf.sprintf "%s:\n%s" msg
1305 List.map (fun (_, _, t) -> CicPp.ppterm t) gl
1308 Printf.sprintf "%d, [%s]" d
1309 (String.concat "; " gl')
1313 let set = add_to set (goals::tl) in
1314 (* print_set set "SET BEFORE"; *)
1315 let n = GoalsSet.cardinal set in
1316 let set = add_to set newgoals in
1317 (* print_set set "SET AFTER"; *)
1318 let m = GoalsSet.cardinal set in
1322 (* let _ = print_set set "SET didn't change" in *)
1326 (* let set = add_to set (newgoals::goals::tl) in *)
1327 (* let res, set = aux set tl in *)
1330 let n = List.length goals in
1331 let res, goals = aux (add_to GoalsSet.empty goals) goals in
1332 let goals = GoalsSet.elements goals in
1333 debug_print (lazy "\n\tapply_to_goals end\n");
1334 let m = List.length goals in
1335 if m = n && is_passive_empty then
1336 raise SearchSpaceOver
1342 let rec given_clause env goals theorems passive active =
1343 let time1 = Unix.gettimeofday () in
1345 let selection_estimate = get_selection_estimate () in
1346 let kept = size_of_passive passive in
1348 if !time_limit = 0. || !processed_clauses = 0 then
1350 else if !elapsed_time > !time_limit then (
1351 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1352 !time_limit !elapsed_time));
1354 ) else if kept > selection_estimate then (
1356 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1357 "(kept: %d, selection_estimate: %d)\n")
1358 kept selection_estimate));
1359 prune_passive selection_estimate active passive
1364 let time2 = Unix.gettimeofday () in
1365 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1367 kept_clauses := (size_of_passive passive) + (size_of_active active);
1369 (* let refl_equal = *)
1370 (* CicUtil.term_of_uri *)
1371 (* (UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)") *)
1373 let goals = simplify_goals env goals active in
1374 let theorems = simplify_theorems env theorems active in
1375 let is_passive_empty = passive_is_empty passive in
1377 let ok, goals = apply_to_goals env is_passive_empty theorems active goals in
1381 | (_, [proof, _, _])::_ -> Some proof
1384 ParamodulationSuccess (proof, env)
1386 match is_passive_empty (* passive_is_empty passive *) with
1387 | true -> (* ParamodulationFailure *)
1388 given_clause env goals theorems passive active
1390 let (sign, current), passive = select env goals passive active in
1391 let time1 = Unix.gettimeofday () in
1392 let res = forward_simplify env (sign, current) ~passive active in
1393 let time2 = Unix.gettimeofday () in
1394 forward_simpl_time := !forward_simpl_time +. (time2 -. time1);
1397 given_clause env goals theorems passive active
1398 | Some (sign, current) ->
1399 if (sign = Negative) && (is_identity env current) then (
1401 (lazy (Printf.sprintf "OK!!! %s %s" (string_of_sign sign)
1402 (string_of_equality ~env current)));
1403 let _, proof, _, _, _ = current in
1404 ParamodulationSuccess (Some proof (* current *), env)
1407 (lazy "\n================================================");
1408 debug_print (lazy (Printf.sprintf "selected: %s %s"
1409 (string_of_sign sign)
1410 (string_of_equality ~env current)));
1412 let t1 = Unix.gettimeofday () in
1413 let new' = infer env sign current active in
1414 let t2 = Unix.gettimeofday () in
1415 infer_time := !infer_time +. (t2 -. t1);
1417 let res, goal' = contains_empty env new' in
1421 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1424 ParamodulationSuccess (proof (* goal *), env)
1426 let t1 = Unix.gettimeofday () in
1427 let new' = forward_simplify_new env new' active in
1428 let t2 = Unix.gettimeofday () in
1430 forward_simpl_new_time :=
1431 !forward_simpl_new_time +. (t2 -. t1)
1435 | Negative -> active
1437 let t1 = Unix.gettimeofday () in
1438 let active, _, newa, _ =
1439 backward_simplify env ([], [current]) active
1441 let t2 = Unix.gettimeofday () in
1442 backward_simpl_time :=
1443 !backward_simpl_time +. (t2 -. t1);
1447 let al, tbl = active in
1448 let nn = List.map (fun e -> Negative, e) n in
1453 Indexing.index tbl e)
1459 (* Printf.printf "active:\n%s\n" *)
1460 (* (String.concat "\n" *)
1462 (* (fun (s, e) -> (string_of_sign s) ^ " " ^ *)
1463 (* (string_of_equality ~env e)) (fst active)))); *)
1464 (* print_newline (); *)
1467 (* match new' with *)
1469 (* Printf.printf "new':\n%s\n" *)
1470 (* (String.concat "\n" *)
1472 (* (fun e -> "Negative " ^ *)
1473 (* (string_of_equality ~env e)) neg) @ *)
1475 (* (fun e -> "Positive " ^ *)
1476 (* (string_of_equality ~env e)) pos))); *)
1477 (* print_newline (); *)
1479 match contains_empty env new' with
1482 let al, tbl = active in
1484 | Negative -> (sign, current)::al, tbl
1486 al @ [(sign, current)], Indexing.index tbl current
1488 let passive = add_to_passive passive new' in
1489 let (_, ns), (_, ps), _ = passive in
1490 (* Printf.printf "passive:\n%s\n" *)
1491 (* (String.concat "\n" *)
1492 (* ((List.map (fun e -> "Negative " ^ *)
1493 (* (string_of_equality ~env e)) *)
1494 (* (EqualitySet.elements ns)) @ *)
1495 (* (List.map (fun e -> "Positive " ^ *)
1496 (* (string_of_equality ~env e)) *)
1497 (* (EqualitySet.elements ps)))); *)
1498 (* print_newline (); *)
1499 given_clause env goals theorems passive active
1504 let _, proof, _, _, _ = goal in Some proof
1507 ParamodulationSuccess (proof (* goal *), env)
1509 with SearchSpaceOver ->
1510 ParamodulationFailure
1514 let rec given_clause_fullred env goals theorems passive active =
1515 let time1 = Unix.gettimeofday () in
1517 let selection_estimate = get_selection_estimate () in
1518 let kept = size_of_passive passive in
1520 if !time_limit = 0. || !processed_clauses = 0 then
1522 else if !elapsed_time > !time_limit then (
1523 debug_print (lazy (Printf.sprintf "Time limit (%.2f) reached: %.2f\n"
1524 !time_limit !elapsed_time));
1526 ) else if kept > selection_estimate then (
1528 (lazy (Printf.sprintf ("Too many passive equalities: pruning..." ^^
1529 "(kept: %d, selection_estimate: %d)\n")
1530 kept selection_estimate));
1531 prune_passive selection_estimate active passive
1536 let time2 = Unix.gettimeofday () in
1537 passive_maintainance_time := !passive_maintainance_time +. (time2 -. time1);
1539 kept_clauses := (size_of_passive passive) + (size_of_active active);
1541 (* let refl_equal = *)
1542 (* CicUtil.term_of_uri *)
1543 (* (UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)") *)
1545 let goals = simplify_goals env goals ~passive active in
1546 let theorems = simplify_theorems env theorems ~passive active in
1547 let is_passive_empty = passive_is_empty passive in
1549 let ok, goals = apply_to_goals env is_passive_empty theorems active goals in
1553 | (_, [proof, _, _])::_ -> Some proof
1556 ParamodulationSuccess (proof, env)
1560 (lazy ("new_goals: " ^ (string_of_int (List.length goals))));
1569 (string_of_proof p) ^ ", " ^ (CicPp.ppterm t)) gl
1571 Printf.sprintf "%d: %s" d (String.concat "; " gl'))
1574 match is_passive_empty (* passive_is_empty passive *) with
1575 | true -> (* ParamodulationFailure *)
1576 given_clause_fullred env goals theorems passive active
1578 let (sign, current), passive = select env 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 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 (* current *), 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 (* let (_, ns), (_, ps), _ = passive in *)
1669 (* Printf.printf "passive:\n%s\n" *)
1670 (* (String.concat "\n" *)
1671 (* ((List.map (fun e -> "Negative " ^ *)
1672 (* (string_of_equality ~env e)) *)
1673 (* (EqualitySet.elements ns)) @ *)
1674 (* (List.map (fun e -> "Positive " ^ *)
1675 (* (string_of_equality ~env e)) *)
1676 (* (EqualitySet.elements ps)))); *)
1677 (* print_newline (); *)
1678 given_clause_fullred env goals theorems passive active
1682 | Some goal -> let _, proof, _, _, _ = goal in Some proof
1685 ParamodulationSuccess (proof (* goal *), env)
1687 with SearchSpaceOver ->
1688 ParamodulationFailure
1692 (* let given_clause_ref = ref given_clause;; *)
1694 let main dbd term metasenv ugraph =
1695 let module C = Cic in
1696 let module T = CicTypeChecker in
1697 let module PET = ProofEngineTypes in
1698 let module PP = CicPp in
1699 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
1700 let status = PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
1701 let proof, goals = status in
1702 let goal' = List.nth goals 0 in
1703 let _, metasenv, meta_proof, _ = proof in
1704 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1705 let eq_indexes, equalities, maxm = find_equalities context proof in
1706 let lib_eq_uris, library_equalities, maxm =
1707 find_library_equalities dbd context (proof, goal') (maxm+2)
1709 maxmeta := maxm+2; (* TODO ugly!! *)
1710 let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
1711 let new_meta_goal, metasenv, type_of_goal =
1712 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1713 Printf.printf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty);
1715 Cic.Meta (maxm+1, irl),
1716 (maxm+1, context, ty)::metasenv,
1719 (* let new_meta_goal = Cic.Meta (goal', irl) in *)
1720 let env = (metasenv, context, ugraph) in
1721 let theorems = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1722 let context_hyp = find_context_hypotheses env eq_indexes in
1723 let theorems = context_hyp @ theorems in
1728 "Theorems:\n-------------------------------------\n%s\n"
1733 "Term: %s, type: %s" (CicPp.ppterm t) (CicPp.ppterm ty))
1737 let goal = Inference.BasicProof new_meta_goal, [], goal in
1738 (* let term_equality = equality_of_term new_meta_goal goal in *)
1739 (* let _, meta_proof, (eq_ty, left, right, ordering), _, _ = term_equality in *)
1740 (* if is_identity env term_equality then *)
1742 (* Cic.Appl [Cic.MutConstruct (\* reflexivity *\) *)
1743 (* (HelmLibraryObjects.Logic.eq_URI, 0, 1, []); *)
1747 (* Printf.printf "OK, found a proof!\n"; *)
1748 (* let names = names_of_context context in *)
1749 (* print_endline (PP.pp proof names) *)
1754 let equalities = equalities @ library_equalities in
1757 (Printf.sprintf "equalities:\n%s\n"
1759 (List.map string_of_equality equalities))));
1760 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1761 let rec simpl e others others_simpl =
1762 let active = others @ others_simpl in
1765 (fun t (_, e) -> Indexing.index t e)
1766 (Indexing.empty_table ()) active
1768 let res = forward_simplify env e (active, tbl) in
1772 | None -> simpl hd tl others_simpl
1773 | Some e -> simpl hd tl (e::others_simpl)
1777 | None -> others_simpl
1778 | Some e -> e::others_simpl
1781 match equalities with
1784 let others = List.map (fun e -> (Positive, e)) tl in
1786 List.rev (List.map snd (simpl (Positive, hd) others []))
1790 (Printf.sprintf "equalities AFTER:\n%s\n"
1792 (List.map string_of_equality res))));
1795 let active = make_active () in
1796 let passive = make_passive [] (* [term_equality] *) equalities in
1797 Printf.printf "\ncurrent goal: %s\n"
1798 (let _, _, g = goal in CicPp.ppterm g);
1799 (* (string_of_equality ~env term_equality); *)
1800 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
1801 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
1802 Printf.printf "\nequalities:\n%s\n"
1805 (string_of_equality ~env)
1806 (equalities @ library_equalities)));
1807 print_endline "--------------------------------------------------";
1808 let start = Unix.gettimeofday () in
1809 print_endline "GO!";
1810 start_time := Unix.gettimeofday ();
1812 (if !use_fullred then given_clause_fullred else given_clause)
1813 env [0, [goal]] theorems passive active
1815 let finish = Unix.gettimeofday () in
1818 | ParamodulationFailure ->
1819 Printf.printf "NO proof found! :-(\n\n"
1820 | ParamodulationSuccess (Some proof (* goal *), env) ->
1821 (* let proof = Inference.build_proof_term goal in *)
1822 let proof = Inference.build_proof_term proof in
1823 Printf.printf "OK, found a proof!\n";
1824 (* REMEMBER: we have to instantiate meta_proof, we should use
1825 apply the "apply" tactic to proof and status
1827 let names = names_of_context context in
1828 print_endline (PP.pp proof names);
1831 (fun m (_, _, _, menv, _) -> m @ menv) metasenv equalities
1834 Printf.printf "OK, found a proof!\n";
1835 (* REMEMBER: we have to instantiate meta_proof, we should use
1836 apply the "apply" tactic to proof and status
1838 let names = names_of_context context in
1839 print_endline (PP.pp proof names);
1842 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
1844 (* Printf.printf "OK, found a proof!\n"; *)
1845 (* (\* REMEMBER: we have to instantiate meta_proof, we should use *)
1846 (* apply the "apply" tactic to proof and status *)
1848 (* let names = names_of_context context in *)
1849 (* print_endline (PP.pp proof names); *)
1850 (* print_endline (PP.ppterm proof); *)
1852 print_endline (string_of_float (finish -. start));
1854 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n\n"
1855 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
1857 (fst (CicReduction.are_convertible
1858 context type_of_goal ty ug)));
1860 Printf.printf "\nEXCEPTION!!! %s\n" (Printexc.to_string e);
1861 Printf.printf "MAXMETA USED: %d\n" !maxmeta;
1862 print_endline (string_of_float (finish -. start));
1866 | ParamodulationSuccess (None, env) ->
1867 Printf.printf "Success, but no proof?!?\n\n"
1869 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
1870 "forward_simpl_new_time: %.9f\n" ^^
1871 "backward_simpl_time: %.9f\n")
1872 !infer_time !forward_simpl_time !forward_simpl_new_time
1873 !backward_simpl_time;
1874 Printf.printf "passive_maintainance_time: %.9f\n"
1875 !passive_maintainance_time;
1876 Printf.printf " successful unification/matching time: %.9f\n"
1877 !Indexing.match_unif_time_ok;
1878 Printf.printf " failed unification/matching time: %.9f\n"
1879 !Indexing.match_unif_time_no;
1880 Printf.printf " indexing retrieval time: %.9f\n"
1881 !Indexing.indexing_retrieval_time;
1882 Printf.printf " demodulate_term.build_newtarget_time: %.9f\n"
1883 !Indexing.build_newtarget_time;
1884 Printf.printf "derived %d clauses, kept %d clauses.\n"
1885 !derived_clauses !kept_clauses;
1887 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
1892 let default_depth = !maxdepth
1893 and default_width = !maxwidth;;
1896 dbd ?(full=false) ?(depth=default_depth) ?(width=default_width) status =
1897 let module C = Cic in
1901 let proof, goal = status in
1903 let uri, metasenv, meta_proof, term_to_prove = proof in
1904 let _, context, goal = CicUtil.lookup_meta goal' metasenv in
1905 let eq_indexes, equalities, maxm = find_equalities context proof in
1906 let new_meta_goal, metasenv, type_of_goal =
1908 CicMkImplicit.identity_relocation_list_for_metavariable context in
1909 let _, context, ty = CicUtil.lookup_meta goal' metasenv in
1911 (lazy (Printf.sprintf "\n\nTIPO DEL GOAL: %s\n" (CicPp.ppterm ty)));
1912 Cic.Meta (maxm+1, irl),
1913 (maxm+1, context, ty)::metasenv,
1916 let ugraph = CicUniv.empty_ugraph in
1917 let env = (metasenv, context, ugraph) in
1918 let goal = Inference.BasicProof new_meta_goal, [], goal in
1920 let lib_eq_uris, library_equalities, maxm =
1921 find_library_equalities dbd context (proof, goal') (maxm+2)
1925 let equalities = equalities @ library_equalities in
1928 (Printf.sprintf "equalities:\n%s\n"
1930 (List.map string_of_equality equalities))));
1931 debug_print (lazy "SIMPLYFYING EQUALITIES...");
1932 let rec simpl e others others_simpl =
1933 let active = others @ others_simpl in
1936 (fun t (_, e) -> Indexing.index t e)
1937 (Indexing.empty_table ()) active
1939 let res = forward_simplify env e (active, tbl) in
1943 | None -> simpl hd tl others_simpl
1944 | Some e -> simpl hd tl (e::others_simpl)
1948 | None -> others_simpl
1949 | Some e -> e::others_simpl
1952 match equalities with
1955 let others = List.map (fun e -> (Positive, e)) tl in
1957 List.rev (List.map snd (simpl (Positive, hd) others []))
1961 (Printf.sprintf "equalities AFTER:\n%s\n"
1963 (List.map string_of_equality res))));
1968 let thms = find_library_theorems dbd env (proof, goal') lib_eq_uris in
1969 let context_hyp = find_context_hypotheses env eq_indexes in
1973 let us = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
1974 UriManager.uri_of_string (us ^ "#xpointer(1/1/1)")
1976 let t = CicUtil.term_of_uri refl_equal in
1977 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
1984 "Theorems:\n-------------------------------------\n%s\n"
1989 "Term: %s, type: %s"
1990 (CicPp.ppterm t) (CicPp.ppterm ty))
1993 let active = make_active () in
1994 let passive = make_passive [(* term_equality *)] equalities in
1995 let start = Unix.gettimeofday () in
1996 let res = given_clause_fullred env [0, [goal]] theorems passive active in
1997 let finish = Unix.gettimeofday () in
1998 (res, finish -. start)
2001 | ParamodulationSuccess (Some proof (* goal *), env) ->
2002 debug_print (lazy "OK, found a proof!");
2003 (* let proof = Inference.build_proof_term goal in *)
2004 let proof = Inference.build_proof_term proof in
2005 let names = names_of_context context in
2008 match new_meta_goal with
2009 | C.Meta (i, _) -> i | _ -> assert false
2011 List.filter (fun (i, _, _) -> i <> i1 && i <> goal') metasenv
2016 CicTypeChecker.type_of_aux' newmetasenv context proof ugraph
2018 debug_print (lazy (CicPp.pp proof [](* names *)));
2022 "\nGOAL was: %s\nPROOF has type: %s\nconvertible?: %s\n"
2023 (CicPp.pp type_of_goal names) (CicPp.pp ty names)
2025 (fst (CicReduction.are_convertible
2026 context type_of_goal ty ug)))));
2027 let equality_for_replace i t1 =
2029 | C.Meta (n, _) -> n = i
2033 ProofEngineReduction.replace
2034 ~equality:equality_for_replace
2035 ~what:[goal'] ~with_what:[proof]
2040 (Printf.sprintf "status:\n%s\n%s\n%s\n%s\n"
2041 (match uri with Some uri -> UriManager.string_of_uri uri
2043 (print_metasenv newmetasenv)
2044 (CicPp.pp real_proof [](* names *))
2045 (CicPp.pp term_to_prove names)));
2046 ((uri, newmetasenv, real_proof, term_to_prove), [])
2047 with CicTypeChecker.TypeCheckerFailure _ ->
2048 debug_print (lazy "THE PROOF DOESN'T TYPECHECK!!!");
2049 debug_print (lazy (CicPp.pp proof names));
2050 raise (ProofEngineTypes.Fail
2051 "Found a proof, but it doesn't typecheck")
2053 debug_print (lazy (Printf.sprintf "\nTIME NEEDED: %.9f" time));
2056 raise (ProofEngineTypes.Fail "NO proof found")
2059 (* dummy function called within matita to trigger linkage *)
2063 (* UGLY SIDE EFFECT... *)
2064 if connect_to_auto then (
2065 AutoTactic.paramodulation_tactic := saturate;
2066 AutoTactic.term_is_equality := Inference.term_is_equality;