Skip to content

[inferno-core] Stop using Set for constraint accumulation, refactor infer a bit - #206

Merged
ngua merged 8 commits into
mainfrom
rory-typechecker-stop-using-set-for-no-reason
Apr 8, 2026
Merged

ngua merged 8 commits into
mainfrom
rory-typechecker-stop-using-set-for-no-reason

Conversation

@ngua

@ngua ngua commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

There's no reason to use a Set for accumulating constraints:

  • A Constraint is an Either (InfernoType, InfernoType, [TypeError SourcePos]) (Location SourcePos, TypeClass). Every Set operation had to compare deeply nested structures (walking entire type trees and error lists with source positions) to maintain ordering
  • The constraint solver (processes constraints as a list via partitionEithers, and doesn't depend on uniqueness or ordering
  • Constraints aren't duplicate in practice as they carry a [TypeError SourcePos] with unique source locations. Even if somehow there was a duplicate type constraint, the solver can handle it: after the first unification binds the type variable, the duplicate becomes e.g. Int ~ Int and succeeds (empty substitution)
  • This is a basic accumulate-then-consume pattern; the asymptotics of a Seq are much better here than a Set

Some other changes:

  • Because I touched many of the same locations, I decided to refactor the main inference function a bit. Instead of anonymous tuple projections, I created small helper records to improve readability
  • These helpers will also be used when we do the rewrite from naive substitution-map approach to union-find for constraint solving

NOTE: Inferno tests pass themselves but need to test this elsewhere too

@ngua
ngua marked this pull request as draft April 6, 2026 09:39
@ngua
ngua marked this pull request as ready for review April 8, 2026 04:06
@ngua ngua changed the title [inferno-core] Stop using Set for constraint accumulation, refactor inference function a bit [inferno-core] Stop using Set for constraint accumulation, refactor inferExpr a bit Apr 8, 2026
@ngua ngua changed the title [inferno-core] Stop using Set for constraint accumulation, refactor inferExpr a bit [inferno-core] Stop using Set for constraint accumulation, refactor infer a bit Apr 8, 2026
ngua added 2 commits April 8, 2026 12:19
…b.com:plow-technologies/inferno into rory-typechecker-stop-using-set-for-no-reason
@ngua
ngua merged commit c37536e into main Apr 8, 2026
1 check passed
@ngua
ngua deleted the rory-typechecker-stop-using-set-for-no-reason branch April 8, 2026 08:56
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.

2 participants