Portability  nonportable (GHC Extensions) 

Stability  experimental 
Maintainer  Patrick Bahr <paba@diku.dk> 
This module contains functionality for automatically deriving boilerplate
code using Template Haskell. Examples include instances of Functor
,
Foldable
, and Traversable
.
 derive :: [Name > Q [Dec]] > [Name] > Q [Dec]
 class ShowF f where
 makeShowF :: Name > Q [Dec]
 class EqF f where
 makeEqF :: Name > Q [Dec]
 class EqF f => OrdF f where
 makeOrdF :: Name > Q [Dec]
 class Functor f
 makeFunctor :: Name > Q [Dec]
 class Foldable t
 makeFoldable :: Name > Q [Dec]
 class (Functor t, Foldable t) => Traversable t
 makeTraversable :: Name > Q [Dec]
 class ArbitraryF f where
 arbitraryF' :: Arbitrary v => [(Int, Gen (f v))]
 arbitraryF :: Arbitrary v => Gen (f v)
 shrinkF :: Arbitrary v => f v > [f v]
 makeArbitraryF :: Name > Q [Dec]
 class Arbitrary a where
 makeArbitrary :: Name > Q [Dec]
 class NFData a where
 rnf :: a > ()
 makeNFData :: Name > Q [Dec]
 class NFDataF f where
 makeNFDataF :: Name > Q [Dec]
 smartConstructors :: Name > Q [Dec]
 smartAConstructors :: Name > Q [Dec]
 liftSum :: Name > Q [Dec]
 caseF :: (f a > b) > (g a > b) > (f :+: g) a > b
Documentation
derive :: [Name > Q [Dec]] > [Name] > Q [Dec]Source
Helper function for generating a list of instances for a list of named
signatures. For example, in order to derive instances Functor
and
ShowF
for a signature Exp
, use derive as follows (requires Template
Haskell):
$(derive [makeFunctor, makeShowF] [''Exp])
Derive boilerplate instances for compositional data type signatures.
ShowF
Signature printing. An instance ShowF f
gives rise to an instance
Show (Term f)
.
makeShowF :: Name > Q [Dec]Source
Derive an instance of ShowF
for a type constructor of any firstorder kind
taking at least one argument.
EqF
Signature equality. An instance EqF f
gives rise to an instance
Eq (Term f)
.
EqF []  
EqF Maybe  
Eq a0 => EqF ((,) a0)  
(Eq a0, Eq b0) => EqF ((,,) a0 b0)  
(EqF f, EqF g) => EqF (:+: f g) 

EqF f => EqF (Cxt h f)  From an 
(Eq a0, Eq b0, Eq c0) => EqF ((,,,) a0 b0 c0)  
(Eq a0, Eq b0, Eq c0, Eq d0) => EqF ((,,,,) a0 b0 c0 d0)  
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0) => EqF ((,,,,,) a0 b0 c0 d0 e0)  
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0) => EqF ((,,,,,,) a0 b0 c0 d0 e0 f0)  
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0, Eq g0) => EqF ((,,,,,,,) a0 b0 c0 d0 e0 f0 g0)  
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0, Eq g0, Eq h0) => EqF ((,,,,,,,,) a0 b0 c0 d0 e0 f0 g0 h0)  
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0, Eq g0, Eq h0, Eq i0) => EqF ((,,,,,,,,,) a0 b0 c0 d0 e0 f0 g0 h0 i0) 
makeEqF :: Name > Q [Dec]Source
Derive an instance of EqF
for a type constructor of any firstorder kind
taking at least one argument.
OrdF
class EqF f => OrdF f whereSource
Signature ordering. An instance OrdF f
gives rise to an instance
Ord (Term f)
.
OrdF []  
OrdF Maybe  
Ord a0 => OrdF ((,) a0)  
(Ord a0, Ord b0) => OrdF ((,,) a0 b0)  
(OrdF f, OrdF g) => OrdF (:+: f g) 

