Skip to content
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

Translate Quint open row tuples to sparse tuples #2634

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .unreleased/bug-fixes/2634-quint-open-tuple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug when translating certain Quint tuple types, see #2634
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package at.forsyte.apalache.io.quint
import at.forsyte.apalache.tla.lir._
import com.typesafe.scalalogging.LazyLogging

import scala.collection.immutable.SortedMap
import scala.collection.mutable

// Convert a QuintType into a TlaType1
Expand Down Expand Up @@ -36,24 +37,24 @@ private class QuintTypeConverter extends LazyLogging {

import QuintType._

private def rowToTupleElems(row: Row): Seq[TlaType1] = {
// Since we have to concat lists here, which can be expensive, we use an
// accumulator to benefit from tail call optimization. This is purely a precaution.
def aux(r: Row, acc0: Seq[TlaType1]): Seq[TlaType1] = r match {
// TODO Update with support for row-based tuples: https://github.com/informalsystems/apalache/issues/1591
// Row variables in tuples are not currently supported. But we will proceed assuming that this is an
// over generalization, and that we can safely treat the tuple as a closed tuple type.
case Row.Var(_) =>
logger.debug(
s"Found open row variable in quint tuple with fields $row. Polymorphic tuples are not supported, but we will proceed assuming the tuple can be treated as a closed type.")
acc0
case Row.Nil() => acc0
private def rowToTupleT1(row: Row): TlaType1 = {
// `aux(r, acc)` is a `(fields, other)`
// where `fields` is a map from tuple indices to tuple types, and
// `other` is either `Some(var)` for an open row or `None` for a closed row.
//
// `acc` is used as an accumulator to enable tail recursion
def aux(r: Row, acc0: SortedMap[Int, TlaType1]): (SortedMap[Int, TlaType1], Option[VarT1]) = r match {
case Row.Nil() => (acc0, None)
case Row.Var(v) => (acc0, Some(VarT1(getVarNo(v))))
case Row.Cell(fields, other) =>
val acc1 = fields.map(f => convert(f.fieldType)) ++ acc0
val acc1 = acc0 ++ fields.map { f => (f.fieldName.toInt + 1) -> convert(f.fieldType) }
aux(other, acc1)
}

aux(row, List())
aux(row, SortedMap()) match {
case (fields, None) => TupT1(fields.map(_._2).toSeq: _*)
case (fields, Some(_)) => SparseTupT1(fields)
}
}

private def rowToRowT1(row: Row): RowT1 = {
Expand Down Expand Up @@ -130,7 +131,7 @@ private class QuintTypeConverter extends LazyLogging {
case QuintSeqT(elem) => SeqT1(convert(elem))
case QuintFunT(arg, res) => FunT1(convert(arg), convert(res))
case QuintOperT(args, res) => OperT1(args.map(convert), convert(res))
case QuintTupleT(row) => TupT1(rowToTupleElems(row): _*)
case QuintTupleT(row) => rowToTupleT1(row)
case QuintRecordT(row) => RecRowT1(rowToRowT1(row))
case QuintUnionT(_, variants) => VariantT1(unionToRowT1(variants))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import org.junit.runner.RunWith
import org.scalatest.funsuite.AnyFunSuite
import org.scalatestplus.junit.JUnitRunner
import at.forsyte.apalache.io.quint.QuintType._
import at.forsyte.apalache.tla.lir.{FunT1, IntT1, RecRowT1, RowT1, StrT1, TlaType1, TupT1, VarT1, VariantT1}
import at.forsyte.apalache.tla.lir.{
FunT1, IntT1, RecRowT1, RowT1, SparseTupT1, StrT1, TlaType1, TupT1, VarT1, VariantT1,
}

/**
* Tests the conversion of quint types, represented as QuintTypes, into TlaType1
Expand All @@ -27,15 +29,15 @@ class TestQuintTypes extends AnyFunSuite {
test("Quint tuple types are converted correctly") {
val tuple =
// i.e.: (int, string)
QuintTupleT(Row.Cell(List(RecordField("1", QuintIntT()), RecordField("2", QuintStrT())), Row.Nil()))
QuintTupleT(Row.Cell(List(RecordField("0", QuintIntT()), RecordField("1", QuintStrT())), Row.Nil()))
assert(translate(tuple) == TupT1(IntT1, StrT1))
}

test("Polymorphic Quint tuples types are converted to plain, closed tuples") {
test("Polymorphic Quint tuples types are converted to sparse tuples") {
val tuple =
// i.e.: (int, string | r)
QuintTupleT(Row.Cell(List(RecordField("1", QuintIntT()), RecordField("2", QuintStrT())), Row.Var("r")))
assert(translate(tuple) == TupT1(IntT1, StrT1))
QuintTupleT(Row.Cell(List(RecordField("0", QuintIntT()), RecordField("1", QuintStrT())), Row.Var("r")))
assert(translate(tuple) == SparseTupT1(1 -> IntT1, 2 -> StrT1))
}

test("Open Quint record types are converted into open TlaType1 records") {
Expand Down