123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132(*
MSAT is free software, using the Apache license, see file LICENSE
Copyright 2016 Guillaume Bury
Copyright 2016 Simon Cruanes
*)openSolver_typesmoduleS=Internaltypeproof=Proof.ttypenonrecatom=atom(** The type of atoms given by the module argument for formulas *)type'clauseclause_sets={cs_hyps:'clauseVec.t;cs_history:'clauseVec.t;cs_local:'clauseVec.t;}(** Current state of the SAT solver *)exceptionUndecidedLit=Internal.UndecidedLitexceptionOut_of_space=Internal.Out_of_spaceexceptionOut_of_time=Internal.Out_of_time(** Main type *)typet=Internal.ttype'astate=|St_sat:t->[`SAT]state|St_unsat:t->[`UNSAT]state(* unsat conflict *)let[@inline]state_solver(typea)(st:astate):t=matchstwith|St_sats->s|St_unsats->slet[@inline]pluginst=S.pluginstlet[@inline]get_servicetk=S.get_servicetklet[@inline]get_service_exntk=S.get_service_exntklet[@inline]servicest=S.servicestletpp_alltlvlstatus=Log.debugflvl(funk->k"@[<v>%s - Full summary:@,@[<hov 2>Trail:@\n%a@]@,@[<hov 2>Temp:@\n%a@]@,\
@[<hov 2>Hyps:@\n%a@]@,@[<hov 2>Lemmas:@\n%a@]@,@]@."status(Vec.pp~sep:""Term.pp)(S.trailt)(Vec.pp~sep:""Clause.debug)(S.tempt)(Vec.pp~sep:""Clause.debug)(S.hypst)(Vec.pp~sep:""Clause.debug)(S.historyt))(* Wrappers around internal functions*)letassume=S.assumeletunsat_core_p=Proof.unsat_coreplettrue_at_level0_sa=tryletb,lev=S.eval_levelainb&&lev=0withS.UndecidedLit_->falseletadd_term=S.add_termletclause_sets(s:t)={cs_hyps=S.hypss;cs_history=S.historys;cs_local=S.temps;}moduleSat_state=structtypet=[`SAT]stateletiter_trail(St_sats)f=Vec.iterf(S.trails)let[@inline]eval(St_sat_s)a=S.evalalet[@inline]eval_opt(St_sat_s)a=trySome(S.evala)withUndecidedLit_->Nonelet[@inline]eval_level(St_sat_s)a=S.eval_levelaletmodel(St_sats)=S.modelsletcheck_model(s:t):bool=letSt_sats=sinbeginmatchS.checkswith|Ok_->true|Errormsg->Log.debugf0(funk->k"(@[solver.check_model.fail:@ %a@])"Format.pp_print_textmsg);falseendendmoduleUnsat_state=structtypet=[`UNSAT]stateletunsat_conflict(St_unsats):clause=beginmatchS.unsat_conflictswith|None->assertfalse|Somec->cendletget_proof(s:t)=letc=unsat_conflictsinProof.prove_unsatcendtyperes=|SatofSat_state.t(** Returned when the solver reaches SAT *)|UnsatofUnsat_state.t(** Returned when the solver reaches UNSAT *)(** Result type for the solver *)letsolve?gc?restarts?time?memory?(progress=false)?(assumptions=[])?switch(s:t):res=tryS.pops;S.pushs;S.localsassumptions;S.solve?gc?restarts?time?memory~progress?switchs;ifprogressthenS.clear_progress();pp_alls99"SAT";Sat(St_sats)withS.Unsat->ifprogressthenS.clear_progress();pp_alls99"UNSAT";Unsat(St_unsats)letcreate~plugins()=letsolver=S.create()in(* sort by increasing priority *)letplugins=List.sortPlugin.Factory.comparepluginsinList.iter(funp->ignore(S.add_pluginsolverp))plugins;solverletpp_stats=S.pp_stats