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

Variants in the type checker #1847

Merged
merged 28 commits into from
Jun 9, 2022
Merged

Variants in the type checker #1847

merged 28 commits into from
Jun 9, 2022

Conversation

konnov
Copy link
Collaborator

@konnov konnov commented Jun 1, 2022

This PR implements support for the variant type. On the way of implementing it, I have found the proposed syntax too inconvenient to work with, both at the user level and the implementation level. I have refactored it, similar to what was suggested earlier by @shonfeder. This will need an update to the ADRs in follow-up PRs. What is done here:

  • Update the type parser to support variant types that look like: Variant1(Int) | Variant2({ value: Int, success: Bool}). See DefaultType1Parser.
  • Change the Variants operators, see Variants.tla.
  • Refactor SanyImporter to support the new API.
  • Translate the Variants operators in ToEtcExpr, which implements the type checking rules.
  • Tests added for any new code
  • Ran make fmt-fix (or had formatting run automatically on all files edited)
  • Documentation added for any new functionality
  • Entries added to ./unreleased/ for any new functionality

@konnov konnov marked this pull request as ready for review June 2, 2022 07:16
@konnov konnov requested a review from bugarela June 2, 2022 07:16
@codecov-commenter
Copy link

codecov-commenter commented Jun 2, 2022

Codecov Report

Merging #1847 (59625d8) into unstable (eeb0d20) will increase coverage by 0.07%.
The diff coverage is 96.82%.

@@             Coverage Diff              @@
##           unstable    #1847      +/-   ##
============================================
+ Coverage     76.69%   76.76%   +0.07%     
============================================
  Files           391      392       +1     
  Lines         12005    12038      +33     
  Branches        550      579      +29     
============================================
+ Hits           9207     9241      +34     
+ Misses         2798     2797       -1     
Impacted Files Coverage Δ
...at/forsyte/apalache/tla/lir/oper/VariantOper.scala 92.30% <92.30%> (ø)
...forsyte/apalache/tla/typecheck/etc/ToEtcExpr.scala 94.92% <96.29%> (+0.09%) ⬆️
...lache/io/typecheck/parser/DefaultType1Parser.scala 95.45% <100.00%> (-0.55%) ⬇️
.../at/forsyte/apalache/tla/imp/StandardLibrary.scala 100.00% <100.00%> (ø)
...in/scala/at/forsyte/apalache/tla/lir/Builder.scala 99.11% <100.00%> (+0.03%) ⬆️
...n/scala/at/forsyte/apalache/tla/lir/TlaType1.scala 93.42% <100.00%> (+0.64%) ⬆️
...he/io/annotations/parser/CommentPreprocessor.scala 96.70% <0.00%> (+1.09%) ⬆️
...a/at/forsyte/apalache/tla/lir/TlaLevelFinder.scala 100.00% <0.00%> (+2.63%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eeb0d20...59625d8. Read the comment docs.

Copy link
Contributor

@shonfeder shonfeder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Some small changes suggested and some questions where I'm unclear, but overall awesome, and I'm psyched to see this landing :D

test/tla/TestReqAckVariants.tla Show resolved Hide resolved
src/tla/Variants.tla Outdated Show resolved Hide resolved
src/tla/Variants.tla Outdated Show resolved Hide resolved
src/tla/Variants.tla Outdated Show resolved Hide resolved
src/tla/Variants.tla Outdated Show resolved Hide resolved
@@ -110,6 +110,23 @@ class TestAnnotationParser extends AnyFunSuite with Checkers {
.map(r => fail("Expected a failure. Found: " + r))
}

test("parse variants in type aliases") {
val extractedText =
"""MESSAGE =""" + "\n" + """ Req({ ask: Int }) """ + "\n" + """ | Ack({ success: Bool })"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem like this could be a lot clearer using a multi-line string, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. The problem is that stripMargin is using | as a delimiter. So the tests were breaking. I could not find a better solution :(

Copy link
Contributor

@shonfeder shonfeder Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see! I think we can do

Suggested change
"""MESSAGE =""" + "\n" + """ Req({ ask: Int }) """ + "\n" + """ | Ack({ success: Bool })"""
"""#MESSAGE =
#| Req({ ask: Int })
#| Ack({ success: Bool })""".stripMargin('#')

Or using whatever char might be better in place of #.

I'd suggest making this change improves readability enough that it would be worth making the change here, but i won't block on it.

Copy link
Collaborator

@bugarela bugarela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this new syntax is awesome 😄

konnov and others added 4 commits June 9, 2022 09:46
Co-authored-by: Shon Feder <shon@informal.systems>
Co-authored-by: Gabriela Moreira <gabrielamoreiramafra@gmail.com>
@konnov konnov enabled auto-merge June 9, 2022 12:17
@@ -156,6 +157,15 @@ class TestCommentPreprocessor extends AnyFunSuite with Checkers {
hasAnnotationsWhenNonEmpty("""(* aaa *)""")
}

test("accept pipe") {
val extractedText =
"""@typeAlias: MESSAGE = { tag: "req", ask: Int }""" + "\n" + """ | { tag: "ack", success: Bool };"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also be cleaned up here using stripMargin

@konnov konnov merged commit 68342fc into unstable Jun 9, 2022
@konnov konnov deleted the ik/variants1777 branch June 9, 2022 13:53
@apalache-bot apalache-bot mentioned this pull request Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants