Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flatMap to the set class in the library #1588

Merged
merged 9 commits into from
Oct 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SatPrecondVerificationSuite extends VerificationComponentTestSuite {
Set()

private val ignorePrincess: Set[String] = ignoreCommon ++
Set()
Set("sat-precondition/valid/SATPrecond4")

private val ignoreCodegen: Set[String] = Set()

Expand All @@ -46,7 +46,9 @@ class SatPrecondVerificationSuite extends VerificationComponentTestSuite {
inox.optSelectedSolvers(Set(solver)),
inox.solvers.optCheckModels(true),
evaluators.optCodeGen(codeGen),
inox.optTimeout(3.seconds),
inox.solvers.unrolling.optFeelingLucky(codeGen)) ++ seq

}
} yield conf
}
Expand Down
59 changes: 40 additions & 19 deletions frontends/library/stainless/lang/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,89 @@ object Set {
new Set(set)
}

@extern @pure @library
def mkString[A](set: Set[A], infix: String)(format: A => String): String = {
set.theSet.map(format).toList.sorted.mkString(infix)
}
// @extern @pure @library
// def mkString[A](set: Set[A], infix: String)(format: A => String): String = {
// set.theSet.map(format).toList.sorted.mkString(infix)
// }

extension[A](set: Set[A]) {

@library
implicit class SetOps[A](val set: Set[A]) extends AnyVal {
@library @extern @pure
def exists(p: A => Boolean): Boolean = {
set.theSet.exists(p)
}

@library @extern @pure
def forall(p: A => Boolean): Boolean = {
set.theSet.forall(p)
}

@extern @pure
@library @extern @pure
def map[B](f: A => B): Set[B] = {
new Set(set.theSet.map(f))
}

@extern @pure
@library @extern @pure
def mapPost1[B](f: A => B)(a: A): Unit = {
()
}.ensuring { _ =>
}.ensuring { _ =>
!set.contains(a) || map[B](f).contains(f(a))
}

@extern @pure
@library @extern @pure
def mapPost2[B](f: A => B)(b: B): A = {
require(map[B](f).contains(b))
(??? : A)
}.ensuring { (a:A) =>
}.ensuring { (a:A) =>
b == f(a) && set.contains(a)
}

@extern @pure
@library @extern @pure
def flatMap[B](f: A => Set[B]): Set[B] = {
new Set(set.theSet.flatMap(f(_).theSet))
}

@library @extern @pure
def flatMapPost[B](f: A => Set[B])(b: B) = {
(??? : A)
}.ensuring { _ =>
set.flatMap(f).contains(b) == set.exists(a => f(a).contains(b))
}

@library @extern @pure
def filter(p: A => Boolean): Set[A] = {
new Set(set.theSet.filter(p))
}

@extern @pure
@library @extern @pure
def filterPost(p: A => Boolean)(a: A): Unit = {
()
}.ensuring (_ => filter(p).contains(a) == (p(a) && set.contains(a)))

@extern @pure
@library @extern @pure
def withFilter(p: A => Boolean): Set[A] = {
new Set(set.theSet.filter(p))
}

@extern @pure
@library @extern @pure
def withFilterPost(p: A => Boolean)(a: A): Unit = {
()
}.ensuring(_ => withFilter(p).contains(a) == (p(a) && set.contains(a)))

@extern @pure
@library @extern @pure
def toList: List[A] = {
List.fromScala(set.theSet.toList)
}.ensuring(res => ListOps.noDuplicate(res) && res.content == set)

@extern @pure
@library @extern @pure
def toListPost(a:A): Unit = {
()
}.ensuring(_ => toList.contains(a) == set.contains(a))

@extern @pure
@library @extern @pure
def toScala: ScalaSet[A] = set.theSet

@extern @pure
@library @extern @pure
def mkString(infix: String)(format: A => String): String = {
set.theSet.map(format).toList.sorted.mkString(infix)
}
Expand Down
Loading