Skip to content

Fix #3965: Correct equality for higher-kinded types #3970

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

Closed
wants to merge 8 commits into from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Feb 6, 2018

What happened was that a higher-kinded type was added repeatedly
to a constraint. Because equality of higher-kinded types was "broken",
the compiler did not realize that the type had already been added.
"Broken" means: Two isomorphic instances of the same higher kinded type
such as [X] => C[X] were treated as different.

For similar reasons the equality check in monitoredSubtype did not
detect loops involving higher-kinded types.

To fix the issue, rework hashing and equality so that two isomorphic types
are identified even if they are dependent (i.e. have back edges from a
BoundType such as ParamRef or RecThis to its LambdaType or RecType
binder.

While we are at it, also use this for hash-consing method and poly types.

Aside: scalac also uses the naive version of type equality that treats PolyTypes
as generative. But it does not have the same issues because it does not use PolyTypes
as higher-kinded types. I believe similar issues could be engineered by making up
type lambdas using refinement types and putting these in certain F-bounds. But so far nobody
seems to have pushed things far enough to notice.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Added" instead of "Add")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

@odersky
Copy link
Contributor Author

odersky commented Feb 7, 2018

test performance please

@dottybot
Copy link
Member

dottybot commented Feb 7, 2018

performance test scheduled: 1 job(s) in queue, 1 running.

This is a first step of a larger refactoring, where hashing and equality need
to have algorithms that detect that isomorphic BindingTypes are equal.

Without this, there is no good way to deal with recursive higher-kinded types,
as we cannot detect that a type has already been added to a constraint or that
a subtype test was already performed.
Make hashes structural for dependent types. Two isomorphic types
should give the same hash even if there are dependencies to BindingTypes.
`equals` of two isomorphic types should return true,
even if they are dependencies to BindingTypes.
What happened was that a higher-kinded type was added repeatedly
to a constraint. Because equality of higher-kinded types was broken,
the compiler did not realize that the type had already been added.
@odersky odersky changed the title Fix #3965: Towards correct equality for higher-kinded types [WIP] Fix #3965: Correct equality for higher-kinded types Feb 7, 2018
@odersky
Copy link
Contributor Author

odersky commented Feb 7, 2018

test performance please

@dottybot
Copy link
Member

dottybot commented Feb 7, 2018

performance test scheduled: 1 job(s) in queue, 1 running.

else tps2.nonEmpty && equals(tps1.head, tps2.head) && equals(tps1.tail, tps2.tail)
}

final def equalBinders(tp1: BindingType, tp2: BindingType): Boolean =
Copy link
Member

Choose a reason for hiding this comment

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

This method is unused

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, there are two calls in Types.

@odersky odersky requested a review from allanrenucci February 7, 2018 19:39
@dottybot
Copy link
Member

dottybot commented Feb 7, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/3970/ to see the changes.

Benchmarks is based on merging with master (39980f3)

Let's see whether this helps combat the observed performance hit.
@odersky
Copy link
Contributor Author

odersky commented Feb 7, 2018

test performance please

@dottybot
Copy link
Member

dottybot commented Feb 7, 2018

performance test scheduled: 1 job(s) in queue, 1 running.

@dottybot
Copy link
Member

dottybot commented Feb 7, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/3970/ to see the changes.

Benchmarks is based on merging with master (39980f3)

1 similar comment
@dottybot
Copy link
Member

dottybot commented Feb 7, 2018

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/3970/ to see the changes.

Benchmarks is based on merging with master (39980f3)

odersky added a commit to dotty-staging/dotty that referenced this pull request Feb 8, 2018
Since correct hashing under binders seems to be very expensive (see performance
data for scala#3970), let's try have fewer types that require this.
@odersky
Copy link
Contributor Author

odersky commented Feb 10, 2018

Subsumed by #3978

@odersky odersky closed this Feb 10, 2018
odersky added a commit to dotty-staging/dotty that referenced this pull request Feb 19, 2018
Since correct hashing under binders seems to be very expensive (see performance
data for scala#3970), let's try have fewer types that require this.
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.

3 participants