Skip to content

Commit 57942e7

Browse files
committed
Always free solver + minor API improvements
1 parent c1798fc commit 57942e7

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/main/scala/inox/solvers/SolverFactory.scala

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,32 @@ object SolverFactory {
9090
// extract <exec> in "smt-z3:<exec>"
9191
private def getZ3Executable(name: String): String = name.drop(7)
9292

93-
def getFromName(noIncName: String, force: Boolean = false)
93+
// extract solver in "no-inc:solver"
94+
private def removeNoInc(name: String): String = name.drop(7)
95+
96+
def getFromName(name: String, force: Boolean = false)
9497
(p: Program, ctx: Context)
9598
(enc: ProgramTransformer {
9699
val sourceProgram: p.type
97100
val targetProgram: Program { val trees: inox.trees.type }
98101
})(implicit sem: p.Semantics): SolverFactory { val program: p.type; type S <: TimeoutSolver { val program: p.type } } = {
99102

100-
val nonIncremental = noIncName.startsWith("no-inc:")
101-
val name = if (nonIncremental) noIncName.drop(7) else noIncName
103+
val nonIncremental = name.startsWith("no-inc:")
104+
val noPrefixName = if (nonIncremental) removeNoInc(name) else name
102105

103106
if (
104107
nonIncremental &&
105-
name != "smt-cvc4" &&
106-
name != "unrollz3" &&
107-
name != "smt-z3" &&
108-
!name.startsWith("smt-z3:")
108+
noPrefixName != "smt-cvc4" &&
109+
noPrefixName != "unrollz3" &&
110+
noPrefixName != "smt-z3" &&
111+
!noPrefixName.startsWith("smt-z3:")
109112
)
110113
throw FatalError(s"Non incremental mode is not available for solver $name")
111114

112115
val finalName = if (force) {
113-
name
116+
noPrefixName
114117
} else {
115-
fallbacks.get(name) match {
118+
fallbacks.get(noPrefixName) match {
116119
case Some((guard, names, requirement)) if !guard() =>
117120
val replacement = names.collectFirst {
118121
case name if fallbacks(name)._1() => name
@@ -124,20 +127,20 @@ object SolverFactory {
124127
}
125128
replacement
126129

127-
case Some(_) => name
128-
case None if name.startsWith("smt-z3:") =>
129-
val z3Exec = getZ3Executable(name)
130+
case Some(_) => noPrefixName
131+
case None if noPrefixName.startsWith("smt-z3:") =>
132+
val z3Exec = getZ3Executable(noPrefixName)
130133
val hasZ3Exec = try {
131134
new Z3Interpreter(z3Exec, Array("-in", "-smt2"))
132135
true
133136
} catch {
134137
case _: java.io.IOException => false
135138
}
136139

137-
if (hasZ3Exec) name
140+
if (hasZ3Exec) noPrefixName
138141
else throw FatalError("Unknown solver: " + z3Exec)
139142

140-
case _ => throw FatalError("Unknown solver: " + name)
143+
case _ => throw FatalError("Unknown solver: " + noPrefixName)
141144
}
142145
}
143146

@@ -367,9 +370,13 @@ object SolverFactory {
367370
}
368371
}
369372

370-
private def supportedSolverRaw(s: String) = solverNames.contains(s) || s.startsWith("smt-z3:")
371373
def supportedSolver(s: String) =
372-
supportedSolverRaw(s) || (s.startsWith("no-inc:") && supportedSolverRaw(s.drop(7)))
374+
solverNames.contains(s) ||
375+
s.startsWith("smt-z3:") ||
376+
s.startsWith("no-inc:smt-z3:") ||
377+
s == "no-inc:smt-z3" ||
378+
s == "no-inc:smt-cvc4" ||
379+
s == "no-inc:unrollz3"
373380

374381
def getFromSettings(p: Program, ctx: Context)
375382
(enc: ProgramTransformer {

src/main/scala/inox/solvers/combinators/NonIncrementalSolver.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,31 @@ trait NonIncrementalSolver extends AbstractSolver { self =>
2929

3030
override def check(config: CheckConfiguration): config.Response[Model, Assumptions] = {
3131
val newSolver = underlying()
32-
currentSolver = Some(newSolver)
33-
for (expression <- assertions)
34-
newSolver.assertCnstr(expression)
35-
val res = newSolver.check(config)
36-
newSolver.free()
37-
currentSolver = None
38-
res
32+
try {
33+
currentSolver = Some(newSolver)
34+
for (expression <- assertions)
35+
newSolver.assertCnstr(expression)
36+
val res = newSolver.check(config)
37+
currentSolver = None
38+
res
39+
} finally {
40+
newSolver.free()
41+
}
3942
}
4043

4144
override def checkAssumptions(config: Configuration)
4245
(assumptions: Set[Trees]): config.Response[Model, Assumptions] = {
4346
val newSolver = underlying()
44-
currentSolver = Some(newSolver)
45-
for (expression <- assertions)
46-
newSolver.assertCnstr(expression)
47-
val res = newSolver.checkAssumptions(config)(assumptions)
48-
newSolver.free()
49-
currentSolver = None
50-
res
47+
try {
48+
currentSolver = Some(newSolver)
49+
for (expression <- assertions)
50+
newSolver.assertCnstr(expression)
51+
val res = newSolver.checkAssumptions(config)(assumptions)
52+
currentSolver = None
53+
res
54+
} finally {
55+
newSolver.free()
56+
}
5157
}
5258

5359
def push(): Unit = assertions.push()

0 commit comments

Comments
 (0)