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

Replace unification-fd with a custom implementation of unification #1802

Merged
merged 50 commits into from
Apr 27, 2024

Conversation

byorgey
Copy link
Member

@byorgey byorgey commented Apr 24, 2024

Closes #1661; towards #154.

unification-fd is very powerful, and extremely fast, but it was written a long time ago and its age shows. It was not possible to incorporate it into our effects system in a nice way, necessitating the use of concrete monad transformers in the typechecking code. In addition it is impossible to customize, and we have been contemplating new type system features such as #153 and #154 that turn out to require hooking into the way the unification algorithm works (see #154 (comment) for more details).

This PR thus removes the dependency on unification-fd and implements our own version of unification. It is not quite as fast as unification-fd but I consider the slowdown acceptable in order to gain e.g. recursive types. And of course there is also room to optimize it.

The custom UTerm from unification-fd is replaced with the standard Free (free monad) construction from the free package, and the custom Fix from unification-fd is replaced with the one from data-fix.

We also get rid of the unifyCheck function, which used to be a quick short-circuiting way to check whether two types definitely did not unify or might unify, allowing us to give better error messages more quickly. Now, the =:= unification operator itself just does this.

Replace with modern standard implementations of `Free` and `Fix`.
Now getting weird compile-time errors from some quasiquotes
Now it compiles but almost everything in the test suite is failing =)
Composition of substitutions is not commutative!
byorgey and others added 2 commits April 24, 2024 19:15
Co-authored-by: Restyled.io <commits@restyled.io>
@byorgey byorgey marked this pull request as draft April 25, 2024 00:22
@byorgey byorgey marked this pull request as ready for review April 25, 2024 23:39
@byorgey byorgey requested a review from kostmo April 25, 2024 23:39
Copy link
Member

@kostmo kostmo left a comment

Choose a reason for hiding this comment

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

Out of my depth, but exciting nonetheless!

Comment on lines +122 to +123
-- unification is subtly wrong; fortunately, a single integration test
-- in the Swarm test suite failed, leading to discovering the bug.
Copy link
Member

Choose a reason for hiding this comment

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

Which test, out of curiosity?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was Challenges/Sliding Puzzles/_sliding-puzzle/solution.sw that failed to typecheck, specifically the getInitialBlankDestination function. Something about the way it involved inferring the type of a higher-order function that took tuple projections as an argument tickled the type inference algorithm in just the right way to trigger the bug. So thanks for writing that code! 😄

@byorgey byorgey added the merge me Trigger the merge process of the Pull request. label Apr 27, 2024
@mergify mergify bot merged commit 917ee5c into main Apr 27, 2024
9 checks passed
@mergify mergify bot deleted the refactor/custom-unification branch April 27, 2024 21:53
@byorgey byorgey mentioned this pull request Jun 1, 2024
@byorgey byorgey added the CHANGELOG Once merged, this PR should be highlighted in the changelog for the next release. label Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CHANGELOG Once merged, this PR should be highlighted in the changelog for the next release. merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom implementation of unification
2 participants