liveBook · Manning

Associated type

As explained in Functional Design and Architecture: Examples in Haskell

An associated type is a type member declared within a type class or similar abstraction and determined by its implementing type.

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

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains declarations, concrete instances, and generic use, while stopping short of a broader formal treatment of type-level programming.
Examples
Demonstrates MeteorEntity and CometEntity instances with distinct keys, stored values, conversions, and a generic loadEntity function.
What it assumes
Assumes familiarity with Haskell type classes and algebraic data types while clarifying how associated representations behave in generic signatures.
What you can do after
Build it from scratch by declaring entity-specific associated types and implementing generic key, value, and conversion operations.
How it connects
Connects associated types with type classes, algebraic data types, functional dependencies, type families, and type-level programming.

Also explained in

Compare how 2 books explain associated type →

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.