-
Notifications
You must be signed in to change notification settings - Fork 52
Add refutation support #583
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7cbf01d
Add refutation plugin
JonasAlaif 8558139
Merge branch 'master' into refute
JonasAlaif ce219b7
Some review changes
JonasAlaif 44d270e
Make files end with newlines
JonasAlaif 327c3d3
Make pretty-printed program parseable
JonasAlaif ee75d52
Add test with branching
JonasAlaif 154c67d
Review changes
JonasAlaif 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 hidden or 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
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
src/main/scala/viper/silver/plugin/standard/refute/RefuteASTExtension.scala
This file contains hidden or 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,20 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| // | ||
| // Copyright (c) 2011-2022 ETH Zurich. | ||
|
|
||
| package viper.silver.plugin.standard.refute | ||
|
|
||
| import viper.silver.ast._ | ||
| import viper.silver.ast.pretty.FastPrettyPrinter.{ContOps, text, toParenDoc} | ||
| import viper.silver.ast.pretty.PrettyPrintPrimitives | ||
|
|
||
| /** An `ExpectFail` info that tells us that this assert is a refute. */ | ||
| case object RefuteInfo extends ExpectFail | ||
|
|
||
| case class Refute(exp: Exp)(val pos: Position = NoPosition, val info: Info = NoInfo, val errT: ErrorTrafo = NoTrafos) extends ExtensionStmt { | ||
| override lazy val prettyPrint: PrettyPrintPrimitives#Cont = text("refute") <+> toParenDoc(exp) | ||
|
|
||
| override val extensionSubnodes: Seq[Node] = Seq(exp) | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/scala/viper/silver/plugin/standard/refute/RefuteErrors.scala
This file contains hidden or 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,29 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| // | ||
| // Copyright (c) 2011-2022 ETH Zurich. | ||
|
|
||
| package viper.silver.plugin.standard.refute | ||
|
|
||
| import viper.silver.verifier._ | ||
|
|
||
| case class RefuteFailed(override val offendingNode: Refute, | ||
| override val reason: ErrorReason, | ||
|
JonasAlaif marked this conversation as resolved.
Outdated
|
||
| override val cached: Boolean = false) extends AbstractVerificationError { | ||
| override val id = "refute.failed" | ||
| override val text = s"Refute holds in all cases or could not be reached (e.g. see Silicon `numberOfErrorsToReport`)." | ||
|
JonasAlaif marked this conversation as resolved.
Outdated
|
||
|
|
||
| override def withNode(offendingNode: errors.ErrorNode = this.offendingNode): RefuteFailed = | ||
| RefuteFailed(this.offendingNode, this.reason, this.cached) | ||
|
|
||
| override def withReason(r: ErrorReason): RefuteFailed = RefuteFailed(offendingNode, r, cached) | ||
| } | ||
|
|
||
| case class RefutationTrue(offendingNode: reasons.ErrorNode) extends AbstractErrorReason { | ||
| override val id: String = "refutation.true" | ||
|
|
||
| override val readableMessage: String = s"Assertion $offendingNode definitely holds." | ||
|
|
||
| override def withNode(offendingNode: reasons.ErrorNode): ErrorMessage = RefutationTrue(offendingNode) | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/main/scala/viper/silver/plugin/standard/refute/RefutePASTExtension.scala
This file contains hidden or 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,22 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| // | ||
| // Copyright (c) 2011-2022 ETH Zurich. | ||
|
|
||
| package viper.silver.plugin.standard.refute | ||
|
|
||
| import viper.silver.ast.{Position, Stmt} | ||
| import viper.silver.parser.TypeHelper.Bool | ||
| import viper.silver.parser._ | ||
|
|
||
| case class PRefute(e: PExp)(val pos: (Position, Position)) extends PExtender with PStmt { | ||
| override val getSubnodes: Seq[PNode] = Seq(e) | ||
|
|
||
| override def typecheck(t: TypeChecker, n: NameAnalyser):Option[Seq[String]] = { | ||
| t.check(e, Bool) | ||
| None | ||
| } | ||
|
|
||
| override def translateStmt(t: Translator): Stmt = Refute(t.exp(e))(t.liftPos(this)) | ||
| } |
78 changes: 78 additions & 0 deletions
78
src/main/scala/viper/silver/plugin/standard/refute/RefutePlugin.scala
This file contains hidden or 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,78 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| // | ||
| // Copyright (c) 2011-2022 ETH Zurich. | ||
|
|
||
| package viper.silver.plugin.standard.refute | ||
|
|
||
| import fastparse.P | ||
| import viper.silver.ast.utility.ViperStrategy | ||
| import viper.silver.ast._ | ||
| import viper.silver.parser.FastParser.{FP, exp, keyword, whitespace} | ||
| import viper.silver.parser.ParserExtension | ||
| import viper.silver.plugin.{ParserPluginTemplate, SilverPlugin} | ||
| import viper.silver.verifier._ | ||
| import viper.silver.verifier.errors.AssertFailed | ||
|
|
||
| class RefutePlugin extends SilverPlugin with ParserPluginTemplate { | ||
|
|
||
| /** Keyword used to define refute statements. */ | ||
| private val RefuteKeyword: String = "refute" | ||
|
|
||
| private var RefuteAsserts: Map[Position, Refute] = Map() | ||
|
JonasAlaif marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** Parser for refute statements. */ | ||
| def refute[_: P]: P[PRefute] = | ||
| FP(keyword(RefuteKeyword) ~/ exp).map{ case (pos, e) => PRefute(e)(pos) } | ||
|
|
||
| /** Add refute to the parser. */ | ||
| override def beforeParse(input: String, isImported: Boolean): String = { | ||
| // Add new keyword | ||
| ParserExtension.addNewKeywords(Set[String](RefuteKeyword)) | ||
| // Add new parser to the precondition | ||
| ParserExtension.addNewStmtAtEnd(refute(_)) | ||
| input | ||
| } | ||
|
|
||
| /** | ||
| * Remove refute statements from the AST and add them as non-det asserts. | ||
| * The ⭐ is nice since such a variable name cannot be parsed, but will it cause issues? | ||
|
JonasAlaif marked this conversation as resolved.
|
||
| */ | ||
| override def beforeVerify(input: Program): Program = | ||
| ViperStrategy.Slim({ | ||
| case r@Refute(exp) => { | ||
| this.RefuteAsserts += (r.pos -> r) | ||
| Seqn(Seq( | ||
| If(LocalVar("⭐", Bool)(r.pos), | ||
| Seqn(Seq( | ||
| Assert(exp)(r.pos, RefuteInfo), | ||
| Inhale(BoolLit(false)(r.pos))(r.pos) | ||
| ), Seq())(r.pos), | ||
| Seqn(Seq(), Seq())(r.pos))(r.pos) | ||
| ), | ||
| Seq(LocalVarDecl("⭐", Bool)(r.pos)) | ||
| )(r.pos) | ||
| } | ||
| }).recurseFunc({ | ||
| case Method(_, _, _, _, _, body) => Seq(body) | ||
| }).execute(input) | ||
|
|
||
| /** Remove refutation related errors and add RefuteAsserts that didn't report an error. */ | ||
| override def mapVerificationResult(input: VerificationResult): VerificationResult = { | ||
| val errors: Seq[AbstractError] = (input match { | ||
| case Success => Seq() | ||
| case Failure(errors) => { | ||
| errors.filter { | ||
| case AssertFailed(a, _, _) if a.info == RefuteInfo => { | ||
| this.RefuteAsserts -= a.pos | ||
| false | ||
| } | ||
| case _ => true | ||
| } | ||
| } | ||
| }) ++ this.RefuteAsserts.map(r => RefuteFailed(r._2, RefutationTrue(r._2.exp))) | ||
| if (errors.length == 0) Success | ||
| else Failure(errors) | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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,18 @@ | ||
| method foo(x: Int) returns (r: Int) | ||
| requires x > 10 | ||
| { | ||
| refute !(x > 0) | ||
| //:: ExpectedOutput(refute.failed:refutation.true) | ||
| refute x > 0 | ||
| refute false | ||
| //:: ExpectedOutput(refute.failed:refutation.true) | ||
| refute true | ||
| refute false | ||
| if (x > 0) { | ||
| r := x | ||
| } else { | ||
| //:: ExpectedOutput(refute.failed:refutation.true) | ||
| refute false | ||
| r := 0 | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.