genifunctors

Last updated: 2016-11-14 11:24:41 +0100

Upstream URL: git clone http://chriswarbo.net/git/genifunctors.git

Repo

View repository

View issue tracker

Contents of follows


Generate (derive) generalized fmap, foldMap and traverse for Bifunctors, Trifunctors, or a functor with any arity

Example:

data U a b c d
    = L [U a b c d]               -- polymorphic recursion
    | M (V (a,b) (Either c d))    -- mutually recursive
    | a :+: Int                   -- infix syntax, record syntax, type synonyms
    | R { c :: c, d :: String }   -- and primitive data types supported

data V u v = X (U v v u u) | Z u

fmapU :: (a ->gt; a') ->gt; (b ->gt; b') ->gt; (c ->gt; c') ->gt; (d ->gt; d') ->gt;
         U a b c d ->gt; U a' b' c' d'
fmapU = $(genFmap ''U)

foldU :: Monoid m =>gt; (a ->gt; m) ->gt; (b ->gt; m) ->gt; (c ->gt; m) ->gt; (d ->gt; m) ->gt;
         U a b c d ->gt; m
foldU = $(genFoldMap ''U)

travU :: Applicative f =>gt;
         (a ->gt; f a') ->gt; (b ->gt; f b') ->gt; (c ->gt; f c') ->gt; (d ->gt; f d') ->gt;
         U a b c d ->gt; f (U a' b' c' d')
travU = $(genTraverse ''U)