123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125(* This file is free software. See file "license" for more details. *)typettype'aprinter=Format.formatter->'a->unit(* use normal convention of positive/negative ints *)moduleLit=structtypet=intletmaken=assert(n>0);nletnegn=-nletmake_with_signbi=letx=makeiinifbthenxelsenegxletabs=absletsignn=n>0letto_intn=nletto_stringx=(ifsignxthen""else"-")^string_of_int(abs@@to_intx)letppoutx=Format.pp_print_stringout(to_stringx)letequal:t->t->bool=(=)letcompare:t->t->int=comparelethash:t->int=Hashtbl.hashendtypeassumptions=Lit.tarraymoduleRaw=structtypelbool=int(* 0,1,2 *)externalcreate:unit->t="ml_batsat_new"(* the [add_clause] functions return [false] if the clause
immediately makes the problem unsat *)externalsimplify:t->bool="ml_batsat_simplify"externaladd_lit:t->Lit.t->bool="ml_batsat_addlit"[@@noalloc]externalassume:t->Lit.t->unit="ml_batsat_assume"[@@noalloc]externalsolve:t->bool="ml_batsat_solve"externalnvars:t->int="ml_batsat_nvars"[@@noalloc]externalnclauses:t->int="ml_batsat_nclauses"[@@noalloc]externalnconflicts:t->int="ml_batsat_nconflicts"[@@noalloc]externalndecisions:t->int="ml_batsat_ndecisions"[@@noalloc]externalnprops:t->int="ml_batsat_nprops"[@@noalloc](* external nrestarts : t -> int "ml_batsat_nrestarts" [@@noalloc] *)externalvalue:t->Lit.t->lbool="ml_batsat_value"[@@noalloc]externalcheck_assumption:t->Lit.t->bool="ml_batsat_check_assumption"[@@noalloc]externalunsat_core:t->Lit.tarray="ml_batsat_unsat_core"externaln_proved:t->int="ml_batsat_n_proved"[@@noalloc]externalget_proved:t->int->Lit.t="ml_batsat_get_proved"[@@noalloc]externalvalue_lvl_0:t->Lit.t->lbool="ml_batsat_value_lvl_0"[@@noalloc]endletcreate()=lets=Raw.create()insexceptionUnsatlet[@inline]check_ret_b=ifnotbthenraiseUnsatletadd_clause_aslits=Array.iter(funx->letr=Raw.add_litsxinassertr)lits;Raw.add_lits0|>check_ret_letadd_clause_lslits=List.iter(funx->letr=Raw.add_litsxinassertr)lits;Raw.add_lits0|>check_ret_letpp_clauseoutl=Format.fprintfout"[@[<hv>";letfirst=reftrueinList.iter(funx->if!firstthenfirst:=falseelseFormat.fprintfout",@ ";Lit.ppoutx)l;Format.fprintfout"@]]"let[@inline]simplifys=Raw.simplifys|>check_ret_letn_vars=Raw.nvarsletn_clauses=Raw.nclausesletn_conflicts=Raw.nconflictsletn_proved_lvl_0=Raw.n_provedletget_proved_lvl_0=Raw.get_proved(* let n_restarts = Raw.nrestarts *)letn_props=Raw.npropsletn_decisions=Raw.ndecisionsletproved_lvl_0s=Array.init(n_proved_lvl_0s)(get_proved_lvl_0s)letsolve?(assumptions=[||])s=Array.iter(funx->Raw.assumesx)assumptions;Raw.solves|>check_ret_letsolve_is_sat?assumptionss=trysolve?assumptionss;truewithUnsat->falseletis_in_unsat_coreslit=Raw.check_assumptionslitletunsat_core=Raw.unsat_coretypevalue=|V_undef|V_true|V_falselet[@inline]string_of_value=function|V_undef->"undef"|V_true->"true"|V_false->"false"letpp_valueoutv=Format.pp_print_stringout(string_of_valuev)letmk_val=function|0->V_true|1->V_false|2|3->V_undef(* yep… *)|n->failwith(Printf.sprintf"unknown lbool: %d"n)letvalueslit=mk_val@@Raw.valueslitletvalue_lvl_0slit=mk_val@@Raw.value_lvl_0slit