product-profunctors-0.5: product-profunctors

Safe HaskellNone
LanguageHaskell98

Data.Profunctor.Product.TH

Description

If you have a data declaration which is a polymorphic product, for example

data Foo a b c = Foo a b c

or

data Foo a b c = Foo { foo :: a, bar :: b, baz :: c }

then you can use Template Haskell to automatically derive the product-profunctor Default instances and product-profunctor "adaptor" with the following imports and splice:

import Data.Profunctor.Product (ProductProfunctor, p<n>)
import Data.Profunctor (dimap)
import Data.Profunctor.Product.Default (Default, def)

$(makeAdaptorAndInstance "pFoo" ''Foo)
  • <n> is the number of fields in your record, so for Foo <n> would be 3.
  • The adaptor for a type Foo is by convention called pFoo, but in practice you can call it anything.

Notice that currently the Template Haskell requires you import a number of other names from other modules. This is because it is badly written and this restriction will go away in a future release.

The instance generated will be

instance (ProductProfunctor p, Default p a a', Default p b b', Default p c c')
      => Default p (Foo a b c) (Foo a' b' c')

and pFoo will have the type

pFoo :: ProductProfunctor p =>
        Foo (p a a') (p b b') (p c c') -> p (Foo a b c) (Foo a' b' c')

Documentation

xTuple :: ([Pat] -> Pat) -> ([Exp] -> Exp) -> (Name, Int) -> Dec Source

o :: Exp -> Exp -> Exp Source

appEAll :: Exp -> [Exp] -> Exp Source