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/.
31 let metas_of_proof_time = ref 0.;;
32 let metas_of_term_time = ref 0.;;
34 type substitution = Cic.substitution
35 let apply_subst = CicMetaSubst.apply_subst
36 let apply_subst_metasenv = CicMetaSubst.apply_subst_metasenv
37 let ppsubst = CicMetaSubst.ppsubst
38 let buildsubst n context t ty = (n,(context,t,ty))
39 let flatten_subst subst =
41 (fun (i, (context, term, ty)) ->
42 let context = (*` apply_subst_context subst*) context in
43 let term = apply_subst subst term in
44 let ty = apply_subst subst ty in
45 (i, (context, term, ty))) subst
46 let rec lookup_subst meta subst =
48 | Cic.Meta (i, _) -> (
49 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst
50 in lookup_subst t subst
51 with Not_found -> meta
57 (* naif version of apply subst; the local context of metas is ignored;
58 we assume the substituted term must be lifted according to the nesting
59 depth of the meta. Alternatively, ee could used implicit instead of
63 type substitution = (int * Cic.term) list
65 let apply_subst subst term =
69 | Cic.Var (uri,exp_named_subst) ->
70 let exp_named_subst' =
71 List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
73 Cic.Var (uri, exp_named_subst')
76 aux k (CicSubstitution.lift k (List.assoc i subst))
80 | Cic.Cast (te,ty) -> Cic.Cast (aux k te, aux k ty)
81 | Cic.Prod (n,s,t) -> Cic.Prod (n, aux k s, aux (k+1) t)
82 | Cic.Lambda (n,s,t) -> Cic.Lambda (n, aux k s, aux (k+1) t)
83 | Cic.LetIn (n,s,t) -> Cic.LetIn (n, aux k s, aux (k+1) t)
84 | Cic.Appl [] -> assert false
85 | Cic.Appl l -> Cic.Appl (List.map (aux k) l)
86 | Cic.Const (uri,exp_named_subst) ->
87 let exp_named_subst' =
88 List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
90 if exp_named_subst' != exp_named_subst then
91 Cic.Const (uri, exp_named_subst')
93 t (* TODO: provare a mantenere il piu' possibile sharing *)
94 | Cic.MutInd (uri,typeno,exp_named_subst) ->
95 let exp_named_subst' =
96 List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
98 Cic.MutInd (uri,typeno,exp_named_subst')
99 | Cic.MutConstruct (uri,typeno,consno,exp_named_subst) ->
100 let exp_named_subst' =
101 List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
103 Cic.MutConstruct (uri,typeno,consno,exp_named_subst')
104 | Cic.MutCase (sp,i,outty,t,pl) ->
105 let pl' = List.map (aux k) pl in
106 Cic.MutCase (sp, i, aux k outty, aux k t, pl')
108 let len = List.length fl in
111 (fun (name, i, ty, bo) -> (name, i, aux k ty, aux (k+len) bo)) fl
114 | Cic.CoFix (i, fl) ->
115 let len = List.length fl in
117 List.map (fun (name, ty, bo) -> (name, aux k ty, aux (k+len) bo)) fl
124 (* naif version of apply_subst_metasenv: we do not apply the
125 substitution to the context *)
127 let apply_subst_metasenv subst metasenv =
129 (fun (n, context, ty) ->
130 (n, context, apply_subst subst ty))
132 (fun (i, _, _) -> not (List.mem_assoc i subst))
139 sprintf "%d:= %s" idx (CicPp.ppterm t))
143 let buildsubst n context t ty = (n,t) ;;
145 let flatten_subst subst =
146 List.map (fun (i,t) -> i, apply_subst subst t ) subst
149 let rec lookup_subst meta subst =
153 lookup_subst (List.assoc i subst) subst
162 (Cic.term * (* type *)
163 Cic.term * (* left side *)
164 Cic.term * (* right side *)
165 Utils.comparison) * (* ordering *)
166 Cic.metasenv (* environment for metas *)
169 | NoProof (* term is the goal missing a proof *)
170 | BasicProof of substitution * Cic.term
172 substitution * UriManager.uri *
173 (Cic.name * Cic.term) * Cic.term * (Utils.pos * equality) * proof
174 | ProofGoalBlock of proof * proof
175 | ProofSymBlock of Cic.term list * proof
176 | SubProof of Cic.term * int * proof
179 let string_of_equality ?env =
183 | w, _, (ty, left, right, o), _ ->
184 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
185 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
187 | Some (_, context, _) -> (
188 let names = names_of_context context in
190 | w, _, (ty, left, right, o), _ ->
191 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
192 (CicPp.pp left names) (string_of_comparison o)
193 (CicPp.pp right names)
198 let rec string_of_proof = function
199 | NoProof -> "NoProof "
200 | BasicProof (s, t) -> "BasicProof " ^
201 (CicPp.ppterm (apply_subst s t))
202 | SubProof (t, i, p) ->
203 Printf.sprintf "SubProof(%s, %s, %s)"
204 (CicPp.ppterm t) (string_of_int i) (string_of_proof p)
205 | ProofSymBlock _ -> "ProofSymBlock"
206 | ProofBlock (subst, _, _, _ ,_,_) ->
207 "ProofBlock" ^ (ppsubst subst)
208 | ProofGoalBlock (p1, p2) ->
209 Printf.sprintf "ProofGoalBlock(%s, %s)"
210 (string_of_proof p1) (string_of_proof p2)
214 let check_disjoint_invariant subst metasenv msg =
216 (fun (i,_,_) -> (List.exists (fun (j,_) -> i=j) subst)) metasenv)
219 prerr_endline ("not disjoint: " ^ msg);
224 (* filter out from metasenv the variables in substs *)
225 let filter subst metasenv =
228 try let _ = List.find (fun (i, _) -> m = i) subst in false
229 with Not_found -> true)
233 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
234 let build_ens_for_sym_eq sym_eq_URI termlist =
235 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph sym_eq_URI in
237 | Cic.Constant (_, _, _, uris, _) ->
238 assert (List.length uris <= List.length termlist);
239 let rec aux = function
241 | (uri::uris), (term::tl) ->
242 let ens, args = aux (uris, tl) in
243 (uri, term)::ens, args
244 | _, _ -> assert false
251 let build_proof_term ?(noproof=Cic.Implicit None) proof =
252 let rec do_build_proof proof =
255 Printf.fprintf stderr "WARNING: no proof!\n";
257 | BasicProof (s,term) -> apply_subst s term
258 | ProofGoalBlock (proofbit, proof) ->
259 print_endline "found ProofGoalBlock, going up...";
260 do_build_goal_proof proofbit proof
261 | ProofSymBlock (termlist, proof) ->
262 let proof = do_build_proof proof in
263 let ens, args = build_ens_for_sym_eq (Utils.sym_eq_URI ()) termlist in
264 Cic.Appl ([Cic.Const (Utils.sym_eq_URI (), ens)] @ args @ [proof])
265 | ProofBlock (subst, eq_URI, (name, ty), bo, (pos, eq), eqproof) ->
266 let t' = Cic.Lambda (name, ty, bo) in
268 let _, proof', _, _ = eq in
269 do_build_proof proof'
271 let eqproof = do_build_proof eqproof in
272 let _, _, (ty, what, other, _), menv' = eq in
274 if pos = Utils.Left then what, other else other, what
277 (Cic.Appl [Cic.Const (eq_URI, []); ty;
278 what; t'; eqproof; other; proof'])
279 | SubProof (term, meta_index, proof) ->
280 let proof = do_build_proof proof in
282 | Cic.Meta (j, _) -> i = j
285 ProofEngineReduction.replace
286 ~equality:eq ~what:[meta_index] ~with_what:[proof] ~where:term
288 and do_build_goal_proof proofbit proof =
290 | ProofGoalBlock (pb, p) ->
291 do_build_proof (ProofGoalBlock (replace_proof proofbit pb, p))
292 | _ -> do_build_proof (replace_proof proofbit proof)
294 and replace_proof newproof = function
295 | ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof) ->
296 let eqproof' = replace_proof newproof eqproof in
297 ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof')
298 | ProofGoalBlock (pb, p) ->
299 let pb' = replace_proof newproof pb in
300 ProofGoalBlock (pb', p)
301 | BasicProof _ -> newproof
302 | SubProof (term, meta_index, p) ->
303 SubProof (term, meta_index, replace_proof newproof p)
309 let rec metas_of_term = function
310 | Cic.Meta (i, c) -> [i]
313 | Cic.MutInd (_, _, ens)
314 | Cic.MutConstruct (_, _, _, ens) ->
315 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
318 | Cic.Lambda (_, s, t)
319 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
320 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
321 | Cic.MutCase (uri, i, s, t, l) ->
322 (metas_of_term s) @ (metas_of_term t) @
323 (List.flatten (List.map metas_of_term l))
326 (List.map (fun (s, i, t1, t2) ->
327 (metas_of_term t1) @ (metas_of_term t2)) il)
328 | Cic.CoFix (i, il) ->
330 (List.map (fun (s, t1, t2) ->
331 (metas_of_term t1) @ (metas_of_term t2)) il)
335 let rec metas_of_proof p =
337 let t1 = Unix.gettimeofday () in
338 let res = metas_of_term (build_proof_term p) in
339 let t2 = Unix.gettimeofday () in
340 metas_of_proof_time := !metas_of_proof_time +. (t2 -. t1);
343 metas_of_term (build_proof_term p)
347 exception NotMetaConvertible;;
349 let meta_convertibility_aux table t1 t2 =
350 let module C = Cic in
351 let rec aux ((table_l, table_r) as table) t1 t2 =
353 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
354 let m1_binding, table_l =
355 try List.assoc m1 table_l, table_l
356 with Not_found -> m2, (m1, m2)::table_l
357 and m2_binding, table_r =
358 try List.assoc m2 table_r, table_r
359 with Not_found -> m1, (m2, m1)::table_r
361 if (m1_binding <> m2) || (m2_binding <> m1) then
362 raise NotMetaConvertible
368 | None, Some _ | Some _, None -> raise NotMetaConvertible
370 | Some t1, Some t2 -> (aux res t1 t2))
371 (table_l, table_r) tl1 tl2
372 with Invalid_argument _ ->
373 raise NotMetaConvertible
375 | C.Var (u1, ens1), C.Var (u2, ens2)
376 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
377 aux_ens table ens1 ens2
378 | C.Cast (s1, t1), C.Cast (s2, t2)
379 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
380 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
381 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
382 let table = aux table s1 s2 in
384 | C.Appl l1, C.Appl l2 -> (
385 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
386 with Invalid_argument _ -> raise NotMetaConvertible
388 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
389 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
390 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
391 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
392 aux_ens table ens1 ens2
393 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
394 when (UriManager.eq u1 u2) && i1 = i2 ->
395 let table = aux table s1 s2 in
396 let table = aux table t1 t2 in (
397 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
398 with Invalid_argument _ -> raise NotMetaConvertible
400 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
403 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
404 if i1 <> i2 then raise NotMetaConvertible
406 let res = (aux res s1 s2) in aux res t1 t2)
408 with Invalid_argument _ -> raise NotMetaConvertible
410 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
413 (fun res (n1, s1, t1) (n2, s2, t2) ->
414 let res = aux res s1 s2 in aux res t1 t2)
416 with Invalid_argument _ -> raise NotMetaConvertible
418 | t1, t2 when t1 = t2 -> table
419 | _, _ -> raise NotMetaConvertible
421 and aux_ens table ens1 ens2 =
422 let cmp (u1, t1) (u2, t2) =
423 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
425 let ens1 = List.sort cmp ens1
426 and ens2 = List.sort cmp ens2 in
429 (fun res (u1, t1) (u2, t2) ->
430 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
433 with Invalid_argument _ -> raise NotMetaConvertible
439 let meta_convertibility_eq eq1 eq2 =
440 let _, _, (ty, left, right, _), _ = eq1
441 and _, _, (ty', left', right', _), _ = eq2 in
444 else if (left = left') && (right = right') then
446 else if (left = right') && (right = left') then
450 let table = meta_convertibility_aux ([], []) left left' in
451 let _ = meta_convertibility_aux table right right' in
453 with NotMetaConvertible ->
455 let table = meta_convertibility_aux ([], []) left right' in
456 let _ = meta_convertibility_aux table right left' in
458 with NotMetaConvertible ->
463 let meta_convertibility t1 t2 =
468 ignore(meta_convertibility_aux ([], []) t1 t2);
470 with NotMetaConvertible ->
475 let rec check_irl start = function
477 | None::tl -> check_irl (start+1) tl
478 | (Some (Cic.Rel x))::tl ->
479 if x = start then check_irl (start+1) tl else false
484 let rec is_simple_term = function
485 | Cic.Appl ((Cic.Meta _)::_) -> false
486 | Cic.Appl l -> List.for_all is_simple_term l
487 | Cic.Meta (i, l) -> check_irl 1 l
489 | Cic.Const _ -> true
490 | Cic.MutInd (_, _, []) -> true
491 | Cic.MutConstruct (_, _, _, []) -> true
496 List.exists (fun (j,_,_) -> i = j) menv
499 let unification_simple locked_menv metasenv context t1 t2 ugraph =
500 let debug_print x = prerr_endline (Lazy.force x) in
501 let module C = Cic in
502 let module M = CicMetaSubst in
503 let module U = CicUnification in
504 let lookup = lookup_subst in
505 let rec occurs_check subst what where =
507 | t when what = t -> true
508 | C.Appl l -> List.exists (occurs_check subst what) l
510 let t = lookup where subst in
511 if t <> where then occurs_check subst what t else false
514 let rec unif subst menv s t =
515 let s = match s with C.Meta _ -> lookup s subst | _ -> s
516 and t = match t with C.Meta _ -> lookup t subst | _ -> t
520 | s, t when s = t -> subst, menv
521 | C.Meta (i, _), C.Meta (j, _)
522 when (locked locked_menv i) &&(locked locked_menv j) ->
524 (U.UnificationFailure (lazy "Inference.unification.unif"))
525 | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->
527 | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
529 | C.Meta _, t when occurs_check subst s t ->
531 (U.UnificationFailure (lazy "Inference.unification.unif"))
532 | C.Meta (i, l), t when (locked locked_menv i) ->
534 (U.UnificationFailure (lazy "Inference.unification.unif"))
535 | C.Meta (i, l), t -> (
537 let _, _, ty = CicUtil.lookup_meta i menv in
538 assert (not (List.mem_assoc i subst));
539 let subst = (buildsubst i context t ty)::subst in
540 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
542 with CicUtil.Meta_not_found m ->
543 let names = names_of_context context in
545 (lazy*) prerr_endline
546 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
547 (CicPp.pp t1 names) (CicPp.pp t2 names)
548 (print_metasenv menv) (print_metasenv metasenv));
551 | _, C.Meta _ -> unif subst menv t s
552 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
553 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
554 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
557 (fun (subst', menv) s t -> unif subst' menv s t)
558 (subst, menv) tls tlt
559 with Invalid_argument _ ->
560 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
563 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
565 let subst, menv = unif [] metasenv t1 t2 in
566 let menv = filter subst menv in
567 List.rev subst, menv, ugraph
570 let profiler = HExtlib.profile "P/Inference.unif_simple[flatten]"
571 let profiler2 = HExtlib.profile "P/Inference.unif_simple[flatten_fast]"
572 let profiler3 = HExtlib.profile "P/Inference.unif_simple[resolve_meta]"
573 let profiler4 = HExtlib.profile "P/Inference.unif_simple[filter]"
575 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
576 let metasenv = metasenv1 @ metasenv2 in
577 let subst, menv, ug =
578 if not (is_simple_term t1) || not (is_simple_term t2) then (
581 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
582 (CicPp.ppterm t1) (CicPp.ppterm t2)));
583 raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
586 (* full unification *)
587 unification_simple [] metasenv context t1 t2 ugraph
589 (* matching: metasenv1 is locked *)
590 unification_simple metasenv1 metasenv context t1 t2 ugraph
592 if Utils.debug_res then
593 ignore(check_disjoint_invariant subst menv "unif");
594 (* let flatten subst =
596 (fun (i, (context, term, ty)) ->
597 let context = apply_subst_context subst context in
598 let term = apply_subst subst term in
599 let ty = apply_subst subst ty in
600 (i, (context, term, ty))) subst
602 let flatten subst = profiler.HExtlib.profile flatten subst in
603 let subst = flatten subst in *)
607 exception MatchingFailure;;
609 let matching1 metasenv1 metasenv2 context t1 t2 ugraph =
611 unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
613 CicUnification .UnificationFailure _ ->
614 raise MatchingFailure
617 let unification = unification_aux true
620 (** matching takes in input the _disjoint_ metasenv of t1 and t2;
621 it perform unification in the union metasenv, then check that
622 the first metasenv has not changed *)
624 let matching = matching1;;
626 let check_eq context msg eq =
627 let w, proof, (eq_ty, left, right, order), metas = eq in
628 if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
629 (fst (CicTypeChecker.type_of_aux' metas context left CicUniv.empty_ugraph))
630 CicUniv.empty_ugraph))
639 let find_equalities context proof =
640 let module C = Cic in
641 let module S = CicSubstitution in
642 let module T = CicTypeChecker in
643 let eq_uri = LibraryObjects.eq_URI () in
644 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
645 let ok_types ty menv =
646 List.for_all (fun (_, _, mt) -> mt = ty) menv
648 let rec aux index newmeta = function
650 | (Some (_, C.Decl (term)))::tl ->
651 let do_find context term =
653 | C.Prod (name, s, t) ->
654 let (head, newmetas, args, newmeta) =
655 ProofEngineHelpers.saturate_term newmeta []
656 context (S.lift index term) 0
659 if List.length args = 0 then
662 C.Appl ((C.Rel index)::args)
665 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
666 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
669 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
670 let o = !Utils.compare_terms t1 t2 in
671 let stat = (ty,t1,t2,o) in
672 let w = compute_equality_weight stat in
673 let proof = BasicProof ([],p) in
674 let e = (w, proof, stat, newmetas) in
678 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
679 when UriManager.eq uri eq_uri ->
680 let ty = S.lift index ty in
681 let t1 = S.lift index t1 in
682 let t2 = S.lift index t2 in
683 let o = !Utils.compare_terms t1 t2 in
684 let stat = (ty,t1,t2,o) in
685 let w = compute_equality_weight stat in
686 let e = (w, BasicProof ([],(C.Rel index)), stat, []) in
690 match do_find context term with
692 let tl, newmeta' = (aux (index+1) newmeta tl) in
693 if newmeta' < newmeta then
694 prerr_endline "big trouble";
695 (index, p)::tl, newmeta' (* max???? *)
697 aux (index+1) newmeta tl
700 aux (index+1) newmeta tl
702 let il, maxm = aux 1 newmeta context in
703 let indexes, equalities = List.split il in
704 ignore (List.iter (check_eq context "find") equalities);
705 indexes, equalities, maxm
710 let equations_blacklist =
712 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
713 UriManager.UriSet.empty [
714 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
715 "cic:/Coq/Init/Logic/trans_eq.con";
716 "cic:/Coq/Init/Logic/f_equal.con";
717 "cic:/Coq/Init/Logic/f_equal2.con";
718 "cic:/Coq/Init/Logic/f_equal3.con";
719 "cic:/Coq/Init/Logic/f_equal4.con";
720 "cic:/Coq/Init/Logic/f_equal5.con";
721 "cic:/Coq/Init/Logic/sym_eq.con";
722 "cic:/Coq/Init/Logic/eq_ind.con";
723 "cic:/Coq/Init/Logic/eq_ind_r.con";
724 "cic:/Coq/Init/Logic/eq_rec.con";
725 "cic:/Coq/Init/Logic/eq_rec_r.con";
726 "cic:/Coq/Init/Logic/eq_rect.con";
727 "cic:/Coq/Init/Logic/eq_rect_r.con";
728 "cic:/Coq/Logic/Eqdep/UIP.con";
729 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
730 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
731 "cic:/Coq/ZArith/Zcompare/rename.con";
732 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
733 perche' questo cacchio di teorema rompe le scatole :'( *)
734 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
736 "cic:/matita/logic/equality/eq_f.con";
737 "cic:/matita/logic/equality/eq_f2.con";
738 "cic:/matita/logic/equality/eq_rec.con";
739 "cic:/matita/logic/equality/eq_rect.con";
743 let equations_blacklist = UriManager.UriSet.empty;;
746 let find_library_equalities dbd context status maxmeta =
747 let module C = Cic in
748 let module S = CicSubstitution in
749 let module T = CicTypeChecker in
752 (fun s u -> UriManager.UriSet.add u s)
754 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
760 if UriManager.UriSet.mem uri blacklist then
763 let t = CicUtil.term_of_uri uri in
765 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
769 (let t1 = Unix.gettimeofday () in
770 let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
771 let t2 = Unix.gettimeofday () in
774 (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
778 let eq_uri1 = eq_XURI ()
779 and eq_uri2 = LibraryObjects.eq_URI () in
781 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
783 let ok_types ty menv =
784 List.for_all (fun (_, _, mt) -> mt = ty) menv
786 let rec has_vars = function
787 | C.Meta _ | C.Rel _ | C.Const _ -> false
789 | C.Appl l -> List.exists has_vars l
790 | C.Prod (_, s, t) | C.Lambda (_, s, t)
791 | C.LetIn (_, s, t) | C.Cast (s, t) ->
792 (has_vars s) || (has_vars t)
795 let rec aux newmeta = function
797 | (uri, term, termty)::tl ->
800 (Printf.sprintf "Examining: %s (%s)"
801 (CicPp.ppterm term) (CicPp.ppterm termty)));
804 | C.Prod (name, s, t) when not (has_vars termty) ->
805 let head, newmetas, args, newmeta =
806 ProofEngineHelpers.saturate_term newmeta [] context termty 0
809 if List.length args = 0 then
815 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
816 when (iseq uri) && (ok_types ty newmetas) ->
819 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
820 let o = !Utils.compare_terms t1 t2 in
821 let stat = (ty,t1,t2,o) in
822 let w = compute_equality_weight stat in
823 let proof = BasicProof ([],p) in
824 let e = (w, proof, stat, newmetas) in
828 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
829 when iseq uri && not (has_vars termty) ->
830 let o = !Utils.compare_terms t1 t2 in
831 let stat = (ty,t1,t2,o) in
832 let w = compute_equality_weight stat in
833 let e = (w, BasicProof ([],term), stat, []) in
839 let tl, newmeta' = aux newmeta tl in
840 if newmeta' < newmeta then
841 prerr_endline "big trouble";
842 (uri, e)::tl, newmeta' (* max???? *)
846 let found, maxm = aux maxmeta candidates in
849 (fun (s, l) (u, e) ->
850 if List.exists (meta_convertibility_eq e) (List.map snd l) then (
853 (Printf.sprintf "NO!! %s already there!"
854 (string_of_equality e)));
855 (UriManager.UriSet.add u s, l)
856 ) else (UriManager.UriSet.add u s, (u, e)::l))
857 (UriManager.UriSet.empty, []) found)
863 let find_library_theorems dbd env status equalities_uris =
864 let module C = Cic in
865 let module S = CicSubstitution in
866 let module T = CicTypeChecker in
869 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
871 UriManager.UriSet.remove refl_equal
872 (UriManager.UriSet.union equalities_uris equations_blacklist)
875 (fun s u -> UriManager.UriSet.add u s)
876 s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
879 let metasenv, context, ugraph = env in
883 if UriManager.UriSet.mem uri blacklist then l
885 let t = CicUtil.term_of_uri uri in
886 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
888 [] (MetadataQuery.signature_of_goal ~dbd status)
891 let u = eq_XURI () in
892 let t = CicUtil.term_of_uri u in
893 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
896 refl_equal::candidates
900 let find_context_hypotheses env equalities_indexes =
901 let metasenv, context, ugraph = env in
908 if List.mem n equalities_indexes then
913 CicTypeChecker.type_of_aux' metasenv context t ugraph in
914 (n+1, (t, ty, [])::l))
921 let fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) =
922 let table = Hashtbl.create (List.length args) in
924 let newargs, newmeta =
926 (fun t (newargs, index) ->
929 if Hashtbl.mem table i then
930 let idx = Hashtbl.find table i in
931 ((Cic.Meta (idx, l))::newargs, index+1)
933 let _ = Hashtbl.add table i index in
934 ((Cic.Meta (index, l))::newargs, index+1)
940 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
945 (fun (i, context, term) menv ->
947 let index = Hashtbl.find table i in
948 (index, context, term)::menv
950 (i, context, term)::menv)
955 and right = repl right in
957 (metas_of_term left) @
958 (metas_of_term right) @
959 (metas_of_term ty) @ (metas_of_proof p) in
960 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
963 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
966 if List.length metas > 0 then
967 let first = List.hd metas in
968 (* this new equality might have less variables than its parents: here
969 we fill the gap with a dummy arg. Example:
970 with (f X Y) = X we can simplify
973 So the new equation has only one variable, but it still has type like
974 \lambda X,Y:..., so we need to pass a dummy arg for Y
975 (I hope this makes some sense...)
980 (function Cic.Meta (i, _) -> i = v | _ -> assert false)
982 Hashtbl.replace table k first)
985 let rec fix_proof = function
987 | BasicProof term -> BasicProof (repl term)
988 | ProofBlock (subst, eq_URI, namety, bo, (pos, eq), p) ->
993 | Cic.Meta (i, l) -> (
995 let j = Hashtbl.find table i in
996 if List.mem_assoc i subst then
999 let _, context, ty = CicUtil.lookup_meta i menv in
1000 (i, (context, Cic.Meta (j, l), ty))::s
1001 with Not_found | CicUtil.Meta_not_found _ ->
1004 | _ -> assert false)
1007 ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1010 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1015 let relocate newmeta menv =
1016 let subst, metasenv, newmeta =
1018 (fun (i, context, ty) (subst, menv, maxmeta) ->
1020 CicMkImplicit.identity_relocation_list_for_metavariable context *)
1022 let newsubst = buildsubst i context (Cic.Meta(maxmeta,irl)) ty in
1023 let newmeta = maxmeta, context, ty in
1024 newsubst::subst, newmeta::menv, maxmeta+1)
1025 menv ([], [], newmeta+1)
1027 let metasenv = apply_subst_metasenv subst metasenv in
1028 let subst = flatten_subst subst in
1029 subst, metasenv, newmeta
1032 let fix_metas newmeta (w, p, (ty, left, right, o), menv) =
1034 let metas = (metas_of_term left)@(metas_of_term right)
1035 @(metas_of_term ty)@(metas_of_proof p) in
1036 let menv = List.filter (fun (i, _, _) -> List.mem i metas) menv in
1040 fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) in
1041 prerr_endline (string_of_equality eq); *)
1042 let subst, metasenv, newmeta = relocate newmeta menv in
1044 if newmeta > 2839 then
1046 prerr_endline (CicPp.ppterm left ^ " = " ^ CicPp.ppterm right);
1047 prerr_endline (CicMetaSubst.ppsubst subst);
1048 prerr_endline (CicMetaSubst.ppmetasenv [] metasenv);
1052 let ty = apply_subst subst ty in
1053 let left = apply_subst subst left in
1054 let right = apply_subst subst right in
1055 let fix_proof = function
1056 | NoProof -> NoProof
1057 | BasicProof (subst',term) -> BasicProof (subst@subst',term)
1058 | ProofBlock (subst', eq_URI, namety, bo, (pos, eq), p) ->
1062 (fun (i, (context, term, ty)) ->
1063 let context = apply_subst_context subst context in
1064 let term = apply_subst subst term in
1065 let ty = apply_subst subst ty in
1066 (i, (context, term, ty))) subst' in *)
1067 ProofBlock (subst@subst', eq_URI, namety, bo, (pos, eq), p)
1070 let p = fix_proof p in
1072 let metas = (metas_of_term left)@(metas_of_term right)
1073 @(metas_of_term ty)@(metas_of_proof p) in
1074 let metasenv = List.filter (fun (i, _, _) -> List.mem i metas) metasenv in
1076 let eq = (w, p, (ty, left, right, o), metasenv) in
1077 (* debug prerr_endline (string_of_equality eq); *)
1080 let term_is_equality term =
1081 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1083 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1088 exception TermIsNotAnEquality;;
1090 let equality_of_term proof term =
1091 let eq_uri = LibraryObjects.eq_URI () in
1092 let iseq uri = UriManager.eq uri eq_uri in
1094 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1095 let o = !Utils.compare_terms t1 t2 in
1096 let stat = (ty,t1,t2,o) in
1097 let w = compute_equality_weight stat in
1098 let e = (w, BasicProof ([],proof), stat, []) in
1101 raise TermIsNotAnEquality
1105 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1107 let is_weak_identity (metasenv, context, ugraph) = function
1108 | (_, _, (ty, left, right, _), menv) ->
1110 (meta_convertibility left right))
1111 (* the test below is not a good idea since it stops
1112 demodulation too early *)
1113 (* (fst (CicReduction.are_convertible
1114 ~metasenv:(metasenv @ menv) context left right ugraph)))*)
1117 let is_identity (metasenv, context, ugraph) = function
1118 | (_, _, (ty, left, right, _), menv) ->
1120 (* (meta_convertibility left right)) *)
1121 (fst (CicReduction.are_convertible
1122 ~metasenv:(metasenv @ menv) context left right ugraph)))
1126 let term_of_equality equality =
1127 let _, _, (ty, left, right, _), menv = equality in
1128 let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1129 let argsno = List.length menv in
1131 CicSubstitution.lift argsno
1132 (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
1136 (fun (i,_,ty) (n, t) ->
1137 let name = Cic.Name ("X" ^ (string_of_int n)) in
1138 let ty = CicSubstitution.lift (n-1) ty in
1140 ProofEngineReduction.replace
1141 ~equality:eq ~what:[i]
1142 ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1144 (n-1, Cic.Prod (name, ty, t)))