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
93 (* let apply_subst = *)
94 (* let profile = CicUtil.profile "apply_subst" in *)
95 (* (fun s a -> profile (apply_subst s) a) *)
101 let empty_table () = []
103 let index table equality =
104 let _, _, (_, l, r, ordering), _, _ = equality in
106 | Utils.Gt -> (Utils.Left, equality)::table
107 | Utils.Lt -> (Utils.Right, equality)::table
108 | _ -> (Utils.Left, equality)::(Utils.Right, equality)::table
111 let remove_index table equality =
112 List.filter (fun (p, e) -> e != equality) table
115 let in_index table equality =
116 List.exists (fun (p, e) -> e == equality) table
119 let get_candidates mode table term = table
126 Path_indexing.PSTrie.empty
129 let index = Path_indexing.index
130 and remove_index = Path_indexing.remove_index
131 and in_index = Path_indexing.in_index;;
133 let get_candidates mode trie term =
134 let t1 = Unix.gettimeofday () in
138 | Matching -> Path_indexing.retrieve_generalizations trie term
139 | Unification -> Path_indexing.retrieve_unifiables trie term
140 (* Path_indexing.retrieve_all trie term *)
142 Path_indexing.PosEqSet.elements s
144 (* print_candidates mode term res; *)
145 let t2 = Unix.gettimeofday () in
146 indexing_retrieval_time := !indexing_retrieval_time +. (t2 -. t1);
152 (* DISCRIMINATION TREES *)
154 Hashtbl.clear Discrimination_tree.arities;
158 Discrimination_tree.DiscriminationTree.empty
161 let index = Discrimination_tree.index
162 and remove_index = Discrimination_tree.remove_index
163 and in_index = Discrimination_tree.in_index;;
165 let get_candidates mode tree term =
166 let t1 = Unix.gettimeofday () in
170 | Matching -> Discrimination_tree.retrieve_generalizations tree term
171 | Unification -> Discrimination_tree.retrieve_unifiables tree term
173 Discrimination_tree.PosEqSet.elements s
175 (* print_candidates mode term res; *)
176 (* print_endline (Discrimination_tree.string_of_discrimination_tree tree); *)
177 (* print_newline (); *)
178 let t2 = Unix.gettimeofday () in
179 indexing_retrieval_time := !indexing_retrieval_time +. (t2 -. t1);
184 (* let get_candidates = *)
185 (* let profile = CicUtil.profile "Indexing.get_candidates" in *)
186 (* (fun mode tree term -> profile.profile (get_candidates mode tree) term) *)
190 let match_unif_time_ok = ref 0.;;
191 let match_unif_time_no = ref 0.;;
194 let rec find_matches metasenv context ugraph lift_amount term termty =
195 let module C = Cic in
196 let module U = Utils in
197 let module S = CicSubstitution in
198 let module M = CicMetaSubst in
199 let module HL = HelmLibraryObjects in
200 let cmp = !Utils.compare_terms in
201 (* let names = Utils.names_of_context context in *)
202 (* let termty, ugraph = *)
203 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
208 let pos, (_, proof, (ty, left, right, o), metas, args) = candidate in
209 (* if not (fst (CicReduction.are_convertible *)
210 (* ~metasenv context termty ty ugraph)) then ( *)
211 (* (\* debug_print (lazy ( *\) *)
212 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
213 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
214 (* find_matches metasenv context ugraph lift_amount term termty tl *)
216 let do_match c (* other *) eq_URI =
217 let subst', metasenv', ugraph' =
218 let t1 = Unix.gettimeofday () in
221 Inference.matching (metasenv @ metas) context
222 term (S.lift lift_amount c) ugraph in
223 let t2 = Unix.gettimeofday () in
224 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
226 with Inference.MatchingFailure as e ->
227 let t2 = Unix.gettimeofday () in
228 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
231 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
234 let c, other, eq_URI =
235 if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
236 else right, left, Utils.eq_ind_r_URI ()
238 if o <> U.Incomparable then
240 do_match c (* other *) eq_URI
241 with Inference.MatchingFailure ->
242 find_matches metasenv context ugraph lift_amount term termty tl
245 try do_match c (* other *) eq_URI
246 with Inference.MatchingFailure -> None
249 | Some (_, s, _, _, _) ->
250 let c' = (* M. *)apply_subst s c
251 and other' = (* M. *)apply_subst s other in
252 let order = cmp c' other' in
253 let names = U.names_of_context context in
256 (* (Printf.sprintf "OK matching: %s and %s, order: %s" *)
257 (* (CicPp.ppterm c') *)
258 (* (CicPp.ppterm other') *)
259 (* (Utils.string_of_comparison order)); *)
261 (* (Printf.sprintf "subst:\n%s\n" (Utils.print_subst s)) *)
267 metasenv context ugraph lift_amount term termty tl
269 find_matches metasenv context ugraph lift_amount term termty tl
273 let rec find_all_matches ?(unif_fun=Inference.unification)
274 metasenv context ugraph lift_amount term termty =
275 let module C = Cic in
276 let module U = Utils in
277 let module S = CicSubstitution in
278 let module M = CicMetaSubst in
279 let module HL = HelmLibraryObjects in
280 let cmp = !Utils.compare_terms in
281 (* let names = Utils.names_of_context context in *)
282 (* let termty, ugraph = *)
283 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
286 (* match term with *)
287 (* | C.Meta _ -> assert false *)
293 let pos, (_, _, (ty, left, right, o), metas, args) = candidate in
294 (* if not (fst (CicReduction.are_convertible *)
295 (* ~metasenv context termty ty ugraph)) then ( *)
296 (* (\* debug_print (lazy ( *\) *)
297 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
298 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
299 (* find_all_matches ~unif_fun metasenv context ugraph *)
300 (* lift_amount term termty tl *)
302 let do_match c (* other *) eq_URI =
303 let subst', metasenv', ugraph' =
304 let t1 = Unix.gettimeofday () in
307 unif_fun (metasenv @ metas) context
308 term (S.lift lift_amount c) ugraph in
309 let t2 = Unix.gettimeofday () in
310 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
313 | Inference.MatchingFailure
314 | CicUnification.UnificationFailure _
315 | CicUnification.Uncertain _ as e ->
316 let t2 = Unix.gettimeofday () in
317 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
320 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
323 let c, other, eq_URI =
324 if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
325 else right, left, Utils.eq_ind_r_URI ()
327 if o <> U.Incomparable then
329 let res = do_match c (* other *) eq_URI in
330 res::(find_all_matches ~unif_fun metasenv context ugraph
331 lift_amount term termty tl)
333 | Inference.MatchingFailure
334 | CicUnification.UnificationFailure _
335 | CicUnification.Uncertain _ ->
336 find_all_matches ~unif_fun metasenv context ugraph
337 lift_amount term termty tl
340 let res = do_match c (* other *) eq_URI in
343 let c' = (* M. *)apply_subst s c
344 and other' = (* M. *)apply_subst s other in
345 let order = cmp c' other' in
346 let names = U.names_of_context context in
347 if order <> U.Lt && order <> U.Le then
348 res::(find_all_matches ~unif_fun metasenv context ugraph
349 lift_amount term termty tl)
351 find_all_matches ~unif_fun metasenv context ugraph
352 lift_amount term termty tl
354 | Inference.MatchingFailure
355 | CicUnification.UnificationFailure _
356 | CicUnification.Uncertain _ ->
357 find_all_matches ~unif_fun metasenv context ugraph
358 lift_amount term termty tl
362 let subsumption env table target =
363 let _, (ty, left, right, _), tmetas, _ = target in
364 let metasenv, context, ugraph = env in
365 let metasenv = metasenv @ tmetas in
366 let samesubst subst subst' =
367 let tbl = Hashtbl.create (List.length subst) in
368 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
370 (fun (m, (c, t1, t2)) ->
372 let c', t1', t2' = Hashtbl.find tbl m in
373 if (c = c') && (t1 = t1') && (t2 = t2') then true
383 let leftc = get_candidates Matching table left in
384 find_all_matches ~unif_fun:Inference.matching
385 metasenv context ugraph 0 left ty leftc
387 let rec ok what = function
389 | (_, subst, menv, ug, ((pos, (_, _, (_, l, r, o), _, _)), _))::tl ->
391 let other = if pos = Utils.Left then r else l in
392 let subst', menv', ug' =
393 let t1 = Unix.gettimeofday () in
396 Inference.matching metasenv context what other ugraph in
397 let t2 = Unix.gettimeofday () in
398 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
400 with Inference.MatchingFailure as e ->
401 let t2 = Unix.gettimeofday () in
402 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
405 if samesubst subst subst' then
409 with Inference.MatchingFailure ->
412 let r, subst = ok right leftr in
420 let rightc = get_candidates Matching table right in
421 find_all_matches ~unif_fun:Inference.matching
422 metasenv context ugraph 0 right ty rightc
428 let rec demodulation_aux metasenv context ugraph table lift_amount term =
429 let module C = Cic in
430 let module S = CicSubstitution in
431 let module M = CicMetaSubst in
432 let module HL = HelmLibraryObjects in
433 let candidates = get_candidates Matching table term in
438 C.Implicit None, ugraph
439 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
442 find_matches metasenv context ugraph lift_amount term termty candidates
453 (res, tl @ [S.lift 1 t])
456 demodulation_aux metasenv context ugraph table
460 | None -> (None, tl @ [S.lift 1 t])
461 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
466 | Some (_, subst, menv, ug, eq_found) ->
467 Some (C.Appl ll, subst, menv, ug, eq_found)
469 | C.Prod (nn, s, t) ->
471 demodulation_aux metasenv context ugraph table lift_amount s in (
475 demodulation_aux metasenv
476 ((Some (nn, C.Decl s))::context) ugraph
477 table (lift_amount+1) t
481 | Some (t', subst, menv, ug, eq_found) ->
482 Some (C.Prod (nn, (S.lift 1 s), t'),
483 subst, menv, ug, eq_found)
485 | Some (s', subst, menv, ug, eq_found) ->
486 Some (C.Prod (nn, s', (S.lift 1 t)),
487 subst, menv, ug, eq_found)
489 | C.Lambda (nn, s, t) ->
491 demodulation_aux metasenv context ugraph table lift_amount s in (
495 demodulation_aux metasenv
496 ((Some (nn, C.Decl s))::context) ugraph
497 table (lift_amount+1) t
501 | Some (t', subst, menv, ug, eq_found) ->
502 Some (C.Lambda (nn, (S.lift 1 s), t'),
503 subst, menv, ug, eq_found)
505 | Some (s', subst, menv, ug, eq_found) ->
506 Some (C.Lambda (nn, s', (S.lift 1 t)),
507 subst, menv, ug, eq_found)
514 let build_newtarget_time = ref 0.;;
517 let demod_counter = ref 1;;
519 let rec demodulation_equality newmeta env table sign target =
520 let module C = Cic in
521 let module S = CicSubstitution in
522 let module M = CicMetaSubst in
523 let module HL = HelmLibraryObjects in
524 let metasenv, context, ugraph = env in
525 let _, proof, (eq_ty, left, right, order), metas, args = target in
526 let metasenv' = metasenv @ metas in
528 let maxmeta = ref newmeta in
530 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
531 let time1 = Unix.gettimeofday () in
533 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
535 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
536 with CicUtil.Meta_not_found _ -> ty
538 let what, other = if pos = Utils.Left then what, other else other, what in
539 let newterm, newproof =
540 let bo = (* M. *)apply_subst subst (S.subst other t) in
542 (* let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in *)
543 (* incr demod_counter; *)
545 (* if is_left then t, S.lift 1 right else S.lift 1 left, t in *)
546 (* (name, ty, S.lift 1 eq_ty, l, r) *)
548 let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in
551 let l, r = if is_left then t, S.lift 1 right else S.lift 1 left, t in
552 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
553 S.lift 1 eq_ty; l; r]
555 if sign = Utils.Positive then
557 Inference.ProofBlock (
558 subst, eq_URI, (name, ty), bo'(* t' *), eq_found, proof))
563 CicMkImplicit.identity_relocation_list_for_metavariable context in
564 Printf.printf "\nADDING META: %d\n" !maxmeta;
566 C.Meta (!maxmeta, irl)
572 (* if pos = Utils.Left then *)
573 (* build_ens_for_sym_eq ty what other *)
575 (* build_ens_for_sym_eq ty other what *)
577 if pos = Utils.Left then [ty; what; other]
578 else [ty; other; what]
580 Inference.ProofSymBlock (termlist, proof')
583 if pos = Utils.Left then what, other else other, what
585 pos, (0, proof', (ty, other, what, Utils.Incomparable),
590 Inference.ProofBlock (subst, eq_URI, (name, ty), bo'(* t' *),
591 eq_found, Inference.BasicProof metaproof)
594 | Inference.BasicProof _ ->
595 print_endline "replacing a BasicProof";
597 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
598 print_endline "replacing another ProofGoalBlock";
599 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
602 (* (0, target_proof, (eq_ty, left, right, order), metas, args) *)
605 C.Appl [C.MutConstruct (* reflexivity *)
606 (LibraryObjects.eq_URI (), 0, 1, []);
607 eq_ty; if is_left then right else left]
610 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
612 let left, right = if is_left then newterm, right else left, newterm in
613 let m = (Inference.metas_of_term left) @ (Inference.metas_of_term right) in
614 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
618 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) args in *)
619 (* let delta = (List.length args) - (List.length a) in *)
620 (* if delta > 0 then *)
621 (* let first = List.hd a in *)
622 (* let rec aux l = function *)
624 (* | d -> let l = aux l (d-1) in l @ [first] *)
630 let ordering = !Utils.compare_terms left right in
632 let time2 = Unix.gettimeofday () in
633 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
636 let w = Utils.compute_equality_weight eq_ty left right in
637 (w, newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
641 let res = demodulation_aux metasenv' context ugraph table 0 left in
644 let newmeta, newtarget = build_newtarget true t in
645 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
646 (Inference.meta_convertibility_eq target newtarget) then
649 (* if subsumption env table newtarget then *)
650 (* newmeta, build_identity newtarget *)
652 demodulation_equality newmeta env table sign newtarget
654 let res = demodulation_aux metasenv' context ugraph table 0 right in
657 let newmeta, newtarget = build_newtarget false t in
658 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
659 (Inference.meta_convertibility_eq target newtarget) then
662 (* if subsumption env table newtarget then *)
663 (* newmeta, build_identity newtarget *)
665 demodulation_equality newmeta env table sign newtarget
671 let rec betaexpand_term metasenv context ugraph table lift_amount term =
672 let module C = Cic in
673 let module S = CicSubstitution in
674 let module M = CicMetaSubst in
675 let module HL = HelmLibraryObjects in
676 let candidates = get_candidates Unification table term in
677 let res, lifted_term =
682 (fun arg (res, lifted_tl) ->
685 let arg_res, lifted_arg =
686 betaexpand_term metasenv context ugraph table
690 (fun (t, s, m, ug, eq_found) ->
691 (Some t)::lifted_tl, s, m, ug, eq_found)
696 (fun (l, s, m, ug, eq_found) ->
697 (Some lifted_arg)::l, s, m, ug, eq_found)
699 (Some lifted_arg)::lifted_tl)
702 (fun (r, s, m, ug, eq_found) ->
703 None::r, s, m, ug, eq_found) res,
709 (fun (l, s, m, ug, eq_found) ->
710 (C.Meta (i, l), s, m, ug, eq_found)) l'
712 e, C.Meta (i, lifted_l)
715 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
717 | C.Prod (nn, s, t) ->
719 betaexpand_term metasenv context ugraph table lift_amount s in
721 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
722 table (lift_amount+1) t in
725 (fun (t, s, m, ug, eq_found) ->
726 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
729 (fun (t, s, m, ug, eq_found) ->
730 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
731 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
733 | C.Lambda (nn, s, t) ->
735 betaexpand_term metasenv context ugraph table lift_amount s in
737 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
738 table (lift_amount+1) t in
741 (fun (t, s, m, ug, eq_found) ->
742 C.Lambda (nn, t, lifted_t), s, m, ug, eq_found) l1
745 (fun (t, s, m, ug, eq_found) ->
746 C.Lambda (nn, lifted_s, t), s, m, ug, eq_found) l2 in
747 l1' @ l2', C.Lambda (nn, lifted_s, lifted_t)
752 (fun arg (res, lifted_tl) ->
753 let arg_res, lifted_arg =
754 betaexpand_term metasenv context ugraph table lift_amount arg
758 (fun (a, s, m, ug, eq_found) ->
759 a::lifted_tl, s, m, ug, eq_found)
764 (fun (r, s, m, ug, eq_found) ->
765 lifted_arg::r, s, m, ug, eq_found)
767 lifted_arg::lifted_tl)
771 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
774 | t -> [], (S.lift lift_amount t)
777 | C.Meta (i, l) -> res, lifted_term
780 C.Implicit None, ugraph
781 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
785 metasenv context ugraph lift_amount term termty candidates
791 let sup_l_counter = ref 1;;
793 let superposition_left newmeta (metasenv, context, ugraph) table target =
794 let module C = Cic in
795 let module S = CicSubstitution in
796 let module M = CicMetaSubst in
797 let module HL = HelmLibraryObjects in
798 let module CR = CicReduction in
799 let module U = Utils in
800 let weight, proof, (eq_ty, left, right, ordering), _, _ = target in
802 let term = if ordering = U.Gt then left else right in
803 betaexpand_term metasenv context ugraph table 0 term
805 let maxmeta = ref newmeta in
806 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
808 print_endline "\nSUPERPOSITION LEFT\n";
810 let time1 = Unix.gettimeofday () in
812 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
813 let what, other = if pos = Utils.Left then what, other else other, what in
814 let newgoal, newproof =
815 let bo' = (* M. *)apply_subst s (S.subst other bo) in
817 (* let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in *)
818 (* incr sup_l_counter; *)
820 (* if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in *)
821 (* (name, ty, S.lift 1 eq_ty, l, r) *)
823 let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in
827 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
828 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
829 S.lift 1 eq_ty; l; r]
834 CicMkImplicit.identity_relocation_list_for_metavariable context in
835 C.Meta (!maxmeta, irl)
841 (* if pos = Utils.Left then *)
842 (* build_ens_for_sym_eq ty what other *)
844 (* build_ens_for_sym_eq ty other what *)
847 if pos = Utils.Left then [ty; what; other]
848 else [ty; other; what]
850 Inference.ProofSymBlock (termlist, proof')
853 if pos = Utils.Left then what, other else other, what
855 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
859 Inference.ProofBlock (s, eq_URI, (name, ty), bo''(* t' *), eq_found,
860 Inference.BasicProof metaproof)
863 | Inference.BasicProof _ ->
864 print_endline "replacing a BasicProof";
866 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
867 print_endline "replacing another ProofGoalBlock";
868 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
871 (* (weight, target_proof, (eq_ty, left, right, ordering), [], []) *)
874 C.Appl [C.MutConstruct (* reflexivity *)
875 (LibraryObjects.eq_URI (), 0, 1, []);
876 eq_ty; if ordering = U.Gt then right else left]
879 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
882 if ordering = U.Gt then newgoal, right else left, newgoal in
883 let neworder = !Utils.compare_terms left right in
885 let time2 = Unix.gettimeofday () in
886 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
889 let w = Utils.compute_equality_weight eq_ty left right in
890 (w, newproof, (eq_ty, left, right, neworder), [], [])
894 !maxmeta, List.map build_new expansions
898 let sup_r_counter = ref 1;;
900 let superposition_right newmeta (metasenv, context, ugraph) table target =
901 let module C = Cic in
902 let module S = CicSubstitution in
903 let module M = CicMetaSubst in
904 let module HL = HelmLibraryObjects in
905 let module CR = CicReduction in
906 let module U = Utils in
907 let _, eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
908 let metasenv' = metasenv @ newmetas in
909 let maxmeta = ref newmeta in
912 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
913 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
917 (fun (_, subst, _, _, _) ->
918 let subst = (* M. *)apply_subst subst in
919 let o = !Utils.compare_terms (subst l) (subst r) in
920 o <> U.Lt && o <> U.Le)
921 (fst (betaexpand_term metasenv' context ugraph table 0 l))
923 (res left right), (res right left)
925 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
927 let time1 = Unix.gettimeofday () in
929 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
930 let what, other = if pos = Utils.Left then what, other else other, what in
931 let newgoal, newproof =
932 let bo' = (* M. *)apply_subst s (S.subst other bo) in
934 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
937 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
938 (name, ty, S.lift 1 eq_ty, l, r)
940 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
944 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
945 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
946 S.lift 1 eq_ty; l; r]
949 Inference.ProofBlock (
950 s, eq_URI, (name, ty), bo''(* t' *), eq_found, eqproof)
952 let newmeta, newequality =
954 if ordering = U.Gt then newgoal, (* M. *)apply_subst s right
955 else (* M. *)apply_subst s left, newgoal in
956 let neworder = !Utils.compare_terms left right
957 and newmenv = newmetas @ menv'
958 and newargs = args @ args' in
960 (* (Inference.metas_of_term left) @ (Inference.metas_of_term right) in *)
963 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) *)
966 (* let delta = (List.length args) - (List.length a) in *)
967 (* if delta > 0 then *)
968 (* let first = List.hd a in *)
969 (* let rec aux l = function *)
971 (* | d -> let l = aux l (d-1) in l @ [first] *)
978 let w = Utils.compute_equality_weight eq_ty left right in
979 (w, newproof, (eq_ty, left, right, neworder), newmenv, newargs)
980 and env = (metasenv, context, ugraph) in
981 let newm, eq' = Inference.fix_metas !maxmeta eq' in
986 let time2 = Unix.gettimeofday () in
987 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
991 let new1 = List.map (build_new U.Gt) res1
992 and new2 = List.map (build_new U.Lt) res2 in
993 (* let ok = function *)
994 (* | _, _, (_, left, right, _), _, _ -> *)
995 (* not (fst (CR.are_convertible context left right ugraph)) *)
998 let env = metasenv, context, ugraph in
1001 (Printf.sprintf "end of superposition_right:\n%s\n"
1004 (fun e -> "Positive " ^
1005 (Inference.string_of_equality ~env e)) (new1 @ new2))))))
1007 let ok e = not (Inference.is_identity (metasenv, context, ugraph) e) in
1009 (List.filter ok (new1 @ new2)))
1013 let rec demodulation_goal newmeta env table goal =
1014 let module C = Cic in
1015 let module S = CicSubstitution in
1016 let module M = CicMetaSubst in
1017 let module HL = HelmLibraryObjects in
1018 let metasenv, context, ugraph = env in
1019 let maxmeta = ref newmeta in
1020 let proof, metas, term = goal in
1021 let metasenv' = metasenv @ metas in
1023 let build_newgoal (t, subst, menv, ug, (eq_found, eq_URI)) =
1024 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
1025 let what, other = if pos = Utils.Left then what, other else other, what in
1027 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
1028 with CicUtil.Meta_not_found _ -> ty
1030 let newterm, newproof =
1031 let bo = (* M. *)apply_subst subst (S.subst other t) in
1032 let bo' = apply_subst subst t in
1033 let name = C.Name ("x_DemodGoal_" ^ (string_of_int !demod_counter)) in
1038 CicMkImplicit.identity_relocation_list_for_metavariable context in
1039 Printf.printf "\nADDING META: %d\n" !maxmeta;
1041 C.Meta (!maxmeta, irl)
1046 (* if pos = Utils.Left then build_ens_for_sym_eq ty what other *)
1047 (* else build_ens_for_sym_eq ty other what *)
1050 if pos = Utils.Left then [ty; what; other]
1051 else [ty; other; what]
1053 Inference.ProofSymBlock (termlist, proof')
1056 if pos = Utils.Left then what, other else other, what
1058 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
1062 Inference.ProofBlock (subst, eq_URI, (name, ty), bo',
1063 eq_found, Inference.BasicProof metaproof)
1065 let rec repl = function
1066 | Inference.NoProof ->
1067 debug_print (lazy "replacing a NoProof");
1069 | Inference.BasicProof _ ->
1070 debug_print (lazy "replacing a BasicProof");
1072 | Inference.ProofGoalBlock (_, parent_proof) ->
1073 debug_print (lazy "replacing another ProofGoalBlock");
1074 Inference.ProofGoalBlock (pb, parent_proof)
1075 | (Inference.SubProof (term, meta_index, p) as subproof) ->
1078 (Printf.sprintf "replacing %s"
1079 (Inference.string_of_proof subproof)));
1080 Inference.SubProof (term, meta_index, repl p)
1084 bo, Inference.ProofGoalBlock (Inference.NoProof, goal_proof)
1086 let m = Inference.metas_of_term newterm in
1087 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1088 !maxmeta, (newproof, newmetasenv, newterm)
1090 let res = demodulation_aux metasenv' context ugraph table 0 term in
1093 let newmeta, newgoal = build_newgoal t in
1094 let _, _, newg = newgoal in
1095 if Inference.meta_convertibility term newg then
1098 demodulation_goal newmeta env table newgoal
1104 let rec demodulation_theorem newmeta env table theorem =
1105 let module C = Cic in
1106 let module S = CicSubstitution in
1107 let module M = CicMetaSubst in
1108 let module HL = HelmLibraryObjects in
1109 let metasenv, context, ugraph = env in
1110 let maxmeta = ref newmeta in
1111 let proof, metas, term = theorem in
1112 let term, termty, metas = theorem in
1113 let metasenv' = metasenv @ metas in
1115 let build_newtheorem (t, subst, menv, ug, (eq_found, eq_URI)) =
1116 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
1117 let what, other = if pos = Utils.Left then what, other else other, what in
1118 let newterm, newty =
1119 let bo = apply_subst subst (S.subst other t) in
1120 let bo' = apply_subst subst t in
1121 let name = C.Name ("x_DemodThm_" ^ (string_of_int !demod_counter)) in
1124 Inference.ProofBlock (subst, eq_URI, (name, ty), bo', eq_found,
1125 Inference.BasicProof term)
1127 (Inference.build_proof_term newproof, bo)
1129 let m = Inference.metas_of_term newterm in
1130 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1131 !maxmeta, (newterm, newty, newmetasenv)
1133 let res = demodulation_aux metasenv' context ugraph table 0 termty in
1136 let newmeta, newthm = build_newtheorem t in
1137 let newt, newty, _ = newthm in
1138 if Inference.meta_convertibility termty newty then
1141 demodulation_theorem newmeta env table newthm