cprover
satcheck_ipasir.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: External SAT Solver Binding
4 
5 Author: Norbert Manthey, nmanthey@amazon.com
6 
7 \*******************************************************************/
8 
9 #ifndef _MSC_VER
10 #include <inttypes.h>
11 #endif
12 
13 #include <algorithm>
14 #include <stack>
15 
16 #include <util/exception_utils.h>
17 #include <util/invariant.h>
18 #include <util/threeval.h>
19 
20 #include "satcheck_ipasir.h"
21 
22 #ifdef HAVE_IPASIR
23 
24 extern "C"
25 {
26 #include <ipasir.h>
27 }
28 
29 /*
30 
31 Interface description:
32 https://github.com/biotomas/ipasir/blob/master/ipasir.h
33 
34 Representation:
35 Variables for a formula start with 1! 0 is used as termination symbol.
36 
37 */
38 
40 {
41  if(a.is_true())
42  return tvt(true);
43  else if(a.is_false())
44  return tvt(false);
45 
46  tvt result;
47 
48  // compare to internal no_variables number
49  if(a.var_no()>=(unsigned)no_variables())
50  return tvt::unknown();
51 
52  const int val=ipasir_val(solver, a.var_no());
53 
54  if(val>0)
55  result=tvt(true);
56  else if(val<0)
57  result=tvt(false);
58  else
59  return tvt::unknown();
60 
61  if(a.sign())
62  result=!result;
63 
64  return result;
65 }
66 
67 const std::string satcheck_ipasirt::solver_text()
68 {
69  return std::string(ipasir_signature());
70 }
71 
72 void satcheck_ipasirt::lcnf(const bvt &bv)
73 {
74  forall_literals(it, bv)
75  {
76  if(it->is_true())
77  return;
78  else if(!it->is_false())
79  INVARIANT(it->var_no()<(unsigned)no_variables(),
80  "reject out of bound variables");
81  }
82 
83  forall_literals(it, bv)
84  {
85  if(!it->is_false())
86  {
87  // add literal with correct sign
88  ipasir_add(solver, it->dimacs());
89  }
90  }
91  ipasir_add(solver, 0); // terminate clause
92 
94 }
95 
97 {
98  INVARIANT(status!=statust::ERROR, "there cannot be an error");
99 
100  log.statistics() << (no_variables() - 1) << " variables, " << clause_counter
101  << " clauses" << messaget::eom;
102 
103  // use the internal representation, as ipasir does not support reporting the
104  // status
106  {
107  log.status() << "SAT checker inconsistent: instance is UNSATISFIABLE"
108  << messaget::eom;
109  }
110  else
111  {
112  // if assumptions contains false, we need this to be UNSAT
113  bvt::const_iterator it = std::find_if(assumptions.begin(),
114  assumptions.end(), is_false);
115  const bool has_false = it != assumptions.end();
116 
117  if(has_false)
118  {
119  log.status() << "got FALSE as assumption: instance is UNSATISFIABLE"
120  << messaget::eom;
121  }
122  else
123  {
125  if(!it->is_false())
126  ipasir_assume(solver, it->dimacs());
127 
128  // solve the formula, and handle the return code (10=SAT, 20=UNSAT)
129  int solver_state=ipasir_solve(solver);
130  if(10==solver_state)
131  {
132  log.status() << "SAT checker: instance is SATISFIABLE" << messaget::eom;
134  return resultt::P_SATISFIABLE;
135  }
136  else if(20==solver_state)
137  {
138  log.status() << "SAT checker: instance is UNSATISFIABLE"
139  << messaget::eom;
140  }
141  else
142  {
143  log.status() << "SAT checker: solving returned without solution"
144  << messaget::eom;
145  throw analysis_exceptiont(
146  "solving inside IPASIR SAT solver has been interrupted");
147  }
148  }
149  }
150 
153 }
154 
155 void satcheck_ipasirt::set_assignment(literalt a, bool value)
156 {
157  INVARIANT(!a.is_constant(), "cannot set an assignment for a constant");
158  INVARIANT(false, "method not supported");
159 }
160 
162 : solver(nullptr)
163 {
164  INVARIANT(!solver, "there cannot be a solver already");
165  solver=ipasir_init();
166 }
167 
169 {
170  if(solver)
171  ipasir_release(solver);
172  solver=nullptr;
173 }
174 
176 {
177  return ipasir_failed(solver, a.var_no());
178 }
179 
181 {
182  bvt::const_iterator it = std::find_if(bv.begin(), bv.end(), is_true);
183  const bool has_true = it != bv.end();
184 
185  if(has_true)
186  {
187  assumptions.clear();
188  return;
189  }
190  // only copy assertions, if there is no false in bt parameter
191  assumptions=bv;
192 }
193 
194 #endif
exception_utils.h
cnf_solvert::statust::SAT
@ SAT
satcheck_ipasirt::set_assignment
void set_assignment(literalt a, bool value) override
bvt
std::vector< literalt > bvt
Definition: literal.h:201
threeval.h
messaget::status
mstreamt & status() const
Definition: message.h:414
satcheck_glucose_baset< Glucose::SimpSolver >::solver
Glucose::SimpSolver * solver
Definition: satcheck_glucose.h:57
propt::resultt::P_UNSATISFIABLE
@ P_UNSATISFIABLE
satcheck_ipasirt::assumptions
bvt assumptions
Definition: satcheck_ipasir.h:52
invariant.h
satcheck_ipasir.h
satcheck_ipasirt::satcheck_ipasirt
satcheck_ipasirt()
satcheck_ipasirt::solver_text
const std::string solver_text() override
This method returns the description produced by the linked SAT solver.
messaget::eom
static eomt eom
Definition: message.h:297
literalt::var_no
var_not var_no() const
Definition: literal.h:83
forall_literals
#define forall_literals(it, bv)
Definition: literal.h:203
cnf_solvert::statust::ERROR
@ ERROR
is_false
bool is_false(const literalt &l)
Definition: literal.h:197
literalt::is_true
bool is_true() const
Definition: literal.h:156
satcheck_ipasirt::is_in_conflict
bool is_in_conflict(literalt a) const override
Returns true if an assumption is in the final conflict.
satcheck_ipasirt::l_get
tvt l_get(literalt a) const override final
This method returns the truth value for a literal of the current SAT model.
cnf_solvert::statust::UNSAT
@ UNSAT
cnf_solvert::status
statust status
Definition: cnf.h:84
propt::resultt::P_SATISFIABLE
@ P_SATISFIABLE
literalt::is_false
bool is_false() const
Definition: literal.h:161
satcheck_ipasirt::lcnf
void lcnf(const bvt &bv) override final
cnf_solvert::clause_counter
size_t clause_counter
Definition: cnf.h:85
tvt::unknown
static tvt unknown()
Definition: threeval.h:33
propt::resultt
resultt
Definition: prop.h:99
satcheck_ipasirt::~satcheck_ipasirt
virtual ~satcheck_ipasirt() override
tvt
Definition: threeval.h:20
literalt::sign
bool sign() const
Definition: literal.h:88
solver
int solver(std::istream &in)
Definition: smt2_solver.cpp:364
satcheck_ipasirt::set_assumptions
void set_assumptions(const bvt &_assumptions) override
literalt
Definition: literal.h:26
propt::log
messaget log
Definition: prop.h:130
literalt::is_constant
bool is_constant() const
Definition: literal.h:166
cnft::no_variables
virtual size_t no_variables() const override
Definition: cnf.h:41
is_true
bool is_true(const literalt &l)
Definition: literal.h:198
satcheck_ipasirt::do_prop_solve
resultt do_prop_solve() override
validation_modet::INVARIANT
@ INVARIANT
messaget::statistics
mstreamt & statistics() const
Definition: message.h:419
analysis_exceptiont
Thrown when an unexpected error occurs during the analysis (e.g., when the SAT solver returns an erro...
Definition: exception_utils.h:157
satcheck_ipasirt::solver
void * solver
Definition: satcheck_ipasir.h:50