(*
This file is part of the dpll(t) engine sammy
Copyright (C) 2004  The University of Iowa
George Hagen
                                                                                
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
                                                                                
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
                                                                                
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*)

(* convert.ml *)

exception Conversion_error

(* converts a list of ids to a list of lits *)
let to_lit_list pred_db = 
  List.map (fun x -> let l = pred_db#get_lit x in
    if (Global.pars.Global.activity_behavior land 1) != 0 then
     Sys_state.cstore#increment_activity x;
    l)
                                                                                
(* turns a list of lists of ids into a list of list of lits *)
let convclause pred_db (cs:Global.pred_id list list) =
  let rec cc cs' (cl1:Pred.lit list list) (cl3:Pred.lit list list) =
    match cs' with
        [] -> List.rev_append cl1 cl3
      | h::t ->
               let (litlist:Pred.lit list) = to_lit_list pred_db h in
               let n = List.length litlist in
               if n < 2 then
                 cc t (litlist::cl1) cl3
               else
                 cc t cl1 (litlist::cl3)
  in
  cc cs [] []
                                                                                
(* turns a formula (of exprtype) into a list of new lit lists *)
let convert_formula_to_lit_lists f pred_db skolemizer =
(*Formula.print_exprtype f;*)
let stime = Sys.time() in
      let skolemized_expr = (skolemizer#skolemize f) in
(*Printf.printf "Skolemized: %f\n" (Sys.time() -. stime);*)
      let (_,normalized_expr)=
          skolemizer#declare_abstractions_and_normalize [] skolemized_expr 1 1
      in
(*Printf.printf "Normalized: %f\n" (Sys.time() -. stime);*)
      let cnf_list=skolemizer#to_cnf normalized_expr in
(*Printf.printf "Converted : %f\n" (Sys.time() -. stime);*)
      convclause pred_db (cnf_list)

(*
let convert_lit_lists_to_clauses cs =
  List.map (fun ll -> (*Pred.cset_add*) (Pred.new_clause ll)) cs
*)
                                                                                
 
let negate_formula f =
  Formula.make_not_expr f 
