]> matita.cs.unibo.it Git - helm.git/blobdiff - components/tactics/paramodulation/inference.ml
added (but not yet used) remove_local_context
[helm.git] / components / tactics / paramodulation / inference.ml
index f088d54543aa265f6550e53bf0552f886c409e8f..1c3daffecf3fc1daff35973004d6d645e58fde74 100644 (file)
@@ -23,6 +23,8 @@
  * http://cs.unibo.it/helm/.
  *)
 
+let _profiler = <:profiler<_profiler>>;;
+
 (* $Id$ *)
 
 open Utils;;
@@ -30,7 +32,7 @@ open Printf;;
 
 let check_disjoint_invariant subst metasenv msg =
   if (List.exists 
-        (fun (i,_,_) -> (Equality.is_in_subst i subst)) metasenv)
+        (fun (i,_,_) -> (Subst.is_in_subst i subst)) metasenv)
   then 
     begin 
       prerr_endline ("not disjoint: " ^ msg);
@@ -49,7 +51,7 @@ let rec check_irl start = function
 let rec is_simple_term = function
   | Cic.Appl ((Cic.Meta _)::_) -> false
   | Cic.Appl l -> List.for_all is_simple_term l
-  | Cic.Meta (i, l) -> check_irl 1 l
+  | Cic.Meta (i, l) -> let l = [] in check_irl 1 l
   | Cic.Rel _ -> true
   | Cic.Const _ -> true
   | Cic.MutInd (_, _, []) -> true
@@ -65,10 +67,10 @@ let unification_simple locked_menv metasenv context t1 t2 ugraph =
   let module C = Cic in
   let module M = CicMetaSubst in
   let module U = CicUnification in
-  let lookup = Equality.lookup_subst in
+  let lookup = Subst.lookup_subst in
   let rec occurs_check subst what where =
     match where with
-    | t when what = t -> true
+    | Cic.Meta(i,_) when i = what -> true
     | C.Appl l -> List.exists (occurs_check subst what) l
     | C.Meta _ ->
         let t = lookup where subst in
@@ -82,6 +84,9 @@ let unification_simple locked_menv metasenv context t1 t2 ugraph =
     in
     match s, t with
     | s, t when s = t -> subst, menv
+    (* sometimes the same meta has different local contexts; this
+       could create "cyclic" substitutions *)
+    | C.Meta (i, _), C.Meta (j, _) when i=j ->  subst, menv
     | C.Meta (i, _), C.Meta (j, _) 
         when (locked locked_menv i) &&(locked locked_menv j) ->
         raise
@@ -90,7 +95,7 @@ let unification_simple locked_menv metasenv context t1 t2 ugraph =
         unif subst menv t s
     | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
         unif subst menv t s
-    | C.Meta _, t when occurs_check subst s t ->
+    | C.Meta (i,_), t when occurs_check subst i t ->
         raise
           (U.UnificationFailure (lazy "Inference.unification.unif"))
     | C.Meta (i, l), t when (locked locked_menv i) -> 
