diff --git a/src/main/scala/leon/synthesis/rules/CEGISLike.scala b/src/main/scala/leon/synthesis/rules/CEGISLike.scala index 884c18254fe97cf3cd61d450d70e5ca3d6165252..c2eac3128e2e6c7d48abdc934bdeed086778a1a4 100644 --- a/src/main/scala/leon/synthesis/rules/CEGISLike.scala +++ b/src/main/scala/leon/synthesis/rules/CEGISLike.scala @@ -420,11 +420,11 @@ abstract class CEGISLike[T <% Typed](name: String) extends Rule(name) { res == BooleanLiteral(true) case EvaluationResults.RuntimeError(err) => - sctx.reporter.warning("RE testing CE: "+err) + sctx.reporter.debug("RE testing CE: "+err) false case EvaluationResults.EvaluatorError(err) => - sctx.reporter.error("Error testing CE: "+err) + sctx.reporter.debug("Error testing CE: "+err) false } } diff --git a/testcases/repair/Compiler/Compiler.scala b/testcases/repair/Compiler/Compiler.scala index 697f333e0c5c8adcbd555734f939c3e501a85bff..f0d0ed1f07fc49fa3f94e1fcf50194c84912e7a9 100644 --- a/testcases/repair/Compiler/Compiler.scala +++ b/testcases/repair/Compiler/Compiler.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,7 +141,7 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { @@ -160,7 +160,7 @@ object Desugar { sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -174,10 +174,10 @@ object Desugar { object Evaluator { import Trees._ - def bToi(b: Boolean) = if (b) 1 else 0 - def iTob(i: Int) = i == 1 + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 - def eval(e: Expr): Int = { + def eval(e: Expr): BigInt = { e match { case Plus(lhs, rhs) => eval(lhs) + eval(rhs) case Minus(lhs, rhs) => eval(lhs) + eval(rhs) diff --git a/testcases/repair/Compiler/Compiler1.scala b/testcases/repair/Compiler/Compiler1.scala index 2971855c71b9b765f457886ade24c313595a60f3..f3e840442bdb3936cc167baa9a29b3937d616d71 100644 --- a/testcases/repair/Compiler/Compiler1.scala +++ b/testcases/repair/Compiler/Compiler1.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,11 +141,11 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { - case Trees.Plus (lhs, rhs) => Neg(desugar(lhs)) // FIXME: Complete nonsense. Leon can't disprove it, uses example... + case Trees.Plus (lhs, rhs) => Neg(desugar(lhs)) // FIXME case Trees.Minus(lhs, rhs) => Plus(desugar(lhs), Neg(desugar(rhs))) case Trees.LessThan(lhs, rhs) => LessThan(desugar(lhs), desugar(rhs)) case Trees.And (lhs, rhs) => Ite(desugar(lhs), desugar(rhs), Literal(0)) @@ -156,15 +156,11 @@ object Desugar { case Trees.Ite(cond, thn, els) => Ite(desugar(cond), desugar(thn), desugar(els)) case Trees.IntLiteral(v) => Literal(v) case Trees.BoolLiteral(b) => Literal(b2i(b)) - }} ensuring { res => - ((e, res) passes { - case Trees.Plus(Trees.IntLiteral(i), Trees.Minus(Trees.IntLiteral(j), Trees.IntLiteral(42))) => - Plus(Literal(i), Plus(Literal(j), Neg(Literal(42)))) - }) && + }} ensuring { res => sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -174,3 +170,43 @@ object Desugar { } } + +object Evaluator { + import Trees._ + + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 + + def eval(e: Expr): BigInt = { + e match { + case Plus(lhs, rhs) => eval(lhs) + eval(rhs) + case Minus(lhs, rhs) => eval(lhs) + eval(rhs) + case LessThan(lhs, rhs) => bToi(eval(lhs) < eval(rhs)) + case And(lhs, rhs) => bToi(iTob(eval(lhs)) && iTob(eval(rhs))) + case Or(lhs, rhs) => bToi(iTob(eval(lhs)) || iTob(eval(rhs))) + case Not(e) => bToi(!iTob(eval(e))) + case Eq(lhs, rhs) => bToi(eval(lhs) == eval(rhs)) + case Ite(cond, thn, els) => if (iTob(eval(cond))) eval(thn) else eval(els) + case IntLiteral(v) => v + case BoolLiteral(b) => bToi(b) + } + } +} + +object Simplifier { + import Trees._ + import Evaluator._ + + @induct + def simplify(e: Expr): Expr = { + e match { + case And(BoolLiteral(false), _) => BoolLiteral(false) + case Or(BoolLiteral(true), _) => BoolLiteral(true) + case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a+b) + case Not(Not(Not(a))) => Not(a) + case e => e + } + } ensuring { + res => eval(res) == eval(e) + } +} diff --git a/testcases/repair/Compiler/Compiler2.scala b/testcases/repair/Compiler/Compiler2.scala index a068dadcba0e35683147b8141bb53e38eb01f7a4..6703cf715953f44bb4c0a9a0b2f4222d7a6df7a8 100644 --- a/testcases/repair/Compiler/Compiler2.scala +++ b/testcases/repair/Compiler/Compiler2.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,12 +141,12 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { case Trees.Plus (lhs, rhs) => Plus(desugar(lhs), desugar(rhs)) - case Trees.Minus(lhs, rhs) => Literal(0)//Plus(desugar(lhs), desugar(rhs)) // FIXME forgot Neg + case Trees.Minus(lhs, rhs) => Literal(0)// FIXME: Plus(desugar(lhs), Neg(desugar(rhs))) case Trees.LessThan(lhs, rhs) => LessThan(desugar(lhs), desugar(rhs)) case Trees.And (lhs, rhs) => Ite(desugar(lhs), desugar(rhs), Literal(0)) case Trees.Or (lhs, rhs) => Ite(desugar(lhs), Literal(1), desugar(rhs)) @@ -157,13 +157,10 @@ object Desugar { case Trees.IntLiteral(v) => Literal(v) case Trees.BoolLiteral(b) => Literal(b2i(b)) }} ensuring { res => - sem(res) == Semantics.semUntyped(e) && ((e,res) passes { - case Trees.Minus(Trees.IntLiteral(42), Trees.IntLiteral(i)) => - Plus(Literal(42), Neg(Literal(i))) - }) + sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -173,3 +170,43 @@ object Desugar { } } + +object Evaluator { + import Trees._ + + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 + + def eval(e: Expr): BigInt = { + e match { + case Plus(lhs, rhs) => eval(lhs) + eval(rhs) + case Minus(lhs, rhs) => eval(lhs) + eval(rhs) + case LessThan(lhs, rhs) => bToi(eval(lhs) < eval(rhs)) + case And(lhs, rhs) => bToi(iTob(eval(lhs)) && iTob(eval(rhs))) + case Or(lhs, rhs) => bToi(iTob(eval(lhs)) || iTob(eval(rhs))) + case Not(e) => bToi(!iTob(eval(e))) + case Eq(lhs, rhs) => bToi(eval(lhs) == eval(rhs)) + case Ite(cond, thn, els) => if (iTob(eval(cond))) eval(thn) else eval(els) + case IntLiteral(v) => v + case BoolLiteral(b) => bToi(b) + } + } +} + +object Simplifier { + import Trees._ + import Evaluator._ + + @induct + def simplify(e: Expr): Expr = { + e match { + case And(BoolLiteral(false), _) => BoolLiteral(false) + case Or(BoolLiteral(true), _) => BoolLiteral(true) + case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a+b) + case Not(Not(Not(a))) => Not(a) + case e => e + } + } ensuring { + res => eval(res) == eval(e) + } +} diff --git a/testcases/repair/Compiler/Compiler3.scala b/testcases/repair/Compiler/Compiler3.scala index ec61a78a5fb3e5a16a344daf979079f3df78b205..24937f81a069ea5e7480b4d088e6db0190eab3ce 100644 --- a/testcases/repair/Compiler/Compiler3.scala +++ b/testcases/repair/Compiler/Compiler3.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,7 +141,7 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { @@ -153,15 +153,14 @@ object Desugar { case Trees.Not(e) => Ite(desugar(e), Literal(0), Literal(1)) case Trees.Eq(lhs, rhs) => Eq(desugar(lhs), desugar(rhs)) - // FIXME switched the branches - case Trees.Ite(cond, thn, els) => Ite(desugar(cond), desugar(els), desugar(thn)) + case Trees.Ite(cond, thn, els) => Ite(desugar(cond), desugar(els), desugar(thn)) // FIXME case Trees.IntLiteral(v) => Literal(v) case Trees.BoolLiteral(b) => Literal(b2i(b)) }} ensuring { res => sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -171,3 +170,43 @@ object Desugar { } } + +object Evaluator { + import Trees._ + + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 + + def eval(e: Expr): BigInt = { + e match { + case Plus(lhs, rhs) => eval(lhs) + eval(rhs) + case Minus(lhs, rhs) => eval(lhs) + eval(rhs) + case LessThan(lhs, rhs) => bToi(eval(lhs) < eval(rhs)) + case And(lhs, rhs) => bToi(iTob(eval(lhs)) && iTob(eval(rhs))) + case Or(lhs, rhs) => bToi(iTob(eval(lhs)) || iTob(eval(rhs))) + case Not(e) => bToi(!iTob(eval(e))) + case Eq(lhs, rhs) => bToi(eval(lhs) == eval(rhs)) + case Ite(cond, thn, els) => if (iTob(eval(cond))) eval(thn) else eval(els) + case IntLiteral(v) => v + case BoolLiteral(b) => bToi(b) + } + } +} + +object Simplifier { + import Trees._ + import Evaluator._ + + @induct + def simplify(e: Expr): Expr = { + e match { + case And(BoolLiteral(false), _) => BoolLiteral(false) + case Or(BoolLiteral(true), _) => BoolLiteral(true) + case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a+b) + case Not(Not(Not(a))) => Not(a) + case e => e + } + } ensuring { + res => eval(res) == eval(e) + } +} diff --git a/testcases/repair/Compiler/Compiler4.scala b/testcases/repair/Compiler/Compiler4.scala index 9f6ac963a2d04e47b66e4eadb172dc19abb4094a..33f24376efc4240a32c6428ae7a2d6db5949b851 100644 --- a/testcases/repair/Compiler/Compiler4.scala +++ b/testcases/repair/Compiler/Compiler4.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,7 +141,7 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { @@ -150,7 +150,7 @@ object Desugar { case Trees.LessThan(lhs, rhs) => LessThan(desugar(lhs), desugar(rhs)) case Trees.And (lhs, rhs) => Ite(desugar(lhs), desugar(rhs), Literal(0)) case Trees.Or (lhs, rhs) => Ite(desugar(lhs), Literal(1), desugar(rhs)) - case Trees.Not(e) => Ite(desugar(e), Literal(1), Literal(1)) // FIXME else should be 0 + case Trees.Not(e) => Ite(desugar(e), Literal(1), Literal(1)) // FIMXE should be 0 case Trees.Eq(lhs, rhs) => Eq(desugar(lhs), desugar(rhs)) case Trees.Ite(cond, thn, els) => Ite(desugar(cond), desugar(thn), desugar(els)) @@ -160,7 +160,7 @@ object Desugar { sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -170,3 +170,43 @@ object Desugar { } } + +object Evaluator { + import Trees._ + + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 + + def eval(e: Expr): BigInt = { + e match { + case Plus(lhs, rhs) => eval(lhs) + eval(rhs) + case Minus(lhs, rhs) => eval(lhs) + eval(rhs) + case LessThan(lhs, rhs) => bToi(eval(lhs) < eval(rhs)) + case And(lhs, rhs) => bToi(iTob(eval(lhs)) && iTob(eval(rhs))) + case Or(lhs, rhs) => bToi(iTob(eval(lhs)) || iTob(eval(rhs))) + case Not(e) => bToi(!iTob(eval(e))) + case Eq(lhs, rhs) => bToi(eval(lhs) == eval(rhs)) + case Ite(cond, thn, els) => if (iTob(eval(cond))) eval(thn) else eval(els) + case IntLiteral(v) => v + case BoolLiteral(b) => bToi(b) + } + } +} + +object Simplifier { + import Trees._ + import Evaluator._ + + @induct + def simplify(e: Expr): Expr = { + e match { + case And(BoolLiteral(false), _) => BoolLiteral(false) + case Or(BoolLiteral(true), _) => BoolLiteral(true) + case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a+b) + case Not(Not(Not(a))) => Not(a) + case e => e + } + } ensuring { + res => eval(res) == eval(e) + } +} diff --git a/testcases/repair/Compiler/Compiler5.scala b/testcases/repair/Compiler/Compiler5.scala index 3a605de3d6fee6d135da43793f440c0ac10f37f7..5ce70621262330906b9182d526994e8e6953bdef 100644 --- a/testcases/repair/Compiler/Compiler5.scala +++ b/testcases/repair/Compiler/Compiler5.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,7 +141,7 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { @@ -150,17 +150,17 @@ object Desugar { case Trees.LessThan(lhs, rhs) => LessThan(desugar(lhs), desugar(rhs)) case Trees.And (lhs, rhs) => Ite(desugar(lhs), desugar(rhs), Literal(0)) case Trees.Or (lhs, rhs) => Ite(desugar(lhs), Literal(1), desugar(rhs)) - case Trees.Not(e) => Ite(desugar(e), Literal(1), Literal(1)) //FIXME + case Trees.Not(e) => Ite(desugar(e), Literal(1), Literal(1)) // FIMXE case Trees.Eq(lhs, rhs) => Eq(desugar(lhs), desugar(rhs)) - case Trees.Ite(cond, thn, els) => Ite(desugar(cond), desugar(els), desugar(thn))//FIXME + case Trees.Ite(cond, thn, els) => Ite(desugar(cond), desugar(els), desugar(thn)) // FIXME case Trees.IntLiteral(v) => Literal(v) case Trees.BoolLiteral(b) => Literal(b2i(b)) }} ensuring { res => sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -174,10 +174,10 @@ object Desugar { object Evaluator { import Trees._ - def bToi(b: Boolean) = if (b) 1 else 0 - def iTob(i: Int) = i == 1 + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 - def eval(e: Expr): Int = { + def eval(e: Expr): BigInt = { e match { case Plus(lhs, rhs) => eval(lhs) + eval(rhs) case Minus(lhs, rhs) => eval(lhs) + eval(rhs) diff --git a/testcases/repair/Compiler/Compiler6.scala b/testcases/repair/Compiler/Compiler6.scala index 95003d1afe6e56bdf4ab42dbff6c24227f41abc2..0848603a1428bee0aa58d4152bf5779c2d70d9ce 100644 --- a/testcases/repair/Compiler/Compiler6.scala +++ b/testcases/repair/Compiler/Compiler6.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,7 +141,7 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { @@ -160,7 +160,7 @@ object Desugar { sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -174,10 +174,10 @@ object Desugar { object Evaluator { import Trees._ - def bToi(b: Boolean) = if (b) 1 else 0 - def iTob(i: Int) = i == 1 + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 - def eval(e: Expr): Int = { + def eval(e: Expr): BigInt = { e match { case Plus(lhs, rhs) => eval(lhs) + eval(rhs) case Minus(lhs, rhs) => eval(lhs) + eval(rhs) @@ -202,7 +202,7 @@ object Simplifier { e match { case And(BoolLiteral(false), _) => BoolLiteral(false) case Or(BoolLiteral(true), _) => BoolLiteral(true) - case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a-b) + case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a-b) // FIXME case Not(Not(Not(a))) => Not(a) case e => e } diff --git a/testcases/repair/Compiler/Compiler7.scala b/testcases/repair/Compiler/Compiler7.scala index 0e6364fc53ec7fd0e7f65c803980689ef310b600..890f42f6ec453ef125142bb94ef2d5a62972765b 100644 --- a/testcases/repair/Compiler/Compiler7.scala +++ b/testcases/repair/Compiler/Compiler7.scala @@ -13,7 +13,7 @@ object Trees { case class Not(e : Expr) extends Expr case class Eq(lhs: Expr, rhs: Expr) extends Expr case class Ite(cond: Expr, thn: Expr, els: Expr) extends Expr - case class IntLiteral(v: Int) extends Expr + case class IntLiteral(v: BigInt) extends Expr case class BoolLiteral(b : Boolean) extends Expr } @@ -74,7 +74,7 @@ object Semantics { import Types._ import TypeChecker._ - def semI(t : Expr) : Int = { + def semI(t : Expr) : BigInt = { require( typeOf(t) == ( Some(IntType) : Option[Type] )) t match { case Plus(lhs , rhs) => semI(lhs) + semI(rhs) @@ -102,10 +102,10 @@ object Semantics { } } - def b2i(b : Boolean) = if (b) 1 else 0 + def b2i(b : Boolean): BigInt = if (b) 1 else 0 @induct - def semUntyped( t : Expr) : Int = { t match { + def semUntyped( t : Expr) : BigInt = { t match { case Plus (lhs, rhs) => semUntyped(lhs) + semUntyped(rhs) case Minus(lhs, rhs) => semUntyped(lhs) - semUntyped(rhs) case And (lhs, rhs) => if (semUntyped(lhs)!=0) semUntyped(rhs) else 0 @@ -141,7 +141,7 @@ object Desugar { case class Ite(cond : SimpleE, thn : SimpleE, els : SimpleE) extends SimpleE case class Eq(lhs : SimpleE, rhs : SimpleE) extends SimpleE case class LessThan(lhs : SimpleE, rhs : SimpleE) extends SimpleE - case class Literal(i : Int) extends SimpleE + case class Literal(i : BigInt) extends SimpleE @induct def desugar(e : Trees.Expr) : SimpleE = { e match { @@ -160,7 +160,7 @@ object Desugar { sem(res) == Semantics.semUntyped(e) } - def sem(e : SimpleE) : Int = e match { + def sem(e : SimpleE) : BigInt = e match { case Plus (lhs, rhs) => sem(lhs) + sem(rhs) case Ite(cond, thn, els) => if (sem(cond) != 0) sem(thn) else sem(els) case Neg(arg) => -sem(arg) @@ -174,10 +174,10 @@ object Desugar { object Evaluator { import Trees._ - def bToi(b: Boolean) = if (b) 1 else 0 - def iTob(i: Int) = i == 1 + def bToi(b: Boolean): BigInt = if (b) 1 else 0 + def iTob(i: BigInt) = i == 1 - def eval(e: Expr): Int = { + def eval(e: Expr): BigInt = { e match { case Plus(lhs, rhs) => eval(lhs) + eval(rhs) case Minus(lhs, rhs) => eval(lhs) + eval(rhs) @@ -201,14 +201,12 @@ object Simplifier { def simplify(e: Expr): Expr = { e match { case And(BoolLiteral(false), _) => BoolLiteral(false) - case Or(BoolLiteral(true), _) => BoolLiteral(false) + case Or(BoolLiteral(true), _) => BoolLiteral(false) // FIMXE case Plus(IntLiteral(a), IntLiteral(b)) => IntLiteral(a+b) case Not(Not(Not(a))) => Not(a) case e => e } } ensuring { - res => eval(res) == eval(e) && ((e, res) passes { - case Or(BoolLiteral(true), e) => BoolLiteral(true) - }) + res => eval(res) == eval(e) } } diff --git a/testcases/repair/Heap/Heap.scala b/testcases/repair/Heap/Heap.scala index 547a11456a7814abaab17661b4948d4723bc50ef..c7df65b60c1b123a8d0d5475f4a52cd4d8b4be04 100644 --- a/testcases/repair/Heap/Heap.scala +++ b/testcases/repair/Heap/Heap.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigBigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigBigInt] = this match { + case Leaf() => Set[BigBigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigBigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigBigInt, i2 : BigBigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigBigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigBigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigBigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigBigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap1.scala b/testcases/repair/Heap/Heap1.scala index 39c1ee2048c56b5946b8e6ce116b5702e8e6c927..bb62940303195930d7b6ddb3020c2ead2dd1b9c0 100644 --- a/testcases/repair/Heap/Heap1.scala +++ b/testcases/repair/Heap/Heap1.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -60,7 +60,7 @@ object Heaps { case (Leaf(), _) => h2 case (_, Leaf()) => h1 case (Node(v1, l1, r1), Node(v2, l2, r2)) => - //if(v1 >= v2) FIXME forgot this condition + //if(v1 >= v2) // FIXME makeN(v1, l1, merge(r1, h2)) //else // makeN(v2, l2, merge(h1, r2)) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap10.scala b/testcases/repair/Heap/Heap10.scala index 25e966e41c645e0e7d690eeecad41da54d6c9eff..bba2389a395a05b0b093e0e01d504eb17a3fe67e 100644 --- a/testcases/repair/Heap/Heap10.scala +++ b/testcases/repair/Heap/Heap10.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -57,7 +57,7 @@ object Heaps { hasHeapProperty(h1) && hasHeapProperty(h2) ) (h1,h2) match { - case (Leaf(), _) => h1 // FIXME: swapped h1 and h2 between the cases + case (Leaf(), _) => h1 // FIXME: swapped these cases case (_, Leaf()) => h2 // FIXME case (Node(v1, l1, r1), Node(v2, l2, r2)) => if(v1 >= v2) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap2.scala b/testcases/repair/Heap/Heap2.scala index c97a9dd52c98a16f15c60bce77e2016c8b1c1044..75f53a9639195f6568568346595326aa8033dbd4 100644 --- a/testcases/repair/Heap/Heap2.scala +++ b/testcases/repair/Heap/Heap2.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -61,9 +61,9 @@ object Heaps { case (_, Leaf()) => h1 case (Node(v1, l1, r1), Node(v2, l2, r2)) => if(v1 >= v2) - Node(v1, l1, merge(r1, h2)) // FIXME forgot to use makeN + Node(v1, l1, merge(r1, h2)) // FIXME should use makeN else - makeN(v2, l2, merge(h1, r2)) // The same + Node(v2, l2, merge(h1, r2)) // FIXME here also } } ensuring { res => hasLeftistProperty(res) && hasHeapProperty(res) && @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -79,9 +79,10 @@ object Heaps { Node(value, left, right) else Node(value, right, left) - } ensuring { hasLeftistProperty(_) } + } ensuring { res => + hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -92,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap3.scala b/testcases/repair/Heap/Heap3.scala index 71445ba971b5f0a8711dfcd07c4d7d15339d1a47..b24024ea79607140b0edb47972878faf936b4dd8 100644 --- a/testcases/repair/Heap/Heap3.scala +++ b/testcases/repair/Heap/Heap3.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap4.scala b/testcases/repair/Heap/Heap4.scala index 51cdb228b7908393db9fdeeb08e8eadd7003d474..d277fde61f487b8614abd9dbc4c0910d9f289520 100644 --- a/testcases/repair/Heap/Heap4.scala +++ b/testcases/repair/Heap/Heap4.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -58,7 +58,7 @@ object Heaps { ) (h1,h2) match { case (Leaf(), _) => h2 - case (_, Leaf()) => h2 // FIXME h2 instead of h1 + case (_, Leaf()) => h2 // FIXME should be h1 case (Node(v1, l1, r1), Node(v2, l2, r2)) => if(v1 >= v2) makeN(v1, l1, merge(r1, h2)) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap5.scala b/testcases/repair/Heap/Heap5.scala index bc1f1678041cbe5155c7359e30fe98ca1e0eee6d..fcddb9e089cb94b89cd2c5a2bf2f34e5bc13e3bc 100644 --- a/testcases/repair/Heap/Heap5.scala +++ b/testcases/repair/Heap/Heap5.scala @@ -8,22 +8,22 @@ import leon.lang._ import leon.collection._ object Heaps { - + sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap6.scala b/testcases/repair/Heap/Heap6.scala index 2fe845f164cffbe3ca040736d0e28b6fb7ee0ac5..aa9eb7230c57231a62f26bcf68da999eb13aeac7 100644 --- a/testcases/repair/Heap/Heap6.scala +++ b/testcases/repair/Heap/Heap6.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,14 +46,14 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) private def merge(h1: Heap, h2: Heap) : Heap = { require( - hasLeftistProperty(h1) && hasLeftistProperty(h2) && + hasLeftistProperty(h1) && hasLeftistProperty(h2) && hasHeapProperty(h1) && hasHeapProperty(h2) ) (h1,h2) match { @@ -63,15 +63,15 @@ object Heaps { if(v1 >= v2) makeN(v1, l1, merge(r1, h2)) else - makeN(v2, l1, merge(h1, r2)) // Fixme: l1 instead of l2 + makeN(v2, l1, merge(h1, r2)) // FIXME: l1 instead of l2 } - } ensuring { res => + } ensuring { res => hasLeftistProperty(res) && hasHeapProperty(res) && heapSize(h1) + heapSize(h2) == heapSize(res) && - h1.content ++ h2.content == res.content + h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap7.scala b/testcases/repair/Heap/Heap7.scala index 3fdc9060847939d8069d7e7f3434c60d31bb4dfd..91baf66c4dd55542e5588eafd00805f0d2a7a4e9 100644 --- a/testcases/repair/Heap/Heap7.scala +++ b/testcases/repair/Heap/Heap7.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -60,7 +60,7 @@ object Heaps { case (Leaf(), _) => h2 case (_, Leaf()) => h1 case (Node(v1, l1, r1), Node(v2, l2, r2)) => - if(v1 + v2 > 0) // FIXME: Nonsense, should be v1 >= v2 + if(v1 + v2 > 0) // FIXME Totally wrong makeN(v1, l1, merge(r1, h2)) else makeN(v2, l2, merge(h1, r2)) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,7 +82,7 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap8.scala b/testcases/repair/Heap/Heap8.scala index 4816b219f9b1f6a1e6f3839b5f566c74136d1651..0d21fc36842a7ecf5fd6983ae7453fc072efa2dc 100644 --- a/testcases/repair/Heap/Heap8.scala +++ b/testcases/repair/Heap/Heap8.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -71,7 +71,7 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) @@ -82,10 +82,10 @@ object Heaps { } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) - merge(Node(element + 1, Leaf(), Leaf()), heap) // FIXME lol. + merge(Node(element + 1, Leaf(), Leaf()), heap) // FIXME: unneeded +1 } ensuring { res => hasLeftistProperty(res) && hasHeapProperty(res) && @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/Heap/Heap9.scala b/testcases/repair/Heap/Heap9.scala index 649f67a3f0189d710d0500aac7c4e86ffbfc82d7..0a9197a34f46e3f7fed010fa5ea3cd4e44389d93 100644 --- a/testcases/repair/Heap/Heap9.scala +++ b/testcases/repair/Heap/Heap9.scala @@ -10,20 +10,20 @@ import leon.collection._ object Heaps { sealed abstract class Heap { - val rank : Int = this match { + val rank : BigInt = this match { case Leaf() => 0 case Node(_, l, r) => 1 + max(l.rank, r.rank) } - def content : Set[Int] = this match { - case Leaf() => Set[Int]() + def content : Set[BigInt] = this match { + case Leaf() => Set[BigInt]() case Node(v,l,r) => l.content ++ Set(v) ++ r.content } } case class Leaf() extends Heap - case class Node(value:Int, left: Heap, right: Heap) extends Heap + case class Node(value:BigInt, left: Heap, right: Heap) extends Heap - def max(i1 : Int, i2 : Int) = if (i1 >= i2) i1 else i2 + def max(i1 : BigInt, i2 : BigInt) = if (i1 >= i2) i1 else i2 def hasHeapProperty(h : Heap) : Boolean = h match { case Leaf() => true @@ -46,7 +46,7 @@ object Heaps { l.rank >= r.rank } - def heapSize(t: Heap): Int = { t match { + def heapSize(t: Heap): BigInt = { t match { case Leaf() => 0 case Node(v, l, r) => heapSize(l) + 1 + heapSize(r) }} ensuring(_ >= 0) @@ -71,18 +71,18 @@ object Heaps { h1.content ++ h2.content == res.content } - private def makeN(value: Int, left: Heap, right: Heap) : Heap = { + private def makeN(value: BigInt, left: Heap, right: Heap) : Heap = { require( hasLeftistProperty(left) && hasLeftistProperty(right) ) - if(left.rank >= right.rank + 42) // FIXME delete +42 + if(left.rank >= right.rank + 42) // FIXME unneeded constant Node(value, left, right) else Node(value, right, left) } ensuring { res => hasLeftistProperty(res) } - def insert(element: Int, heap: Heap) : Heap = { + def insert(element: BigInt, heap: Heap) : Heap = { require(hasLeftistProperty(heap) && hasHeapProperty(heap)) merge(Node(element, Leaf(), Leaf()), heap) @@ -93,7 +93,7 @@ object Heaps { res.content == heap.content ++ Set(element) } - def findMax(h: Heap) : Option[Int] = { + def findMax(h: Heap) : Option[BigInt] = { h match { case Node(m,_,_) => Some(m) case Leaf() => None() diff --git a/testcases/repair/List/List.scala b/testcases/repair/List/List.scala index caa4aa0d34678b0e282b2694191cc640cd466fc3..3f1e6581480d4cc1f9eb5e6009823992706c4b77 100644 --- a/testcases/repair/List/List.scala +++ b/testcases/repair/List/List.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,135 +52,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -191,96 +191,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -290,127 +290,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List1.scala b/testcases/repair/List/List1.scala index 9f600f7282660ccf581aa0050e62e3bcb3aac622..ffa4f480e841a4d2a05fc023bc7fb8536f26f694 100644 --- a/testcases/repair/List/List1.scala +++ b/testcases/repair/List/List1.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,135 +52,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s-1, e)) // FIXME should be s + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s-1, e)) // FIXME should be s }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -191,96 +191,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -290,127 +290,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List10.scala b/testcases/repair/List/List10.scala index 1c4b0016aead23b3ce26f1c018385e8812f396f6..edef652eb6ca560b6ee56e2a6d7ce714c2f75e2f 100644 --- a/testcases/repair/List/List10.scala +++ b/testcases/repair/List/List10.scala @@ -7,46 +7,46 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 3 + t.size // FIXME 1+t.size - }) ensuring {(this, _) passes { - case Cons0(_, Nil0()) => 1 - case Nil0() => 0 +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 3 + t.size //FIXME + }) ensuring { (this, _) passes { + case Cons(_, Nil()) => 1 + case Nil() => 0 }} def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -55,135 +55,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -194,96 +194,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -293,127 +293,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List11.scala b/testcases/repair/List/List11.scala index 7fe816b465e70afe6522ef75754c274f12c22784..12eb4c6a1f027ffe8f1fd2df35944ea08bd36d79 100644 --- a/testcases/repair/List/List11.scala +++ b/testcases/repair/List/List11.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,135 +52,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -191,96 +191,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -290,135 +290,136 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } - def sum(l : List0[Int]) : Int = { l match { - case Nil0() => 0 - case Cons0(h, t) => 1 + sum(t) // FIXME h + sum(t) - }} ensuring { (l, _) passes { - case Cons0(a, Nil0()) => a - case Cons0(a, Cons0(b, Nil0())) => a + b + def sum(l: List[BigInt]): BigInt = { l match { + case Nil() => 0 + case Cons(x, xs) => 1 + sum(xs) // FIXME + }} ensuring { (l, _) passes { + case Cons(a, Nil()) => a + case Cons(a, Cons(b, Nil())) => a + b }} + } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List12.scala b/testcases/repair/List/List12.scala index 19f2d779e227d5b2daa52e3fc77b1310583ade23..1774d1efd0b33861cf39bec63aad9eeb83cb5ad1 100644 --- a/testcases/repair/List/List12.scala +++ b/testcases/repair/List/List12.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,139 +52,136 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = { - this match { - case Cons0(h, t) => - if (e == h) { - t // FIXME - } else { - Cons0(h, t - e) - } - case Nil0() => - Nil0() - } - } ensuring { - res => res.content == this.content -- Set(e) - } + def -(e: T): List[T] = { this match { + case Cons(h, t) => + if (e == h) { + t // FIXME missing rec. call + } else { + Cons(h, t - e) + } + case Nil() => + Nil() + }} ensuring { _.content == this.content -- Set(e) } + - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -195,96 +192,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -294,127 +291,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List13.scala b/testcases/repair/List/List13.scala index b585f3ef1027c4e69c55495b3b904e485079f044..96dc9abcc01bce1e09f2493c34a1e1938e0ed17b 100644 --- a/testcases/repair/List/List13.scala +++ b/testcases/repair/List/List13.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,138 +52,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = { (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), 0) => - t // FIXME should be this - case (Cons0(_, t), i) => - t.drop(i) //FIXME should be i-1 - }} ensuring { res => ((this, i), res) passes { - case (Cons0(_, Nil0()), 42) => Nil0() - case (l@Cons0(_, _), 0) => l - case (Cons0(a, Cons0(b, Nil0())), 1) => Cons0(b, Nil0()) - case (Cons0(a, Cons0(b, Cons0(c, Nil0()))), 2) => Cons0(c, Nil0()) - }} - - def slice(from: Int, to: Int): List0[T] = { + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => + if (i == 0) { + Cons(h, t) + } else { + t.drop(i) // FIXME Should be -1 + } + } + + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -194,96 +191,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -293,127 +290,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List2.scala b/testcases/repair/List/List2.scala index f83d1d2223c21b29d1258b1f211e7886bb859b9a..e5c850130597cb271e6a690e262175b507d53460 100644 --- a/testcases/repair/List/List2.scala +++ b/testcases/repair/List/List2.scala @@ -7,49 +7,46 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => xs ++ that // FIXME did not Cons - }) ensuring { res => - res.content == this.content ++ that.content && - res.size == this.size + that.size/* && - (((this, that), res) passes { - case ( x@Cons0(x1, Cons0(x2, Nil0())), y) => x - })*/ + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => xs ++ that // FIXME forgot x + }) ensuring { res => + (res.content == this.content ++ that.content) && + (res.size == this.size + that.size) } def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -58,135 +55,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -197,96 +194,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -296,127 +293,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List3.scala b/testcases/repair/List/List3.scala index 1ad7ef0fd71e8aa8f0d43b31d13e94d261fb45c5..8fc99b7d260210bdb3c985808e13fb7505a11362 100644 --- a/testcases/repair/List/List3.scala +++ b/testcases/repair/List/List3.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,138 +52,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def ap(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Nil0() // FIXME did not add element - case Cons0(x, xs) => Cons0(x, xs ap (t)) + case Nil() => this // FIXME forgot t + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - - def :+(t:T) = this ap t - - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -194,101 +191,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - //case Cons0(h, t) => - // Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => - ((r.size < this.size) || (this.size == 0)) && - ((this, r) passes { - case Cons0(a, Cons0(b, Cons0(c, Nil0()))) => Cons0(a, Cons0(b, Nil0())) - }) - ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -298,127 +290,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List4.scala b/testcases/repair/List/List4.scala index 0b641cd8079f35a1430f0dd5b96a454f84a7f6d7..d48256fe02feaac9fb7fe36980f13f2d3b61cd83 100644 --- a/testcases/repair/List/List4.scala +++ b/testcases/repair/List/List4.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,135 +52,140 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = { (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => - t.drop(i-1) //FIXME missing if-split - }} ensuring { res => ((this, i), res) passes { - case (Cons0(_, Nil0()), 42) => Nil0() - case (l@Cons0(_, _), 0) => l - case (Cons0(a, Cons0(b, Nil0())), 1) => Cons0(b, Nil0()) + def drop(i: BigInt): List[T] = { (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => + // FIXME + //if (i == 0) { + // Cons(h, t) + //} else { + t.drop(i-1) + //} + }} ensuring { ((this, i), _) passes { + case (Cons(_, Nil()), BigInt(42)) => Nil() + case (l@Cons(_, _), BigInt(0)) => l + case (Cons(a, Cons(b, Nil())), BigInt(1)) => Cons(b, Nil()) }} - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -191,96 +196,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -290,127 +295,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List5.scala b/testcases/repair/List/List5.scala index 34ae1877d1eb502d426ea5046c2ad12fbecb4dc4..471bff80009fa36646058205a83f0c5a8647034b 100644 --- a/testcases/repair/List/List5.scala +++ b/testcases/repair/List/List5.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,135 +52,138 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = { this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = { this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) - Cons0(h, r) // FIXME + //if (h == from) { FIXME + // Cons(to, r) + //} else { + Cons(h, r) + //} }} ensuring { res => - (((this.content -- Set(from)) ++ - (if (this.content contains from) Set(to) else Set[T]())) == res.content) && - res.size == this.size + (((this.content -- Set(from)) ++ (if (this.content contains from) Set(to) else Set[T]())) == res.content) && + res.size == this.size } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -191,96 +194,96 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -290,127 +293,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List6.scala b/testcases/repair/List/List6.scala index 2e1297cb4b6f9f0655c1083de043d0efa2bc1e5b..f7881ff1ea941d683ef6e046f73e3e82c6eff2ab 100644 --- a/testcases/repair/List/List6.scala +++ b/testcases/repair/List/List6.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,135 +52,135 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { @@ -191,99 +191,100 @@ sealed abstract class List0[T] { } } - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = { this match { - case Cons0(h, t) => + def count(e: T): BigInt = { this match { + case Cons(h, t) => if (h == e) { - t.count(e) // FIXME +1 + t.count(e) // FIXME missing +1 } else { t.count(e) } - case Nil0() => + case Nil() => 0 }} ensuring {((this, e), _) passes { - case (Cons0(a, Cons0(b, Cons0(a1, Cons0(b2, Nil0())))), a2) if a == a1 && a == a2 && b != a2 && b2 != a2 => 2 - case (Cons0(a, Cons0(b, Nil0())), c) if a != c && b != c => 0 - }} + case (Cons(a, Cons(b, Cons(a1, Cons(b2, Nil())))), a2) if a == a1 && a == a2 && b != a2 && b2 != a2 => 2 + case (Cons(a, Cons(b, Nil())), c) if a != c && b != c => 0 + }} - def evenSplit: (List0[T], List0[T]) = { + + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -293,127 +294,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List7.scala b/testcases/repair/List/List7.scala index 069dd35d2e65b4387c07095f4c02a00e3741c4f0..a67f7e5c4616e742b256aaa2d9074fd3ff986158 100644 --- a/testcases/repair/List/List7.scala +++ b/testcases/repair/List/List7.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,141 +52,141 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = { this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = { this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { t.find(e) match { case None() => None() - case Some(i) => Some(i) // FIXME should be i+1 + case Some(i) => Some(i) // FIXME forgot +1 } } }} ensuring { res => @@ -197,97 +197,96 @@ sealed abstract class List0[T] { } } - - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -297,127 +296,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List8.scala b/testcases/repair/List/List8.scala index bf99c2e7532aef4b3864a6743ac33401d4b9839c..0204d4f52e2c803ee9907f483d9a1192c0dfd3b7 100644 --- a/testcases/repair/List/List8.scala +++ b/testcases/repair/List/List8.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,141 +52,141 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = { this match { - case Nil0() => None() - case Cons0(h, t) => + def find(e: T): Option[BigInt] = { this match { + case Nil() => None() + case Cons(h, t) => if (h == e) { Some(0) } else { t.find(e) match { case None() => None() - case Some(i) => Some(i+2) // FIXME should be i+1 + case Some(i) => Some(i+2) // FIXME +1 } } }} ensuring { res => @@ -197,97 +197,96 @@ sealed abstract class List0[T] { } } - - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -297,127 +296,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/List/List9.scala b/testcases/repair/List/List9.scala index 59d05b6c526e985341906128506e9b83d50e6ced..176cb595f29fcf632935dfd3f149ef874061103c 100644 --- a/testcases/repair/List/List9.scala +++ b/testcases/repair/List/List9.scala @@ -7,43 +7,43 @@ import leon.lang._ import leon.collection._ import leon.annotation._ -sealed abstract class List0[T] { - def size: Int = (this match { - case Nil0() => 0 - case Cons0(h, t) => 1 + t.size +sealed abstract class List[T] { + def size: BigInt = (this match { + case Nil() => 0 + case Cons(h, t) => 1 + t.size }) ensuring (_ >= 0) def content: Set[T] = this match { - case Nil0() => Set() - case Cons0(h, t) => Set(h) ++ t.content + case Nil() => Set() + case Cons(h, t) => Set(h) ++ t.content } def contains(v: T): Boolean = (this match { - case Cons0(h, t) if h == v => true - case Cons0(_, t) => t.contains(v) - case Nil0() => false + case Cons(h, t) if h == v => true + case Cons(_, t) => t.contains(v) + case Nil() => false }) ensuring { res => res == (content contains v) } - def ++(that: List0[T]): List0[T] = (this match { - case Nil0() => that - case Cons0(x, xs) => Cons0(x, xs ++ that) + def ++(that: List[T]): List[T] = (this match { + case Nil() => that + case Cons(x, xs) => Cons(x, xs ++ that) }) ensuring { res => (res.content == this.content ++ that.content) && (res.size == this.size + that.size)} def head: T = { - require(this != Nil0[T]()) + require(this != Nil[T]()) this match { - case Cons0(h, t) => h + case Cons(h, t) => h } } - def tail: List0[T] = { - require(this != Nil0[T]()) + def tail: List[T] = { + require(this != Nil[T]()) this match { - case Cons0(h, t) => t + case Cons(h, t) => t } } - def apply(index: Int): T = { + def apply(index: BigInt): T = { require(0 <= index && index < size) if (index == 0) { head @@ -52,141 +52,141 @@ sealed abstract class List0[T] { } } - def ::(t:T): List0[T] = Cons0(t, this) + def ::(t:T): List[T] = Cons(t, this) - def :+(t:T): List0[T] = { + def :+(t:T): List[T] = { this match { - case Nil0() => Cons0(t, this) - case Cons0(x, xs) => Cons0(x, xs :+ (t)) + case Nil() => Cons(t, this) + case Cons(x, xs) => Cons(x, xs :+ (t)) } } ensuring(res => (res.size == size + 1) && (res.content == content ++ Set(t))) - def reverse: List0[T] = { + def reverse: List[T] = { this match { - case Nil0() => this - case Cons0(x,xs) => xs.reverse :+ x + case Nil() => this + case Cons(x,xs) => xs.reverse :+ x } } ensuring (res => (res.size == size) && (res.content == content)) - def take(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def take(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Nil0() + Nil() } else { - Cons0(h, t.take(i-1)) + Cons(h, t.take(i-1)) } } - def drop(i: Int): List0[T] = (this, i) match { - case (Nil0(), _) => Nil0() - case (Cons0(h, t), i) => + def drop(i: BigInt): List[T] = (this, i) match { + case (Nil(), _) => Nil() + case (Cons(h, t), i) => if (i == 0) { - Cons0(h, t) + Cons(h, t) } else { t.drop(i-1) } } - def slice(from: Int, to: Int): List0[T] = { + def slice(from: BigInt, to: BigInt): List[T] = { require(from < to && to < size && from >= 0) drop(from).take(to-from) } - def replace(from: T, to: T): List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => + def replace(from: T, to: T): List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => val r = t.replace(from, to) if (h == from) { - Cons0(to, r) + Cons(to, r) } else { - Cons0(h, r) + Cons(h, r) } } - private def chunk0(s: Int, l: List0[T], acc: List0[T], res: List0[List0[T]], s0: Int): List0[List0[T]] = l match { - case Nil0() => + private def chunk0(s: BigInt, l: List[T], acc: List[T], res: List[List[T]], s0: BigInt): List[List[T]] = l match { + case Nil() => if (acc.size > 0) { res :+ acc } else { res } - case Cons0(h, t) => + case Cons(h, t) => if (s0 == 0) { - chunk0(s, l, Nil0(), res :+ acc, s) + chunk0(s, l, Nil(), res :+ acc, s) } else { chunk0(s, t, acc :+ h, res, s0-1) } } - def chunks(s: Int): List0[List0[T]] = { + def chunks(s: BigInt): List[List[T]] = { require(s > 0) - chunk0(s, this, Nil0(), Nil0(), s) + chunk0(s, this, Nil(), Nil(), s) } - def zip[B](that: List0[B]): List0[(T, B)] = (this, that) match { - case (Cons0(h1, t1), Cons0(h2, t2)) => - Cons0((h1, h2), t1.zip(t2)) + def zip[B](that: List[B]): List[(T, B)] = (this, that) match { + case (Cons(h1, t1), Cons(h2, t2)) => + Cons((h1, h2), t1.zip(t2)) case (_) => - Nil0() + Nil() } - def -(e: T): List0[T] = this match { - case Cons0(h, t) => + def -(e: T): List[T] = this match { + case Cons(h, t) => if (e == h) { t - e } else { - Cons0(h, t - e) + Cons(h, t - e) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def --(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def --(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { t -- that } else { - Cons0(h, t -- that) + Cons(h, t -- that) } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def &(that: List0[T]): List0[T] = this match { - case Cons0(h, t) => + def &(that: List[T]): List[T] = this match { + case Cons(h, t) => if (that.contains(h)) { - Cons0(h, t & that) + Cons(h, t & that) } else { t & that } - case Nil0() => - Nil0() + case Nil() => + Nil() } - def pad(s: Int, e: T): List0[T] = { (this, s) match { + def pad(s: BigInt, e: T): List[T] = { (this, s) match { case (_, s) if s <= 0 => this - case (Nil0(), s) => - Cons0(e, Nil0().pad(s-1, e)) - case (Cons0(h, t), s) => - Cons0(h, t.pad(s, e)) + case (Nil(), s) => + Cons(e, Nil().pad(s-1, e)) + case (Cons(h, t), s) => + Cons(h, t.pad(s, e)) }} ensuring { res => ((this,s,e), res) passes { - case (Cons0(a,Nil0()), 2, x) => Cons0(a, Cons0(x, Cons0(x, Nil0()))) + case (Cons(a,Nil()), BigInt(2), x) => Cons(a, Cons(x, Cons(x, Nil()))) } } - def find(e: T): Option[Int] = { this match { - case Nil0() => None() - case Cons0(h, t) => - if (h != e) { // FIXME should be == + def find(e: T): Option[BigInt] = { this match { + case Nil() => None() + case Cons(h, t) => + if (h != e) { // FIXME Some(0) } else { t.find(e) match { case None() => None() - case Some(i) => Some(i+1) + case Some(i) => Some(i + 1) } } }} ensuring { res => @@ -197,97 +197,96 @@ sealed abstract class List0[T] { } } - - def init: List0[T] = (this match { - case Cons0(h, Nil0()) => - Nil0[T]() - case Cons0(h, t) => - Cons0[T](h, t.init) - case Nil0() => - Nil0[T]() - }) ensuring ( (r: List0[T]) => ((r.size < this.size) || (this.size == 0)) ) + def init: List[T] = (this match { + case Cons(h, Nil()) => + Nil[T]() + case Cons(h, t) => + Cons[T](h, t.init) + case Nil() => + Nil[T]() + }) ensuring ( (r: List[T]) => ((r.size < this.size) || (this.size == 0)) ) def lastOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => t.lastOption.orElse(Some(h)) - case Nil0() => + case Nil() => None() } def firstOption: Option[T] = this match { - case Cons0(h, t) => + case Cons(h, t) => Some(h) - case Nil0() => + case Nil() => None() } - def unique: List0[T] = this match { - case Nil0() => Nil0() - case Cons0(h, t) => - Cons0(h, t.unique - h) + def unique: List[T] = this match { + case Nil() => Nil() + case Cons(h, t) => + Cons(h, t.unique - h) } - def splitAt(e: T): List0[List0[T]] = split(Cons0(e, Nil0())) + def splitAt(e: T): List[List[T]] = split(Cons(e, Nil())) - def split(seps: List0[T]): List0[List0[T]] = this match { - case Cons0(h, t) => + def split(seps: List[T]): List[List[T]] = this match { + case Cons(h, t) => if (seps.contains(h)) { - Cons0(Nil0(), t.split(seps)) + Cons(Nil(), t.split(seps)) } else { val r = t.split(seps) - Cons0(Cons0(h, r.head), r.tail) + Cons(Cons(h, r.head), r.tail) } - case Nil0() => - Cons0(Nil0(), Nil0()) + case Nil() => + Cons(Nil(), Nil()) } - def count(e: T): Int = this match { - case Cons0(h, t) => + def count(e: T): BigInt = this match { + case Cons(h, t) => if (h == e) { 1 + t.count(e) } else { t.count(e) } - case Nil0() => + case Nil() => 0 } - def evenSplit: (List0[T], List0[T]) = { + def evenSplit: (List[T], List[T]) = { val c = size/2 (take(c), drop(c)) } - def insertAt(pos: Int, l: List0[T]): List0[T] = { + def insertAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { insertAt(size + pos, l) } else if(pos == 0) { l ++ this } else { this match { - case Cons0(h, t) => - Cons0(h, t.insertAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.insertAt(pos-1, l)) + case Nil() => l } } } - def replaceAt(pos: Int, l: List0[T]): List0[T] = { + def replaceAt(pos: BigInt, l: List[T]): List[T] = { if(pos < 0) { replaceAt(size + pos, l) } else if(pos == 0) { l ++ this.drop(l.size) } else { this match { - case Cons0(h, t) => - Cons0(h, t.replaceAt(pos-1, l)) - case Nil0() => + case Cons(h, t) => + Cons(h, t.replaceAt(pos-1, l)) + case Nil() => l } } } - def rotate(s: Int): List0[T] = { + def rotate(s: BigInt): List[T] = { if (s < 0) { rotate(size+s) } else { @@ -297,127 +296,127 @@ sealed abstract class List0[T] { } def isEmpty = this match { - case Nil0() => true + case Nil() => true case _ => false } } @ignore -object List0 { - def apply[T](elems: T*): List0[T] = ??? +object List { + def apply[T](elems: T*): List[T] = ??? } @library -object List0Ops { - def flatten[T](ls: List0[List0[T]]): List0[T] = ls match { - case Cons0(h, t) => h ++ flatten(t) - case Nil0() => Nil0() +object ListOps { + def flatten[T](ls: List[List[T]]): List[T] = ls match { + case Cons(h, t) => h ++ flatten(t) + case Nil() => Nil() } - def isSorted(ls: List0[Int]): Boolean = ls match { - case Nil0() => true - case Cons0(_, Nil0()) => true - case Cons0(h1, Cons0(h2, _)) if(h1 > h2) => false - case Cons0(_, t) => isSorted(t) + def isSorted(ls: List[BigInt]): Boolean = ls match { + case Nil() => true + case Cons(_, Nil()) => true + case Cons(h1, Cons(h2, _)) if(h1 > h2) => false + case Cons(_, t) => isSorted(t) } - def sorted(ls: List0[Int]): List0[Int] = ls match { - case Cons0(h, t) => insSort(sorted(t), h) - case Nil0() => Nil0() + def sorted(ls: List[BigInt]): List[BigInt] = ls match { + case Cons(h, t) => insSort(sorted(t), h) + case Nil() => Nil() } - def insSort(ls: List0[Int], v: Int): List0[Int] = ls match { - case Nil0() => Cons0(v, Nil0()) - case Cons0(h, t) => + def insSort(ls: List[BigInt], v: BigInt): List[BigInt] = ls match { + case Nil() => Cons(v, Nil()) + case Cons(h, t) => if (v <= h) { - Cons0(v, t) + Cons(v, t) } else { - Cons0(h, insSort(t, v)) + Cons(h, insSort(t, v)) } } } -case class Cons0[T](h: T, t: List0[T]) extends List0[T] -case class Nil0[T]() extends List0[T] +case class Cons[T](h: T, t: List[T]) extends List[T] +case class Nil[T]() extends List[T] @library -object List0Specs { - def snocIndex[T](l : List0[T], t : T, i : Int) : Boolean = { +object ListSpecs { + def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { require(0 <= i && i < l.size + 1) // proof: (l match { - case Nil0() => true - case Cons0(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true + case Nil() => true + case Cons(x, xs) => if (i > 0) snocIndex[T](xs, t, i-1) else true }) && // claim: ((l :+ t).apply(i) == (if (i < l.size) l(i) else t)) }.holds - def reverseIndex[T](l : List0[T], i : Int) : Boolean = { + def reverseIndex[T](l : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l.size) (l match { - case Nil0() => true - case Cons0(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) + case Nil() => true + case Cons(x,xs) => snocIndex(l, x, i) && reverseIndex[T](l,i) }) && (l.reverse.apply(i) == l.apply(l.size - 1 - i)) }.holds - def appendIndex[T](l1 : List0[T], l2 : List0[T], i : Int) : Boolean = { + def appendIndex[T](l1 : List[T], l2 : List[T], i : BigInt) : Boolean = { require(0 <= i && i < l1.size + l2.size) (l1 match { - case Nil0() => true - case Cons0(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) + case Nil() => true + case Cons(x,xs) => if (i==0) true else appendIndex[T](xs,l2,i-1) }) && ((l1 ++ l2).apply(i) == (if (i < l1.size) l1(i) else l2(i - l1.size))) }.holds - def appendAssoc[T](l1 : List0[T], l2 : List0[T], l3 : List0[T]) : Boolean = { + def appendAssoc[T](l1 : List[T], l2 : List[T], l3 : List[T]) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => appendAssoc(xs,l2,l3) + case Nil() => true + case Cons(x,xs) => appendAssoc(xs,l2,l3) }) && (((l1 ++ l2) ++ l3) == (l1 ++ (l2 ++ l3))) }.holds - def snocIsAppend[T](l : List0[T], t : T) : Boolean = { + def snocIsAppend[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocIsAppend(xs,t) + case Nil() => true + case Cons(x,xs) => snocIsAppend(xs,t) }) && - ((l :+ t) == l ++ Cons0[T](t, Nil0())) + ((l :+ t) == l ++ Cons[T](t, Nil())) }.holds - def snocAfterAppend[T](l1 : List0[T], l2 : List0[T], t : T) : Boolean = { + def snocAfterAppend[T](l1 : List[T], l2 : List[T], t : T) : Boolean = { (l1 match { - case Nil0() => true - case Cons0(x,xs) => snocAfterAppend(xs,l2,t) + case Nil() => true + case Cons(x,xs) => snocAfterAppend(xs,l2,t) }) && ((l1 ++ l2) :+ t == (l1 ++ (l2 :+ t))) }.holds - def snocReverse[T](l : List0[T], t : T) : Boolean = { + def snocReverse[T](l : List[T], t : T) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => snocReverse(xs,t) + case Nil() => true + case Cons(x,xs) => snocReverse(xs,t) }) && - ((l :+ t).reverse == Cons0(t, l.reverse)) + ((l :+ t).reverse == Cons(t, l.reverse)) }.holds - def reverseReverse[T](l : List0[T]) : Boolean = { + def reverseReverse[T](l : List[T]) : Boolean = { (l match { - case Nil0() => true - case Cons0(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) + case Nil() => true + case Cons(x,xs) => reverseReverse[T](xs) && snocReverse[T](xs.reverse, x) }) && (l.reverse.reverse == l) }.holds //// my hand calculation shows this should work, but it does not seem to be found - //def reverseAppend[T](l1 : List0[T], l2 : List0[T]) : Boolean = { + //def reverseAppend[T](l1 : List[T], l2 : List[T]) : Boolean = { // (l1 match { - // case Nil0() => true - // case Cons0(x,xs) => { + // case Nil() => true + // case Cons(x,xs) => { // reverseAppend(xs,l2) && // snocAfterAppend[T](l2.reverse, xs.reverse, x) && // l1.reverse == (xs.reverse :+ x) diff --git a/testcases/repair/MergeSort/MergeSort.scala b/testcases/repair/MergeSort/MergeSort.scala index d4a0f89e9acacd601766461f4090ce84692e32f4..aac638b9a6d308dcf2872f2dadb8f3c85edeeea8 100644 --- a/testcases/repair/MergeSort/MergeSort.scala +++ b/testcases/repair/MergeSort/MergeSort.scala @@ -2,7 +2,7 @@ import leon.collection._ object MergeSort { - def split(l : List[Int]) : (List[Int],List[Int]) = { l match { + def split(l : List[BigInt]) : (List[BigInt],List[BigInt]) = { l match { case Cons(a, Cons(b, t)) => val (rec1, rec2) = split(t) (Cons(a, rec1), Cons(b, rec2)) @@ -15,12 +15,12 @@ object MergeSort { l1.content ++ l2.content == l.content } - def isSorted(l : List[Int]) : Boolean = l match { + def isSorted(l : List[BigInt]) : Boolean = l match { case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t) case _ => true } - def merge(l1 : List[Int], l2 : List[Int]) : List[Int] = { + def merge(l1 : List[BigInt], l2 : List[BigInt]) : List[BigInt] = { require(isSorted(l1) && isSorted(l2)) (l1, l2) match { case (Cons(h1, t1), Cons(h2,t2)) => @@ -37,7 +37,7 @@ object MergeSort { res.content == l1.content ++ l2.content } - def mergeSort(l : List[Int]) : List[Int] = { l match { + def mergeSort(l : List[BigInt]) : List[BigInt] = { l match { case Nil() => l case Cons(_, Nil()) => l case other => diff --git a/testcases/repair/MergeSort/MergeSort1.scala b/testcases/repair/MergeSort/MergeSort1.scala index 901d4c6a270f49a7902d2cc9e41eb27c93a96184..bef7f239105679afe84221f6b604e51bbd46b5e9 100644 --- a/testcases/repair/MergeSort/MergeSort1.scala +++ b/testcases/repair/MergeSort/MergeSort1.scala @@ -1,25 +1,11 @@ -import leon.lang._ +import leon.collection._ object MergeSort { - abstract class List { - def size : Int = this match { - case Nil() => 0 - case Cons(_, tl) => 1 + tl.size - } - def content : Set[Int] = this match { - case Nil() => Set.empty[Int] - case Cons(hd, tl) => tl.content ++ Set(hd) - } - } - - case class Nil() extends List - case class Cons(hd : Int, tl : List) extends List - - def split(l : List) : (List,List) = { l match { + def split(l : List[BigInt]) : (List[BigInt],List[BigInt]) = { l match { case Cons(a, Cons(b, t)) => val (rec1, rec2) = split(t) - (rec1, rec2) //FIXME forgot a and b + (rec1, rec2) // FIXME: Forgot a,b case other => (other, Nil()) }} ensuring { res => val (l1, l2) = res @@ -29,12 +15,12 @@ object MergeSort { l1.content ++ l2.content == l.content } - def isSorted(l : List) : Boolean = l match { + def isSorted(l : List[BigInt]) : Boolean = l match { case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t) case _ => true } - def merge(l1 : List, l2 : List) : List = { + def merge(l1 : List[BigInt], l2 : List[BigInt]) : List[BigInt] = { require(isSorted(l1) && isSorted(l2)) (l1, l2) match { case (Cons(h1, t1), Cons(h2,t2)) => @@ -51,7 +37,7 @@ object MergeSort { res.content == l1.content ++ l2.content } - def mergeSort(l : List) : List = { l match { + def mergeSort(l : List[BigInt]) : List[BigInt] = { l match { case Nil() => l case Cons(_, Nil()) => l case other => diff --git a/testcases/repair/MergeSort/MergeSort2.scala b/testcases/repair/MergeSort/MergeSort2.scala index d9966ecdc70e8200e318580999169059fb7fed98..11c3a1cdb2af326c103d48db512987a70346b862 100644 --- a/testcases/repair/MergeSort/MergeSort2.scala +++ b/testcases/repair/MergeSort/MergeSort2.scala @@ -2,7 +2,7 @@ import leon.collection._ object MergeSort { - def split(l : List[Int]) : (List[Int],List[Int]) = { l match { + def split(l : List[BigInt]) : (List[BigInt],List[BigInt]) = { l match { case Cons(a, Cons(b, t)) => val (rec1, rec2) = split(t) (Cons(a, rec1), Cons(b, rec2)) @@ -15,19 +15,19 @@ object MergeSort { l1.content ++ l2.content == l.content } - def isSorted(l : List[Int]) : Boolean = l match { + def isSorted(l : List[BigInt]) : Boolean = l match { case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t) case _ => true } - def merge(l1 : List[Int], l2 : List[Int]) : List[Int] = { + def merge(l1 : List[BigInt], l2 : List[BigInt]) : List[BigInt] = { require(isSorted(l1) && isSorted(l2)) (l1, l2) match { case (Cons(h1, t1), Cons(h2,t2)) => if (h1 <= h2) Cons(h1, merge(t1, l2)) else - Cons(h1, merge(l1, t2)) // FIXME h1 instead of h2 + Cons(h1, merge(l1, t2)) // FIXME: h1 -> h2 case (Nil(), _) => l2 case (_, Nil()) => l1 } @@ -37,7 +37,7 @@ object MergeSort { res.content == l1.content ++ l2.content } - def mergeSort(l : List[Int]) : List[Int] = { l match { + def mergeSort(l : List[BigInt]) : List[BigInt] = { l match { case Nil() => l case Cons(_, Nil()) => l case other => diff --git a/testcases/repair/MergeSort/MergeSort3.scala b/testcases/repair/MergeSort/MergeSort3.scala index 7da332e56aa1f7d6f4feb56f03c80813c6bd09ee..3d9c8192a0c4584bbc16c88055f8693e460a4609 100644 --- a/testcases/repair/MergeSort/MergeSort3.scala +++ b/testcases/repair/MergeSort/MergeSort3.scala @@ -2,7 +2,7 @@ import leon.collection._ object MergeSort { - def split(l : List[Int]) : (List[Int],List[Int]) = { l match { + def split(l : List[BigInt]) : (List[BigInt],List[BigInt]) = { l match { case Cons(a, Cons(b, t)) => val (rec1, rec2) = split(t) (Cons(a, rec1), Cons(b, rec2)) @@ -15,16 +15,16 @@ object MergeSort { l1.content ++ l2.content == l.content } - def isSorted(l : List[Int]) : Boolean = l match { + def isSorted(l : List[BigInt]) : Boolean = l match { case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t) case _ => true } - def merge(l1 : List[Int], l2 : List[Int]) : List[Int] = { + def merge(l1 : List[BigInt], l2 : List[BigInt]) : List[BigInt] = { require(isSorted(l1) && isSorted(l2)) (l1, l2) match { case (Cons(h1, t1), Cons(h2,t2)) => - if (h1 >= h2) // FIXME: should be <= + if (h1 >= h2) // FIXME Condition inverted Cons(h1, merge(t1, l2)) else Cons(h2, merge(l1, t2)) @@ -37,7 +37,7 @@ object MergeSort { res.content == l1.content ++ l2.content } - def mergeSort(l : List[Int]) : List[Int] = { l match { + def mergeSort(l : List[BigInt]) : List[BigInt] = { l match { case Nil() => l case Cons(_, Nil()) => l case other => diff --git a/testcases/repair/MergeSort/MergeSort4.scala b/testcases/repair/MergeSort/MergeSort4.scala index 93872082dce48f13acc28034ca6787f554170ba5..4422402b187f1e8fec33825369489f223af7e559 100644 --- a/testcases/repair/MergeSort/MergeSort4.scala +++ b/testcases/repair/MergeSort/MergeSort4.scala @@ -2,7 +2,7 @@ import leon.collection._ object MergeSort { - def split(l : List[Int]) : (List[Int],List[Int]) = { l match { + def split(l : List[BigInt]) : (List[BigInt],List[BigInt]) = { l match { case Cons(a, Cons(b, t)) => val (rec1, rec2) = split(t) (Cons(a, rec1), Cons(b, rec2)) @@ -15,19 +15,19 @@ object MergeSort { l1.content ++ l2.content == l.content } - def isSorted(l : List[Int]) : Boolean = l match { + def isSorted(l : List[BigInt]) : Boolean = l match { case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t) case _ => true } - def merge(l1 : List[Int], l2 : List[Int]) : List[Int] = { + def merge(l1 : List[BigInt], l2 : List[BigInt]) : List[BigInt] = { require(isSorted(l1) && isSorted(l2)) (l1, l2) match { case (Cons(h1, t1), Cons(h2,t2)) => if (h1 <= h2) Cons(h1, merge(t1, l2)) else - merge(l1, t2) // FIXME missing Cons(h2 + merge(l1, t2) // FIXME: missing h2 case (Nil(), _) => l2 case (_, Nil()) => l1 } @@ -37,7 +37,7 @@ object MergeSort { res.content == l1.content ++ l2.content } - def mergeSort(l : List[Int]) : List[Int] = { l match { + def mergeSort(l : List[BigInt]) : List[BigInt] = { l match { case Nil() => l case Cons(_, Nil()) => l case other => diff --git a/testcases/repair/MergeSort/MergeSort5.scala b/testcases/repair/MergeSort/MergeSort5.scala index a04a0f491eb8fe6ec1d85bdaf9280c3190d365f2..1bf9d50ce3279e5fd6b1876b3c51e4c3bd249fe4 100644 --- a/testcases/repair/MergeSort/MergeSort5.scala +++ b/testcases/repair/MergeSort/MergeSort5.scala @@ -2,7 +2,7 @@ import leon.collection._ object MergeSort { - def split(l : List[Int]) : (List[Int],List[Int]) = { l match { + def split(l : List[BigInt]) : (List[BigInt],List[BigInt]) = { l match { case Cons(a, Cons(b, t)) => val (rec1, rec2) = split(t) (Cons(a, rec1), Cons(b, rec2)) @@ -15,21 +15,21 @@ object MergeSort { l1.content ++ l2.content == l.content } - def isSorted(l : List[Int]) : Boolean = l match { + def isSorted(l : List[BigInt]) : Boolean = l match { case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t) case _ => true } - def merge(l1 : List[Int], l2 : List[Int]) : List[Int] = { + def merge(l1 : List[BigInt], l2 : List[BigInt]) : List[BigInt] = { require(isSorted(l1) && isSorted(l2)) (l1, l2) match { - case (Nil(), _) => l1 // FIXME should be l2 - case (_, Nil()) => l1 case (Cons(h1, t1), Cons(h2,t2)) => if (h1 <= h2) Cons(h1, merge(t1, l2)) else - Cons(h1, merge(l1, t2)) // FIXME should be h2 + Cons(h2, merge(l1, t2)) + case (Nil(), _) => l1 // FIXME should be l2 + case (_, Nil()) => l1 } } ensuring { res => isSorted(res) && @@ -37,7 +37,7 @@ object MergeSort { res.content == l1.content ++ l2.content } - def mergeSort(l : List[Int]) : List[Int] = { l match { + def mergeSort(l : List[BigInt]) : List[BigInt] = { l match { case Nil() => l case Cons(_, Nil()) => l case other => diff --git a/testcases/repair/MergeSort/repairs.last b/testcases/repair/MergeSort/repairs.last deleted file mode 100644 index cbb39c41a4c3152a77203d261b0a4bc1a94e2cf5..0000000000000000000000000000000000000000 --- a/testcases/repair/MergeSort/repairs.last +++ /dev/null @@ -1,4 +0,0 @@ - MergeSort, MergeSort1.scala, merge, 705, 20, 5, 1081, 1324, 37526, - MergeSort, MergeSort3.scala, merge, 705, 20, 14, 941, 1299, 47637, - MergeSort, MergeSort4.scala, mergeSort, 704, 21, 4, 965, 4481, 37402, - MergeSort, MergeSort5.scala, split, 169, 21, 3, 975, 1445, 6971, diff --git a/testcases/repair/Numerical/Numerical.scala b/testcases/repair/Numerical/Numerical.scala index 1175da9cec6017d69ea1e1ba46ef0877b0be9ea9..06d0516abbf5beb1bcad8ec8c9a994bb5a16ce06 100644 --- a/testcases/repair/Numerical/Numerical.scala +++ b/testcases/repair/Numerical/Numerical.scala @@ -5,26 +5,26 @@ import leon.lang._ import leon.annotation._ object Numerical { - def power(base: Int, p: Int): Int = { - require(p >= 0) - if (p == 0) { - 1 - } else if (p%2 == 0) { - power(base*base, p/2) + def power(base: BigInt, p: BigInt): BigInt = { + require(p >= BigInt(0)) + if (p == BigInt(0)) { + BigInt(1) + } else if (p%BigInt(2) == BigInt(0)) { + power(base*base, p/BigInt(2)) } else { - base*power(base, p-1) + base*power(base, p-BigInt(1)) } } ensuring { res => ((base, p), res) passes { - case (_, 0) => 1 - case (b, 1) => b - case (2, 7) => 128 - case (2, 10) => 1024 + case (_, BigInt(0)) => BigInt(1) + case (b, BigInt(1)) => b + case (BigInt(2), BigInt(7)) => BigInt(128) + case (BigInt(2), BigInt(10)) => BigInt(1024) } } - def gcd(a: Int, b: Int): Int = { - require(a > 0 && b > 0); + def gcd(a: BigInt, b: BigInt): BigInt = { + require(a > BigInt(0) && b > BigInt(0)); if (a == b) { a @@ -34,15 +34,15 @@ object Numerical { gcd(a, b-a) } } ensuring { - res => (a%res == 0) && (b%res == 0) && (((a,b), res) passes { - case (468, 24) => 12 + res => (a%res == BigInt(0)) && (b%res == BigInt(0)) && (((a,b), res) passes { + case (BigInt(468), BigInt(24)) => BigInt(12) }) } - def moddiv(a: Int, b: Int): (Int, Int) = { - require(a >= 0 && b > 0); + def moddiv(a: BigInt, b: BigInt): (BigInt, BigInt) = { + require(a >= BigInt(0) && b > BigInt(0)); if (b > a) { - (a, 0) + (a, BigInt(0)) } else { val (r1, r2) = moddiv(a-b, b) (r1, r2+1) diff --git a/testcases/repair/Numerical/Numerical1.scala b/testcases/repair/Numerical/Numerical1.scala index 4f1a32f8b1c45cbf343deba21035497342b515a1..0818b015677e02be9661db68d8d0b8bb4a2d6e2d 100644 --- a/testcases/repair/Numerical/Numerical1.scala +++ b/testcases/repair/Numerical/Numerical1.scala @@ -5,26 +5,26 @@ import leon.lang._ import leon.annotation._ object Numerical { - def power(base: Int, p: Int): Int = { - require(p >= 0) - if (p == 0) { - 1 - } else if (p%2 == 0) { - power(base*base, p/2) + def power(base: BigInt, p: BigInt): BigInt = { + require(p >= BigInt(0)) + if (p == BigInt(0)) { + BigInt(1) + } else if (p%BigInt(2) == BigInt(0)) { + power(base*base, p/BigInt(2)) } else { - power(base, p-1) // fixme: Missing base* + power(base, p-BigInt(1)) // fixme: Missing base* } } ensuring { res => ((base, p), res) passes { - case (_, 0) => 1 - case (b, 1) => b - case (2, 7) => 128 - case (2, 10) => 1024 + case (_, BigInt(0)) => BigInt(1) + case (b, BigInt(1)) => b + case (BigInt(2), BigInt(7)) => BigInt(128) + case (BigInt(2), BigInt(10)) => BigInt(1024) } } - def gcd(a: Int, b: Int): Int = { - require(a > 0 && b > 0); + def gcd(a: BigInt, b: BigInt): BigInt = { + require(a > BigInt(0) && b > BigInt(0)); if (a == b) { a @@ -34,15 +34,15 @@ object Numerical { gcd(a, b-a) } } ensuring { - res => (a%res == 0) && (b%res == 0) && (((a,b), res) passes { - case (468, 24) => 12 + res => (a%res == BigInt(0)) && (b%res == BigInt(0)) && (((a,b), res) passes { + case (BigInt(468), BigInt(24)) => BigInt(12) }) } - def moddiv(a: Int, b: Int): (Int, Int) = { - require(a >= 0 && b > 0); + def moddiv(a: BigInt, b: BigInt): (BigInt, BigInt) = { + require(a >= BigInt(0) && b > BigInt(0)); if (b > a) { - (a, 0) + (a, BigInt(0)) } else { val (r1, r2) = moddiv(a-b, b) (r1, r2+1) diff --git a/testcases/repair/Numerical/Numerical2.scala b/testcases/repair/Numerical/Numerical2.scala index ec191f681f128d52fa63438f32d9124b6fcffaf6..a5eb3595f311e96d8a3d40bd36e66f6a46c60947 100644 --- a/testcases/repair/Numerical/Numerical2.scala +++ b/testcases/repair/Numerical/Numerical2.scala @@ -5,39 +5,39 @@ import leon.lang._ import leon.annotation._ object Numerical { - def power(base: Int, p: Int): Int = { - require(p >= 0) - if (p == 0) { - 1 - } else if (p%2 == 0) { - power(base*base, p/2) + def power(base: BigInt, p: BigInt): BigInt = { + require(p >= BigInt(0)) + if (p == BigInt(0)) { + BigInt(1) + } else if (p%BigInt(2) == BigInt(0)) { + power(base*base, p/BigInt(2)) } else { - base*power(base, p-1) + base*power(base, p-BigInt(1)) } } ensuring { res => ((base, p), res) passes { - case (_, 0) => 1 - case (b, 1) => b - case (2, 7) => 128 - case (2, 10) => 1024 + case (_, BigInt(0)) => BigInt(1) + case (b, BigInt(1)) => b + case (BigInt(2), BigInt(7)) => BigInt(128) + case (BigInt(2), BigInt(10)) => BigInt(1024) } } - def gcd(a: Int, b: Int): Int = { - require(a > 0 && b > 0); + def gcd(a: BigInt, b: BigInt): BigInt = { + require(a > BigInt(0) && b > BigInt(0)); if (a == b) { - 1 // fixme: should be a + BigInt(1) // fixme: should be a } else if (a > b) { gcd(a-b, b) } else { gcd(a, b-a) } } ensuring { - res => (a%res == 0) && (b%res == 0) && (((a,b), res) passes { - case (120, 24) => 12 - case (5, 7) => 1 - case (5, 5) => 5 + res => (a%res == BigInt(0)) && (b%res == BigInt(0)) && (((a,b), res) passes { + case (BigInt(120), BigInt(24)) => BigInt(12) + case (BigInt(5), BigInt(7)) => BigInt(1) + case (BigInt(5), BigInt(5)) => BigInt(5) }) } } diff --git a/testcases/repair/Numerical/Numerical3.scala b/testcases/repair/Numerical/Numerical3.scala index 4f782cab34f390c9a05d7362c2beeaa86718c911..5e0dbee5950b00fa99395f7aaf0c6db7c0d361e1 100644 --- a/testcases/repair/Numerical/Numerical3.scala +++ b/testcases/repair/Numerical/Numerical3.scala @@ -5,26 +5,26 @@ import leon.lang._ import leon.annotation._ object Numerical { - def power(base: Int, p: Int): Int = { - require(p >= 0) - if (p == 0) { - 1 - } else if (p%2 == 0) { - power(base*base, p/2) + def power(base: BigInt, p: BigInt): BigInt = { + require(p >= BigInt(0)) + if (p == BigInt(0)) { + BigInt(1) + } else if (p%BigInt(2) == BigInt(0)) { + power(base*base, p/BigInt(2)) } else { - base*power(base, p-1) + base*power(base, p-BigInt(1)) } } ensuring { res => ((base, p), res) passes { - case (_, 0) => 1 - case (b, 1) => b - case (2, 7) => 128 - case (2, 10) => 1024 + case (_, BigInt(0)) => BigInt(1) + case (b, BigInt(1)) => b + case (BigInt(2), BigInt(7)) => BigInt(128) + case (BigInt(2), BigInt(10)) => BigInt(1024) } } - def gcd(a: Int, b: Int): Int = { - require(a > 0 && b > 0); + def gcd(a: BigInt, b: BigInt): BigInt = { + require(a > BigInt(0) && b > BigInt(0)); if (a == b) { a @@ -34,15 +34,15 @@ object Numerical { gcd(a, b-a) } } ensuring { - res => (a%res == 0) && (b%res == 0) && (((a,b), res) passes { - case (468, 24) => 12 + res => (a%res == BigInt(0)) && (b%res == BigInt(0)) && (((a,b), res) passes { + case (BigInt(468), BigInt(24)) => BigInt(12) }) } - def moddiv(a: Int, b: Int): (Int, Int) = { - require(a >= 0 && b > 0); + def moddiv(a: BigInt, b: BigInt): (BigInt, BigInt) = { + require(a >= BigInt(0) && b > BigInt(0)); if (b > a) { - (1, 0) // fixme: should be (a, 0) + (BigInt(1), BigInt(0)) // fixme: should be (a, BigInt(0)) } else { val (r1, r2) = moddiv(a-b, b) (r1, r2+1) diff --git a/testcases/repair/PropLogic/PropLogic.scala b/testcases/repair/PropLogic/PropLogic.scala index 718a36e89b1fb70e7eb1c4cfc0a35520f7f953c4..b654b8fbc63cf3b6b5d01d366e72ffd8bb531026 100644 --- a/testcases/repair/PropLogic/PropLogic.scala +++ b/testcases/repair/PropLogic/PropLogic.scala @@ -9,16 +9,16 @@ object SemanticsPreservation { case class Or(lhs : Formula, rhs : Formula) extends Formula case class Not(f: Formula) extends Formula case class Const(v: Boolean) extends Formula - case class Literal(id: Int) extends Formula + case class Literal(id: BigBigInt) extends Formula - def size(f : Formula) : Int = { f match { + def size(f : Formula) : BigBigInt = { f match { case And(l,r) => 1 + size(l) + size(r) case Or(l,r) => 1 + size(l) + size(r) case Not(e) => 1 + size(e) case _ => 1 }} ensuring { _ >= 0 } - def eval(formula: Formula)(implicit trueVars : Set[Int]): Boolean = formula match { + def eval(formula: Formula)(implicit trueVars : Set[BigBigInt]): Boolean = formula match { case And(lhs, rhs) => eval(lhs) && eval(rhs) case Or(lhs, rhs) => eval(lhs) || eval(rhs) case Not(f) => !eval(f) diff --git a/testcases/repair/PropLogic/PropLogic1.scala b/testcases/repair/PropLogic/PropLogic1.scala index 47752be39082a9ab05e73030919f423131fad1c6..93cd6bfbf947c019538deeaaa3620a26f04e7d57 100644 --- a/testcases/repair/PropLogic/PropLogic1.scala +++ b/testcases/repair/PropLogic/PropLogic1.scala @@ -5,20 +5,20 @@ import leon.collection._ object SemanticsPreservation { sealed abstract class Formula - case class Const(v: Boolean) extends Formula - case class Literal(id: Int) extends Formula - case class Not(f: Formula) extends Formula case class And(lhs : Formula, rhs : Formula) extends Formula case class Or(lhs : Formula, rhs : Formula) extends Formula + case class Not(f: Formula) extends Formula + case class Const(v: Boolean) extends Formula + case class Literal(id: BigInt) extends Formula - def size(f : Formula) : Int = { f match { + def size(f : Formula) : BigInt = { f match { case And(l,r) => 1 + size(l) + size(r) case Or(l,r) => 1 + size(l) + size(r) case Not(e) => 1 + size(e) case _ => 1 }} ensuring { _ >= 0 } - def eval(formula: Formula)(implicit trueVars : Set[Int]): Boolean = formula match { + def eval(formula: Formula)(implicit trueVars : Set[BigInt]): Boolean = formula match { case And(lhs, rhs) => eval(lhs) && eval(rhs) case Or(lhs, rhs) => eval(lhs) || eval(rhs) case Not(f) => !eval(f) @@ -30,17 +30,14 @@ object SemanticsPreservation { case Not(And(lhs,rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Or(lhs,rhs)) => And(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Const(v)) => Const(!v) - case Not(Not(e)) => e // FIXME: should be nnf(e) + case Not(Not(e)) => e // FIXME missing nnf case And(lhs, rhs) => And(nnf(lhs), nnf(rhs)) case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs)) - case other => other + case other => other }} ensuring { res => - isNNF(res) && ((formula, res) passes { - case Not(Not(Not( Const(a) ))) => Const(!a) - }) + isNNF(res) } - def isNNF(f : Formula) : Boolean = f match { case Not(Literal(_)) => true case Not(_) => false diff --git a/testcases/repair/PropLogic/PropLogic2.scala b/testcases/repair/PropLogic/PropLogic2.scala index 2799b20788010fc952e51de8d2c0a69421c303cb..2819234ff176f4b0bac2e2b781397dda49edef53 100644 --- a/testcases/repair/PropLogic/PropLogic2.scala +++ b/testcases/repair/PropLogic/PropLogic2.scala @@ -9,16 +9,16 @@ object SemanticsPreservation { case class Or(lhs : Formula, rhs : Formula) extends Formula case class Not(f: Formula) extends Formula case class Const(v: Boolean) extends Formula - case class Literal(id: Int) extends Formula + case class Literal(id: BigInt) extends Formula - def size(f : Formula) : Int = { f match { + def size(f : Formula) : BigInt = { f match { case And(l,r) => 1 + size(l) + size(r) case Or(l,r) => 1 + size(l) + size(r) case Not(e) => 1 + size(e) case _ => 1 }} ensuring { _ >= 0 } - def eval(formula: Formula)(implicit trueVars : Set[Int]): Boolean = formula match { + def eval(formula: Formula)(implicit trueVars : Set[BigInt]): Boolean = formula match { case And(lhs, rhs) => eval(lhs) && eval(rhs) case Or(lhs, rhs) => eval(lhs) || eval(rhs) case Not(f) => !eval(f) @@ -30,14 +30,12 @@ object SemanticsPreservation { case Not(And(lhs,rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Or(lhs,rhs)) => And(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Const(v)) => Const(!v) + case Not(Not(e)) => nnf(e) case And(lhs, rhs) => And(nnf(lhs), nnf(rhs)) case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs)) - // FIXME: forgot to handle the Not(Not(_)) case case other => other }} ensuring { res => - isNNF(res) && ((formula, res) passes { - case Not(Not(Not(Const(a)))) => Const(!a) - }) + isNNF(res) } def isNNF(f : Formula) : Boolean = f match { @@ -47,5 +45,6 @@ object SemanticsPreservation { case Or(lhs, rhs) => isNNF(lhs) && isNNF(rhs) case _ => true } - + + } diff --git a/testcases/repair/PropLogic/PropLogic3.scala b/testcases/repair/PropLogic/PropLogic3.scala index b65f6e2a29a3ce0e5a60116285007c00a6073f3e..8f2a62cd8273ed7ee613891de72f567812b4a806 100644 --- a/testcases/repair/PropLogic/PropLogic3.scala +++ b/testcases/repair/PropLogic/PropLogic3.scala @@ -9,16 +9,16 @@ object SemanticsPreservation { case class Or(lhs : Formula, rhs : Formula) extends Formula case class Not(f: Formula) extends Formula case class Const(v: Boolean) extends Formula - case class Literal(id: Int) extends Formula + case class Literal(id: BigInt) extends Formula - def size(f : Formula) : Int = { f match { + def size(f : Formula) : BigInt = { f match { case And(l,r) => 1 + size(l) + size(r) case Or(l,r) => 1 + size(l) + size(r) case Not(e) => 1 + size(e) case _ => 1 }} ensuring { _ >= 0 } - def eval(formula: Formula)(implicit trueVars : Set[Int]): Boolean = formula match { + def eval(formula: Formula)(implicit trueVars : Set[BigInt]): Boolean = formula match { case And(lhs, rhs) => eval(lhs) && eval(rhs) case Or(lhs, rhs) => eval(lhs) || eval(rhs) case Not(f) => !eval(f) @@ -29,16 +29,13 @@ object SemanticsPreservation { def nnf(formula : Formula) : Formula = { formula match { case Not(And(lhs,rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Or(lhs,rhs)) => And(nnf(Not(lhs)), nnf(Not(rhs))) - case Not(Const(v)) => Const(v) // FIXME should be !v. + case Not(Const(v)) => Const(v) // FIXME case Not(Not(e)) => nnf(e) case And(lhs, rhs) => And(nnf(lhs), nnf(rhs)) case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs)) case other => other - }} ensuring { res => - isNNF(res) && ((formula, res) passes { - case Not(Const(true)) => Const(false) - case Not(Const(false)) => Const(true) - }) + }} ensuring { res => + isNNF(res) } def isNNF(f : Formula) : Boolean = f match { @@ -49,4 +46,5 @@ object SemanticsPreservation { case _ => true } + } diff --git a/testcases/repair/PropLogic/PropLogic4.scala b/testcases/repair/PropLogic/PropLogic4.scala index 025c0a8b556c111b76362ea8133776f1f2cff82d..40cddee807cbba06e33cfe51028a9be7fa6e1a87 100644 --- a/testcases/repair/PropLogic/PropLogic4.scala +++ b/testcases/repair/PropLogic/PropLogic4.scala @@ -1,4 +1,3 @@ -// Fails import leon.lang._ import leon.annotation._ import leon.collection._ @@ -10,16 +9,16 @@ object SemanticsPreservation { case class Or(lhs : Formula, rhs : Formula) extends Formula case class Not(f: Formula) extends Formula case class Const(v: Boolean) extends Formula - case class Literal(id: Int) extends Formula + case class Literal(id: BigInt) extends Formula - def size(f : Formula) : Int = { f match { + def size(f : Formula) : BigInt = { f match { case And(l,r) => 1 + size(l) + size(r) case Or(l,r) => 1 + size(l) + size(r) case Not(e) => 1 + size(e) case _ => 1 }} ensuring { _ >= 0 } - def eval(formula: Formula)(implicit trueVars : Set[Int]): Boolean = formula match { + def eval(formula: Formula)(implicit trueVars : Set[BigInt]): Boolean = formula match { case And(lhs, rhs) => eval(lhs) && eval(rhs) case Or(lhs, rhs) => eval(lhs) || eval(rhs) case Not(f) => !eval(f) @@ -28,17 +27,15 @@ object SemanticsPreservation { } def nnf(formula : Formula) : Formula = { formula match { - case Not(And(lhs,rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs))) + case Not(And(lhs,rhs)) => And(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Or(lhs,rhs)) => And(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Const(v)) => Const(!v) case Not(Not(e)) => nnf(e) case And(lhs, rhs) => And(nnf(lhs), nnf(rhs)) - case Or(lhs, rhs) => And(nnf(lhs), nnf(rhs)) // FIXME should be Or + case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs)) case other => other }} ensuring { res => - isNNF(res) && ((formula, res) passes { - case Or(Not(Const(c)), Not(Literal(l))) => Or(Const(!c), Not(Literal(l))) - }) + isNNF(res) } def isNNF(f : Formula) : Boolean = f match { @@ -49,4 +46,5 @@ object SemanticsPreservation { case _ => true } + } diff --git a/testcases/repair/PropLogic/PropLogic5.scala b/testcases/repair/PropLogic/PropLogic5.scala index 4b411082fdb6c777c8b4fdfbb83e4f7cc67a5e11..6b24d992663541e6a22fb61ca44520ce0291544f 100644 --- a/testcases/repair/PropLogic/PropLogic5.scala +++ b/testcases/repair/PropLogic/PropLogic5.scala @@ -1,4 +1,3 @@ -// Fails import leon.lang._ import leon.annotation._ import leon.collection._ @@ -10,16 +9,16 @@ object SemanticsPreservation { case class Or(lhs : Formula, rhs : Formula) extends Formula case class Not(f: Formula) extends Formula case class Const(v: Boolean) extends Formula - case class Literal(id: Int) extends Formula + case class Literal(id: BigInt) extends Formula - def size(f : Formula) : Int = { f match { + def size(f : Formula) : BigInt = { f match { case And(l,r) => 1 + size(l) + size(r) case Or(l,r) => 1 + size(l) + size(r) case Not(e) => 1 + size(e) case _ => 1 }} ensuring { _ >= 0 } - def eval(formula: Formula)(implicit trueVars : Set[Int]): Boolean = formula match { + def eval(formula: Formula)(implicit trueVars : Set[BigInt]): Boolean = formula match { case And(lhs, rhs) => eval(lhs) && eval(rhs) case Or(lhs, rhs) => eval(lhs) || eval(rhs) case Not(f) => !eval(f) @@ -29,16 +28,14 @@ object SemanticsPreservation { def nnf(formula : Formula) : Formula = { formula match { case Not(And(lhs,rhs)) => Or(nnf(Not(lhs)), nnf(Not(rhs))) - case Not(Or(lhs,rhs)) => And(nnf(Not(lhs)), nnf(Not(rhs))) + case Not(Or(lhs,rhs)) => formula//FIXME And(nnf(Not(lhs)), nnf(Not(rhs))) case Not(Const(v)) => Const(!v) case Not(Not(e)) => nnf(e) case And(lhs, rhs) => And(nnf(lhs), nnf(rhs)) - case Or(lhs, rhs) => formula//And(nnf(lhs), nnf(rhs)) // FIXME should be Or + case Or(lhs, rhs) => Or(nnf(lhs), nnf(rhs)) case other => other }} ensuring { res => - isNNF(res) && ((formula, res) passes { - case Or(Not(Const(c)), Not(Literal(l))) => Or(Const(!c), Not(Literal(l))) - }) + isNNF(res) } def isNNF(f : Formula) : Boolean = f match { @@ -49,4 +46,5 @@ object SemanticsPreservation { case _ => true } + } diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree.scala b/testcases/repair/RedBlackTree-old/RedBlackTree.scala deleted file mode 100644 index 8f88abf2802bbae9e02598d3256c3657131c0592..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree.scala +++ /dev/null @@ -1,101 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - - (c,a,x,b) match { - case (Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case (Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case (Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case (Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case (c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b)))// && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree1.scala b/testcases/repair/RedBlackTree-old/RedBlackTree1.scala deleted file mode 100644 index 93d5b61e9288be38928b9a01dfd3092889bfeef3..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree1.scala +++ /dev/null @@ -1,143 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - -// def buggyAdd(x: Int, t: Tree): Tree = { -// require(redNodesHaveBlackChildren(t)) -// // makeBlack(ins(x, t)) -// ins(x, t) -// } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res)) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,xV,d)) // FIXME: xV instead of zV in right node - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,xV,d)) // FIXME: xV instead of zV in right node - case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b))) //&& redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree2.scala b/testcases/repair/RedBlackTree-old/RedBlackTree2.scala deleted file mode 100644 index d4675cadb43edffbc9eb395e373d5751a03e5ecc..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree2.scala +++ /dev/null @@ -1,143 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) Node(c, ins(x, a), y, b) // FIXME : forgot to balance - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - -// def buggyAdd(x: Int, t: Tree): Tree = { -// require(redNodesHaveBlackChildren(t)) -// // makeBlack(ins(x, t)) -// ins(x, t) -// } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res)) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b)) )// && redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree3.scala b/testcases/repair/RedBlackTree-old/RedBlackTree3.scala deleted file mode 100644 index 4d8e1f39d07d8c667a0fa733d6d09fce2569f1a4..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree3.scala +++ /dev/null @@ -1,143 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - -// def buggyAdd(x: Int, t: Tree): Tree = { -// require(redNodesHaveBlackChildren(t)) -// // makeBlack(ins(x, t)) -// ins(x, t) -// } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res)) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,xV,d)) // FIXME: xV instead of zV in right node - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b))) //&& redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree4.scala b/testcases/repair/RedBlackTree-old/RedBlackTree4.scala deleted file mode 100644 index cff237c6f16bbd9f8f1a9000d333966b2ef4a555..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree4.scala +++ /dev/null @@ -1,143 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - -// def buggyAdd(x: Int, t: Tree): Tree = { -// require(redNodesHaveBlackChildren(t)) -// // makeBlack(ins(x, t)) -// ins(x, t) -// } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res)) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),xV,Node(Black,c,zV,d)) // FIXME: xV instead of yV in top node - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b))) //&& redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree5.scala b/testcases/repair/RedBlackTree-old/RedBlackTree5.scala deleted file mode 100644 index 531a5b491e6f529bc7390842af5822af6f75b153..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree5.scala +++ /dev/null @@ -1,144 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) && content(res) == content(n) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - -// def buggyAdd(x: Int, t: Tree): Tree = { -// require(redNodesHaveBlackChildren(t)) -// // makeBlack(ins(x, t)) -// ins(x, t) -// } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res)) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // FIXME: forgot this case - //case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b)) )// && redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree6.scala b/testcases/repair/RedBlackTree-old/RedBlackTree6.scala deleted file mode 100644 index 36cfb692b4a9efb218be24eee6bb78dc0c680851..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree6.scala +++ /dev/null @@ -1,142 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case _ => n - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - //def add(x: Int, t: Tree): Tree = { - // require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - // makeBlack(ins(x, t)) - //} ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - ins(x, t) // FIXME: makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b)) )// && redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/RedBlackTree-old/RedBlackTree7.scala b/testcases/repair/RedBlackTree-old/RedBlackTree7.scala deleted file mode 100644 index 66b83042888f4e72bbde73cd4940edceeb935df7..0000000000000000000000000000000000000000 --- a/testcases/repair/RedBlackTree-old/RedBlackTree7.scala +++ /dev/null @@ -1,143 +0,0 @@ -import leon._ -import lang._ -import annotation._ - -object RedBlackTree { - sealed abstract class Color - case object Red extends Color - case object Black extends Color - - sealed abstract class Tree - case object Empty extends Tree - case class Node(color: Color, left: Tree, value: Int, right: Tree) extends Tree - - def content(t: Tree) : Set[Int] = t match { - case Empty => Set.empty - case Node(_, l, v, r) => content(l) ++ Set(v) ++ content(r) - } - - def size(t: Tree) : Int = t match { - case Empty => 0 - case Node(_, l, v, r) => size(l) + 1 + size(r) - } - - /* We consider leaves to be black by definition */ - def isBlack(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black,_,_,_) => true - case _ => false - } - - def redNodesHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(Black, l, _, r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - case Node(Red, l, _, r) => isBlack(l) && isBlack(r) && redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def redDescHaveBlackChildren(t: Tree) : Boolean = t match { - case Empty => true - case Node(_,l,_,r) => redNodesHaveBlackChildren(l) && redNodesHaveBlackChildren(r) - } - - def blackBalanced(t : Tree) : Boolean = t match { - case Node(_,l,_,r) => blackBalanced(l) && blackBalanced(r) && blackHeight(l) == blackHeight(r) - case Empty => true - } - - def blackHeight(t : Tree) : Int = t match { - case Empty => 1 - case Node(Black, l, _, _) => blackHeight(l) + 1 - case Node(Red, l, _, _) => blackHeight(l) - } - - - // <<insert element x into the tree t>> - def ins(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t)) - t match { - case Empty => Node(Red,Empty,x,Empty) - case Node(c,a,y,b) => - if (x < y) balance(c, ins(x, a), y, b) - else if (x == y) Node(c,a,y,b) - else balance(c,a,y,ins(x, b)) - } - } ensuring (res => - content(res) == content(t) ++ Set(x) && - size(t) <= size(res) && - size(res) <= size(t) + 1 && - redDescHaveBlackChildren(res) && - blackBalanced(res) - ) - - def makeBlack(n: Tree): Tree = { - require(redDescHaveBlackChildren(n) && blackBalanced(n) ) - n match { - case Node(Red,l,v,r) => Node(Black,l,v,r) - case Node(Black,l,v,r) => Node(Red,l,v,r) // FIXME : Red instead of Black - } - } ensuring(res => redNodesHaveBlackChildren(res) && blackBalanced(res) && content(res) == content(n) ) - - def add(x: Int, t: Tree): Tree = { - require(redNodesHaveBlackChildren(t) && blackBalanced(t) ) - makeBlack(ins(x, t)) - } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res) && blackBalanced(res) ) - -// def buggyAdd(x: Int, t: Tree): Tree = { -// require(redNodesHaveBlackChildren(t)) -// // makeBlack(ins(x, t)) -// ins(x, t) -// } ensuring (res => content(res) == content(t) ++ Set(x) && redNodesHaveBlackChildren(res)) - - def balance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // require( - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,_,b),_,c),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,Node(Red,a,_,Node(Red,b,_,c)),_,d) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,Node(Red,b,_,c),_,d)) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case Node(Black,a,_,Node(Red,b,_,Node(Red,c,_,d))) => - // redNodesHaveBlackChildren(a) && - // redNodesHaveBlackChildren(b) && - // redNodesHaveBlackChildren(c) && - // redNodesHaveBlackChildren(d) - // case t => redDescHaveBlackChildren(t) - // } - // ) - Node(c,a,x,b) match { - case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - case Node(c,a,xV,b) => Node(c,a,xV,b) - } - } ensuring (res => content(res) == content(Node(c,a,x,b)) )// && redDescHaveBlackChildren(res)) - - // def buggyBalance(c: Color, a: Tree, x: Int, b: Tree): Tree = { - // Node(c,a,x,b) match { - // case Node(Black,Node(Red,Node(Red,a,xV,b),yV,c),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,Node(Red,a,xV,Node(Red,b,yV,c)),zV,d) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,Node(Red,b,yV,c),zV,d)) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // case Node(Black,a,xV,Node(Red,b,yV,Node(Red,c,zV,d))) => - // Node(Red,Node(Black,a,xV,b),yV,Node(Black,c,zV,d)) - // } - // } ensuring (res => content(res) == content(Node(c,a,x,b)) ) // && redDescHaveBlackChildren(res)) - -} diff --git a/testcases/repair/runTests.sh b/testcases/repair/runTests.sh index 5a3d49c706630a4c1362201d8b93a273570a368d..55ca5cdf9f74828855e9ce85eb8aa18bf8ba3169 100755 --- a/testcases/repair/runTests.sh +++ b/testcases/repair/runTests.sh @@ -40,7 +40,7 @@ echo "# Category, File, function, p.S, fuS ./leon --repair --timeout=30 --solvers=fairz3,enum --functions=pad testcases/repair/List/List1.scala | tee -a $fullLog ./leon --repair --timeout=30 --solvers=fairz3,enum --functions=++ testcases/repair/List/List2.scala | tee -a $fullLog -./leon --repair --timeout=30 --solvers=fairz3,enum --functions=ap testcases/repair/List/List3.scala | tee -a $fullLog +./leon --repair --timeout=30 --solvers=fairz3,enum --functions=:+ testcases/repair/List/List3.scala | tee -a $fullLog ./leon --repair --timeout=30 --functions=drop testcases/repair/List/List4.scala | tee -a $fullLog ./leon --repair --timeout=30 --functions=replace testcases/repair/List/List5.scala | tee -a $fullLog ./leon --repair --timeout=30 --solvers=fairz3,enum --functions=count testcases/repair/List/List6.scala | tee -a $fullLog