-
Notifications
You must be signed in to change notification settings - Fork 54
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
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e470e3d
scala version bump to 3.3.3; FunctionOf tree changed; Use Iterable in…
samuelchassot 28b8c2f
new error message for not hot fields
samuelchassot 354d232
add Monadic try to the library + tests
samuelchassot d896a95
Merge branch 'main' into sam/throw
samuelchassot 1281ab4
typo
samuelchassot ac95f46
Merge branch 'sam/throw' of github.com:samuelchassot/stainless into s…
samuelchassot 217ed80
failing tests
samuelchassot c3ab9f4
Merge branch 'main' into sam/throw
samuelchassot 7e9b8d9
Update MonadicTry2.scala
samuelchassot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 | ||
}) | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.