From f922e90f50f1f9bd9940246181679873bb516a13 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 2 Nov 2026 21:10:52 +0100 Subject: [PATCH] Resolve `Has` with a closed type family MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- .../src/Squeal/PostgreSQL/Type/Alias.hs | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/squeal-postgresql/src/Squeal/PostgreSQL/Type/Alias.hs b/squeal-postgresql/src/Squeal/PostgreSQL/Type/Alias.hs index a7c1ce01..15932d80 100644 --- a/squeal-postgresql/src/Squeal/PostgreSQL/Type/Alias.hs +++ b/squeal-postgresql/src/Squeal/PostgreSQL/Type/Alias.hs @@ -195,15 +195,34 @@ type HasUnique alias fields field = fields ~ '[alias ::: field] -- @fields@ has a field of @alias ::: field@, inferring @field@ -- from @alias@ and @fields@. class (KnownSymbol alias) => Has (alias :: Symbol) (fields :: [(Symbol, kind)]) (field :: kind) | alias fields -> field --- having these instances forces 'Has' to inspect 'alias' and 'fields' and thereby fail before delegating to --- 'HasErr', which means 'Has' shows up in error messages instead of 'HasErr' -instance {-# OVERLAPPING #-} (KnownSymbol alias, HasErr (alias ::: field0 ': fields) alias (alias ::: field0 ': fields) field1) - => Has alias (alias ::: field0 ': fields) field1 -instance {-# OVERLAPPABLE #-} (KnownSymbol alias, HasErr (field' ': fields) alias (field' ': fields) field) +-- The lookup is a closed type family rather than a chain of overlapping +-- instances: resolving the k-th field then needs constant-size evidence +-- (a 'KnownSymbol' dictionary) instead of a chain of k dictionaries that +-- each mention the whole list. +-- 'HasLookup' reports the same errors as 'HasErr' once the lookup is done. +-- The instances match on the list’s shape, as the overlapping ones did, so +-- a 'Has' constraint over a list variable in a signature stays as written +-- rather than being simplified through the instance. +instance (KnownSymbol alias, HasLookup alias (field' ': fields) (Lookup alias (field' ': fields)) field) => Has alias (field' ': fields) field -instance (KnownSymbol alias, HasErr '[] alias '[] field) +instance (KnownSymbol alias, HasLookup alias '[] 'Nothing field) => Has alias '[] field +-- | @Lookup alias fields@ is the field under @alias@, if there is one. +type family Lookup (alias :: Symbol) (fields :: [(Symbol, kind)]) :: Maybe kind where + Lookup alias (alias ::: field ': _) = 'Just field + Lookup alias (_ ': fields) = Lookup alias fields + Lookup _ '[] = 'Nothing + +-- | Turns the result of 'Lookup' into the field, or into the error +-- 'HasErr' would report. +class HasLookup (alias :: Symbol) (fields :: [(Symbol, kind)]) (found :: Maybe kind) (field :: kind) + | alias fields found -> field +instance (field0 ~ field1, MismatchError alias fields field0 field1) + => HasLookup alias fields ('Just field0) field1 +instance (LookupFailedError alias fields, field ~ Any) + => HasLookup alias fields 'Nothing field + {- | 'HasErr' is like `Has` except it also retains the original list of fields being searched, so that error messages are more useful.