OrdF f => OrdF (Cxt h f)  From an 
(Ord a0, Ord b0, Ord c0) => OrdF ((,,,) a0 b0 c0)  
(Ord a0, Ord b0, Ord c0, Ord d0) => OrdF ((,,,,) a0 b0 c0 d0)  
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0) => OrdF ((,,,,,) a0 b0 c0 d0 e0)  
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0) => OrdF ((,,,,,,) a0 b0 c0 d0 e0 f0)  
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0, Ord g0) => OrdF ((,,,,,,,) a0 b0 c0 d0 e0 f0 g0)  
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0, Ord g0, Ord h0) => OrdF ((,,,,,,,,) a0 b0 c0 d0 e0 f0 g0 h0)  
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0, Ord g0, Ord h0, Ord i0) => OrdF ((,,,,,,,,,) a0 b0 c0 d0 e0 f0 g0 h0 i0) 
makeOrdF :: Name > Q [Dec]Source
Derive an instance of OrdF
for a type constructor of any firstorder kind
taking at least one argument.
Functor
class Functor f
The Functor
class is used for types that can be mapped over.
Instances of Functor
should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor
for lists, Data.Maybe.Maybe
and System.IO.IO
satisfy these laws.
makeFunctor :: Name > Q [Dec]Source
Derive an instance of Functor
for a type constructor of any firstorder
kind taking at least one argument.
Foldable
class Foldable t
Data structures that can be folded.
Minimal complete definition: foldMap
or foldr
.
For example, given a data type
data Tree a = Empty  Leaf a  Node (Tree a) a (Tree a)
a suitable instance would be
instance Foldable Tree where foldMap f Empty = mempty foldMap f (Leaf x) = f x foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r
This is suitable even for abstract types, as the monoid is assumed
to satisfy the monoid laws. Alternatively, one could define foldr
:
instance Foldable Tree where foldr f z Empty = z foldr f z (Leaf x) = f x z foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l
makeFoldable :: Name > Q [Dec]Source
Derive an instance of Foldable
for a type constructor of any firstorder
kind taking at least one argument.
Traversable
class (Functor t, Foldable t) => Traversable t
Functors representing data structures that can be traversed from left to right.
Minimal complete definition: traverse
or sequenceA
.
Instances are similar to Functor
, e.g. given a data type
data Tree a = Empty  Leaf a  Node (Tree a) a (Tree a)
a suitable instance would be
instance Traversable Tree where traverse f Empty = pure Empty traverse f (Leaf x) = Leaf <$> f x traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
This is suitable even for abstract types, as the laws for <*>
imply a form of associativity.
The superclass instances should satisfy the following:
 In the
Functor
instance,fmap
should be equivalent to traversal with the identity applicative functor (fmapDefault
).  In the
Foldable
instance,Data.Foldable.foldMap
should be equivalent to traversal with a constant applicative functor (foldMapDefault
).
Traversable []  
Traversable Maybe  
Traversable Tree  
Traversable IntMap  
Traversable Identity  
Traversable (Map k)  
Ix i => Traversable (Array i)  
(Traversable f, Traversable g) => Traversable (Compose f g)  
Traversable f => Traversable (:&: f a)  
(Traversable f, Traversable g) => Traversable (:+: f g)  
Traversable f => Traversable (Cxt h f) 
makeTraversable :: Name > Q [Dec]Source
Derive an instance of Traversable
for a type constructor of any
firstorder kind taking at least one argument.
Arbitrary
class ArbitraryF f whereSource
Signature arbitration. An instance ArbitraryF f
gives rise to an instance
Arbitrary (Term f)
.
arbitraryF' :: Arbitrary v => [(Int, Gen (f v))]Source
arbitraryF :: Arbitrary v => Gen (f v)Source
makeArbitraryF :: Name > Q [Dec]Source
Derive an instance of ArbitraryF
for a type constructor of any
firstorder kind taking at least one argument. It is necessary that
all types that are used by the data type definition are themselves
instances of Arbitrary
.
class Arbitrary a where
Random generation and shrinking of values.
A generator for values of the given type.
shrink :: a > [a]
Produces a (possibly) empty list of all the possible immediate shrinks of the given value.
class NFData a where
A class of types that can be fully evaluated.
rnf :: a > ()
rnf should reduce its argument to normal form (that is, fully evaluate all subcomponents), and then return '()'.
The default implementation of rnf
is
rnf a = a `seq` ()
which may be convenient when defining instances for data types with no unevaluated fields (e.g. enumerations).
DeepSeq
Signature normal form. An instance NFDataF f
gives rise to an instance
NFData (Term f)
.
makeNFDataF :: Name > Q [Dec]Source
Derive an instance of NFDataF
for a type constructor of any firstorder
kind taking at least one argument.
Smart Constructors
smartConstructors :: Name > Q [Dec]Source
Derive smart constructors for a type constructor of any firstorder kind
taking at least one argument. The smart constructors are similar to the
ordinary constructors, but an inject
is automatically inserted.
Smart Constructors w/ Annotations
smartAConstructors :: Name > Q [Dec]Source
Derive smart constructors with products for a type constructor of any
parametric kind taking at least one argument. The smart constructors are
similar to the ordinary constructors, but an injectA
is automatically
inserted.