@@ -99,8 +104,8 @@ let unification_simple locked_menv metasenv context t1 t2 ugraph =
     | C.Meta (i, l), t -> (
         try
           let _, _, ty = CicUtil.lookup_meta i menv in
-          assert (not (Equality.is_in_subst i subst));
-          let subst = Equality.buildsubst i context t ty subst in
+          assert (not (Subst.is_in_subst i subst));
+          let subst = Subst.buildsubst i context t ty subst in
           let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
           subst, menv
         with CicUtil.Meta_not_found m ->
@@ -126,8 +131,8 @@ let unification_simple locked_menv metasenv context t1 t2 ugraph =
     | _, _ ->
         raise (U.UnificationFailure (lazy "Inference.unification.unif"))
   in
-  let subst, menv = unif Equality.empty_subst metasenv t1 t2 in
-  let menv = Equality.filter subst menv in
+  let subst, menv = unif Subst.empty_subst metasenv t1 t2 in
+  let menv = Subst.filter subst menv in
   subst, menv, ugraph
 ;;
 
@@ -137,14 +142,17 @@ let profiler3 = HExtlib.profile "P/Inference.unif_simple[resolve_meta]"
 let profiler4 = HExtlib.profile "P/Inference.unif_simple[filter]"
 
 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
-  let metasenv = metasenv1 @ metasenv2 in
+  let metasenv = 
+    HExtlib.list_uniq (List.sort Pervasives.compare (metasenv1 @ metasenv2)) 
+    (* metasenv1 @ metasenv2 *)
+  in
   let subst, menv, ug =
     if not (is_simple_term t1) || not (is_simple_term t2) then (
       debug_print
         (lazy
            (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
               (CicPp.ppterm t1) (CicPp.ppterm t2)));
-      raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
+      raise (CicUnification.UnificationFailure (lazy "Inference.unification.unif"))
     ) else
       if b then
         (* full unification *)
@@ -170,22 +178,24 @@ let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
 
 exception MatchingFailure;;
 
-let matching1 metasenv1 metasenv2 context t1 t2 ugraph = 
+(** matching takes in input the _disjoint_ metasenv of t1 and  t2;
+it perform unification in the union metasenv, then check that
+the first metasenv has not changed *)
+let matching metasenv1 metasenv2 context t1 t2 ugraph = 
   try 
     unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
   with
-    CicUnification .UnificationFailure _ ->
+    CicUnification.UnificationFailure _ -> 
       raise MatchingFailure
 ;;
 
-let unification = unification_aux true 
+let unification m1 m2 c t1 t2 ug = 
+  try 
+    unification_aux true m1 m2 c t1 t2 ug
+  with exn -> 
+    raise exn
 ;;
 
-(** matching takes in input the _disjoint_ metasenv of t1 and  t2;
-it perform unification in the union metasenv, then check that
-the first metasenv has not changed *)
-
-let matching = matching1;;
 
 let check_eq context msg eq =
   let w, proof, (eq_ty, left, right, order), metas = eq in
@@ -204,7 +214,6 @@ let find_equalities context proof =
   let module C = Cic in
   let module S = CicSubstitution in
   let module T = CicTypeChecker in
-  let eq_uri = LibraryObjects.eq_URI () in
   let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
   let ok_types ty menv =
     List.for_all (fun (_, _, mt) -> mt = ty) menv
@@ -227,23 +236,21 @@ let find_equalities context proof =
               in (
                 match head with
                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
-                    when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
+                    when (LibraryObjects.is_eq_URI uri) && 
+                         (ok_types ty newmetas) ->
                     debug_print
                       (lazy
                          (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
                     let o = !Utils.compare_terms t1 t2 in
                     let stat = (ty,t1,t2,o) in
                     let w = compute_equality_weight stat in
-                    let proof_old = 
-                      Equality.BasicProof (Equality.empty_subst,p) in
-                    let proof_new = Equality.Exact p in
-                    let proof = proof_new , proof_old in
+                    let proof = Equality.Exact p in
                     let e = Equality.mk_equality (w, proof, stat, newmetas) in
                     Some e, (newmeta+1)
                 | _ -> None, newmeta
               )
           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
-              when UriManager.eq uri eq_uri ->
+              when LibraryObjects.is_eq_URI uri ->
               let ty = S.lift index ty in
               let t1 = S.lift index t1 in
               let t2 = S.lift index t2 in
@@ -251,9 +258,7 @@ let find_equalities context proof =
               let stat = (ty,t1,t2,o) in
               let w = compute_equality_weight stat in
               let p = C.Rel index in
-              let proof_old = Equality.BasicProof (Equality.empty_subst,p) in
-              let proof_new = Equality.Exact p in
-              let proof = proof_new, proof_old in
+              let proof = Equality.Exact p in
               let e = Equality.mk_equality (w, proof,stat,[]) in
               Some e, (newmeta+1)
           | _ -> None, newmeta
@@ -313,43 +318,39 @@ let equations_blacklist =
 *)
 let equations_blacklist = UriManager.UriSet.empty;;
 
+let tty_of_u u = 
+  let _ = <:start<tty_of_u>> in
+  let t = CicUtil.term_of_uri u in
+  let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
+  let _ = <:stop<tty_of_u>> in
+  t, ty
+;;
+
+let utty_of_u u =
+  let t,ty = tty_of_u u in
+  u, t, ty
+;;
 
 let find_library_equalities dbd context status maxmeta = 
   let module C = Cic in
   let module S = CicSubstitution in
   let module T = CicTypeChecker in
-  let blacklist =
-    List.fold_left
-      (fun s u -> UriManager.UriSet.add u s)
-      equations_blacklist
-      [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
-       eq_ind_r_URI ()]
-  in
+  let _ = <:start<equations_for_goal>> in
+  let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
+  let _ = <:stop<equations_for_goal>> in
   let candidates =
     List.fold_left
       (fun l uri ->
-         if UriManager.UriSet.mem uri blacklist then
+        if LibraryObjects.is_eq_refl_URI uri ||
+           LibraryObjects.is_sym_eq_URI uri ||
+           LibraryObjects.is_trans_eq_URI uri ||
+           LibraryObjects.is_eq_ind_URI uri ||
+           LibraryObjects.is_eq_ind_r_URI uri 
+         then 
            l
          else
-           let t = CicUtil.term_of_uri uri in
-           let ty, _ =
-             CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
-           in
-           (uri, t, ty)::l)
-      []
-      (let t1 = Unix.gettimeofday () in
-       let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
-       let t2 = Unix.gettimeofday () in
-       (debug_print
-          (lazy
-             (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
-                (t2 -. t1))));
-       eqs)
-  in
-  let eq_uri1 = eq_XURI ()
-  and eq_uri2 = LibraryObjects.eq_URI () in
-  let iseq uri =
-    (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
+           (utty_of_u uri)::l)
+      [] eqs
   in
   let ok_types ty menv =
     List.for_all (fun (_, _, mt) -> mt = ty) menv
@@ -384,29 +385,29 @@ let find_library_equalities dbd context status maxmeta =
               in (
                   match head with
                     | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
-                        when (iseq uri) && (ok_types ty newmetas) ->
+                        when (LibraryObjects.is_eq_URI uri ||
+                              LibraryObjects.is_eq_refl_URI uri) && 
+                             (ok_types ty newmetas) ->
                     debug_print
                       (lazy
                          (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
                     let o = !Utils.compare_terms t1 t2 in
                     let stat = (ty,t1,t2,o) in
                     let w = compute_equality_weight stat in
-                    let proof_old = 
-                      Equality.BasicProof (Equality.empty_subst,p) in
-                    let proof_new = Equality.Exact p in
-                    let proof = proof_new, proof_old in
+                    let proof = Equality.Exact p in
                     let e = Equality.mk_equality (w, proof, stat, newmetas) in
                     Some e, (newmeta+1)
                 | _ -> None, newmeta
               )
           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
-              when iseq uri && not (has_vars termty) ->
+              when 
+                (LibraryObjects.is_eq_URI uri || 
+                 LibraryObjects.is_eq_refl_URI uri) && 
+                not (has_vars termty) ->
               let o = !Utils.compare_terms t1 t2 in
               let stat = (ty,t1,t2,o) in
               let w = compute_equality_weight stat in
-              let old_proof = Equality.BasicProof (Equality.empty_subst,term) in
-              let new_proof = Equality.Exact term in 
-              let proof = new_proof, old_proof in
+              let proof = Equality.Exact term in 
               let e = Equality.mk_equality (w, proof, stat, []) in
               Some e, (newmeta+1)
           | _ -> None, newmeta
@@ -442,36 +443,33 @@ let find_library_theorems dbd env status equalities_uris =
   let module C = Cic in
   let module S = CicSubstitution in
   let module T = CicTypeChecker in
-  let blacklist =
-    let refl_equal =
-      UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
-    let s =
-      UriManager.UriSet.remove refl_equal
-        (UriManager.UriSet.union equalities_uris equations_blacklist)
-    in
-    List.fold_left
-      (fun s u -> UriManager.UriSet.add u s)
-      s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
-         eq_ind_r_URI ()]
-  in
-  let metasenv, context, ugraph = env in
   let candidates =
     List.fold_left
       (fun l uri ->
-         if UriManager.UriSet.mem uri blacklist then l
+        if LibraryObjects.is_sym_eq_URI uri ||
+           LibraryObjects.is_trans_eq_URI uri ||
+           LibraryObjects.is_eq_ind_URI uri ||
+           LibraryObjects.is_eq_ind_r_URI uri 
+         then l
          else
-           let t = CicUtil.term_of_uri uri in
-           let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
-           (t, ty, [])::l)
+           (let t,ty = tty_of_u uri in t, ty, [] )::l)
       [] (MetadataQuery.signature_of_goal ~dbd status)
   in
   let refl_equal =
-    let u = eq_XURI () in
-    let t = CicUtil.term_of_uri u in
+    let eq = 
+      match LibraryObjects.eq_URI () with
+      | Some u -> u
+      | None -> 
+          raise 
+            (ProofEngineTypes.Fail 
+              (lazy "No default eq defined when running find_library_theorems"))
+    in
+    let t = CicUtil.term_of_uri (LibraryObjects.eq_refl_URI ~eq) in
     let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
     (t, ty, [])
   in
-  refl_equal::candidates
+  let res = refl_equal::candidates in
+  res
 ;;
 
 
@@ -495,3 +493,4 @@ let find_context_hypotheses env equalities_indexes =
   res
 ;;
 
+let get_stats () = <:show<Inference.>> ;;