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 rec lookup_subst meta subst =
372 | Cic.Meta (i, _) -> (
373 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst
374 in lookup_subst t subst
375 with Not_found -> meta
381 List.exists (fun (j,_,_) -> i = j) menv
384 let unification_simple locked_menv metasenv context t1 t2 ugraph =
385 let module C = Cic in
386 let module M = CicMetaSubst in
387 let module U = CicUnification in
388 let lookup = lookup_subst in
389 let rec occurs_check subst what where =
391 | t when what = t -> true
392 | C.Appl l -> List.exists (occurs_check subst what) l
394 let t = lookup where subst in
395 if t <> where then occurs_check subst what t else false
398 let rec unif subst menv s t =
399 let s = match s with C.Meta _ -> lookup s subst | _ -> s
400 and t = match t with C.Meta _ -> lookup t subst | _ -> t
404 | s, t when s = t -> subst, menv
405 | C.Meta (i, _), C.Meta (j, _)
406 when (locked locked_menv i) &&(locked locked_menv j) ->
408 (U.UnificationFailure (lazy "Inference.unification.unif"))
409 | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->
411 | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
413 | C.Meta _, t when occurs_check subst s t ->
415 (U.UnificationFailure (lazy "Inference.unification.unif"))
416 | C.Meta (i, l), t when (locked locked_menv i) ->
418 (U.UnificationFailure (lazy "Inference.unification.unif"))
419 | C.Meta (i, l), t -> (
421 let _, _, ty = CicUtil.lookup_meta i menv in
422 assert (not (List.mem_assoc i subst));
423 let subst = (i, (context, t, ty))::subst in
424 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
426 with CicUtil.Meta_not_found m ->
427 let names = names_of_context context in
430 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
431 (CicPp.pp t1 names) (CicPp.pp t2 names)
432 (print_metasenv menv) (print_metasenv metasenv)));
435 | _, C.Meta _ -> unif subst menv t s
436 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
437 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
438 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
441 (fun (subst', menv) s t -> unif subst' menv s t)
442 (subst, menv) tls tlt
443 with Invalid_argument _ ->
444 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
447 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
449 let subst, menv = unif [] metasenv t1 t2 in
450 let menv = filter subst menv in
451 List.rev subst, menv, ugraph
454 let profiler = HExtlib.profile "flatten"
456 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
457 let metasenv = metasenv1 @ metasenv2 in
458 let subst, menv, ug =
459 if not (is_simple_term t1) || not (is_simple_term t2) then (
462 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
463 (CicPp.ppterm t1) (CicPp.ppterm t2)));
464 raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
467 (* full unification *)
468 unification_simple [] metasenv context t1 t2 ugraph
470 (* matching: metasenv1 is locked *)
471 unification_simple metasenv1 metasenv context t1 t2 ugraph
473 if Utils.debug_res then
474 ignore(check_disjoint_invariant subst menv "unif");
477 (fun (i, (context, term, ty)) ->
478 let context = CicMetaSubst.apply_subst_context subst context in
479 let term = CicMetaSubst.apply_subst subst term in
480 let ty = CicMetaSubst.apply_subst subst ty in
481 (i, (context, term, ty))) subst
483 let flatten subst = profiler.HExtlib.profile flatten subst in
484 let subst = flatten subst in
488 exception MatchingFailure;;
490 let matching1 metasenv1 metasenv2 context t1 t2 ugraph =
492 unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
494 CicUnification .UnificationFailure _ ->
495 raise MatchingFailure
498 let unification = unification_aux true
504 let unification metasenv1 metasenv2 context t1 t2 ugraph =
505 let (subst, metasenv, ugraph) =
506 CicUnification.fo_unif (metasenv1@metasenv2) context t1 t2 ugraph in
507 if Utils.debug_res then
508 ignore(check_disjoint_invariant subst metasenv "fo_unif");
509 (subst, metasenv, ugraph)
516 let matching_simple metasenv context t1 t2 ugraph =
517 let module C = Cic in
518 let module M = CicMetaSubst in
519 let module U = CicUnification in
520 let lookup meta subst =
523 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
524 with Not_found -> meta
528 let rec do_match subst menv s t =
530 | s, t when s = t -> subst, menv
531 | s, C.Meta (i, l) ->
532 let filter_menv i menv =
533 List.filter (fun (m, _, _) -> i <> m) menv
536 let value = lookup t subst in
538 | value when value = t ->
539 let _, _, ty = CicUtil.lookup_meta i menv in
540 (i, (context, s, ty))::subst, filter_menv i menv
541 | value when value <> s ->
542 raise MatchingFailure
543 | value -> do_match subst menv s value
546 | C.Appl ls, C.Appl lt -> (
549 (fun (subst, menv) s t -> do_match subst menv s t)
551 with Invalid_argument _ ->
552 raise MatchingFailure
555 raise MatchingFailure
557 let subst, menv = do_match [] metasenv t1 t2 in
563 let matching metasenv context t1 t2 ugraph =
565 let subst, metasenv, ugraph =
567 unification metasenv context t1 t2 ugraph
568 with CicUtil.Meta_not_found _ as exn ->
569 Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
570 (CicPp.ppterm t1) (CicPp.ppterm t2)
571 (CicMetaSubst.ppmetasenv [] metasenv);
574 if Utils.debug_res then
575 ignore(check_disjoint_invariant subst metasenv "qua-2");
576 let t' = CicMetaSubst.apply_subst subst t1 in
577 if not (meta_convertibility t1 t') then
578 raise MatchingFailure
580 if Utils.debug_res then
581 ignore(check_disjoint_invariant subst metasenv "qua-1");
582 let metas = metas_of_term t1 in
585 (fun (i, (context, term, ty)) ->
586 let context = CicMetaSubst.apply_subst_context subst context in
587 let term = CicMetaSubst.apply_subst subst term in
588 let ty = CicMetaSubst.apply_subst subst ty in
589 (i, (context, term, ty))) subst in
590 if Utils.debug_res then
591 ignore(check_disjoint_invariant subst metasenv "qua0");
593 let subst, metasenv =
596 (subst,metasenv) s ->
598 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
600 List.filter (fun (x, _, _) -> x<>j) metasenv
602 ((j, (c, Cic.Meta (i, lc), ty))::subst,
604 |_ -> s::subst,metasenv) ([],metasenv) subst
606 if Utils.debug_res then
607 ignore(check_disjoint_invariant subst metasenv "qua1");
609 let fix_subst = function
610 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
611 (j, (c, Cic.Meta (i, lc), ty))
614 let subst = List.map fix_subst subst in *)
615 if CicMetaSubst.apply_subst subst t1 = t1 then
616 subst, metasenv, ugraph
618 (prerr_endline "mah"; raise MatchingFailure)
620 | CicUnification.UnificationFailure _
621 | CicUnification.Uncertain _ ->
622 raise MatchingFailure
626 (** matching takes in input the _disjoint_ metasenv of t1 and t2;
627 it perform unification in the union metasenv, then check that
628 the first metasenv has not changed *)
631 let matching2 metasenv1 metasenv2 context t1 t2 ugraph =
632 let subst, metasenv, ugraph =
634 unification metasenv1 metasenv2 context t1 t2 ugraph
636 CicUtil.Meta_not_found _ as exn ->
637 Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
638 (CicPp.ppterm t1) (CicPp.ppterm t2)
639 (CicMetaSubst.ppmetasenv [] (metasenv1@metasenv2));
641 | CicUnification.UnificationFailure _
642 | CicUnification.Uncertain _ ->
643 raise MatchingFailure
645 if Utils.debug_res then
646 ignore(check_disjoint_invariant subst metasenv "qua-2");
647 (* let us unfold subst *)
648 if metasenv = metasenv1 then
651 (fun (i, (context, term, ty)) ->
652 let context = CicMetaSubst.apply_subst_context subst context in
653 let term = CicMetaSubst.apply_subst subst term in
654 let ty = CicMetaSubst.apply_subst subst ty in
655 (i, (context, term, ty))) subst in
656 subst, metasenv, ugraph (* everything is fine *)
658 (* let us unfold subst *)
662 (fun (i, (context, term, ty)) ->
663 let context = CicMetaSubst.apply_subst_context subst context in
664 let term = CicMetaSubst.apply_subst subst term in
665 let ty = CicMetaSubst.apply_subst subst ty in
666 (i, (context, term, ty))) subst in
668 (* let us revert Meta-Meta in subst privileging metasenv1 *)
669 let subst, metasenv =
672 (subst,metasenv) s ->
674 | (i, (c, Cic.Meta (j, lc), ty))
675 when (List.exists (fun (x, _, _) -> x=i) metasenv1) &&
676 not (List.exists (fun (x, _) -> x=j) subst) ->
678 List.filter (fun (x, _, _) -> x<>j) metasenv
680 ((j, (c, Cic.Meta (i, lc), ty))::subst,
682 |_ -> s::subst,metasenv) ([],metasenv) subst
684 (* finally, let us chek again that metasenv = metasenv1 *)
685 if metasenv = metasenv1 then
686 subst, metasenv, ugraph
687 else raise MatchingFailure
691 let matching metasenv1 metasenv2 context t1 t2 ugraph =
693 try Some (matching1 metasenv1 metasenv2 context t1 t2 ugraph)
694 with MatchingFailure -> None
698 Some (matching2 metasenv1 metasenv2 context t1 t2 ugraph)
699 with MatchingFailure -> None
702 | Some (s,m,g) , None ->
703 prerr_endline (CicPp.ppterm t1);
704 prerr_endline (CicPp.ppterm t2);
705 prerr_endline "SOLO NOI";
706 prerr_endline (CicMetaSubst.ppsubst s);
709 prerr_endline (CicPp.ppterm t1);
710 prerr_endline (CicPp.ppterm t2);
711 prerr_endline "SOLO LUI";
713 | None, None -> raise MatchingFailure
714 | Some (s,m,g), Some (s',m',g') ->
715 let s = List.sort (fun (i,_) (j,_) -> i - j) s in
716 let s' = List.sort (fun (i,_) (j,_) -> i - j) s' in
719 prerr_endline (CicMetaSubst.ppsubst s);
720 prerr_endline (CicMetaSubst.ppsubst s');
721 prerr_endline (CicPp.ppterm t1);
722 prerr_endline (CicPp.ppterm t2);
726 let matching = matching1;;
728 let check_eq context msg eq =
729 let w, proof, (eq_ty, left, right, order), metas, args = eq in
730 if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
731 (fst (CicTypeChecker.type_of_aux' metas context left CicUniv.empty_ugraph))
732 CicUniv.empty_ugraph))
741 let find_equalities context proof =
742 let module C = Cic in
743 let module S = CicSubstitution in
744 let module T = CicTypeChecker in
745 let eq_uri = LibraryObjects.eq_URI () in
746 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
747 let ok_types ty menv =
748 List.for_all (fun (_, _, mt) -> mt = ty) menv
750 let rec aux index newmeta = function
752 | (Some (_, C.Decl (term)))::tl ->
753 let do_find context term =
755 | C.Prod (name, s, t) ->
756 let (head, newmetas, args, newmeta) =
757 ProofEngineHelpers.saturate_term newmeta []
758 context (S.lift index term) 0
761 if List.length args = 0 then
764 C.Appl ((C.Rel index)::args)
767 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
768 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
771 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
772 let o = !Utils.compare_terms t1 t2 in
773 let stat = (ty,t1,t2,o) in
774 let w = compute_equality_weight stat in
775 let proof = BasicProof p in
776 let e = (w, proof, stat, newmetas, args) in
780 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
781 when UriManager.eq uri eq_uri ->
782 let ty = S.lift index ty in
783 let t1 = S.lift index t1 in
784 let t2 = S.lift index t2 in
785 let o = !Utils.compare_terms t1 t2 in
786 let stat = (ty,t1,t2,o) in
787 let w = compute_equality_weight stat in
788 let e = (w, BasicProof (C.Rel index), stat, [], []) in
792 match do_find context term with
794 let tl, newmeta' = (aux (index+1) newmeta tl) in
795 if newmeta' < newmeta then
796 prerr_endline "big trouble";
797 (index, p)::tl, newmeta' (* max???? *)
799 aux (index+1) newmeta tl
802 aux (index+1) newmeta tl
804 let il, maxm = aux 1 newmeta context in
805 let indexes, equalities = List.split il in
806 ignore (List.iter (check_eq context "find") equalities);
807 indexes, equalities, maxm
812 let equations_blacklist =
814 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
815 UriManager.UriSet.empty [
816 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
817 "cic:/Coq/Init/Logic/trans_eq.con";
818 "cic:/Coq/Init/Logic/f_equal.con";
819 "cic:/Coq/Init/Logic/f_equal2.con";
820 "cic:/Coq/Init/Logic/f_equal3.con";
821 "cic:/Coq/Init/Logic/f_equal4.con";
822 "cic:/Coq/Init/Logic/f_equal5.con";
823 "cic:/Coq/Init/Logic/sym_eq.con";
824 "cic:/Coq/Init/Logic/eq_ind.con";
825 "cic:/Coq/Init/Logic/eq_ind_r.con";
826 "cic:/Coq/Init/Logic/eq_rec.con";
827 "cic:/Coq/Init/Logic/eq_rec_r.con";
828 "cic:/Coq/Init/Logic/eq_rect.con";
829 "cic:/Coq/Init/Logic/eq_rect_r.con";
830 "cic:/Coq/Logic/Eqdep/UIP.con";
831 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
832 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
833 "cic:/Coq/ZArith/Zcompare/rename.con";
834 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
835 perche' questo cacchio di teorema rompe le scatole :'( *)
836 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
838 "cic:/matita/logic/equality/eq_f.con";
839 "cic:/matita/logic/equality/eq_f2.con";
840 "cic:/matita/logic/equality/eq_rec.con";
841 "cic:/matita/logic/equality/eq_rect.con";
845 let equations_blacklist = UriManager.UriSet.empty;;
848 let find_library_equalities dbd context status maxmeta =
849 let module C = Cic in
850 let module S = CicSubstitution in
851 let module T = CicTypeChecker in
854 (fun s u -> UriManager.UriSet.add u s)
856 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
862 if UriManager.UriSet.mem uri blacklist then
865 let t = CicUtil.term_of_uri uri in
867 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
871 (let t1 = Unix.gettimeofday () in
872 let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
873 let t2 = Unix.gettimeofday () in
876 (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
880 let eq_uri1 = eq_XURI ()
881 and eq_uri2 = LibraryObjects.eq_URI () in
883 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
885 let ok_types ty menv =
886 List.for_all (fun (_, _, mt) -> mt = ty) menv
888 let rec has_vars = function
889 | C.Meta _ | C.Rel _ | C.Const _ -> false
891 | C.Appl l -> List.exists has_vars l
892 | C.Prod (_, s, t) | C.Lambda (_, s, t)
893 | C.LetIn (_, s, t) | C.Cast (s, t) ->
894 (has_vars s) || (has_vars t)
897 let rec aux newmeta = function
899 | (uri, term, termty)::tl ->
902 (Printf.sprintf "Examining: %s (%s)"
903 (CicPp.ppterm term) (CicPp.ppterm termty)));
906 | C.Prod (name, s, t) when not (has_vars termty) ->
907 let head, newmetas, args, newmeta =
908 ProofEngineHelpers.saturate_term newmeta [] context termty 0
911 if List.length args = 0 then
917 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
918 when (iseq uri) && (ok_types ty newmetas) ->
921 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
922 let o = !Utils.compare_terms t1 t2 in
923 let stat = (ty,t1,t2,o) in
924 let w = compute_equality_weight stat in
925 let proof = BasicProof p in
926 let e = (w, proof, stat, newmetas, args) in
930 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
931 when iseq uri && not (has_vars termty) ->
932 let o = !Utils.compare_terms t1 t2 in
933 let stat = (ty,t1,t2,o) in
934 let w = compute_equality_weight stat in
935 let e = (w, BasicProof term, stat, [], []) in
941 let tl, newmeta' = aux newmeta tl in
942 if newmeta' < newmeta then
943 prerr_endline "big trouble";
944 (uri, e)::tl, newmeta' (* max???? *)
948 let found, maxm = aux maxmeta candidates in
951 (fun (s, l) (u, e) ->
952 if List.exists (meta_convertibility_eq e) (List.map snd l) then (
955 (Printf.sprintf "NO!! %s already there!"
956 (string_of_equality e)));
957 (UriManager.UriSet.add u s, l)
958 ) else (UriManager.UriSet.add u s, (u, e)::l))
959 (UriManager.UriSet.empty, []) found)
965 let find_library_theorems dbd env status equalities_uris =
966 let module C = Cic in
967 let module S = CicSubstitution in
968 let module T = CicTypeChecker in
971 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
973 UriManager.UriSet.remove refl_equal
974 (UriManager.UriSet.union equalities_uris equations_blacklist)
977 (fun s u -> UriManager.UriSet.add u s)
978 s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
981 let metasenv, context, ugraph = env in
985 if UriManager.UriSet.mem uri blacklist then l
987 let t = CicUtil.term_of_uri uri in
988 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
990 [] (MetadataQuery.signature_of_goal ~dbd status)
993 let u = eq_XURI () in
994 let t = CicUtil.term_of_uri u in
995 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
998 refl_equal::candidates
1002 let find_context_hypotheses env equalities_indexes =
1003 let metasenv, context, ugraph = env in
1006 (fun (n, l) entry ->
1010 if List.mem n equalities_indexes then
1013 let t = Cic.Rel n in
1015 CicTypeChecker.type_of_aux' metasenv context t ugraph in
1016 (n+1, (t, ty, [])::l))
1023 let fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) =
1024 let table = Hashtbl.create (List.length args) in
1026 let newargs, newmeta =
1028 (fun t (newargs, index) ->
1030 | Cic.Meta (i, l) ->
1031 if Hashtbl.mem table i then
1032 let idx = Hashtbl.find table i in
1033 ((Cic.Meta (idx, l))::newargs, index+1)
1035 let _ = Hashtbl.add table i index in
1036 ((Cic.Meta (index, l))::newargs, index+1)
1037 | _ -> assert false)
1038 args ([], newmeta+1)
1042 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1047 (fun (i, context, term) menv ->
1049 let index = Hashtbl.find table i in
1050 (index, context, term)::menv
1052 (i, context, term)::menv)
1056 and left = repl left
1057 and right = repl right in
1059 (metas_of_term left) @
1060 (metas_of_term right) @
1061 (metas_of_term ty) @ (metas_of_proof p) in
1062 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
1065 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1068 if List.length metas > 0 then
1069 let first = List.hd metas in
1070 (* this new equality might have less variables than its parents: here
1071 we fill the gap with a dummy arg. Example:
1072 with (f X Y) = X we can simplify
1075 So the new equation has only one variable, but it still has type like
1076 \lambda X,Y:..., so we need to pass a dummy arg for Y
1077 (I hope this makes some sense...)
1082 (function Cic.Meta (i, _) -> i = v | _ -> assert false)
1084 Hashtbl.replace table k first)
1085 (Hashtbl.copy table)
1087 let rec fix_proof = function
1088 | NoProof -> NoProof
1089 | BasicProof term -> BasicProof (repl term)
1090 | ProofBlock (subst, eq_URI, namety, bo, (pos, eq), p) ->
1095 | Cic.Meta (i, l) -> (
1097 let j = Hashtbl.find table i in
1098 if List.mem_assoc i subst then
1101 let _, context, ty = CicUtil.lookup_meta i menv in
1102 (i, (context, Cic.Meta (j, l), ty))::s
1103 with Not_found | CicUtil.Meta_not_found _ ->
1106 | _ -> assert false)
1109 ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1112 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1117 let relocate newmeta menv =
1118 let subst, metasenv, newmeta =
1120 (fun (i, context, ty) (subst, menv, maxmeta) ->
1121 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1122 let newsubst = (i, (context, (Cic.Meta (maxmeta, irl)), ty)) in
1123 let newmeta = maxmeta, context, ty in
1124 newsubst::subst, newmeta::menv, maxmeta+1)
1125 menv ([], [], newmeta+1)
1127 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1130 (fun (i, (context, term, ty)) ->
1131 let context = CicMetaSubst.apply_subst_context subst context in
1132 let term = CicMetaSubst.apply_subst subst term in
1133 let ty = CicMetaSubst.apply_subst subst ty in
1134 (i, (context, term, ty))) subst in
1135 subst, metasenv, newmeta
1138 let fix_metas newmeta (w, p, (ty, left, right, o), menv, args) =
1140 let metas = (metas_of_term left)@(metas_of_term right)
1141 @(metas_of_term ty)@(metas_of_proof p) in
1142 let menv = List.filter (fun (i, _, _) -> List.mem i metas) menv in
1146 fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) in
1147 prerr_endline (string_of_equality eq); *)
1148 let subst, metasenv, newmeta = relocate newmeta menv in
1149 let ty = CicMetaSubst.apply_subst subst ty in
1150 let left = CicMetaSubst.apply_subst subst left in
1151 let right = CicMetaSubst.apply_subst subst right in
1152 let args = List.map (CicMetaSubst.apply_subst subst) args in
1153 let rec fix_proof = function
1154 | NoProof -> NoProof
1155 | BasicProof term -> BasicProof (CicMetaSubst.apply_subst subst term)
1156 | ProofBlock (subst', eq_URI, namety, bo, (pos, eq), p) ->
1160 (fun (i, (context, term, ty)) ->
1161 let context = CicMetaSubst.apply_subst_context subst context in
1162 let term = CicMetaSubst.apply_subst subst term in
1163 let ty = CicMetaSubst.apply_subst subst ty in
1164 (i, (context, term, ty))) subst' in *)
1165 ProofBlock (subst@subst', eq_URI, namety, bo, (pos, eq), p)
1168 let p = fix_proof p in
1170 let metas = (metas_of_term left)@(metas_of_term right)
1171 @(metas_of_term ty)@(metas_of_proof p) in
1172 let metasenv = List.filter (fun (i, _, _) -> List.mem i metas) metasenv in
1174 let eq = (w, p, (ty, left, right, o), metasenv, args) in
1175 (* debug prerr_endline (string_of_equality eq); *)
1178 let term_is_equality term =
1179 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1181 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1186 exception TermIsNotAnEquality;;
1188 let equality_of_term proof term =
1189 let eq_uri = LibraryObjects.eq_URI () in
1190 let iseq uri = UriManager.eq uri eq_uri in
1192 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1193 let o = !Utils.compare_terms t1 t2 in
1194 let stat = (ty,t1,t2,o) in
1195 let w = compute_equality_weight stat in
1196 let e = (w, BasicProof proof, stat, [], []) in
1199 raise TermIsNotAnEquality
1203 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1205 let is_weak_identity (metasenv, context, ugraph) = function
1206 | (_, _, (ty, left, right, _), menv, _) ->
1208 (meta_convertibility left right))
1209 (* the test below is not a good idea since it stops
1210 demodulation too early *)
1211 (* (fst (CicReduction.are_convertible
1212 ~metasenv:(metasenv @ menv) context left right ugraph)))*)
1215 let is_identity (metasenv, context, ugraph) = function
1216 | (_, _, (ty, left, right, _), menv, _) ->
1218 (* (meta_convertibility left right)) *)
1219 (fst (CicReduction.are_convertible
1220 ~metasenv:(metasenv @ menv) context left right ugraph)))
1224 let term_of_equality equality =
1225 let _, _, (ty, left, right, _), menv, _ = equality in
1226 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1227 let argsno = List.length menv in
1229 CicSubstitution.lift argsno
1230 (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
1234 (fun (i,_,ty) (n, t) ->
1235 let name = Cic.Name ("X" ^ (string_of_int n)) in
1236 let ty = CicSubstitution.lift (n-1) ty in
1238 ProofEngineReduction.replace
1239 ~equality:eq ~what:[i]
1240 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1242 (n-1, Cic.Prod (name, ty, t)))