4 (* profiling statistics... *)
5 let infer_time = ref 0.;;
6 let forward_simpl_time = ref 0.;;
7 let backward_simpl_time = ref 0.;;
9 (* limited-resource-strategy related globals *)
10 let processed_clauses = ref 0;; (* number of equalities selected so far... *)
11 let time_limit = ref 0.;; (* in seconds, settable by the user... *)
12 let start_time = ref 0.;; (* time at which the execution started *)
13 let elapsed_time = ref 0.;;
14 let maximal_retained_equality = ref None;;
16 (* equality-selection related globals *)
17 let weight_age_ratio = ref 0;; (* settable by the user from the command line *)
18 let weight_age_counter = ref !weight_age_ratio;;
19 let symbols_ratio = ref 0;;
20 let symbols_counter = ref 0;;
22 (* index of the greatest Cic.Meta created - TODO: find a better way! *)
28 | Success of Cic.term option * environment
33 let symbols_of_equality (_, (_, left, right), _, _) =
34 TermSet.union (symbols_of_term left) (symbols_of_term right)
38 let symbols_of_equality ((_, (_, left, right, _), _, _) as equality) =
39 let m1 = symbols_of_term left in
44 let c = TermMap.find k res in
45 TermMap.add k (c+v) res
48 (symbols_of_term right) m1
50 (* Printf.printf "symbols_of_equality %s:\n" *)
51 (* (string_of_equality equality); *)
52 (* TermMap.iter (fun k v -> Printf.printf "%s: %d\n" (CicPp.ppterm k) v) m; *)
53 (* print_newline (); *)
58 module OrderedEquality = struct
59 type t = Inference.equality
62 match meta_convertibility_eq eq1 eq2 with
65 let _, (ty, left, right, _), _, _ = eq1
66 and _, (ty', left', right', _), _, _ = eq2 in
67 let weight_of t = fst (weight_of_term ~consider_metas:false t) in
68 let w1 = (weight_of ty) + (weight_of left) + (weight_of right)
69 and w2 = (weight_of ty') + (weight_of left') + (weight_of right') in
70 match Pervasives.compare w1 w2 with
71 | 0 -> Pervasives.compare eq1 eq2
75 module EqualitySet = Set.Make(OrderedEquality);;
78 let select env passive (active, _) =
79 processed_clauses := !processed_clauses + 1;
81 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
83 List.filter (fun e -> e <> eq) l
85 if !weight_age_ratio > 0 then
86 weight_age_counter := !weight_age_counter - 1;
87 match !weight_age_counter with
89 weight_age_counter := !weight_age_ratio;
90 match neg_list, pos_list with
92 (* Negatives aren't indexed, no need to remove them... *)
94 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
96 let passive_table = Indexing.remove_index passive_table hd in
98 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
99 | _, _ -> assert false
101 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) -> (
102 symbols_counter := !symbols_counter - 1;
103 let cardinality map =
104 TermMap.fold (fun k v res -> res + v) map 0
107 | (Negative, e)::_ ->
108 let symbols = symbols_of_equality e in
109 let card = cardinality symbols in
110 let f equality (i, e) =
114 if TermMap.mem k symbols then
115 let c = TermMap.find k symbols in
116 let c1 = abs (c - v) in
121 (symbols_of_equality equality) (0, 0)
123 let c = others + (abs (common - card)) in
124 if c < i then (c, equality)
127 let e1 = EqualitySet.min_elt pos_set in
132 if TermMap.mem k symbols then
133 let c = TermMap.find k symbols in
134 let c1 = abs (c - v) in
135 let c2 = v - (abs (c - v)) in
139 (symbols_of_equality e1) (0, 0)
141 (others + (abs (common - card))), e1
143 let _, current = EqualitySet.fold f pos_set initial in
144 (* Printf.printf "\nsymbols-based selection: %s\n\n" *)
145 (* (string_of_equality ~env current); *)
146 let passive_table = Indexing.remove_index passive_table current in
149 (remove current pos_list, EqualitySet.remove current pos_set),
152 let current = EqualitySet.min_elt pos_set in
155 (remove current pos_list, EqualitySet.remove current pos_set),
156 Indexing.remove_index passive_table current
158 (Positive, current), passive
161 symbols_counter := !symbols_ratio;
162 let set_selection set = EqualitySet.min_elt set in
163 if EqualitySet.is_empty neg_set then
164 let current = set_selection pos_set in
167 (remove current pos_list, EqualitySet.remove current pos_set),
168 Indexing.remove_index passive_table current
170 (Positive, current), passive
172 let current = set_selection neg_set in
174 (remove current neg_list, EqualitySet.remove current neg_set),
178 (Negative, current), passive
182 let make_passive neg pos =
183 let set_of equalities =
184 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
186 let table = Indexing.empty_table () in
189 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
194 [], Indexing.empty_table ()
198 let add_to_passive passive (new_neg, new_pos) =
199 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
200 let ok set equality = not (EqualitySet.mem equality set) in
201 let neg = List.filter (ok neg_set) new_neg
202 and pos = List.filter (ok pos_set) new_pos in
203 let add set equalities =
204 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
206 (neg @ neg_list, add neg_set neg),
207 (pos_list @ pos, add pos_set pos),
208 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
212 let passive_is_empty = function
213 | ([], _), ([], _), _ -> true
218 let size_of_passive ((_, ns), (_, ps), _) =
219 (EqualitySet.cardinal ns) + (EqualitySet.cardinal ps)
223 let prune_passive (((nl, ns), (pl, ps), tbl) as passive) =
225 int_of_float ((!time_limit -. !elapsed_time) /.
226 (!elapsed_time *. (float_of_int !weight_age_ratio)))
228 let in_weight = !processed_clauses * !weight_age_ratio * f
229 and in_age = !processed_clauses * f in
233 let e = EqualitySet.min_elt s in
234 let w, s' = pickw (w-1) (EqualitySet.remove e s) in
235 w, EqualitySet.add e s'
241 let in_weight, ns = pickw in_weight ns in
242 let _, ps = pickw in_weight ps in
243 let rec picka w s l =
247 | hd::tl when not (EqualitySet.mem hd s) ->
248 let w, s, l = picka (w-1) s tl in
249 w, EqualitySet.add hd s, hd::l
251 let w, s, l = picka w s tl in
256 let in_age, ns, nl = picka in_age ns nl in
257 let _, ps, pl = picka in_age ps pl in
258 if not (EqualitySet.is_empty ps) then
259 maximal_retained_equality := Some (EqualitySet.max_elt ps);
262 (fun e tbl -> Indexing.index tbl e) ps (Indexing.empty_table ()) in
263 (nl, ns), (pl, ps), tbl
267 let infer env sign current (active_list, active_table) =
268 let new_neg, new_pos =
271 Indexing.superposition_left env active_table current, []
274 Indexing.superposition_right !maxmeta env active_table current in
276 let rec infer_positive table = function
278 | (Negative, equality)::tl ->
279 let res = Indexing.superposition_left env table equality in
280 let neg, pos = infer_positive table tl in
282 | (Positive, equality)::tl ->
284 Indexing.superposition_right !maxmeta env table equality in
286 let neg, pos = infer_positive table tl in
289 let curr_table = Indexing.index (Indexing.empty_table ()) current in
290 let neg, pos = infer_positive curr_table active_list in
293 match !maximal_retained_equality with
294 | None -> new_neg, new_pos
297 List.filter (fun e -> OrderedEquality.compare e eq <= 0) new_pos in
302 let contains_empty env (negative, positive) =
303 let metasenv, context, ugraph = env in
305 let (proof, _, _, _) =
307 (fun (proof, (ty, left, right, ordering), m, a) ->
308 fst (CicReduction.are_convertible context left right ugraph))
317 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
318 let pl, passive_table =
321 | Some ((pn, _), (pp, _), pt) ->
322 let pn = List.map (fun e -> (Negative, e)) pn
323 and pp = List.map (fun e -> (Positive, e)) pp in
326 let all = active_list @ pl in
327 let rec find_duplicate sign current = function
329 | (s, eq)::tl when s = sign ->
330 if meta_convertibility_eq current eq then true
331 else find_duplicate sign current tl
332 | _::tl -> find_duplicate sign current tl
334 let demodulate table current =
335 let newmeta, newcurrent =
336 Indexing.demodulation !maxmeta env table current in
338 if is_identity env newcurrent then
339 if sign = Negative then Some (sign, newcurrent) else None
341 Some (sign, newcurrent)
344 let res = demodulate active_table current in
347 | Some (sign, newcurrent) ->
348 match passive_table with
350 | Some passive_table -> demodulate passive_table newcurrent
355 if find_duplicate s c all then
359 (* if s = Utils.Negative then *)
362 (* if Indexing.subsumption env active_table c then *)
365 (* match passive_table with *)
367 (* | Some passive_table -> *)
368 (* if Indexing.subsumption env passive_table c then *)
374 (* let pred (sign, eq) = *)
375 (* if sign <> s then false *)
376 (* else subsumption env c eq *)
378 (* if List.exists pred all then None *)
382 type fs_time_info_t = {
383 mutable build_all: float;
384 mutable demodulate: float;
385 mutable subsumption: float;
388 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
391 let forward_simplify_new env (new_neg, new_pos) ?passive active =
392 let t1 = Unix.gettimeofday () in
394 let active_list, active_table = active in
395 let pl, passive_table =
398 | Some ((pn, _), (pp, _), pt) ->
399 let pn = List.map (fun e -> (Negative, e)) pn
400 and pp = List.map (fun e -> (Positive, e)) pp in
403 let all = active_list @ pl in
405 let t2 = Unix.gettimeofday () in
406 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
408 let demodulate table target =
409 let newmeta, newtarget = Indexing.demodulation !maxmeta env table target in
413 (* let f sign' target (sign, eq) = *)
414 (* if sign <> sign' then false *)
415 (* else subsumption env target eq *)
418 let t1 = Unix.gettimeofday () in
420 let new_neg, new_pos =
421 let new_neg = List.map (demodulate active_table) new_neg
422 and new_pos = List.map (demodulate active_table) new_pos in
423 match passive_table with
424 | None -> new_neg, new_pos
425 | Some passive_table ->
426 List.map (demodulate passive_table) new_neg,
427 List.map (demodulate passive_table) new_pos
430 let t2 = Unix.gettimeofday () in
431 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
436 if not (Inference.is_identity env e) then EqualitySet.add e s else s)
437 EqualitySet.empty new_pos
439 let new_pos = EqualitySet.elements new_pos_set in
442 match passive_table with
444 (fun e -> not (Indexing.subsumption env active_table e))
445 | Some passive_table ->
446 (fun e -> not ((Indexing.subsumption env active_table e) ||
447 (Indexing.subsumption env passive_table e)))
450 let t1 = Unix.gettimeofday () in
452 (* let new_neg, new_pos = *)
453 (* List.filter subs new_neg, *)
454 (* List.filter subs new_pos *)
457 (* let new_neg, new_pos = *)
458 (* (List.filter (fun e -> not (List.exists (f Negative e) all)) new_neg, *)
459 (* List.filter (fun e -> not (List.exists (f Positive e) all)) new_pos) *)
462 let t2 = Unix.gettimeofday () in
463 fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1);
467 (* (List.filter (fun e -> not (List.exists (f Negative e) all)) new_neg, *)
468 (* List.filter (fun e -> not (List.exists (f Positive e) all)) new_pos) *)
474 let backward_simplify_active env (new_neg, new_pos) active =
475 let active_list, active_table = active in
476 let new_pos, new_table =
478 (fun (l, t) e -> (Positive, e)::l, Indexing.index t e)
479 ([], Indexing.empty_table ()) new_pos
481 let active_list, newa =
483 (fun (s, equality) (res, newn) ->
484 match forward_simplify env (s, equality) (new_pos, new_table) with
494 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
498 (fun (s, eq) (res, tbl) ->
499 if (is_identity env eq) || (find eq res) then
502 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
503 active_list ([], Indexing.empty_table ()),
505 (fun (s, eq) (n, p) ->
506 if (s <> Negative) && (is_identity env eq) then
509 if s = Negative then eq::n, p
514 | [], [] -> active, None
515 | _ -> active, Some newa
519 let backward_simplify_passive env (new_neg, new_pos) passive =
520 let new_pos, new_table =
522 (fun (l, t) e -> (Positive, e)::l, Indexing.index t e)
523 ([], Indexing.empty_table ()) new_pos
525 let (nl, ns), (pl, ps), passive_table = passive in
526 let f sign equality (resl, ress, newn) =
527 match forward_simplify env (sign, equality) (new_pos, new_table) with
528 | None -> resl, EqualitySet.remove equality ress, newn
531 equality::resl, ress, newn
533 let ress = EqualitySet.remove equality ress in
536 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
537 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
540 (fun tbl e -> Indexing.index tbl e) (Indexing.empty_table ()) pl
542 match newn, newp with
543 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
544 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
548 let backward_simplify env new' ?passive active =
549 let active, newa = backward_simplify_active env new' active in
552 active, (make_passive [] []), newa, None
555 backward_simplify_passive env new' passive in
556 active, passive, newa, newp
560 let get_selection_estimate () =
561 elapsed_time := (Unix.gettimeofday ()) -. !start_time;
562 !processed_clauses * (int_of_float (!time_limit /. !elapsed_time))
566 let rec given_clause env passive active =
567 let selection_estimate = get_selection_estimate () in
568 let kept = size_of_passive passive in
570 if !time_limit = 0. then
572 else if !elapsed_time > !time_limit then
574 else if kept > selection_estimate then
575 prune_passive passive
580 match passive_is_empty passive with
583 let (sign, current), passive = select env passive active in
584 match forward_simplify env (sign, current) ~passive active with
586 given_clause env passive active
587 | Some (sign, current) ->
588 if (sign = Negative) && (is_identity env current) then (
589 Printf.printf "OK!!! %s %s" (string_of_sign sign)
590 (string_of_equality ~env current);
592 let proof, _, _, _ = current in
593 Success (Some proof, env)
595 print_endline "\n================================================";
596 Printf.printf "selected: %s %s"
597 (string_of_sign sign) (string_of_equality ~env current);
600 let t1 = Unix.gettimeofday () in
601 let new' = infer env sign current active in
602 let t2 = Unix.gettimeofday () in
603 infer_time := !infer_time +. (t2 -. t1);
605 let res, proof = contains_empty env new' in
609 let t1 = Unix.gettimeofday () in
610 let new' = forward_simplify_new env new' active in
611 let t2 = Unix.gettimeofday () in
613 forward_simpl_time := !forward_simpl_time +. (t2 -. t1)
619 let t1 = Unix.gettimeofday () in
620 let active, _, newa, _ =
621 backward_simplify env ([], [current]) active
623 let t2 = Unix.gettimeofday () in
624 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
628 let al, tbl = active in
629 let nn = List.map (fun e -> Negative, e) n in
634 Indexing.index tbl e)
640 Printf.printf "active:\n%s\n"
643 (fun (s, e) -> (string_of_sign s) ^ " " ^
644 (string_of_equality ~env e)) (fst active))));
650 Printf.printf "new':\n%s\n"
653 (fun e -> "Negative " ^
654 (string_of_equality ~env e)) neg) @
656 (fun e -> "Positive " ^
657 (string_of_equality ~env e)) pos)));
660 match contains_empty env new' with
663 let al, tbl = active in
665 | Negative -> (sign, current)::al, tbl
667 al @ [(sign, current)], Indexing.index tbl current
669 let passive = add_to_passive passive new' in
670 let (_, ns), (_, ps), _ = passive in
671 Printf.printf "passive:\n%s\n"
673 ((List.map (fun e -> "Negative " ^
674 (string_of_equality ~env e))
675 (EqualitySet.elements ns)) @
676 (List.map (fun e -> "Positive " ^
677 (string_of_equality ~env e))
678 (EqualitySet.elements ps))));
680 given_clause env passive active
687 let rec given_clause_fullred env passive active =
688 match passive_is_empty passive with
691 let (sign, current), passive = select env passive active in
692 match forward_simplify env (sign, current) ~passive active with
694 given_clause_fullred env passive active
695 | Some (sign, current) ->
696 if (sign = Negative) && (is_identity env current) then (
697 Printf.printf "OK!!! %s %s" (string_of_sign sign)
698 (string_of_equality ~env current);
700 let proof, _, _, _ = current in
701 Success (Some proof, env)
703 print_endline "\n================================================";
704 Printf.printf "selected: %s %s"
705 (string_of_sign sign) (string_of_equality ~env current);
708 let t1 = Unix.gettimeofday () in
709 let new' = infer env sign current active in
710 let t2 = Unix.gettimeofday () in
711 infer_time := !infer_time +. (t2 -. t1);
714 if is_identity env current then active
716 let al, tbl = active in
718 | Negative -> (sign, current)::al, tbl
719 | Positive -> al @ [(sign, current)], Indexing.index tbl current
721 let rec simplify new' active passive =
722 let t1 = Unix.gettimeofday () in
723 let new' = forward_simplify_new env new' ~passive active in
724 let t2 = Unix.gettimeofday () in
725 forward_simpl_time := !forward_simpl_time +. (t2 -. t1);
726 let t1 = Unix.gettimeofday () in
727 let active, passive, newa, retained =
728 backward_simplify env new' ~passive active in
729 let t2 = Unix.gettimeofday () in
730 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
731 match newa, retained with
732 | None, None -> active, passive, new'
734 | None, Some (n, p) ->
736 simplify (nn @ n, np @ p) active passive
737 | Some (n, p), Some (rn, rp) ->
739 simplify (nn @ n @ rn, np @ p @ rp) active passive
741 let active, passive, new' = simplify new' active passive in
743 Printf.printf "active:\n%s\n"
746 (fun (s, e) -> (string_of_sign s) ^ " " ^
747 (string_of_equality ~env e)) (fst active))));
753 Printf.printf "new':\n%s\n"
756 (fun e -> "Negative " ^
757 (string_of_equality ~env e)) neg) @
759 (fun e -> "Positive " ^
760 (string_of_equality ~env e)) pos)));
763 match contains_empty env new' with
765 let passive = add_to_passive passive new' in
766 given_clause_fullred env passive active
773 let get_from_user () =
774 let dbd = Mysql.quick_connect
775 ~host:"localhost" ~user:"helm" ~database:"mowgli" () in
777 match read_line () with
781 let term_string = String.concat "\n" (get ()) in
782 let env, metasenv, term, ugraph =
783 List.nth (Disambiguate.Trivial.disambiguate_string dbd term_string) 0
785 term, metasenv, ugraph
789 let given_clause_ref = ref given_clause;;
793 let module C = Cic in
794 let module T = CicTypeChecker in
795 let module PET = ProofEngineTypes in
796 let module PP = CicPp in
797 let term, metasenv, ugraph = get_from_user () in
798 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
800 PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
801 let goal = List.nth goals 0 in
802 let _, metasenv, meta_proof, _ = proof in
803 let _, context, goal = CicUtil.lookup_meta goal metasenv in
804 let equalities, maxm = find_equalities context proof in
805 maxmeta := maxm; (* TODO ugly!! *)
806 let env = (metasenv, context, ugraph) in
808 let term_equality = equality_of_term meta_proof goal in
809 let meta_proof, (eq_ty, left, right, ordering), _, _ = term_equality in
810 let active = make_active () in
811 let passive = make_passive [term_equality] equalities in
812 Printf.printf "\ncurrent goal: %s\n"
813 (string_of_equality ~env term_equality);
814 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
815 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
816 Printf.printf "\nequalities:\n%s\n"
819 (string_of_equality ~env)
821 print_endline "--------------------------------------------------";
822 let start = Unix.gettimeofday () in
824 start_time := Unix.gettimeofday ();
825 let res = !given_clause_ref env passive active in
826 let finish = Unix.gettimeofday () in
829 Printf.printf "NO proof found! :-(\n\n"
830 | Success (Some proof, env) ->
831 Printf.printf "OK, found a proof!:\n%s\n%.9f\n"
832 (PP.pp proof (names_of_context context))
834 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
835 "backward_simpl_time: %.9f\n")
836 !infer_time !forward_simpl_time !backward_simpl_time;
837 Printf.printf ("forward_simpl_details:\n build_all: %.9f\n" ^^
838 " demodulate: %.9f\n subsumption: %.9f\n")
839 fs_time_info.build_all fs_time_info.demodulate
840 fs_time_info.subsumption;
841 | Success (None, env) ->
842 Printf.printf "Success, but no proof?!?\n\n"
844 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
849 let configuration_file = ref "../../gTopLevel/gTopLevel.conf.xml";;
852 let set_ratio v = weight_age_ratio := (v+1); weight_age_counter := (v+1)
853 and set_sel v = symbols_ratio := v; symbols_counter := v;
854 and set_conf f = configuration_file := f
855 and set_lpo () = Utils.compare_terms := lpo
856 and set_kbo () = Utils.compare_terms := nonrec_kbo
857 and set_fullred () = given_clause_ref := given_clause_fullred
858 and set_time_limit v = time_limit := float_of_int v
861 "-f", Arg.Unit set_fullred, "Use full-reduction strategy";
863 "-r", Arg.Int set_ratio, "Weight-Age equality selection ratio (default: 0)";
865 "-s", Arg.Int set_sel,
866 "symbols-based selection ratio (relative to the weight ratio)";
868 "-c", Arg.String set_conf, "Configuration file (for the db connection)";
870 "-lpo", Arg.Unit set_lpo, "Use lpo term ordering";
872 "-kbo", Arg.Unit set_kbo, "Use (non-recursive) kbo term ordering (default)";
874 "-l", Arg.Int set_time_limit, "Time limit (in seconds)";
875 ] (fun a -> ()) "Usage:"
877 Helm_registry.load_from !configuration_file;