Skip to content
Snippets Groups Projects
Commit c727548f authored by Manos Koukoutos's avatar Manos Koukoutos
Browse files

Merge ND-evaluator into master

parents cd7a4652 9a418d03
No related branches found
No related tags found
No related merge requests found
Showing with 194 additions and 61 deletions
import leon.lang._
object Existentials {
def exists[A](p: A => Boolean): Boolean = !forall((x: A) => !p(x))
def check1(y: BigInt, p: BigInt => Boolean) : Boolean = {
p(y) == exists((y1:BigInt) => p(y1))
}.holds
/*
def check2(y: BigInt, p: BigInt => Boolean) : Boolean = {
p(y) ==> exists((y1:BigInt) => p(y1))
}.holds
*/
}
import leon.lang._
object ForallAssoc {
/*
def test3(f: (BigInt, BigInt) => BigInt): Boolean = {
require(forall((x: BigInt, y: BigInt, z: BigInt) => f(x, f(y, z)) == f(f(x, y), z)))
f(1, f(2, f(3, f(4, 5)))) == f(f(f(f(1, 2), 3), 4), 4)
}.holds
*/
def test4(f: (BigInt, BigInt) => BigInt): Boolean = {
require(forall((x: BigInt, y: BigInt, z: BigInt) => f(x, f(y, z)) == f(f(x, y), z)))
f(1, f(2, f(3, 4))) == 0
}.holds
}
import leon.lang._
object Existentials {
def exists[A](p: A => Boolean): Boolean = !forall((x: A) => !p(x))
/*
def check1(y: BigInt, p: BigInt => Boolean) : Boolean = {
p(y) == exists((y1:BigInt) => p(y1))
}.holds
*/
def check2(y: BigInt, p: BigInt => Boolean) : Boolean = {
p(y) ==> exists((y1:BigInt) => p(y1))
}.holds
}
import leon.lang._
object ForallAssoc {
def ex[A](x1: A, x2: A, x3: A, x4: A, x5: A, f: (A, A) => A) = {
require(forall {
(x: A, y: A, z: A) => f(x, f(y, z)) == f(f(x, y), z)
})
f(x1, f(x2, f(x3, f(x4, x5)))) == f(f(x1, f(x2, f(x3, x4))), x5)
}.holds
def test1(f: (BigInt, BigInt) => BigInt): Boolean = {
require(forall((x: BigInt, y: BigInt, z: BigInt) => f(x, f(y, z)) == f(f(x, y), z)))
f(1, f(2, f(3, 4))) == f(f(f(1, 2), 3), 4)
}.holds
def test2(f: (BigInt, BigInt) => BigInt): Boolean = {
require(forall((x: BigInt, y: BigInt, z: BigInt) => f(x, f(y, z)) == f(f(x, y), z)))
f(1, f(2, f(3, f(4, 5)))) == f(f(f(f(1, 2), 3), 4), 5)
}.holds
}
package leon.monads.predicate
import leon.collection._
import leon.lang._
import leon.annotation._
object Predicate {
def exists[A](p: A => Boolean): Boolean = !forall((a: A) => !p(a))
// Monadic bind
@inline
def flatMap[A,B](p: A => Boolean, f: A => (B => Boolean)): B => Boolean = {
(b: B) => exists[A](a => p(a) && f(a)(b))
}
// All monads are also functors, and they define the map function
@inline
def map[A,B](p: A => Boolean, f: A => B): B => Boolean = {
(b: B) => exists[A](a => p(a) && f(a) == b)
}
/*
@inline
def >>=[B](f: A => Predicate[B]): Predicate[B] = flatMap(f)
@inline
def >>[B](that: Predicate[B]) = >>= ( _ => that )
@inline
def withFilter(f: A => Boolean): Predicate[A] = {
Predicate { a => p(a) && f(a) }
}
*/
def equals[A](p: A => Boolean, that: A => Boolean): Boolean = {
forall[A](a => p(a) == that(a))
}
def test[A,B,C](p: A => Boolean, f: A => B, g: B => C): Boolean = {
equals(map(map(p, f), g), map(p, (a: A) => g(f(a))))
}.holds
def testInt(p: BigInt => Boolean, f: BigInt => BigInt, g: BigInt => BigInt): Boolean = {
equals(map(map(p, f), g), map(p, (x: BigInt) => g(f(x))))
}.holds
}
...@@ -32,61 +32,72 @@ class SolversSuite extends LeonTestSuiteWithProgram { ...@@ -32,61 +32,72 @@ class SolversSuite extends LeonTestSuiteWithProgram {
) else Nil) ) else Nil)
} }
// Check that we correctly extract several types from solver models val types = Seq(
for ((sname, sf) <- getFactories) { BooleanType,
test(s"Model Extraction in $sname") { implicit fix => UnitType,
val ctx = fix._1 CharType,
val pgm = fix._2 RealType,
IntegerType,
Int32Type,
TypeParameter.fresh("T"),
SetType(IntegerType),
MapType(IntegerType, IntegerType),
FunctionType(Seq(IntegerType), IntegerType),
TupleType(Seq(IntegerType, BooleanType, Int32Type))
)
val solver = sf(ctx, pgm) val vs = types.map(FreshIdentifier("v", _).toVariable)
val types = Seq( // We need to make sure models are not co-finite
BooleanType, val cnstrs = vs.map(v => v.getType match {
UnitType, case UnitType =>
CharType, Equals(v, simplestValue(v.getType))
IntegerType, case SetType(base) =>
Int32Type, Not(ElementOfSet(simplestValue(base), v))
TypeParameter.fresh("T"), case MapType(from, to) =>
SetType(IntegerType), Not(Equals(MapApply(v, simplestValue(from)), simplestValue(to)))
MapType(IntegerType, IntegerType), case FunctionType(froms, to) =>
TupleType(Seq(IntegerType, BooleanType, Int32Type)) Not(Equals(Application(v, froms.map(simplestValue)), simplestValue(to)))
) case _ =>
not(Equals(v, simplestValue(v.getType)))
})
val vs = types.map(FreshIdentifier("v", _).toVariable) def checkSolver(solver: Solver, vs: Set[Variable], cnstr: Expr)(implicit fix: (LeonContext, Program)): Unit = {
try {
solver.assertCnstr(cnstr)
solver.check match {
// We need to make sure models are not co-finite case Some(true) =>
val cnstr = andJoin(vs.map(v => v.getType match { val model = solver.getModel
case UnitType => for (v <- vs) {
Equals(v, simplestValue(v.getType)) if (model.isDefinedAt(v.id)) {
case SetType(base) => assert(model(v.id).getType === v.getType, "Extracting value of type "+v.getType)
Not(ElementOfSet(simplestValue(base), v)) } else {
case MapType(from, to) => fail("Model does not contain "+v.id+" of type "+v.getType)
Not(Equals(MapApply(v, simplestValue(from)), simplestValue(to)))
case _ =>
not(Equals(v, simplestValue(v.getType)))
}))
try {
solver.assertCnstr(cnstr)
solver.check match {
case Some(true) =>
val model = solver.getModel
for (v <- vs) {
if (model.isDefinedAt(v.id)) {
assert(model(v.id).getType === v.getType, "Extracting value of type "+v.getType)
} else {
fail("Model does not contain "+v.id+" of type "+v.getType)
}
} }
case _ => }
fail("Constraint "+cnstr.asString+" is unsat!?") case _ =>
} fail("Constraint "+cnstr.asString+" is unsat!?")
} finally {
solver.free()
} }
} finally {
solver.free
}
}
// Check that we correctly extract several types from solver models
for ((sname, sf) <- getFactories) {
test(s"Model Extraction in $sname") { implicit fix =>
val ctx = fix._1
val pgm = fix._2
val solver = sf(ctx, pgm)
checkSolver(solver, vs.toSet, andJoin(cnstrs))
}
}
test(s"Data generation in enum solver") { implicit fix =>
for ((v,cnstr) <- vs zip cnstrs) {
val solver = new EnumerationSolver(fix._1, fix._2)
checkSolver(solver, Set(v), cnstr)
} }
} }
} }
...@@ -154,9 +154,9 @@ object Injection { ...@@ -154,9 +154,9 @@ object Injection {
case class Nil() extends List case class Nil() extends List
// proved with unrolling=0 // proved with unrolling=0
def size(l: List) : Int = (l match { def size(l: List) : BigInt = (l match {
case Nil() => 0 case Nil() => BigInt(0)
case Cons(t) => 1 + size(t) case Cons(t) => BigInt(1) + size(t)
}) ensuring(res => res >= 0) }) ensuring(res => res >= 0)
def simple(in: List) = choose{out: List => size(out) == size(in) } def simple(in: List) = choose{out: List => size(out) == size(in) }
...@@ -274,10 +274,10 @@ object ChurchNumerals { ...@@ -274,10 +274,10 @@ object ChurchNumerals {
case object Z extends Num case object Z extends Num
case class S(pred: Num) extends Num case class S(pred: Num) extends Num
def value(n:Num) : Int = { def value(n:Num) : BigInt = {
n match { n match {
case Z => 0 case Z => BigInt(0)
case S(p) => 1 + value(p) case S(p) => BigInt(1) + value(p)
} }
} ensuring (_ >= 0) } ensuring (_ >= 0)
...@@ -309,10 +309,10 @@ object ChurchNumerals { ...@@ -309,10 +309,10 @@ object ChurchNumerals {
case object Z extends Num case object Z extends Num
case class S(pred: Num) extends Num case class S(pred: Num) extends Num
def value(n:Num) : Int = { def value(n:Num) : BigInt = {
n match { n match {
case Z => 0 case Z => BigInt(0)
case S(p) => 1 + value(p) case S(p) => BigInt(1) + value(p)
} }
} ensuring (_ >= 0) } ensuring (_ >= 0)
......
...@@ -59,10 +59,10 @@ class PureScalaValidSuite3 extends PureScalaValidSuite { ...@@ -59,10 +59,10 @@ class PureScalaValidSuite3 extends PureScalaValidSuite {
val optionVariants = List(opts(2)) val optionVariants = List(opts(2))
} }
class PureScalaValidSuiteZ3 extends PureScalaValidSuite { class PureScalaValidSuiteZ3 extends PureScalaValidSuite {
val optionVariants = if (isZ3Available) List(opts(3)) else Nil val optionVariants = Nil//if (isZ3Available) List(opts(3)) else Nil
} }
class PureScalaValidSuiteCVC4 extends PureScalaValidSuite { class PureScalaValidSuiteCVC4 extends PureScalaValidSuite {
val optionVariants = if (isCVC4Available) List(opts(4)) else Nil val optionVariants = Nil//if (isCVC4Available) List(opts(4)) else Nil
} }
class PureScalaInvalidSuite extends PureScalaVerificationSuite { class PureScalaInvalidSuite extends PureScalaVerificationSuite {
......
...@@ -30,10 +30,12 @@ object Complete { ...@@ -30,10 +30,12 @@ object Complete {
if(i < 0) -i else i if(i < 0) -i else i
} ensuring(_ >= 0) } ensuring(_ >= 0)
def dispatch(es: (BigInt, BigInt), rest: (List, List)): (List, List) = {
(Cons(es._1, rest._1), Cons(es._2, rest._2))
}
def split(list : List) : (List,List) = { def split(list : List) : (List,List) = {
choose { (res : (List,List)) => splitSpec(list, res) } choose { (res : (List,List)) => splitSpec(list, res) }
} }
// case (h1, (h2, t)) => (h1 :: split(t)._1, h2 :: split(t)._2)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment