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  for(const auto &literal : bv)
75  {
76  if(literal.is_true())
77  return;
78  else if(!literal.is_false())
79  {
80  INVARIANT(
81  literal.var_no() < (unsigned)no_variables(),
82  "reject out of bound variables");
83  }
84  }
85 
86  for(const auto &literal : bv)
87  {
88  if(!literal.is_false())
89  {
90  // add literal with correct sign
91  ipasir_add(solver, literal.dimacs());
92  }
93  }
94  ipasir_add(solver, 0); // terminate clause
95 
96  with_solver_hardness([this, &bv](solver_hardnesst &hardness) {
97  // To map clauses to lines of program code, track clause indices in the
98  // dimacs cnf output. Dimacs output is generated after processing
99  // clauses to remove duplicates and clauses that are trivially true.
100  // Here, a clause is checked to see if it can be thus eliminated. If
101  // not, add the clause index to list of clauses in
102  // solver_hardnesst::register_clause().
103  static size_t cnf_clause_index = 0;
104  bvt cnf;
105  bool clause_removed = process_clause(bv, cnf);
106 
107  if(!clause_removed)
108  cnf_clause_index++;
109 
110  hardness.register_clause(bv, cnf, cnf_clause_index, !clause_removed);
111  });
112 
113  clause_counter++;
114 }
115 
117 {
118  INVARIANT(status!=statust::ERROR, "there cannot be an error");
119 
120  log.statistics() << (no_variables() - 1) << " variables, " << clause_counter
121  << " clauses" << messaget::eom;
122 
123  // if assumptions contains false, we need this to be UNSAT
124  bvt::const_iterator it =
125  std::find_if(assumptions.begin(), assumptions.end(), is_false);
126  const bool has_false = it != assumptions.end();
127 
128  if(has_false)
129  {
130  log.status() << "got FALSE as assumption: instance is UNSATISFIABLE"
131  << messaget::eom;
132  }
133  else
134  {
135  for(const auto &literal : assumptions)
136  {
137  if(!literal.is_false())
138  ipasir_assume(solver, literal.dimacs());
139  }
140 
141  // solve the formula, and handle the return code (10=SAT, 20=UNSAT)
142  int solver_state = ipasir_solve(solver);
143  if(10 == solver_state)
144  {
145  log.status() << "SAT checker: instance is SATISFIABLE" << messaget::eom;
147  return resultt::P_SATISFIABLE;
148  }
149  else if(20 == solver_state)
150  {
151  log.status() << "SAT checker: instance is UNSATISFIABLE" << messaget::eom;
152  }
153  else
154  {
155  log.status() << "SAT checker: solving returned without solution"
156  << messaget::eom;
157  throw analysis_exceptiont(
158  "solving inside IPASIR SAT solver has been interrupted");
159  }
160  }
161 
164 }
165 
166 void satcheck_ipasirt::set_assignment(literalt a, bool value)
167 {
168  INVARIANT(!a.is_constant(), "cannot set an assignment for a constant");
169  INVARIANT(false, "method not supported");
170 }
171 
173  : cnf_solvert(message_handler), solver(nullptr)
174 {
175  INVARIANT(!solver, "there cannot be a solver already");
176  solver=ipasir_init();
177 }
178 
180 {
181  if(solver)
182  ipasir_release(solver);
183  solver=nullptr;
184 }
185 
187 {
188  return ipasir_failed(solver, a.var_no());
189 }
190 
192 {
193  bvt::const_iterator it = std::find_if(bv.begin(), bv.end(), is_true);
194  const bool has_true = it != bv.end();
195 
196  if(has_true)
197  {
198  assumptions.clear();
199  return;
200  }
201  // only copy assertions, if there is no false in bt parameter
202  assumptions=bv;
203 }
204 
205 #endif
cnft::process_clause
bool process_clause(const bvt &bv, bvt &dest) const
filter 'true' from clause, eliminate duplicates, recognise trivially satisfied clauses
Definition: cnf.cpp:425
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:73
propt::resultt::P_UNSATISFIABLE
@ P_UNSATISFIABLE
satcheck_ipasirt::assumptions
bvt assumptions
Definition: satcheck_ipasir.h:68
invariant.h
satcheck_ipasirt::satcheck_ipasirt
satcheck_ipasirt(message_handlert &message_handler)
satcheck_ipasir.h
satcheck_ipasirt::with_solver_hardness
void with_solver_hardness(std::function< void(solver_hardnesst &)> handler) override
Definition: satcheck_ipasir.h:50
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
cnf_solvert
Definition: cnf.h:73
cnf_solvert::statust::ERROR
@ ERROR
is_false
bool is_false(const literalt &l)
Definition: literal.h:197
solver_hardnesst::register_clause
void register_clause(const bvt &bv, const bvt &cnf, const size_t cnf_clause_index, bool register_cnf)
Called e.g.
Definition: solver_hardness.cpp:92
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:87
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:88
message_handlert
Definition: message.h:28
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_hardnesst
A structure that facilitates collecting the complexity statistics from a decision procedure.
Definition: solver_hardness.h:45
solver
int solver(std::istream &in)
Definition: smt2_solver.cpp:412
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
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:424
cnft::no_variables
virtual size_t no_variables() const override
Definition: cnf.h:42
is_true
bool is_true(const literalt &l)
Definition: literal.h:198
satcheck_ipasirt::do_prop_solve
resultt do_prop_solve() override
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:66