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 check_disjoint_invariant subst metasenv msg =
33 (fun (i,_,_) -> (Subst.is_in_subst i subst)) metasenv)
36 prerr_endline ("not disjoint: " ^ msg);
41 let rec check_irl start = function
43 | None::tl -> check_irl (start+1) tl
44 | (Some (Cic.Rel x))::tl ->
45 if x = start then check_irl (start+1) tl else false
49 let rec is_simple_term = function
50 | Cic.Appl ((Cic.Meta _)::_) -> false
51 | Cic.Appl l -> List.for_all is_simple_term l
52 | Cic.Meta (i, l) -> check_irl 1 l
55 | Cic.MutInd (_, _, []) -> true
56 | Cic.MutConstruct (_, _, _, []) -> true
61 List.exists (fun (j,_,_) -> i = j) menv
64 let unification_simple locked_menv metasenv context t1 t2 ugraph =
66 let module M = CicMetaSubst in
67 let module U = CicUnification in
68 let lookup = Subst.lookup_subst in
69 let rec occurs_check subst what where =
71 | t when what = t -> true
72 | C.Appl l -> List.exists (occurs_check subst what) l
74 let t = lookup where subst in
75 if t <> where then occurs_check subst what t else false
78 let rec unif subst menv s t =
79 let s = match s with C.Meta _ -> lookup s subst | _ -> s
80 and t = match t with C.Meta _ -> lookup t subst | _ -> t
84 | s, t when s = t -> subst, menv
85 | C.Meta (i, _), C.Meta (j, _)
86 when (locked locked_menv i) &&(locked locked_menv j) ->
88 (U.UnificationFailure (lazy "Inference.unification.unif"))
89 | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->
91 | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
93 | C.Meta _, t when occurs_check subst s t ->
95 (U.UnificationFailure (lazy "Inference.unification.unif"))
96 | C.Meta (i, l), t when (locked locked_menv i) ->
98 (U.UnificationFailure (lazy "Inference.unification.unif"))
99 | C.Meta (i, l), t -> (
101 let _, _, ty = CicUtil.lookup_meta i menv in
102 assert (not (Subst.is_in_subst i subst));
103 let subst = Subst.buildsubst i context t ty subst in
104 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
106 with CicUtil.Meta_not_found m ->
107 let names = names_of_context context in
109 (lazy*) prerr_endline
110 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
111 (CicPp.pp t1 names) (CicPp.pp t2 names)
112 (print_metasenv menv) (print_metasenv metasenv));
115 | _, C.Meta _ -> unif subst menv t s
116 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
117 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
118 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
121 (fun (subst', menv) s t -> unif subst' menv s t)
122 (subst, menv) tls tlt
123 with Invalid_argument _ ->
124 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
127 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
129 let subst, menv = unif Subst.empty_subst metasenv t1 t2 in
130 let menv = Subst.filter subst menv in
134 let profiler = HExtlib.profile "P/Inference.unif_simple[flatten]"
135 let profiler2 = HExtlib.profile "P/Inference.unif_simple[flatten_fast]"
136 let profiler3 = HExtlib.profile "P/Inference.unif_simple[resolve_meta]"
137 let profiler4 = HExtlib.profile "P/Inference.unif_simple[filter]"
139 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
140 let metasenv = metasenv1 @ metasenv2 in
141 let subst, menv, ug =
142 if not (is_simple_term t1) || not (is_simple_term t2) then (
145 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
146 (CicPp.ppterm t1) (CicPp.ppterm t2)));
147 raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
150 (* full unification *)
151 unification_simple [] metasenv context t1 t2 ugraph
153 (* matching: metasenv1 is locked *)
154 unification_simple metasenv1 metasenv context t1 t2 ugraph
156 if Utils.debug_res then
157 ignore(check_disjoint_invariant subst menv "unif");
158 (* let flatten subst =
160 (fun (i, (context, term, ty)) ->
161 let context = apply_subst_context subst context in
162 let term = apply_subst subst term in
163 let ty = apply_subst subst ty in
164 (i, (context, term, ty))) subst
166 let flatten subst = profiler.HExtlib.profile flatten subst in
167 let subst = flatten subst in *)
171 exception MatchingFailure;;
173 let matching1 metasenv1 metasenv2 context t1 t2 ugraph =
175 unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
177 CicUnification .UnificationFailure _ ->
178 raise MatchingFailure
181 let unification = unification_aux true
184 (** matching takes in input the _disjoint_ metasenv of t1 and t2;
185 it perform unification in the union metasenv, then check that
186 the first metasenv has not changed *)
188 let matching = matching1;;
190 let check_eq context msg eq =
191 let w, proof, (eq_ty, left, right, order), metas = eq in
192 if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
193 (fst (CicTypeChecker.type_of_aux' metas context left CicUniv.empty_ugraph))
194 CicUniv.empty_ugraph))
203 let find_equalities context proof =
204 let module C = Cic in
205 let module S = CicSubstitution in
206 let module T = CicTypeChecker in
207 let eq_uri = LibraryObjects.eq_URI () in
208 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
209 let ok_types ty menv =
210 List.for_all (fun (_, _, mt) -> mt = ty) menv
212 let rec aux index newmeta = function
214 | (Some (_, C.Decl (term)))::tl ->
215 let do_find context term =
217 | C.Prod (name, s, t) ->
218 let (head, newmetas, args, newmeta) =
219 ProofEngineHelpers.saturate_term newmeta []
220 context (S.lift index term) 0
223 if List.length args = 0 then
226 C.Appl ((C.Rel index)::args)
229 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
230 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
233 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
234 let o = !Utils.compare_terms t1 t2 in
235 let stat = (ty,t1,t2,o) in
236 let w = compute_equality_weight stat in
237 let proof = Equality.Exact p in
238 let e = Equality.mk_equality (w, proof, stat, newmetas) in
242 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
243 when UriManager.eq uri eq_uri ->
244 let ty = S.lift index ty in
245 let t1 = S.lift index t1 in
246 let t2 = S.lift index t2 in
247 let o = !Utils.compare_terms t1 t2 in
248 let stat = (ty,t1,t2,o) in
249 let w = compute_equality_weight stat in
250 let p = C.Rel index in
251 let proof = Equality.Exact p in
252 let e = Equality.mk_equality (w, proof,stat,[]) in
256 match do_find context term with
258 let tl, newmeta' = (aux (index+1) newmeta tl) in
259 if newmeta' < newmeta then
260 prerr_endline "big trouble";
261 (index, p)::tl, newmeta' (* max???? *)
263 aux (index+1) newmeta tl
266 aux (index+1) newmeta tl
268 let il, maxm = aux 1 newmeta context in
269 let indexes, equalities = List.split il in
270 (* ignore (List.iter (check_eq context "find") equalities); *)
271 indexes, equalities, maxm
276 let equations_blacklist =
278 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
279 UriManager.UriSet.empty [
280 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
281 "cic:/Coq/Init/Logic/trans_eq.con";
282 "cic:/Coq/Init/Logic/f_equal.con";
283 "cic:/Coq/Init/Logic/f_equal2.con";
284 "cic:/Coq/Init/Logic/f_equal3.con";
285 "cic:/Coq/Init/Logic/f_equal4.con";
286 "cic:/Coq/Init/Logic/f_equal5.con";
287 "cic:/Coq/Init/Logic/sym_eq.con";
288 "cic:/Coq/Init/Logic/eq_ind.con";
289 "cic:/Coq/Init/Logic/eq_ind_r.con";
290 "cic:/Coq/Init/Logic/eq_rec.con";
291 "cic:/Coq/Init/Logic/eq_rec_r.con";
292 "cic:/Coq/Init/Logic/eq_rect.con";
293 "cic:/Coq/Init/Logic/eq_rect_r.con";
294 "cic:/Coq/Logic/Eqdep/UIP.con";
295 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
296 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
297 "cic:/Coq/ZArith/Zcompare/rename.con";
298 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
299 perche' questo cacchio di teorema rompe le scatole :'( *)
300 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
302 "cic:/matita/logic/equality/eq_f.con";
303 "cic:/matita/logic/equality/eq_f2.con";
304 "cic:/matita/logic/equality/eq_rec.con";
305 "cic:/matita/logic/equality/eq_rect.con";
309 let equations_blacklist = UriManager.UriSet.empty;;
312 let find_library_equalities dbd context status maxmeta =
313 let module C = Cic in
314 let module S = CicSubstitution in
315 let module T = CicTypeChecker in
318 (fun s u -> UriManager.UriSet.add u s)
320 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
326 if UriManager.UriSet.mem uri blacklist then
329 let t = CicUtil.term_of_uri uri in
331 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
335 (let t1 = Unix.gettimeofday () in
336 let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
337 let t2 = Unix.gettimeofday () in
340 (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
344 let eq_uri1 = eq_XURI ()
345 and eq_uri2 = LibraryObjects.eq_URI () in
347 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
349 let ok_types ty menv =
350 List.for_all (fun (_, _, mt) -> mt = ty) menv
352 let rec has_vars = function
353 | C.Meta _ | C.Rel _ | C.Const _ -> false
355 | C.Appl l -> List.exists has_vars l
356 | C.Prod (_, s, t) | C.Lambda (_, s, t)
357 | C.LetIn (_, s, t) | C.Cast (s, t) ->
358 (has_vars s) || (has_vars t)
361 let rec aux newmeta = function
363 | (uri, term, termty)::tl ->
366 (Printf.sprintf "Examining: %s (%s)"
367 (CicPp.ppterm term) (CicPp.ppterm termty)));
370 | C.Prod (name, s, t) when not (has_vars termty) ->
371 let head, newmetas, args, newmeta =
372 ProofEngineHelpers.saturate_term newmeta [] context termty 0
375 if List.length args = 0 then
381 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
382 when (iseq uri) && (ok_types ty newmetas) ->
385 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
386 let o = !Utils.compare_terms t1 t2 in
387 let stat = (ty,t1,t2,o) in
388 let w = compute_equality_weight stat in
389 let proof = Equality.Exact p in
390 let e = Equality.mk_equality (w, proof, stat, newmetas) in
394 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
395 when iseq uri && not (has_vars termty) ->
396 let o = !Utils.compare_terms t1 t2 in
397 let stat = (ty,t1,t2,o) in
398 let w = compute_equality_weight stat in
399 let proof = Equality.Exact term in
400 let e = Equality.mk_equality (w, proof, stat, []) in
406 let tl, newmeta' = aux newmeta tl in
407 if newmeta' < newmeta then
408 prerr_endline "big trouble";
409 (uri, e)::tl, newmeta' (* max???? *)
413 let found, maxm = aux maxmeta candidates in
415 let mceq = Equality.meta_convertibility_eq in
417 (fun (s, l) (u, e) ->
418 if List.exists (mceq e) (List.map snd l) then (
421 (Printf.sprintf "NO!! %s already there!"
422 (Equality.string_of_equality e)));
423 (UriManager.UriSet.add u s, l)
424 ) else (UriManager.UriSet.add u s, (u, e)::l))
425 (UriManager.UriSet.empty, []) found)
431 let find_library_theorems dbd env status equalities_uris =
432 let module C = Cic in
433 let module S = CicSubstitution in
434 let module T = CicTypeChecker in
437 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
439 UriManager.UriSet.remove refl_equal
440 (UriManager.UriSet.union equalities_uris equations_blacklist)
443 (fun s u -> UriManager.UriSet.add u s)
444 s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
447 let metasenv, context, ugraph = env in
451 if UriManager.UriSet.mem uri blacklist then l
453 let t = CicUtil.term_of_uri uri in
454 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
456 [] (MetadataQuery.signature_of_goal ~dbd status)
459 let u = eq_XURI () in
460 let t = CicUtil.term_of_uri u in
461 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
464 refl_equal::candidates
468 let find_context_hypotheses env equalities_indexes =
469 let metasenv, context, ugraph = env in
476 if List.mem n equalities_indexes then
481 CicTypeChecker.type_of_aux' metasenv context t ugraph in
482 (n+1, (t, ty, [])::l))