Skip to content

Commit

Permalink
Add flatMap to the set class in the library (#1588)
Browse files Browse the repository at this point in the history
* add flatmap to set

* add flatmap postcondition

* update set

* Move forall and exists to the SetOps class

* move to extension methods instead of implicit class

* new timeout for sat precond test

* new big timeout

* ignore sat-precondition/valid/SATPrecond4 with Princess
  • Loading branch information
samuelchassot authored Oct 16, 2024
1 parent bd9da42 commit d3f12aa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
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

0 comments on commit d3f12aa

Please sign in to comment.