-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ranged for syntax to the PVL frontend. This kind of loop matches the use case where the bounds of the loop do not depend on the state changed within the loop, and hence strict loop bounds can be added to the contract.
- Loading branch information
1 parent
f3f99e4
commit c12fd19
Showing
9 changed files
with
88 additions
and
1 deletion.
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
13 changes: 13 additions & 0 deletions
13
src/col/vct/col/ast/statement/composite/RangedForImpl.scala
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,13 @@ | ||
package vct.col.ast.statement.composite | ||
|
||
import vct.col.ast.{Declaration, RangedFor} | ||
import vct.col.print._ | ||
|
||
trait RangedForImpl[G] { this: RangedFor[G] => | ||
override def declarations: Seq[Declaration[G]] = Seq(iter.variable) | ||
|
||
override def layout(implicit ctx: Ctx): Doc = | ||
Doc.stack(Seq( | ||
Group(Text("for") <> "(" <> Doc.arg(iter) <> ")"), | ||
)) <+> body.layoutAsBlock | ||
} |
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
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
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
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
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
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,47 @@ | ||
package vct.col.rewrite | ||
|
||
import hre.util.ScopedStack | ||
import vct.col.ast.{Select, Block, Assign, Expr, IterVariable, Local, LocalDecl, Loop, LoopInvariant, IterationContract, Eval, LoopContract, PostAssignExpression, Range, RangedFor, SeqMember, Statement, Variable} | ||
import vct.col.origin.AssignLocalOk | ||
import vct.col.util.AstBuildHelpers.const | ||
import vct.col.util.AstBuildHelpers._ | ||
import vct.col.ast.RewriteHelpers._ | ||
|
||
case object EncodeRangedFor extends RewriterBuilder { | ||
override def key: String = "encodeRangedFor" | ||
override def desc: String = "Encodes ranged for as a regular for loop" | ||
} | ||
|
||
case class EncodeRangedFor[Pre <: Generation]() extends Rewriter[Pre] { | ||
val bounds = ScopedStack[(RangedFor[Pre], Expr[Post])]() | ||
|
||
override def dispatch(stat: Statement[Pre]): Statement[Post] = stat match { | ||
case rf @ RangedFor(iv @ IterVariable(iVar, from, to), contract, body) => | ||
implicit val o = iVar.o | ||
val i: Local[Post] = Local(succ[Variable[Post]](iVar))(iVar.o) | ||
Loop( | ||
Block(Seq( | ||
LocalDecl(variables.collect(dispatch(iVar))._1.head), | ||
Assign(i, dispatch(from))(AssignLocalOk) | ||
))(iVar.o), | ||
{ implicit val o = iv.o; SeqMember(i, Range(dispatch(from), dispatch(to))) }, | ||
Eval(PostAssignExpression(i, i + const(1))(AssignLocalOk)), | ||
bounds.having((rf, | ||
{ implicit val o = iv.o; | ||
Select(dispatch(from) < dispatch(to), | ||
SeqMember(i, Range(dispatch(from), dispatch(to) + const(1))), | ||
i === dispatch(from)) | ||
}))(dispatch(contract)), | ||
dispatch(body) | ||
)(rf.o) | ||
case stat => rewriteDefault(stat) | ||
} | ||
|
||
override def dispatch(contract: LoopContract[Pre]): LoopContract[Post] = (bounds.topOption, contract) match { | ||
case (Some((rf, iBounds)), l: LoopInvariant[Pre]) => | ||
l.rewrite(invariant = (iBounds &* dispatch(l.invariant))(rf.o)) | ||
case (Some((rf, iBounds)), ic: IterationContract[Pre]) => | ||
ic.rewrite(context_everywhere = (iBounds &* dispatch(ic.context_everywhere))(rf.o)) | ||
case (None, c) => rewriteDefault(c) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
test/main/vct/test/integration/examples/TechnicalPvlSpec.scala
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,18 @@ | ||
package vct.test.integration.examples | ||
|
||
import vct.test.integration.helper.VercorsSpec | ||
|
||
class TechnicalPvlSpec extends VercorsSpec { | ||
vercors should verify using silicon in "ranged for loop" pvl | ||
""" | ||
void m() { | ||
int max = -1; | ||
loop_invariant max == i - 1; | ||
for (int i = 0 .. 10) { | ||
assert 0 <= i && i < 10; | ||
max = i; | ||
} | ||
assert max == 9; | ||
} | ||
""" | ||
} |