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 _profiler = <:profiler<_profiler>>;; *)
30 module Index = Equality_indexing.DT (* discrimination tree based indexing *)
32 module Index = Equality_indexing.DT (* path tree based indexing *)
35 let debug_print = Utils.debug_print;;
39 let check_equation env equation msg =
40 let w, proof, (eq_ty, left, right, order), metas, args = equation in
41 let metasenv, context, ugraph = env
42 let metasenv' = metasenv @ metas in
44 CicTypeChecker.type_of_aux' metasenv' context left ugraph;
45 CicTypeChecker.type_of_aux' metasenv' context right ugraph;
48 CicUtil.Meta_not_found _ as exn ->
51 prerr_endline (CicPp.ppterm left);
52 prerr_endline (CicPp.ppterm right);
57 type retrieval_mode = Matching | Unification;;
59 let string_of_res ?env =
62 | Some (t, s, m, u, (p,e)) ->
63 Printf.sprintf "Some: (%s, %s, %s)"
64 (Utils.string_of_pos p)
65 (Equality.string_of_equality ?env e)
69 let print_res ?env res =
72 (List.map (string_of_res ?env) res))
75 let print_candidates ?env mode term res =
79 prerr_endline ("| candidates Matching " ^ (CicPp.ppterm term))
81 prerr_endline ("| candidates Unification " ^ (CicPp.ppterm term))
87 Printf.sprintf "| (%s, %s)" (Utils.string_of_pos p)
88 (Equality.string_of_equality ?env e))
93 let apply_subst = Subst.apply_subst
95 let index = Index.index
96 let remove_index = Index.remove_index
97 let in_index = Index.in_index
98 let empty = Index.empty
99 let init_index = Index.init_index
101 let check_disjoint_invariant subst metasenv msg =
103 (fun (i,_,_) -> (Subst.is_in_subst i subst)) metasenv)
106 prerr_endline ("not disjoint: " ^ msg);
111 let check_for_duplicates metas msg =
112 let rec aux = function
114 | (m,_,_)::tl -> not (List.exists (fun (i, _, _) -> i = m) tl) && aux tl in
118 prerr_endline ("DUPLICATI " ^ msg);
119 prerr_endline (CicMetaSubst.ppmetasenv [] metas);
125 let check_metasenv msg menv =
128 try ignore(CicTypeChecker.type_of_aux' menv ctx ty
129 CicUniv.empty_ugraph)
131 | CicUtil.Meta_not_found _ ->
132 prerr_endline (msg ^ CicMetaSubst.ppmetasenv [] menv);
138 (* the metasenv returned by res must included in the original one,
139 due to matching. If it fails, it is probably because we are not
140 demodulating with a unit equality *)
142 let not_unit_eq ctx eq =
143 let (_,_,(ty,left,right,o),metas,_) = Equality.open_equality eq in
148 let s,_ = CicTypeChecker.type_of_aux' metas ctx ty CicUniv.empty_ugraph
149 in s = Cic.Sort(Cic.Prop)
151 prerr_endline ("ERROR typing " ^ CicPp.ppterm ty); assert false) metas
154 if b then prerr_endline ("not a unit equality: " ^ Equality.string_of_equality eq); b *)
157 let check_demod_res res metasenv msg =
159 | Some (_, _, menv, _, _) ->
163 (List.exists (fun (j,_,_) -> i=j) metasenv)) menv
167 prerr_endline ("extended context " ^ msg);
168 prerr_endline (CicMetaSubst.ppmetasenv [] menv);
174 let check_res res msg =
176 | Some (t, subst, menv, ug, eq_found) ->
177 let eqs = Equality.string_of_equality (snd eq_found) in
178 check_metasenv msg menv;
179 check_disjoint_invariant subst menv msg;
180 check_for_duplicates menv (msg ^ "\nchecking " ^ eqs);
184 let check_target bag context target msg =
185 let w, proof, (eq_ty, left, right, order), metas,_ =
186 Equality.open_equality target in
187 (* check that metas does not contains duplicates *)
188 let eqs = Equality.string_of_equality target in
189 let _ = check_for_duplicates metas (msg ^ "\nchecking " ^ eqs) in
190 let actual = (Utils.metas_of_term left)@(Utils.metas_of_term right)
191 @(Utils.metas_of_term eq_ty)@(Equality.metas_of_proof bag proof) in
192 let menv = List.filter (fun (i, _, _) -> List.mem i actual) metas in
193 let _ = if menv <> metas then
195 prerr_endline ("extra metas " ^ msg);
196 prerr_endline (CicMetaSubst.ppmetasenv [] metas);
197 prerr_endline "**********************";
198 prerr_endline (CicMetaSubst.ppmetasenv [] menv);
199 prerr_endline ("left: " ^ (CicPp.ppterm left));
200 prerr_endline ("right: " ^ (CicPp.ppterm right));
201 prerr_endline ("ty: " ^ (CicPp.ppterm eq_ty));
207 ignore(CicTypeChecker.type_of_aux'
208 metas context (Founif.build_proof_term proof) CicUniv.empty_ugraph)
211 prerr_endline (Founif.string_of_proof proof);
212 prerr_endline (CicPp.ppterm (Founif.build_proof_term proof));
213 prerr_endline ("+++++++++++++left: " ^ (CicPp.ppterm left));
214 prerr_endline ("+++++++++++++right: " ^ (CicPp.ppterm right));
219 (* returns a list of all the equalities in the tree that are in relation
220 "mode" with the given term, where mode can be either Matching or
223 Format of the return value: list of tuples in the form:
224 (position - Left or Right - of the term that matched the given one in this
228 Note that if equality is "left = right", if the ordering is left > right,
229 the position will always be Left, and if the ordering is left < right,
230 position will be Right.
233 let get_candidates ?env mode tree term =
237 Index.retrieve_generalizations tree term
239 Index.retrieve_unifiables tree term
242 Index.PosEqSet.elements s
246 finds the first equality in the index that matches "term", of type "termty"
247 termty can be Implicit if it is not needed. The result (one of the sides of
248 the equality, actually) should be not greater (wrt the term ordering) than
251 Format of the return value:
253 (term to substitute, [Cic.Rel 1 properly lifted - see the various
254 build_newtarget functions inside the various
255 demodulation_* functions]
256 substitution used for the matching,
258 ugraph, [substitution, metasenv and ugraph have the same meaning as those
259 returned by CicUnification.fo_unif]
260 (equality where the matching term was found, [i.e. the equality to use as
262 uri [either eq_ind_URI or eq_ind_r_URI, depending on the direction of
263 the equality: this is used to build the proof term, again see one of
264 the build_newtarget functions]
267 let rec find_matches bag metasenv context ugraph lift_amount term termty =
268 let module C = Cic in
269 let module U = Utils in
270 let module S = CicSubstitution in
271 let module M = CicMetaSubst in
272 let module HL = HelmLibraryObjects in
273 let cmp = !Utils.compare_terms in
274 let check = match termty with C.Implicit None -> false | _ -> true in
278 let pos, equality = candidate in
279 (* if not_unit_eq context equality then
281 prerr_endline "not a unit";
282 prerr_endline (Equality.string_of_equality equality)
284 let (_, proof, (ty, left, right, o), metas,_) =
285 Equality.open_equality equality
287 if Utils.debug_metas then
288 ignore(check_target bag context (snd candidate) "find_matches");
289 if Utils.debug_res then
291 let c="eq = "^(Equality.string_of_equality (snd candidate)) ^ "\n"in
292 let t="t = " ^ (CicPp.ppterm term) ^ "\n" in
293 let m="metas = " ^ (CicMetaSubst.ppmetasenv [] metas) ^ "\n" in
294 let ms="metasenv =" ^ (CicMetaSubst.ppmetasenv [] metasenv) ^ "\n" in
296 match LibraryObjects.eq_URI () with
298 | None -> raise (ProofEngineTypes.Fail (lazy "equality not declared")) in
300 (CicPp.ppterm(Equality.build_proof_term bag eq_uri [] 0 proof))^"\n"
303 check_for_duplicates metas "gia nella metas";
304 check_for_duplicates metasenv "gia nel metasenv";
305 check_for_duplicates (metasenv@metas) ("not disjoint"^c^t^m^ms^p)
307 if check && not (fst (CicReduction.are_convertible
308 ~metasenv context termty ty ugraph)) then (
309 find_matches bag metasenv context ugraph lift_amount term termty tl
312 let subst', metasenv', ugraph' =
314 metasenv metas context term (S.lift lift_amount c) ugraph
316 check_metasenv "founif :" metasenv';
317 Some (Cic.Rel(1+lift_amount),subst',metasenv',ugraph',candidate)
320 if pos = Utils.Left then left, right
323 if o <> U.Incomparable then
327 with Founif.MatchingFailure ->
328 find_matches bag metasenv context ugraph lift_amount term termty tl
330 if Utils.debug_res then ignore (check_res res "find1");
335 with Founif.MatchingFailure -> None
337 if Utils.debug_res then ignore (check_res res "find2");
339 | Some (_, s, _, _, _) ->
340 let c' = apply_subst s c in
342 let other' = U.guarded_simpl context (apply_subst s other) in *)
343 let other' = apply_subst s other in
344 let order = cmp c' other' in
349 metasenv context ugraph lift_amount term termty tl
351 find_matches bag metasenv context ugraph lift_amount term termty tl
354 let find_matches metasenv context ugraph lift_amount term termty =
355 find_matches metasenv context ugraph lift_amount term termty
359 as above, but finds all the matching equalities, and the matching condition
360 can be either Founif.matching or Inference.unification
362 (* XXX termty unused *)
363 let rec find_all_matches ?(unif_fun=Founif.unification)
364 metasenv context ugraph lift_amount term termty =
365 let module C = Cic in
366 let module U = Utils in
367 let module S = CicSubstitution in
368 let module M = CicMetaSubst in
369 let module HL = HelmLibraryObjects in
370 (* prerr_endline ("matching " ^ CicPp.ppterm term); *)
371 let cmp = !Utils.compare_terms in
372 let check = match termty with C.Implicit None -> false | _ -> true in
376 let pos, equality = candidate in
377 let (_,_,(ty,left,right,o),metas,_)= Equality.open_equality equality in
378 if check && not (fst (CicReduction.are_convertible
379 ~metasenv context termty ty ugraph)) then (
380 find_all_matches metasenv context ugraph lift_amount term termty tl
383 let subst', metasenv', ugraph' =
384 unif_fun metasenv metas context term (S.lift lift_amount c) ugraph
386 (C.Rel (1+lift_amount),subst',metasenv',ugraph',candidate)
390 if pos = Utils.Left then left, right
393 if o <> U.Incomparable then
395 let res = do_match c in
396 res::(find_all_matches ~unif_fun metasenv context ugraph
397 lift_amount term termty tl)
399 | Founif.MatchingFailure
400 | CicUnification.UnificationFailure _
401 | CicUnification.Uncertain _ ->
402 find_all_matches ~unif_fun metasenv context ugraph
403 lift_amount term termty tl
406 let res = do_match c in
409 let c' = apply_subst s c
410 and other' = apply_subst s other in
411 let order = cmp c' other' in
412 if order <> U.Lt && order <> U.Le then
413 res::(find_all_matches ~unif_fun metasenv context ugraph
414 lift_amount term termty tl)
416 find_all_matches ~unif_fun metasenv context ugraph
417 lift_amount term termty tl
419 | Founif.MatchingFailure
420 | CicUnification.UnificationFailure _
421 | CicUnification.Uncertain _ ->
422 find_all_matches ~unif_fun metasenv context ugraph
423 lift_amount term termty tl
427 ?unif_fun metasenv context ugraph lift_amount term termty l
430 ?unif_fun metasenv context ugraph lift_amount term termty l
431 (*prerr_endline "CANDIDATES:";
432 List.iter (fun (_,x)->prerr_endline (Founif.string_of_equality x)) l;
433 prerr_endline ("MATCHING:" ^ CicPp.ppterm term ^ " are " ^ string_of_int
437 returns true if target is subsumed by some equality in table
441 prerr_endline (String.concat "\n" (List.map (fun (_, subst, menv, ug,
442 ((pos,equation),_)) -> Equality.string_of_equality equation)l))
446 let subsumption_aux use_unification env table target =
447 let _, _, (ty, left, right, _), tmetas, _ = Equality.open_equality target in
448 let _, context, ugraph = env in
449 let metasenv = tmetas in
450 let predicate, unif_fun =
451 if use_unification then
452 Unification, Founif.unification
454 Matching, Founif.matching
458 | Cic.Meta _ when not use_unification -> []
460 let leftc = get_candidates predicate table left in
461 find_all_matches ~unif_fun
462 metasenv context ugraph 0 left ty leftc
464 let rec ok what leftorright = function
466 | (_, subst, menv, ug, (pos,equation))::tl ->
467 let _, _, (_, l, r, o), m,_ = Equality.open_equality equation in
469 let other = if pos = Utils.Left then r else l in
470 let what' = Subst.apply_subst subst what in
471 let other' = Subst.apply_subst subst other in
472 let subst', menv', ug' =
473 unif_fun metasenv m context what' other' ugraph
475 (match Subst.merge_subst_if_possible subst subst' with
476 | None -> ok what leftorright tl
477 | Some s -> Some (s, equation, leftorright <> pos ))
479 | Founif.MatchingFailure
480 | CicUnification.UnificationFailure _ -> ok what leftorright tl
482 match ok right Utils.Left leftr with
483 | Some _ as res -> res
487 | Cic.Meta _ when not use_unification -> []
489 let rightc = get_candidates predicate table right in
490 find_all_matches ~unif_fun
491 metasenv context ugraph 0 right ty rightc
493 ok left Utils.Right rightr
496 let subsumption x y z =
497 subsumption_aux false x y z
500 let unification x y z =
501 subsumption_aux true x y z
504 (* the target must be disjoint from the equations in the table *)
505 let subsumption_aux_all use_unification env table target =
506 let _, _, (ty, left, right, _), tmetas, _ = Equality.open_equality target in
507 let _, context, ugraph = env in
508 let metasenv = tmetas in
509 check_for_duplicates metasenv "subsumption_aux_all";
510 let predicate, unif_fun =
511 if use_unification then
512 Unification, Founif.unification
514 Matching, Founif.matching
518 | Cic.Meta _ (*when not use_unification*) -> []
520 let leftc = get_candidates predicate table left in
521 find_all_matches ~unif_fun
522 metasenv context ugraph 0 left ty leftc
526 | Cic.Meta _ (*when not use_unification*) -> []
528 let rightc = get_candidates predicate table right in
529 find_all_matches ~unif_fun
530 metasenv context ugraph 0 right ty rightc
532 let rec ok_all what leftorright = function
534 | (_, subst, menv, ug, (pos,equation))::tl ->
535 let _, _, (_, l, r, o), m,_ = Equality.open_equality equation in
537 let other = if pos = Utils.Left then r else l in
538 let what' = Subst.apply_subst subst what in
539 let other' = Subst.apply_subst subst other in
540 let subst', menv', ug' =
541 unif_fun [] menv context what' other' ugraph
543 (match Subst.merge_subst_if_possible subst subst' with
544 | None -> ok_all what leftorright tl
546 (s, equation, leftorright <> pos )::(ok_all what leftorright tl))
548 | Founif.MatchingFailure
549 | CicUnification.UnificationFailure _ -> (ok_all what leftorright tl)
551 (ok_all right Utils.Left leftr)@(ok_all left Utils.Right rightr )
554 let subsumption_all x y z =
555 subsumption_aux_all false x y z
558 let unification_all x y z =
559 prerr_endline "unification_all"; subsumption_aux_all true x y z
562 let rec demodulation_aux bag ?from ?(typecheck=false)
563 metasenv context ugraph table lift_amount term =
564 let module C = Cic in
565 let module S = CicSubstitution in
566 let module M = CicMetaSubst in
567 let module HL = HelmLibraryObjects in
568 (* prerr_endline ("demodulating " ^ CicPp.ppterm term); *)
569 check_for_duplicates metasenv "in input a demodulation aux";
572 ~env:(metasenv,context,ugraph) (* Unification *) Matching table term
573 in let candidates = List.filter (fun _,x -> not (not_unit_eq context x)) candidates
583 CicTypeChecker.type_of_aux' metasenv context term ugraph
585 C.Implicit None, ugraph
587 find_matches bag metasenv context ugraph
588 lift_amount term termty candidates
590 prerr_endline "type checking error";
591 prerr_endline ("menv :\n" ^ CicMetaSubst.ppmetasenv [] metasenv);
592 prerr_endline ("term: " ^ (CicPp.ppterm term));
597 (if Utils.debug_res then
598 ignore(check_res res "demod1");
599 if check_demod_res res metasenv "demod" then res else None) in
609 (res, tl @ [S.lift 1 t])
612 demodulation_aux bag ~from:"1" metasenv context ugraph table ~typecheck
616 | None -> (None, tl @ [S.lift 1 t])
617 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
622 | Some (_, subst, menv, ug, eq_found) ->
623 Some (C.Appl ll, subst, menv, ug, eq_found)
626 | C.Prod (nn, s, t) ->
628 demodulation_aux bag ~from:"2"
629 metasenv context ugraph table lift_amount s in (
633 demodulation_aux bag metasenv
634 ((Some (nn, C.Decl s))::context) ugraph
635 table (lift_amount+1) t
639 | Some (t', subst, menv, ug, eq_found) ->
640 Some (C.Prod (nn, (S.lift 1 s), t'),
641 subst, menv, ug, eq_found)
643 | Some (s', subst, menv, ug, eq_found) ->
644 Some (C.Prod (nn, s', (S.lift 1 t)),
645 subst, menv, ug, eq_found)
647 | C.Lambda (nn, s, t) ->
648 prerr_endline "siam qui";
651 metasenv context ugraph table lift_amount s in (
655 demodulation_aux bag metasenv
656 ((Some (nn, C.Decl s))::context) ugraph
657 table (lift_amount+1) t
661 | Some (t', subst, menv, ug, eq_found) ->
662 Some (C.Lambda (nn, (S.lift 1 s), t'),
663 subst, menv, ug, eq_found)
665 | Some (s', subst, menv, ug, eq_found) ->
666 Some (C.Lambda (nn, s', (S.lift 1 t)),
667 subst, menv, ug, eq_found)
673 if Utils.debug_res then ignore(check_res res "demod_aux output");
679 (** demodulation, when target is an equality *)
680 let rec demodulation_equality bag ?from eq_uri newmeta env table target =
682 prerr_endline ("demodulation_eq:\n");
683 Index.iter table (fun l ->
684 let l = Index.PosEqSet.elements l in
686 List.map (fun (p,e) ->
687 Utils.string_of_pos p ^ Equality.string_of_equality e) l in
688 prerr_endline (String.concat "\n" l)
691 let module C = Cic in
692 let module S = CicSubstitution in
693 let module M = CicMetaSubst in
694 let module HL = HelmLibraryObjects in
695 let module U = Utils in
696 let metasenv, context, ugraph = env in
697 let w, proof, (eq_ty, left, right, order), metas, id =
698 Equality.open_equality target
700 (* first, we simplify *)
701 (* let right = U.guarded_simpl context right in *)
702 (* let left = U.guarded_simpl context left in *)
703 (* let order = !Utils.compare_terms left right in *)
704 (* let stat = (eq_ty, left, right, order) in *)
705 (* let w = Utils.compute_equality_weight stat in*)
706 (* let target = Equality.mk_equality (w, proof, stat, metas) in *)
707 if Utils.debug_metas then
708 ignore(check_target bag context target "demod equalities input");
709 let metasenv' = (* metasenv @ *) metas in
710 let maxmeta = ref newmeta in
712 let build_newtarget is_left (t, subst, menv, ug, eq_found) =
714 if Utils.debug_metas then
716 ignore(check_for_duplicates menv "input1");
717 ignore(check_disjoint_invariant subst menv "input2");
718 let substs = Subst.ppsubst subst in
719 ignore(check_target bag context (snd eq_found) ("input3" ^ substs))
721 let pos, equality = eq_found in
723 (ty, what, other, _), menv',id') = Equality.open_equality equality in
725 try fst (CicTypeChecker.type_of_aux' metasenv context what ugraph)
726 with CicUtil.Meta_not_found _ -> ty
728 let ty, eq_ty = apply_subst subst ty, apply_subst subst eq_ty in
729 let what, other = if pos = Utils.Left then what, other else other, what in
730 let newterm, newproof =
732 Utils.guarded_simpl context (apply_subst subst (S.subst other t)) in
733 (* let name = C.Name ("x_Demod" ^ (string_of_int !demod_counter)) in*)
734 let name = C.Name "x" in
736 let l, r = if is_left then t, S.lift 1 right else S.lift 1 left, t in
737 C.Appl [C.MutInd (eq_uri, 0, []); S.lift 1 eq_ty; l; r]
739 (bo, (Equality.Step (subst,(Equality.Demodulation, id,(pos,id'),
740 (Cic.Lambda (name, ty, bo'))))))
742 let newmenv = menv in
743 let left, right = if is_left then newterm, right else left, newterm in
744 let ordering = !Utils.compare_terms left right in
745 let stat = (eq_ty, left, right, ordering) in
747 let w = Utils.compute_equality_weight stat in
748 (Equality.mk_equality bag (w, newproof, stat,newmenv))
750 if Utils.debug_metas then
751 ignore(check_target bag context res "buildnew_target output");
756 demodulation_aux bag ~from:"from3" metasenv' context ugraph table 0 left
758 if Utils.debug_res then check_res res "demod result";
759 let newmeta, newtarget =
762 let newmeta, newtarget = build_newtarget true t in
763 (* assert (not (Equality.meta_convertibility_eq target newtarget)); *)
764 if (Equality.is_weak_identity newtarget) (* || *)
765 (*Equality.meta_convertibility_eq target newtarget*) then
768 demodulation_equality bag ?from eq_uri newmeta env table newtarget
770 let res = demodulation_aux bag metasenv' context ugraph table 0 right in
771 if Utils.debug_res then check_res res "demod result 1";
774 let newmeta, newtarget = build_newtarget false t in
775 if (Equality.is_weak_identity newtarget) ||
776 (Equality.meta_convertibility_eq target newtarget) then
779 demodulation_equality bag ?from eq_uri newmeta env table newtarget
783 (* newmeta, newtarget *)
788 Performs the beta expansion of the term "term" w.r.t. "table",
789 i.e. returns the list of all the terms t s.t. "(t term) = t2", for some t2
792 let rec betaexpand_term
793 ?(subterms_only=false) metasenv context ugraph table lift_amount term
795 let module C = Cic in
796 let module S = CicSubstitution in
797 let module M = CicMetaSubst in
798 let module HL = HelmLibraryObjects in
800 let res, lifted_term =
806 (fun arg (res, lifted_tl) ->
809 let arg_res, lifted_arg =
810 betaexpand_term metasenv context ugraph table
814 (fun (t, s, m, ug, eq_found) ->
815 (Some t)::lifted_tl, s, m, ug, eq_found)
820 (fun (l, s, m, ug, eq_found) ->
821 (Some lifted_arg)::l, s, m, ug, eq_found)
823 (Some lifted_arg)::lifted_tl)
826 (fun (r, s, m, ug, eq_found) ->
827 None::r, s, m, ug, eq_found) res,
833 (fun (l, s, m, ug, eq_found) ->
834 (C.Meta (i, l), s, m, ug, eq_found)) l'
836 e, C.Meta (i, lifted_l)
839 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
841 | C.Prod (nn, s, t) ->
843 betaexpand_term metasenv context ugraph table lift_amount s in
845 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
846 table (lift_amount+1) t in
849 (fun (t, s, m, ug, eq_found) ->
850 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
853 (fun (t, s, m, ug, eq_found) ->
854 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
855 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
857 | C.Lambda (nn, s, t) ->
859 betaexpand_term metasenv context ugraph table lift_amount s in
861 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
862 table (lift_amount+1) t in
865 (fun (t, s, m, ug, eq_found) ->
866 C.Lambda (nn, t, lifted_t), s, m, ug, eq_found) l1
869 (fun (t, s, m, ug, eq_found) ->
870 C.Lambda (nn, lifted_s, t), s, m, ug, eq_found) l2 in
871 l1' @ l2', C.Lambda (nn, lifted_s, lifted_t)
876 (fun (res, lifted_tl) arg ->
877 let arg_res, lifted_arg =
878 betaexpand_term metasenv context ugraph table lift_amount arg
882 (fun (a, s, m, ug, eq_found) ->
883 a::lifted_tl, s, m, ug, eq_found)
888 (fun (r, s, m, ug, eq_found) ->
889 lifted_arg::r, s, m, ug, eq_found)
891 lifted_arg::lifted_tl)
892 ) ([], []) (List.rev l)
895 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
898 | t -> [], (S.lift lift_amount t)
901 | C.Meta (i, l) -> res, lifted_term
904 (* C.Implicit None, ugraph *)
905 CicTypeChecker.type_of_aux' metasenv context term ugraph
907 let candidates = get_candidates Unification table term in
909 if subterms_only then
913 metasenv context ugraph lift_amount term termty candidates
920 returns a list of new clauses inferred with a right superposition step
921 between the positive equation "target" and one in the "table" "newmeta" is
922 the first free meta index, i.e. the first number above the highest meta
923 index: its updated value is also returned
925 let superposition_right bag
926 ?(subterms_only=false) eq_uri newmeta (metasenv, context, ugraph) table target=
927 let module C = Cic in
928 let module S = CicSubstitution in
929 let module M = CicMetaSubst in
930 let module HL = HelmLibraryObjects in
931 let module CR = CicReduction in
932 let module U = Utils in
933 let w, eqproof, (eq_ty, left, right, ordering), newmetas,id =
934 Equality.open_equality target
936 if Utils.debug_metas then
937 ignore (check_target bag context target "superpositionright");
938 let metasenv' = newmetas in
939 let maxmeta = ref newmeta in
943 fst (betaexpand_term ~subterms_only metasenv' context ugraph table 0 left), []
945 [], fst (betaexpand_term ~subterms_only metasenv' context ugraph table 0 right)
949 (fun (_, subst, _, _, _) ->
950 let subst = apply_subst subst in
951 let o = !Utils.compare_terms (subst l) (subst r) in
952 o <> U.Lt && o <> U.Le)
953 (fst (betaexpand_term ~subterms_only metasenv' context ugraph table 0 l))
955 (res left right), (res right left)
957 let build_new ordering (bo, s, m, ug, eq_found) =
958 if Utils.debug_metas then
959 ignore (check_target bag context (snd eq_found) "buildnew1" );
961 let pos, equality = eq_found in
962 let (_, proof', (ty, what, other, _), menv',id') =
963 Equality.open_equality equality in
964 let what, other = if pos = Utils.Left then what, other else other, what in
966 let ty, eq_ty = apply_subst s ty, apply_subst s eq_ty in
967 let newgoal, newproof =
970 Utils.guarded_simpl context (apply_subst s (S.subst other bo))
972 let name = C.Name "x" in
975 if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
976 C.Appl [C.MutInd (eq_uri, 0, []); S.lift 1 eq_ty; l; r]
980 (s,(Equality.SuperpositionRight,
981 id,(pos,id'),(Cic.Lambda(name,ty,bo''))))
983 let newmeta, newequality =
985 if ordering = U.Gt then newgoal, apply_subst s right
986 else apply_subst s left, newgoal in
987 let neworder = !Utils.compare_terms left right in
988 let newmenv = (* Founif.filter s *) m in
989 let stat = (eq_ty, left, right, neworder) in
991 let w = Utils.compute_equality_weight stat in
992 Equality.mk_equality bag (w, newproof, stat, newmenv) in
993 if Utils.debug_metas then
994 ignore (check_target bag context eq' "buildnew3");
995 let newm, eq' = Equality.fix_metas bag !maxmeta eq' in
996 if Utils.debug_metas then
997 ignore (check_target bag context eq' "buildnew4");
1001 if Utils.debug_metas then
1002 ignore(check_target bag context newequality "buildnew2");
1005 let new1 = List.map (build_new U.Gt) res1
1006 and new2 = List.map (build_new U.Lt) res2 in
1007 let ok e = not (Equality.is_identity (metasenv', context, ugraph) e) in
1009 (List.filter ok (new1 @ new2)))
1012 (** demodulation, when the target is a theorem *)
1013 let rec demodulation_theorem bag env table theorem =
1014 let module C = Cic in
1015 let module S = CicSubstitution in
1016 let module M = CicMetaSubst in
1017 let module HL = HelmLibraryObjects in
1019 match LibraryObjects.eq_URI() with
1021 | None -> assert false in
1022 let metasenv, context, ugraph = env in
1023 let proof, theo, metas = theorem in
1024 let build_newtheorem (t, subst, menv, ug, eq_found) =
1025 let pos, equality = eq_found in
1026 let (_, proof', (ty, what, other, _), menv',id) =
1027 Equality.open_equality equality in
1030 | Equality.Exact p -> p
1031 | _ -> assert false in
1033 if pos = Utils.Left then what, other else other, what in
1034 let newtheo = apply_subst subst (S.subst other t) in
1035 let name = C.Name "x" in
1036 let body = apply_subst subst t in
1037 let pred = C.Lambda(name,ty,body) in
1041 Equality.mk_eq_ind eq_uri ty what pred proof other peq
1043 Equality.mk_eq_ind eq_uri ty what pred proof other peq
1047 let res = demodulation_aux bag metas context ugraph table 0 theo in
1050 let newproof, newtheo = build_newtheorem t in
1051 if Equality.meta_convertibility theo newtheo then
1054 demodulation_theorem bag env table (newproof,newtheo,[])
1059 (*****************************************************************************)
1060 (** OPERATIONS ON GOALS **)
1062 (** DEMODULATION_GOAL & SUPERPOSITION_LEFT **)
1063 (*****************************************************************************)
1065 (* new: demodulation of non_equality terms *)
1066 let build_newg bag context goal rule expansion =
1067 let goalproof,_,_ = goal in
1068 let (t,subst,menv,ug,eq_found) = expansion in
1069 let pos, equality = eq_found in
1070 let (_, proof', (ty, what, other, _), menv',id) =
1071 Equality.open_equality equality in
1072 let what, other = if pos = Utils.Left then what, other else other, what in
1073 let newterm, newgoalproof =
1075 Utils.guarded_simpl context
1076 (apply_subst subst (CicSubstitution.subst other t))
1078 let bo' = apply_subst subst t in
1079 let ty = apply_subst subst ty in
1080 let name = Cic.Name "x" in
1081 let newgoalproofstep = (rule,pos,id,subst,Cic.Lambda (name,ty,bo')) in
1082 bo, (newgoalproofstep::goalproof)
1084 let newmetasenv = (* Founif.filter subst *) menv in
1085 (newgoalproof, newmetasenv, newterm)
1088 let rec demod bag env table goal =
1089 let goalproof,menv,t = goal in
1090 let _, context, ugraph = env in
1091 let res = demodulation_aux bag menv context ugraph table 0 t (~typecheck:true)in
1095 build_newg bag context goal Equality.Demodulation newt
1097 let _,_,newt = newg in
1098 if Equality.meta_convertibility t newt then
1101 true, snd (demod bag env table newg)
1108 | (proof,menv,Cic.Appl[(Cic.MutInd(uri,0,_)) as eq;ty;l;r]) ->
1109 (* assert (LibraryObjects.is_eq_URI uri); *)
1110 proof,menv,eq,ty,l,r
1113 let ty_of_goal (_,_,ty) = ty ;;
1115 (* checks if two goals are metaconvertible *)
1116 let goal_metaconvertibility_eq g1 g2 =
1117 Equality.meta_convertibility (ty_of_goal g1) (ty_of_goal g2)
1120 (* when the betaexpand_term function is called on the left/right side of the
1121 * goal, the predicate has to be fixed
1122 * C[x] ---> (eq ty unchanged C[x])
1123 * [posu] is the side of the [unchanged] term in the original goal
1126 let fix_expansion goal posu (t, subst, menv, ug, eq_f) =
1127 let _,_,eq,ty,l,r = open_goal goal in
1128 let unchanged = if posu = Utils.Left then l else r in
1129 let unchanged = CicSubstitution.lift 1 unchanged in
1130 let ty = CicSubstitution.lift 1 ty in
1133 | Utils.Left -> Cic.Appl [eq;ty;unchanged;t]
1134 | Utils.Right -> Cic.Appl [eq;ty;t;unchanged]
1136 (pred, subst, menv, ug, eq_f)
1139 (* ginve the old [goal], the side that has not changed [posu] and the
1140 * expansion builds a new goal *)
1141 let build_newgoal bag context goal posu rule expansion =
1142 let goalproof,_,_,_,_,_ = open_goal goal in
1143 let (t,subst,menv,ug,eq_found) = fix_expansion goal posu expansion in
1144 let pos, equality = eq_found in
1145 let (_, proof', (ty, what, other, _), menv',id) =
1146 Equality.open_equality equality in
1147 let what, other = if pos = Utils.Left then what, other else other, what in
1148 let newterm, newgoalproof =
1150 Utils.guarded_simpl context
1151 (apply_subst subst (CicSubstitution.subst other t))
1153 let bo' = apply_subst subst t in
1154 let ty = apply_subst subst ty in
1155 let name = Cic.Name "x" in
1156 let newgoalproofstep = (rule,pos,id,subst,Cic.Lambda (name,ty,bo')) in
1157 bo, (newgoalproofstep::goalproof)
1159 let newmetasenv = (* Founif.filter subst *) menv in
1160 (newgoalproof, newmetasenv, newterm)
1165 returns a list of new clauses inferred with a left superposition step
1166 the negative equation "target" and one of the positive equations in "table"
1168 let superposition_left bag (metasenv, context, ugraph) table goal maxmeta =
1169 let names = Utils.names_of_context context in
1170 let proof,menv,eq,ty,l,r = open_goal goal in
1171 let c = !Utils.compare_terms l r in
1173 if c = Utils.Incomparable then
1175 let expansionsl, _ = betaexpand_term menv context ugraph table 0 l in
1176 let expansionsr, _ = betaexpand_term menv context ugraph table 0 r in
1177 (* prerr_endline "incomparable";
1178 prerr_endline (string_of_int (List.length expansionsl));
1179 prerr_endline (string_of_int (List.length expansionsr));
1181 List.map (build_newgoal bag context goal Utils.Right Equality.SuperpositionLeft) expansionsl
1183 List.map (build_newgoal bag context goal Utils.Left Equality.SuperpositionLeft) expansionsr
1187 | Utils.Gt -> (* prerr_endline "GT"; *)
1188 let big,small,possmall = l,r,Utils.Right in
1189 let expansions, _ = betaexpand_term menv context ugraph table 0 big in
1191 (build_newgoal bag context goal possmall Equality.SuperpositionLeft)
1193 | Utils.Lt -> (* prerr_endline "LT"; *)
1194 let big,small,possmall = r,l,Utils.Left in
1195 let expansions, _ = betaexpand_term menv context ugraph table 0 big in
1197 (build_newgoal bag context goal possmall Equality.SuperpositionLeft)
1202 ("NOT GT, LT NOR EQ : "^CicPp.pp l names^" - "^CicPp.pp r names);
1205 (* rinfresco le meta *)
1208 let max,g = Equality.fix_metas_goal max g in max,g::acc)
1209 newgoals (maxmeta,[])
1212 (** demodulation, when the target is a goal *)
1213 let rec demodulation_goal bag env table goal =
1214 let goalproof,menv,_,_,left,right = open_goal goal in
1215 let _, context, ugraph = env in
1216 (* let term = Utils.guarded_simpl (~debug:true) context term in*)
1218 let resright = demodulation_aux bag menv context ugraph table 0 right in
1222 build_newgoal bag context goal Utils.Left Equality.Demodulation t
1224 if goal_metaconvertibility_eq goal newg then
1227 true, snd (demodulation_goal bag env table newg)
1228 | None -> false, goal
1230 let resleft = demodulation_aux bag menv context ugraph table 0 left in
1233 let newg = build_newgoal bag context goal Utils.Right Equality.Demodulation t in
1234 if goal_metaconvertibility_eq goal newg then
1237 true, snd (demodulation_goal bag env table newg)
1238 | None -> do_right ()
1242 type solved = Yes of Equality.goal | No of Equality.goal list
1244 (* returns all the 1 step demodulations *)
1246 module S = CicSubstitution;;
1247 let rec demodulation_all_aux
1248 metasenv context ugraph table lift_amount term
1251 get_candidates ~env:(metasenv,context,ugraph) Matching table term
1256 let termty, ugraph = C.Implicit None, ugraph in
1259 metasenv context ugraph lift_amount term termty candidates
1268 (fun (rel, s, m, ug, c) ->
1269 (Cic.Appl (l@[rel]@List.tl r), s, m, ug, c))
1270 (demodulation_all_aux
1271 metasenv context ugraph table lift_amount t),
1272 l@[List.hd r], List.tl r)
1273 (res, [], List.map (S.lift 1) l) l
1277 | C.Lambda (nn, s, t) ->
1278 let context = (Some (nn, C.Decl s))::context in
1281 | Cic.Prod _ -> Cic.Prod (nn,s,t) | _ -> Cic.Lambda (nn,s,t)
1285 (fun (rel, subst, m, ug, c) ->
1286 mk (S.lift 1 s) rel, subst, m, ug, c)
1287 (demodulation_all_aux
1288 metasenv context ugraph table (lift_amount+1) t)
1289 (* we could demodulate also in s, but then t may be badly
1294 let solve_demodulating bag env table initgoal steps =
1295 let _, context, ugraph = env in
1296 let solved goal res side =
1297 let newg = build_newgoal bag context goal side Equality.Demodulation res in
1299 | (goalproof,m,Cic.Appl[Cic.MutInd(uri,n,ens);eq_ty;left;right])
1300 when LibraryObjects.is_eq_URI uri ->
1303 Founif.unification m m context left right CicUniv.empty_ugraph
1306 with CicUnification.UnificationFailure _ -> No [newg])
1309 let solved goal res_list side =
1310 let newg = List.map (fun x -> solved goal x side) res_list in
1312 List.find (function Yes _ -> true | _ -> false) newg
1314 No (List.flatten (List.map (function No s -> s | _-> assert false) newg))
1321 | None -> first f tl
1322 | Some x as ok -> ok
1324 let rec aux steps next goal =
1325 if steps = 0 then None else
1326 let goalproof,menv,_,_,left,right = open_goal goal in
1328 demodulation_all_aux menv context ugraph table 0 t
1332 (match do_step left with
1334 (match solved goal res Utils.Right with
1336 (match first (aux (steps - 1) L) newgoals with
1337 | Some g as success -> success
1338 | None -> aux steps R goal)
1339 | Yes newgoal -> Some newgoal)
1340 | [] -> aux steps R goal)
1342 (match do_step right with
1344 (match solved goal res Utils.Left with
1346 (match first (aux (steps - 1) L) newgoals with
1347 | Some g as success -> success
1349 | Yes newgoal -> Some newgoal)
1352 aux steps L initgoal
1355 let get_stats () = "" ;;