An associated type lets a type class declare type members whose concrete definitions belong to each implementing type. This keeps related types attached to the abstraction instead of passing every representation as an unrelated parameter. section 11.3 section 11.2.3
A type-safe key–value interface uses DBEntity db entity to associate each entity with its key and stored-value types. The entity tag selects distinct associated ADTs, so MeteorEntity can use MeteorKey and KVDBMeteor while CometEntity uses different representations. Generic operations can mention KeyEntity entity and ValueEntity entity without knowing those concrete definitions. section 11.2.3 section 11.2.4
Entity-specific database types
A DBEntity instance for MeteorEntity declares KeyEntity MeteorEntity = MeteorKey MeteorID and ValueEntity MeteorEntity = KVDBMeteor. A second instance can declare CometKey and KVDBComet instead. loadEntity then loads a key, decodes the associated database value, and converts it into the requested domain type through AsValueEntity. section 11.2.3 section 11.2.4
class DBEntity db entity | entity -> db where
toDBKey #1
:: KeyEntity entity -> KVDBKey
toDBValue #2
:: ValueEntity entity -> KVDBValue
fromDBValue #3
:: KVDBValue -> Maybe (ValueEntity entity)
Inspect the DBEntity declaration to see how associated KeyEntity and ValueEntity members are declared alongside the class operations. section 11.2.4
The design separates domain types from database types while preserving compile-time relationships among the database, entity, key, and value. Adding another entity extends the instances and conversions rather than changing the generic loading mechanism. section 11.2.4

