module Index(B : Orderings.Blob) = struct
   module U = FoUtils.Utils(B)
+  module Unif = FoUnif.Founif(B)
+  module Pp = Pp.Pp(B)
 
   module ClauseOT =
     struct 
       type dataset = ClauseSet.t
     = Make(FotermIndexable)(ClauseSet)
 
-  let index_unit_clause t = function
+  let are_invertible maxvar varlist l r =
+    let _,_,subst = U.relocate maxvar varlist FoSubst.id_subst in
+    let l = FoSubst.apply_subst subst l in
+      try (ignore(Unif.alpha_eq l r);true) with
+         FoUnif.UnificationFailure _ -> false
+  
+  let index_unit_clause maxvar t = function
     | (_,Terms.Equation (l,_,_,Terms.Gt),_,_) as c -> 
           DT.index t l (Terms.Left2Right, c)
     | (_,Terms.Equation (_,r,_,Terms.Lt),_,_) as c -> 
           DT.index t r (Terms.Right2Left, c)
-    | (_,Terms.Equation (l,r,_,Terms.Incomparable),_,_) as c ->  
+    | (_,Terms.Equation (l,r,_,Terms.Incomparable),vl,_) as c ->
+       if are_invertible maxvar vl l r then
+         (prerr_endline ("Invertible " ^ (Pp.pp_foterm l) ^ "=" ^
+            (Pp.pp_foterm r));
+           DT.index t l (Terms.Left2Right, c))
+          else
           DT.index  
            (DT.index t l (Terms.Left2Right, c))
            r (Terms.Right2Left, c)
 
       match simplify atable maxvar bag new_clause with
         | bag,None -> bag,None (* new_clause has been discarded *)
         | bag,(Some clause) ->
-            let ctable = IDX.index_unit_clause IDX.DT.empty clause in
+            let ctable = IDX.index_unit_clause maxvar IDX.DT.empty clause in
             let bag, alist, atable = 
               List.fold_left 
                 (fun (bag, alist, atable) c ->
                      |bag,None -> (bag,alist,atable)
                         (* an active clause as been discarded *)
                      |bag,Some c1 ->
-                        bag, c :: alist, IDX.index_unit_clause atable c)
+                        bag, c :: alist, IDX.index_unit_clause maxvar atable c)
                 (bag,[],IDX.DT.empty) alist
             in
               bag, Some (clause, (alist,atable))
     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
       let atable1 =
         if new_cl then atable else
-        IDX.index_unit_clause atable cl
+        IDX.index_unit_clause maxvar atable cl
       in
         (* Simplification of new_clause with :      *
          * - actives and cl if new_clause is not cl *
           | bag,Some clause ->
               (* Simplification of each active clause with clause *
                * which is the simplified form of new_clause       *)
-              let ctable = IDX.index_unit_clause IDX.DT.empty clause in
+              let ctable = IDX.index_unit_clause maxvar IDX.DT.empty clause in
               let bag, newa, alist, atable = 
                 List.fold_left 
                   (fun (bag, newa, alist, atable) c ->
                        |bag,Some c1 ->
                             if (c1 == c) then 
                               bag, newa, c :: alist,
-                            IDX.index_unit_clause atable c
+                            IDX.index_unit_clause maxvar atable c
                             else
                               bag, c1 :: newa, alist, atable)                  
                   (bag,[],[],IDX.DT.empty) alist
                   | bag,(None, Some _) -> bag,None
                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
                       let alist,atable =
-                        (clause::alist, IDX.index_unit_clause atable clause)
+                     (clause::alist, IDX.index_unit_clause maxvar atable clause)
                       in
                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
                           bag (newa@tl)
       (* We demodulate actives clause with current until all *
        * active clauses are reduced w.r.t each other         *)
       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
-      let ctable = IDX.index_unit_clause IDX.DT.empty current in
+      let ctable = IDX.index_unit_clause maxvar IDX.DT.empty current in
       (* let bag, (alist, atable) = 
         let bag, alist = 
           HExtlib.filter_map_acc (simplify ctable) bag alist
         (* We add current to active clauses so that it can be *
          * superposed with itself                             *)
       let alist, atable = 
-        current :: alist, IDX.index_unit_clause atable current
+        current :: alist, IDX.index_unit_clause maxvar atable current
       in
         debug "Indexed";
       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in