Skip to content

Commit

Permalink
Merge pull request #2916 from konnov/igor/quint-generate
Browse files Browse the repository at this point in the history
Translate Quint `generate`
  • Loading branch information
konnov authored Aug 14, 2024
2 parents 6ba3a45 + 4e48675 commit 9e6071b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions .unreleased/features/translate-generate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Translate Quint's generate into `Apalache!Gen` (#2916)
29 changes: 28 additions & 1 deletion tla-io/src/main/scala/at/forsyte/apalache/io/quint/Quint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,13 @@ class Quint(quintOutput: QuintOutput) {

// Convert Quint's nondet variable binding
//
// val nondet name = oneOf(domain); scope
// nondet name = oneOf(domain); scope
// ~~>
// \E name \in domain: scope
//
// nondet name = generate(sz, typeSet); scope
// ~~>
// \E name \in { Apalache!Gen(sz) }: scope
private val nondetBinding: (QuintDef.QuintOpDef, QuintEx) => NullaryOpReader[TBuilderInstruction] = {
case (QuintDef.QuintOpDef(_, name, "nondet", QuintApp(id, "oneOf", Seq(domain))), scope) =>
val elemType = typeConv.convert(types(id).typ)
Expand All @@ -666,6 +670,19 @@ class Quint(quintOutput: QuintOutput) {
tlaDomain <- tlaExpression(domain)
tlaScope <- tlaExpression(scope)
} yield tla.exists(tlaName, tlaDomain, tlaScope)

case (QuintDef.QuintOpDef(_, name, "nondet", QuintApp(id, "generate", Seq(bound, _))), scope) =>
val elemType = typeConv.convert(types(id).typ)
val boundIntConst = intFromExpr(bound)
if (boundIntConst.isEmpty) {
throw new QuintIRParseError(s"nondet $name = generate($bound) ... expects an integer constant")
}
val genExpr = tla.enumSet(tla.gen(boundIntConst.get, elemType))
val tlaName = tla.name(name, elemType)
for {
tlaScope <- tlaExpression(scope)
} yield tla.exists(tlaName, genExpr, tlaScope)

case invalidValue =>
throw new QuintIRParseError(s"nondet keyword used to bind invalid value ${invalidValue}")
}
Expand Down Expand Up @@ -804,4 +821,14 @@ class Quint(quintOutput: QuintOutput) {
.reverse // Return the declarations to their original order
}
} yield TlaModule(module.name, declarations)

private def intFromExpr(expr: QuintEx): Option[BigInt] = {
expr match {
case QuintInt(_, value) =>
Some(value)

case _ =>
None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,15 @@ class TestQuintEx extends AnyFunSuite {
val chooseSomeFromIntSet = app("chooseSome", intSet)(QuintIntT())
val oneOfSet = app("oneOf", intSet)(QuintIntT())
val nondetBinding =
e(QuintLet(uid, d(QuintDef.QuintOpDef(uid, "n", "nondet", oneOfSet), QuintIntT()), nIsGreaterThan0), QuintIntT())
e(QuintLet(uid, d(QuintDef.QuintOpDef(uid, "n", "nondet", oneOfSet), QuintIntT()), nIsGreaterThan0), QuintBoolT())
val generateSet = app("generate", _42, app("Set", _42)(QuintSetT(QuintIntT())))(QuintSetT(QuintIntT()))
val nondetGenerateId = uid
val appGenSet =
app("eq", e(QuintName(uid, "S"), QuintSetT(QuintIntT())), app("Set")(QuintSetT(QuintIntT())))(QuintBoolT())
val nondetGenerate =
e(QuintLet(uid,
d(QuintDef.QuintOpDef(nondetGenerateId, "S", "nondet", generateSet),
QuintOperT(Seq(), QuintSetT(QuintIntT()))), appGenSet), QuintBoolT())
// Requires ID registered with type
val selectGreaterThanZero = app("select", intList, intIsGreaterThanZero)(QuintSeqT(QuintIntT()))
val addOne = app("iadd", name, _1)(QuintIntT())
Expand Down Expand Up @@ -710,10 +718,14 @@ class TestQuintEx extends AnyFunSuite {
assert(convert(Q.app("put", Q.intMap, Q._3, Q._42)(intMapT)) == expected)
}

test("can convert nondet bindings") {
test("can convert nondet...oneOf") {
assert(convert(Q.nondetBinding) == "∃n ∈ {1, 2, 3}: (n > 0)")
}

test("can convert nondet...generate") {
assert(convert(Q.nondetGenerate) == "∃S ∈ {Apalache!Gen(42)}: (S = {})")
}

test("can convert let binding with reference to name in scope") {
assert(convert(Q.letNbe42inNisGreaterThan0) == "LET n ≜ 42 IN n() > 0")
}
Expand Down

0 comments on commit 9e6071b

Please sign in to comment.