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

Add Comparator conversion for all types #4067

Merged
merged 68 commits into from
Feb 10, 2023

Conversation

Akirathan
Copy link
Member

@Akirathan Akirathan commented Jan 19, 2023

Pull Request Description

Add Comparator type class emulation for all types. Migrate all the types in stdlib to this new Comparator API. The main documentation is in Ordering.enso.

Fixes these pivotals:

Important Notes

  • The new Comparator API forces users to specify both equals and hash methods on their custom comparators.
  • All the compare_to overrides were replaced by definition of a custom ordered comparator.
  • All the call sites of x.compare_to y method were replaced with Ordering.compare x y.
    • Ordering.compare is essentially a shortcut for Comparable.from x . compare x y.
  • The default comparator for Any is Default_Unordered_Comparator, which just forwards to the builtin EqualsNode and HashCodeNode nodes.
  • For x, one can get its hash with Comparable.from x . hash x.
    • This makes hash as hidden as possible. There are no other public methods to get a hash code of an object.
  • Comparing x and y can be done either by Ordering.compare x y or Comparable.from x . compare x y instead of x.compare_to y.

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the
    Scala,
    Java,
    and
    Rust
    style guides.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed: Enso GUI was tested when built using BOTH
      ./run ide build and ./run ide watch.

@Akirathan Akirathan force-pushed the wip/akirathan/custom-eq-183945328 branch from e997c5f to 0d6560f Compare January 19, 2023 10:48
@Akirathan Akirathan force-pushed the wip/akirathan/custom-eq-183945328 branch from 0d6560f to a170e53 Compare January 19, 2023 10:51
- Revert changes in Any, so that we have still the old behavior
- Define new Any.=== , Any.<== and Any.>== operators as extension methods in Eq.enso
- Make Any.hash_code accessible as a temporary workaround
- Must not contain `self` keyword.
- We should remove Eq_Spec in the future.
@Akirathan Akirathan force-pushed the wip/akirathan/custom-eq-183945328 branch from fd14bb8 to 93ed516 Compare January 24, 2023 16:45
Copy link
Collaborator

@hubertp hubertp left a comment

Choose a reason for hiding this comment

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

Conditional approval based on our internal discussion.

Please file a follow up ticket for removing the need to import Standard.Base.Data.Any.Any when performing simple equality operation.

Copy link
Member

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

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

I am really glad to see this endeavor converging! I might have some suggestions for a change, but I believe it is more important to integrate this while we are green and only then solve problems that might appear anywhere.

- If hash(x) != hash(y) then x != y
- Consistency: if x == y then x == y for all the subsequent invocations.
- Symmetry: if x == y then y == x
- Reflexivity: x == x
Copy link
Member

Choose a reason for hiding this comment

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

Reflexivity should be enforced. Meta_is_same_object x y implies x == y.

- If x == y then hash(x) == hash(y)
- If hash(x) != hash(y) then x != y
- Consistency: if x == y then x == y for all the subsequent invocations.
- Symmetry: if x == y then y == x
Copy link
Member

Choose a reason for hiding this comment

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

Symmetry could be enforced. Just like we check hash code consistency we could also verify (from time to time) that x == y and y == x is consistent.

@Akirathan Akirathan added the CI: Clean build required CI runners will be cleaned before and after this PR is built. label Feb 9, 2023
@Akirathan Akirathan removed the CI: Clean build required CI runners will be cleaned before and after this PR is built. label Feb 9, 2023
@Akirathan Akirathan added the CI: Ready to merge This PR is eligible for automatic merge label Feb 9, 2023
@JaroslavTulach JaroslavTulach added the CI: Clean build required CI runners will be cleaned before and after this PR is built. label Feb 10, 2023
@mergify mergify bot merged commit 1f8511d into develop Feb 10, 2023
@mergify mergify bot deleted the wip/akirathan/custom-eq-183945328 branch February 10, 2023 09:22
mergify bot pushed a commit that referenced this pull request Feb 21, 2023
Critical performance improvements after #4067

# Important Notes
- Replace if-then-else expressions in `Any.==` with case expressions.
- Fix caching in `EqualsNode`.
- This includes fixing specializations, along with fallback guard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
--breaking Important: a change that will break a public API or user-facing behaviour -compiler CI: Clean build required CI runners will be cleaned before and after this PR is built. CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consistent ordering for <, >, <= and >= Custom eq & hash with consistency
5 participants