|
1 | 1 | package io.ksmt.solver.bitwuzla |
2 | 2 |
|
3 | | -import it.unimi.dsi.fastutil.longs.LongOpenHashSet |
4 | 3 | import io.ksmt.KContext |
5 | | -import io.ksmt.expr.KExpr |
6 | | -import io.ksmt.solver.KModel |
7 | | -import io.ksmt.solver.KSolver |
8 | | -import io.ksmt.solver.KSolverStatus |
9 | | -import org.ksmt.solver.bitwuzla.bindings.BitwuzlaNativeException |
10 | | -import org.ksmt.solver.bitwuzla.bindings.BitwuzlaOption |
11 | | -import org.ksmt.solver.bitwuzla.bindings.BitwuzlaResult |
12 | | -import org.ksmt.solver.bitwuzla.bindings.BitwuzlaTerm |
13 | | -import org.ksmt.solver.bitwuzla.bindings.BitwuzlaTermArray |
14 | | -import org.ksmt.solver.bitwuzla.bindings.Native |
15 | | -import io.ksmt.sort.KBoolSort |
16 | | -import kotlin.time.Duration |
17 | 4 |
|
18 | | -open class KBitwuzlaSolver(private val ctx: KContext) : KSolver<KBitwuzlaSolverConfiguration> { |
19 | | - open val bitwuzlaCtx = KBitwuzlaContext(ctx) |
20 | | - open val exprInternalizer: KBitwuzlaExprInternalizer by lazy { |
21 | | - KBitwuzlaExprInternalizer(bitwuzlaCtx) |
22 | | - } |
23 | | - open val exprConverter: KBitwuzlaExprConverter by lazy { |
24 | | - KBitwuzlaExprConverter(ctx, bitwuzlaCtx) |
25 | | - } |
26 | | - |
27 | | - private var lastCheckStatus = KSolverStatus.UNKNOWN |
28 | | - private var lastReasonOfUnknown: String? = null |
29 | | - private var lastAssumptions: TrackedAssumptions? = null |
30 | | - private var lastModel: KBitwuzlaModel? = null |
31 | | - |
32 | | - init { |
33 | | - Native.bitwuzlaSetOption(bitwuzlaCtx.bitwuzla, BitwuzlaOption.BITWUZLA_OPT_INCREMENTAL, value = 1) |
34 | | - Native.bitwuzlaSetOption(bitwuzlaCtx.bitwuzla, BitwuzlaOption.BITWUZLA_OPT_PRODUCE_MODELS, value = 1) |
35 | | - } |
36 | | - |
37 | | - private var trackedAssertions = mutableListOf<Pair<KExpr<KBoolSort>, BitwuzlaTerm>>() |
38 | | - private val trackVarsAssertionFrames = arrayListOf(trackedAssertions) |
| 5 | +open class KBitwuzlaSolver(ctx: KContext) : KBitwuzlaSolverBase(ctx) { |
39 | 6 |
|
40 | 7 | override fun configure(configurator: KBitwuzlaSolverConfiguration.() -> Unit) { |
41 | 8 | KBitwuzlaSolverConfigurationImpl(bitwuzlaCtx.bitwuzla).configurator() |
42 | 9 | } |
43 | | - |
44 | | - override fun assert(expr: KExpr<KBoolSort>) = bitwuzlaCtx.bitwuzlaTry { |
45 | | - ctx.ensureContextMatch(expr) |
46 | | - |
47 | | - val assertionWithAxioms = with(exprInternalizer) { expr.internalizeAssertion() } |
48 | | - |
49 | | - assertionWithAxioms.axioms.forEach { |
50 | | - Native.bitwuzlaAssert(bitwuzlaCtx.bitwuzla, it) |
51 | | - } |
52 | | - Native.bitwuzlaAssert(bitwuzlaCtx.bitwuzla, assertionWithAxioms.assertion) |
53 | | - } |
54 | | - |
55 | | - override fun assertAndTrack(expr: KExpr<KBoolSort>) = bitwuzlaCtx.bitwuzlaTry { |
56 | | - ctx.ensureContextMatch(expr) |
57 | | - |
58 | | - val trackVarExpr = ctx.mkFreshConst("track", ctx.boolSort) |
59 | | - val trackedExpr = with(ctx) { !trackVarExpr or expr } |
60 | | - |
61 | | - assert(trackedExpr) |
62 | | - |
63 | | - val trackVarTerm = with(exprInternalizer) { trackVarExpr.internalize() } |
64 | | - trackedAssertions += expr to trackVarTerm |
65 | | - } |
66 | | - |
67 | | - override fun push(): Unit = bitwuzlaCtx.bitwuzlaTry { |
68 | | - Native.bitwuzlaPush(bitwuzlaCtx.bitwuzla, nlevels = 1) |
69 | | - |
70 | | - trackedAssertions = trackedAssertions.toMutableList() |
71 | | - trackVarsAssertionFrames.add(trackedAssertions) |
72 | | - |
73 | | - bitwuzlaCtx.createNestedDeclarationScope() |
74 | | - } |
75 | | - |
76 | | - override fun pop(n: UInt): Unit = bitwuzlaCtx.bitwuzlaTry { |
77 | | - val currentLevel = trackVarsAssertionFrames.lastIndex.toUInt() |
78 | | - require(n <= currentLevel) { |
79 | | - "Cannot pop $n scope levels because current scope level is $currentLevel" |
80 | | - } |
81 | | - |
82 | | - if (n == 0u) return |
83 | | - |
84 | | - repeat(n.toInt()) { |
85 | | - trackVarsAssertionFrames.removeLast() |
86 | | - bitwuzlaCtx.popDeclarationScope() |
87 | | - } |
88 | | - |
89 | | - trackedAssertions = trackVarsAssertionFrames.last() |
90 | | - |
91 | | - Native.bitwuzlaPop(bitwuzlaCtx.bitwuzla, n.toInt()) |
92 | | - } |
93 | | - |
94 | | - override fun check(timeout: Duration): KSolverStatus = |
95 | | - checkWithAssumptions(emptyList(), timeout) |
96 | | - |
97 | | - override fun checkWithAssumptions(assumptions: List<KExpr<KBoolSort>>, timeout: Duration): KSolverStatus = |
98 | | - bitwuzlaTryCheck { |
99 | | - ctx.ensureContextMatch(assumptions) |
100 | | - |
101 | | - val currentAssumptions = TrackedAssumptions().also { lastAssumptions = it } |
102 | | - |
103 | | - trackedAssertions.forEach { |
104 | | - currentAssumptions.assumeTrackedAssertion(it) |
105 | | - } |
106 | | - |
107 | | - with(exprInternalizer) { |
108 | | - assumptions.forEach { |
109 | | - currentAssumptions.assumeAssumption(it, it.internalize()) |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - checkWithTimeout(timeout).processCheckResult() |
114 | | - } |
115 | | - |
116 | | - private fun checkWithTimeout(timeout: Duration): BitwuzlaResult = if (timeout.isInfinite()) { |
117 | | - Native.bitwuzlaCheckSatResult(bitwuzlaCtx.bitwuzla) |
118 | | - } else { |
119 | | - Native.bitwuzlaCheckSatTimeoutResult(bitwuzlaCtx.bitwuzla, timeout.inWholeMilliseconds) |
120 | | - } |
121 | | - |
122 | | - override fun model(): KModel = bitwuzlaCtx.bitwuzlaTry { |
123 | | - require(lastCheckStatus == KSolverStatus.SAT) { "Model are only available after SAT checks" } |
124 | | - val model = lastModel ?: KBitwuzlaModel( |
125 | | - ctx, bitwuzlaCtx, exprConverter, |
126 | | - bitwuzlaCtx.declarations(), |
127 | | - bitwuzlaCtx.uninterpretedSortsWithRelevantDecls() |
128 | | - ) |
129 | | - lastModel = model |
130 | | - model |
131 | | - } |
132 | | - |
133 | | - override fun unsatCore(): List<KExpr<KBoolSort>> = bitwuzlaCtx.bitwuzlaTry { |
134 | | - require(lastCheckStatus == KSolverStatus.UNSAT) { "Unsat cores are only available after UNSAT checks" } |
135 | | - val unsatAssumptions = Native.bitwuzlaGetUnsatAssumptions(bitwuzlaCtx.bitwuzla) |
136 | | - lastAssumptions?.resolveUnsatCore(unsatAssumptions) ?: emptyList() |
137 | | - } |
138 | | - |
139 | | - override fun reasonOfUnknown(): String = bitwuzlaCtx.bitwuzlaTry { |
140 | | - require(lastCheckStatus == KSolverStatus.UNKNOWN) { |
141 | | - "Unknown reason is only available after UNKNOWN checks" |
142 | | - } |
143 | | - |
144 | | - // There is no way to retrieve reason of unknown from Bitwuzla in general case. |
145 | | - return lastReasonOfUnknown ?: "unknown" |
146 | | - } |
147 | | - |
148 | | - override fun interrupt() = bitwuzlaCtx.bitwuzlaTry { |
149 | | - Native.bitwuzlaForceTerminate(bitwuzlaCtx.bitwuzla) |
150 | | - } |
151 | | - |
152 | | - override fun close() = bitwuzlaCtx.bitwuzlaTry { |
153 | | - bitwuzlaCtx.close() |
154 | | - } |
155 | | - |
156 | | - private fun BitwuzlaResult.processCheckResult() = when (this) { |
157 | | - BitwuzlaResult.BITWUZLA_SAT -> KSolverStatus.SAT |
158 | | - BitwuzlaResult.BITWUZLA_UNSAT -> KSolverStatus.UNSAT |
159 | | - BitwuzlaResult.BITWUZLA_UNKNOWN -> KSolverStatus.UNKNOWN |
160 | | - }.also { lastCheckStatus = it } |
161 | | - |
162 | | - private fun invalidateSolverState() { |
163 | | - /** |
164 | | - * Bitwuzla model is only valid until the next check-sat call. |
165 | | - * */ |
166 | | - lastModel?.markInvalid() |
167 | | - lastModel = null |
168 | | - |
169 | | - lastCheckStatus = KSolverStatus.UNKNOWN |
170 | | - lastReasonOfUnknown = null |
171 | | - |
172 | | - lastAssumptions = null |
173 | | - } |
174 | | - |
175 | | - private inline fun bitwuzlaTryCheck(body: () -> KSolverStatus): KSolverStatus = try { |
176 | | - invalidateSolverState() |
177 | | - body() |
178 | | - } catch (ex: BitwuzlaNativeException) { |
179 | | - lastReasonOfUnknown = ex.message |
180 | | - KSolverStatus.UNKNOWN.also { lastCheckStatus = it } |
181 | | - } |
182 | | - |
183 | | - private inner class TrackedAssumptions { |
184 | | - private val assumedExprs = arrayListOf<Pair<KExpr<KBoolSort>, BitwuzlaTerm>>() |
185 | | - |
186 | | - fun assumeTrackedAssertion(trackedAssertion: Pair<KExpr<KBoolSort>, BitwuzlaTerm>) { |
187 | | - assumedExprs.add(trackedAssertion) |
188 | | - Native.bitwuzlaAssume(bitwuzlaCtx.bitwuzla, trackedAssertion.second) |
189 | | - } |
190 | | - |
191 | | - fun assumeAssumption(expr: KExpr<KBoolSort>, term: BitwuzlaTerm) = |
192 | | - assumeTrackedAssertion(expr to term) |
193 | | - |
194 | | - fun resolveUnsatCore(unsatAssumptions: BitwuzlaTermArray): List<KExpr<KBoolSort>> { |
195 | | - val unsatCoreTerms = LongOpenHashSet(unsatAssumptions) |
196 | | - return assumedExprs.mapNotNull { (expr, term) -> expr.takeIf { unsatCoreTerms.contains(term) } } |
197 | | - } |
198 | | - } |
199 | 10 | } |
0 commit comments