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/.
26 let debug_print = Utils.debug_print;;
29 type retrieval_mode = Matching | Unification;;
32 let print_candidates mode term res =
36 Printf.printf "| candidates Matching %s\n" (CicPp.ppterm term)
38 Printf.printf "| candidates Unification %s\n" (CicPp.ppterm term)
44 Printf.sprintf "| (%s, %s)" (Utils.string_of_pos p)
45 (Inference.string_of_equality e))
51 let indexing_retrieval_time = ref 0.;;
54 (* let my_apply_subst subst term = *)
55 (* let module C = Cic in *)
56 (* let lookup lift_amount meta = *)
58 (* | C.Meta (i, _) -> ( *)
60 (* let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in *)
61 (* (\* CicSubstitution.lift lift_amount *\)t *)
62 (* with Not_found -> meta *)
64 (* | _ -> assert false *)
66 (* let rec apply_aux lift_amount = function *)
67 (* | C.Meta (i, l) as t -> lookup lift_amount t *)
68 (* | C.Appl l -> C.Appl (List.map (apply_aux lift_amount) l) *)
69 (* | C.Prod (nn, s, t) -> *)
70 (* C.Prod (nn, apply_aux lift_amount s, apply_aux (lift_amount+1) t) *)
71 (* | C.Lambda (nn, s, t) -> *)
72 (* C.Lambda (nn, apply_aux lift_amount s, apply_aux (lift_amount+1) t) *)
75 (* apply_aux 0 term *)
79 (* let apply_subst subst term = *)
80 (* Printf.printf "| apply_subst:\n| subst: %s\n| term: %s\n" *)
81 (* (Utils.print_subst ~prefix:" ; " subst) (CicPp.ppterm term); *)
82 (* let res = my_apply_subst subst term in *)
83 (* (\* let res = CicMetaSubst.apply_subst subst term in *\) *)
84 (* Printf.printf "| res: %s\n" (CicPp.ppterm res); *)
85 (* print_endline "|"; *)
89 (* let apply_subst = my_apply_subst *)
90 let apply_subst = CicMetaSubst.apply_subst
95 let profile = CicUtil.profile "apply_subst" in
96 (fun s a -> profile.profile (apply_subst s) a)
103 let empty_table () = []
105 let index table equality =
106 let _, _, (_, l, r, ordering), _, _ = equality in
108 | Utils.Gt -> (Utils.Left, equality)::table
109 | Utils.Lt -> (Utils.Right, equality)::table
110 | _ -> (Utils.Left, equality)::(Utils.Right, equality)::table
113 let remove_index table equality =
114 List.filter (fun (p, e) -> e != equality) table
117 let in_index table equality =
118 List.exists (fun (p, e) -> e == equality) table
121 let get_candidates mode table term = table
128 Path_indexing.PSTrie.empty
131 let index = Path_indexing.index
132 and remove_index = Path_indexing.remove_index
133 and in_index = Path_indexing.in_index;;
135 let get_candidates mode trie term =
136 let t1 = Unix.gettimeofday () in
140 | Matching -> Path_indexing.retrieve_generalizations trie term
141 | Unification -> Path_indexing.retrieve_unifiables trie term
142 (* Path_indexing.retrieve_all trie term *)
144 Path_indexing.PosEqSet.elements s
146 (* print_candidates mode term res; *)
147 let t2 = Unix.gettimeofday () in
148 indexing_retrieval_time := !indexing_retrieval_time +. (t2 -. t1);
154 (* DISCRIMINATION TREES *)
156 Discrimination_tree.DiscriminationTree.empty
159 let index = Discrimination_tree.index
160 and remove_index = Discrimination_tree.remove_index
161 and in_index = Discrimination_tree.in_index;;
163 let get_candidates mode tree term =
164 let t1 = Unix.gettimeofday () in
168 | Matching -> Discrimination_tree.retrieve_generalizations tree term
169 | Unification -> Discrimination_tree.retrieve_unifiables tree term
171 Discrimination_tree.PosEqSet.elements s
173 (* print_candidates mode term res; *)
174 (* print_endline (Discrimination_tree.string_of_discrimination_tree tree); *)
175 (* print_newline (); *)
176 let t2 = Unix.gettimeofday () in
177 indexing_retrieval_time := !indexing_retrieval_time +. (t2 -. t1);
182 (* let get_candidates = *)
183 (* let profile = CicUtil.profile "Indexing.get_candidates" in *)
184 (* (fun mode tree term -> profile.profile (get_candidates mode tree) term) *)
188 let match_unif_time_ok = ref 0.;;
189 let match_unif_time_no = ref 0.;;
192 let rec find_matches metasenv context ugraph lift_amount term termty =
193 let module C = Cic in
194 let module U = Utils in
195 let module S = CicSubstitution in
196 let module M = CicMetaSubst in
197 let module HL = HelmLibraryObjects in
198 let cmp = !Utils.compare_terms in
199 (* let names = Utils.names_of_context context in *)
200 (* let termty, ugraph = *)
201 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
206 let pos, (_, proof, (ty, left, right, o), metas, args) = candidate in
207 (* if not (fst (CicReduction.are_convertible *)
208 (* ~metasenv context termty ty ugraph)) then ( *)
209 (* (\* debug_print (lazy ( *\) *)
210 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
211 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
212 (* find_matches metasenv context ugraph lift_amount term termty tl *)
214 let do_match c (* other *) eq_URI =
215 let subst', metasenv', ugraph' =
216 let t1 = Unix.gettimeofday () in
219 Inference.matching (metasenv @ metas) context
220 term (S.lift lift_amount c) ugraph in
221 let t2 = Unix.gettimeofday () in
222 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
224 with Inference.MatchingFailure as e ->
225 let t2 = Unix.gettimeofday () in
226 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
229 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
232 let c, other, eq_URI =
233 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
234 else right, left, HL.Logic.eq_ind_r_URI
236 if o <> U.Incomparable then
238 do_match c (* other *) eq_URI
239 with Inference.MatchingFailure ->
240 find_matches metasenv context ugraph lift_amount term termty tl
243 try do_match c (* other *) eq_URI
244 with Inference.MatchingFailure -> None
247 | Some (_, s, _, _, _) ->
248 let c' = (* M. *)apply_subst s c
249 and other' = (* M. *)apply_subst s other in
250 let order = cmp c' other' in
251 let names = U.names_of_context context in
256 metasenv context ugraph lift_amount term termty tl
258 find_matches metasenv context ugraph lift_amount term termty tl
262 let rec find_all_matches ?(unif_fun=Inference.unification)
263 metasenv context ugraph lift_amount term termty =
264 let module C = Cic in
265 let module U = Utils in
266 let module S = CicSubstitution in
267 let module M = CicMetaSubst in
268 let module HL = HelmLibraryObjects in
269 let cmp = !Utils.compare_terms in
270 (* let names = Utils.names_of_context context in *)
271 (* let termty, ugraph = *)
272 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
275 (* match term with *)
276 (* | C.Meta _ -> assert false *)
282 let pos, (_, _, (ty, left, right, o), metas, args) = candidate in
283 (* if not (fst (CicReduction.are_convertible *)
284 (* ~metasenv context termty ty ugraph)) then ( *)
285 (* (\* debug_print (lazy ( *\) *)
286 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
287 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
288 (* find_all_matches ~unif_fun metasenv context ugraph *)
289 (* lift_amount term termty tl *)
291 let do_match c (* other *) eq_URI =
292 let subst', metasenv', ugraph' =
293 let t1 = Unix.gettimeofday () in
296 unif_fun (metasenv @ metas) context
297 term (S.lift lift_amount c) ugraph in
298 let t2 = Unix.gettimeofday () in
299 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
302 | Inference.MatchingFailure
303 | CicUnification.UnificationFailure _
304 | CicUnification.Uncertain _ as e ->
305 let t2 = Unix.gettimeofday () in
306 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
309 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
312 let c, other, eq_URI =
313 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
314 else right, left, HL.Logic.eq_ind_r_URI
316 if o <> U.Incomparable then
318 let res = do_match c (* other *) eq_URI in
319 res::(find_all_matches ~unif_fun metasenv context ugraph
320 lift_amount term termty tl)
322 | Inference.MatchingFailure
323 | CicUnification.UnificationFailure _
324 | CicUnification.Uncertain _ ->
325 find_all_matches ~unif_fun metasenv context ugraph
326 lift_amount term termty tl
329 let res = do_match c (* other *) eq_URI in
332 let c' = (* M. *)apply_subst s c
333 and other' = (* M. *)apply_subst s other in
334 let order = cmp c' other' in
335 let names = U.names_of_context context in
336 if order <> U.Lt && order <> U.Le then
337 res::(find_all_matches ~unif_fun metasenv context ugraph
338 lift_amount term termty tl)
340 find_all_matches ~unif_fun metasenv context ugraph
341 lift_amount term termty tl
343 | Inference.MatchingFailure
344 | CicUnification.UnificationFailure _
345 | CicUnification.Uncertain _ ->
346 find_all_matches ~unif_fun metasenv context ugraph
347 lift_amount term termty tl
351 let subsumption env table target =
352 let _, (ty, left, right, _), tmetas, _ = target in
353 let metasenv, context, ugraph = env in
354 let metasenv = metasenv @ tmetas in
355 let samesubst subst subst' =
356 let tbl = Hashtbl.create (List.length subst) in
357 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
359 (fun (m, (c, t1, t2)) ->
361 let c', t1', t2' = Hashtbl.find tbl m in
362 if (c = c') && (t1 = t1') && (t2 = t2') then true
372 let leftc = get_candidates Matching table left in
373 find_all_matches ~unif_fun:Inference.matching
374 metasenv context ugraph 0 left ty leftc
376 let ok what (_, subst, menv, ug, ((pos, (_, _, (_, l, r, o), _, _)), _)) =
378 let other = if pos = Utils.Left then r else l in
379 let subst', menv', ug' =
380 let t1 = Unix.gettimeofday () in
383 Inference.matching metasenv context what other ugraph in
384 let t2 = Unix.gettimeofday () in
385 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
387 with Inference.MatchingFailure as e ->
388 let t2 = Unix.gettimeofday () in
389 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
392 samesubst subst subst'
393 with Inference.MatchingFailure ->
396 let r = List.exists (ok right) leftr in
404 let rightc = get_candidates Matching table right in
405 find_all_matches ~unif_fun:Inference.matching
406 metasenv context ugraph 0 right ty rightc
408 List.exists (ok left) rightr
412 let rec demodulate_term metasenv context ugraph table lift_amount term =
413 let module C = Cic in
414 let module S = CicSubstitution in
415 let module M = CicMetaSubst in
416 let module HL = HelmLibraryObjects in
417 let candidates = get_candidates Matching table term in
422 C.Implicit None, ugraph
423 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
426 find_matches metasenv context ugraph lift_amount term termty candidates
437 (res, tl @ [S.lift 1 t])
440 demodulate_term metasenv context ugraph table
444 | None -> (None, tl @ [S.lift 1 t])
445 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
450 | Some (_, subst, menv, ug, eq_found) ->
451 Some (C.Appl ll, subst, menv, ug, eq_found)
453 | C.Prod (nn, s, t) ->
455 demodulate_term metasenv context ugraph table lift_amount s in (
459 demodulate_term metasenv
460 ((Some (nn, C.Decl s))::context) ugraph
461 table (lift_amount+1) t
465 | Some (t', subst, menv, ug, eq_found) ->
466 Some (C.Prod (nn, (S.lift 1 s), t'),
467 subst, menv, ug, eq_found)
469 | Some (s', subst, menv, ug, eq_found) ->
470 Some (C.Prod (nn, s', (S.lift 1 t)),
471 subst, menv, ug, eq_found)
473 | C.Lambda (nn, s, t) ->
475 demodulate_term metasenv context ugraph table lift_amount s in (
479 demodulate_term metasenv
480 ((Some (nn, C.Decl s))::context) ugraph
481 table (lift_amount+1) t
485 | Some (t', subst, menv, ug, eq_found) ->
486 Some (C.Lambda (nn, (S.lift 1 s), t'),
487 subst, menv, ug, eq_found)
489 | Some (s', subst, menv, ug, eq_found) ->
490 Some (C.Lambda (nn, s', (S.lift 1 t)),
491 subst, menv, ug, eq_found)
498 let build_ens_for_sym_eq ty x y =
499 [(UriManager.uri_of_string
500 "cic:/Coq/Init/Logic/Logic_lemmas/equality/A.var", ty);
501 (UriManager.uri_of_string
502 "cic:/Coq/Init/Logic/Logic_lemmas/equality/x.var", x);
503 (UriManager.uri_of_string
504 "cic:/Coq/Init/Logic/Logic_lemmas/equality/y.var", y)]
508 let build_newtarget_time = ref 0.;;
511 let demod_counter = ref 1;;
513 let rec demodulation newmeta env table sign target =
514 let module C = Cic in
515 let module S = CicSubstitution in
516 let module M = CicMetaSubst in
517 let module HL = HelmLibraryObjects in
518 let metasenv, context, ugraph = env in
519 let _, proof, (eq_ty, left, right, order), metas, args = target in
520 let metasenv' = metasenv @ metas in
522 let maxmeta = ref newmeta in
524 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
525 let time1 = Unix.gettimeofday () in
527 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
529 CicTypeChecker.type_of_aux' metasenv context what ugraph
531 let what, other = if pos = Utils.Left then what, other else other, what in
532 let newterm, newproof =
533 let bo = (* M. *)apply_subst subst (S.subst other t) in
535 (* let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in *)
536 (* incr demod_counter; *)
538 (* if is_left then t, S.lift 1 right else S.lift 1 left, t in *)
539 (* (name, ty, S.lift 1 eq_ty, l, r) *)
541 let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in
544 let l, r = if is_left then t, S.lift 1 right else S.lift 1 left, t in
545 C.Appl [C.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, []);
546 S.lift 1 eq_ty; l; r]
548 if sign = Utils.Positive then
550 Inference.ProofBlock (
551 subst, eq_URI, (name, ty), bo'(* t' *), eq_found, proof))
556 CicMkImplicit.identity_relocation_list_for_metavariable context in
557 Printf.printf "\nADDING META: %d\n" !maxmeta;
559 C.Meta (!maxmeta, irl)
565 if pos = Utils.Left then
566 build_ens_for_sym_eq ty what other
568 build_ens_for_sym_eq ty other what
570 Inference.ProofSymBlock (ens, proof')
573 if pos = Utils.Left then what, other else other, what
575 pos, (0, proof', (ty, other, what, Utils.Incomparable),
580 Inference.ProofBlock (subst, eq_URI, (name, ty), bo'(* t' *),
581 eq_found, Inference.BasicProof metaproof)
584 | Inference.BasicProof _ ->
585 print_endline "replacing a BasicProof";
587 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
588 print_endline "replacing another ProofGoalBlock";
589 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
592 (* (0, target_proof, (eq_ty, left, right, order), metas, args) *)
595 C.Appl [C.MutConstruct (* reflexivity *)
596 (HelmLibraryObjects.Logic.eq_URI, 0, 1, []);
597 eq_ty; if is_left then right else left]
600 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
602 let left, right = if is_left then newterm, right else left, newterm in
603 let m = (Inference.metas_of_term left) @ (Inference.metas_of_term right) in
604 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
608 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) args in *)
609 (* let delta = (List.length args) - (List.length a) in *)
610 (* if delta > 0 then *)
611 (* let first = List.hd a in *)
612 (* let rec aux l = function *)
614 (* | d -> let l = aux l (d-1) in l @ [first] *)
620 let ordering = !Utils.compare_terms left right in
622 let time2 = Unix.gettimeofday () in
623 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
626 let w = Utils.compute_equality_weight eq_ty left right in
627 (w, newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
631 let res = demodulate_term metasenv' context ugraph table 0 left in
634 let newmeta, newtarget = build_newtarget true t in
635 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
636 (Inference.meta_convertibility_eq target newtarget) then
639 (* if subsumption env table newtarget then *)
640 (* newmeta, build_identity newtarget *)
642 demodulation newmeta env table sign newtarget
644 let res = demodulate_term metasenv' context ugraph table 0 right in
647 let newmeta, newtarget = build_newtarget false t in
648 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
649 (Inference.meta_convertibility_eq target newtarget) then
652 (* if subsumption env table newtarget then *)
653 (* newmeta, build_identity newtarget *)
655 demodulation newmeta env table sign newtarget
661 let rec betaexpand_term metasenv context ugraph table lift_amount term =
662 let module C = Cic in
663 let module S = CicSubstitution in
664 let module M = CicMetaSubst in
665 let module HL = HelmLibraryObjects in
666 let candidates = get_candidates Unification table term in
667 let res, lifted_term =
672 (fun arg (res, lifted_tl) ->
675 let arg_res, lifted_arg =
676 betaexpand_term metasenv context ugraph table
680 (fun (t, s, m, ug, eq_found) ->
681 (Some t)::lifted_tl, s, m, ug, eq_found)
686 (fun (l, s, m, ug, eq_found) ->
687 (Some lifted_arg)::l, s, m, ug, eq_found)
689 (Some lifted_arg)::lifted_tl)
692 (fun (r, s, m, ug, eq_found) ->
693 None::r, s, m, ug, eq_found) res,
699 (fun (l, s, m, ug, eq_found) ->
700 (C.Meta (i, l), s, m, ug, eq_found)) l'
702 e, C.Meta (i, lifted_l)
705 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
707 | C.Prod (nn, s, t) ->
709 betaexpand_term metasenv context ugraph table lift_amount s in
711 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
712 table (lift_amount+1) t in
715 (fun (t, s, m, ug, eq_found) ->
716 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
719 (fun (t, s, m, ug, eq_found) ->
720 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
721 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
723 | C.Lambda (nn, s, t) ->
725 betaexpand_term metasenv context ugraph table lift_amount s in
727 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
728 table (lift_amount+1) t in
731 (fun (t, s, m, ug, eq_found) ->
732 C.Lambda (nn, t, lifted_t), s, m, ug, eq_found) l1
735 (fun (t, s, m, ug, eq_found) ->
736 C.Lambda (nn, lifted_s, t), s, m, ug, eq_found) l2 in
737 l1' @ l2', C.Lambda (nn, lifted_s, lifted_t)
742 (fun arg (res, lifted_tl) ->
743 let arg_res, lifted_arg =
744 betaexpand_term metasenv context ugraph table lift_amount arg
748 (fun (a, s, m, ug, eq_found) ->
749 a::lifted_tl, s, m, ug, eq_found)
754 (fun (r, s, m, ug, eq_found) ->
755 lifted_arg::r, s, m, ug, eq_found)
757 lifted_arg::lifted_tl)
761 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
764 | t -> [], (S.lift lift_amount t)
767 | C.Meta (i, l) -> res, lifted_term
770 C.Implicit None, ugraph
771 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
775 metasenv context ugraph lift_amount term termty candidates
781 let sup_l_counter = ref 1;;
783 let superposition_left newmeta (metasenv, context, ugraph) table target =
784 let module C = Cic in
785 let module S = CicSubstitution in
786 let module M = CicMetaSubst in
787 let module HL = HelmLibraryObjects in
788 let module CR = CicReduction in
789 let module U = Utils in
790 let weight, proof, (eq_ty, left, right, ordering), _, _ = target in
792 let term = if ordering = U.Gt then left else right in
793 betaexpand_term metasenv context ugraph table 0 term
795 let maxmeta = ref newmeta in
796 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
798 print_endline "\nSUPERPOSITION LEFT\n";
800 let time1 = Unix.gettimeofday () in
802 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
803 let what, other = if pos = Utils.Left then what, other else other, what in
804 let newgoal, newproof =
805 let bo' = (* M. *)apply_subst s (S.subst other bo) in
807 (* let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in *)
808 (* incr sup_l_counter; *)
810 (* if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in *)
811 (* (name, ty, S.lift 1 eq_ty, l, r) *)
813 let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in
817 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
818 C.Appl [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty; l; r]
823 CicMkImplicit.identity_relocation_list_for_metavariable context in
824 C.Meta (!maxmeta, irl)
830 if pos = Utils.Left then
831 build_ens_for_sym_eq ty what other
833 build_ens_for_sym_eq ty other what
835 Inference.ProofSymBlock (ens, proof')
838 if pos = Utils.Left then what, other else other, what
840 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
844 Inference.ProofBlock (s, eq_URI, (name, ty), bo''(* t' *), eq_found,
845 Inference.BasicProof metaproof)
848 | Inference.BasicProof _ ->
849 print_endline "replacing a BasicProof";
851 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
852 print_endline "replacing another ProofGoalBlock";
853 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
856 (* (weight, target_proof, (eq_ty, left, right, ordering), [], []) *)
859 C.Appl [C.MutConstruct (* reflexivity *)
860 (HelmLibraryObjects.Logic.eq_URI, 0, 1, []);
861 eq_ty; if ordering = U.Gt then right else left]
864 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
867 if ordering = U.Gt then newgoal, right else left, newgoal in
868 let neworder = !Utils.compare_terms left right in
870 let time2 = Unix.gettimeofday () in
871 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
874 let w = Utils.compute_equality_weight eq_ty left right in
875 (w, newproof, (eq_ty, left, right, neworder), [], [])
879 !maxmeta, List.map build_new expansions
883 let sup_r_counter = ref 1;;
885 let superposition_right newmeta (metasenv, context, ugraph) table target =
886 let module C = Cic in
887 let module S = CicSubstitution in
888 let module M = CicMetaSubst in
889 let module HL = HelmLibraryObjects in
890 let module CR = CicReduction in
891 let module U = Utils in
892 let _, eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
893 let metasenv' = metasenv @ newmetas in
894 let maxmeta = ref newmeta in
897 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
898 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
902 (fun (_, subst, _, _, _) ->
903 let subst = (* M. *)apply_subst subst in
904 let o = !Utils.compare_terms (subst l) (subst r) in
905 o <> U.Lt && o <> U.Le)
906 (fst (betaexpand_term metasenv' context ugraph table 0 l))
908 (res left right), (res right left)
910 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
912 let time1 = Unix.gettimeofday () in
914 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
915 let what, other = if pos = Utils.Left then what, other else other, what in
916 let newgoal, newproof =
917 let bo' = (* M. *)apply_subst s (S.subst other bo) in
919 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
922 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
923 (name, ty, S.lift 1 eq_ty, l, r)
925 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
929 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
930 C.Appl [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty; l; r]
933 Inference.ProofBlock (
934 s, eq_URI, (name, ty), bo''(* t' *), eq_found, eqproof)
936 let newmeta, newequality =
938 if ordering = U.Gt then newgoal, (* M. *)apply_subst s right
939 else (* M. *)apply_subst s left, newgoal in
940 let neworder = !Utils.compare_terms left right
941 and newmenv = newmetas @ menv'
942 and newargs = args @ args' in
944 (* (Inference.metas_of_term left) @ (Inference.metas_of_term right) in *)
947 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) *)
950 (* let delta = (List.length args) - (List.length a) in *)
951 (* if delta > 0 then *)
952 (* let first = List.hd a in *)
953 (* let rec aux l = function *)
955 (* | d -> let l = aux l (d-1) in l @ [first] *)
962 let w = Utils.compute_equality_weight eq_ty left right in
963 (w, newproof, (eq_ty, left, right, neworder), newmenv, newargs)
964 and env = (metasenv, context, ugraph) in
965 let newm, eq' = Inference.fix_metas !maxmeta eq' in
970 let time2 = Unix.gettimeofday () in
971 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
975 let new1 = List.map (build_new U.Gt) res1
976 and new2 = List.map (build_new U.Lt) res2 in
977 (* let ok = function *)
978 (* | _, _, (_, left, right, _), _, _ -> *)
979 (* not (fst (CR.are_convertible context left right ugraph)) *)
981 let ok e = not (Inference.is_identity (metasenv, context, ugraph) e) in
983 (List.filter ok (new1 @ new2)))
987 let rec demodulation_goal newmeta env table goal =
988 let module C = Cic in
989 let module S = CicSubstitution in
990 let module M = CicMetaSubst in
991 let module HL = HelmLibraryObjects in
992 let metasenv, context, ugraph = env in
993 let maxmeta = ref newmeta in
994 let proof, metas, term = goal in
995 let metasenv' = metasenv @ metas in
997 let build_newgoal (t, subst, menv, ug, (eq_found, eq_URI)) =
998 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
999 let what, other = if pos = Utils.Left then what, other else other, what in
1000 let newterm, newproof =
1001 let bo = (* M. *)apply_subst subst (S.subst other t) in
1002 let name = C.Name ("x_DemodGoal_" ^ (string_of_int !demod_counter)) in
1007 CicMkImplicit.identity_relocation_list_for_metavariable context in
1008 Printf.printf "\nADDING META: %d\n" !maxmeta;
1010 C.Meta (!maxmeta, irl)
1015 if pos = Utils.Left then build_ens_for_sym_eq ty what other
1016 else build_ens_for_sym_eq ty other what
1018 Inference.ProofSymBlock (ens, proof')
1021 if pos = Utils.Left then what, other else other, what
1023 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
1027 Inference.ProofBlock (subst, eq_URI, (name, ty), bo,
1028 eq_found, Inference.BasicProof metaproof)
1031 | Inference.NoProof ->
1032 debug_print (lazy "replacing a NoProof");
1034 | Inference.BasicProof _ ->
1035 debug_print (lazy "replacing a BasicProof");
1037 | Inference.ProofGoalBlock (_, parent_proof) ->
1038 debug_print (lazy "replacing another ProofGoalBlock");
1039 Inference.ProofGoalBlock (pb, parent_proof)
1042 bo, Inference.ProofGoalBlock (Inference.NoProof, goal_proof)
1044 let m = Inference.metas_of_term newterm in
1045 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1046 !maxmeta, (newproof, newmetasenv, newterm)
1049 let res = demodulate_term metasenv' context ugraph table 0 term in
1052 let newmeta, newgoal = build_newgoal t in
1053 let _, _, newg = newgoal in
1054 if Inference.meta_convertibility term newg then
1057 demodulation_goal newmeta env table newgoal