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 Discrimination_tree.DiscriminationTree.empty
157 let index = Discrimination_tree.index
158 and remove_index = Discrimination_tree.remove_index
159 and in_index = Discrimination_tree.in_index;;
161 let get_candidates mode tree term =
162 let t1 = Unix.gettimeofday () in
166 | Matching -> Discrimination_tree.retrieve_generalizations tree term
167 | Unification -> Discrimination_tree.retrieve_unifiables tree term
169 Discrimination_tree.PosEqSet.elements s
171 (* print_candidates mode term res; *)
172 (* print_endline (Discrimination_tree.string_of_discrimination_tree tree); *)
173 (* print_newline (); *)
174 let t2 = Unix.gettimeofday () in
175 indexing_retrieval_time := !indexing_retrieval_time +. (t2 -. t1);
180 (* let get_candidates = *)
181 (* let profile = CicUtil.profile "Indexing.get_candidates" in *)
182 (* (fun mode tree term -> profile.profile (get_candidates mode tree) term) *)
186 let match_unif_time_ok = ref 0.;;
187 let match_unif_time_no = ref 0.;;
190 let rec find_matches metasenv context ugraph lift_amount term termty =
191 let module C = Cic in
192 let module U = Utils in
193 let module S = CicSubstitution in
194 let module M = CicMetaSubst in
195 let module HL = HelmLibraryObjects in
196 let cmp = !Utils.compare_terms in
197 (* let names = Utils.names_of_context context in *)
198 (* let termty, ugraph = *)
199 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
204 let pos, (_, proof, (ty, left, right, o), metas, args) = candidate in
205 (* if not (fst (CicReduction.are_convertible *)
206 (* ~metasenv context termty ty ugraph)) then ( *)
207 (* (\* debug_print (lazy ( *\) *)
208 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
209 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
210 (* find_matches metasenv context ugraph lift_amount term termty tl *)
212 let do_match c (* other *) eq_URI =
213 let subst', metasenv', ugraph' =
214 let t1 = Unix.gettimeofday () in
217 Inference.matching (metasenv @ metas) context
218 term (S.lift lift_amount c) ugraph in
219 let t2 = Unix.gettimeofday () in
220 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
222 with Inference.MatchingFailure as e ->
223 let t2 = Unix.gettimeofday () in
224 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
227 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
230 let c, other, eq_URI =
231 if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
232 else right, left, Utils.eq_ind_r_URI ()
234 if o <> U.Incomparable then
236 do_match c (* other *) eq_URI
237 with Inference.MatchingFailure ->
238 find_matches metasenv context ugraph lift_amount term termty tl
241 try do_match c (* other *) eq_URI
242 with Inference.MatchingFailure -> None
245 | Some (_, s, _, _, _) ->
246 let c' = (* M. *)apply_subst s c
247 and other' = (* M. *)apply_subst s other in
248 let order = cmp c' other' in
249 let names = U.names_of_context context in
252 (* (Printf.sprintf "OK matching: %s and %s, order: %s" *)
253 (* (CicPp.ppterm c') *)
254 (* (CicPp.ppterm other') *)
255 (* (Utils.string_of_comparison order)); *)
257 (* (Printf.sprintf "subst:\n%s\n" (Utils.print_subst s)) *)
263 metasenv context ugraph lift_amount term termty tl
265 find_matches metasenv context ugraph lift_amount term termty tl
269 let rec find_all_matches ?(unif_fun=Inference.unification)
270 metasenv context ugraph lift_amount term termty =
271 let module C = Cic in
272 let module U = Utils in
273 let module S = CicSubstitution in
274 let module M = CicMetaSubst in
275 let module HL = HelmLibraryObjects in
276 let cmp = !Utils.compare_terms in
277 (* let names = Utils.names_of_context context in *)
278 (* let termty, ugraph = *)
279 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
282 (* match term with *)
283 (* | C.Meta _ -> assert false *)
289 let pos, (_, _, (ty, left, right, o), metas, args) = candidate in
290 (* if not (fst (CicReduction.are_convertible *)
291 (* ~metasenv context termty ty ugraph)) then ( *)
292 (* (\* debug_print (lazy ( *\) *)
293 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
294 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
295 (* find_all_matches ~unif_fun metasenv context ugraph *)
296 (* lift_amount term termty tl *)
298 let do_match c (* other *) eq_URI =
299 let subst', metasenv', ugraph' =
300 let t1 = Unix.gettimeofday () in
303 unif_fun (metasenv @ metas) context
304 term (S.lift lift_amount c) ugraph in
305 let t2 = Unix.gettimeofday () in
306 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
309 | Inference.MatchingFailure
310 | CicUnification.UnificationFailure _
311 | CicUnification.Uncertain _ as e ->
312 let t2 = Unix.gettimeofday () in
313 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
316 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
319 let c, other, eq_URI =
320 if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
321 else right, left, Utils.eq_ind_r_URI ()
323 if o <> U.Incomparable then
325 let res = do_match c (* other *) eq_URI in
326 res::(find_all_matches ~unif_fun metasenv context ugraph
327 lift_amount term termty tl)
329 | Inference.MatchingFailure
330 | CicUnification.UnificationFailure _
331 | CicUnification.Uncertain _ ->
332 find_all_matches ~unif_fun metasenv context ugraph
333 lift_amount term termty tl
336 let res = do_match c (* other *) eq_URI in
339 let c' = (* M. *)apply_subst s c
340 and other' = (* M. *)apply_subst s other in
341 let order = cmp c' other' in
342 let names = U.names_of_context context in
343 if order <> U.Lt && order <> U.Le then
344 res::(find_all_matches ~unif_fun metasenv context ugraph
345 lift_amount term termty tl)
347 find_all_matches ~unif_fun metasenv context ugraph
348 lift_amount term termty tl
350 | Inference.MatchingFailure
351 | CicUnification.UnificationFailure _
352 | CicUnification.Uncertain _ ->
353 find_all_matches ~unif_fun metasenv context ugraph
354 lift_amount term termty tl
358 let subsumption env table target =
359 let _, (ty, left, right, _), tmetas, _ = target in
360 let metasenv, context, ugraph = env in
361 let metasenv = metasenv @ tmetas in
362 let samesubst subst subst' =
363 let tbl = Hashtbl.create (List.length subst) in
364 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
366 (fun (m, (c, t1, t2)) ->
368 let c', t1', t2' = Hashtbl.find tbl m in
369 if (c = c') && (t1 = t1') && (t2 = t2') then true
379 let leftc = get_candidates Matching table left in
380 find_all_matches ~unif_fun:Inference.matching
381 metasenv context ugraph 0 left ty leftc
383 let rec ok what = function
385 | (_, subst, menv, ug, ((pos, (_, _, (_, l, r, o), _, _)), _))::tl ->
387 let other = if pos = Utils.Left then r else l in
388 let subst', menv', ug' =
389 let t1 = Unix.gettimeofday () in
392 Inference.matching metasenv context what other ugraph in
393 let t2 = Unix.gettimeofday () in
394 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
396 with Inference.MatchingFailure as e ->
397 let t2 = Unix.gettimeofday () in
398 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
401 if samesubst subst subst' then
405 with Inference.MatchingFailure ->
408 let r, subst = ok right leftr in
416 let rightc = get_candidates Matching table right in
417 find_all_matches ~unif_fun:Inference.matching
418 metasenv context ugraph 0 right ty rightc
424 let rec demodulation_aux metasenv context ugraph table lift_amount term =
425 let module C = Cic in
426 let module S = CicSubstitution in
427 let module M = CicMetaSubst in
428 let module HL = HelmLibraryObjects in
429 let candidates = get_candidates Matching table term in
434 C.Implicit None, ugraph
435 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
438 find_matches metasenv context ugraph lift_amount term termty candidates
449 (res, tl @ [S.lift 1 t])
452 demodulation_aux metasenv context ugraph table
456 | None -> (None, tl @ [S.lift 1 t])
457 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
462 | Some (_, subst, menv, ug, eq_found) ->
463 Some (C.Appl ll, subst, menv, ug, eq_found)
465 | C.Prod (nn, s, t) ->
467 demodulation_aux metasenv context ugraph table lift_amount s in (
471 demodulation_aux metasenv
472 ((Some (nn, C.Decl s))::context) ugraph
473 table (lift_amount+1) t
477 | Some (t', subst, menv, ug, eq_found) ->
478 Some (C.Prod (nn, (S.lift 1 s), t'),
479 subst, menv, ug, eq_found)
481 | Some (s', subst, menv, ug, eq_found) ->
482 Some (C.Prod (nn, s', (S.lift 1 t)),
483 subst, menv, ug, eq_found)
485 | C.Lambda (nn, s, t) ->
487 demodulation_aux metasenv context ugraph table lift_amount s in (
491 demodulation_aux metasenv
492 ((Some (nn, C.Decl s))::context) ugraph
493 table (lift_amount+1) t
497 | Some (t', subst, menv, ug, eq_found) ->
498 Some (C.Lambda (nn, (S.lift 1 s), t'),
499 subst, menv, ug, eq_found)
501 | Some (s', subst, menv, ug, eq_found) ->
502 Some (C.Lambda (nn, s', (S.lift 1 t)),
503 subst, menv, ug, eq_found)
510 let build_newtarget_time = ref 0.;;
513 let demod_counter = ref 1;;
515 let rec demodulation_equality newmeta env table sign target =
516 let module C = Cic in
517 let module S = CicSubstitution in
518 let module M = CicMetaSubst in
519 let module HL = HelmLibraryObjects in
520 let metasenv, context, ugraph = env in
521 let _, proof, (eq_ty, left, right, order), metas, args = target in
522 let metasenv' = metasenv @ metas in
524 let maxmeta = ref newmeta in
526 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
527 let time1 = Unix.gettimeofday () in
529 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
531 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
532 with CicUtil.Meta_not_found _ -> ty
534 let what, other = if pos = Utils.Left then what, other else other, what in
535 let newterm, newproof =
536 let bo = (* M. *)apply_subst subst (S.subst other t) in
538 (* let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in *)
539 (* incr demod_counter; *)
541 (* if is_left then t, S.lift 1 right else S.lift 1 left, t in *)
542 (* (name, ty, S.lift 1 eq_ty, l, r) *)
544 let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in
547 let l, r = if is_left then t, S.lift 1 right else S.lift 1 left, t in
548 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
549 S.lift 1 eq_ty; l; r]
551 if sign = Utils.Positive then
553 Inference.ProofBlock (
554 subst, eq_URI, (name, ty), bo'(* t' *), eq_found, proof))
559 CicMkImplicit.identity_relocation_list_for_metavariable context in
560 Printf.printf "\nADDING META: %d\n" !maxmeta;
562 C.Meta (!maxmeta, irl)
568 (* if pos = Utils.Left then *)
569 (* build_ens_for_sym_eq ty what other *)
571 (* build_ens_for_sym_eq ty other what *)
573 if pos = Utils.Left then [ty; what; other]
574 else [ty; other; what]
576 Inference.ProofSymBlock (termlist, proof')
579 if pos = Utils.Left then what, other else other, what
581 pos, (0, proof', (ty, other, what, Utils.Incomparable),
586 Inference.ProofBlock (subst, eq_URI, (name, ty), bo'(* t' *),
587 eq_found, Inference.BasicProof metaproof)
590 | Inference.BasicProof _ ->
591 print_endline "replacing a BasicProof";
593 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
594 print_endline "replacing another ProofGoalBlock";
595 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
598 (* (0, target_proof, (eq_ty, left, right, order), metas, args) *)
601 C.Appl [C.MutConstruct (* reflexivity *)
602 (LibraryObjects.eq_URI (), 0, 1, []);
603 eq_ty; if is_left then right else left]
606 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
608 let left, right = if is_left then newterm, right else left, newterm in
609 let m = (Inference.metas_of_term left) @ (Inference.metas_of_term right) in
610 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
614 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) args in *)
615 (* let delta = (List.length args) - (List.length a) in *)
616 (* if delta > 0 then *)
617 (* let first = List.hd a in *)
618 (* let rec aux l = function *)
620 (* | d -> let l = aux l (d-1) in l @ [first] *)
626 let ordering = !Utils.compare_terms left right in
628 let time2 = Unix.gettimeofday () in
629 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
632 let w = Utils.compute_equality_weight eq_ty left right in
633 (w, newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
637 let res = demodulation_aux metasenv' context ugraph table 0 left in
640 let newmeta, newtarget = build_newtarget true t in
641 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
642 (Inference.meta_convertibility_eq target newtarget) then
645 (* if subsumption env table newtarget then *)
646 (* newmeta, build_identity newtarget *)
648 demodulation_equality newmeta env table sign newtarget
650 let res = demodulation_aux metasenv' context ugraph table 0 right in
653 let newmeta, newtarget = build_newtarget false t in
654 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
655 (Inference.meta_convertibility_eq target newtarget) then
658 (* if subsumption env table newtarget then *)
659 (* newmeta, build_identity newtarget *)
661 demodulation_equality newmeta env table sign newtarget
667 let rec betaexpand_term metasenv context ugraph table lift_amount term =
668 let module C = Cic in
669 let module S = CicSubstitution in
670 let module M = CicMetaSubst in
671 let module HL = HelmLibraryObjects in
672 let candidates = get_candidates Unification table term in
673 let res, lifted_term =
678 (fun arg (res, lifted_tl) ->
681 let arg_res, lifted_arg =
682 betaexpand_term metasenv context ugraph table
686 (fun (t, s, m, ug, eq_found) ->
687 (Some t)::lifted_tl, s, m, ug, eq_found)
692 (fun (l, s, m, ug, eq_found) ->
693 (Some lifted_arg)::l, s, m, ug, eq_found)
695 (Some lifted_arg)::lifted_tl)
698 (fun (r, s, m, ug, eq_found) ->
699 None::r, s, m, ug, eq_found) res,
705 (fun (l, s, m, ug, eq_found) ->
706 (C.Meta (i, l), s, m, ug, eq_found)) l'
708 e, C.Meta (i, lifted_l)
711 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
713 | C.Prod (nn, s, t) ->
715 betaexpand_term metasenv context ugraph table lift_amount s in
717 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
718 table (lift_amount+1) t in
721 (fun (t, s, m, ug, eq_found) ->
722 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
725 (fun (t, s, m, ug, eq_found) ->
726 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
727 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
729 | C.Lambda (nn, s, t) ->
731 betaexpand_term metasenv context ugraph table lift_amount s in
733 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
734 table (lift_amount+1) t in
737 (fun (t, s, m, ug, eq_found) ->
738 C.Lambda (nn, t, lifted_t), s, m, ug, eq_found) l1
741 (fun (t, s, m, ug, eq_found) ->
742 C.Lambda (nn, lifted_s, t), s, m, ug, eq_found) l2 in
743 l1' @ l2', C.Lambda (nn, lifted_s, lifted_t)
748 (fun arg (res, lifted_tl) ->
749 let arg_res, lifted_arg =
750 betaexpand_term metasenv context ugraph table lift_amount arg
754 (fun (a, s, m, ug, eq_found) ->
755 a::lifted_tl, s, m, ug, eq_found)
760 (fun (r, s, m, ug, eq_found) ->
761 lifted_arg::r, s, m, ug, eq_found)
763 lifted_arg::lifted_tl)
767 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
770 | t -> [], (S.lift lift_amount t)
773 | C.Meta (i, l) -> res, lifted_term
776 C.Implicit None, ugraph
777 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
781 metasenv context ugraph lift_amount term termty candidates
787 let sup_l_counter = ref 1;;
789 let superposition_left newmeta (metasenv, context, ugraph) table target =
790 let module C = Cic in
791 let module S = CicSubstitution in
792 let module M = CicMetaSubst in
793 let module HL = HelmLibraryObjects in
794 let module CR = CicReduction in
795 let module U = Utils in
796 let weight, proof, (eq_ty, left, right, ordering), _, _ = target in
798 let term = if ordering = U.Gt then left else right in
799 betaexpand_term metasenv context ugraph table 0 term
801 let maxmeta = ref newmeta in
802 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
804 print_endline "\nSUPERPOSITION LEFT\n";
806 let time1 = Unix.gettimeofday () in
808 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
809 let what, other = if pos = Utils.Left then what, other else other, what in
810 let newgoal, newproof =
811 let bo' = (* M. *)apply_subst s (S.subst other bo) in
813 (* let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in *)
814 (* incr sup_l_counter; *)
816 (* if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in *)
817 (* (name, ty, S.lift 1 eq_ty, l, r) *)
819 let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in
823 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
824 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
825 S.lift 1 eq_ty; l; r]
830 CicMkImplicit.identity_relocation_list_for_metavariable context in
831 C.Meta (!maxmeta, irl)
837 (* if pos = Utils.Left then *)
838 (* build_ens_for_sym_eq ty what other *)
840 (* build_ens_for_sym_eq ty other what *)
843 if pos = Utils.Left then [ty; what; other]
844 else [ty; other; what]
846 Inference.ProofSymBlock (termlist, proof')
849 if pos = Utils.Left then what, other else other, what
851 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
855 Inference.ProofBlock (s, eq_URI, (name, ty), bo''(* t' *), eq_found,
856 Inference.BasicProof metaproof)
859 | Inference.BasicProof _ ->
860 print_endline "replacing a BasicProof";
862 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
863 print_endline "replacing another ProofGoalBlock";
864 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
867 (* (weight, target_proof, (eq_ty, left, right, ordering), [], []) *)
870 C.Appl [C.MutConstruct (* reflexivity *)
871 (LibraryObjects.eq_URI (), 0, 1, []);
872 eq_ty; if ordering = U.Gt then right else left]
875 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
878 if ordering = U.Gt then newgoal, right else left, newgoal in
879 let neworder = !Utils.compare_terms left right in
881 let time2 = Unix.gettimeofday () in
882 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
885 let w = Utils.compute_equality_weight eq_ty left right in
886 (w, newproof, (eq_ty, left, right, neworder), [], [])
890 !maxmeta, List.map build_new expansions
894 let sup_r_counter = ref 1;;
896 let superposition_right newmeta (metasenv, context, ugraph) table target =
897 let module C = Cic in
898 let module S = CicSubstitution in
899 let module M = CicMetaSubst in
900 let module HL = HelmLibraryObjects in
901 let module CR = CicReduction in
902 let module U = Utils in
903 let _, eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
904 let metasenv' = metasenv @ newmetas in
905 let maxmeta = ref newmeta in
908 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
909 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
913 (fun (_, subst, _, _, _) ->
914 let subst = (* M. *)apply_subst subst in
915 let o = !Utils.compare_terms (subst l) (subst r) in
916 o <> U.Lt && o <> U.Le)
917 (fst (betaexpand_term metasenv' context ugraph table 0 l))
919 (res left right), (res right left)
921 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
923 let time1 = Unix.gettimeofday () in
925 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
926 let what, other = if pos = Utils.Left then what, other else other, what in
927 let newgoal, newproof =
928 let bo' = (* M. *)apply_subst s (S.subst other bo) in
930 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
933 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
934 (name, ty, S.lift 1 eq_ty, l, r)
936 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
940 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
941 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
942 S.lift 1 eq_ty; l; r]
945 Inference.ProofBlock (
946 s, eq_URI, (name, ty), bo''(* t' *), eq_found, eqproof)
948 let newmeta, newequality =
950 if ordering = U.Gt then newgoal, (* M. *)apply_subst s right
951 else (* M. *)apply_subst s left, newgoal in
952 let neworder = !Utils.compare_terms left right
953 and newmenv = newmetas @ menv'
954 and newargs = args @ args' in
956 (* (Inference.metas_of_term left) @ (Inference.metas_of_term right) in *)
959 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) *)
962 (* let delta = (List.length args) - (List.length a) in *)
963 (* if delta > 0 then *)
964 (* let first = List.hd a in *)
965 (* let rec aux l = function *)
967 (* | d -> let l = aux l (d-1) in l @ [first] *)
974 let w = Utils.compute_equality_weight eq_ty left right in
975 (w, newproof, (eq_ty, left, right, neworder), newmenv, newargs)
976 and env = (metasenv, context, ugraph) in
977 let newm, eq' = Inference.fix_metas !maxmeta eq' in
982 let time2 = Unix.gettimeofday () in
983 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
987 let new1 = List.map (build_new U.Gt) res1
988 and new2 = List.map (build_new U.Lt) res2 in
989 (* let ok = function *)
990 (* | _, _, (_, left, right, _), _, _ -> *)
991 (* not (fst (CR.are_convertible context left right ugraph)) *)
993 let ok e = not (Inference.is_identity (metasenv, context, ugraph) e) in
995 (List.filter ok (new1 @ new2)))
999 let rec demodulation_goal newmeta env table goal =
1000 let module C = Cic in
1001 let module S = CicSubstitution in
1002 let module M = CicMetaSubst in
1003 let module HL = HelmLibraryObjects in
1004 let metasenv, context, ugraph = env in
1005 let maxmeta = ref newmeta in
1006 let proof, metas, term = goal in
1007 let metasenv' = metasenv @ metas in
1009 let build_newgoal (t, subst, menv, ug, (eq_found, eq_URI)) =
1010 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
1011 let what, other = if pos = Utils.Left then what, other else other, what in
1013 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
1014 with CicUtil.Meta_not_found _ -> ty
1016 let newterm, newproof =
1017 let bo = (* M. *)apply_subst subst (S.subst other t) in
1018 let bo' = apply_subst subst t in
1019 let name = C.Name ("x_DemodGoal_" ^ (string_of_int !demod_counter)) in
1024 CicMkImplicit.identity_relocation_list_for_metavariable context in
1025 Printf.printf "\nADDING META: %d\n" !maxmeta;
1027 C.Meta (!maxmeta, irl)
1032 (* if pos = Utils.Left then build_ens_for_sym_eq ty what other *)
1033 (* else build_ens_for_sym_eq ty other what *)
1036 if pos = Utils.Left then [ty; what; other]
1037 else [ty; other; what]
1039 Inference.ProofSymBlock (termlist, proof')
1042 if pos = Utils.Left then what, other else other, what
1044 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
1048 Inference.ProofBlock (subst, eq_URI, (name, ty), bo',
1049 eq_found, Inference.BasicProof metaproof)
1051 let rec repl = function
1052 | Inference.NoProof ->
1053 debug_print (lazy "replacing a NoProof");
1055 | Inference.BasicProof _ ->
1056 debug_print (lazy "replacing a BasicProof");
1058 | Inference.ProofGoalBlock (_, parent_proof) ->
1059 debug_print (lazy "replacing another ProofGoalBlock");
1060 Inference.ProofGoalBlock (pb, parent_proof)
1061 | (Inference.SubProof (term, meta_index, p) as subproof) ->
1064 (Printf.sprintf "replacing %s"
1065 (Inference.string_of_proof subproof)));
1066 Inference.SubProof (term, meta_index, repl p)
1070 bo, Inference.ProofGoalBlock (Inference.NoProof, goal_proof)
1072 let m = Inference.metas_of_term newterm in
1073 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1074 !maxmeta, (newproof, newmetasenv, newterm)
1076 let res = demodulation_aux metasenv' context ugraph table 0 term in
1079 let newmeta, newgoal = build_newgoal t in
1080 let _, _, newg = newgoal in
1081 if Inference.meta_convertibility term newg then
1084 demodulation_goal newmeta env table newgoal
1090 let rec demodulation_theorem newmeta env table theorem =
1091 let module C = Cic in
1092 let module S = CicSubstitution in
1093 let module M = CicMetaSubst in
1094 let module HL = HelmLibraryObjects in
1095 let metasenv, context, ugraph = env in
1096 let maxmeta = ref newmeta in
1097 let proof, metas, term = theorem in
1098 let term, termty, metas = theorem in
1099 let metasenv' = metasenv @ metas in
1101 let build_newtheorem (t, subst, menv, ug, (eq_found, eq_URI)) =
1102 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
1103 let what, other = if pos = Utils.Left then what, other else other, what in
1104 let newterm, newty =
1105 let bo = apply_subst subst (S.subst other t) in
1106 let bo' = apply_subst subst t in
1107 let name = C.Name ("x_DemodThm_" ^ (string_of_int !demod_counter)) in
1110 Inference.ProofBlock (subst, eq_URI, (name, ty), bo', eq_found,
1111 Inference.BasicProof term)
1113 (Inference.build_proof_term newproof, bo)
1115 let m = Inference.metas_of_term newterm in
1116 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1117 !maxmeta, (newterm, newty, newmetasenv)
1119 let res = demodulation_aux metasenv' context ugraph table 0 termty in
1122 let newmeta, newthm = build_newtheorem t in
1123 let newt, newty, _ = newthm in
1124 if Inference.meta_convertibility termty newty then
1127 demodulation_theorem newmeta env table newthm