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
19 changes: 17 additions & 2 deletions frontends/library/stainless/lang/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,30 @@ object Set {
@extern @pure
def mapPost1[B](f: A => B)(a: A): Unit = {
()
}.ensuring { _ =>
}.ensuring { _ =>
!set.contains(a) || map[B](f).contains(f(a))
}

@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
def flatMap[B](f: A => Set[B]): Set[B] = {
new Set(set.theSet.flatMap(f(_).theSet))
}

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

@extern @pure
def filter(p: A => Boolean): Set[A] = {
new Set(set.theSet.filter(p))
Expand Down Expand Up @@ -114,4 +126,7 @@ sealed case class Set[T](theSet: scala.collection.immutable.Set[T]) {

def contains(a: T): Boolean = theSet.contains(a)
def subsetOf(b: Set[T]): Boolean = theSet.subsetOf(b.theSet)

def exists(p: T => Boolean): Boolean = theSet.exists(p)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is set.exists available to users? It does not seem like we have many properties of it known. If no, then it's a problem if it is used in spec of flatMap. If yes, we should probably add some axioms for it, like something returning an unspecified witness if a value exists and something instantiating property if it holds for all.

def forall(p: T => Boolean): Boolean = theSet.forall(p)
}
Loading