]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/tactics/paramodulation/indexing.ml
added timeout parameter to auto paramodulation.
[helm.git] / helm / software / components / tactics / paramodulation / indexing.ml
index e4dec639749adff715b48e7ce993d7d4ca45296c..f3af1b482e8afb2eece8d1ce79d3e10af7812ec7 100644 (file)
  * http://cs.unibo.it/helm/.
  *)
 
-let _profiler = <:profiler<_profiler>>;;
+(* let _profiler = <:profiler<_profiler>>;; *)
 
 (* $Id$ *)
 
-type goal = Equality.goal_proof * Cic.metasenv * Cic.term
-
 module Index = Equality_indexing.DT (* discrimination tree based indexing *)
 (*
 module Index = Equality_indexing.DT (* path tree based indexing *)
@@ -61,7 +59,7 @@ type retrieval_mode = Matching | Unification;;
 let string_of_res ?env =
   function
       None -> "None"
-    | Some (t, s, m, u, ((p,e), eq_URI)) ->
+    | Some (t, s, m, u, (p,e)) ->
         Printf.sprintf "Some: (%s, %s, %s)" 
           (Utils.string_of_pos p)
           (Equality.string_of_equality ?env e)
@@ -122,7 +120,7 @@ let check_for_duplicates metas msg =
 
 let check_res res msg =
   match res with
-      Some (t, subst, menv, ug, (eq_found, eq_URI)) ->
+      Some (t, subst, menv, ug, eq_found) ->
         let eqs = Equality.string_of_equality (snd eq_found) in
         check_disjoint_invariant subst menv msg;
         check_for_duplicates menv (msg ^ "\nchecking " ^ eqs);
@@ -181,8 +179,11 @@ let check_target context target msg =
 let get_candidates ?env mode tree term =
   let s = 
     match mode with
-    | Matching -> Index.retrieve_generalizations tree term
-    | Unification -> Index.retrieve_unifiables tree term
+    | Matching -> 
+        Index.retrieve_generalizations tree term
+    | Unification -> 
+        Index.retrieve_unifiables tree term
+        
   in
   Index.PosEqSet.elements s
 ;;
@@ -243,22 +244,21 @@ let rec find_matches metasenv context ugraph lift_amount term termty =
                                 ~metasenv context termty ty ugraph)) then (
           find_matches metasenv context ugraph lift_amount term termty tl
         ) else
-          let do_match c eq_URI =
+          let do_match c =
             let subst', metasenv', ugraph' =
               Inference.matching 
                 metasenv metas context term (S.lift lift_amount c) ugraph
             in
