Associated type synonyms

As a unifying example for the next sections, we return to Chapter 6: Patterns of Generic Programming, where we created a List' type along with its type representation RList:

data List' a
  = Nil' | Cons' a (List' a)
  deriving (Show)

data U = U                 
  deriving (Show)

data Choice a b = L a | R b   
  deriving (Show)

data Combo a b = Combo a b     
  deriving (Show)

type RList a = Choice U (Combo a (List' a))

In addition to this, we defined functions fromL and toL to mediate between the type and representation:

  fromL :: List' a -> RList a
  toL   :: RList a -> List' a

We embedded this in the container type EP as follows:

data EP d r = EP {from_ :: (d -> r),
                 to_ ::  (r -> d)}

Using functional dependencies

Instead of the container type EP mentioned above, ...

Get Haskell Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.