-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AntiAliasing: avoid rebuilding mutated objects when possible (#1507)
- Loading branch information
1 parent
e9f9193
commit 97caa41
Showing
13 changed files
with
642 additions
and
35 deletions.
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
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
32 changes: 32 additions & 0 deletions
32
frontends/benchmarks/imperative/invalid/OpaqueMutation1.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,32 @@ | ||
import stainless.lang.{ghost => ghostExpr, _} | ||
import stainless.proof._ | ||
import stainless.annotation._ | ||
import StaticChecks._ | ||
|
||
object OpaqueMutation1 { | ||
|
||
case class Box(var cnt: BigInt, var other: BigInt) { | ||
@opaque // Note the opaque | ||
def secretSauce(x: BigInt): BigInt = cnt + x // Nobody thought of it! | ||
|
||
@opaque // Note the opaque here as well | ||
def increment(): Unit = { | ||
@ghost val oldBox = snapshot(this) | ||
cnt += 1 | ||
ghostExpr { | ||
unfold(secretSauce(other)) | ||
unfold(oldBox.secretSauce(other)) | ||
check(oldBox.secretSauce(other) + 1 == this.secretSauce(other)) | ||
} | ||
}.ensuring(_ => old(this).secretSauce(other) + 1 == this.secretSauce(other)) | ||
} | ||
|
||
def test(b: Box): Unit = { | ||
@ghost val oldBox = snapshot(b) | ||
b.increment() | ||
// Note that, even though the implementation of `increment` does not alter `other`, | ||
// we do not have that knowledge here since the function is marked as opaque. | ||
// Therefore, the following is incorrect (but it holds for `b.other`, see the other `valid/OpaqueMutation`) | ||
assert(oldBox.secretSauce(oldBox.other) + 1 == b.secretSauce(oldBox.other)) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
frontends/benchmarks/imperative/invalid/OpaqueMutation2.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,33 @@ | ||
import stainless.lang.{ghost => ghostExpr, _} | ||
import stainless.proof._ | ||
import stainless.annotation._ | ||
import StaticChecks._ | ||
|
||
object OpaqueMutation2 { | ||
case class SmallerBox(var otherCnt: BigInt) | ||
|
||
case class Box(var cnt: BigInt, var smallerBox: SmallerBox) { | ||
@opaque // Note the opaque | ||
def secretSauce(x: BigInt): BigInt = cnt + x // Nobody thought of it! | ||
|
||
@opaque // Note the opaque here as well | ||
def increment(): Unit = { | ||
@ghost val oldBox = snapshot(this) | ||
cnt += 1 | ||
ghostExpr { | ||
unfold(secretSauce(smallerBox.otherCnt)) | ||
unfold(oldBox.secretSauce(smallerBox.otherCnt)) | ||
check(oldBox.secretSauce(smallerBox.otherCnt) + 1 == this.secretSauce(smallerBox.otherCnt)) | ||
} | ||
}.ensuring(_ => old(this).secretSauce(smallerBox.otherCnt) + 1 == this.secretSauce(smallerBox.otherCnt)) | ||
} | ||
|
||
def test(b: Box): Unit = { | ||
@ghost val oldBox = snapshot(b) | ||
b.increment() | ||
// Note that, even though the implementation of `increment` does not alter `smallerBox`, | ||
// we do not have that knowledge here since the function is marked as opaque. | ||
// Therefore, the following is incorrect (but it holds for `b.other`, see the other `valid/OpaqueMutation`) | ||
assert(oldBox.secretSauce(oldBox.smallerBox.otherCnt) + 1 == b.secretSauce(oldBox.smallerBox.otherCnt)) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
frontends/benchmarks/imperative/valid/ExternMutation.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,15 @@ | ||
import stainless.annotation._ | ||
|
||
object ExternMutation { | ||
case class Box(var value: BigInt) | ||
case class Container[@mutable T](t: T) | ||
|
||
@extern | ||
def f2(b: Container[Box]): Unit = ??? | ||
|
||
def g2(b: Container[Box]) = { | ||
val b0 = b | ||
f2(b) | ||
assert(b == b0) // Ok, even though `b` is assumed to be modified because `b0` is an alias of `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
Oops, something went wrong.