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 *)
205 let check = match termty with C.Implicit None -> false | _ -> true in
209 let pos, (_, proof, (ty, left, right, o), metas, args) = candidate in
210 if check && not (fst (CicReduction.are_convertible
211 ~metasenv context termty ty ugraph)) then (
212 (* debug_print (lazy ( *)
213 (* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *)
214 (* (CicPp.pp termty names) (CicPp.pp ty names))); *)
215 find_matches metasenv context ugraph lift_amount term termty tl
217 let do_match c (* other *) eq_URI =
218 let subst', metasenv', ugraph' =
219 let t1 = Unix.gettimeofday () in
222 Inference.matching (metasenv @ metas) context
223 term (S.lift lift_amount c) ugraph in
224 let t2 = Unix.gettimeofday () in
225 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
227 with Inference.MatchingFailure as e ->
228 let t2 = Unix.gettimeofday () in
229 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
232 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
235 let c, other, eq_URI =
236 if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
237 else right, left, Utils.eq_ind_r_URI ()
239 if o <> U.Incomparable then
241 do_match c (* other *) eq_URI
242 with Inference.MatchingFailure ->
243 find_matches metasenv context ugraph lift_amount term termty tl
246 try do_match c (* other *) eq_URI
247 with Inference.MatchingFailure -> None
250 | Some (_, s, _, _, _) ->
251 let c' = (* M. *)apply_subst s c
252 and other' = (* M. *)apply_subst s other in
253 let order = cmp c' other' in
254 let names = U.names_of_context context in
257 (* (Printf.sprintf "OK matching: %s and %s, order: %s" *)
258 (* (CicPp.ppterm c') *)
259 (* (CicPp.ppterm other') *)
260 (* (Utils.string_of_comparison order)); *)
262 (* (Printf.sprintf "subst:\n%s\n" (Utils.print_subst s)) *)
268 metasenv context ugraph lift_amount term termty tl
270 find_matches metasenv context ugraph lift_amount term termty tl
274 let rec find_all_matches ?(unif_fun=Inference.unification)
275 metasenv context ugraph lift_amount term termty =
276 let module C = Cic in
277 let module U = Utils in
278 let module S = CicSubstitution in
279 let module M = CicMetaSubst in
280 let module HL = HelmLibraryObjects in
281 let cmp = !Utils.compare_terms in
282 (* let names = Utils.names_of_context context in *)
283 (* let termty, ugraph = *)
284 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
287 (* match term with *)
288 (* | C.Meta _ -> assert false *)
294 let pos, (_, _, (ty, left, right, o), metas, args) = candidate in
295 (* if not (fst (CicReduction.are_convertible *)
296 (* ~metasenv context termty ty ugraph)) then ( *)
297 (* (\* debug_print (lazy ( *\) *)
298 (* (\* Printf.sprintf "CANDIDATE HAS WRONG TYPE: %s required, %s found" *\) *)
299 (* (\* (CicPp.pp termty names) (CicPp.pp ty names))); *\) *)
300 (* find_all_matches ~unif_fun metasenv context ugraph *)
301 (* lift_amount term termty tl *)
303 let do_match c (* other *) eq_URI =
304 let subst', metasenv', ugraph' =
305 let t1 = Unix.gettimeofday () in
308 unif_fun (metasenv @ metas) context
309 term (S.lift lift_amount c) ugraph in
310 let t2 = Unix.gettimeofday () in
311 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
314 | Inference.MatchingFailure
315 | CicUnification.UnificationFailure _
316 | CicUnification.Uncertain _ as e ->
317 let t2 = Unix.gettimeofday () in
318 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
321 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
324 let c, other, eq_URI =
325 if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
326 else right, left, Utils.eq_ind_r_URI ()
328 if o <> U.Incomparable then
330 let res = do_match c (* other *) eq_URI in
331 res::(find_all_matches ~unif_fun metasenv context ugraph
332 lift_amount term termty tl)
334 | Inference.MatchingFailure
335 | CicUnification.UnificationFailure _
336 | CicUnification.Uncertain _ ->
337 find_all_matches ~unif_fun metasenv context ugraph
338 lift_amount term termty tl
341 let res = do_match c (* other *) eq_URI in
344 let c' = (* M. *)apply_subst s c
345 and other' = (* M. *)apply_subst s other in
346 let order = cmp c' other' in
347 let names = U.names_of_context context in
348 if order <> U.Lt && order <> U.Le then
349 res::(find_all_matches ~unif_fun metasenv context ugraph
350 lift_amount term termty tl)
352 find_all_matches ~unif_fun metasenv context ugraph
353 lift_amount term termty tl
355 | Inference.MatchingFailure
356 | CicUnification.UnificationFailure _
357 | CicUnification.Uncertain _ ->
358 find_all_matches ~unif_fun metasenv context ugraph
359 lift_amount term termty tl
363 let subsumption env table target =
364 let _, (ty, left, right, _), tmetas, _ = target in
365 let metasenv, context, ugraph = env in
366 let metasenv = metasenv @ tmetas in
367 let samesubst subst subst' =
368 let tbl = Hashtbl.create (List.length subst) in
369 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
371 (fun (m, (c, t1, t2)) ->
373 let c', t1', t2' = Hashtbl.find tbl m in
374 if (c = c') && (t1 = t1') && (t2 = t2') then true
384 let leftc = get_candidates Matching table left in
385 find_all_matches ~unif_fun:Inference.matching
386 metasenv context ugraph 0 left ty leftc
388 let rec ok what = function
390 | (_, subst, menv, ug, ((pos, (_, _, (_, l, r, o), _, _)), _))::tl ->
392 let other = if pos = Utils.Left then r else l in
393 let subst', menv', ug' =
394 let t1 = Unix.gettimeofday () in
397 Inference.matching metasenv context what other ugraph in
398 let t2 = Unix.gettimeofday () in
399 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
401 with Inference.MatchingFailure as e ->
402 let t2 = Unix.gettimeofday () in
403 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
406 if samesubst subst subst' then
410 with Inference.MatchingFailure ->
413 let r, subst = ok right leftr in
421 let rightc = get_candidates Matching table right in
422 find_all_matches ~unif_fun:Inference.matching
423 metasenv context ugraph 0 right ty rightc
429 let rec demodulation_aux ?(typecheck=false)
430 metasenv context ugraph table lift_amount term =
431 let module C = Cic in
432 let module S = CicSubstitution in
433 let module M = CicMetaSubst in
434 let module HL = HelmLibraryObjects in
435 let candidates = get_candidates Matching table term in
441 CicTypeChecker.type_of_aux' metasenv context term ugraph
443 C.Implicit None, ugraph
446 find_matches metasenv context ugraph lift_amount term termty candidates
457 (res, tl @ [S.lift 1 t])
460 demodulation_aux metasenv context ugraph table
464 | None -> (None, tl @ [S.lift 1 t])
465 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
470 | Some (_, subst, menv, ug, eq_found) ->
471 Some (C.Appl ll, subst, menv, ug, eq_found)
473 | C.Prod (nn, s, t) ->
475 demodulation_aux metasenv context ugraph table lift_amount s in (
479 demodulation_aux 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.Prod (nn, (S.lift 1 s), t'),
487 subst, menv, ug, eq_found)
489 | Some (s', subst, menv, ug, eq_found) ->
490 Some (C.Prod (nn, s', (S.lift 1 t)),
491 subst, menv, ug, eq_found)
493 | C.Lambda (nn, s, t) ->
495 demodulation_aux metasenv context ugraph table lift_amount s in (
499 demodulation_aux metasenv
500 ((Some (nn, C.Decl s))::context) ugraph
501 table (lift_amount+1) t
505 | Some (t', subst, menv, ug, eq_found) ->
506 Some (C.Lambda (nn, (S.lift 1 s), t'),
507 subst, menv, ug, eq_found)
509 | Some (s', subst, menv, ug, eq_found) ->
510 Some (C.Lambda (nn, s', (S.lift 1 t)),
511 subst, menv, ug, eq_found)
518 let build_newtarget_time = ref 0.;;
521 let demod_counter = ref 1;;
523 let rec demodulation_equality newmeta env table sign target =
524 let module C = Cic in
525 let module S = CicSubstitution in
526 let module M = CicMetaSubst in
527 let module HL = HelmLibraryObjects in
528 let metasenv, context, ugraph = env in
529 let _, proof, (eq_ty, left, right, order), metas, args = target in
530 let metasenv' = metasenv @ metas in
532 let maxmeta = ref newmeta in
534 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
535 let time1 = Unix.gettimeofday () in
537 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
539 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
540 with CicUtil.Meta_not_found _ -> ty
542 let what, other = if pos = Utils.Left then what, other else other, what in
543 let newterm, newproof =
544 let bo = (* M. *)apply_subst subst (S.subst other t) in
546 (* let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in *)
547 (* incr demod_counter; *)
549 (* if is_left then t, S.lift 1 right else S.lift 1 left, t in *)
550 (* (name, ty, S.lift 1 eq_ty, l, r) *)
552 let name = C.Name ("x_Demod_" ^ (string_of_int !demod_counter)) in
555 let l, r = if is_left then t, S.lift 1 right else S.lift 1 left, t in
556 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
557 S.lift 1 eq_ty; l; r]
559 if sign = Utils.Positive then
561 Inference.ProofBlock (
562 subst, eq_URI, (name, ty), bo'(* t' *), eq_found, proof))
567 CicMkImplicit.identity_relocation_list_for_metavariable context in
568 Printf.printf "\nADDING META: %d\n" !maxmeta;
570 C.Meta (!maxmeta, irl)
576 (* if pos = Utils.Left then *)
577 (* build_ens_for_sym_eq ty what other *)
579 (* build_ens_for_sym_eq ty other what *)
581 if pos = Utils.Left then [ty; what; other]
582 else [ty; other; what]
584 Inference.ProofSymBlock (termlist, proof')
587 if pos = Utils.Left then what, other else other, what
589 pos, (0, proof', (ty, other, what, Utils.Incomparable),
594 Inference.ProofBlock (subst, eq_URI, (name, ty), bo'(* t' *),
595 eq_found, Inference.BasicProof metaproof)
598 | Inference.BasicProof _ ->
599 print_endline "replacing a BasicProof";
601 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
602 print_endline "replacing another ProofGoalBlock";
603 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
606 (* (0, target_proof, (eq_ty, left, right, order), metas, args) *)
609 C.Appl [C.MutConstruct (* reflexivity *)
610 (LibraryObjects.eq_URI (), 0, 1, []);
611 eq_ty; if is_left then right else left]
614 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
616 let left, right = if is_left then newterm, right else left, newterm in
617 let m = (Inference.metas_of_term left) @ (Inference.metas_of_term right) in
618 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
622 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) args in *)
623 (* let delta = (List.length args) - (List.length a) in *)
624 (* if delta > 0 then *)
625 (* let first = List.hd a in *)
626 (* let rec aux l = function *)
628 (* | d -> let l = aux l (d-1) in l @ [first] *)
634 let ordering = !Utils.compare_terms left right in
636 let time2 = Unix.gettimeofday () in
637 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
640 let w = Utils.compute_equality_weight eq_ty left right in
641 (w, newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
645 let res = demodulation_aux metasenv' context ugraph table 0 left in
648 let newmeta, newtarget = build_newtarget true t in
649 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
650 (Inference.meta_convertibility_eq target newtarget) then
653 (* if subsumption env table newtarget then *)
654 (* newmeta, build_identity newtarget *)
656 demodulation_equality newmeta env table sign newtarget
658 let res = demodulation_aux metasenv' context ugraph table 0 right in
661 let newmeta, newtarget = build_newtarget false t in
662 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
663 (Inference.meta_convertibility_eq target newtarget) then
666 (* if subsumption env table newtarget then *)
667 (* newmeta, build_identity newtarget *)
669 demodulation_equality newmeta env table sign newtarget
675 let rec betaexpand_term metasenv context ugraph table lift_amount term =
676 let module C = Cic in
677 let module S = CicSubstitution in
678 let module M = CicMetaSubst in
679 let module HL = HelmLibraryObjects in
680 let candidates = get_candidates Unification table term in
681 let res, lifted_term =
686 (fun arg (res, lifted_tl) ->
689 let arg_res, lifted_arg =
690 betaexpand_term metasenv context ugraph table
694 (fun (t, s, m, ug, eq_found) ->
695 (Some t)::lifted_tl, s, m, ug, eq_found)
700 (fun (l, s, m, ug, eq_found) ->
701 (Some lifted_arg)::l, s, m, ug, eq_found)
703 (Some lifted_arg)::lifted_tl)
706 (fun (r, s, m, ug, eq_found) ->
707 None::r, s, m, ug, eq_found) res,
713 (fun (l, s, m, ug, eq_found) ->
714 (C.Meta (i, l), s, m, ug, eq_found)) l'
716 e, C.Meta (i, lifted_l)
719 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
721 | C.Prod (nn, s, t) ->
723 betaexpand_term metasenv context ugraph table lift_amount s in
725 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
726 table (lift_amount+1) t in
729 (fun (t, s, m, ug, eq_found) ->
730 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
733 (fun (t, s, m, ug, eq_found) ->
734 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
735 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
737 | C.Lambda (nn, s, t) ->
739 betaexpand_term metasenv context ugraph table lift_amount s in
741 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
742 table (lift_amount+1) t in
745 (fun (t, s, m, ug, eq_found) ->
746 C.Lambda (nn, t, lifted_t), s, m, ug, eq_found) l1
749 (fun (t, s, m, ug, eq_found) ->
750 C.Lambda (nn, lifted_s, t), s, m, ug, eq_found) l2 in
751 l1' @ l2', C.Lambda (nn, lifted_s, lifted_t)
756 (fun arg (res, lifted_tl) ->
757 let arg_res, lifted_arg =
758 betaexpand_term metasenv context ugraph table lift_amount arg
762 (fun (a, s, m, ug, eq_found) ->
763 a::lifted_tl, s, m, ug, eq_found)
768 (fun (r, s, m, ug, eq_found) ->
769 lifted_arg::r, s, m, ug, eq_found)
771 lifted_arg::lifted_tl)
775 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
778 | t -> [], (S.lift lift_amount t)
781 | C.Meta (i, l) -> res, lifted_term
784 C.Implicit None, ugraph
785 (* CicTypeChecker.type_of_aux' metasenv context term ugraph *)
789 metasenv context ugraph lift_amount term termty candidates
795 let sup_l_counter = ref 1;;
797 let superposition_left newmeta (metasenv, context, ugraph) table target =
798 let module C = Cic in
799 let module S = CicSubstitution in
800 let module M = CicMetaSubst in
801 let module HL = HelmLibraryObjects in
802 let module CR = CicReduction in
803 let module U = Utils in
804 let weight, proof, (eq_ty, left, right, ordering), _, _ = target in
806 let term = if ordering = U.Gt then left else right in
807 betaexpand_term metasenv context ugraph table 0 term
809 let maxmeta = ref newmeta in
810 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
812 print_endline "\nSUPERPOSITION LEFT\n";
814 let time1 = Unix.gettimeofday () in
816 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
817 let what, other = if pos = Utils.Left then what, other else other, what in
818 let newgoal, newproof =
819 let bo' = (* M. *)apply_subst s (S.subst other bo) in
821 (* let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in *)
822 (* incr sup_l_counter; *)
824 (* if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in *)
825 (* (name, ty, S.lift 1 eq_ty, l, r) *)
827 let name = C.Name ("x_SupL_" ^ (string_of_int !sup_l_counter)) in
831 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
832 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
833 S.lift 1 eq_ty; l; r]
838 CicMkImplicit.identity_relocation_list_for_metavariable context in
839 C.Meta (!maxmeta, irl)
845 (* if pos = Utils.Left then *)
846 (* build_ens_for_sym_eq ty what other *)
848 (* build_ens_for_sym_eq ty other what *)
851 if pos = Utils.Left then [ty; what; other]
852 else [ty; other; what]
854 Inference.ProofSymBlock (termlist, proof')
857 if pos = Utils.Left then what, other else other, what
859 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
863 Inference.ProofBlock (s, eq_URI, (name, ty), bo''(* t' *), eq_found,
864 Inference.BasicProof metaproof)
867 | Inference.BasicProof _ ->
868 print_endline "replacing a BasicProof";
870 | Inference.ProofGoalBlock (_, parent_proof(* parent_eq *)) ->
871 print_endline "replacing another ProofGoalBlock";
872 Inference.ProofGoalBlock (pb, parent_proof(* parent_eq *))
875 (* (weight, target_proof, (eq_ty, left, right, ordering), [], []) *)
878 C.Appl [C.MutConstruct (* reflexivity *)
879 (LibraryObjects.eq_URI (), 0, 1, []);
880 eq_ty; if ordering = U.Gt then right else left]
883 Inference.ProofGoalBlock (Inference.BasicProof refl, target_proof(* target' *)))
886 if ordering = U.Gt then newgoal, right else left, newgoal in
887 let neworder = !Utils.compare_terms left right in
889 let time2 = Unix.gettimeofday () in
890 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
893 let w = Utils.compute_equality_weight eq_ty left right in
894 (w, newproof, (eq_ty, left, right, neworder), [], [])
898 !maxmeta, List.map build_new expansions
902 let sup_r_counter = ref 1;;
904 let superposition_right newmeta (metasenv, context, ugraph) table target =
905 let module C = Cic in
906 let module S = CicSubstitution in
907 let module M = CicMetaSubst in
908 let module HL = HelmLibraryObjects in
909 let module CR = CicReduction in
910 let module U = Utils in
911 let _, eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
912 let metasenv' = metasenv @ newmetas in
913 let maxmeta = ref newmeta in
916 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
917 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
921 (fun (_, subst, _, _, _) ->
922 let subst = (* M. *)apply_subst subst in
923 let o = !Utils.compare_terms (subst l) (subst r) in
924 o <> U.Lt && o <> U.Le)
925 (fst (betaexpand_term metasenv' context ugraph table 0 l))
927 (res left right), (res right left)
929 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
931 let time1 = Unix.gettimeofday () in
933 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
934 let what, other = if pos = Utils.Left then what, other else other, what in
935 let newgoal, newproof =
936 let bo' = (* M. *)apply_subst s (S.subst other bo) in
938 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
941 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
942 (name, ty, S.lift 1 eq_ty, l, r)
944 let name = C.Name ("x_SupR_" ^ (string_of_int !sup_r_counter)) in
948 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
949 C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
950 S.lift 1 eq_ty; l; r]
953 Inference.ProofBlock (
954 s, eq_URI, (name, ty), bo''(* t' *), eq_found, eqproof)
956 let newmeta, newequality =
958 if ordering = U.Gt then newgoal, (* M. *)apply_subst s right
959 else (* M. *)apply_subst s left, newgoal in
960 let neworder = !Utils.compare_terms left right
961 and newmenv = newmetas @ menv'
962 and newargs = args @ args' in
964 (* (Inference.metas_of_term left) @ (Inference.metas_of_term right) in *)
967 (* (function C.Meta (i, _) -> List.mem i m | _ -> assert false) *)
970 (* let delta = (List.length args) - (List.length a) in *)
971 (* if delta > 0 then *)
972 (* let first = List.hd a in *)
973 (* let rec aux l = function *)
975 (* | d -> let l = aux l (d-1) in l @ [first] *)
982 let w = Utils.compute_equality_weight eq_ty left right in
983 (w, newproof, (eq_ty, left, right, neworder), newmenv, newargs)
984 and env = (metasenv, context, ugraph) in
985 let newm, eq' = Inference.fix_metas !maxmeta eq' in
990 let time2 = Unix.gettimeofday () in
991 build_newtarget_time := !build_newtarget_time +. (time2 -. time1);
995 let new1 = List.map (build_new U.Gt) res1
996 and new2 = List.map (build_new U.Lt) res2 in
997 (* let ok = function *)
998 (* | _, _, (_, left, right, _), _, _ -> *)
999 (* not (fst (CR.are_convertible context left right ugraph)) *)
1001 let ok e = not (Inference.is_identity (metasenv, context, ugraph) e) in
1003 (List.filter ok (new1 @ new2)))
1007 let rec demodulation_goal newmeta env table goal =
1008 let module C = Cic in
1009 let module S = CicSubstitution in
1010 let module M = CicMetaSubst in
1011 let module HL = HelmLibraryObjects in
1012 let metasenv, context, ugraph = env in
1013 let maxmeta = ref newmeta in
1014 let proof, metas, term = goal in
1015 let metasenv' = metasenv @ metas in
1017 let build_newgoal (t, subst, menv, ug, (eq_found, eq_URI)) =
1018 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
1019 let what, other = if pos = Utils.Left then what, other else other, what in
1021 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
1022 with CicUtil.Meta_not_found _ -> ty
1024 let newterm, newproof =
1025 let bo = (* M. *)apply_subst subst (S.subst other t) in
1026 let bo' = apply_subst subst t in
1027 let name = C.Name ("x_DemodGoal_" ^ (string_of_int !demod_counter)) in
1032 CicMkImplicit.identity_relocation_list_for_metavariable context in
1033 Printf.printf "\nADDING META: %d\n" !maxmeta;
1035 C.Meta (!maxmeta, irl)
1040 (* if pos = Utils.Left then build_ens_for_sym_eq ty what other *)
1041 (* else build_ens_for_sym_eq ty other what *)
1044 if pos = Utils.Left then [ty; what; other]
1045 else [ty; other; what]
1047 Inference.ProofSymBlock (termlist, proof')
1050 if pos = Utils.Left then what, other else other, what
1052 pos, (0, proof', (ty, other, what, Utils.Incomparable), menv', args')
1056 Inference.ProofBlock (subst, eq_URI, (name, ty), bo',
1057 eq_found, Inference.BasicProof metaproof)
1059 let rec repl = function
1060 | Inference.NoProof ->
1061 debug_print (lazy "replacing a NoProof");
1063 | Inference.BasicProof _ ->
1064 debug_print (lazy "replacing a BasicProof");
1066 | Inference.ProofGoalBlock (_, parent_proof) ->
1067 debug_print (lazy "replacing another ProofGoalBlock");
1068 Inference.ProofGoalBlock (pb, parent_proof)
1069 | (Inference.SubProof (term, meta_index, p) as subproof) ->
1072 (Printf.sprintf "replacing %s"
1073 (Inference.string_of_proof subproof)));
1074 Inference.SubProof (term, meta_index, repl p)
1078 bo, Inference.ProofGoalBlock (Inference.NoProof, goal_proof)
1080 let m = Inference.metas_of_term newterm in
1081 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1082 !maxmeta, (newproof, newmetasenv, newterm)
1085 demodulation_aux ~typecheck:true metasenv' context ugraph table 0 term
1089 let newmeta, newgoal = build_newgoal t in
1090 let _, _, newg = newgoal in
1091 if Inference.meta_convertibility term newg then
1094 demodulation_goal newmeta env table newgoal
1100 let rec demodulation_theorem newmeta env table theorem =
1101 let module C = Cic in
1102 let module S = CicSubstitution in
1103 let module M = CicMetaSubst in
1104 let module HL = HelmLibraryObjects in
1105 let metasenv, context, ugraph = env in
1106 let maxmeta = ref newmeta in
1107 let proof, metas, term = theorem in
1108 let term, termty, metas = theorem in
1109 let metasenv' = metasenv @ metas in
1111 let build_newtheorem (t, subst, menv, ug, (eq_found, eq_URI)) =
1112 let pos, (_, proof', (ty, what, other, _), menv', args') = eq_found in
1113 let what, other = if pos = Utils.Left then what, other else other, what in
1114 let newterm, newty =
1115 let bo = apply_subst subst (S.subst other t) in
1116 let bo' = apply_subst subst t in
1117 let name = C.Name ("x_DemodThm_" ^ (string_of_int !demod_counter)) in
1120 Inference.ProofBlock (subst, eq_URI, (name, ty), bo', eq_found,
1121 Inference.BasicProof term)
1123 (Inference.build_proof_term newproof, bo)
1125 let m = Inference.metas_of_term newterm in
1126 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas in
1127 !maxmeta, (newterm, newty, newmetasenv)
1130 demodulation_aux ~typecheck:true metasenv' context ugraph table 0 termty
1134 let newmeta, newthm = build_newtheorem t in
1135 let newt, newty, _ = newthm in
1136 if Inference.meta_convertibility termty newty then
1139 demodulation_theorem newmeta env table newthm