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/.
30 let metas_of_proof_time = ref 0.;;
31 let metas_of_term_time = ref 0.;;
36 (Cic.term * (* type *)
37 Cic.term * (* left side *)
38 Cic.term * (* right side *)
39 Utils.comparison) * (* ordering *)
40 Cic.metasenv * (* environment for metas *)
41 Cic.term list (* arguments *)
44 | NoProof (* term is the goal missing a proof *)
45 | BasicProof of Cic.term
47 Cic.substitution * UriManager.uri *
48 (Cic.name * Cic.term) * Cic.term * (Utils.pos * equality) * proof
49 | ProofGoalBlock of proof * proof
50 | ProofSymBlock of Cic.term list * proof
51 | SubProof of Cic.term * int * proof
54 let string_of_equality ?env =
58 | w, _, (ty, left, right, o), _, _ ->
59 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
60 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
62 | Some (_, context, _) -> (
63 let names = names_of_context context in
65 | w, _, (ty, left, right, o), _, _ ->
66 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
67 (CicPp.pp left names) (string_of_comparison o)
68 (CicPp.pp right names)
73 let rec string_of_proof = function
74 | NoProof -> "NoProof "
75 | BasicProof t -> "BasicProof " ^ (CicPp.ppterm t)
76 | SubProof (t, i, p) ->
77 Printf.sprintf "SubProof(%s, %s, %s)"
78 (CicPp.ppterm t) (string_of_int i) (string_of_proof p)
79 | ProofSymBlock _ -> "ProofSymBlock"
80 | ProofBlock (subst, _, _, _ ,_,_) ->
81 "ProofBlock" ^ (CicMetaSubst.ppsubst subst)
82 | ProofGoalBlock (p1, p2) ->
83 Printf.sprintf "ProofGoalBlock(%s, %s)"
84 (string_of_proof p1) (string_of_proof p2)
88 let check_disjoint_invariant subst metasenv msg =
90 (fun (i,_,_) -> (List.exists (fun (j,_) -> i=j) subst)) metasenv)
93 prerr_endline ("not disjoint: " ^ msg);
98 (* filter out from metasenv the variables in substs *)
99 let filter subst metasenv =
102 try let _ = List.find (fun (i, _) -> m = i) subst in false
103 with Not_found -> true)
107 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
108 let build_ens_for_sym_eq sym_eq_URI termlist =
109 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph sym_eq_URI in
111 | Cic.Constant (_, _, _, uris, _) ->
112 assert (List.length uris <= List.length termlist);
113 let rec aux = function
115 | (uri::uris), (term::tl) ->
116 let ens, args = aux (uris, tl) in
117 (uri, term)::ens, args
118 | _, _ -> assert false
125 let build_proof_term ?(noproof=Cic.Implicit None) proof =
126 let rec do_build_proof proof =
129 Printf.fprintf stderr "WARNING: no proof!\n";
131 | BasicProof term -> term
132 | ProofGoalBlock (proofbit, proof) ->
133 print_endline "found ProofGoalBlock, going up...";
134 do_build_goal_proof proofbit proof
135 | ProofSymBlock (termlist, proof) ->
136 let proof = do_build_proof proof in
137 let ens, args = build_ens_for_sym_eq (Utils.sym_eq_URI ()) termlist in
138 Cic.Appl ([Cic.Const (Utils.sym_eq_URI (), ens)] @ args @ [proof])
139 | ProofBlock (subst, eq_URI, (name, ty), bo, (pos, eq), eqproof) ->
140 let t' = Cic.Lambda (name, ty, bo) in
142 let _, proof', _, _, _ = eq in
143 do_build_proof proof'
145 let eqproof = do_build_proof eqproof in
146 let _, _, (ty, what, other, _), menv', args' = eq in
148 if pos = Utils.Left then what, other else other, what
150 CicMetaSubst.apply_subst subst
151 (Cic.Appl [Cic.Const (eq_URI, []); ty;
152 what; t'; eqproof; other; proof'])
153 | SubProof (term, meta_index, proof) ->
154 let proof = do_build_proof proof in
156 | Cic.Meta (j, _) -> i = j
159 ProofEngineReduction.replace
160 ~equality:eq ~what:[meta_index] ~with_what:[proof] ~where:term
162 and do_build_goal_proof proofbit proof =
164 | ProofGoalBlock (pb, p) ->
165 do_build_proof (ProofGoalBlock (replace_proof proofbit pb, p))
166 | _ -> do_build_proof (replace_proof proofbit proof)
168 and replace_proof newproof = function
169 | ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof) ->
170 let eqproof' = replace_proof newproof eqproof in
171 ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof')
172 | ProofGoalBlock (pb, p) ->
173 let pb' = replace_proof newproof pb in
174 ProofGoalBlock (pb', p)
175 | BasicProof _ -> newproof
176 | SubProof (term, meta_index, p) ->
177 SubProof (term, meta_index, replace_proof newproof p)
184 let rec metas_of_term = function
185 | Cic.Meta (i, c) -> [i]
188 | Cic.MutInd (_, _, ens)
189 | Cic.MutConstruct (_, _, _, ens) ->
190 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
193 | Cic.Lambda (_, s, t)
194 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
195 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
196 | Cic.MutCase (uri, i, s, t, l) ->
197 (metas_of_term s) @ (metas_of_term t) @
198 (List.flatten (List.map metas_of_term l))
201 (List.map (fun (s, i, t1, t2) ->
202 (metas_of_term t1) @ (metas_of_term t2)) il)
203 | Cic.CoFix (i, il) ->
205 (List.map (fun (s, t1, t2) ->
206 (metas_of_term t1) @ (metas_of_term t2)) il)
210 let rec metas_of_proof p =
212 let t1 = Unix.gettimeofday () in
213 let res = metas_of_term (build_proof_term p) in
214 let t2 = Unix.gettimeofday () in
215 metas_of_proof_time := !metas_of_proof_time +. (t2 -. t1);
218 metas_of_term (build_proof_term p)
221 exception NotMetaConvertible;;
223 let meta_convertibility_aux table t1 t2 =
224 let module C = Cic in
225 let rec aux ((table_l, table_r) as table) t1 t2 =
227 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
228 let m1_binding, table_l =
229 try List.assoc m1 table_l, table_l
230 with Not_found -> m2, (m1, m2)::table_l
231 and m2_binding, table_r =
232 try List.assoc m2 table_r, table_r
233 with Not_found -> m1, (m2, m1)::table_r
235 if (m1_binding <> m2) || (m2_binding <> m1) then
236 raise NotMetaConvertible
242 | None, Some _ | Some _, None -> raise NotMetaConvertible
244 | Some t1, Some t2 -> (aux res t1 t2))
245 (table_l, table_r) tl1 tl2
246 with Invalid_argument _ ->
247 raise NotMetaConvertible
249 | C.Var (u1, ens1), C.Var (u2, ens2)
250 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
251 aux_ens table ens1 ens2
252 | C.Cast (s1, t1), C.Cast (s2, t2)
253 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
254 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
255 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
256 let table = aux table s1 s2 in
258 | C.Appl l1, C.Appl l2 -> (
259 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
260 with Invalid_argument _ -> raise NotMetaConvertible
262 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
263 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
264 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
265 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
266 aux_ens table ens1 ens2
267 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
268 when (UriManager.eq u1 u2) && i1 = i2 ->
269 let table = aux table s1 s2 in
270 let table = aux table t1 t2 in (
271 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
272 with Invalid_argument _ -> raise NotMetaConvertible
274 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
277 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
278 if i1 <> i2 then raise NotMetaConvertible
280 let res = (aux res s1 s2) in aux res t1 t2)
282 with Invalid_argument _ -> raise NotMetaConvertible
284 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
287 (fun res (n1, s1, t1) (n2, s2, t2) ->
288 let res = aux res s1 s2 in aux res t1 t2)
290 with Invalid_argument _ -> raise NotMetaConvertible
292 | t1, t2 when t1 = t2 -> table
293 | _, _ -> raise NotMetaConvertible
295 and aux_ens table ens1 ens2 =
296 let cmp (u1, t1) (u2, t2) =
297 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
299 let ens1 = List.sort cmp ens1
300 and ens2 = List.sort cmp ens2 in
303 (fun res (u1, t1) (u2, t2) ->
304 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
307 with Invalid_argument _ -> raise NotMetaConvertible
313 let meta_convertibility_eq eq1 eq2 =
314 let _, _, (ty, left, right, _), _, _ = eq1
315 and _, _, (ty', left', right', _), _, _ = eq2 in
318 else if (left = left') && (right = right') then
320 else if (left = right') && (right = left') then
324 let table = meta_convertibility_aux ([], []) left left' in
325 let _ = meta_convertibility_aux table right right' in
327 with NotMetaConvertible ->
329 let table = meta_convertibility_aux ([], []) left right' in
330 let _ = meta_convertibility_aux table right left' in
332 with NotMetaConvertible ->
337 let meta_convertibility t1 t2 =
342 ignore(meta_convertibility_aux ([], []) t1 t2);
344 with NotMetaConvertible ->
349 let rec check_irl start = function
351 | None::tl -> check_irl (start+1) tl
352 | (Some (Cic.Rel x))::tl ->
353 if x = start then check_irl (start+1) tl else false
358 let rec is_simple_term = function
359 | Cic.Appl ((Cic.Meta _)::_) -> false
360 | Cic.Appl l -> List.for_all is_simple_term l
361 | Cic.Meta (i, l) -> check_irl 1 l
363 | Cic.Const _ -> true
364 | Cic.MutInd (_, _, []) -> true
365 | Cic.MutConstruct (_, _, _, []) -> true
370 let lookup_subst meta subst =
372 | Cic.Meta (i, _) -> (
373 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
374 with Not_found -> meta
380 let unification_simple metasenv context t1 t2 ugraph =
381 let module C = Cic in
382 let module M = CicMetaSubst in
383 let module U = CicUnification in
384 let lookup = lookup_subst in
385 let rec occurs_check subst what where =
387 | t when what = t -> true
388 | C.Appl l -> List.exists (occurs_check subst what) l
390 let t = lookup where subst in
391 if t <> where then occurs_check subst what t else false
394 let rec unif subst menv s t =
395 let s = match s with C.Meta _ -> lookup s subst | _ -> s
396 and t = match t with C.Meta _ -> lookup t subst | _ -> t
399 | s, t when s = t -> subst, menv
400 | C.Meta (i, _), C.Meta (j, _) when i > j ->
402 | C.Meta _, t when occurs_check subst s t ->
404 (U.UnificationFailure (lazy "Inference.unification.unif"))
405 | C.Meta (i, l), t -> (
407 let _, _, ty = CicUtil.lookup_meta i menv in
408 assert (not (List.mem_assoc i subst));
409 let subst = (i, (context, t, ty))::subst in
410 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
412 with CicUtil.Meta_not_found m ->
413 let names = names_of_context context in
416 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
417 (CicPp.pp t1 names) (CicPp.pp t2 names)
418 (print_metasenv menv) (print_metasenv metasenv)));
421 | _, C.Meta _ -> unif subst menv t s
422 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
423 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
424 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
427 (fun (subst', menv) s t -> unif subst' menv s t)
428 (subst, menv) tls tlt
429 with Invalid_argument _ ->
430 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
433 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
435 let subst, menv = unif [] metasenv t1 t2 in
436 let menv = filter subst menv in
437 List.rev subst, menv, ugraph
441 let unification metasenv1 metasenv2 context t1 t2 ugraph =
442 let metasenv = metasenv1 metasenv2 in
443 let subst, menv, ug =
444 if not (is_simple_term t1) || not (is_simple_term t2) then (
447 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
448 (CicPp.ppterm t1) (CicPp.ppterm t2)));
449 CicUnification.fo_unif metasenv context t1 t2 ugraph
451 unification_simple metasenv context t1 t2 ugraph
453 if Utils.debug_res then
454 ignore(check_disjoint_invariant subst menv "unif");
457 (fun (i, (context, term, ty)) ->
458 let context = CicMetaSubst.apply_subst_context subst context in
459 let term = CicMetaSubst.apply_subst subst term in
460 let ty = CicMetaSubst.apply_subst subst ty in
461 (i, (context, term, ty))) subst in
463 let rec fix_term = function
464 | (Cic.Meta (i, l) as t) ->
465 let t' = lookup_subst t subst in
466 if t <> t' then fix_term t' else t
467 | Cic.Appl l -> Cic.Appl (List.map fix_term l)
470 let rec fix_subst = function
472 | (i, (c, t, ty))::tl -> (i, (c, fix_term t, fix_term ty))::(fix_subst tl)
474 fix_subst subst, menv, ug *)
479 let unification metasenv1 metasenv2 context t1 t2 ugraph =
480 let (subst, metasenv, ugraph) =
481 CicUnification.fo_unif (metasenv1@metasenv2) context t1 t2 ugraph in
482 if Utils.debug_res then
483 ignore(check_disjoint_invariant subst metasenv "fo_unif");
484 (subst, metasenv, ugraph)
488 exception MatchingFailure;;
492 let matching_simple metasenv context t1 t2 ugraph =
493 let module C = Cic in
494 let module M = CicMetaSubst in
495 let module U = CicUnification in
496 let lookup meta subst =
499 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
500 with Not_found -> meta
504 let rec do_match subst menv s t =
506 | s, t when s = t -> subst, menv
507 | s, C.Meta (i, l) ->
508 let filter_menv i menv =
509 List.filter (fun (m, _, _) -> i <> m) menv
512 let value = lookup t subst in
514 | value when value = t ->
515 let _, _, ty = CicUtil.lookup_meta i menv in
516 (i, (context, s, ty))::subst, filter_menv i menv
517 | value when value <> s ->
518 raise MatchingFailure
519 | value -> do_match subst menv s value
522 | C.Appl ls, C.Appl lt -> (
525 (fun (subst, menv) s t -> do_match subst menv s t)
527 with Invalid_argument _ ->
528 raise MatchingFailure
531 raise MatchingFailure
533 let subst, menv = do_match [] metasenv t1 t2 in
539 let matching metasenv context t1 t2 ugraph =
541 let subst, metasenv, ugraph =
543 unification metasenv context t1 t2 ugraph
544 with CicUtil.Meta_not_found _ as exn ->
545 Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
546 (CicPp.ppterm t1) (CicPp.ppterm t2)
547 (CicMetaSubst.ppmetasenv [] metasenv);
550 if Utils.debug_res then
551 ignore(check_disjoint_invariant subst metasenv "qua-2");
552 let t' = CicMetaSubst.apply_subst subst t1 in
553 if not (meta_convertibility t1 t') then
554 raise MatchingFailure
556 if Utils.debug_res then
557 ignore(check_disjoint_invariant subst metasenv "qua-1");
558 let metas = metas_of_term t1 in
561 (fun (i, (context, term, ty)) ->
562 let context = CicMetaSubst.apply_subst_context subst context in
563 let term = CicMetaSubst.apply_subst subst term in
564 let ty = CicMetaSubst.apply_subst subst ty in
565 (i, (context, term, ty))) subst in
566 if Utils.debug_res then
567 ignore(check_disjoint_invariant subst metasenv "qua0");
569 let subst, metasenv =
572 (subst,metasenv) s ->
574 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
576 List.filter (fun (x, _, _) -> x<>j) metasenv
578 ((j, (c, Cic.Meta (i, lc), ty))::subst,
580 |_ -> s::subst,metasenv) ([],metasenv) subst
582 if Utils.debug_res then
583 ignore(check_disjoint_invariant subst metasenv "qua1");
585 let fix_subst = function
586 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
587 (j, (c, Cic.Meta (i, lc), ty))
590 let subst = List.map fix_subst subst in *)
591 if CicMetaSubst.apply_subst subst t1 = t1 then
592 subst, metasenv, ugraph
594 (prerr_endline "mah"; raise MatchingFailure)
596 | CicUnification.UnificationFailure _
597 | CicUnification.Uncertain _ ->
598 raise MatchingFailure
602 (** matching takes in input the _disjoint_ metasenv of t1 and t2;
603 it perform unification in the union metasenv, then check that
604 the first metasenv has not changed *)
606 let matching metasenv1 metasenv2 context t1 t2 ugraph =
607 let subst, metasenv, ugraph =
609 unification metasenv1 metasenv2 context t1 t2 ugraph
611 CicUtil.Meta_not_found _ as exn ->
612 Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
613 (CicPp.ppterm t1) (CicPp.ppterm t2)
614 (CicMetaSubst.ppmetasenv [] (metasenv1@metasenv2));
616 | CicUnification.UnificationFailure _
617 | CicUnification.Uncertain _ ->
618 raise MatchingFailure
620 if Utils.debug_res then
621 ignore(check_disjoint_invariant subst metasenv "qua-2");
622 (* let us unfold subst *)
623 if metasenv = metasenv1 then
624 subst, metasenv, ugraph (* everything is fine *)
626 (* let us unfold subst *)
629 (fun (i, (context, term, ty)) ->
630 let context = CicMetaSubst.apply_subst_context subst context in
631 let term = CicMetaSubst.apply_subst subst term in
632 let ty = CicMetaSubst.apply_subst subst ty in
633 (i, (context, term, ty))) subst in
634 (* let us revert Meta-Meta in subst privileging metasenv1 *)
635 let subst, metasenv =
638 (subst,metasenv) s ->
640 | (i, (c, Cic.Meta (j, lc), ty))
641 when (List.exists (fun (x, _, _) -> x=i) metasenv1) &&
642 not (List.exists (fun (x, _) -> x=j) subst) ->
644 List.filter (fun (x, _, _) -> x<>j) metasenv
646 ((j, (c, Cic.Meta (i, lc), ty))::subst,
648 |_ -> s::subst,metasenv) ([],metasenv) subst
650 (* finally, let us chek again that metasenv = metasenv1 *)
651 if metasenv = metasenv1 then
652 subst, metasenv, ugraph
653 else raise MatchingFailure
656 let check_eq context msg eq =
657 let w, proof, (eq_ty, left, right, order), metas, args = eq in
658 if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
659 (fst (CicTypeChecker.type_of_aux' metas context left CicUniv.empty_ugraph))
660 CicUniv.empty_ugraph))
669 let find_equalities context proof =
670 let module C = Cic in
671 let module S = CicSubstitution in
672 let module T = CicTypeChecker in
673 let eq_uri = LibraryObjects.eq_URI () in
674 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
675 let ok_types ty menv =
676 List.for_all (fun (_, _, mt) -> mt = ty) menv
678 let rec aux index newmeta = function
680 | (Some (_, C.Decl (term)))::tl ->
681 let do_find context term =
683 | C.Prod (name, s, t) ->
684 let (head, newmetas, args, newmeta) =
685 ProofEngineHelpers.saturate_term newmeta []
686 context (S.lift index term) 0
689 if List.length args = 0 then
692 C.Appl ((C.Rel index)::args)
695 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
696 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
699 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
700 let o = !Utils.compare_terms t1 t2 in
701 let stat = (ty,t1,t2,o) in
702 let w = compute_equality_weight stat in
703 let proof = BasicProof p in
704 let e = (w, proof, stat, newmetas, args) in
708 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
709 when UriManager.eq uri eq_uri ->
710 let ty = S.lift index ty in
711 let t1 = S.lift index t1 in
712 let t2 = S.lift index t2 in
713 let o = !Utils.compare_terms t1 t2 in
714 let stat = (ty,t1,t2,o) in
715 let w = compute_equality_weight stat in
716 let e = (w, BasicProof (C.Rel index), stat, [], []) in
720 match do_find context term with
722 let tl, newmeta' = (aux (index+1) newmeta tl) in
723 if newmeta' < newmeta then
724 prerr_endline "big trouble";
725 (index, p)::tl, newmeta' (* max???? *)
727 aux (index+1) newmeta tl
730 aux (index+1) newmeta tl
732 let il, maxm = aux 1 newmeta context in
733 let indexes, equalities = List.split il in
734 ignore (List.iter (check_eq context "find") equalities);
735 indexes, equalities, maxm
740 let equations_blacklist =
742 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
743 UriManager.UriSet.empty [
744 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
745 "cic:/Coq/Init/Logic/trans_eq.con";
746 "cic:/Coq/Init/Logic/f_equal.con";
747 "cic:/Coq/Init/Logic/f_equal2.con";
748 "cic:/Coq/Init/Logic/f_equal3.con";
749 "cic:/Coq/Init/Logic/f_equal4.con";
750 "cic:/Coq/Init/Logic/f_equal5.con";
751 "cic:/Coq/Init/Logic/sym_eq.con";
752 "cic:/Coq/Init/Logic/eq_ind.con";
753 "cic:/Coq/Init/Logic/eq_ind_r.con";
754 "cic:/Coq/Init/Logic/eq_rec.con";
755 "cic:/Coq/Init/Logic/eq_rec_r.con";
756 "cic:/Coq/Init/Logic/eq_rect.con";
757 "cic:/Coq/Init/Logic/eq_rect_r.con";
758 "cic:/Coq/Logic/Eqdep/UIP.con";
759 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
760 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
761 "cic:/Coq/ZArith/Zcompare/rename.con";
762 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
763 perche' questo cacchio di teorema rompe le scatole :'( *)
764 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
766 "cic:/matita/logic/equality/eq_f.con";
767 "cic:/matita/logic/equality/eq_f2.con";
768 "cic:/matita/logic/equality/eq_rec.con";
769 "cic:/matita/logic/equality/eq_rect.con";
773 let equations_blacklist = UriManager.UriSet.empty;;
776 let find_library_equalities dbd context status maxmeta =
777 let module C = Cic in
778 let module S = CicSubstitution in
779 let module T = CicTypeChecker in
782 (fun s u -> UriManager.UriSet.add u s)
784 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
790 if UriManager.UriSet.mem uri blacklist then
793 let t = CicUtil.term_of_uri uri in
795 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
799 (let t1 = Unix.gettimeofday () in
800 let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
801 let t2 = Unix.gettimeofday () in
804 (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
808 let eq_uri1 = eq_XURI ()
809 and eq_uri2 = LibraryObjects.eq_URI () in
811 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
813 let ok_types ty menv =
814 List.for_all (fun (_, _, mt) -> mt = ty) menv
816 let rec has_vars = function
817 | C.Meta _ | C.Rel _ | C.Const _ -> false
819 | C.Appl l -> List.exists has_vars l
820 | C.Prod (_, s, t) | C.Lambda (_, s, t)
821 | C.LetIn (_, s, t) | C.Cast (s, t) ->
822 (has_vars s) || (has_vars t)
825 let rec aux newmeta = function
827 | (uri, term, termty)::tl ->
830 (Printf.sprintf "Examining: %s (%s)"
831 (CicPp.ppterm term) (CicPp.ppterm termty)));
834 | C.Prod (name, s, t) when not (has_vars termty) ->
835 let head, newmetas, args, newmeta =
836 ProofEngineHelpers.saturate_term newmeta [] context termty 0
839 if List.length args = 0 then
845 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
846 when (iseq uri) && (ok_types ty newmetas) ->
849 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
850 let o = !Utils.compare_terms t1 t2 in
851 let stat = (ty,t1,t2,o) in
852 let w = compute_equality_weight stat in
853 let proof = BasicProof p in
854 let e = (w, proof, stat, newmetas, args) in
858 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
859 when iseq uri && not (has_vars termty) ->
860 let o = !Utils.compare_terms t1 t2 in
861 let stat = (ty,t1,t2,o) in
862 let w = compute_equality_weight stat in
863 let e = (w, BasicProof term, stat, [], []) in
869 let tl, newmeta' = aux newmeta tl in
870 if newmeta' < newmeta then
871 prerr_endline "big trouble";
872 (uri, e)::tl, newmeta' (* max???? *)
876 let found, maxm = aux maxmeta candidates in
879 (fun (s, l) (u, e) ->
880 if List.exists (meta_convertibility_eq e) (List.map snd l) then (
883 (Printf.sprintf "NO!! %s already there!"
884 (string_of_equality e)));
885 (UriManager.UriSet.add u s, l)
886 ) else (UriManager.UriSet.add u s, (u, e)::l))
887 (UriManager.UriSet.empty, []) found)
893 let find_library_theorems dbd env status equalities_uris =
894 let module C = Cic in
895 let module S = CicSubstitution in
896 let module T = CicTypeChecker in
899 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
901 UriManager.UriSet.remove refl_equal
902 (UriManager.UriSet.union equalities_uris equations_blacklist)
905 (fun s u -> UriManager.UriSet.add u s)
906 s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
909 let metasenv, context, ugraph = env in
913 if UriManager.UriSet.mem uri blacklist then l
915 let t = CicUtil.term_of_uri uri in
916 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
918 [] (MetadataQuery.signature_of_goal ~dbd status)
921 let u = eq_XURI () in
922 let t = CicUtil.term_of_uri u in
923 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
926 refl_equal::candidates
930 let find_context_hypotheses env equalities_indexes =
931 let metasenv, context, ugraph = env in
938 if List.mem n equalities_indexes then
943 CicTypeChecker.type_of_aux' metasenv context t ugraph in
944 (n+1, (t, ty, [])::l))
951 let fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) =
952 let table = Hashtbl.create (List.length args) in
954 let newargs, newmeta =
956 (fun t (newargs, index) ->
959 if Hashtbl.mem table i then
960 let idx = Hashtbl.find table i in
961 ((Cic.Meta (idx, l))::newargs, index+1)
963 let _ = Hashtbl.add table i index in
964 ((Cic.Meta (index, l))::newargs, index+1)
970 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
975 (fun (i, context, term) menv ->
977 let index = Hashtbl.find table i in
978 (index, context, term)::menv
980 (i, context, term)::menv)
985 and right = repl right in
987 (metas_of_term left) @
988 (metas_of_term right) @
989 (metas_of_term ty) @ (metas_of_proof p) in
990 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
993 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
996 if List.length metas > 0 then
997 let first = List.hd metas in
998 (* this new equality might have less variables than its parents: here
999 we fill the gap with a dummy arg. Example:
1000 with (f X Y) = X we can simplify
1003 So the new equation has only one variable, but it still has type like
1004 \lambda X,Y:..., so we need to pass a dummy arg for Y
1005 (I hope this makes some sense...)
1010 (function Cic.Meta (i, _) -> i = v | _ -> assert false)
1012 Hashtbl.replace table k first)
1013 (Hashtbl.copy table)
1015 let rec fix_proof = function
1016 | NoProof -> NoProof
1017 | BasicProof term -> BasicProof (repl term)
1018 | ProofBlock (subst, eq_URI, namety, bo, (pos, eq), p) ->
1023 | Cic.Meta (i, l) -> (
1025 let j = Hashtbl.find table i in
1026 if List.mem_assoc i subst then
1029 let _, context, ty = CicUtil.lookup_meta i menv in
1030 (i, (context, Cic.Meta (j, l), ty))::s
1031 with Not_found | CicUtil.Meta_not_found _ ->
1034 | _ -> assert false)
1037 ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1040 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1045 let relocate newmeta menv =
1046 let subst, metasenv, newmeta =
1048 (fun (i, context, ty) (subst, menv, maxmeta) ->
1049 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1050 let newsubst = (i, (context, (Cic.Meta (maxmeta, irl)), ty)) in
1051 let newmeta = maxmeta, context, ty in
1052 newsubst::subst, newmeta::menv, maxmeta+1)
1053 menv ([], [], newmeta+1)
1055 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1058 (fun (i, (context, term, ty)) ->
1059 let context = CicMetaSubst.apply_subst_context subst context in
1060 let term = CicMetaSubst.apply_subst subst term in
1061 let ty = CicMetaSubst.apply_subst subst ty in
1062 (i, (context, term, ty))) subst in
1063 subst, metasenv, newmeta
1066 let fix_metas newmeta (w, p, (ty, left, right, o), menv, args) =
1068 let metas = (metas_of_term left)@(metas_of_term right)
1069 @(metas_of_term ty)@(metas_of_proof p) in
1070 let menv = List.filter (fun (i, _, _) -> List.mem i metas) menv in
1074 fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) in
1075 prerr_endline (string_of_equality eq); *)
1076 let subst, metasenv, newmeta = relocate newmeta menv in
1077 let ty = CicMetaSubst.apply_subst subst ty in
1078 let left = CicMetaSubst.apply_subst subst left in
1079 let right = CicMetaSubst.apply_subst subst right in
1080 let args = List.map (CicMetaSubst.apply_subst subst) args in
1081 let rec fix_proof = function
1082 | NoProof -> NoProof
1083 | BasicProof term -> BasicProof (CicMetaSubst.apply_subst subst term)
1084 | ProofBlock (subst', eq_URI, namety, bo, (pos, eq), p) ->
1088 (fun (i, (context, term, ty)) ->
1089 let context = CicMetaSubst.apply_subst_context subst context in
1090 let term = CicMetaSubst.apply_subst subst term in
1091 let ty = CicMetaSubst.apply_subst subst ty in
1092 (i, (context, term, ty))) subst' in *)
1093 ProofBlock (subst@subst', eq_URI, namety, bo, (pos, eq), p)
1096 let p = fix_proof p in
1098 let metas = (metas_of_term left)@(metas_of_term right)
1099 @(metas_of_term ty)@(metas_of_proof p) in
1100 let metasenv = List.filter (fun (i, _, _) -> List.mem i metas) metasenv in
1102 let eq = (w, p, (ty, left, right, o), metasenv, args) in
1103 (* debug prerr_endline (string_of_equality eq); *)
1106 let term_is_equality term =
1107 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1109 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1114 exception TermIsNotAnEquality;;
1116 let equality_of_term proof term =
1117 let eq_uri = LibraryObjects.eq_URI () in
1118 let iseq uri = UriManager.eq uri eq_uri in
1120 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1121 let o = !Utils.compare_terms t1 t2 in
1122 let stat = (ty,t1,t2,o) in
1123 let w = compute_equality_weight stat in
1124 let e = (w, BasicProof proof, stat, [], []) in
1127 raise TermIsNotAnEquality
1131 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1133 let is_weak_identity (metasenv, context, ugraph) = function
1134 | (_, _, (ty, left, right, _), menv, _) ->
1136 (meta_convertibility left right))
1137 (* the test below is not a good idea since it stops
1138 demodulation too early *)
1139 (* (fst (CicReduction.are_convertible
1140 ~metasenv:(metasenv @ menv) context left right ugraph)))*)
1143 let is_identity (metasenv, context, ugraph) = function
1144 | (_, _, (ty, left, right, _), menv, _) ->
1146 (* (meta_convertibility left right)) *)
1147 (fst (CicReduction.are_convertible
1148 ~metasenv:(metasenv @ menv) context left right ugraph)))
1152 let term_of_equality equality =
1153 let _, _, (ty, left, right, _), menv, _ = equality in
1154 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1155 let argsno = List.length menv in
1157 CicSubstitution.lift argsno
1158 (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
1162 (fun (i,_,ty) (n, t) ->
1163 let name = Cic.Name ("X" ^ (string_of_int n)) in
1164 let ty = CicSubstitution.lift (n-1) ty in
1166 ProofEngineReduction.replace
1167 ~equality:eq ~what:[i]
1168 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1170 (n-1, Cic.Prod (name, ty, t)))