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 Monadic Try construct to the Stainless library #1515

Merged
merged 9 commits into from
May 3, 2024
19 changes: 19 additions & 0 deletions frontends/benchmarks/verification/valid/MonadicTry1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright 2009-2024 EPFL, Lausanne */

import stainless.lang._
import stainless.proof._

object MonadicTry1 {

def success(): Try[Unit] = Success[Unit](())
def failure(): Try[Unit] = Failure[Unit](Exception("failure"))

def checkVal(b: Boolean): Try[Unit] = {
if (b) Success[Unit](())
else Failure[Unit](Exception("checkVal failed"))
} ensuring(res => res match {
case Success(_) => b
case Failure(_) => !b
})

}
40 changes: 40 additions & 0 deletions frontends/benchmarks/verification/valid/MonadicTry2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright 2009-2024 EPFL, Lausanne */

import stainless.lang._
import stainless.proof._
import scala.collection.immutable.Range.BigInt
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we import scala.collection.immutable.Range.BigInt ? I did not see this in other Stainless programs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

indeed, that was imported automatically by metals and I didn't spot it.

samuelchassot marked this conversation as resolved.
Show resolved Hide resolved

object MonadicTry2 {

def checkPositive(i: BigInt): Try[BigInt]= {
if (i > 0) Success[BigInt](i)
else Failure[BigInt](Exception("i is not positive"))
} ensuring(res => res match {
case Success(ii) => i > 0 && i == ii
case Failure(_) => i <= 0
})

def checkEven(i: BigInt): Try[BigInt] = {
if (i % 2 == 0) Success[BigInt](i)
else Failure[BigInt](Exception("i is not even"))
} ensuring(res => res match {
case Success(ii) => i % 2 == 0 && i == ii
case Failure(_) => i % 2 != 0
})

def checkEvenPositive(i: BigInt): Try[BigInt] = {
checkPositive(i).flatMap(ii => checkEven(ii))
} ensuring(res => res match {
case Success(ii) => i > 0 && i % 2 == 0 && i == ii
case Failure(_) => i <= 0 || i % 2 != 0
})

def evenPlusOne(i: BigInt): Try[BigInt] = {
checkEven(i).flatMap(ii => checkPositive(ii)).map(ii => ii + 1)
} ensuring(res => res match {
case Success(ii) => i % 2 == 0 && i > 0 && ii == i + 1 && ii % 2 == 1
case Failure(_) => i % 2 != 0 || i <= 0
})


}
19 changes: 19 additions & 0 deletions frontends/library/stainless/lang/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ package object lang {
@library
abstract class Exception extends Throwable

@library
@extern
def Exception(msg: String): Exception = new Exception{}

@library
sealed abstract class Try[T]{
def map[U](f: T => U): Try[U] = this match {
case Success(t) => Success(f(t))
case Failure(exc: Exception) => Failure(exc)
}

def flatMap[U](f: T => Try[U]): Try[U] = this match {
case Success(t) => f(t)
case Failure(exc: Exception) => Failure(exc)
}
}
@library case class Success[T](t: T) extends Try[T]
@library case class Failure[T](exc: Exception) extends Try[T]

@ignore
implicit class Throwing[T](underlying: => T) {
def throwing(pred: Exception => Boolean): T = try {
Expand Down