Skip to content

Resolve Has with a closed type family - #366

Merged
echatav merged 1 commit into
morphismtech:devfrom
gasi:has-closed-type-family
Nov 5, 2026
Merged

echatav merged 1 commit into
morphismtech:devfrom
gasi:has-closed-type-family

Conversation

@gasi

@gasi gasi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Why

Has resolves every table and column reference through a chain of overlapping instances: finding the k-th field builds k dictionaries whose types each carry the whole list, and every reference in every statement repeats that work. In an application with a large schema, type checking these lookups is one of the most expensive parts of compiling its modules of statements.

What

  • Has now looks the field up with a closed type family, Lookup :: Symbol -> [(Symbol, k)] -> Maybe k, and a two-instance class, HasLookup, turns the result into the field or into the existing error. Evidence for Has is constant-size (a KnownSymbol dictionary) instead of a chain of k dictionaries.
  • The Has instances still match on the list’s shape (cons or nil), as the overlapping ones did. A single catch-all instance would make GHC simplify Has constraints over a list variable in user signatures through the instance (-Wsimplifiable-class-constraints), which changes inference for existing code.
  • HasErr, MismatchError, LookupFailedError and the pretty-printing families are unchanged and still exported; a missing or mismatched field reports the same messages.

Effect

Measured on a production application (713 modules, a 23-table schema, GHC 9.6.7, -O0, -j4, M1 Max, clean library build, one run each):

before after
wall time 300 s 248 s (−17 %)
peak memory 19.7 GB 15.9 GB (−19 %)

On a 233-module extract of it, the type checker of the largest storage module went from 11.1 GB to 7.5 GB allocated. No change at run time.

How tested

  • The application above builds against this branch, and its test suite passes (4,925 examples). The extract’s HTTP server passes a 116-case conformance suite and a differential fuzzer against an independent implementation, with no differences.
  • Not run: this repository’s own test suite and doctests. I’d be glad to add a test or adjust anything to fit.

`Has` found a field through a chain of overlapping instances, one per
list element.  Looking up the k-th table or column built k dictionaries,
each of whose types carried the whole list, and every column reference
in every statement repeated that work.  In a large schema this makes
the type checker one of the most expensive parts of compiling a module
of statements.

Look the field up with a closed type family, `Lookup`, instead, and
turn its result into the field, or into the existing error, with a
small class, `HasLookup`.  Evidence for `Has` is now constant-size.
The `Has` instances still match on the list’s shape, so a `Has`
constraint over a list variable in a signature isn’t simplified
through an instance.  `HasErr` and the error families are unchanged,
and so are the messages for a missing or mismatched field.

On a 233-module application with a 23-table schema (GHC 9.6.7, -O0),
a clean build took 14 % less wall time (129 s to 111 s) and 5 % less
peak memory; in its largest storage module the type checker went from
38 s and 11.1 GB allocated to 23 s and 7.5 GB.
@echatav

echatav commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thanks! Looks good to me.

@echatav
echatav self-requested a review November 5, 2026 16:39

@echatav echatav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@echatav
echatav merged commit cc7a783 into morphismtech:dev Nov 5, 2026
3 checks passed
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