-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
11 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
36 changes: 36 additions & 0 deletions
36
...ain/scala/at/forsyte/apalache/tla/bmcmt/stratifiedRules/base/IntConstStratifiedRule.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,36 @@ | ||
package at.forsyte.apalache.tla.bmcmt.stratifiedRules.base | ||
|
||
import at.forsyte.apalache.tla.bmcmt._ | ||
import at.forsyte.apalache.tla.bmcmt.stratifiedRules.aux.caches.IntValueCache | ||
import at.forsyte.apalache.tla.bmcmt.stratifiedRules.{Rewriter, RewriterScope, StratifiedRule} | ||
import at.forsyte.apalache.tla.lir.values.TlaInt | ||
import at.forsyte.apalache.tla.lir.{TlaEx, ValEx} | ||
import at.forsyte.apalache.tla.types.tla | ||
|
||
/** | ||
* Rewrites integer constants. | ||
* | ||
* @author | ||
* Jure Kukovec | ||
*/ | ||
class IntConstStratifiedRule(rewriter: Rewriter, intValueCache: IntValueCache) extends StratifiedRule[BigInt] { | ||
|
||
def isApplicable(ex: TlaEx, scope: RewriterScope): Boolean = ex match { | ||
case ValEx(TlaInt(_)) => true | ||
case _ => false | ||
} | ||
|
||
def buildArena(ex: TlaEx)(startingScope: RewriterScope): (RewriterScope, ArenaCell, BigInt) = ex match { | ||
case ValEx(TlaInt(n)) => | ||
val (newArena, cell) = intValueCache.getOrCreate(startingScope.arena, n) | ||
(startingScope.copy(arena = newArena), cell, n) | ||
|
||
case _ => | ||
throw new RewriterException(getClass.getSimpleName + " is not applicable", ex) | ||
} | ||
|
||
def addConstraints(scope: RewriterScope, cell: ArenaCell, auxData: BigInt): Unit = { | ||
rewriter.assert(tla.eql(cell.toBuilder, tla.int(auxData))) | ||
} | ||
|
||
} |
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
53 changes: 53 additions & 0 deletions
53
tla-bmcmt/src/test/scala/at/forsyte/apalache/tla/bmcmt/stratifiedRules/IntRewriterTest.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,53 @@ | ||
package at.forsyte.apalache.tla.bmcmt.stratifiedRules | ||
|
||
import at.forsyte.apalache.tla.lir.{IntT1, NameEx, SetT1, Typed} | ||
import at.forsyte.apalache.tla.types.tla | ||
import org.junit.runner.RunWith | ||
import org.scalatest.BeforeAndAfterEach | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatestplus.junit.JUnitRunner | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
class IntRewriterTest extends AnyFunSuite with BeforeAndAfterEach { | ||
|
||
var rewriter: TestingRewriter = TestingRewriter(Map.empty) | ||
|
||
override def beforeEach(): Unit = { | ||
rewriter = TestingRewriter(Map.empty) | ||
} | ||
|
||
test("Integer set rewriting rule: Nat ~~> $C$3 / Int ~~> $C$4") { | ||
val natSetEx = tla.natSet() | ||
val intSetEx = tla.intSet() | ||
|
||
val cellNat = rewriter.rewrite(natSetEx)(RewriterScope.initial())._2 | ||
assert(cellNat.toNameEx.eqTyped(NameEx("$C$3")(Typed(SetT1(IntT1))))) | ||
|
||
val cellInt = rewriter.rewrite(intSetEx)(RewriterScope.initial())._2 | ||
assert(cellInt.toNameEx.eqTyped(NameEx("$C$4")(Typed(SetT1(IntT1))))) | ||
} | ||
|
||
test("Integer constant rewriting rule.") { | ||
val i1 = tla.int(0) | ||
val i2 = tla.int(42) | ||
|
||
val initScope = RewriterScope.initial() | ||
|
||
val (scope1, cell1) = rewriter.rewrite(i1)(initScope) | ||
|
||
assert( | ||
rewriter.assertSeq == Seq(tla.eql(cell1.toBuilder, i1).build) | ||
) | ||
|
||
val (_, cell2) = rewriter.rewrite(i2)(scope1) | ||
|
||
assert( | ||
rewriter.assertSeq == Seq( | ||
tla.eql(cell1.toBuilder, i1), | ||
tla.eql(cell2.toBuilder, i2), | ||
).map(_.build) | ||
) | ||
|
||
} | ||
|
||
} |