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/.
26 let _profiler = <:profiler<_profiler>>;;
33 let check_disjoint_invariant subst metasenv msg =
35 (fun (i,_,_) -> (Subst.is_in_subst i subst)) metasenv)
38 prerr_endline ("not disjoint: " ^ msg);
43 let rec check_irl start = function
45 | None::tl -> check_irl (start+1) tl
46 | (Some (Cic.Rel x))::tl ->
47 if x = start then check_irl (start+1) tl else false
51 let rec is_simple_term = function
52 | Cic.Appl ((Cic.Meta _)::_) -> false
53 | Cic.Appl l -> List.for_all is_simple_term l
54 | Cic.Meta (i, l) -> check_irl 1 l
57 | Cic.MutInd (_, _, []) -> true
58 | Cic.MutConstruct (_, _, _, []) -> true
63 List.exists (fun (j,_,_) -> i = j) menv
66 let unification_simple locked_menv metasenv context t1 t2 ugraph =
68 let module M = CicMetaSubst in
69 let module U = CicUnification in
70 let lookup = Subst.lookup_subst in
71 let rec occurs_check subst what where =
73 | t when what = t -> true
74 | C.Appl l -> List.exists (occurs_check subst what) l
76 let t = lookup where subst in
77 if t <> where then occurs_check subst what t else false
80 let rec unif subst menv s t =
81 let s = match s with C.Meta _ -> lookup s subst | _ -> s
82 and t = match t with C.Meta _ -> lookup t subst | _ -> t
86 | s, t when s = t -> subst, menv
87 | C.Meta (i, _), C.Meta (j, _)
88 when (locked locked_menv i) &&(locked locked_menv j) ->
90 (U.UnificationFailure (lazy "Inference.unification.unif"))
91 | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->
93 | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
95 | C.Meta _, t when occurs_check subst s t ->
97 (U.UnificationFailure (lazy "Inference.unification.unif"))
98 | C.Meta (i, l), t when (locked locked_menv i) ->
100 (U.UnificationFailure (lazy "Inference.unification.unif"))
101 | C.Meta (i, l), t -> (
103 let _, _, ty = CicUtil.lookup_meta i menv in
104 assert (not (Subst.is_in_subst i subst));
105 let subst = Subst.buildsubst i context t ty subst in
106 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
108 with CicUtil.Meta_not_found m ->
109 let names = names_of_context context in
111 (lazy*) prerr_endline
112 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
113 (CicPp.pp t1 names) (CicPp.pp t2 names)
114 (print_metasenv menv) (print_metasenv metasenv));
117 | _, C.Meta _ -> unif subst menv t s
118 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
119 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
120 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
123 (fun (subst', menv) s t -> unif subst' menv s t)
124 (subst, menv) tls tlt
125 with Invalid_argument _ ->
126 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
129 raise (U.UnificationFailure (lazy "Inference.unification.unif"))
131 let subst, menv = unif Subst.empty_subst metasenv t1 t2 in
132 let menv = Subst.filter subst menv in
136 let profiler = HExtlib.profile "P/Inference.unif_simple[flatten]"
137 let profiler2 = HExtlib.profile "P/Inference.unif_simple[flatten_fast]"
138 let profiler3 = HExtlib.profile "P/Inference.unif_simple[resolve_meta]"
139 let profiler4 = HExtlib.profile "P/Inference.unif_simple[filter]"
141 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
142 let metasenv = metasenv1 @ metasenv2 in
143 let subst, menv, ug =
144 if not (is_simple_term t1) || not (is_simple_term t2) then (
147 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
148 (CicPp.ppterm t1) (CicPp.ppterm t2)));
149 raise (CicUnification.UnificationFailure (lazy "Inference.unification.unif"))
152 (* full unification *)
153 unification_simple [] metasenv context t1 t2 ugraph
155 (* matching: metasenv1 is locked *)
156 unification_simple metasenv1 metasenv context t1 t2 ugraph
158 if Utils.debug_res then
159 ignore(check_disjoint_invariant subst menv "unif");
160 (* let flatten subst =
162 (fun (i, (context, term, ty)) ->
163 let context = apply_subst_context subst context in
164 let term = apply_subst subst term in
165 let ty = apply_subst subst ty in
166 (i, (context, term, ty))) subst
168 let flatten subst = profiler.HExtlib.profile flatten subst in
169 let subst = flatten subst in *)
173 exception MatchingFailure;;
175 (** matching takes in input the _disjoint_ metasenv of t1 and t2;
176 it perform unification in the union metasenv, then check that
177 the first metasenv has not changed *)
178 let matching metasenv1 metasenv2 context t1 t2 ugraph =
180 unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
182 CicUnification.UnificationFailure _ ->
183 raise MatchingFailure
186 let unification m1 m2 c t1 t2 ug =
188 unification_aux true m1 m2 c t1 t2 ug
194 let check_eq context msg eq =
195 let w, proof, (eq_ty, left, right, order), metas = eq in
196 if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
197 (fst (CicTypeChecker.type_of_aux' metas context left CicUniv.empty_ugraph))
198 CicUniv.empty_ugraph))
207 let find_equalities context proof =
208 let module C = Cic in
209 let module S = CicSubstitution in
210 let module T = CicTypeChecker in
211 let eq_uri = LibraryObjects.eq_URI () in
212 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
213 let ok_types ty menv =
214 List.for_all (fun (_, _, mt) -> mt = ty) menv
216 let rec aux index newmeta = function
218 | (Some (_, C.Decl (term)))::tl ->
219 let do_find context term =
221 | C.Prod (name, s, t) ->
222 let (head, newmetas, args, newmeta) =
223 ProofEngineHelpers.saturate_term newmeta []
224 context (S.lift index term) 0
227 if List.length args = 0 then
230 C.Appl ((C.Rel index)::args)
233 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
234 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
237 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
238 let o = !Utils.compare_terms t1 t2 in
239 let stat = (ty,t1,t2,o) in
240 let w = compute_equality_weight stat in
241 let proof = Equality.Exact p in
242 let e = Equality.mk_equality (w, proof, stat, newmetas) in
246 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
247 when UriManager.eq uri eq_uri ->
248 let ty = S.lift index ty in
249 let t1 = S.lift index t1 in
250 let t2 = S.lift index t2 in
251 let o = !Utils.compare_terms t1 t2 in
252 let stat = (ty,t1,t2,o) in
253 let w = compute_equality_weight stat in
254 let p = C.Rel index in
255 let proof = Equality.Exact p in
256 let e = Equality.mk_equality (w, proof,stat,[]) in
260 match do_find context term with
262 let tl, newmeta' = (aux (index+1) newmeta tl) in
263 if newmeta' < newmeta then
264 prerr_endline "big trouble";
265 (index, p)::tl, newmeta' (* max???? *)
267 aux (index+1) newmeta tl
270 aux (index+1) newmeta tl
272 let il, maxm = aux 1 newmeta context in
273 let indexes, equalities = List.split il in
274 (* ignore (List.iter (check_eq context "find") equalities); *)
275 indexes, equalities, maxm
280 let equations_blacklist =
282 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
283 UriManager.UriSet.empty [
284 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
285 "cic:/Coq/Init/Logic/trans_eq.con";
286 "cic:/Coq/Init/Logic/f_equal.con";
287 "cic:/Coq/Init/Logic/f_equal2.con";
288 "cic:/Coq/Init/Logic/f_equal3.con";
289 "cic:/Coq/Init/Logic/f_equal4.con";
290 "cic:/Coq/Init/Logic/f_equal5.con";
291 "cic:/Coq/Init/Logic/sym_eq.con";
292 "cic:/Coq/Init/Logic/eq_ind.con";
293 "cic:/Coq/Init/Logic/eq_ind_r.con";
294 "cic:/Coq/Init/Logic/eq_rec.con";
295 "cic:/Coq/Init/Logic/eq_rec_r.con";
296 "cic:/Coq/Init/Logic/eq_rect.con";
297 "cic:/Coq/Init/Logic/eq_rect_r.con";
298 "cic:/Coq/Logic/Eqdep/UIP.con";
299 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
300 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
301 "cic:/Coq/ZArith/Zcompare/rename.con";
302 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
303 perche' questo cacchio di teorema rompe le scatole :'( *)
304 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
306 "cic:/matita/logic/equality/eq_f.con";
307 "cic:/matita/logic/equality/eq_f2.con";
308 "cic:/matita/logic/equality/eq_rec.con";
309 "cic:/matita/logic/equality/eq_rect.con";
313 let equations_blacklist = UriManager.UriSet.empty;;
316 let find_library_equalities dbd context status maxmeta =
317 let module C = Cic in
318 let module S = CicSubstitution in
319 let module T = CicTypeChecker in
322 (fun s u -> UriManager.UriSet.add u s)
324 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
330 if UriManager.UriSet.mem uri blacklist then
333 let t = CicUtil.term_of_uri uri in
335 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
339 (let t1 = Unix.gettimeofday () in
340 let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
341 let t2 = Unix.gettimeofday () in
344 (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
348 let eq_uri1 = eq_XURI ()
349 and eq_uri2 = LibraryObjects.eq_URI () in
351 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
353 let ok_types ty menv =
354 List.for_all (fun (_, _, mt) -> mt = ty) menv
356 let rec has_vars = function
357 | C.Meta _ | C.Rel _ | C.Const _ -> false
359 | C.Appl l -> List.exists has_vars l
360 | C.Prod (_, s, t) | C.Lambda (_, s, t)
361 | C.LetIn (_, s, t) | C.Cast (s, t) ->
362 (has_vars s) || (has_vars t)
365 let rec aux newmeta = function
367 | (uri, term, termty)::tl ->
370 (Printf.sprintf "Examining: %s (%s)"
371 (CicPp.ppterm term) (CicPp.ppterm termty)));
374 | C.Prod (name, s, t) when not (has_vars termty) ->
375 let head, newmetas, args, newmeta =
376 ProofEngineHelpers.saturate_term newmeta [] context termty 0
379 if List.length args = 0 then
385 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
386 when (iseq uri) && (ok_types ty newmetas) ->
389 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
390 let o = !Utils.compare_terms t1 t2 in
391 let stat = (ty,t1,t2,o) in
392 let w = compute_equality_weight stat in
393 let proof = Equality.Exact p in
394 let e = Equality.mk_equality (w, proof, stat, newmetas) in
398 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
399 when iseq uri && not (has_vars termty) ->
400 let o = !Utils.compare_terms t1 t2 in
401 let stat = (ty,t1,t2,o) in
402 let w = compute_equality_weight stat in
403 let proof = Equality.Exact term in
404 let e = Equality.mk_equality (w, proof, stat, []) in
410 let tl, newmeta' = aux newmeta tl in
411 if newmeta' < newmeta then
412 prerr_endline "big trouble";
413 (uri, e)::tl, newmeta' (* max???? *)
417 let found, maxm = aux maxmeta candidates in
419 let mceq = Equality.meta_convertibility_eq in
421 (fun (s, l) (u, e) ->
422 if List.exists (mceq e) (List.map snd l) then (
425 (Printf.sprintf "NO!! %s already there!"
426 (Equality.string_of_equality e)));
427 (UriManager.UriSet.add u s, l)
428 ) else (UriManager.UriSet.add u s, (u, e)::l))
429 (UriManager.UriSet.empty, []) found)
435 let find_library_theorems dbd env status equalities_uris =
436 let module C = Cic in
437 let module S = CicSubstitution in
438 let module T = CicTypeChecker in
441 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
443 UriManager.UriSet.remove refl_equal
444 (UriManager.UriSet.union equalities_uris equations_blacklist)
447 (fun s u -> UriManager.UriSet.add u s)
448 s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
451 let metasenv, context, ugraph = env in
455 if UriManager.UriSet.mem uri blacklist then l
457 let t = CicUtil.term_of_uri uri in
458 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
460 [] (MetadataQuery.signature_of_goal ~dbd status)
463 let u = eq_XURI () in
464 let t = CicUtil.term_of_uri u in
465 let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
468 refl_equal::candidates
472 let find_context_hypotheses env equalities_indexes =
473 let metasenv, context, ugraph = env in
480 if List.mem n equalities_indexes then
485 CicTypeChecker.type_of_aux' metasenv context t ugraph in
486 (n+1, (t, ty, [])::l))
492 let get_stats () = <:show<Inference.>> ;;