-            Some (Cic.Rel (1 + lift_amount), subst', metasenv', ugraph',
-                  (candidate, eq_URI))
+            Some (Cic.Rel(1+lift_amount),subst',metasenv',ugraph',candidate)
           in
-          let c, other, eq_URI =
-            if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
-            else right, left, Utils.eq_ind_r_URI ()
+          let c, other =
+            if pos = Utils.Left then left, right
+            else right, left
           in
           if o <> U.Incomparable then
             let res =
               try
-                do_match c eq_URI
+                do_match c 
               with Inference.MatchingFailure ->
                 find_matches metasenv context ugraph lift_amount term termty tl
             in
@@ -266,7 +266,7 @@ let rec find_matches metasenv context ugraph lift_amount term termty =
               res
           else
             let res =
-              try do_match c eq_URI
+              try do_match c 
               with Inference.MatchingFailure -> None
             in
             if Utils.debug_res then ignore (check_res res "find2");
@@ -307,19 +307,19 @@ let rec find_all_matches ?(unif_fun=Inference.unification)
     | candidate::tl ->
         let pos, equality = candidate in 
         let (_,_,(ty,left,right,o),metas,_)=Equality.open_equality equality in
-        let do_match c eq_URI =
+        let do_match c =
           let subst', metasenv', ugraph' =
             unif_fun metasenv metas context term (S.lift lift_amount c) ugraph
           in
-          (C.Rel (1+lift_amount),subst',metasenv',ugraph',(candidate, eq_URI))
+          (C.Rel (1+lift_amount),subst',metasenv',ugraph',candidate)
         in
-        let c, other, eq_URI =
-          if pos = Utils.Left then left, right, Utils.eq_ind_URI ()
-          else right, left, Utils.eq_ind_r_URI ()
+        let c, other =
+          if pos = Utils.Left then left, right
+          else right, left
         in
         if o <> U.Incomparable then
           try
-            let res = do_match c eq_URI in
+            let res = do_match c in
             res::(find_all_matches ~unif_fun metasenv context ugraph
                     lift_amount term termty tl)
           with
@@ -330,7 +330,7 @@ let rec find_all_matches ?(unif_fun=Inference.unification)
                 lift_amount term termty tl
         else
           try
-            let res = do_match c eq_URI in
+            let res = do_match c in
             match res with
             | _, s, _, _, _ ->
                 let c' = apply_subst s c
@@ -388,7 +388,7 @@ let subsumption_aux use_unification env table target =
   in
   let rec ok what leftorright = function
     | [] -> None
-    | (_, subst, menv, ug, ((pos,equation),_))::tl ->
+    | (_, subst, menv, ug, (pos,equation))::tl ->
         let _, _, (_, l, r, o), m,_ = Equality.open_equality equation in
         try
           let other = if pos = Utils.Left then r else l in
@@ -528,7 +528,7 @@ let rec demodulation_aux ?from ?(typecheck=false)
 exception Foo
 
 (** demodulation, when target is an equality *)
-let rec demodulation_equality ?from newmeta env table sign target =
+let rec demodulation_equality ?from eq_uri newmeta env table target =
   let module C = Cic in
   let module S = CicSubstitution in
   let module M = CicMetaSubst in
@@ -550,7 +550,7 @@ let rec demodulation_equality ?from newmeta env table sign target =
   let metasenv' = (* metasenv @ *) metas in
   let maxmeta = ref newmeta in
   
-  let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
+  let build_newtarget is_left (t, subst, menv, ug, eq_found) =
     
     if Utils.debug_metas then
       begin
@@ -574,93 +574,12 @@ let rec demodulation_equality ?from newmeta env table sign target =
       let name = C.Name "x" in
       let bo' =
         let l, r = if is_left then t, S.lift 1 right else S.lift 1 left, t in
-          C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
-                  S.lift 1 eq_ty; l; r]
+          C.Appl [C.MutInd (eq_uri, 0, []); S.lift 1 eq_ty; l; r]
       in
-      if sign = Utils.Positive then
           (bo, (Equality.Step (subst,(Equality.Demodulation, id,(pos,id'),
           (Cic.Lambda (name, ty, bo'))))))
-      else
-        assert false
-(*
-        begin
-        prerr_endline "***************************************negative";
-        let metaproof = 
-          incr maxmeta;
-          let irl =
-            CicMkImplicit.identity_relocation_list_for_metavariable context in
-(*        debug_print (lazy (Printf.sprintf "\nADDING META: %d\n" !maxmeta)); *)
-(*        print_newline (); *)
-          C.Meta (!maxmeta, irl)
-        in
-          let eq_found =
-            let proof'_old' =
-              let termlist =
-                if pos = Utils.Left then [ty; what; other]
-                else [ty; other; what]
-              in
-              Equality.ProofSymBlock (termlist, proof'_old)
-            in
-            let proof'_new' = assert false (* not implemented *) in
-            let what, other =
-              if pos = Utils.Left then what, other else other, what
-            in
-            pos, 
-              Equality.mk_equality 
-                (0, (proof'_new',proof'_old'), 
-                (ty, other, what, Utils.Incomparable),menv')
-          in
-          let target_proof =
-            let pb =
-              Equality.ProofBlock 
-                (subst, eq_URI, (name, ty), bo',
-                 eq_found, Equality.BasicProof (Equality.empty_subst,metaproof))
-            in
-            assert false, (* not implemented *)
-            (match snd proof with
-            | Equality.BasicProof _ ->
-                (* print_endline "replacing a BasicProof"; *)
-                pb
-            | Equality.ProofGoalBlock (_, parent_proof) ->
-  (* print_endline "replacing another ProofGoalBlock"; *)
-                Equality.ProofGoalBlock (pb, parent_proof)
-            | _ -> assert false)
-          in
-        let refl =
-          C.Appl [C.MutConstruct (* reflexivity *)
-                    (LibraryObjects.eq_URI (), 0, 1, []);
-                  eq_ty; if is_left then right else left]          
-        in
-        (bo,
-         (assert false, (* not implemented *)
-         Equality.ProofGoalBlock 
-           (Equality.BasicProof (Equality.empty_subst,refl), snd target_proof)))
-      end
-*)
     in
-    let newmenv = (* Inference.filter subst *) menv in
-(*
-    let _ = 
-      if Utils.debug_metas then 
-        try ignore(CicTypeChecker.type_of_aux'
-          newmenv context 
-            (Equality.build_proof_term newproof) ugraph);
-          () 
-        with exc ->                   
-          prerr_endline "sempre lui";
-          prerr_endline (Subst.ppsubst subst);
-          prerr_endline (CicPp.ppterm 
-            (Equality.build_proof_term newproof));
-          prerr_endline ("+++++++++++++termine: " ^ (CicPp.ppterm t));
-          prerr_endline ("+++++++++++++what: " ^ (CicPp.ppterm what));
-          prerr_endline ("+++++++++++++other: " ^ (CicPp.ppterm other));
-          prerr_endline ("+++++++++++++subst: " ^ (Subst.ppsubst subst));
-          prerr_endline ("+++++++++++++newmenv: " ^ (CicMetaSubst.ppmetasenv []
-            newmenv));
-          raise exc;
-      else () 
-    in
-*)
+    let newmenv = menv in
     let left, right = if is_left then newterm, right else left, newterm in
     let ordering = !Utils.compare_terms left right in
     let stat = (eq_ty, left, right, ordering) in
@@ -684,7 +603,7 @@ let rec demodulation_equality ?from newmeta env table sign target =
             (Equality.meta_convertibility_eq target newtarget) then
               newmeta, newtarget
           else 
-            demodulation_equality ?from newmeta env table sign newtarget
+            demodulation_equality ?from eq_uri newmeta env table newtarget
     | None ->
         let res = demodulation_aux metasenv' context ugraph table 0 right in
         if Utils.debug_res then check_res res "demod result 1"; 
@@ -695,7 +614,7 @@ let rec demodulation_equality ?from newmeta env table sign target =
                   (Equality.meta_convertibility_eq target newtarget) then
                     newmeta, newtarget
                 else
-                   demodulation_equality ?from newmeta env table sign newtarget
+                   demodulation_equality ?from eq_uri newmeta env table newtarget
           | None ->
               newmeta, target
   in
@@ -719,6 +638,7 @@ let rec betaexpand_term
   let res, lifted_term = 
     match term with
     | C.Meta (i, l) ->
+        let l = [] in
         let l', lifted_l =
           List.fold_right
             (fun arg (res, lifted_tl) ->
@@ -790,8 +710,8 @@ let rec betaexpand_term
 
     | C.Appl l ->
         let l', lifted_l =
-          List.fold_right
-            (fun arg (res, lifted_tl) ->
+          List.fold_left
+            (fun (res, lifted_tl) arg ->
                let arg_res, lifted_arg =
                  betaexpand_term metasenv context ugraph table lift_amount arg
                in
@@ -807,7 +727,7 @@ let rec betaexpand_term
                         lifted_arg::r, s, m, ug, eq_found)
                      res),
                 lifted_arg::lifted_tl)
-            ) l ([], [])
+            ) ([], []) (List.rev l)
         in
         (List.map
            (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
@@ -841,8 +761,7 @@ let rec betaexpand_term
    index: its updated value is also returned
 *)
 let superposition_right 
-  ?(subterms_only=false) newmeta (metasenv, context, ugraph) table target 
-=
+  ?(subterms_only=false) eq_uri newmeta (metasenv, context, ugraph) table target=
   let module C = Cic in
   let module S = CicSubstitution in
   let module M = CicMetaSubst in
@@ -873,7 +792,7 @@ let superposition_right
         in
         (res left right), (res right left)
   in
-  let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
+  let build_new ordering (bo, s, m, ug, eq_found) =
     if Utils.debug_metas then 
       ignore (check_target context (snd eq_found) "buildnew1" );
     
@@ -891,8 +810,7 @@ let superposition_right
       let bo'' =
         let l, r =
           if ordering = U.Gt then bo, S.lift 1 right else S.lift 1 left, bo in
-        C.Appl [C.MutInd (LibraryObjects.eq_URI (), 0, []);
-                S.lift 1 eq_ty; l; r]
+        C.Appl [C.MutInd (eq_uri, 0, []); S.lift 1 eq_ty; l; r]
       in
       bo',
         Equality.Step 
@@ -939,7 +857,7 @@ let rec demodulation_theorem newmeta env table theorem =
   let term, termty, metas = theorem in
   let metasenv' = metas in
   
-  let build_newtheorem (t, subst, menv, ug, (eq_found, eq_URI)) =
+  let build_newtheorem (t, subst, menv, ug, eq_found) =
     let pos, equality = eq_found in
     let (_, proof', (ty, what, other, _), menv',id) = 
       Equality.open_equality equality in
@@ -1018,7 +936,7 @@ let fix_expansion goal posu (t, subst, menv, ug, eq_f) =
  * expansion builds a new goal *)
 let build_newgoal context goal posu rule expansion =
   let goalproof,_,_,_,_,_ = open_goal goal in
-  let (t,subst,menv,ug,(eq_found,eq_URI)) = fix_expansion goal posu expansion in
+  let (t,subst,menv,ug,eq_found) = fix_expansion goal posu expansion in
   let pos, equality = eq_found in
   let (_, proof', (ty, what, other, _), menv',id) = 
     Equality.open_equality equality in
@@ -1042,17 +960,48 @@ let build_newgoal context goal posu rule expansion =
    returns a list of new clauses inferred with a left superposition step
    the negative equation "target" and one of the positive equations in "table"
 *)
-let superposition_left (metasenv, context, ugraph) table goal = 
+let superposition_left (metasenv, context, ugraph) table goal maxmeta = 
+  let names = Utils.names_of_context context in
   let proof,menv,eq,ty,l,r = open_goal goal in
-  let c = 
-    Utils.compare_weights ~normalize:true
-      (Utils.weight_of_term l) (Utils.weight_of_term r)
-  in
-  let big,small,possmall = 
-    match c with Utils.Gt -> l,r,Utils.Right | _ -> r,l,Utils.Left
+  let c = !Utils.compare_terms l r in
+  let newgoals = 
+    if c = Utils.Incomparable then
+      begin
+      let expansionsl, _ = betaexpand_term menv context ugraph table 0 l in
+      let expansionsr, _ = betaexpand_term menv context ugraph table 0 r in
+      (* prerr_endline "incomparable"; 
+      prerr_endline (string_of_int (List.length expansionsl));
+      prerr_endline (string_of_int (List.length expansionsr));
+      *)
+      List.map (build_newgoal context goal Utils.Right Equality.SuperpositionLeft) expansionsl
+      @
+      List.map (build_newgoal context goal Utils.Left Equality.SuperpositionLeft) expansionsr
+      end
+    else
+        match c with 
+        | Utils.Gt -> (* prerr_endline "GT"; *) 
+            let big,small,possmall = l,r,Utils.Right in
+            let expansions, _ = betaexpand_term menv context ugraph table 0 big in
+            List.map 
+              (build_newgoal context goal possmall Equality.SuperpositionLeft) 
+              expansions
+        | Utils.Lt -> (* prerr_endline "LT"; *) 
+            let big,small,possmall = r,l,Utils.Left in
+            let expansions, _ = betaexpand_term menv context ugraph table 0 big in
+            List.map 
+              (build_newgoal context goal possmall Equality.SuperpositionLeft) 
+              expansions
+        | Utils.Eq -> []
+        | _ ->
+            prerr_endline 
+              ("NOT GT, LT NOR EQ : "^CicPp.pp l names^" - "^CicPp.pp r names);
+            assert false
   in
-  let expansions, _ = betaexpand_term menv context ugraph table 0 big in
-  List.map (build_newgoal context goal possmall Equality.SuperpositionLeft) expansions
+  (* rinfresco le meta *)
+  List.fold_right
+    (fun g (max,acc) -> 
+       let max,g = Equality.fix_metas_goal max g in max,g::acc) 
+    newgoals (maxmeta,[])
 ;;
 
 (** demodulation, when the target is a goal *)
@@ -1084,4 +1033,4 @@ let rec demodulation_goal env table goal =
   | None -> do_right ()
 ;;
 
-let get_stats () = <:show<Indexing.>> ;;
+let get_stats () = "" ;;