]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/inference.ml
occur_check and unification fixed. now Meta(i,[]) and Meta(i,[...]) are
[helm.git] / components / tactics / paramodulation / inference.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 let _profiler = <:profiler<_profiler>>;;
27
28 (* $Id$ *)
29
30 open Utils;;
31 open Printf;;
32
33 let check_disjoint_invariant subst metasenv msg =
34   if (List.exists 
35         (fun (i,_,_) -> (Subst.is_in_subst i subst)) metasenv)
36   then 
37     begin 
38       prerr_endline ("not disjoint: " ^ msg);
39       assert false
40     end
41 ;;
42
43 let rec check_irl start = function
44   | [] -> true
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
48   | _ -> false
49 ;;
50
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) -> let l = [] in check_irl 1 l
55   | Cic.Rel _ -> true
56   | Cic.Const _ -> true
57   | Cic.MutInd (_, _, []) -> true
58   | Cic.MutConstruct (_, _, _, []) -> true
59   | _ -> false
60 ;;
61
62 let locked menv i =
63   List.exists (fun (j,_,_) -> i = j) menv
64 ;;
65
66 let unification_simple locked_menv metasenv context t1 t2 ugraph =
67   let module C = Cic in
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 =
72     match where with
73     | Cic.Meta(i,_) when i = what -> true
74     | C.Appl l -> List.exists (occurs_check subst what) l
75     | C.Meta _ ->
76         let t = lookup where subst in
77         if t <> where then occurs_check subst what t else false
78     | _ -> false
79   in
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
83     
84     in
85     match s, t with
86     | s, t when s = t -> subst, menv
87     (* sometimes the same meta has different local contexts; this
88        could create "cyclic" substitutions *)
89     | C.Meta (i, _), C.Meta (j, _) when i=j ->  subst, menv
90     | C.Meta (i, _), C.Meta (j, _) 
91         when (locked locked_menv i) &&(locked locked_menv j) ->
92         raise
93           (U.UnificationFailure (lazy "Inference.unification.unif"))
94     | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->          
95         unif subst menv t s
96     | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
97         unif subst menv t s
98     | C.Meta (i,_), t when occurs_check subst i t ->
99         raise
100           (U.UnificationFailure (lazy "Inference.unification.unif"))
101     | C.Meta (i, l), t when (locked locked_menv i) -> 
102         raise
103           (U.UnificationFailure (lazy "Inference.unification.unif"))
104     | C.Meta (i, l), t -> (
105         try
106           let _, _, ty = CicUtil.lookup_meta i menv in
107           assert (not (Subst.is_in_subst i subst));
108           let subst = Subst.buildsubst i context t ty subst in
109           let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
110           subst, menv
111         with CicUtil.Meta_not_found m ->
112           let names = names_of_context context in
113           (*debug_print
114             (lazy*) prerr_endline 
115                (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
116                   (CicPp.pp t1 names) (CicPp.pp t2 names)
117                   (print_metasenv menv) (print_metasenv metasenv));
118           assert false
119       )
120     | _, C.Meta _ -> unif subst menv t s
121     | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
122         raise (U.UnificationFailure (lazy "Inference.unification.unif"))
123     | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
124         try
125           List.fold_left2
126             (fun (subst', menv) s t -> unif subst' menv s t)
127             (subst, menv) tls tlt
128         with Invalid_argument _ ->
129           raise (U.UnificationFailure (lazy "Inference.unification.unif"))
130       )
131     | _, _ ->
132         raise (U.UnificationFailure (lazy "Inference.unification.unif"))
133   in
134   let subst, menv = unif Subst.empty_subst metasenv t1 t2 in
135   let menv = Subst.filter subst menv in
136   subst, menv, ugraph
137 ;;
138
139 let profiler = HExtlib.profile "P/Inference.unif_simple[flatten]"
140 let profiler2 = HExtlib.profile "P/Inference.unif_simple[flatten_fast]"
141 let profiler3 = HExtlib.profile "P/Inference.unif_simple[resolve_meta]"
142 let profiler4 = HExtlib.profile "P/Inference.unif_simple[filter]"
143
144 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
145   let metasenv = 
146     HExtlib.list_uniq (List.sort Pervasives.compare (metasenv1 @ metasenv2)) 
147     (* metasenv1 @ metasenv2 *)
148   in
149   let subst, menv, ug =
150     if not (is_simple_term t1) || not (is_simple_term t2) then (
151       debug_print
152         (lazy
153            (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
154               (CicPp.ppterm t1) (CicPp.ppterm t2)));
155       raise (CicUnification.UnificationFailure (lazy "Inference.unification.unif"))
156     ) else
157       if b then
158         (* full unification *)
159         unification_simple [] metasenv context t1 t2 ugraph
160       else
161         (* matching: metasenv1 is locked *)
162         unification_simple metasenv1 metasenv context t1 t2 ugraph
163   in
164   if Utils.debug_res then
165             ignore(check_disjoint_invariant subst menv "unif");
166   (* let flatten subst = 
167     List.map
168       (fun (i, (context, term, ty)) ->
169          let context = apply_subst_context subst context in
170          let term = apply_subst subst term in
171          let ty = apply_subst subst ty in  
172            (i, (context, term, ty))) subst 
173   in
174   let flatten subst = profiler.HExtlib.profile flatten subst in
175   let subst = flatten subst in *)
176     subst, menv, ug
177 ;;
178
179 exception MatchingFailure;;
180
181 (** matching takes in input the _disjoint_ metasenv of t1 and  t2;
182 it perform unification in the union metasenv, then check that
183 the first metasenv has not changed *)
184 let matching metasenv1 metasenv2 context t1 t2 ugraph = 
185   try 
186     unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
187   with
188     CicUnification.UnificationFailure _ -> 
189       raise MatchingFailure
190 ;;
191
192 let unification m1 m2 c t1 t2 ug = 
193   try 
194     unification_aux true m1 m2 c t1 t2 ug
195   with exn -> 
196     raise exn
197 ;;
198
199
200 let check_eq context msg eq =
201   let w, proof, (eq_ty, left, right, order), metas = eq in
202   if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
203    (fst (CicTypeChecker.type_of_aux' metas context  left CicUniv.empty_ugraph))
204    CicUniv.empty_ugraph))
205   then
206     begin
207       prerr_endline msg;
208       assert false;
209     end
210   else ()
211 ;;
212
213 let find_equalities context proof =
214   let module C = Cic in
215   let module S = CicSubstitution in
216   let module T = CicTypeChecker in
217   let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
218   let ok_types ty menv =
219     List.for_all (fun (_, _, mt) -> mt = ty) menv
220   in
221   let rec aux index newmeta = function
222     | [] -> [], newmeta
223     | (Some (_, C.Decl (term)))::tl ->
224         let do_find context term =
225           match term with
226           | C.Prod (name, s, t) ->
227               let (head, newmetas, args, newmeta) =
228                 ProofEngineHelpers.saturate_term newmeta []
229                   context (S.lift index term) 0
230               in
231               let p =
232                 if List.length args = 0 then
233                   C.Rel index
234                 else
235                   C.Appl ((C.Rel index)::args)
236               in (
237                 match head with
238                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
239                     when (LibraryObjects.is_eq_URI uri) && 
240                          (ok_types ty newmetas) ->
241                     debug_print
242                       (lazy
243                          (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
244                     let o = !Utils.compare_terms t1 t2 in
245                     let stat = (ty,t1,t2,o) in
246                     let w = compute_equality_weight stat in
247                     let proof = Equality.Exact p in
248                     let e = Equality.mk_equality (w, proof, stat, newmetas) in
249                     Some e, (newmeta+1)
250                 | _ -> None, newmeta
251               )
252           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
253               when LibraryObjects.is_eq_URI uri ->
254               let ty = S.lift index ty in
255               let t1 = S.lift index t1 in
256               let t2 = S.lift index t2 in
257               let o = !Utils.compare_terms t1 t2 in
258               let stat = (ty,t1,t2,o) in
259               let w = compute_equality_weight stat in
260               let p = C.Rel index in
261               let proof = Equality.Exact p in
262               let e = Equality.mk_equality (w, proof,stat,[]) in
263               Some e, (newmeta+1)
264           | _ -> None, newmeta
265         in (
266           match do_find context term with
267           | Some p, newmeta ->
268               let tl, newmeta' = (aux (index+1) newmeta tl) in
269               if newmeta' < newmeta then 
270                 prerr_endline "big trouble";
271               (index, p)::tl, newmeta' (* max???? *)
272           | None, _ ->
273               aux (index+1) newmeta tl
274         )
275     | _::tl ->
276         aux (index+1) newmeta tl
277   in
278   let il, maxm = aux 1 newmeta context in
279   let indexes, equalities = List.split il in
280   (* ignore (List.iter (check_eq context "find") equalities); *)
281   indexes, equalities, maxm
282 ;;
283
284
285 (*
286 let equations_blacklist =
287   List.fold_left
288     (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
289     UriManager.UriSet.empty [
290       "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
291       "cic:/Coq/Init/Logic/trans_eq.con";
292       "cic:/Coq/Init/Logic/f_equal.con";
293       "cic:/Coq/Init/Logic/f_equal2.con";
294       "cic:/Coq/Init/Logic/f_equal3.con";
295       "cic:/Coq/Init/Logic/f_equal4.con";
296       "cic:/Coq/Init/Logic/f_equal5.con";
297       "cic:/Coq/Init/Logic/sym_eq.con";
298       "cic:/Coq/Init/Logic/eq_ind.con";
299       "cic:/Coq/Init/Logic/eq_ind_r.con";
300       "cic:/Coq/Init/Logic/eq_rec.con";
301       "cic:/Coq/Init/Logic/eq_rec_r.con";
302       "cic:/Coq/Init/Logic/eq_rect.con";
303       "cic:/Coq/Init/Logic/eq_rect_r.con";
304       "cic:/Coq/Logic/Eqdep/UIP.con";
305       "cic:/Coq/Logic/Eqdep/UIP_refl.con";
306       "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
307       "cic:/Coq/ZArith/Zcompare/rename.con";
308       (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
309          perche' questo cacchio di teorema rompe le scatole :'( *)
310       "cic:/Rocq/SUBST/comparith/mult_n_2.con";
311
312       "cic:/matita/logic/equality/eq_f.con";
313       "cic:/matita/logic/equality/eq_f2.con";
314       "cic:/matita/logic/equality/eq_rec.con";
315       "cic:/matita/logic/equality/eq_rect.con";
316     ]
317 ;;
318 *)
319 let equations_blacklist = UriManager.UriSet.empty;;
320
321 let tty_of_u u = 
322   let _ = <:start<tty_of_u>> in
323   let t = CicUtil.term_of_uri u in
324   let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
325   let _ = <:stop<tty_of_u>> in
326   t, ty
327 ;;
328
329 let utty_of_u u =
330   let t,ty = tty_of_u u in
331   u, t, ty
332 ;;
333
334 let find_library_equalities dbd context status maxmeta = 
335   let module C = Cic in
336   let module S = CicSubstitution in
337   let module T = CicTypeChecker in
338   let _ = <:start<equations_for_goal>> in
339   let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
340   let _ = <:stop<equations_for_goal>> in
341   let candidates =
342     List.fold_left
343       (fun l uri ->
344         if LibraryObjects.is_eq_refl_URI uri ||
345            LibraryObjects.is_sym_eq_URI uri ||
346            LibraryObjects.is_trans_eq_URI uri ||
347            LibraryObjects.is_eq_ind_URI uri ||
348            LibraryObjects.is_eq_ind_r_URI uri 
349          then 
350            l
351          else
352            (utty_of_u uri)::l)
353       [] eqs
354   in
355   let ok_types ty menv =
356     List.for_all (fun (_, _, mt) -> mt = ty) menv
357   in
358   let rec has_vars = function
359     | C.Meta _ | C.Rel _ | C.Const _ -> false
360     | C.Var _ -> true
361     | C.Appl l -> List.exists has_vars l
362     | C.Prod (_, s, t) | C.Lambda (_, s, t)
363     | C.LetIn (_, s, t) | C.Cast (s, t) ->
364         (has_vars s) || (has_vars t)
365     | _ -> false
366  in
367   let rec aux newmeta = function
368     | [] -> [], newmeta
369     | (uri, term, termty)::tl ->
370         debug_print
371           (lazy
372              (Printf.sprintf "Examining: %s (%s)"
373                 (CicPp.ppterm term) (CicPp.ppterm termty)));
374         let res, newmeta = 
375           match termty with
376           | C.Prod (name, s, t) when not (has_vars termty) ->
377               let head, newmetas, args, newmeta =
378                 ProofEngineHelpers.saturate_term newmeta [] context termty 0
379               in
380               let p =
381                 if List.length args = 0 then
382                   term
383                 else
384                   C.Appl (term::args)
385               in (
386                   match head with
387                     | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
388                         when (LibraryObjects.is_eq_URI uri ||
389                               LibraryObjects.is_eq_refl_URI uri) && 
390                              (ok_types ty newmetas) ->
391                     debug_print
392                       (lazy
393                          (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
394                     let o = !Utils.compare_terms t1 t2 in
395                     let stat = (ty,t1,t2,o) in
396                     let w = compute_equality_weight stat in
397                     let proof = Equality.Exact p in
398                     let e = Equality.mk_equality (w, proof, stat, newmetas) in
399                     Some e, (newmeta+1)
400                 | _ -> None, newmeta
401               )
402           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
403               when 
404                 (LibraryObjects.is_eq_URI uri || 
405                  LibraryObjects.is_eq_refl_URI uri) && 
406                 not (has_vars termty) ->
407               let o = !Utils.compare_terms t1 t2 in
408               let stat = (ty,t1,t2,o) in
409               let w = compute_equality_weight stat in
410               let proof = Equality.Exact term in 
411               let e = Equality.mk_equality (w, proof, stat, []) in
412               Some e, (newmeta+1)
413           | _ -> None, newmeta
414         in
415         match res with
416         | Some e ->
417             let tl, newmeta' = aux newmeta tl in
418               if newmeta' < newmeta then 
419                 prerr_endline "big trouble";
420               (uri, e)::tl, newmeta' (* max???? *)
421         | None ->
422             aux newmeta tl
423   in
424   let found, maxm = aux maxmeta candidates in
425   let uriset, eqlist = 
426     let mceq = Equality.meta_convertibility_eq in
427     (List.fold_left
428        (fun (s, l) (u, e) ->
429           if List.exists (mceq e) (List.map snd l) then (
430             debug_print
431               (lazy
432                  (Printf.sprintf "NO!! %s already there!"
433                     (Equality.string_of_equality e)));
434             (UriManager.UriSet.add u s, l)
435           ) else (UriManager.UriSet.add u s, (u, e)::l))
436        (UriManager.UriSet.empty, []) found)
437   in
438   uriset, eqlist, maxm
439 ;;
440
441
442 let find_library_theorems dbd env status equalities_uris =
443   let module C = Cic in
444   let module S = CicSubstitution in
445   let module T = CicTypeChecker in
446   let candidates =
447     List.fold_left
448       (fun l uri ->
449         if LibraryObjects.is_sym_eq_URI uri ||
450            LibraryObjects.is_trans_eq_URI uri ||
451            LibraryObjects.is_eq_ind_URI uri ||
452            LibraryObjects.is_eq_ind_r_URI uri 
453          then l
454          else
455            (let t,ty = tty_of_u uri in t, ty, [] )::l)
456       [] (MetadataQuery.signature_of_goal ~dbd status)
457   in
458   let refl_equal =
459     let eq = 
460       match LibraryObjects.eq_URI () with
461       | Some u -> u
462       | None -> 
463           raise 
464             (ProofEngineTypes.Fail 
465               (lazy "No default eq defined when running find_library_theorems"))
466     in
467     let t = CicUtil.term_of_uri (LibraryObjects.eq_refl_URI ~eq) in
468     let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
469     (t, ty, [])
470   in
471   let res = refl_equal::candidates in
472   res
473 ;;
474
475
476 let find_context_hypotheses env equalities_indexes =
477   let metasenv, context, ugraph = env in
478   let _, res = 
479     List.fold_left
480       (fun (n, l) entry ->
481          match entry with
482          | None -> (n+1, l)
483          | Some _ ->
484              if List.mem n equalities_indexes then
485                (n+1, l)
486              else
487                let t = Cic.Rel n in
488                let ty, _ =
489                  CicTypeChecker.type_of_aux' metasenv context t ugraph in 
490                (n+1, (t, ty, [])::l))
491       (1, []) context
492   in
493   res
494 ;;
495
496 let get_stats () = <:show<Inference.>> ;;