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

Type unification for rows, new records, and variants #1646

Merged
merged 17 commits into from
Apr 26, 2022

Conversation

konnov
Copy link
Collaborator

@konnov konnov commented Apr 20, 2022

Closes #1622. This PR implements type unification for rows as part of the meta-issue #401. Unification for new records and variants is very simple: It delegates to row unification. The idea is described in ADR014. However, our unification is much simpler. We do not need kinds, scoped labels, etc.

This version does not require different options of a variant type to have compatible fields, as discussed in Section 5.4. Postponing this decision until implementing the rewriting rules in the model checker. Perhaps, we do not need this requirement.

  • 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
  • Entry added to UNRELEASED.md for any new functionality

@codecov-commenter
Copy link

codecov-commenter commented Apr 20, 2022

Codecov Report

Merging #1646 (684d3f8) into unstable (3168490) will increase coverage by 0.19%.
The diff coverage is 98.38%.

@@             Coverage Diff              @@
##           unstable    #1646      +/-   ##
============================================
+ Coverage     74.96%   75.15%   +0.19%     
============================================
  Files           358      358              
  Lines         11612    11654      +42     
  Branches        541      569      +28     
============================================
+ Hits           8705     8759      +54     
+ Misses         2907     2895      -12     
Impacted Files Coverage Δ
...rsyte/apalache/tla/typecheck/etc/TypeUnifier.scala 97.26% <98.00%> (+0.14%) ⬆️
...lache/io/typecheck/parser/DefaultType1Parser.scala 96.00% <100.00%> (-0.04%) ⬇️
...ain/scala/at/forsyte/apalache/tla/pp/Inliner.scala 92.18% <100.00%> (+0.12%) ⬆️
.../apalache/tla/typecheck/etc/ConstraintSolver.scala 97.22% <100.00%> (ø)
...te/apalache/tla/typecheck/etc/EtcTypeChecker.scala 83.55% <100.00%> (ø)
...n/scala/at/forsyte/apalache/tla/lir/TlaType1.scala 92.04% <100.00%> (+1.13%) ⬆️
.../forsyte/apalache/io/typecheck/parser/tokens.scala 93.10% <0.00%> (-3.45%) ⬇️
...he/io/annotations/parser/CommentPreprocessor.scala 96.70% <0.00%> (+1.09%) ⬆️
... and 1 more

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 3168490...684d3f8. Read the comment docs.

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.

I feel this is a little harder to follow than it should be. I had to look back at some previous PR to remember the relation between RecRow and Row. I still believe that introducing a different kind than TlaType1 would make this relation clearer in the code. I'm fine with keeping things like this for now, but I would like to try an approach with kinds on TNT and then, if it's clearer, we can copy it to apalache or something like that.

Copy link
Collaborator

@Kukovec Kukovec left a comment

Choose a reason for hiding this comment

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

👍

Comment on lines +85 to +86
val maxUsedVar = Math.max(genericType.usedNames.foldLeft(0)(Math.max), targetType.usedNames.foldLeft(0)(Math.max))
new TypeUnifier(new TypeVarPool(maxUsedVar + 1)).unify(Substitution.empty, genericType, targetType) match {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, was there a case where type-var name IDs were causing problems?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. We had it a month or two ago.

// the base case
(lvar, rvar) match {
case (None, None) =>
if (rfields.nonEmpty) None else Some(RowT1())
Copy link
Collaborator

Choose a reason for hiding this comment

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

As an aside, I really don't like how object RowT1 methods are all apply, because it makes it hard to track what is actually being constructed, since RowT1(), RowT1(_), RowT1(_,_) are all different methods that return a RowT1.

Comment on lines +290 to +297
if (
compute(lvar.getOrElse(RowT1()), RowT1(rfields, Some(tailVar))).isEmpty
|| compute(rvar.getOrElse(RowT1()), RowT1(lfields, Some(tailVar))).isEmpty
) {
None
} else {
// apply the computed substitution to obtain the whole row
asRow(Some(Substitution(solution).sub(RowT1(lfields, lvar))._1))
Copy link
Collaborator

Choose a reason for hiding this comment

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

The fact that compute mutates solution internally makes this really hard to understand. I'd at least appreciate a comment on that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, if you don't need the substitution, it does unify things. But since we need the resulting substitution, compute mutates the substitution pretty much everywhere.

@konnov konnov enabled auto-merge April 26, 2022 19:45
@konnov konnov merged commit 856075f into unstable Apr 26, 2022
This was referenced May 2, 2022
@shonfeder shonfeder deleted the ik/unification1622 branch October 23, 2023 13:13
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.

Extend type unification
4 participants