summaryrefslogtreecommitdiff
path: root/src/prop/kissat.cpp
blob: 10b7b07de3a885c5c1dfbacf19aa868e6c62ac0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*********************                                                        */
/*! \file kissat.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Aina Niemetz
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 ** in the top-level source directory and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Wrapper for Kissat SAT Solver.
 **
 ** Wrapper for the Kissat SAT solver (for theory of bit-vectors).
 **/

#include "prop/kissat.h"

#ifdef CVC4_USE_KISSAT

#include "base/check.h"

namespace cvc5 {
namespace prop {

using KissatLit = int32_t;
using KissatVar = int32_t;

// helper functions
namespace {

SatValue toSatValue(int32_t result)
{
  if (result == 10) return SAT_VALUE_TRUE;
  if (result == 20) return SAT_VALUE_FALSE;
  Assert(result == 0);
  return SAT_VALUE_UNKNOWN;
}

/**
 * Helper to convert the value of a literal as returned by Kissat to the
 * corresponding true/false SAT_VALUE.
 * Note: Kissat returns lit/-lit for true/false. Older versions returned 1/-1.
 */
SatValue toSatValueLit(int value)
{
  if (value > 0) return SAT_VALUE_TRUE;
  Assert(value < 0);
  return SAT_VALUE_FALSE;
}

/** Helper to convert SatLiteral to KissatLit. */
KissatLit toKissatLit(const SatLiteral lit)
{
  return lit.isNegated() ? -lit.getSatVariable() : lit.getSatVariable();
}

/** Helper to convert a SatVariable to a KissatVar. */
KissatVar toKissatVar(SatVariable var) { return var; }

}  // namespace

KissatSolver::KissatSolver(StatisticsRegistry* registry,
                           const std::string& name)
    : d_solver(kissat_init()),
      // Note: Kissat variables start with index 1 rather than 0 since negated
      //       literals are represented as the negation of the index.
      d_nextVarIdx(1),
      d_statistics(registry, name)
{
}

void KissatSolver::init()
{
  d_true = newVar();
  d_false = newVar();
  kissat_add(d_solver, toKissatVar(d_true));
  kissat_add(d_solver, 0);
  kissat_add(d_solver, -toKissatVar(d_false));
  kissat_add(d_solver, 0);
}

KissatSolver::~KissatSolver() { kissat_release(d_solver); }

ClauseId KissatSolver::addClause(SatClause& clause, bool removable)
{
  for (const SatLiteral& lit : clause)
  {
    kissat_add(d_solver, toKissatLit(lit));
  }
  kissat_add(d_solver, 0);
  ++d_statistics.d_numClauses;
  return ClauseIdError;
}

ClauseId KissatSolver::addXorClause(SatClause& clause, bool rhs, bool removable)
{
  Unreachable() << "Kissat does not support adding XOR clauses.";
}

SatVariable KissatSolver::newVar(bool isTheoryAtom,
                                 bool preRegister,
                                 bool canErase)
{
  ++d_statistics.d_numVariables;
  return d_nextVarIdx++;
}

SatVariable KissatSolver::trueVar() { return d_true; }

SatVariable KissatSolver::falseVar() { return d_false; }

SatValue KissatSolver::solve()
{
  TimerStat::CodeTimer codeTimer(d_statistics.d_solveTime);
  SatValue res = toSatValue(kissat_solve(d_solver));
  d_okay = (res == SAT_VALUE_TRUE);
  ++d_statistics.d_numSatCalls;
  return res;
}

SatValue KissatSolver::solve(long unsigned int&)
{
  Unimplemented() << "Setting limits for Kissat not supported yet";
};

SatValue KissatSolver::solve(const std::vector<SatLiteral>& assumptions)
{
  Unimplemented() << "Incremental solving with Kissat not supported yet";
}

void KissatSolver::interrupt() { kissat_terminate(d_solver); }

SatValue KissatSolver::value(SatLiteral l)
{
  Assert(d_okay);
  return toSatValueLit(kissat_value(d_solver, toKissatLit(l)));
}

SatValue KissatSolver::modelValue(SatLiteral l)
{
  Assert(d_okay);
  return value(l);
}

unsigned KissatSolver::getAssertionLevel() const
{
  Unreachable() << "Kissat does not support assertion levels.";
}

bool KissatSolver::ok() const { return d_okay; }

KissatSolver::Statistics::Statistics(StatisticsRegistry* registry,
                                     const std::string& prefix)
    : d_registry(registry),
      d_numSatCalls("theory::bv::" + prefix + "::Kissat::calls_to_solve", 0),
      d_numVariables("theory::bv::" + prefix + "::Kissat::variables", 0),
      d_numClauses("theory::bv::" + prefix + "::Kissat::clauses", 0),
      d_solveTime("theory::bv::" + prefix + "::Kissat::solve_time")
{
  d_registry->registerStat(&d_numSatCalls);
  d_registry->registerStat(&d_numVariables);
  d_registry->registerStat(&d_numClauses);
  d_registry->registerStat(&d_solveTime);
}

KissatSolver::Statistics::~Statistics()
{
  d_registry->unregisterStat(&d_numSatCalls);
  d_registry->unregisterStat(&d_numVariables);
  d_registry->unregisterStat(&d_numClauses);
  d_registry->unregisterStat(&d_solveTime);
}

}  // namespace prop
}  // namespace cvc5

#endif  // CVC4_USE_KISSAT
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback