Commit d5e78c71 authored by Christian Müller's avatar Christian Müller

better tests

parent 8542dd8d
......@@ -10,6 +10,9 @@ import java.util.HashMap
import com.typesafe.scalalogging.LazyLogging
object toZ3 extends LazyLogging {
type VarCtx = Map[Var, (Option[Int], Expr, Sort)]
def succ(ctx: Context, e: ArithExpr) = {
val One = ctx.mkInt(1)
ctx.mkAdd(e, One)
......@@ -17,6 +20,7 @@ object toZ3 extends LazyLogging {
var counter = 0
// TODO: how to not make this static?
val fun_ctx = new HashMap[String, FuncDecl]()
def new_tname() = {
......@@ -24,7 +28,7 @@ object toZ3 extends LazyLogging {
"t" + counter
}
def buildVarCtx(ctx: Context, var_ctx: Map[Var, (Int, Expr, Sort)], vars: List[Var]) = {
def buildVarCtx(ctx: Context, var_ctx: VarCtx, vars: List[Var]): VarCtx = {
val indices = (vars.size - 1) to 0 by -1
val newexprs = (for ((v, i) <- vars.zip(indices)) yield {
// count = count - 1;
......@@ -35,26 +39,31 @@ object toZ3 extends LazyLogging {
} else {
ctx.mkFiniteDomainSort(v.typ, 1) // TODO: sort size?
}
v -> (i, ctx.mkBound(i, sort), sort)
v -> (Some(i), ctx.mkBound(i, sort), sort)
}) toMap
// if the index is defined, increment, otherwise use the expr (which is a constant f.e.)
val oldvars = (for ((v, (i, e, s)) <- var_ctx) yield {
val newi = i + vars.size
val newbound = ctx.mkBound(newi, s)
v -> (newi, newbound, s)
if (i.isDefined) {
val newi = i.get + vars.size
val newbound = ctx.mkBound(newi, s)
v -> (Some(newi), newbound, s)
} else {
v -> (i, e, s)
}
})
newexprs ++ oldvars
}
def updateTime(texpr: ArithExpr, tvar: Option[Var], oldctx: Map[Var, (Int, Expr, Sort)], newctx: Map[Var, (Int, Expr, Sort)]) = {
def updateTime(texpr: ArithExpr, tvar: Option[Var], oldctx: VarCtx, newctx: VarCtx) = {
if (tvar.isEmpty) { texpr } else {
texpr.substitute(oldctx(tvar.get)._2, newctx(tvar.get)._2).asInstanceOf[ArithExpr]
}
}
def unaryTemporalOperator(ctx: Context, f: Formula, texpr: ArithExpr, tvar: Option[Var],
var_ctx: Map[Var, (Int, Expr, Sort)]) = {
var_ctx: VarCtx) = {
val new_tvar_name = new_tname()
val new_tsymbol = ctx.mkSymbol(new_tvar_name)
val new_tvar = Var(new_tvar_name, "Int")
......@@ -71,7 +80,7 @@ object toZ3 extends LazyLogging {
}
def toZ3(ctx: Context, f: Formula, texpr: ArithExpr, tvar: Option[Var],
var_ctx: Map[Var, (Int, Expr, Sort)]): BoolExpr = {
var_ctx: VarCtx): BoolExpr = {
// texpr is an expression e with the form given by the following grammar:
// e ::= 0 | t | succ(e)
// tvar is Some(t) if e contains t or None otherwise
......@@ -214,24 +223,19 @@ object toZ3 extends LazyLogging {
}
}
def checkZ3(f: Formula) = {
val cfg = new HashMap[String, String]();
cfg.put("model", "true");
val ctx = new Context(cfg);
def translate(f: Formula, ctx: Context) = {
logger.info(s"Using formula:\n$f")
fun_ctx.clear()
val expr = toZ3(ctx, f, ctx.mkInt(0), None, Map())
logger.info(s"Checking Z3 expression:\n$expr")
val s = ctx.mkSolver()
s.add(expr)
s
logger.info(s"Built Z3 expression:\n$expr")
expr
}
def checkZ3(f: Formula, ctx: Context, varctx: Map[Var, (Int, Expr, Sort)]) = {
def translate(f: Formula, ctx: Context, varctx: VarCtx) = {
logger.info(s"Using formula:\n${f.pretty()}")
fun_ctx.clear()
val expr = toZ3(ctx, f, ctx.mkInt(0), None, varctx)
logger.info(s"Checking Z3 expression:\n$expr")
val s = ctx.mkSolver()
s.add(expr)
s
logger.info(s"Built Z3 expression:\n$expr")
expr
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment