]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/inference.ml
removed a bad prerr_endline
[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 (* $Id$ *)
27
28 open Utils;;
29 open Printf;;
30
31 let check_disjoint_invariant subst metasenv msg =
32   if (List.exists 
33         (fun (i,_,_) -> (Subst.is_in_subst i subst)) metasenv)
34   then 
35     begin 
36       prerr_endline ("not disjoint: " ^ msg);
37       assert false
38     end
39 ;;
40
41 let rec check_irl start = function
42   | [] -> true
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
46   | _ -> false
47 ;;
48
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
53   | Cic.Rel _ -> true
54   | Cic.Const _ -> true
55   | Cic.MutInd (_, _, []) -> true
56   | Cic.MutConstruct (_, _, _, []) -> true
57   | _ -> false
58 ;;
59
60 let locked menv i =
61   List.exists (fun (j,_,_) -> i = j) menv
62 ;;
63
64 let unification_simple locked_menv metasenv context t1 t2 ugraph =
65   let module C = Cic in
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 =
70     match where with
71     | t when what = t -> true
72     | C.Appl l -> List.exists (occurs_check subst what) l
73     | C.Meta _ ->
74         let t = lookup where subst in
75         if t <> where then occurs_check subst what t else false
76     | _ -> false
77   in
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
81     
82     in
83     match s, t with
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) ->
87         raise
88           (U.UnificationFailure (lazy "Inference.unification.unif"))
89     | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->          
90         unif subst menv t s
91     | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
92         unif subst menv t s
93     | C.Meta _, t when occurs_check subst s t ->
94         raise
95           (U.UnificationFailure (lazy "Inference.unification.unif"))
96     | C.Meta (i, l), t when (locked locked_menv i) -> 
97         raise
98           (U.UnificationFailure (lazy "Inference.unification.unif"))
99     | C.Meta (i, l), t -> (
100         try
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 *)
105           subst, menv
106         with CicUtil.Meta_not_found m ->
107           let names = names_of_context context in
108           (*debug_print
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));
113           assert false
114       )
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) -> (
119         try
120           List.fold_left2
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"))
125       )
126     | _, _ ->
127         raise (U.UnificationFailure (lazy "Inference.unification.unif"))
128   in
129   let subst, menv = unif Subst.empty_subst metasenv t1 t2 in
130   let menv = Subst.filter subst menv in
131   subst, menv, ugraph
132 ;;
133
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]"
138
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 (
143       debug_print
144         (lazy
145            (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
146               (CicPp.ppterm t1) (CicPp.ppterm t2)));
147       raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
148     ) else
149       if b then
150         (* full unification *)
151         unification_simple [] metasenv context t1 t2 ugraph
152       else
153         (* matching: metasenv1 is locked *)
154         unification_simple metasenv1 metasenv context t1 t2 ugraph
155   in
156   if Utils.debug_res then
157             ignore(check_disjoint_invariant subst menv "unif");
158   (* let flatten subst = 
159     List.map
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 
165   in
166   let flatten subst = profiler.HExtlib.profile flatten subst in
167   let subst = flatten subst in *)
168     subst, menv, ug
169 ;;
170
171 exception MatchingFailure;;
172
173 let matching1 metasenv1 metasenv2 context t1 t2 ugraph = 
174   try 
175     unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
176   with
177     CicUnification .UnificationFailure _ ->
178       raise MatchingFailure
179 ;;
180
181 let unification = unification_aux true 
182 ;;
183
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 *)
187
188 let matching = matching1;;
189
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))
195   then
196     begin
197       prerr_endline msg;
198       assert false;
199     end
200   else ()
201 ;;
202
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
211   in
212   let rec aux index newmeta = function
213     | [] -> [], newmeta
214     | (Some (_, C.Decl (term)))::tl ->
215         let do_find context term =
216           match term with
217           | C.Prod (name, s, t) ->
218               let (head, newmetas, args, newmeta) =
219                 ProofEngineHelpers.saturate_term newmeta []
220                   context (S.lift index term) 0
221               in
222               let p =
223                 if List.length args = 0 then
224                   C.Rel index
225                 else
226                   C.Appl ((C.Rel index)::args)
227               in (
228                 match head with
229                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
230                     when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
231                     debug_print
232                       (lazy
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
239                     Some e, (newmeta+1)
240                 | _ -> None, newmeta
241               )
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
253               Some e, (newmeta+1)
254           | _ -> None, newmeta
255         in (
256           match do_find context term with
257           | Some p, newmeta ->
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???? *)
262           | None, _ ->
263               aux (index+1) newmeta tl
264         )
265     | _::tl ->
266         aux (index+1) newmeta tl
267   in
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
272 ;;
273
274
275 (*
276 let equations_blacklist =
277   List.fold_left
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";
301
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";
306     ]
307 ;;
308 *)
309 let equations_blacklist = UriManager.UriSet.empty;;
310
311
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
316   let blacklist =
317     List.fold_left
318       (fun s u -> UriManager.UriSet.add u s)
319       equations_blacklist
320       [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
321        eq_ind_r_URI ()]
322   in
323   let candidates =
324     List.fold_left
325       (fun l uri ->
326          if UriManager.UriSet.mem uri blacklist then
327            l
328          else
329            let t = CicUtil.term_of_uri uri in
330            let ty, _ =
331              CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
332            in
333            (uri, t, ty)::l)
334       []
335       (let t1 = Unix.gettimeofday () in
336        let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
337        let t2 = Unix.gettimeofday () in
338        (debug_print
339           (lazy
340              (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
341                 (t2 -. t1))));
342        eqs)
343   in
344   let eq_uri1 = eq_XURI ()
345   and eq_uri2 = LibraryObjects.eq_URI () in
346   let iseq uri =
347     (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
348   in
349   let ok_types ty menv =
350     List.for_all (fun (_, _, mt) -> mt = ty) menv
351   in
352   let rec has_vars = function
353     | C.Meta _ | C.Rel _ | C.Const _ -> false
354     | C.Var _ -> true
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)
359     | _ -> false
360  in
361   let rec aux newmeta = function
362     | [] -> [], newmeta
363     | (uri, term, termty)::tl ->
364         debug_print
365           (lazy
366              (Printf.sprintf "Examining: %s (%s)"
367                 (CicPp.ppterm term) (CicPp.ppterm termty)));
368         let res, newmeta = 
369           match termty with
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
373               in
374               let p =
375                 if List.length args = 0 then
376                   term
377                 else
378                   C.Appl (term::args)
379               in (
380                   match head with
381                     | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
382                         when (iseq uri) && (ok_types ty newmetas) ->
383                     debug_print
384                       (lazy
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
391                     Some e, (newmeta+1)
392                 | _ -> None, newmeta
393               )
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
401               Some e, (newmeta+1)
402           | _ -> None, newmeta
403         in
404         match res with
405         | Some e ->
406             let tl, newmeta' = aux newmeta tl in
407               if newmeta' < newmeta then 
408                 prerr_endline "big trouble";
409               (uri, e)::tl, newmeta' (* max???? *)
410         | None ->
411             aux newmeta tl
412   in
413   let found, maxm = aux maxmeta candidates in
414   let uriset, eqlist = 
415     let mceq = Equality.meta_convertibility_eq in
416     (List.fold_left
417        (fun (s, l) (u, e) ->
418           if List.exists (mceq e) (List.map snd l) then (
419             debug_print
420               (lazy
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)
426   in
427   uriset, eqlist, maxm
428 ;;
429
430
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
435   let blacklist =
436     let refl_equal =
437       UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
438     let s =
439       UriManager.UriSet.remove refl_equal
440         (UriManager.UriSet.union equalities_uris equations_blacklist)
441     in
442     List.fold_left
443       (fun s u -> UriManager.UriSet.add u s)
444       s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
445          eq_ind_r_URI ()]
446   in
447   let metasenv, context, ugraph = env in
448   let candidates =
449     List.fold_left
450       (fun l uri ->
451          if UriManager.UriSet.mem uri blacklist then l
452          else
453            let t = CicUtil.term_of_uri uri in
454            let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
455            (t, ty, [])::l)
456       [] (MetadataQuery.signature_of_goal ~dbd status)
457   in
458   let refl_equal =
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
462     (t, ty, [])
463   in
464   refl_equal::candidates
465 ;;
466
467
468 let find_context_hypotheses env equalities_indexes =
469   let metasenv, context, ugraph = env in
470   let _, res = 
471     List.fold_left
472       (fun (n, l) entry ->
473          match entry with
474          | None -> (n+1, l)
475          | Some _ ->
476              if List.mem n equalities_indexes then
477                (n+1, l)
478              else
479                let t = Cic.Rel n in
480                let ty, _ =
481                  CicTypeChecker.type_of_aux' metasenv context t ugraph in 
482                (n+1, (t, ty, [])::l))
483       (1, []) context
484   in
485   res
486 ;;
487