liveBook · Manning

Closed type family

As explained in Functional Design and Architecture: Examples in Haskell

A closed type family is a type family whose equations are fixed in its declaration and cannot be extended by separate instances.

A closed type family fixes every equation in its declaration. Its supported cases form a finite compile-time mapping, so extending it requires editing the family itself rather than adding an external instance. section 12.1.3

Selecting representation types

TypeSelector maps a domain marker and field marker to a concrete type: Domain' Int' becomes Int, SQLite' Int' becomes INTEGER, and Postgres' Int' becomes PgInt. The declaration lists all supported combinations, including identifiers, numeric values, and timestamps. section 12.1.3

Listing 12.2 Type family for picking types in compile time
type family TypeSelector typeSet t
 where
   TypeSelector Domain'   Id'        = Int
   TypeSelector Domain'   Int'       = Int
   TypeSelector Domain'   Float'     = Float
   TypeSelector Domain'   Timestamp' = UTCTime
 
   TypeSelector SQLite'   Id'        = ID
   TypeSelector SQLite'   Int'       = INTEGER
   TypeSelector SQLite'   Float'     = FLOAT
   TypeSelector SQLite'   Timestamp' = TIMESTAMP
 
   TypeSelector Postgres' Id'        = PgPrimaryKey
   TypeSelector Postgres' Int'       = PgInt
   TypeSelector Postgres' Float'     = PgFloat
   TypeSelector Postgres' Timestamp' = PgTimestamp

Inspect the complete TypeSelector equation table and its explicitly bounded set of supported type combinations. section 12.1.3

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains declaration equations and extension limits, but stops short of broader closed-type-family techniques.
Examples
Demonstrates TypeSelector mappings for domain, SQLite, and Postgres meteor-record field types.
What it assumes
Assumes familiarity with type-level code and the HKD pattern while showing the mapping mechanism through concrete data representations.
What you can do after
Evaluate compile-time representation choices and recognize when adding support requires editing the central type family.
How it connects
Connects closed type families to the HKD pattern, type-level programming, polymorphic ADTs, and database models.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.