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 *)
43 | NoProof (* term is the goal missing a proof *)
44 | BasicProof of Cic.term
46 Cic.substitution * UriManager.uri *
47 (Cic.name * Cic.term) * Cic.term * (Utils.pos * equality) * proof
48 | ProofGoalBlock of proof * proof
49 | ProofSymBlock of Cic.term list * proof
50 | SubProof of Cic.term * int * proof
53 let string_of_equality ?env =
57 | w, _, (ty, left, right, o), _ ->
58 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
59 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
61 | Some (_, context, _) -> (
62 let names = names_of_context context in
64 | w, _, (ty, left, right, o), _ ->
65 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
66 (CicPp.pp left names) (string_of_comparison o)
67 (CicPp.pp right names)
72 let rec string_of_proof = function
73 | NoProof -> "NoProof "
74 | BasicProof t -> "BasicProof " ^ (CicPp.ppterm t)
75 | SubProof (t, i, p) ->
76 Printf.sprintf "SubProof(%s, %s, %s)"
77 (CicPp.ppterm t) (string_of_int i) (string_of_proof p)
78 | ProofSymBlock _ -> "ProofSymBlock"
79 | ProofBlock (subst, _, _, _ ,_,_) ->
80 "ProofBlock" ^ (CicMetaSubst.ppsubst subst)
81 | ProofGoalBlock (p1, p2) ->
82 Printf.sprintf "ProofGoalBlock(%s, %s)"
83 (string_of_proof p1) (string_of_proof p2)
87 let check_disjoint_invariant subst metasenv msg =
89 (fun (i,_,_) -> (List.exists (fun (j,_) -> i=j) subst)) metasenv)
92 prerr_endline ("not disjoint: " ^ msg);
97 (* filter out from metasenv the variables in substs *)
98 let filter subst metasenv =
101 try let _ = List.find (fun (i, _) -> m = i) subst in false
102 with Not_found -> true)
106 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
107 let build_ens_for_sym_eq sym_eq_URI termlist =
108 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph sym_eq_URI in
110 | Cic.Constant (_, _, _, uris, _) ->
111 assert (List.length uris <= List.length termlist);
112 let rec aux = function
114 | (uri::uris), (term::tl) ->
115 let ens, args = aux (uris, tl) in
116 (uri, term)::ens, args
117 | _, _ -> assert false
124 let build_proof_term ?(noproof=Cic.Implicit None) proof =
125 let rec do_build_proof proof =
128 Printf.fprintf stderr "WARNING: no proof!\n";
130 | BasicProof term -> term
131 | ProofGoalBlock (proofbit, proof) ->
132 print_endline "found ProofGoalBlock, going up...";
133 do_build_goal_proof proofbit proof
134 | ProofSymBlock (termlist, proof) ->
135 let proof = do_build_proof proof in
136 let ens, args = build_ens_for_sym_eq (Utils.sym_eq_URI ()) termlist in
137 Cic.Appl ([Cic.Const (Utils.sym_eq_URI (), ens)] @ args @ [proof])
138 | ProofBlock (subst, eq_URI, (name, ty), bo, (pos, eq), eqproof) ->
139 let t' = Cic.Lambda (name, ty, bo) in
141 let _, proof', _, _ = eq in
142 do_build_proof proof'
144 let eqproof = do_build_proof eqproof in
145 let _, _, (ty, what, other, _), menv' = eq in
147 if pos = Utils.Left then what, other else other, what
149 CicMetaSubst.apply_subst subst
150 (Cic.Appl [Cic.Const (eq_URI, []); ty;
151 what; t'; eqproof; other; proof'])
152 | SubProof (term, meta_index, proof) ->
153 let proof = do_build_proof proof in
155 | Cic.Meta (j, _) -> i = j
158 ProofEngineReduction.replace
159 ~equality:eq ~what:[meta_index] ~with_what:[proof] ~where:term
161 and do_build_goal_proof proofbit proof =
163 | ProofGoalBlock (pb, p) ->
164 do_build_proof (ProofGoalBlock (replace_proof proofbit pb, p))
165 | _ -> do_build_proof (replace_proof proofbit proof)
167 and replace_proof newproof = function
168 | ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof) ->
169 let eqproof' = replace_proof newproof eqproof in
170 ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof')
171 | ProofGoalBlock (pb, p) ->
172 let pb' = replace_proof newproof pb in
173 ProofGoalBlock (pb', p)
174 | BasicProof _ -> newproof
175 | SubProof (term, meta_index, p) ->
176 SubProof (term, meta_index, replace_proof newproof p)
183 let rec metas_of_term = function
184 | Cic.Meta (i, c) -> [i]
187 | Cic.MutInd (_, _, ens)
188 | Cic.MutConstruct (_, _, _, ens) ->
189 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
192 | Cic.Lambda (_, s, t)
193 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
194 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
195 | Cic.MutCase (uri, i, s, t, l) ->
196 (metas_of_term s) @ (metas_of_term t) @
197 (List.flatten (List.map metas_of_term l))
200 (List.map (fun (s, i, t1, t2) ->
201 (metas_of_term t1) @ (metas_of_term t2)) il)
202 | Cic.CoFix (i, il) ->
204 (List.map (fun (s, t1, t2) ->
205 (metas_of_term t1) @ (metas_of_term t2)) il)
209 let rec metas_of_proof p =
211 let t1 = Unix.gettimeofday () in
212 let res = metas_of_term (build_proof_term p) in
213 let t2 = Unix.gettimeofday () in
214 metas_of_proof_time := !metas_of_proof_time +. (t2 -. t1);
217 metas_of_term (build_proof_term p)
220 exception NotMetaConvertible;;
222 let meta_convertibility_aux table t1 t2 =
223 let module C = Cic in
224 let rec aux ((table_l, table_r) as table) t1 t2 =
226 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
227 let m1_binding, table_l =
228 try List.assoc m1 table_l, table_l
229 with Not_found -> m2, (m1, m2)::table_l
230 and m2_binding, table_r =
231 try List.assoc m2 table_r, table_r
232 with Not_found -> m1, (m2, m1)::table_r
234 if (m1_binding <> m2) || (m2_binding <> m1) then
235 raise NotMetaConvertible
241 | None, Some _ | Some _, None -> raise NotMetaConvertible
243 | Some t1, Some t2 -> (aux res t1 t2))
244 (table_l, table_r) tl1 tl2
245 with Invalid_argument _ ->
246 raise NotMetaConvertible
248 | C.Var (u1, ens1), C.Var (u2, ens2)
249 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
250 aux_ens table ens1 ens2
251 | C.Cast (s1, t1), C.Cast (s2, t2)
252 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
253 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
254 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
255 let table = aux table s1 s2 in
257 | C.Appl l1, C.Appl l2 -> (
258 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
259 with Invalid_argument _ -> raise NotMetaConvertible
261 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
262 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
263 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
264 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
265 aux_ens table ens1 ens2
266 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
267 when (UriManager.eq u1 u2) && i1 = i2 ->
268 let table = aux table s1 s2 in
269 let table = aux table t1 t2 in (
270 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
271 with Invalid_argument _ -> raise NotMetaConvertible
273 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
276 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
277 if i1 <> i2 then raise NotMetaConvertible
279 let res = (aux res s1 s2) in aux res t1 t2)
281 with Invalid_argument _ -> raise NotMetaConvertible
283 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
286 (fun res (n1, s1, t1) (n2, s2, t2) ->
287 let res = aux res s1 s2 in aux res t1 t2)
289 with Invalid_argument _ -> raise NotMetaConvertible
291 | t1, t2 when t1 = t2 -> table
292 | _, _ -> raise NotMetaConvertible
294 and aux_ens table ens1 ens2 =
295 let cmp (u1, t1) (u2, t2) =
296 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
298 let ens1 = List.sort cmp ens1
299 and ens2 = List.sort cmp ens2 in
302 (fun res (u1, t1) (u2, t2) ->
303 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
306 with Invalid_argument _ -> raise NotMetaConvertible
312 let meta_convertibility_eq eq1 eq2 =
313 let _, _, (ty, left, right, _), _ = eq1
314 and _, _, (ty', left', right', _), _ = eq2 in
317 else if (left = left') && (right = right') then
319 else if (left = right') && (right = left') then
323 let table = meta_convertibility_aux ([], []) left left' in
324 let _ = meta_convertibility_aux table right right' in
326 with NotMetaConvertible ->
328 let table = meta_convertibility_aux ([], []) left right' in
329 let _ = meta_convertibility_aux table right left' in
331 with NotMetaConvertible ->
336 let meta_convertibility t1 t2 =
341 ignore(meta_convertibility_aux ([], []) t1 t2);
343 with NotMetaConvertible ->
348 let rec check_irl start = function
350 | None::tl -> check_irl (start+1) tl
351 | (Some (Cic.Rel x))::tl ->
352 if x = start then check_irl (start+1) tl else false
357 let rec is_simple_term = function
358 | Cic.Appl ((Cic.Meta _)::_) -> false
359 | Cic.Appl l -> List.for_all is_simple_term l
360 | Cic.Meta (i, l) -> check_irl 1 l
362 | Cic.Const _ -> true
363 | Cic.MutInd (_, _, []) -> true
364 | Cic.MutConstruct (_, _, _, []) -> true
369 let rec lookup_subst meta subst =
371 | Cic.Meta (i, _) -> (
372 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst
373 in lookup_subst t subst
374 with Not_found -> meta
380 List.exists (fun (j,_,_) -> i = j) menv
383 let unification_simple locked_menv metasenv context t1 t2 ugraph =
384 let module C = Cic in
385 let module M = CicMetaSubst in
386 let module U = CicUnification in
387 let lookup = lookup_subst in
388 let rec occurs_check subst what where =
390 | t when what = t -> true
391 | C.Appl l -> List.exists (occurs_check subst what) l
393 let t = lookup where subst in
394 if t <> where then occurs_check subst what t else false
397 let rec unif subst menv s t =
398 let s = match s with C.Meta _ -> lookup s subst | _ -> s
399 and t = match t with C.Meta _ -> lookup t subst | _ -> t
403 | s, t when s = t -> subst, menv
404 | C.Meta (i, _), C.Meta (j, _)
405 when (locked locked_menv i) &&(locked locked_menv j) ->
407 (U.UnificationFailure (lazy "Inference.unification.unif"))
408 | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->
410 | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
412 | C.Meta _, t when occurs_check subst s t ->
414 (U.UnificationFailure (lazy "Inference.unification.unif"))
415 | C.Meta (i, l), t when (locked locked_menv i) ->
417 (U.UnificationFailure (lazy "Inference.unification.unif"))
418 | C.Meta (i, l), t -> (
420 let _, _, ty = CicUtil.lookup_meta i menv in
421 assert (not (List.mem_assoc i subst));
422 let subst = (i, (context, t, ty))::subst in
423 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
425 with CicUtil.Meta_not_found m ->
426 let names = names_of_context context in
429 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
430 (CicPp.pp t1 names) (CicPp.pp t2 names)
431 (print_metasenv menv) (print_metasenv metasenv)));
434 | _, C.Meta _ -> unif subst menv t s
435 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
436 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
437 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
440 (fun (subst', menv) s t -> unif subst' menv s t)
441 (subst, menv) tls tlt
442 with Invalid_argument _ ->
443 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
446 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
448 let subst, menv = unif [] metasenv t1 t2 in
449 let menv = filter subst menv in
450 List.rev subst, menv, ugraph
453 let profiler = HExtlib.profile "flatten"
455 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
456 let metasenv = metasenv1 @ metasenv2 in
457 let subst, menv, ug =
458 if not (is_simple_term t1) || not (is_simple_term t2) then (
461 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
462 (CicPp.ppterm t1) (CicPp.ppterm t2)));
463 raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
466 (* full unification *)
467 unification_simple [] metasenv context t1 t2 ugraph
469 (* matching: metasenv1 is locked *)
470 unification_simple metasenv1 metasenv context t1 t2 ugraph
472 if Utils.debug_res then
473 ignore(check_disjoint_invariant subst menv "unif");
476 (fun (i, (context, term, ty)) ->
477 let context = CicMetaSubst.apply_subst_context subst context in
478 let term = CicMetaSubst.apply_subst subst term in
479 let ty = CicMetaSubst.apply_subst subst ty in
480 (i, (context, term, ty))) subst
482 let flatten subst = profiler.HExtlib.profile flatten subst in
483 let subst = flatten subst in
487 exception MatchingFailure;;
489 let matching1 metasenv1 metasenv2 context t1 t2 ugraph =
491 unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
493 CicUnification .UnificationFailure _ ->
494 raise MatchingFailure
497 let unification = unification_aux true
503 let unification metasenv1 metasenv2 context t1 t2 ugraph =
504 let (subst, metasenv, ugraph) =
505 CicUnification.fo_unif (metasenv1@metasenv2) context t1 t2 ugraph in
506 if Utils.debug_res then
507 ignore(check_disjoint_invariant subst metasenv "fo_unif");
508 (subst, metasenv, ugraph)
515 let matching_simple metasenv context t1 t2 ugraph =
516 let module C = Cic in
517 let module M = CicMetaSubst in
518 let module U = CicUnification in
519 let lookup meta subst =
522 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
523 with Not_found -> meta
527 let rec do_match subst menv s t =
529 | s, t when s = t -> subst, menv
530 | s, C.Meta (i, l) ->
531 let filter_menv i menv =
532 List.filter (fun (m, _, _) -> i <> m) menv
535 let value = lookup t subst in
537 | value when value = t ->
538 let _, _, ty = CicUtil.lookup_meta i menv in
539 (i, (context, s, ty))::subst, filter_menv i menv
540 | value when value <> s ->
541 raise MatchingFailure
542 | value -> do_match subst menv s value
545 | C.Appl ls, C.Appl lt -> (
548 (fun (subst, menv) s t -> do_match subst menv s t)
550 with Invalid_argument _ ->
551 raise MatchingFailure
554 raise MatchingFailure
556 let subst, menv = do_match [] metasenv t1 t2 in
562 let matching metasenv context t1 t2 ugraph =
564 let subst, metasenv, ugraph =
566 unification metasenv context t1 t2 ugraph
567 with CicUtil.Meta_not_found _ as exn ->
568 Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
569 (CicPp.ppterm t1) (CicPp.ppterm t2)
570 (CicMetaSubst.ppmetasenv [] metasenv);
573 if Utils.debug_res then
574 ignore(check_disjoint_invariant subst metasenv "qua-2");
575 let t' = CicMetaSubst.apply_subst subst t1 in
576 if not (meta_convertibility t1 t') then
577 raise MatchingFailure
579 if Utils.debug_res then
580 ignore(check_disjoint_invariant subst metasenv "qua-1");
581 let metas = metas_of_term t1 in
584 (fun (i, (context, term, ty)) ->
585 let context = CicMetaSubst.apply_subst_context subst context in
586 let term = CicMetaSubst.apply_subst subst term in
587 let ty = CicMetaSubst.apply_subst subst ty in
588 (i, (context, term, ty))) subst in
589 if Utils.debug_res then
590 ignore(check_disjoint_invariant subst metasenv "qua0");
592 let subst, metasenv =
595 (subst,metasenv) s ->
597 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
599 List.filter (fun (x, _, _) -> x<>j) metasenv
601 ((j, (c, Cic.Meta (i, lc), ty))::subst,
603 |_ -> s::subst,metasenv) ([],metasenv) subst
605 if Utils.debug_res then
606 ignore(check_disjoint_invariant subst metasenv "qua1");
608 let fix_subst = function
609 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
610 (j, (c, Cic.Meta (i, lc), ty))
613 let subst = List.map fix_subst subst in *)
614 if CicMetaSubst.apply_subst subst t1 = t1 then
615 subst, metasenv, ugraph
617 (prerr_endline "mah"; raise MatchingFailure)
619 | CicUnification.UnificationFailure _
620 | CicUnification.Uncertain _ ->
621 raise MatchingFailure
625 (** matching takes in input the _disjoint_ metasenv of t1 and t2;
626 it perform unification in the union metasenv, then check that
627 the first metasenv has not changed *)
630 let matching2 metasenv1 metasenv2 context t1 t2 ugraph =
631 let subst, metasenv, ugraph =
633 unification metasenv1 metasenv2 context t1 t2 ugraph
635 CicUtil.Meta_not_found _ as exn ->
636 Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
637 (CicPp.ppterm t1) (CicPp.ppterm t2)
638 (CicMetaSubst.ppmetasenv [] (metasenv1@metasenv2));
640 | CicUnification.UnificationFailure _
641 | CicUnification.Uncertain _ ->
642 raise MatchingFailure
644 if Utils.debug_res then
645 ignore(check_disjoint_invariant subst metasenv "qua-2");
646 (* let us unfold subst *)
647 if metasenv = metasenv1 then
650 (fun (i, (context, term, ty)) ->
651 let context = CicMetaSubst.apply_subst_context subst context in
652 let term = CicMetaSubst.apply_subst subst term in
653 let ty = CicMetaSubst.apply_subst subst ty in
654 (i, (context, term, ty))) subst in
655 subst, metasenv, ugraph (* everything is fine *)
657 (* let us unfold subst *)
661 (fun (i, (context, term, ty)) ->
662 let context = CicMetaSubst.apply_subst_context subst context in
663 let term = CicMetaSubst.apply_subst subst term in
664 let ty = CicMetaSubst.apply_subst subst ty in
665 (i, (context, term, ty))) subst in
667 (* let us revert Meta-Meta in subst privileging metasenv1 *)
668 let subst, metasenv =
671 (subst,metasenv) s ->
673 | (i, (c, Cic.Meta (j, lc), ty))
674 when (List.exists (fun (x, _, _) -> x=i) metasenv1) &&
675 not (List.exists (fun (x, _) -> x=j) subst) ->
677 List.filter (fun (x, _, _) -> x<>j) metasenv
679 ((j, (c, Cic.Meta (i, lc), ty))::subst,
681 |_ -> s::subst,metasenv) ([],metasenv) subst
683 (* finally, let us chek again that metasenv = metasenv1 *)
684 if metasenv = metasenv1 then
685 subst, metasenv, ugraph
686 else raise MatchingFailure
690 let matching metasenv1 metasenv2 context t1 t2 ugraph =
692 try Some (matching1 metasenv1 metasenv2 context t1 t2 ugraph)
693 with MatchingFailure -> None
697 Some (matching2 metasenv1 metasenv2 context t1 t2 ugraph)
698 with MatchingFailure -> None
701 | Some (s,m,g) , None ->
702 prerr_endline (CicPp.ppterm t1);
703 prerr_endline (CicPp.ppterm t2);
704 prerr_endline "SOLO NOI";
705 prerr_endline (CicMetaSubst.ppsubst s);
708 prerr_endline (CicPp.ppterm t1);
709 prerr_endline (CicPp.ppterm t2);
710 prerr_endline "SOLO LUI";
712 | None, None -> raise MatchingFailure
713 | Some (s,m,g), Some (s',m',g') ->
714 let s = List.sort (fun (i,_) (j,_) -> i - j) s in
715 let s' = List.sort (fun (i,_) (j,_) -> i - j) s' in
718 prerr_endline (CicMetaSubst.ppsubst s);
719 prerr_endline (CicMetaSubst.ppsubst s');
720 prerr_endline (CicPp.ppterm t1);
721 prerr_endline (CicPp.ppterm t2);
725 let matching = matching1;;
727 let check_eq context msg eq =
728 let w, proof, (eq_ty, left, right, order), metas = eq in
729 if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
730 (fst (CicTypeChecker.type_of_aux' metas context left CicUniv.empty_ugraph))
731 CicUniv.empty_ugraph))
740 let find_equalities context proof =
741 let module C = Cic in
742 let module S = CicSubstitution in
743 let module T = CicTypeChecker in
744 let eq_uri = LibraryObjects.eq_URI () in
745 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
746 let ok_types ty menv =
747 List.for_all (fun (_, _, mt) -> mt = ty) menv
749 let rec aux index newmeta = function
751 | (Some (_, C.Decl (term)))::tl ->
752 let do_find context term =
754 | C.Prod (name, s, t) ->
755 let (head, newmetas, args, newmeta) =
756 ProofEngineHelpers.saturate_term newmeta []
757 context (S.lift index term) 0
760 if List.length args = 0 then
763 C.Appl ((C.Rel index)::args)
766 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
767 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
770 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
771 let o = !Utils.compare_terms t1 t2 in
772 let stat = (ty,t1,t2,o) in
773 let w = compute_equality_weight stat in
774 let proof = BasicProof p in
775 let e = (w, proof, stat, newmetas) in
779 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
780 when UriManager.eq uri eq_uri ->
781 let ty = S.lift index ty in
782 let t1 = S.lift index t1 in
783 let t2 = S.lift index t2 in
784 let o = !Utils.compare_terms t1 t2 in
785 let stat = (ty,t1,t2,o) in
786 let w = compute_equality_weight stat in
787 let e = (w, BasicProof (C.Rel index), stat, []) in
791 match do_find context term with
793 let tl, newmeta' = (aux (index+1) newmeta tl) in
794 if newmeta' < newmeta then
795 prerr_endline "big trouble";
796 (index, p)::tl, newmeta' (* max???? *)
798 aux (index+1) newmeta tl
801 aux (index+1) newmeta tl
803 let il, maxm = aux 1 newmeta context in
804 let indexes, equalities = List.split il in
805 ignore (List.iter (check_eq context "find") equalities);
806 indexes, equalities, maxm
811 let equations_blacklist =
813 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
814 UriManager.UriSet.empty [
815 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
816 "cic:/Coq/Init/Logic/trans_eq.con";
817 "cic:/Coq/Init/Logic/f_equal.con";
818 "cic:/Coq/Init/Logic/f_equal2.con";
819 "cic:/Coq/Init/Logic/f_equal3.con";
820 "cic:/Coq/Init/Logic/f_equal4.con";
821 "cic:/Coq/Init/Logic/f_equal5.con";
822 "cic:/Coq/Init/Logic/sym_eq.con";
823 "cic:/Coq/Init/Logic/eq_ind.con";
824 "cic:/Coq/Init/Logic/eq_ind_r.con";
825 "cic:/Coq/Init/Logic/eq_rec.con";
826 "cic:/Coq/Init/Logic/eq_rec_r.con";
827 "cic:/Coq/Init/Logic/eq_rect.con";
828 "cic:/Coq/Init/Logic/eq_rect_r.con";
829 "cic:/Coq/Logic/Eqdep/UIP.con";
830 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
831 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
832 "cic:/Coq/ZArith/Zcompare/rename.con";
833 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
834 perche' questo cacchio di teorema rompe le scatole :'( *)
835 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
837 "cic:/matita/logic/equality/eq_f.con";
838 "cic:/matita/logic/equality/eq_f2.con";
839 "cic:/matita/logic/equality/eq_rec.con";
840 "cic:/matita/logic/equality/eq_rect.con";
844 let equations_blacklist = UriManager.UriSet.empty;;
847 let find_library_equalities dbd context status maxmeta =
848 let module C = Cic in
849 let module S = CicSubstitution in
850 let module T = CicTypeChecker in
853 (fun s u -> UriManager.UriSet.add u s)
855 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
861 if UriManager.UriSet.mem uri blacklist then
864 let t = CicUtil.term_of_uri uri in
866 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
870 (let t1 = Unix.gettimeofday () in
871 let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
872 let t2 = Unix.gettimeofday () in
875 (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
879 let eq_uri1 = eq_XURI ()
880 and eq_uri2 = LibraryObjects.eq_URI () in
882 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
884 let ok_types ty menv =
885 List.for_all (fun (_, _, mt) -> mt = ty) menv
887 let rec has_vars = function
888 | C.Meta _ | C.Rel _ | C.Const _ -> false
890 | C.Appl l -> List.exists has_vars l
891 | C.Prod (_, s, t) | C.Lambda (_, s, t)
892 | C.LetIn (_, s, t) | C.Cast (s, t) ->
893 (has_vars s) || (has_vars t)
896 let rec aux newmeta = function
898 | (uri, term, termty)::tl ->
901 (Printf.sprintf "Examining: %s (%s)"
902 (CicPp.ppterm term) (CicPp.ppterm termty)));
905 | C.Prod (name, s, t) when not (has_vars termty) ->
906 let head, newmetas, args, newmeta =
907 ProofEngineHelpers.saturate_term newmeta [] context termty 0
910 if List.length args = 0 then
916 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
917 when (iseq uri) && (ok_types ty newmetas) ->
920 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
921 let o = !Utils.compare_terms t1 t2 in
922 let stat = (ty,t1,t2,o) in
923 let w = compute_equality_weight stat in
924 let proof = BasicProof p in
925 let e = (w, proof, stat, newmetas) in
929 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
930 when iseq uri && not (has_vars termty) ->
931 let o = !Utils.compare_terms t1 t2 in
932 let stat = (ty,t1,t2,o) in
933 let w = compute_equality_weight stat in
934 let e = (w, BasicProof term, stat, []) in
940 let tl, newmeta' = aux newmeta tl in
941 if newmeta' < newmeta then
942 prerr_endline "big trouble";
943 (uri, e)::tl, newmeta' (* max???? *)
947 let found, maxm = aux maxmeta candidates in
950 (fun (s, l) (u, e) ->
951 if List.exists (meta_convertibility_eq e) (List.map snd l) then (
954 (Printf.sprintf "NO!! %s already there!"
955 (string_of_equality e)));
956 (UriManager.UriSet.add u s, l)
957 ) else (UriManager.UriSet.add u s, (u, e)::l))
958 (UriManager.UriSet.empty, []) found)
964 let find_library_theorems dbd env status equalities_uris =
965 let module C = Cic in
966 let module S = CicSubstitution in
967 let module T = CicTypeChecker in
970 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
972 UriManager.UriSet.remove refl_equal
973 (UriManager.UriSet.union equalities_uris equations_blacklist)
976 (fun s u -> UriManager.UriSet.add u s)
977 s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
980 let metasenv, context, ugraph = env in
984 if UriManager.UriSet.mem uri blacklist then l
986 let t = CicUtil.term_of_uri uri in
987 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
989 [] (MetadataQuery.signature_of_goal ~dbd status)
992 let u = eq_XURI () in
993 let t = CicUtil.term_of_uri u in
994 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
997 refl_equal::candidates
1001 let find_context_hypotheses env equalities_indexes =
1002 let metasenv, context, ugraph = env in
1005 (fun (n, l) entry ->
1009 if List.mem n equalities_indexes then
1012 let t = Cic.Rel n in
1014 CicTypeChecker.type_of_aux' metasenv context t ugraph in
1015 (n+1, (t, ty, [])::l))
1022 let fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) =
1023 let table = Hashtbl.create (List.length args) in
1025 let newargs, newmeta =
1027 (fun t (newargs, index) ->
1029 | Cic.Meta (i, l) ->
1030 if Hashtbl.mem table i then
1031 let idx = Hashtbl.find table i in
1032 ((Cic.Meta (idx, l))::newargs, index+1)
1034 let _ = Hashtbl.add table i index in
1035 ((Cic.Meta (index, l))::newargs, index+1)
1036 | _ -> assert false)
1037 args ([], newmeta+1)
1041 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1046 (fun (i, context, term) menv ->
1048 let index = Hashtbl.find table i in
1049 (index, context, term)::menv
1051 (i, context, term)::menv)
1055 and left = repl left
1056 and right = repl right in
1058 (metas_of_term left) @
1059 (metas_of_term right) @
1060 (metas_of_term ty) @ (metas_of_proof p) in
1061 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
1064 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1067 if List.length metas > 0 then
1068 let first = List.hd metas in
1069 (* this new equality might have less variables than its parents: here
1070 we fill the gap with a dummy arg. Example:
1071 with (f X Y) = X we can simplify
1074 So the new equation has only one variable, but it still has type like
1075 \lambda X,Y:..., so we need to pass a dummy arg for Y
1076 (I hope this makes some sense...)
1081 (function Cic.Meta (i, _) -> i = v | _ -> assert false)
1083 Hashtbl.replace table k first)
1084 (Hashtbl.copy table)
1086 let rec fix_proof = function
1087 | NoProof -> NoProof
1088 | BasicProof term -> BasicProof (repl term)
1089 | ProofBlock (subst, eq_URI, namety, bo, (pos, eq), p) ->
1094 | Cic.Meta (i, l) -> (
1096 let j = Hashtbl.find table i in
1097 if List.mem_assoc i subst then
1100 let _, context, ty = CicUtil.lookup_meta i menv in
1101 (i, (context, Cic.Meta (j, l), ty))::s
1102 with Not_found | CicUtil.Meta_not_found _ ->
1105 | _ -> assert false)
1108 ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1111 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1116 let relocate newmeta menv =
1117 let subst, metasenv, newmeta =
1119 (fun (i, context, ty) (subst, menv, maxmeta) ->
1120 let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1121 let newsubst = (i, (context, (Cic.Meta (maxmeta, irl)), ty)) in
1122 let newmeta = maxmeta, context, ty in
1123 newsubst::subst, newmeta::menv, maxmeta+1)
1124 menv ([], [], newmeta+1)
1126 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1129 (fun (i, (context, term, ty)) ->
1130 let context = CicMetaSubst.apply_subst_context subst context in
1131 let term = CicMetaSubst.apply_subst subst term in
1132 let ty = CicMetaSubst.apply_subst subst ty in
1133 (i, (context, term, ty))) subst in
1134 subst, metasenv, newmeta
1137 let fix_metas newmeta (w, p, (ty, left, right, o), menv) =
1139 let metas = (metas_of_term left)@(metas_of_term right)
1140 @(metas_of_term ty)@(metas_of_proof p) in
1141 let menv = List.filter (fun (i, _, _) -> List.mem i metas) menv in
1145 fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) in
1146 prerr_endline (string_of_equality eq); *)
1147 let subst, metasenv, newmeta = relocate newmeta menv in
1148 let ty = CicMetaSubst.apply_subst subst ty in
1149 let left = CicMetaSubst.apply_subst subst left in
1150 let right = CicMetaSubst.apply_subst subst right in
1151 let rec fix_proof = function
1152 | NoProof -> NoProof
1153 | BasicProof term -> BasicProof (CicMetaSubst.apply_subst subst term)
1154 | ProofBlock (subst', eq_URI, namety, bo, (pos, eq), p) ->
1158 (fun (i, (context, term, ty)) ->
1159 let context = CicMetaSubst.apply_subst_context subst context in
1160 let term = CicMetaSubst.apply_subst subst term in
1161 let ty = CicMetaSubst.apply_subst subst ty in
1162 (i, (context, term, ty))) subst' in *)
1163 ProofBlock (subst@subst', eq_URI, namety, bo, (pos, eq), p)
1166 let p = fix_proof p in
1168 let metas = (metas_of_term left)@(metas_of_term right)
1169 @(metas_of_term ty)@(metas_of_proof p) in
1170 let metasenv = List.filter (fun (i, _, _) -> List.mem i metas) metasenv in
1172 let eq = (w, p, (ty, left, right, o), metasenv) in
1173 (* debug prerr_endline (string_of_equality eq); *)
1176 let term_is_equality term =
1177 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1179 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1184 exception TermIsNotAnEquality;;
1186 let equality_of_term proof term =
1187 let eq_uri = LibraryObjects.eq_URI () in
1188 let iseq uri = UriManager.eq uri eq_uri in
1190 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1191 let o = !Utils.compare_terms t1 t2 in
1192 let stat = (ty,t1,t2,o) in
1193 let w = compute_equality_weight stat in
1194 let e = (w, BasicProof proof, stat, []) in
1197 raise TermIsNotAnEquality
1201 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1203 let is_weak_identity (metasenv, context, ugraph) = function
1204 | (_, _, (ty, left, right, _), menv) ->
1206 (meta_convertibility left right))
1207 (* the test below is not a good idea since it stops
1208 demodulation too early *)
1209 (* (fst (CicReduction.are_convertible
1210 ~metasenv:(metasenv @ menv) context left right ugraph)))*)
1213 let is_identity (metasenv, context, ugraph) = function
1214 | (_, _, (ty, left, right, _), menv) ->
1216 (* (meta_convertibility left right)) *)
1217 (fst (CicReduction.are_convertible
1218 ~metasenv:(metasenv @ menv) context left right ugraph)))
1222 let term_of_equality equality =
1223 let _, _, (ty, left, right, _), menv = equality in
1224 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1225 let argsno = List.length menv in
1227 CicSubstitution.lift argsno
1228 (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
1232 (fun (i,_,ty) (n, t) ->
1233 let name = Cic.Name ("X" ^ (string_of_int n)) in
1234 let ty = CicSubstitution.lift (n-1) ty in
1236 ProofEngineReduction.replace
1237 ~equality:eq ~what:[i]
1238 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1240 (n-1, Cic.Prod (name, ty, t)))