6 | Success of Cic.term option * environment
11 let symbols_of_equality (_, (_, left, right), _, _) =
12 TermSet.union (symbols_of_term left) (symbols_of_term right)
16 let symbols_of_equality ((_, (_, left, right, _), _, _) as equality) =
17 let m1 = symbols_of_term left in
22 let c = TermMap.find k res in
23 TermMap.add k (c+v) res
26 (symbols_of_term right) m1
28 (* Printf.printf "symbols_of_equality %s:\n" *)
29 (* (string_of_equality equality); *)
30 (* TermMap.iter (fun k v -> Printf.printf "%s: %d\n" (CicPp.ppterm k) v) m; *)
31 (* print_newline (); *)
36 module OrderedEquality =
38 type t = Inference.equality
41 match meta_convertibility_eq eq1 eq2 with
44 let _, (ty, left, right, _), _, _ = eq1
45 and _, (ty', left', right', _), _, _ = eq2 in
46 let weight_of t = fst (weight_of_term ~consider_metas:false t) in
47 let w1 = (weight_of ty) + (weight_of left) + (weight_of right)
48 and w2 = (weight_of ty') + (weight_of left') + (weight_of right') in
49 match Pervasives.compare w1 w2 with
50 | 0 -> Pervasives.compare eq1 eq2
54 module EqualitySet = Set.Make(OrderedEquality);;
57 let weight_age_ratio = ref 0;; (* settable by the user from the command line *)
58 let weight_age_counter = ref !weight_age_ratio;;
60 let symbols_ratio = ref 0;;
61 let symbols_counter = ref 0;;
64 let select env passive (active, _) =
65 let (neg_list, neg_set), (pos_list, pos_set), passive_table = passive in
67 List.filter (fun e -> e <> eq) l
69 if !weight_age_ratio > 0 then
70 weight_age_counter := !weight_age_counter - 1;
71 match !weight_age_counter with
73 weight_age_counter := !weight_age_ratio;
74 match neg_list, pos_list with
76 (* Negatives aren't indexed, no need to remove them... *)
78 ((tl, EqualitySet.remove hd neg_set), (pos, pos_set), passive_table)
80 let passive_table = Indexing.remove_index passive_table hd in
82 (([], neg_set), (tl, EqualitySet.remove hd pos_set), passive_table)
83 | _, _ -> assert false
85 | _ when (!symbols_counter > 0) && (EqualitySet.is_empty neg_set) -> (
86 symbols_counter := !symbols_counter - 1;
88 TermMap.fold (fun k v res -> res + v) map 0
92 let symbols = symbols_of_equality e in
93 let card = cardinality symbols in
94 let f equality (i, e) =
98 if TermMap.mem k symbols then
99 let c = TermMap.find k symbols in
100 let c1 = abs (c - v) in
105 (symbols_of_equality equality) (0, 0)
107 (* Printf.printf "equality: %s, common: %d, others: %d\n" *)
108 (* (string_of_equality ~env equality) common others; *)
109 let c = others + (abs (common - card)) in
110 if c < i then (c, equality)
113 let e1 = EqualitySet.min_elt pos_set in
118 if TermMap.mem k symbols then
119 let c = TermMap.find k symbols in
120 let c1 = abs (c - v) in
121 let c2 = v - (abs (c - v)) in
125 (symbols_of_equality e1) (0, 0)
127 (others + (abs (common - card))), e1
129 let _, current = EqualitySet.fold f pos_set initial in
130 (* Printf.printf "\nsymbols-based selection: %s\n\n" *)
131 (* (string_of_equality ~env current); *)
132 let passive_table = Indexing.remove_index passive_table current in
135 (remove current pos_list, EqualitySet.remove current pos_set),
138 let current = EqualitySet.min_elt pos_set in
141 (remove current pos_list, EqualitySet.remove current pos_set),
142 Indexing.remove_index passive_table current
144 (Positive, current), passive
147 symbols_counter := !symbols_ratio;
148 let set_selection set = EqualitySet.min_elt set in
149 if EqualitySet.is_empty neg_set then
150 let current = set_selection pos_set in
153 (remove current pos_list, EqualitySet.remove current pos_set),
154 Indexing.remove_index passive_table current
156 (Positive, current), passive
158 let current = set_selection neg_set in
160 (remove current neg_list, EqualitySet.remove current neg_set),
164 (Negative, current), passive
168 let make_passive neg pos =
169 let set_of equalities =
170 List.fold_left (fun s e -> EqualitySet.add e s) EqualitySet.empty equalities
172 let table = Hashtbl.create (List.length pos) in
175 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
184 let add_to_passive passive (new_neg, new_pos) =
185 let (neg_list, neg_set), (pos_list, pos_set), table = passive in
186 let ok set equality = not (EqualitySet.mem equality set) in
187 let neg = List.filter (ok neg_set) new_neg
188 and pos = List.filter (ok pos_set) new_pos in
189 let add set equalities =
190 List.fold_left (fun s e -> EqualitySet.add e s) set equalities
192 (neg @ neg_list, add neg_set neg),
193 (pos_list @ pos, add pos_set pos),
194 List.fold_left (fun tbl e -> Indexing.index tbl e) table pos
198 let passive_is_empty = function
199 | ([], _), ([], _), _ -> true
204 (* TODO: find a better way! *)
205 let maxmeta = ref 0;;
207 let infer env sign current (active_list, active_table) =
210 Indexing.superposition_left env active_table current, []
213 Indexing.superposition_right !maxmeta env active_table current in
215 let rec infer_positive table = function
217 | (Negative, equality)::tl ->
218 let res = Indexing.superposition_left env table equality in
219 let neg, pos = infer_positive table tl in
221 | (Positive, equality)::tl ->
223 Indexing.superposition_right !maxmeta env table equality in
225 let neg, pos = infer_positive table tl in
228 let curr_table = Indexing.index (Hashtbl.create 1) current in
229 let neg, pos = infer_positive curr_table active_list in
234 let contains_empty env (negative, positive) =
235 let metasenv, context, ugraph = env in
237 let (proof, _, _, _) =
239 (fun (proof, (ty, left, right, ordering), m, a) ->
240 fst (CicReduction.are_convertible context left right ugraph))
249 let forward_simplify env (sign, current) ?passive (active_list, active_table) =
250 let pl, passive_table =
253 | Some ((pn, _), (pp, _), pt) ->
254 let pn = List.map (fun e -> (Negative, e)) pn
255 and pp = List.map (fun e -> (Positive, e)) pp in
258 let all = active_list @ pl in
259 let rec find_duplicate sign current = function
261 | (s, eq)::tl when s = sign ->
262 if meta_convertibility_eq current eq then true
263 else find_duplicate sign current tl
264 | _::tl -> find_duplicate sign current tl
266 let demodulate table current =
267 let newmeta, newcurrent =
268 Indexing.demodulation !maxmeta env table current in
270 if is_identity env newcurrent then
271 if sign = Negative then Some (sign, newcurrent) else None
273 Some (sign, newcurrent)
276 let res = demodulate active_table current in
279 | Some (sign, newcurrent) ->
280 match passive_table with
282 | Some passive_table -> demodulate passive_table newcurrent
287 if find_duplicate s c all then
290 let pred (sign, eq) =
291 if sign <> s then false
292 else subsumption env c eq
294 if List.exists pred all then None
298 type fs_time_info_t = {
299 mutable build_all: float;
300 mutable demodulate: float;
301 mutable subsumption: float;
304 let fs_time_info = { build_all = 0.; demodulate = 0.; subsumption = 0. };;
307 let forward_simplify_new env (new_neg, new_pos) ?passive active =
308 let t1 = Unix.gettimeofday () in
310 let active_list, active_table = active in
311 let pl, passive_table =
314 | Some ((pn, _), (pp, _), pt) ->
315 let pn = List.map (fun e -> (Negative, e)) pn
316 and pp = List.map (fun e -> (Positive, e)) pp in
319 let all = active_list @ pl in
321 let t2 = Unix.gettimeofday () in
322 fs_time_info.build_all <- fs_time_info.build_all +. (t2 -. t1);
324 let demodulate table target =
325 let newmeta, newtarget = Indexing.demodulation !maxmeta env table target in
329 let f sign' target (sign, eq) =
330 if sign <> sign' then false
331 else subsumption env target eq
334 let t1 = Unix.gettimeofday () in
336 let new_neg, new_pos =
337 (List.filter (fun e -> not (List.exists (f Negative e) all)) new_neg,
338 List.filter (fun e -> not (List.exists (f Positive e) all)) new_pos)
341 let t2 = Unix.gettimeofday () in
342 fs_time_info.subsumption <- fs_time_info.subsumption +. (t2 -. t1);
343 let t1 = Unix.gettimeofday () in
345 let new_neg, new_pos =
346 let new_neg = List.map (demodulate active_table) new_neg
347 and new_pos = List.map (demodulate active_table) new_pos in
348 match passive_table with
349 | None -> new_neg, new_pos
350 | Some passive_table ->
351 List.map (demodulate passive_table) new_neg,
352 List.map (demodulate passive_table) new_pos
355 let t2 = Unix.gettimeofday () in
356 fs_time_info.demodulate <- fs_time_info.demodulate +. (t2 -. t1);
361 if not (Inference.is_identity env e) then EqualitySet.add e s else s)
362 EqualitySet.empty new_pos
364 let new_pos = EqualitySet.elements new_pos_set in
367 (* (List.filter (fun e -> not (List.exists (f Negative e) all)) new_neg, *)
368 (* List.filter (fun e -> not (List.exists (f Positive e) all)) new_pos) *)
374 let backward_simplify_active env (new_neg, new_pos) active =
375 let active_list, active_table = active in
376 let new_pos, new_table =
378 (fun (l, t) e -> (Positive, e)::l, Indexing.index t e)
379 ([], Hashtbl.create (List.length new_pos)) new_pos
381 let active_list, newa =
383 (fun (s, equality) (res, newn) ->
384 match forward_simplify env (s, equality) (new_pos, new_table) with
394 List.exists (fun (s, e) -> meta_convertibility_eq eq1 e) where
398 (fun (s, eq) (res, tbl) ->
399 if (is_identity env eq) || (find eq res) then
402 (s, eq)::res, if s = Negative then tbl else Indexing.index tbl eq)
403 active_list ([], Hashtbl.create (List.length active_list)),
405 (fun (s, eq) (n, p) ->
406 if (s <> Negative) && (is_identity env eq) then
409 if s = Negative then eq::n, p
414 | [], [] -> active, None
415 | _ -> active, Some newa
419 let backward_simplify_passive env (new_neg, new_pos) passive =
420 let new_pos, new_table =
422 (fun (l, t) e -> (Positive, e)::l, Indexing.index t e)
423 ([], Hashtbl.create (List.length new_pos)) new_pos
425 let (nl, ns), (pl, ps), passive_table = passive in
426 let f sign equality (resl, ress, newn) =
427 match forward_simplify env (sign, equality) (new_pos, new_table) with
428 | None -> resl, EqualitySet.remove equality ress, newn
431 equality::resl, ress, newn
433 let ress = EqualitySet.remove equality ress in
436 let nl, ns, newn = List.fold_right (f Negative) nl ([], ns, [])
437 and pl, ps, newp = List.fold_right (f Positive) pl ([], ps, []) in
440 (fun tbl e -> Indexing.index tbl e) (Hashtbl.create (List.length pl)) pl
442 match newn, newp with
443 | [], [] -> ((nl, ns), (pl, ps), passive_table), None
444 | _, _ -> ((nl, ns), (pl, ps), passive_table), Some (newn, newp)
448 let backward_simplify env new' ?passive active =
449 let active, newa = backward_simplify_active env new' active in
452 active, (make_passive [] []), newa, None
455 backward_simplify_passive env new' passive in
456 active, passive, newa, newp
460 let infer_time = ref 0.;;
461 let forward_simpl_time = ref 0.;;
462 let backward_simpl_time = ref 0.;;
465 let rec given_clause env passive active =
466 match passive_is_empty passive with
469 let (sign, current), passive = select env passive active in
470 match forward_simplify env (sign, current) ~passive active with
472 given_clause env passive active
473 | Some (sign, current) ->
474 if (sign = Negative) && (is_identity env current) then (
475 Printf.printf "OK!!! %s %s" (string_of_sign sign)
476 (string_of_equality ~env current);
478 let proof, _, _, _ = current in
479 Success (Some proof, env)
481 print_endline "\n================================================";
482 Printf.printf "selected: %s %s"
483 (string_of_sign sign) (string_of_equality ~env current);
486 let t1 = Unix.gettimeofday () in
487 let new' = infer env sign current active in
488 let t2 = Unix.gettimeofday () in
489 infer_time := !infer_time +. (t2 -. t1);
491 let res, proof = contains_empty env new' in
495 let t1 = Unix.gettimeofday () in
496 let new' = forward_simplify_new env new' active in
497 let t2 = Unix.gettimeofday () in
499 forward_simpl_time := !forward_simpl_time +. (t2 -. t1)
505 let t1 = Unix.gettimeofday () in
506 let active, _, newa, _ =
507 backward_simplify env ([], [current]) active
509 let t2 = Unix.gettimeofday () in
510 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
514 let al, tbl = active in
515 let nn = List.map (fun e -> Negative, e) n in
520 Indexing.index tbl e)
526 Printf.printf "active:\n%s\n"
529 (fun (s, e) -> (string_of_sign s) ^ " " ^
530 (string_of_equality ~env e)) (fst active))));
536 Printf.printf "new':\n%s\n"
539 (fun e -> "Negative " ^
540 (string_of_equality ~env e)) neg) @
542 (fun e -> "Positive " ^
543 (string_of_equality ~env e)) pos)));
546 match contains_empty env new' with
549 let al, tbl = active in
551 | Negative -> (sign, current)::al, tbl
553 al @ [(sign, current)], Indexing.index tbl current
555 let passive = add_to_passive passive new' in
556 let (_, ns), (_, ps), _ = passive in
557 Printf.printf "passive:\n%s\n"
559 ((List.map (fun e -> "Negative " ^
560 (string_of_equality ~env e))
561 (EqualitySet.elements ns)) @
562 (List.map (fun e -> "Positive " ^
563 (string_of_equality ~env e))
564 (EqualitySet.elements ps))));
566 given_clause env passive active
573 let rec given_clause_fullred env passive active =
574 match passive_is_empty passive with
577 let (sign, current), passive = select env passive active in
578 match forward_simplify env (sign, current) ~passive active with
580 given_clause_fullred env passive active
581 | Some (sign, current) ->
582 if (sign = Negative) && (is_identity env current) then (
583 Printf.printf "OK!!! %s %s" (string_of_sign sign)
584 (string_of_equality ~env current);
586 let proof, _, _, _ = current in
587 Success (Some proof, env)
589 print_endline "\n================================================";
590 Printf.printf "selected: %s %s"
591 (string_of_sign sign) (string_of_equality ~env current);
594 let t1 = Unix.gettimeofday () in
595 let new' = infer env sign current active in
596 let t2 = Unix.gettimeofday () in
597 infer_time := !infer_time +. (t2 -. t1);
600 if is_identity env current then active
602 let al, tbl = active in
604 | Negative -> (sign, current)::al, tbl
605 | Positive -> al @ [(sign, current)], Indexing.index tbl current
607 let rec simplify new' active passive =
608 let t1 = Unix.gettimeofday () in
609 let new' = forward_simplify_new env new' ~passive active in
610 let t2 = Unix.gettimeofday () in
611 forward_simpl_time := !forward_simpl_time +. (t2 -. t1);
612 let t1 = Unix.gettimeofday () in
613 let active, passive, newa, retained =
614 backward_simplify env new' ~passive active in
615 let t2 = Unix.gettimeofday () in
616 backward_simpl_time := !backward_simpl_time +. (t2 -. t1);
617 match newa, retained with
618 | None, None -> active, passive, new'
620 | None, Some (n, p) ->
622 simplify (nn @ n, np @ p) active passive
623 | Some (n, p), Some (rn, rp) ->
625 simplify (nn @ n @ rn, np @ p @ rp) active passive
627 let active, passive, new' = simplify new' active passive in
629 Printf.printf "active:\n%s\n"
632 (fun (s, e) -> (string_of_sign s) ^ " " ^
633 (string_of_equality ~env e)) (fst active))));
639 Printf.printf "new':\n%s\n"
642 (fun e -> "Negative " ^
643 (string_of_equality ~env e)) neg) @
645 (fun e -> "Positive " ^
646 (string_of_equality ~env e)) pos)));
649 match contains_empty env new' with
651 let passive = add_to_passive passive new' in
652 given_clause_fullred env passive active
659 let get_from_user () =
660 let dbd = Mysql.quick_connect
661 ~host:"localhost" ~user:"helm" ~database:"mowgli" () in
663 match read_line () with
667 let term_string = String.concat "\n" (get ()) in
668 let env, metasenv, term, ugraph =
669 List.nth (Disambiguate.Trivial.disambiguate_string dbd term_string) 0
671 term, metasenv, ugraph
675 let given_clause_ref = ref given_clause;;
679 let module C = Cic in
680 let module T = CicTypeChecker in
681 let module PET = ProofEngineTypes in
682 let module PP = CicPp in
683 let term, metasenv, ugraph = get_from_user () in
684 let proof = None, (1, [], term)::metasenv, C.Meta (1, []), term in
686 PET.apply_tactic (PrimitiveTactics.intros_tac ()) (proof, 1) in
687 let goal = List.nth goals 0 in
688 let _, metasenv, meta_proof, _ = proof in
689 let _, context, goal = CicUtil.lookup_meta goal metasenv in
690 let equalities, maxm = find_equalities context proof in
691 maxmeta := maxm; (* TODO ugly!! *)
692 let env = (metasenv, context, ugraph) in
694 let term_equality = equality_of_term meta_proof goal in
695 let meta_proof, (eq_ty, left, right, ordering), _, _ = term_equality in
696 let active = make_active () in
697 let passive = make_passive [term_equality] equalities in
698 Printf.printf "\ncurrent goal: %s\n"
699 (string_of_equality ~env term_equality);
700 Printf.printf "\ncontext:\n%s\n" (PP.ppcontext context);
701 Printf.printf "\nmetasenv:\n%s\n" (print_metasenv metasenv);
702 Printf.printf "\nequalities:\n%s\n"
705 (string_of_equality ~env)
707 print_endline "--------------------------------------------------";
708 let start = Unix.gettimeofday () in
710 let res = !given_clause_ref env passive active in
711 let finish = Unix.gettimeofday () in
714 Printf.printf "NO proof found! :-(\n\n"
715 | Success (Some proof, env) ->
716 Printf.printf "OK, found a proof!:\n%s\n%.9f\n"
717 (PP.pp proof (names_of_context context))
719 Printf.printf ("infer_time: %.9f\nforward_simpl_time: %.9f\n" ^^
720 "backward_simpl_time: %.9f\n")
721 !infer_time !forward_simpl_time !backward_simpl_time;
722 Printf.printf ("forward_simpl_details:\n build_all: %.9f\n" ^^
723 " demodulate: %.9f\n subsumption: %.9f\n")
724 fs_time_info.build_all fs_time_info.demodulate
725 fs_time_info.subsumption;
726 | Success (None, env) ->
727 Printf.printf "Success, but no proof?!?\n\n"
729 print_endline ("EXCEPTION: " ^ (Printexc.to_string exc));
733 let configuration_file = ref "../../gTopLevel/gTopLevel.conf.xml";;
736 let set_ratio v = weight_age_ratio := (v+1); weight_age_counter := (v+1)
737 and set_sel v = symbols_ratio := v; symbols_counter := v;
738 and set_conf f = configuration_file := f
739 and set_lpo () = Utils.compare_terms := lpo
740 and set_kbo () = Utils.compare_terms := nonrec_kbo
741 and set_fullred () = given_clause_ref := given_clause_fullred
742 and set_use_index v = Indexing.use_index := v
745 "-f", Arg.Unit set_fullred, "Use full-reduction strategy";
747 "-r", Arg.Int set_ratio, "Weight-Age equality selection ratio (default: 0)";
749 "-s", Arg.Int set_sel,
750 "symbols-based selection ratio (relative to the weight ratio)";
752 "-c", Arg.String set_conf, "Configuration file (for the db connection)";
754 "-lpo", Arg.Unit set_lpo, "Use lpo term ordering";
756 "-kbo", Arg.Unit set_kbo, "Use (non-recursive) kbo term ordering (default)";
758 "-i", Arg.Bool set_use_index, "Use indexing yes/no (default: yes)";
759 ] (fun a -> ()) "Usage:"
761 Helm_registry.load_from !configuration_file;