]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/tactics/auto.ml
added alternative implementation for hMysql relying
[helm.git] / helm / software / components / tactics / auto.ml
index c65826a4feab8e9e319df6219c65a8db70d516ca..c16c41152cfbb05aca9d9a45a04586a3b3e0c0bc 100644 (file)
 open AutoTypes;;
 open AutoCache;;
 
-let debug_print s = () (*prerr_endline s;;*)
+let debug = true;;
+let debug_print s = 
+  if debug then prerr_endline (Lazy.force s);;
 
-(* {{{ *********** local given_clause wrapper ***********)
+let elems = ref [] ;;
 
-let given_clause dbd ?tables maxm ?auto cache subst flags smart_flag status =
-  let active,passive,bag,cache,maxmeta,goal_steps,saturation_steps,timeout =
-    match tables with
-    | None -> 
-        (* first time, do a huge saturation *)
-        let bag, equalities, cache, maxmeta = 
-          Saturation.find_equalities dbd status ?auto smart_flag cache
-        in
-        let passive = Saturation.make_passive equalities in
-        let active = Saturation.make_active [] in
-        let goal_steps, saturation_steps, timeout =
-          if flags.use_only_paramod then max_int,max_int,flags.timeout
-          else 82, 82, infinity
-        in
-        let maxm = max maxm maxmeta in
-        active,passive,bag,cache,maxm,goal_steps,saturation_steps,timeout
-    | Some (active,passive,bag,oldcache) -> 
-        (* saturate a bit more if cache cahnged *)
-        let bag, equalities, cache, maxm = 
-          if cache_size oldcache <> cache_size cache then 
-            Saturation.saturate_more
-              bag active maxm status smart_flag ?auto cache
-          else
-            bag, [], cache, maxm
-        in
-        let minsteps = List.length equalities in
-        let passive = Saturation.add_to_passive equalities passive in
-        let goal_steps, saturation_steps, timeout =
-          if flags.use_only_paramod then max_int,max_int,flags.timeout
-          else max 50 minsteps, minsteps, infinity
-        in
-        active,passive,bag,cache,maxm,goal_steps,saturation_steps,timeout
+(* closing a term w.r.t. its metavariables
+   very naif version: it does not take dependencies into account *)
+
+let naif_closure t metasenv context =
+  let metasenv = ProofEngineHelpers.sort_metasenv metasenv in
+  let n = List.length metasenv in
+  let what = List.map (fun (i,cc,ty) -> Cic.Meta(i,[])) metasenv in
+  let _,with_what =
+    List.fold_left
+      (fun (i,acc) (_,cc,ty) -> (i-1,Cic.Rel i::acc)) 
+      (n,[]) metasenv 
   in
-  let res,actives,passives,maxmeta = 
-    Saturation.given_clause bag maxmeta status active passive 
-      goal_steps saturation_steps timeout
+  let t = CicSubstitution.lift n t in
+  let body =
+    ProofEngineReduction.replace_lifting 
+      ~equality:(fun c t1 t2 ->
+         match t1,t2 with
+         | Cic.Meta(i,_),Cic.Meta(j,_) -> i = j
+         | _ -> false) 
+      ~context ~what ~with_what ~where:t 
   in
-  res,actives,passives,bag,cache,maxmeta
+  let _, t =
+    List.fold_left
+      (fun (n,t) (_,cc,ty) -> 
+        n-1, Cic.Lambda(Cic.Name ("x_"^string_of_int n),
+               CicSubstitution.lift n ty,t))
+      (n-1,body) metasenv 
+  in
+  t
 ;;
+  
+(* functions for retrieving theorems *)
+
+exception FillingFailure of AutoCache.cache * int
+
+let rec unfold context = function
+  | Cic.Prod(name,s,t) -> 
+      let t' = unfold ((Some (name,Cic.Decl s))::context) t in
+        Cic.Prod(name,s,t')        
+  | t -> ProofEngineReduction.unfold context t
+
+let find_library_theorems dbd proof goal = 
+  let univ = MetadataQuery.universe_of_goal ~dbd false proof goal in
+  let terms = List.map CicUtil.term_of_uri univ in
+  List.map 
+    (fun t -> 
+       (t,fst(CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph))) 
+    terms
+
+let find_context_theorems context metasenv =
+  let l,_ =
+    List.fold_left
+      (fun (res,i) ctxentry ->
+         match ctxentry with
+           | Some (_,Cic.Decl t) -> 
+               (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
+           | Some (_,Cic.Def (_,Some t)) ->
+               (Cic.Rel i, CicSubstitution.lift i t)::res,i+1
+           | Some (_,Cic.Def (_,None)) ->
+               let t = Cic.Rel i in
+               let ty,_ = 
+                 CicTypeChecker.type_of_aux' 
+                   metasenv context t CicUniv.empty_ugraph
+               in
+                 (t,ty)::res,i+1
+           |  _ -> res,i+1)
+      ([],1) context
+  in l
+
+let rec is_an_equality = function
+  | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] 
+    when (LibraryObjects.is_eq_URI uri) -> true
+  | Cic.Prod (_, _, t) -> is_an_equality t
+  | _ -> false
+;;
+
+let partition_equalities =
+  List.partition (fun (_,ty) -> is_an_equality ty)
+
+
+let default_auto maxm _ _ cache _ _ _ _ = [],cache,maxm ;; 
+
+
+let is_unit_equation context metasenv oldnewmeta term = 
+  let head, metasenv, args, newmeta =
+    TermUtil.saturate_term oldnewmeta metasenv context term 0
+  in
+  let propositional_args = 
+    HExtlib.filter_map
+      (function 
+      | Cic.Meta(i,_) -> 
+          let _,_,mt = CicUtil.lookup_meta i metasenv in
+          let sort,u = 
+            CicTypeChecker.type_of_aux' metasenv context mt 
+              CicUniv.empty_ugraph
+          in
+          let b, _ = 
+            CicReduction.are_convertible ~metasenv context 
+              sort (Cic.Sort Cic.Prop) u
+          in
+          if b then Some i else None 
+      | _ -> assert false)
+    args
+  in
+    if propositional_args = [] then 
+      let newmetas = List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv in
+        Some (args,metasenv,newmetas,head,newmeta)
+    else None
+;;
+
+let get_candidates universe cache t =
+  let candidates= 
+    (Universe.get_candidates universe t)@(AutoCache.get_candidates cache t)
+  in 
+  let debug_msg =
+    (lazy ("candidates for " ^ (CicPp.ppterm t) ^ " = " ^ 
+             (String.concat "\n" (List.map CicPp.ppterm candidates)))) in
+  debug_print debug_msg;
+  candidates
+;;
+
+let only signature context t =
+  try
+    let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph in
+    let consts = MetadataConstraints.constants_of ty in
+    let b = MetadataConstraints.UriManagerSet.subset consts signature in
+    if b then b 
+    else
+      try
+        let ty' = unfold context ty in
+        let consts' = MetadataConstraints.constants_of ty' in
+        MetadataConstraints.UriManagerSet.subset consts' signature 
+      with _-> false
+  with _ -> false
+;;
+
+let not_default_eq_term t =
+  try
+    let uri = CicUtil.uri_of_term t in
+      not (LibraryObjects.in_eq_URIs uri)
+  with Invalid_argument _ -> true
+
+let retrieve_equations signature universe cache context=
+  match LibraryObjects.eq_URI() with
+    | None -> [] 
+    | Some eq_uri -> 
+        let eq_uri = UriManager.strip_xpointer eq_uri in
+        let fake= Cic.Meta(-1,[]) in
+        let fake_eq = Cic.Appl [Cic.MutInd (eq_uri,0, []);fake;fake;fake] in
+        let candidates = get_candidates universe cache fake_eq in
+    (* defaults eq uris are built-in in auto *)
+        let candidates = List.filter not_default_eq_term candidates in
+        let candidates = List.filter (only signature context) candidates in
+        List.iter (fun t -> debug_print (lazy (CicPp.ppterm t))) candidates;
+        candidates
+
+let build_equality bag head args proof newmetas maxmeta = 
+  match head with
+  | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] ->
+      let p =
+        if args = [] then proof else Cic.Appl (proof::args)
+      in 
+      let o = !Utils.compare_terms t1 t2 in
+      let stat = (ty,t1,t2,o) in
+      (* let w = compute_equality_weight stat in *)
+      let w = 0 in 
+      let proof = Equality.Exact p in
+      let e = Equality.mk_equality bag (w, proof, stat, newmetas) in
+      (* to clean the local context of metas *)
+      Equality.fix_metas bag maxmeta e
+  | _ -> assert false
+;;
+
+let partition_unit_equalities context metasenv newmeta bag equations =
+  List.fold_left
+    (fun (units,other,maxmeta)(t,ty) ->
+       match is_unit_equation context metasenv maxmeta ty with
+         | Some (args,metasenv,newmetas,head,newmeta') ->
+             let maxmeta,equality =
+               build_equality bag head args t newmetas newmeta' in
+             equality::units,other,maxmeta
+         | None -> 
+             units,(t,ty)::other,maxmeta)
+    ([],[],newmeta) equations
+
+let empty_tables = 
+  (Saturation.make_active [], 
+   Saturation.make_passive [],
+   Equality.mk_equality_bag)
+
+let init_cache_and_tables dbd use_library paramod universe (proof, goal) =
+  (* the local cache in initially empty  *)
+  let cache = AutoCache.cache_empty in
+  let _, metasenv, _subst,_, _, _ = proof in
+  let signature = MetadataQuery.signature_of metasenv goal in
+  let newmeta = CicMkImplicit.new_meta metasenv [] in
+  let _,context,_ = CicUtil.lookup_meta goal metasenv in
+  let ct = find_context_theorems context metasenv in
+  debug_print 
+    (lazy ("ho trovato nel contesto " ^ (string_of_int (List.length ct))));
+  let lt = 
+    if use_library then 
+       find_library_theorems dbd metasenv goal 
+    else [] in
+  debug_print 
+    (lazy ("ho trovato nella libreria " ^ (string_of_int (List.length lt))));
+  let cache = cache_add_list cache context (ct@lt) in  
+  let equations = 
+    retrieve_equations signature universe cache context in
+  debug_print 
+    (lazy ("ho trovato equazioni n. "^(string_of_int (List.length equations))));
+  let eqs_and_types =
+    HExtlib.filter_map 
+      (fun t -> 
+         let ty,_ =
+           CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph in
+           (* retrieve_equations could also return flexible terms *)
+           if is_an_equality ty then Some(t,ty) 
+           else
+             try
+               let ty' = unfold context ty in
+                 if is_an_equality ty' then Some(t,ty') else None
+             with _ -> None) (* catturare l'eccezione giusta di unfold *)
+      equations in
+  let bag = Equality.mk_equality_bag () in
+  let units, other_equalities, newmeta = 
+    partition_unit_equalities context metasenv newmeta bag eqs_and_types in
+  (* let env = (metasenv, context, CicUniv.empty_ugraph) in 
+    let equalities = 
+    let eq_uri = 
+    match LibraryObjects.eq_URI() with
+      | None ->assert false
+      | Some eq_uri -> eq_uri in
+    Saturation.simplify_equalities bag eq_uri env units in *)
+  let passive = Saturation.make_passive units in
+  let no = List.length units in
+  let active = Saturation.make_active [] in
+  let active,passive,newmeta = 
+    if paramod then active,passive,newmeta
+    else
+      Saturation.pump_actives 
+        context bag newmeta active passive (no+1) infinity
+  in 
+    (active,passive,bag),cache,newmeta
+
+let fill_hypothesis context metasenv oldnewmeta term tables (universe:Universe.universe) cache auto fast = 
+  let head, metasenv, args, newmeta =
+    TermUtil.saturate_term oldnewmeta metasenv context term 0
+  in
+  let propositional_args = 
+    HExtlib.filter_map
+      (function 
+      | Cic.Meta(i,_) -> 
+          let _,_,mt = CicUtil.lookup_meta i metasenv in
+          let sort,u = 
+            CicTypeChecker.type_of_aux' metasenv context mt 
+              CicUniv.empty_ugraph
+          in
+          let b, _ = 
+            CicReduction.are_convertible ~metasenv context 
+              sort (Cic.Sort Cic.Prop) u
+          in
+          if b then Some i else None 
+      | _ -> assert false)
+    args
+  in
+  let results,cache,newmeta = 
+    if propositional_args = [] then 
+      let newmetas = List.filter (fun (i,_,_) -> i >= oldnewmeta) metasenv in
+      [args,metasenv,newmetas,head,newmeta],cache,newmeta
+    else
+      (*
+      let proof = 
+        None,metasenv,term,term (* term non e' significativo *)
+      in *)
+      let flags = 
+        if fast then
+          {AutoTypes.default_flags() with 
+           AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
+           maxwidth = 2;maxdepth = 2;
+           use_paramod=true;use_only_paramod=false}
+        else
+          {AutoTypes.default_flags() with
+           AutoTypes.timeout = Unix.gettimeofday() +. 1.0;
+           maxwidth = 2;maxdepth = 4;
+           use_paramod=true;use_only_paramod=false} 
+      in
+      match auto newmeta tables universe cache context metasenv propositional_args flags with
+      | [],cache,newmeta -> raise (FillingFailure (cache,newmeta))
+      | substs,cache,newmeta ->
+          List.map 
+            (fun subst ->
+              let metasenv = 
+                CicMetaSubst.apply_subst_metasenv subst metasenv
+              in
+              let head = CicMetaSubst.apply_subst subst head in
+              let newmetas = 
+                List.filter (fun (i,_,_) ->i >= oldnewmeta) metasenv 
+              in
+              let args = List.map (CicMetaSubst.apply_subst subst) args in
+              let newm = CicMkImplicit.new_meta metasenv subst in
+                args,metasenv,newmetas,head,max newm newmeta)
+            substs, cache, newmeta
+  in
+  results,cache,newmeta
 
-(* }}} ****************************************************************)
+let build_equalities auto context metasenv tables universe cache newmeta equations =
+  List.fold_left 
+    (fun (facts,cache,newmeta) (t,ty) ->
+       (* in any case we add the equation to the cache *)
+       let cache = AutoCache.cache_add_list cache context [(t,ty)] in
+       try
+         let saturated,cache,newmeta = 
+           fill_hypothesis context metasenv newmeta ty tables universe cache auto true
+         in
+         let (active,passive,bag) = tables in
+         let eqs,bag,newmeta = 
+           List.fold_left 
+             (fun (acc,bag,newmeta) (args,metasenv,newmetas,head,newmeta') ->
+                let maxmeta,equality =
+                  build_equality bag head args t newmetas newmeta'
+                in
+                  equality::acc,bag,maxmeta)
+             ([],bag,newmeta) saturated
+         in
+           (eqs@facts, cache, newmeta)
+       with FillingFailure (cache,newmeta) ->
+         (* if filling hypothesis fails we add the equation to
+            the cache *)
+         (facts,cache,newmeta)
+      )
+    ([],cache,newmeta) equations
 
-(* {{{ **************** applyS *******************)
+let close_more tables maxmeta context status auto universe cache =
+  let (active,passive,bag) = tables in
+  let proof, goalno = status in
+  let _, metasenv,_subst,_,_, _ = proof in  
+  let signature = MetadataQuery.signature_of metasenv goalno in
+  let equations = retrieve_equations signature universe cache context in
+  let eqs_and_types =
+    HExtlib.filter_map 
+      (fun t -> 
+         let ty,_ =
+           CicTypeChecker.type_of_aux' metasenv context t CicUniv.empty_ugraph in
+           (* retrieve_equations could also return flexible terms *)
+           if is_an_equality ty then Some(t,ty) else None)
+      equations in
+  let units, cache, maxm = 
+      build_equalities auto context metasenv tables universe cache maxmeta eqs_and_types in
+  debug_print (lazy (">>>>>>> gained from a new context saturation >>>>>>>>>" ^
+    string_of_int maxm));
+  List.iter
+    (fun e -> debug_print (lazy (Equality.string_of_equality e))) 
+    units;
+  debug_print (lazy ">>>>>>>>>>>>>>>>>>>>>>");
+  let passive = Saturation.add_to_passive units passive in
+  let no = List.length units in
+  debug_print (lazy ("No = " ^ (string_of_int no)));
+  let active,passive,newmeta = 
+    Saturation.pump_actives context bag maxm active passive (no+1) infinity
+  in 
+    (active,passive,bag),cache,newmeta
+
+let find_context_equalities 
+  maxmeta bag context proof (universe:Universe.universe) cache 
+=
+  let module C = Cic in
+  let module S = CicSubstitution in
+  let module T = CicTypeChecker in
+  let _,metasenv,_subst,_,_, _ = proof in
+  let newmeta = max (ProofEngineHelpers.new_meta_of_proof ~proof) maxmeta in
+  (* if use_auto is true, we try to close the hypothesis of equational
+    statements using auto; a naif, and probably wrong approach *)
+  let rec aux cache index newmeta = function
+    | [] -> [], newmeta,cache
+    | (Some (_, C.Decl (term)))::tl ->
+        debug_print
+          (lazy
+             (Printf.sprintf "Examining: %d (%s)" index (CicPp.ppterm term)));
+        let do_find context term =
+          match term with
+          | C.Prod (name, s, t) when is_an_equality t ->
+              (try 
+                
+                let term = S.lift index term in
+                let saturated,cache,newmeta = 
+                  fill_hypothesis context metasenv newmeta term 
+                    empty_tables universe cache default_auto false
+                in
+                let eqs,newmeta = 
+                  List.fold_left 
+                   (fun (acc,newmeta) (args,metasenv,newmetas,head,newmeta') ->
+                     let newmeta, equality = 
+                       build_equality
+                         bag head args (Cic.Rel index) newmetas (max newmeta newmeta')
+                     in
+                     equality::acc, newmeta + 1)
+                   ([],newmeta) saturated
+                in
+                 eqs, newmeta, cache
+              with FillingFailure (cache,newmeta) ->
+                [],newmeta,cache)
+          | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
+              when LibraryObjects.is_eq_URI uri ->
+              let term = S.lift index term in
+              let newmeta, e = 
+                build_equality bag term [] (Cic.Rel index) [] newmeta
+              in
+              [e], (newmeta+1),cache
+          | _ -> [], newmeta, cache
+        in 
+        let eqs, newmeta, cache = do_find context term in
+        let rest, newmeta,cache = aux cache (index+1) newmeta tl in
+        List.map (fun x -> index,x) eqs @ rest, newmeta, cache
+    | _::tl ->
+        aux cache (index+1) newmeta tl
+  in
+  let il, maxm, cache = 
+    aux cache 1 newmeta context 
+  in
+  let indexes, equalities = List.split il in
+  indexes, equalities, maxm, cache
+;;
+
+(***************** applyS *******************)
 
 let new_metasenv_and_unify_and_t 
-  dbd proof goal ?tables newmeta' metasenv' context term' ty termty goal_arity 
+ dbd flags universe proof goal ?tables newmeta' metasenv' 
+ context term' ty termty goal_arity 
 =
  let (consthead,newmetasenv,arguments,_) =
-   ProofEngineHelpers.saturate_term newmeta' metasenv' context termty goal_arity in
+   TermUtil.saturate_term newmeta' metasenv' context termty goal_arity in
  let term'' = 
    match arguments with [] -> term' | _ -> Cic.Appl (term'::arguments) 
  in
  let proof',oldmetasenv =
-  let (puri,metasenv,pbo,pty) = proof in
-   (puri,newmetasenv,pbo,pty),metasenv
+  let (puri,metasenv,_subst,pbo,pty, attrs) = proof in
+   (puri,newmetasenv,_subst,pbo,pty, attrs),metasenv
  in
  let goal_for_paramod =
   match LibraryObjects.eq_URI () with
@@ -94,13 +479,16 @@ let new_metasenv_and_unify_and_t
  in
  let newmeta = CicMkImplicit.new_meta newmetasenv (*subst*) [] in
  let metasenv_for_paramod = (newmeta,context,goal_for_paramod)::newmetasenv in
- let proof'' = let uri,_,p,ty = proof' in uri,metasenv_for_paramod,p,ty in
+ let proof'' = 
+   let uri,_,_subst,p,ty, attrs = proof' in 
+   uri,metasenv_for_paramod,_subst,p,ty, attrs 
+ in
  let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
  let proof''',goals =
   ProofEngineTypes.apply_tactic
     (EqualityTactics.rewrite_tac ~direction:`RightToLeft
       ~pattern:(ProofEngineTypes.conclusion_pattern None) 
-        (Cic.Meta(newmeta,irl)))
+        (Cic.Meta(newmeta,irl)) [])
    (proof'',goal)
  in
  let goal = match goals with [g] -> g | _ -> assert false in
@@ -108,18 +496,18 @@ let new_metasenv_and_unify_and_t
    PrimitiveTactics.apply_with_subst ~term:term'' ~subst:[] (proof''',goal) 
  in
  match 
-   let cache, flags = cache_empty, default_flags() in
-   let flags = 
-     {flags with use_only_paramod=true;timeout=Unix.gettimeofday() +. 30.0} 
+   let (active, passive,bag), cache, maxmeta =
+     init_cache_and_tables dbd flags.use_library true universe (proof'''',newmeta)
    in
-   given_clause dbd ?tables 0 cache [] flags true (proof'''',newmeta) 
+     Saturation.given_clause bag maxmeta (proof'''',newmeta) active passive 
+       max_int max_int flags.timeout
  with
- | None, active, passive, bag,_,_ -> 
+ | None, _,_,_ -> 
      raise (ProofEngineTypes.Fail (lazy ("FIXME: propaga le tabelle"))) 
- | Some (_,proof''''',_), active, passive,bag,_,_  ->
+ | Some (_,proof''''',_), active,passive,_  ->
      subst,proof''''',
      ProofEngineHelpers.compare_metasenvs ~oldmetasenv
-       ~newmetasenv:(let _,m,_,_ = proof''''' in m),  active, passive
+       ~newmetasenv:(let _,m,_subst,_,_, _ = proof''''' in m),  active, passive
 ;;
 
 let rec count_prods context ty =
@@ -127,11 +515,11 @@ let rec count_prods context ty =
     Cic.Prod (n,s,t) -> 1 + count_prods (Some (n,Cic.Decl s)::context) t
   | _ -> 0
 
-let apply_smart ~dbd ~term ~subst ?tables (proof, goal) =
+let apply_smart ~dbd ~term ~subst ~universe ?tables flags (proof, goal) =
  let module T = CicTypeChecker in
  let module R = CicReduction in
  let module C = Cic in
-  let (_,metasenv,_,_) = proof in
+  let (_,metasenv,_subst,_,_, _) = proof in
   let metano,context,ty = CicUtil.lookup_meta goal metasenv in
   let newmeta = CicMkImplicit.new_meta metasenv subst in
    let exp_named_subst_diff,newmeta',newmetasenvfragment,term' =
@@ -173,15 +561,13 @@ let apply_smart ~dbd ~term ~subst ?tables (proof, goal) =
    let termty = CicSubstitution.subst_vars exp_named_subst_diff termty in
    let goal_arity = count_prods context ty in
    let subst, proof, gl, active, passive =
-    new_metasenv_and_unify_and_t dbd proof goal ?tables
+    new_metasenv_and_unify_and_t dbd flags universe proof goal ?tables
      newmeta' metasenv' context term' ty termty goal_arity
    in
     subst, proof, gl, active, passive
 ;;
 
-(* }}} **************** applyS **************)
-
-(* {{{ ***************** AUTO ********************)
+(****************** AUTO ********************)
 
 let mk_irl ctx = CicMkImplicit.identity_relocation_list_for_metavariable ctx;;
 let ugraph = CicUniv.empty_ugraph;;
@@ -192,30 +578,35 @@ let ppterm ctx t =
 ;;
 let is_in_prop context subst metasenv ty =
   let sort,u = typeof ~subst metasenv context ty CicUniv.empty_ugraph in
-  fst (CicReduction.are_convertible context (Cic.Sort Cic.Prop) sort u)
+  fst (CicReduction.are_convertible context sort (Cic.Sort Cic.Prop) u)
 ;;
+
 let assert_proof_is_valid proof metasenv context goalty =
-  let ty,u = typeof metasenv context proof CicUniv.empty_ugraph in
-  let b,_ = CicReduction.are_convertible context ty goalty u in
-  if not b then
+  if debug then
     begin
-      let names = 
-        List.map (function None -> None | Some (x,_) -> Some x) context 
-      in
-      prerr_endline ("PROOF:" ^ CicPp.pp proof names);
-      prerr_endline ("PROOFTY:" ^ CicPp.pp ty names);
-      prerr_endline ("GOAL:" ^ CicPp.pp goalty names);
-      prerr_endline ("METASENV:" ^ CicMetaSubst.ppmetasenv [] metasenv);
-    end;
-  assert b
+      let ty,u = typeof metasenv context proof CicUniv.empty_ugraph in
+      let b,_ = CicReduction.are_convertible context ty goalty u in
+        if not b then
+          begin
+            let names = 
+              List.map (function None -> None | Some (x,_) -> Some x) context 
+            in
+              debug_print (lazy ("PROOF:" ^ CicPp.pp proof names));
+              debug_print (lazy ("PROOFTY:" ^ CicPp.pp ty names));
+              debug_print (lazy ("GOAL:" ^ CicPp.pp goalty names));
+              debug_print (lazy ("MENV:" ^ CicMetaSubst.ppmetasenv [] metasenv));
+          end;
+        assert b
+    end
+  else ()
 ;;
+
 let assert_subst_are_disjoint subst subst' =
-  assert(List.for_all
-    (fun (i,_) -> List.for_all (fun (j,_) -> i<>j) subst') 
-    subst)
-;;
-let sort_new_elems = 
-  List.sort (fun (_,_,l1) (_,_,l2) -> List.length l1 - List.length l2) 
+  if debug then
+    assert(List.for_all
+             (fun (i,_) -> List.for_all (fun (j,_) -> i<>j) subst') 
+             subst)
+  else ()
 ;;
 
 let split_goals_in_prop metasenv subst gl =
@@ -224,12 +615,16 @@ let split_goals_in_prop metasenv subst gl =
       let _,context,ty = CicUtil.lookup_meta g metasenv in
       try
         let sort,u = typeof ~subst metasenv context ty ugraph in
-        fst (CicReduction.are_convertible context (Cic.Sort Cic.Prop) sort u)
+        let b,_ = 
+          CicReduction.are_convertible 
+            ~subst ~metasenv context sort (Cic.Sort Cic.Prop) u in
+        b
       with 
       | CicTypeChecker.AssertFailure s 
       | CicTypeChecker.TypeCheckerFailure s -> 
-          debug_print (ppterm context (CicMetaSubst.apply_subst subst ty));
-          debug_print (Lazy.force s);
+          debug_print 
+            (lazy ("NON TIPA" ^ ppterm context (CicMetaSubst.apply_subst subst ty)));
+          debug_print s;
           false)
     (* FIXME... they should type! *)
     gl
@@ -246,7 +641,7 @@ let split_goals_with_metas metasenv subst gl =
 
 let order_new_goals metasenv subst open_goals ppterm =
   let prop,rest = split_goals_in_prop metasenv subst open_goals in
-  let open_prop,closed_prop = split_goals_with_metas metasenv subst prop in
+  let closed_prop, open_prop = split_goals_with_metas metasenv subst prop in
   let open_goals =
     (List.map (fun x -> x,P) (closed_prop @ open_prop)) 
     @ 
@@ -254,12 +649,16 @@ let order_new_goals metasenv subst open_goals ppterm =
   in
   let tys = 
     List.map 
-      (fun (i,_) -> 
-        let _,_,ty = CicUtil.lookup_meta i metasenv in i,ty) open_goals 
+      (fun (i,sort) -> 
+        let _,_,ty = CicUtil.lookup_meta i metasenv in i,ty,sort) open_goals 
   in
-  debug_print ("   OPEN: "^
-    String.concat " " 
-      (List.map (fun (i,t) -> string_of_int i ^":"^ppterm t) tys));
+  debug_print (lazy ("   OPEN: "^
+    String.concat "\n" 
+      (List.map 
+         (function
+            | (i,t,P) -> string_of_int i   ^ ":"^ppterm t^ "Prop" 
+            | (i,t,T) -> string_of_int i  ^ ":"^ppterm t^ "Type")
+         tys)));
   open_goals
 ;;
 
@@ -268,170 +667,692 @@ let is_an_equational_goal = function
   | _ -> false
 ;;
 
+(*
+let prop = function (_,depth,P) -> depth < 9 | _ -> false;;
+*)
+
+let calculate_timeout flags = 
+    if flags.timeout = 0. then 
+      (debug_print (lazy "AUTO WITH NO TIMEOUT");
+       {flags with timeout = infinity})
+    else 
+      flags 
+;;
+let is_equational_case goalty flags =
+  let ensure_equational t = 
+    if is_an_equational_goal t then true 
+    else false
+    (*
+      let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
+      raise (ProofEngineTypes.Fail (lazy msg))
+    *)
+  in
+  (flags.use_paramod && is_an_equational_goal goalty) || 
+  (flags.use_only_paramod && ensure_equational goalty)
+;;
+(*
+let cache_add_success sort cache k v =
+  if sort = P then cache_add_success cache k v else cache_remove_underinspection
+  cache k
+;;
+*)
+
+type menv = Cic.metasenv
+type subst = Cic.substitution
+type goal = ProofEngineTypes.goal * int * AutoTypes.sort
+let candidate_no = ref 0;;
+type candidate = int * Cic.term
+type cache = AutoCache.cache
+type tables = 
+  Saturation.active_table * Saturation.passive_table * Equality.equality_bag
+
+type fail = 
+  (* the goal (mainly for depth) and key of the goal *)
+  goal * AutoCache.cache_key
+type op = 
+  (* goal has to be proved *)
+  | D of goal 
+  (* goal has to be cached as a success obtained using candidate as the first
+   * step *)
+  | S of goal * AutoCache.cache_key * candidate * int 
+type elem = 
+  (* menv, subst, size, operations to do, failures to cache if any op fails *)
+  menv * subst * int * op list * fail list 
+type status = 
+  (* list of computations that may lead to the solution: all op list will
+   * end with the same (S(g,_)) *)
+  elem list
+type auto_result = 
+  (* menv, subst, alternatives, tables, cache, maxmeta *)
+  | Proved of menv * subst * elem list * tables * cache * int
+  | Gaveup of tables * cache * int 
+
+
+(* the status exported to the external observer *)  
+type auto_status = 
+  (* context, (goal,candidate) list, and_list, history *)
+  Cic.context * (Cic.term * (int * Cic.term) list) list * 
+  Cic.term list * Cic.term list
+
+let d_prefix l =
+  let rec aux acc = function
+    | (D g)::tl -> aux (acc@[g]) tl
+    | _ -> acc
+  in
+    aux [] l
+;;
+let prop_only l =
+  List.filter (function (_,_,P) -> true | _ -> false) l
+;;
+
+let d_goals l =
+  let rec aux acc = function
+    | (D g)::tl -> aux (acc@[g]) tl
+    | (S _)::tl -> aux acc tl
+    | [] -> acc
+  in
+    aux [] l
+;;
+let calculate_goal_ty (goalno,_,_) s m = 
+  try
+    let _,cc,goalty = CicUtil.lookup_meta goalno m in
+    (* XXX applicare la subst al contesto? *)
+    Some (cc, CicMetaSubst.apply_subst s goalty)
+  with CicUtil.Meta_not_found i when i = goalno -> None
+;;
+let calculate_closed_goal_ty (goalno,_,_) s = 
+  try
+    let cc,_,goalty = List.assoc goalno s in
+    (* XXX applicare la subst al contesto? *)
+    Some (cc, CicMetaSubst.apply_subst s goalty)
+  with Not_found -> None
+;;
+let pp_status ctx status = 
+  if debug then 
+  let names = Utils.names_of_context ctx in
+  let pp x = 
+    let x = 
+      ProofEngineReduction.replace 
+        ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false) 
+          ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:x
+    in
+    CicPp.pp x names
+  in
+  let string_of_do m s (gi,_,_ as g) d =
+    match calculate_goal_ty g s m with
+    | Some (_,gty) -> Printf.sprintf "D(%d, %s, %d)" gi (pp gty) d
+    | None -> Printf.sprintf "D(%d, _, %d)" gi d
+  in
+  let string_of_s m su k (ci,ct) gi =
+    Printf.sprintf "S(%d, %s, %s, %d)" gi (pp k) (pp ct) ci
+  in
+  let string_of_ol m su l =
+    String.concat " | " 
+      (List.map 
+        (function 
+          | D (g,d,s) -> string_of_do m su (g,d,s) d 
+          | S ((gi,_,_),k,c,_) -> string_of_s m su k c gi) 
+        l)
+  in
+  let string_of_fl m s fl = 
+    String.concat " | " 
+      (List.map (fun ((i,_,_),ty) -> 
+         Printf.sprintf "(%d, %s)" i (pp ty)) fl)
+  in
+  let rec aux = function
+    | [] -> ()
+    | (m,s,ol,fl)::tl ->
+        Printf.eprintf "< [%s] ;;; [%s]>\n" 
+          (string_of_ol m s ol) (string_of_fl m s fl);
+        aux tl
+  in
+    Printf.eprintf "-------------------------- status -------------------\n";
+    aux status;
+    Printf.eprintf "-----------------------------------------------------\n";
+;;
+  
+let auto_status = ref [] ;;
+let auto_context = ref [];;
+let in_pause = ref false;;
+let pause b = in_pause := b;;
+let cond = Condition.create ();;
+let mutex = Mutex.create ();;
+let hint = ref None;;
+
+let step _ = Condition.signal cond;;
+let give_hint n = hint := Some n;;
+
+let check_pause _ =
+  if !in_pause then
+    begin
+      Mutex.lock mutex;
+      Condition.wait cond mutex;
+      Mutex.unlock mutex
+    end
+;;
+
+let get_auto_status _ = 
+  let status = !auto_status in
+(*
+  debug_print "status:";
+  List.iter (fun ((cand,ty),_,_,gl) ->
+    Printf.eprintf "cand: %s; ty: %s; gl: %d\n" 
+      (CicPp.ppterm cand) (CicPp.ppterm ty) (List.length gl)) status;
+*)
+  let and_list,elems,last = 
+    match status with
+    | [] -> [],[],[]
+    | (m,s,_,gl,fail)::tl ->
+        let and_list = 
+          List.map snd 
+            (HExtlib.filter_map 
+              (fun g -> calculate_goal_ty g s m) (d_goals gl))
+        in
+        let rows = 
+          (* these are the S goalsin the or list *)
+          let orlist = 
+            List.map
+              (fun (m,s,_,gl,fail) -> 
+                HExtlib.filter_map
+                  (function S (g,k,c,_) -> Some (g,k,c) | _ -> None) gl)
+              status
+          in
+          (* this function eats id from a list l::[id,x] returning x, l *)
+          let eat_tail_if_eq id l = 
+            match (List.rev l) with 
+            | ((id1,_,_),k1,c)::tl when id = id1 -> Some c, List.rev tl
+            | _ -> None, l
+          in
+          let eat_in_parallel id l =
+            let rec aux (eaten, new_l as acc) l =
+              match l with
+              | [] -> acc
+              | l::tl ->
+                  match eat_tail_if_eq id l with
+                  | None, l -> aux (eaten, new_l@[l]) tl
+                  | Some t,l -> aux (eaten@[t], new_l@[l]) tl
+            in
+            aux ([],[]) l
+          in
+          let rec eat_all rows l =
+            match l with
+            | [] -> rows
+            | elem::or_list ->
+                match List.rev elem with
+                | ((to_eat,_,_),k,_)::next_lunch ->
+                    let eaten, l = eat_in_parallel to_eat l in
+                    let eaten = HExtlib.list_uniq eaten in
+                    let rows = rows @ [k,eaten] in
+                    eat_all rows l
+                | [] -> eat_all rows or_list
+          in
+          eat_all [] orlist
+        in
+        let history = 
+          HExtlib.filter_map
+            (function (S (_,_,(_,c),_)) -> Some c | _ -> None) 
+            gl 
+        in
+(*         let rows = List.filter (fun (_,l) -> l <> []) rows in *)
+        and_list, rows, history
+  in
+  !auto_context, elems, and_list, last
+;;
+
+(* Works if there is no dependency over proofs *)
+let is_a_green_cut goalty =
+  CicUtil.is_meta_closed goalty
+;;
+let rec first_s = function
+  | (D _)::tl -> first_s tl
+  | (S (g,k,c,s))::tl -> Some ((g,k,c,s),tl)
+  | [] -> None
+;;
+let list_union l1 l2 =
+  (* TODO ottimizzare compare *)
+  HExtlib.list_uniq (List.sort compare (l1 @ l1))
+;;
+let eat_head todo id fl orlist = 
+  let rec aux acc = function
+  | [] -> [], acc
+  | (m, s, _, todo1, fl1)::tl as orlist -> 
+      let rec aux1 todo1 =
+        match first_s todo1 with
+        | None -> orlist, acc
+        | Some (((gno,_,_),_,_,_), todo11) ->
+            (* TODO confronto tra todo da ottimizzare *)
+            if gno = id && todo11 = todo then 
+              aux (list_union fl1 acc) tl
+            else 
+              aux1 todo11
+      in
+       aux1 todo1
+  in 
+    aux fl orlist
+;;
+let close_proof p ty menv context = 
+  let metas =
+    List.map fst (CicUtil.metas_of_term p @ CicUtil.metas_of_term ty)
+  in
+  let menv = List.filter (fun (i,_,_) -> List.exists ((=)i) metas) menv in
+  naif_closure p menv context
+;;
+(* XXX capire bene quando aggiungere alla cache *)
+let add_to_cache_and_del_from_orlist_if_green_cut
+  g s m cache key todo orlist fl ctx size minsize
+= 
+  let cache = cache_remove_underinspection cache key in
+  (* prima per fare la irl usavamo il contesto vero e proprio e non quello 
+   * canonico! XXX *)
+  match calculate_closed_goal_ty g s with
+  | None -> assert false
+  | Some (canonical_ctx , gty) ->
+      let goalno,depth,sort = g in
+      assert (sort = P);
+      let irl = mk_irl canonical_ctx in
+      let goal = Cic.Meta(goalno, irl) in
+      let proof = CicMetaSubst.apply_subst s goal in
+      let green_proof, closed_proof = 
+        let b = is_a_green_cut proof in
+        if not b then
+          b, (* close_proof proof gty m ctx *) proof 
+        else
+          b, proof
+      in
+      debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm key));
+      if is_a_green_cut key then
+        (* if the initia goal was closed, we cut alternatives *)
+        let _ = debug_print (lazy ("MANGIO: " ^ string_of_int goalno)) in
+        let orlist, fl = eat_head todo goalno fl orlist in
+        let cache = 
+          if size < minsize then 
+            (debug_print (lazy ("NO CACHE: 2 (size <= minsize)"));cache)
+          else 
+          (* if the proof is closed we cache it *)
+          if green_proof then cache_add_success cache key proof
+          else (* cache_add_success cache key closed_proof *) 
+            (debug_print (lazy ("NO CACHE: (no gree proof)"));cache)
+        in
+        cache, orlist, fl
+      else
+        let cache = 
+          debug_print (lazy ("TENTATIVE CACHE: " ^ CicPp.ppterm gty));
+          if size < minsize then 
+            (debug_print (lazy ("NO CACHE: (size <= minsize)")); cache) else
+          (* if the substituted goal and the proof are closed we cache it *)
+          if is_a_green_cut gty then
+            if green_proof then cache_add_success cache gty proof
+            else (* cache_add_success cache gty closed_proof *) 
+              (debug_print (lazy ("NO CACHE: (no green proof (gty))"));cache)
+          else (*
+            try
+              let ty, _ =
+                CicTypeChecker.type_of_aux' ~subst:s 
+                  m ctx closed_proof CicUniv.oblivion_ugraph
+              in
+              if is_a_green_cut ty then 
+                cache_add_success cache ty closed_proof
+              else cache
+            with
+            | CicTypeChecker.TypeCheckerFailure _ ->*) 
+          (debug_print (lazy ("NO CACHE: (no green gty )"));cache)
+        in
+        cache, orlist, fl
+;;
+let close_failures (fl : fail list) (cache : cache) = 
+  List.fold_left 
+    (fun cache ((gno,depth,_),gty) -> 
+      debug_print (lazy ("FAIL: INDUCED: " ^ string_of_int gno));
+      cache_add_failure cache gty depth) 
+    cache fl
+;;
+let put_in_subst subst metasenv  (goalno,_,_) canonical_ctx t ty =
+  let entry = goalno, (canonical_ctx, t,ty) in
+  assert_subst_are_disjoint subst [entry];
+  let subst = entry :: subst in
+  let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
+  subst, metasenv
+;;
+let mk_fake_proof metasenv (goalno,_,_) goalty context = 
+  None,metasenv,[],Cic.Meta(goalno,mk_irl context),goalty, [] 
+;;
 let equational_case 
-  dbd tables maxm ?auto cache depth fake_proof goalno goalty subst context 
+  tables maxm cache depth fake_proof goalno goalty subst context 
     flags
 =
+  let active,passive,bag = tables in
   let ppterm = ppterm context in
-  prerr_endline ("PARAMOD SU: " ^ string_of_int goalno ^ " " ^ ppterm goalty );
-(*
-  prerr_endline "<CACHE>";
-  prerr_endline (cache_print context cache);
-  prerr_endline "</CACHE>";
-*)
-  match 
-    given_clause dbd ?tables maxm ?auto cache subst flags false (fake_proof,goalno) 
-  with
-  | None, active,passive, bag, cache, maxmeta -> 
-      let tables = Some (active,passive,bag,cache) in
-      None, tables, cache, maxmeta
-  | Some(subst',(_,metasenv,proof,_),open_goals),active,passive,bag,cache,maxmeta ->
-      let tables = Some (active,passive,bag,cache) in
-      assert_subst_are_disjoint subst subst';
-      let subst = subst@subst' in
-      let open_goals = order_new_goals metasenv subst open_goals ppterm in
-      let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
-      Some [metasenv,subst,open_goals], tables, cache, maxmeta
+  let status = (fake_proof,goalno) in
+    if flags.use_only_paramod then
+      begin
+        debug_print (lazy ("PARAMODULATION SU: " ^ 
+                         string_of_int goalno ^ " " ^ ppterm goalty ));
+        let goal_steps, saturation_steps, timeout =
+          max_int,max_int,flags.timeout 
+        in
+        match
+          Saturation.given_clause bag maxm status active passive 
+            goal_steps saturation_steps timeout
+        with 
+          | None, active, passive, maxmeta -> 
+              [], (active,passive,bag), cache, maxmeta, flags
+          | Some(subst',(_,metasenv,_subst,proof,_, _),open_goals),active,
+            passive,maxmeta ->
+              assert_subst_are_disjoint subst subst';
+              let subst = subst@subst' in
+              let open_goals = 
+                order_new_goals metasenv subst open_goals ppterm 
+              in
+              let open_goals = 
+                List.map (fun (x,sort) -> x,depth-1,sort) open_goals 
+              in
+              incr candidate_no;
+                      [(!candidate_no,proof),metasenv,subst,open_goals], 
+                (active,passive,bag), 
+                cache, maxmeta, flags
+      end
+    else
+      begin
+        debug_print 
+          (lazy 
+           ("SUBSUMPTION SU: " ^ string_of_int goalno ^ " " ^ ppterm goalty));
+        let res, maxmeta = 
+          Saturation.all_subsumed bag maxm status active passive 
+        in
+        assert (maxmeta >= maxm);
+        let res' =
+          List.map 
+            (fun subst',(_,metasenv,_subst,proof,_, _),open_goals ->
+               assert_subst_are_disjoint subst subst';
+               let subst = subst@subst' in
+               let open_goals = 
+                 order_new_goals metasenv subst open_goals ppterm 
+               in
+               let open_goals = 
+                 List.map (fun (x,sort) -> x,depth-1,sort) open_goals 
+               in
+               incr candidate_no;
+                 (!candidate_no,proof),metasenv,subst,open_goals)
+            res 
+          in
+          res', (active,passive,bag), cache, maxmeta, flags 
+      end
 ;;
 
 let try_candidate 
-  goalty dbd tables maxm subst fake_proof goalno depth context cand 
+  goalty tables maxm subst fake_proof goalno depth context cand 
 =
   let ppterm = ppterm context in
   try 
-    let subst', ((_,metasenv,_,_), open_goals), maxmeta =
+    let subst', ((_,metasenv,_subst,_,_, _), open_goals), maxmeta =
       PrimitiveTactics.apply_with_subst 
         ~maxmeta:maxm ~term:cand ~subst (fake_proof,goalno) 
     in
-    debug_print ("   OK: " ^ ppterm cand);
-    assert (maxmeta >= maxm);
-    (*FIXME:sicuro che posso @?*)
-    assert_subst_are_disjoint subst subst';
-    let subst = subst@subst' in
+    debug_print (lazy ("   OK: " ^ ppterm cand));
+    let metasenv = CicRefine.pack_coercion_metasenv metasenv in
+    let subst = subst' in
     let open_goals = order_new_goals metasenv subst open_goals ppterm in
     let open_goals = List.map (fun (x,sort) -> x,depth-1,sort) open_goals in
-    Some (metasenv,subst,open_goals), tables , maxmeta
-  with ProofEngineTypes.Fail s -> 
-    (*debug_print("   KO: "^Lazy.force s);*)None,tables, maxm
+    incr candidate_no;
+    Some ((!candidate_no,cand),metasenv,subst,open_goals), tables , maxmeta
+  with 
+    | ProofEngineTypes.Fail s -> None,tables, maxm
+    | CicUnification.Uncertain s ->  None,tables, maxm
+;;
+
+let sort_new_elems = 
+  List.sort (fun (_,_,_,l1) (_,_,_,l2) -> List.length l1 - List.length l2) 
 ;;
 
 let applicative_case 
-  dbd tables maxm depth subst fake_proof goalno goalty metasenv context cache
+  tables maxm depth subst fake_proof goalno goalty metasenv context universe
+  cache
 = 
-  let candidates = get_candidates cache goalty in
+  let candidates = get_candidates universe cache goalty in
   let tables, elems, maxm = 
     List.fold_left 
       (fun (tables,elems,maxm) cand ->
         match 
           try_candidate goalty
-            dbd tables maxm subst fake_proof goalno depth context cand
+            tables maxm subst fake_proof goalno depth context cand
         with
         | None, tables,maxm  -> tables,elems, maxm 
         | Some x, tables, maxm -> tables,x::elems, maxm)
       (tables,[],maxm) candidates
   in
   let elems = sort_new_elems elems in
-  elems, tables, cache, maxm
+  elems, tables, cache, maxm 
 ;;
 
-(* Works if there is no dependency over proofs *)
-let is_a_green_cut goalty =
-  CicUtil.is_meta_closed goalty
+let equational_and_applicative_case 
+  universe flags m s g gty tables cache maxm context 
+=
+  let goalno, depth, sort = g in
+  let fake_proof = mk_fake_proof m g gty context in
+  if is_equational_case gty flags then
+    let elems,tables,cache,maxm1, flags =
+      equational_case tables maxm cache
+        depth fake_proof goalno gty s context flags 
+    in
+    let maxm = maxm1 in
+    let more_elems, tables, cache, maxm1 =
+      if flags.use_only_paramod then
+        [],tables, cache, maxm
+      else
+        applicative_case 
+          tables maxm depth s fake_proof goalno 
+            gty m context universe cache 
+    in
+    let maxm = maxm1 in
+      elems@more_elems, tables, cache, maxm, flags            
+  else
+    let elems, tables, cache, maxm =
+      applicative_case tables maxm depth s fake_proof goalno 
+        gty m context universe cache 
+    in
+      elems, tables, cache, maxm, flags  
 ;;
-let prop = function (_,_,P) -> true | _ -> false;;
-let calculate_timeout flags = 
-    if flags.timeout = 0. then 
-      (prerr_endline "AUTO WITH NO TIMEOUT";{flags with timeout = infinity})
-    else 
-      flags 
+let rec first_s_cand = function
+  | [] -> 0
+  | S (_,_,(i,_),_):: _ -> i
+  | _::tl -> first_s_cand tl
 ;;
-let is_equational_case goalty flags =
-  let ensure_equational t = 
-    if is_an_equational_goal t then true 
-    else false
-    (*
-      let msg="Not an equational goal.\nYou cant use the paramodulation flag"in
-      raise (ProofEngineTypes.Fail (lazy msg))
-    *)
-  in
-  (flags.use_paramod && is_an_equational_goal goalty) || 
-  (flags.use_only_paramod && ensure_equational goalty)
-;;
-let cache_add_success sort cache k v =
-  if sort = P then cache_add_success cache k v else cache_remove_underinspection
-  cache k
+let remove_s_from_fl (id,_,_) (fl : fail list) =
+  let rec aux = function
+    | [] -> []
+    | ((id1,_,_),_)::tl when id = id1 -> tl
+    | hd::tl ->  hd :: aux tl
+  in 
+    aux fl
 ;;
-
-let rec auto_main dbd tables maxm context flags elems cache =
-  let callback_for_paramod maxm flags proof commonctx cache l = 
-    let flags = {flags with use_paramod = false;dont_cache_failures=true} in
-    let _,metasenv,_,_ = proof in
-    let oldmetasenv = metasenv in
-    match
-      auto_all_solutions
-        dbd tables maxm cache commonctx metasenv l flags
-    with
-    | [],cache,maxm -> [],cache,maxm
-    | solutions,cache,maxm -> 
-        let solutions = 
-          HExtlib.filter_map
-            (fun (subst,newmetasenv) ->
-              let opened = 
-                ProofEngineHelpers.compare_metasenvs ~oldmetasenv ~newmetasenv
-              in
-              if opened = [] then Some subst else None)
-            solutions
+let auto_main tables maxm context flags universe cache elems =
+  auto_context := context;
+  let rec aux tables maxm flags cache (elems : status) =
+(*     pp_status context elems;  *)
+    match elems with
+    | [] ->
+        (* complete failure *)
+        Gaveup (tables, cache, maxm)
+    | (m, s, _, [],_)::orlist ->
+        (* complete success *)
+        Proved (m, s, orlist, tables, cache, maxm)
+    | (m, s, size, (D (_,_,T))::todo, fl)::orlist ->
+        (* skip since not Prop, don't even check if closed by side-effect *)
+        aux tables maxm flags cache ((m, s, size, todo, fl)::orlist)
+    | (m, s, size, (S(g, key, c,minsize))::todo, fl)::orlist ->
+        (* partial success, cache g and go on *)
+        let cache, orlist, fl = 
+          add_to_cache_and_del_from_orlist_if_green_cut 
+            g s m cache key todo orlist fl context size minsize
         in
-         solutions,cache,maxm
+        debug_print (lazy (AutoCache.cache_print context cache));
+        let fl = remove_s_from_fl g fl in
+        aux tables maxm flags cache ((m, s, size, todo, fl)::orlist)
+    | (m, s, size, todo, fl)::orlist 
+      when List.length(prop_only (d_goals todo)) > flags.maxwidth ->
+        debug_print (lazy ("FAIL: WIDTH"));
+        (* too many goals in and generated by last th *)
+        let cache = close_failures fl cache in
+        aux tables maxm flags cache orlist
+    | (m, s, size, todo, fl)::orlist 
+      when size > flags.maxsize ->
+        debug_print (lazy ("FAIL: SIZE"));
+        (* we already have a too large proof term *)
+        let cache = close_failures fl cache in
+        aux tables maxm flags cache orlist
+    | _ when Unix.gettimeofday () > flags.timeout ->
+        (* timeout *)
+        debug_print (lazy ("FAIL: SIZE"));
+        Gaveup (tables, cache, maxm)
+    | (m, s, size, (D (gno,depth,P as g))::todo, fl)::orlist as status ->
+        (* attack g *)
+        match calculate_goal_ty g s m with
+        | None -> 
+            (* closed by side effect *)
+            debug_print (lazy ("SUCCESS: SIDE EFFECT: " ^ string_of_int gno));
+            aux tables maxm flags cache ((m,s,size,todo, fl)::orlist)
+        | Some (canonical_ctx, gty) -> 
+            (* still to be proved *)
+            debug_print (lazy ("EXAMINE: "^CicPp.ppterm gty));
+            debug_print (lazy (AutoCache.cache_print context cache));
+            match cache_examine cache gty with
+            | Failed_in d when d >= depth -> 
+                (* fail depth *)
+                debug_print (lazy ("FAIL: DEPTH (cache): "^string_of_int gno));
+                let cache = close_failures fl cache in
+                aux tables maxm flags cache orlist
+            | UnderInspection -> 
+                (* fail loop *)
+                debug_print (lazy ("FAIL: LOOP: " ^ string_of_int gno));
+                let cache = close_failures fl cache in
+                aux tables maxm flags cache orlist
+            | Succeded t -> 
+                debug_print (lazy ("SUCCESS: CACHE HIT: " ^ string_of_int gno));
+                let s, m = put_in_subst s m g canonical_ctx t gty in
+                aux tables maxm flags cache ((m, s, size, todo, fl)::orlist)
+            | Notfound 
+            | Failed_in _ when depth > 0 -> 
+                (match !hint with
+                | Some i when first_s_cand todo <> i ->
+                    aux tables maxm flags cache orlist
+                | _ -> hint := None;
+                    (* more depth or is the first time we see the goal *)
+                    let cache = cache_add_underinspection cache gty depth in
+                    auto_status := status;
+                    check_pause ();
+                    debug_print 
+                      (lazy ("INSPECTING: " ^ 
+                        string_of_int gno ^ "("^ string_of_int size ^ "): "^CicPp.ppterm gty));
+                    (* elems are possible computations for proving gty *)
+                    let elems, tables, cache, maxm, flags =
+                      equational_and_applicative_case 
+                        universe flags m s g gty tables cache maxm context
+                    in
+                    if elems = [] then
+                      (* this goal has failed *)
+                      let cache = close_failures ((g,gty)::fl) cache in
+                      aux tables maxm flags cache orlist
+                    else
+                      (* elems = (cand,m,s,gl) *)
+                      let size_gl l = List.length 
+                        (List.filter (function (_,_,P) -> true | _ -> false) l) 
+                      in
+                      let elems = 
+                        let inj_gl gl = List.map (fun g -> D g) gl in
+                        let rec map = function
+                          | [] -> assert false
+                          | (cand,m,s,gl)::[] ->
+                              (* in the last one we add the failure *)
+                              let todo = inj_gl gl @ (S(g,gty,cand,size+1))::todo in
+                              (* we are the last in OR, we fail on g and 
+                               * also on all failures implied by g *)
+                              (m,s, size + size_gl gl, todo, (g,gty)::fl) :: orlist
+                          | (cand,m,s,gl)::tl -> 
+                              (* we add the S step after gl and before todo *)
+                              let todo = inj_gl gl @ (S(g,gty,cand,size+1))::todo in
+                              (* since we are not the last in OR, we do not
+                               * imply failures *)
+                              (m,s, size + size_gl gl, todo, []) :: map tl
+                        in
+                          map elems
+                      in
+                        aux tables maxm flags cache elems)
+            | _ -> 
+                (* no more depth *)
+                debug_print (lazy ("FAIL: DEPTH: " ^ string_of_int gno));
+                let cache = close_failures fl cache in
+                aux tables maxm flags cache orlist
   in
+    (aux tables maxm flags cache elems : auto_result)
+;;
+    
+(*
+let rec auto_main tables maxm context flags elems universe cache =
+  auto_context := context;
   let flags = calculate_timeout flags in
   let ppterm = ppterm context in
   let irl = mk_irl context in
-  let rec aux tables maxm cache = function (* elems in OR *)
+  let rec aux flags tables maxm cache elems status = 
+    match elems with (* elems in OR *)
     | [] -> Fail "no more steps can be done", tables, cache, maxm
          (*COMPLETE FAILURE*)
-    | (metasenv,subst,[])::tl -> 
-        Success (metasenv,subst,tl), tables, cache,maxm (* solution::cont *)
-    | (metasenv,subst,goals)::tl when 
+    | (p ,metasenv,subst,[])::tl -> 
+        Success (p,metasenv,subst,tl), tables, cache,maxm (* solution::cont *)
+    | (_,metasenv,subst,goals)::tl when 
       List.length (List.filter prop goals) > flags.maxwidth -> 
-        debug_print (" FAILURE(width): " ^ string_of_int (List.length goals));
-        aux tables maxm cache tl (* FAILURE (width) *)
-    | (metasenv,subst,((goalno,depth,sort) as elem)::gl)::tl -> 
+        debug_print 
+          (lazy (" FAILURE(width): " ^ string_of_int (List.length goals)));
+        aux flags tables maxm cache tl (List.tl status) (* FAILURE (width) *)
+    | (p,metasenv,subst,((goalno,depth,sort) as elem)::gl)::tl -> 
+        let (_,_,_,_::gl_status)::tl_status = status in
         if Unix.gettimeofday() > flags.timeout then 
           Fail "timeout",tables,cache,maxm 
         else
         try
           let _,cc,goalty = CicUtil.lookup_meta goalno metasenv in
-          debug_print ("INSPECTING " ^ string_of_int goalno^ ":"^ppterm goalty);
-          if sort = T && tl <> [] then (* FIXME!!!! *)
-            (debug_print (" FAILURE(not in prop)");
-            aux tables maxm cache tl (* FAILURE (not in prop) *))
+          let id,_,_ = p in
+          debug_print 
+            (lazy ("INSPECTING " ^ string_of_int goalno^
+            ":"^"(id="^string_of_int id^")"^ppterm goalty ^
+                   "with depth"^string_of_int depth));
+          debug_print (lazy (AutoCache.cache_print context cache));
+          if sort = T (* && tl <> []*)  then 
+            (debug_print 
+               (lazy (" FAILURE(not in prop)"));
+            aux flags tables maxm cache ((p,metasenv,subst,gl)::tl)
+            ((p,metasenv,subst,gl)::tl_status))
           else
-          match aux_single tables maxm cache metasenv subst elem goalty cc with
+          match aux_single flags tables maxm universe cache metasenv subst elem
+          goalty cc gl_status tl_status p with
           | Fail s, tables, cache, maxm' -> 
-              assert(maxm' >= maxm);let maxm = maxm' in
+              let maxm = maxm' in
               debug_print
-                (" FAIL "^s^": "^string_of_int goalno^":"^ppterm goalty);
+                (lazy 
+                   (" FAIL "^s^": "^string_of_int goalno^":"^ppterm goalty));
               let cache = 
-                if flags.dont_cache_failures then 
+                if flags.dont_cache_failures or s = "hint" then 
                   cache_remove_underinspection cache goalty
-                else cache_add_failure cache goalty depth 
+                else 
+                  cache_add_failure cache goalty depth 
               in
-              aux tables maxm cache tl
-          | Success (metasenv,subst,others), tables, cache, maxm' ->
-              assert(maxm' >= maxm);let maxm = maxm' in
+              aux flags tables maxm cache tl tl_status
+          | Success (p1,metasenv,subst,others), tables, cache, maxm' ->
+              let maxm = maxm' in
               (* others are alternatives in OR *)
               try
                 let goal = Cic.Meta(goalno,irl) in
                 let proof = CicMetaSubst.apply_subst subst goal in
-                debug_print ("DONE: " ^ ppterm goalty^" with: "^ppterm proof);
+                debug_print 
+                  (lazy ("DONE: " ^ ppterm goalty^" with: "^ppterm proof));
                 if is_a_green_cut goalty then
-                  (assert_proof_is_valid proof metasenv context goalty;
+                  (* assert_proof_is_valid proof metasenv context goalty; *)
                   let cache = cache_add_success sort cache goalty proof in
-                  aux tables maxm cache ((metasenv,subst,gl)::tl))
+                  aux flags tables maxm cache ((p,metasenv,subst,gl)::tl)
+                    ((p,metasenv,subst,gl)::tl_status)
+                  
                 else
                   (let goalty = CicMetaSubst.apply_subst subst goalty in
-                  assert_proof_is_valid proof metasenv context goalty;
+                 (* assert_proof_is_valid proof metasenv context goalty; *)
                   let cache = 
                     if is_a_green_cut goalty then
                       cache_add_success sort cache goalty proof
@@ -440,17 +1361,32 @@ let rec auto_main dbd tables maxm context flags elems cache =
                   in
                   let others = 
                     List.map 
-                      (fun (metasenv,subst,goals) -> (metasenv,subst,goals@gl)) 
+                      (fun (p,metasenv,subst,goals) -> 
+                        (p,metasenv,subst,goals@gl)) 
                     others
                   in 
-                  aux tables maxm cache ((metasenv,subst,gl)::others@tl))
+                  aux flags tables maxm cache 
+                    ((p,metasenv,subst,gl)::others@tl)
+                    ((p,metasenv,subst,gl)::others@tl_status)
+                    
+                    )
+                    
               with CicUtil.Meta_not_found i when i = goalno ->
                 assert false
         with CicUtil.Meta_not_found i when i = goalno -> 
           (* goalno was closed by sideeffect *)
-          debug_print ("Goal "^string_of_int goalno^" closed by sideeffect");
-          aux tables maxm cache ((metasenv,subst,gl)::tl)
-  and aux_single tables maxm cache metasenv subst (goalno, depth, _) goalty cc =
+          debug_print 
+            (lazy ("Goal "^string_of_int goalno^" closed by sideeffect"));
+          aux flags tables maxm cache ((p,metasenv,subst,gl)::tl)
+            ((p,metasenv,subst,gl)::tl_status)
+
+  and aux_single flags tables maxm universe cache metasenv subst (goalno, depth,
+  _) goalty cc e l (id,_,_) =
+    match !hint with
+    | Some id' when id <> id' -> Fail "hint", tables,cache,maxm
+    | _ -> 
+        hint := None;
+    (* let flags = if depth < 10 then {flags with maxwidth=3} else flags in *)
     let goalty = CicMetaSubst.apply_subst subst goalty in
 (*     else if not (is_in_prop context subst metasenv goalty) then Fail,cache *)
       (* FAILURE (euristic cut) *)
@@ -459,88 +1395,570 @@ let rec auto_main dbd tables maxm context flags elems cache =
         Fail ("depth " ^ string_of_int d ^ ">=" ^ string_of_int depth),
         tables,cache,maxm(*FAILURE(depth)*)
     | Succeded t -> 
-        assert(List.for_all (fun (i,_) -> i <> goalno) subst);
         let entry = goalno, (cc, t,goalty) in
         assert_subst_are_disjoint subst [entry];
         let subst = entry :: subst in
         let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
-        debug_print ("  CACHE HIT!");
-        Success (metasenv, subst, []), tables, cache, maxm
-    | UnderInspection -> Fail "looping",tables,cache, maxm
+        debug_print (lazy ("  CACHE HIT!"));
+        incr candidate_no;
+        Success ((!candidate_no,t,goalty),metasenv, subst, []), tables, cache, maxm
+    | UnderInspection -> 
+        (* assert (not (is_a_green_cut goalty)); *)
+        Fail "looping",tables,cache, maxm
     | Notfound 
     | Failed_in _ when depth > 0 -> (* we have more depth now *)
         let cache = cache_add_underinspection cache goalty depth in
-        let fake_proof = None,metasenv,Cic.Meta(goalno,irl),goalty in
-        let elems, tables, cache, maxm =
+        let fake_proof = None,metasenv,Cic.Meta(goalno,irl),goalty, [] in (* FG: attrs *)
+        let elems, tables, cache, maxm, flags =
           if is_equational_case goalty flags then
-            match 
-              equational_case dbd tables maxm
-                ~auto:callback_for_paramod cache
-                depth fake_proof goalno goalty subst context flags 
-            with
-            | Some elems, tables, cache, maxm -> 
-                elems, tables, cache, maxm 
-            | None, tables,cache,maxm -> 
-                applicative_case dbd tables maxm depth subst fake_proof goalno 
-                  goalty metasenv context cache
+            let elems,tables,cache,maxm1, flags =
+              equational_case tables maxm cache
+                depth fake_proof goalno goalty subst context flags in
+              let maxm = maxm1 in
+            let more_elems, tables, cache, maxm1 =
+              if flags.use_only_paramod then
+                [],tables, cache, maxm
+              else
+                applicative_case 
+                  tables maxm depth subst fake_proof goalno 
+                  goalty metasenv context universe cache in
+              let maxm = maxm1 in
+              elems@more_elems, tables, cache, maxm, flags            
           else
-            applicative_case dbd tables maxm depth subst fake_proof goalno 
-              goalty metasenv context cache
+            let elems, tables, cache, maxm =
+            applicative_case tables maxm depth subst fake_proof goalno 
+              goalty metasenv context universe cache in
+            elems, tables, cache, maxm, flags  
         in
-        aux tables maxm cache elems
-    | _ -> Fail "??",tables,cache,maxm 
+        let status =
+          List.map (fun (p,m,s,l) -> p,m,s,l@e) elems @ l
+        in
+    auto_status := status;
+    check_pause ();
+        let rc = aux flags tables maxm cache elems status in
+        debug_print "BACK!";
+        rc
+    | _ -> Fail "depth = 0",tables,cache,maxm 
   in
-    aux tables maxm cache elems
+  aux flags tables maxm cache elems elems
 
 and
-  auto_all_solutions dbd tables maxm cache context metasenv gl flags 
+*)
+
+(*
+let
+  auto_all_solutions maxm tables universe cache context metasenv gl flags 
 =
   let goals = order_new_goals metasenv [] gl CicPp.ppterm in
   let goals = List.map (fun (x,s) -> x,flags.maxdepth,s) goals in
-  let elems = [metasenv,[],goals] in
+  let elems = [(0,Cic.Implicit None,Cic.Implicit None), metasenv,[],goals] in
+  let rec aux tables maxm solutions cache elems flags =
+    match auto_main tables maxm context flags elems universe cache with
+    | Fail s,tables,cache,maxm ->debug_print s; solutions,cache,maxm
+    | Success (_,metasenv,subst,others),tables,cache,maxm -> 
+        if Unix.gettimeofday () > flags.timeout then
+          ((subst,metasenv)::solutions), cache, maxm
+        else
+          aux tables maxm ((subst,metasenv)::solutions) cache others flags
+  in
+  let rc = aux tables maxm [] cache elems flags in
+    match rc with
+    | [],cache,maxm -> [],cache,maxm
+    | solutions,cache,maxm -> 
+        let solutions = 
+          HExtlib.filter_map
+            (fun (subst,newmetasenv) ->
+              let opened = 
+                ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
+              in
+              if opened = [] then Some subst else None)
+            solutions
+        in
+         solutions,cache,maxm
+;;
+*)
+let
+  auto_all_solutions maxm tables universe cache context metasenv gl flags 
+=
+  let goals = order_new_goals metasenv [] gl CicPp.ppterm in
+  let goals = 
+    List.map 
+      (fun (x,s) -> D (x,flags.maxdepth,s)) goals 
+  in
+  let elems = [metasenv,[],1,goals,[]] in
   let rec aux tables maxm solutions cache elems flags =
-    match auto_main dbd tables maxm context flags elems cache with
-    | Fail s,tables,cache,maxm ->prerr_endline s; solutions,cache,maxm
-    | Success (metasenv,subst,others),tables,cache,maxm -> 
+    match auto_main tables maxm context flags universe cache elems with
+    | Gaveup (tables,cache,maxm) ->
+        solutions,cache,maxm
+    | Proved (metasenv,subst,others,tables,cache,maxm) -> 
         if Unix.gettimeofday () > flags.timeout then
           ((subst,metasenv)::solutions), cache, maxm
         else
           aux tables maxm ((subst,metasenv)::solutions) cache others flags
   in
   let rc = aux tables maxm [] cache elems flags in
-  prerr_endline "fine auto all solutions";
-  rc
+    match rc with
+    | [],cache,maxm -> [],cache,maxm
+    | solutions,cache,maxm -> 
+        let solutions = 
+          HExtlib.filter_map
+            (fun (subst,newmetasenv) ->
+              let opened = 
+                ProofEngineHelpers.compare_metasenvs ~oldmetasenv:metasenv ~newmetasenv
+              in
+              if opened = [] then Some subst else None)
+            solutions
+        in
+         solutions,cache,maxm
 ;;
 
 (* }}} ****************** AUTO ***************)
 
-let auto_all_solutions dbd cache context metasenv gl flags =
+(* script generation for applicative proofs 
+let cic2grafite context menv t =
+  let module PT = CicNotationPt in
+  let module GA = GrafiteAst in
+  let pp_t context t =
+    let names = Utils.names_of_context context in
+    CicPp.pp t names
+  in
+  let sort_of context t = 
+    let ty,_ = 
+      CicTypeChecker.type_of_aux' menv context t
+        CicUniv.oblivion_ugraph 
+    in
+    let sort,_ = CicTypeChecker.type_of_aux' menv context ty
+        CicUniv.oblivion_ugraph
+    in
+    match sort with
+    | Cic.Sort Cic.Prop -> P
+    | _ -> T
+  in
+  let floc = HExtlib.dummy_floc in
+  let rec print_term c = function
+    | Cic.Rel _
+    | Cic.MutConstruct _ 
+    | Cic.MutInd _ 
+    | Cic.Const _ as t -> 
+        PT.Ident (pp_t c t, None)
+    | Cic.Appl l -> PT.Appl (List.map (print_term c) l)
+    | Cic.Implicit _ -> PT.Implicit
+    | Cic.Lambda (Cic.Name n, s, t) ->
+        PT.Binder (`Lambda, (PT.Ident (n,None), Some (print_term c s)),
+          print_term (Some (Cic.Name n, Cic.Decl s)::c) t)
+    | Cic.Prod (Cic.Name n, s, t) ->
+        PT.Binder (`Forall, (PT.Ident (n,None), Some (print_term c s)),
+          print_term (Some (Cic.Name n, Cic.Decl s)::c) t)
+    | Cic.LetIn (Cic.Name n, s, t) ->
+        PT.Binder (`Lambda, (PT.Ident (n,None), Some (print_term c s)),
+          print_term (Some (Cic.Name n, Cic.Def (s,None))::c) t)
+    | Cic.Meta _ -> PT.Implicit
+    | _ as t -> 
+        PT.Ident ("ERROR"^CicPp.ppterm t, None)
+    (*
+        debug_print (lazy (CicPp.ppterm t));
+        assert false
+        *)
+  in
+  let rec print_proof context = function
+    | Cic.Rel _
+    | Cic.Const _ as t -> 
+        [GA.Executable (floc, 
+          GA.Tactic (floc,
+          Some (GA.Apply (floc, print_term context t)), GA.Dot floc))]
+    | Cic.Appl (he::tl) ->
+        let tl = List.map (fun t -> t, sort_of context t) tl in
+        let subgoals = 
+          HExtlib.filter_map (function (t,P) -> Some t | _ -> None) tl
+        in
+        let args = 
+            List.map 
+              (function 
+                | (t,P) -> Cic.Implicit None
+                | (t,_) -> t)
+              tl
+        in
+        if List.length subgoals > 1 then
+          (* branch *)
+          [GA.Executable (floc, 
+            GA.Tactic (floc,
+              Some (GA.Apply (floc, print_term context (Cic.Appl (he::args)))),
+              GA.Semicolon floc))] @
+          [GA.Executable (floc, GA.Tactic (floc, None, GA.Branch floc))] @
+          (HExtlib.list_concat 
+          ~sep:[GA.Executable (floc, GA.Tactic (floc, None,GA.Shift floc))]
+            (List.map (print_proof context) subgoals)) @
+          [GA.Executable (floc, GA.Tactic (floc, None,GA.Merge floc))]
+        else
+          (* simple apply *)
+          [GA.Executable (floc, 
+            GA.Tactic (floc,
+            Some (GA.Apply 
+              (floc, print_term context (Cic.Appl (he::args)) )), GA.Dot floc))]
+          @
+          (match subgoals with
+          | [] -> []
+          | [x] -> print_proof context x
+          | _ -> assert false)
+    | _ -> []
+    (*
+        debug_print (lazy (CicPp.ppterm t));
+        assert false
+        *)
+  in
+  let ast = print_proof context t in
+  let pp t = 
+    (* ZACK: setting width to 80 will trigger a bug of BoxPp.render_to_string
+     * which will show up using the following command line:
+     * ./tptp2grafite -tptppath ~tassi/TPTP-v3.1.1 GRP170-1 *)
+    let width = max_int in
+    let term_pp content_term =
+      let pres_term = TermContentPres.pp_ast content_term in
+      let dummy_tbl = Hashtbl.create 1 in
+      let markup = CicNotationPres.render dummy_tbl pres_term in
+      let s = "(" ^ BoxPp.render_to_string List.hd width markup ^ ")" in
+      Pcre.substitute 
+        ~pat:"\\\\forall [Ha-z][a-z0-9_]*" ~subst:(fun x -> "\n" ^ x) s
+    in
+    CicNotationPp.set_pp_term term_pp;
+    let lazy_term_pp = fun x -> assert false in
+    let obj_pp = CicNotationPp.pp_obj CicNotationPp.pp_term in
+    GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp t
+  in
+  String.concat "\n" (List.map pp ast)
+;;
+let auto_all tables universe cache context metasenv gl flags =
   let solutions, cache, _ = 
-    auto_all_solutions dbd None 0 cache context metasenv gl flags
+    auto_all_solutions 0 tables universe cache context metasenv gl flags
   in
   solutions, cache
 ;;
+*)
 
-let auto dbd cache context metasenv gl flags =
+let auto flags metasenv tables universe cache context metasenv gl =
   let initial_time = Unix.gettimeofday() in
   let goals = order_new_goals metasenv [] gl CicPp.ppterm in
-  let goals = List.map (fun (x,s) -> x,flags.maxdepth,s) goals in
-  let elems = [metasenv,[],goals] in
-  match auto_main dbd None 0 context flags elems cache with
-  | Success (metasenv,subst,_), tables,cache,_ -> 
-      prerr_endline("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time));
+  let goals = List.map (fun (x,s) -> D(x,flags.maxdepth,s)) goals in
+  let elems = [metasenv,[],1,goals,[]] in
+  match auto_main tables 0 context flags universe cache elems with
+  | Proved (metasenv,subst,_, tables,cache,_) -> 
+      debug_print(lazy
+        ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
       Some (subst,metasenv), cache
-  | Fail s,tables,cache,maxm -> None,cache
+  | Gaveup (tables,cache,maxm) -> 
+      debug_print(lazy
+        ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time)));
+      None,cache
 ;;
 
-let applyS_tac ~dbd ~term =
+let bool params name default =
+    try 
+      let s = List.assoc name params in 
+      if s = "" || s = "1" || s = "true" || s = "yes" || s = "on" then true
+      else if s = "0" || s = "false" || s = "no" || s= "off" then false
+      else 
+        let msg = "Unrecognized value for parameter "^name^"\n" in
+        let msg = msg^"Accepted values are 1,true,yes,on and 0,false,no,off" in
+        raise (ProofEngineTypes.Fail (lazy msg))
+    with Not_found -> default
+;; 
+
+let string params name default =
+    try List.assoc name params with
+    | Not_found -> default
+;; 
+
+let int params name default =
+    try int_of_string (List.assoc name params) with
+    | Not_found -> default
+    | Failure _ -> 
+        raise (ProofEngineTypes.Fail (lazy (name ^ " must be an integer")))
+;;  
+
+let flags_of_params params ?(for_applyS=false) () =
+ let int = int params in
+ let bool = bool params in
+ let close_more = bool "close_more" false in
+ let use_paramod = bool "use_paramod" true in
+ let use_only_paramod =
+  if for_applyS then true else bool "paramodulation" false in
+ let use_library = bool "library"  
+   ((AutoTypes.default_flags()).AutoTypes.use_library) in
+ let depth = int "depth" ((AutoTypes.default_flags()).AutoTypes.maxdepth) in
+ let width = int "width" ((AutoTypes.default_flags()).AutoTypes.maxwidth) in
+ let size = int "size" ((AutoTypes.default_flags()).AutoTypes.maxsize) in
+ let timeout = int "timeout" 0 in
+  { AutoTypes.maxdepth = 
+      if use_only_paramod then 2 else depth;
+    AutoTypes.maxwidth = width;
+    AutoTypes.maxsize = size;
+    AutoTypes.timeout = 
+      if timeout = 0 then
+       if for_applyS then Unix.gettimeofday () +. 30.0
+       else
+         infinity
+      else
+       Unix.gettimeofday() +. (float_of_int timeout);
+    AutoTypes.use_library = use_library; 
+    AutoTypes.use_paramod = use_paramod;
+    AutoTypes.use_only_paramod = use_only_paramod;
+    AutoTypes.close_more = close_more;
+    AutoTypes.dont_cache_failures = false;
+  }
+
+let applyS_tac ~dbd ~term ~params ~universe =
  ProofEngineTypes.mk_tactic
   (fun status ->
     try 
-      let _, proof, gl,_,_ = apply_smart ~dbd ~term ~subst:[] status in 
-      proof, gl
+      let _, proof, gl,_,_ =
+       apply_smart ~dbd ~term ~subst:[] ~universe
+        (flags_of_params params ~for_applyS:true ()) status
+      in 
+       proof, gl
     with 
     | CicUnification.UnificationFailure msg
     | CicTypeChecker.TypeCheckerFailure msg ->
         raise (ProofEngineTypes.Fail msg))
 
+(* SUPERPOSITION *)
+
+(* Syntax: 
+ *   auto superposition target = NAME 
+ *     [table = NAME_LIST] [demod_table = NAME_LIST] [subterms_only]
+ *
+ *  - if table is omitted no superposition will be performed
+ *  - if demod_table is omitted no demodulation will be prformed
+ *  - subterms_only is passed to Indexing.superposition_right
+ *
+ *  lists are coded using _ (example: H_H1_H2)
+ *)
+
+let eq_and_ty_of_goal = function
+  | Cic.Appl [Cic.MutInd(uri,0,_);t;_;_] when LibraryObjects.is_eq_URI uri ->
+      uri,t
+  | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
+;;
+
+let rec find_in_ctx i name = function
+  | [] -> raise (ProofEngineTypes.Fail (lazy ("Hypothesis not found: " ^ name)))
+  | Some (Cic.Name name', _)::tl when name = name' -> i
+  | _::tl -> find_in_ctx (i+1) name tl
+;;
+
+let rec position_of i x = function
+  | [] -> assert false
+  | j::tl when j <> x -> position_of (i+1) x tl
+  | _ -> i
+;;
+
+
+let superposition_tac ~target ~table ~subterms_only ~demod_table status = 
+  Saturation.reset_refs();
+  let proof,goalno = status in 
+  let curi,metasenv,_subst,pbo,pty, attrs = proof in
+  let metano,context,ty = CicUtil.lookup_meta goalno metasenv in
+  let eq_uri,tty = eq_and_ty_of_goal ty in
+  let env = (metasenv, context, CicUniv.empty_ugraph) in
+  let names = Utils.names_of_context context in
+  let bag = Equality.mk_equality_bag () in
+  let eq_index, equalities, maxm,cache  = 
+    find_context_equalities 0 bag context proof Universe.empty AutoCache.cache_empty 
+  in
+  let eq_what = 
+    let what = find_in_ctx 1 target context in
+    List.nth equalities (position_of 0 what eq_index)
+  in
+  let eq_other = 
+    if table <> "" then
+      let other = 
+        let others = Str.split (Str.regexp "_") table in 
+        List.map (fun other -> find_in_ctx 1 other context) others 
+      in
+      List.map 
+        (fun other -> List.nth equalities (position_of 0 other eq_index)) 
+        other 
+    else
+      []
+  in
+  let index = List.fold_left Indexing.index Indexing.empty eq_other in
+  let maxm, eql = 
+    if table = "" then maxm,[eq_what] else 
+    Indexing.superposition_right bag
+      ~subterms_only eq_uri maxm env index eq_what
+  in
+  debug_print (lazy ("Superposition right:"));
+  debug_print (lazy ("\n eq: " ^ Equality.string_of_equality eq_what ~env));
+  debug_print (lazy ("\n table: "));
+  List.iter 
+    (fun e -> 
+       debug_print (lazy ("  " ^ Equality.string_of_equality e ~env))) eq_other;
+  debug_print (lazy ("\n result: "));
+  List.iter (fun e -> debug_print (lazy (Equality.string_of_equality e ~env))) eql;
+  debug_print (lazy ("\n result (cut&paste): "));
+  List.iter 
+    (fun e -> 
+      let t = Equality.term_of_equality eq_uri e in
+      debug_print (lazy (CicPp.pp t names))) 
+  eql;
+  debug_print (lazy ("\n result proofs: "));
+  List.iter (fun e -> 
+    debug_print (lazy (let _,p,_,_,_ = Equality.open_equality e in
+    let s = match p with Equality.Exact _ -> Subst.empty_subst | Equality.Step (s,_) -> s in
+    Subst.ppsubst s ^ "\n" ^ 
+    CicPp.pp (Equality.build_proof_term bag eq_uri [] 0 p) names))) eql;
+  if demod_table <> "" then
+    begin
+      let eql = 
+        if eql = [] then [eq_what] else eql
+      in
+      let demod = 
+        let demod = Str.split (Str.regexp "_") demod_table in 
+        List.map (fun other -> find_in_ctx 1 other context) demod 
+      in
+      let eq_demod = 
+        List.map 
+          (fun demod -> List.nth equalities (position_of 0 demod eq_index)) 
+          demod 
+      in
+      let table = List.fold_left Indexing.index Indexing.empty eq_demod in
+      let maxm,eql = 
+        List.fold_left 
+          (fun (maxm,acc) e -> 
+            let maxm,eq = 
+              Indexing.demodulation_equality bag eq_uri maxm env table e
+            in
+            maxm,eq::acc) 
+          (maxm,[]) eql
+      in
+      let eql = List.rev eql in
+      debug_print (lazy ("\n result [demod]: "));
+      List.iter 
+        (fun e -> debug_print (lazy (Equality.string_of_equality e ~env))) eql;
+      debug_print (lazy ("\n result [demod] (cut&paste): "));
+      List.iter 
+        (fun e -> 
+          let t = Equality.term_of_equality eq_uri e in
+          debug_print (lazy (CicPp.pp t names)))
+      eql;
+    end;
+  proof,[goalno]
+;;
+
+let auto_tac ~(dbd:HSql.dbd) ~params ~universe (proof, goal) =
+  (* argument parsing *)
+  let string = string params in
+  let bool = bool params in
+  (* hacks to debug paramod *)
+  let superposition = bool "superposition" false in
+  let target = string "target" "" in
+  let table = string "table" "" in
+  let subterms_only = bool "subterms_only" false in
+  let demod_table = string "demod_table" "" in
+  match superposition with
+  | true -> 
+      (* this is the ugly hack to debug paramod *)
+      superposition_tac 
+        ~target ~table ~subterms_only ~demod_table (proof,goal)
+  | false -> 
+      (* this is the real auto *)
+      let _,metasenv,_subst,_,_, _ = proof in
+      let _,context,goalty = CicUtil.lookup_meta goal metasenv in
+      let flags = flags_of_params params () in
+      (* just for testing *)
+      let use_library = flags.use_library in
+      let tables,cache,newmeta =
+        init_cache_and_tables dbd use_library flags.use_only_paramod 
+          universe (proof, goal) in
+      let tables,cache,newmeta =
+        if flags.close_more then
+          close_more 
+            tables newmeta context (proof, goal) 
+              auto_all_solutions universe cache 
+        else tables,cache,newmeta in
+      let initial_time = Unix.gettimeofday() in
+      let (_,oldmetasenv,_subst,_,_, _) = proof in
+      hint := None;
+      let elem = 
+        metasenv,[],1,[D (goal,flags.maxdepth,P)],[]
+      in
+      match auto_main tables newmeta context flags universe cache [elem] with
+        | Proved (metasenv,subst,_, tables,cache,_) -> 
+            prerr_endline 
+              ("TIME:"^string_of_float(Unix.gettimeofday()-.initial_time));
+            (* script generation 
+            let irl = mk_irl context in
+            let goal_term = Cic.Meta(goal, irl) in
+            let proof_term = CicMetaSubst.apply_subst subst goal_term in
+            HLog.debug (cic2grafite context metasenv proof_term);
+            *)
+            let proof,metasenv = 
+            ProofEngineHelpers.subst_meta_and_metasenv_in_proof
+              proof goal subst metasenv
+            in
+            let opened = 
+              ProofEngineHelpers.compare_metasenvs ~oldmetasenv
+                ~newmetasenv:metasenv
+            in
+              proof,opened
+        | Gaveup (tables,cache,maxm) -> 
+            debug_print
+              (lazy ("TIME:"^
+                string_of_float(Unix.gettimeofday()-.initial_time)));
+            raise (ProofEngineTypes.Fail (lazy "Auto gave up"))
+;;
+
+let auto_tac ~dbd ~params ~universe = 
+  ProofEngineTypes.mk_tactic (auto_tac ~params ~dbd ~universe);;
+
+let eq_of_goal = function
+  | Cic.Appl [Cic.MutInd(uri,0,_);_;_;_] when LibraryObjects.is_eq_URI uri ->
+      uri
+  | _ -> raise (ProofEngineTypes.Fail (lazy ("The goal is not an equality ")))
+;;
+
+(* DEMODULATE *)
+let demodulate_tac ~dbd ~universe (proof,goal)= 
+  let curi,metasenv,_subst,pbo,pty, attrs = proof in
+  let metano,context,ty = CicUtil.lookup_meta goal metasenv in
+  let irl = CicMkImplicit.identity_relocation_list_for_metavariable context in
+  let initgoal = [], [], ty in
+  let eq_uri = eq_of_goal ty in
+  let (active,passive,bag), cache, maxm =
+     init_cache_and_tables dbd false true universe (proof,goal) in
+  let equalities = (Saturation.list_of_passive passive) in
+  (* we demodulate using both actives passives *)
+  let table = 
+    List.fold_left 
+      (fun tbl eq -> Indexing.index tbl eq) 
+      (snd active) equalities
+  in
+  let changed,(newproof,newmetasenv, newty) = 
+    Indexing.demodulation_goal bag
+      (metasenv,context,CicUniv.empty_ugraph) table initgoal 
+  in
+  if changed then
+    begin
+      let opengoal = Equality.Exact (Cic.Meta(maxm,irl)) in
+      let proofterm,_ = 
+        Equality.build_goal_proof bag
+          eq_uri newproof opengoal ty [] context metasenv
+      in
+        let extended_metasenv = (maxm,context,newty)::metasenv in
+        let extended_status = 
+          (curi,extended_metasenv,_subst,pbo,pty, attrs),goal in
+        let (status,newgoals) = 
+          ProofEngineTypes.apply_tactic 
+            (PrimitiveTactics.apply_tac ~term:proofterm)
+            extended_status in
+        (status,maxm::newgoals)
+    end
+  else (* if newty = ty then *)
+    raise (ProofEngineTypes.Fail (lazy "no progress"))
+  (*else ProofEngineTypes.apply_tactic 
+    (ReductionTactics.simpl_tac
+      ~pattern:(ProofEngineTypes.conclusion_pattern None)) initialstatus*)
+;;
+
+let demodulate_tac ~dbd ~universe = 
+  ProofEngineTypes.mk_tactic (demodulate_tac ~dbd ~universe);;
+