Search code examples
haskelltemplate-haskell

Fieldwise distribution of Monoid, Applicative, Monad etc for Haskell records


Is there a package that does roughly the following:

Given a record:

data R = R { a :: TypeA,
             b :: TypeB,
             c :: TypeC }

derives a lifted record:

data R_L f = R_L { a_L :: f TypeA,
                   b_L :: f TypeB,
                   c_L :: f TypeC }

and offers a couple of instances and functions similar to:

instance (Monoid (f TypeA), Monoid (f TypeB), Monoid (f TypeC))
         => (Monoid (R_L f)) where
  mempty = R_L mempty mempty mempty
  mplus a b = ...fieldwise mplus...

sequenceR :: (Monad m) => R_L m -> m R
sequenceR = ... run fields, sum results ...
sequenceRA :: (Applicative m) => R_L m -> m R
sequenceRA x = R <$> a_L x <*> b_L x <*> c_L x

and probably others. Is there a package that provides this functionality and when not, which of the mechanisms (TH? Generics?) is best to use to implement it?


Solution

  • In the true open source spirit I wrote my own library:

    http://hackage.haskell.org/package/fieldwise

    https://github.com/gracjan/fieldwise