Copyright | (c) 2010-2011 Patrick Bahr |
---|---|
License | BSD3 |
Maintainer | Patrick Bahr <paba@diku.dk> |
Stability | experimental |
Portability | non-portable (GHC Extensions) |
Safe Haskell | None |
Language | Haskell98 |
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 ShowConstr f where
- showConstr :: f a -> String
- makeShowConstr :: 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]
- makeHaskellStrict :: Name -> Q [Dec]
- haskellStrict :: (Monad m, HaskellStrict f, f :<: (m :+: g)) => f (TermT m g) -> TermT m g
- haskellStrict' :: (Monad m, HaskellStrict f, f :<: (m :+: g)) => f (TermT m g) -> TermT m g
- 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]
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 first-order kind
taking at least one argument.
class ShowConstr f where Source
Constructor printing.
showConstr :: f a -> String Source
(ShowConstr f, Show p) => ShowConstr ((:&:) * f p) | |
(ShowConstr f, ShowConstr g) => ShowConstr ((:+:) * f g) |
makeShowConstr :: Name -> Q [Dec] Source
Derive an instance of showConstr
for a type constructor of any first-order 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 ((,) a) | |
(Eq a0, Eq b0) => EqF ((,,) a b) | |
EqF f => EqF (Cxt h f) | |
(Eq a0, Eq b0, Eq c0) => EqF ((,,,) a b c) | |
(EqF f, EqF g) => EqF ((:+:) * f g) |
|
(Eq a0, Eq b0, Eq c0, Eq d0) => EqF ((,,,,) a b c d) | |
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0) => EqF ((,,,,,) a b c d e) | |
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0) => EqF ((,,,,,,) a b c d e f) | |
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0, Eq g0) => EqF ((,,,,,,,) a b c d e f g) | |
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0, Eq g0, Eq h0) => EqF ((,,,,,,,,) a b c d e f g h) | |
(Eq a0, Eq b0, Eq c0, Eq d0, Eq e0, Eq f0, Eq g0, Eq h0, Eq i0) => EqF ((,,,,,,,,,) a b c d e f g h i) |
makeEqF :: Name -> Q [Dec] Source
Derive an instance of EqF
for a type constructor of any first-order kind
taking at least one argument.
OrdF
class EqF f => OrdF f where Source
Signature ordering. An instance OrdF f
gives rise to an instance
Ord (Term f)
.
OrdF [] | |
OrdF Maybe | |
Ord a0 => OrdF ((,) a) | |
(Ord a0, Ord b0) => OrdF ((,,) a b) | |
OrdF f => OrdF (Cxt h f) | |
(Ord a0, Ord b0, Ord c0) => OrdF ((,,,) a b c) | |
(OrdF f, OrdF g) => OrdF ((:+:) * f g) |
|
(Ord a0, Ord b0, Ord c0, Ord d0) => OrdF ((,,,,) a b c d) | |
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0) => OrdF ((,,,,,) a b c d e) | |
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0) => OrdF ((,,,,,,) a b c d e f) | |
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0, Ord g0) => OrdF ((,,,,,,,) a b c d e f g) | |
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0, Ord g0, Ord h0) => OrdF ((,,,,,,,,) a b c d e f g h) | |
(Ord a0, Ord b0, Ord c0, Ord d0, Ord e0, Ord f0, Ord g0, Ord h0, Ord i0) => OrdF ((,,,,,,,,,) a b c d e f g h i) |
makeOrdF :: Name -> Q [Dec] Source
Derive an instance of OrdF
for a type constructor of any first-order 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, Maybe
and IO
satisfy these laws.
Functor [] | |
Functor IO | |
Functor Q | |
Functor Id | |
Functor ZipList | |
Functor Maybe | |
Functor IntMap | |
Functor Tree | |
Functor PprM | |
Functor I | |
Functor Identity | |
Functor Gen | |
Functor XName | |
Functor XAttr | |
Functor WarningText | |
Functor Type | |
Functor TyVarBind | |
Functor Stmt | |
Functor Splice | |
Functor SpecialCon | |
Functor Safety | |
Functor RuleVar | |
Functor Rule | |
Functor Rhs | |
Functor RPatOp | |
Functor RPat | |
Functor QualStmt | |
Functor QualConDecl | |
Functor QOp | |
Functor QName | |
Functor Promoted | |
Functor PatField | |
Functor Pat | |
Functor PXAttr | |
Functor Op | |
Functor Name | |
Functor ModulePragma | |
Functor ModuleName | |
Functor ModuleHead | |
Functor Module | |
Functor Match | |
Functor Literal | |
Functor Kind | |
Functor InstHead | |
Functor InstDecl | |
Functor ImportSpecList | |
Functor ImportSpec | |
Functor ImportDecl | |
Functor IfAlt | |
Functor IPName | |
Functor IPBind | |
Functor GuardedRhs | |
Functor GuardedAlts | |
Functor GuardedAlt | |
Functor GadtDecl | |
Functor FunDep | |
Functor FieldUpdate | |
Functor FieldDecl | |
Functor ExportSpecList | |
Functor ExportSpec | |
Functor Exp | |
Functor Deriving | |
Functor DeclHead | |
Functor Decl | |
Functor DataOrNew | |
Functor Context | |
Functor ConDecl | |
Functor ClassDecl | |
Functor CallConv | |
Functor CName | |
Functor Bracket | |
Functor Binds | |
Functor BangType | |
Functor Asst | |
Functor Assoc | |
Functor Annotation | |
Functor Alt | |
Functor Activation | |
Functor ((->) r) | |
Functor (Either a) | |
Functor ((,) a) | |
Ix i => Functor (Array i) | |
Functor (StateL s) | |
Functor (StateR s) | |
Functor (Const m) | |
Monad m => Functor (WrappedMonad m) | |
Arrow a => Functor (ArrowMonad a) | |
Functor (Proxy *) | |
Functor (Map k) | |
Functor (K a) | |
Functor m => Functor (MaybeT m) | |
Functor m => Functor (ListT m) | |
Functor m => Functor (IdentityT m) | |
Functor (HashMap k) | |
Arrow a => Functor (WrappedArrow a b) | |
Functor m => Functor (StateT s m) | |
Functor m => Functor (ErrorT e m) | |
Functor m => Functor (WriterT w m) | |
Functor m => Functor (WriterT w m) | |
Functor m => Functor (StateT s m) | |
Functor m => Functor (ReaderT r m) | |
Functor m => Functor (ExceptT e m) | |
Functor (ContT r m) | |
Functor f => Functor (Cxt h f) | |
Functor f => Functor ((:&:) * f a) | |
(Functor f, Functor g) => Functor ((:+:) * f g) | |
Functor m => Functor (RWST r w s m) | |
Functor m => Functor (RWST r w s m) |
makeFunctor :: Name -> Q [Dec] Source
Derive an instance of Functor
for a type constructor of any first-order
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
Foldable [] | |
Foldable Maybe | |
Foldable IntMap | |
Foldable Set | |
Foldable Tree | |
Foldable Identity | |
Foldable HashSet | |
Foldable XName | |
Foldable XAttr | |
Foldable WarningText | |
Foldable Type | |
Foldable TyVarBind | |
Foldable Stmt | |
Foldable Splice | |
Foldable SpecialCon | |
Foldable Safety | |
Foldable RuleVar | |
Foldable Rule | |
Foldable Rhs | |
Foldable RPatOp | |
Foldable RPat | |
Foldable QualStmt | |
Foldable QualConDecl | |
Foldable QOp | |
Foldable QName | |
Foldable Promoted | |
Foldable PatField | |
Foldable Pat | |
Foldable PXAttr | |
Foldable Op | |
Foldable Name | |
Foldable ModulePragma | |
Foldable ModuleName | |
Foldable ModuleHead | |
Foldable Module | |
Foldable Match | |
Foldable Literal | |
Foldable Kind | |
Foldable InstHead | |
Foldable InstDecl | |
Foldable ImportSpecList | |
Foldable ImportSpec | |
Foldable ImportDecl | |
Foldable IfAlt | |
Foldable IPName | |
Foldable IPBind | |
Foldable GuardedRhs | |
Foldable GuardedAlts | |
Foldable GuardedAlt | |
Foldable GadtDecl | |
Foldable FunDep | |
Foldable FieldUpdate | |
Foldable FieldDecl | |
Foldable ExportSpecList | |
Foldable ExportSpec | |
Foldable Exp | |
Foldable Deriving | |
Foldable DeclHead | |
Foldable Decl | |
Foldable DataOrNew | |
Foldable Context | |
Foldable ConDecl | |
Foldable ClassDecl | |
Foldable CallConv | |
Foldable CName | |
Foldable Bracket | |
Foldable Binds | |
Foldable BangType | |
Foldable Asst | |
Foldable Assoc | |
Foldable Annotation | |
Foldable Alt | |
Foldable Activation | |
Foldable (Either a) | |
Foldable ((,) a) | |
Ix i => Foldable (Array i) | |
Foldable (Const m) | |
Foldable (Proxy *) | |
Foldable (Map k) | |
Foldable f => Foldable (MaybeT f) | |
Foldable f => Foldable (ListT f) | |
Foldable f => Foldable (IdentityT f) | |
Foldable (HashMap k) | |
Foldable f => Foldable (ErrorT e f) | |
Foldable f => Foldable (WriterT w f) | |
Foldable f => Foldable (WriterT w f) | |
Foldable f => Foldable (ExceptT e f) | |
Foldable f => Foldable (Cxt h f) | |
Foldable f => Foldable ((:&:) * f a) | |
(Foldable f, Foldable g) => Foldable ((:+:) * f g) |
makeFoldable :: Name -> Q [Dec] Source
Derive an instance of Foldable
for a type constructor of any first-order
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
.
A definition of traverse
must satisfy the following laws:
- naturality
t .
for every applicative transformationtraverse
f =traverse
(t . f)t
- identity
traverse
Identity = Identity- composition
traverse
(Compose .fmap
g . f) = Compose .fmap
(traverse
g) .traverse
f
A definition of sequenceA
must satisfy the following laws:
- naturality
t .
for every applicative transformationsequenceA
=sequenceA
.fmap
tt
- identity
sequenceA
.fmap
Identity = Identity- composition
sequenceA
.fmap
Compose = Compose .fmap
sequenceA
.sequenceA
where an applicative transformation is a function
t :: (Applicative f, Applicative g) => f a -> g a
preserving the Applicative
operations, i.e.
and the identity functor Identity
and composition of functors Compose
are defined as
newtype Identity a = Identity a instance Functor Identity where fmap f (Identity x) = Identity (f x) instance Applicative Indentity where pure x = Identity x Identity f <*> Identity x = Identity (f x) newtype Compose f g a = Compose (f (g a)) instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose (fmap (fmap f) x) instance (Applicative f, Applicative g) => Applicative (Compose f g) where pure x = Compose (pure (pure x)) Compose f <*> Compose x = Compose ((<*>) <$> f <*> x)
(The naturality law is implied by parametricity.)
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,foldMap
should be equivalent to traversal with a constant applicative functor (foldMapDefault
).
makeTraversable :: Name -> Q [Dec] Source
Derive an instance of Traversable
for a type constructor of any
first-order kind taking at least one argument.
HaskellStrict
makeHaskellStrict :: Name -> Q [Dec] Source
Derive an instance of HaskellStrict
for a type constructor of any
first-order kind taking at least one argument.
Arbitrary
class ArbitraryF f where Source
Signature arbitration. An instance ArbitraryF f
gives rise to an instance
Arbitrary (Term f)
.
Nothing
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
first-order 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
Nothing
class NFData a where
A class of types that can be fully evaluated.
Since: 1.1.0.0
Nothing
rnf :: a -> ()
rnf should reduce its argument to normal form (that is, fully evaluate all sub-components), 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).
NFData Bool | |
NFData Char | |
NFData Double | |
NFData Float | |
NFData Int | |
NFData Int8 | |
NFData Int16 | |
NFData Int32 | |
NFData Int64 | |
NFData Integer | |
NFData Word | |
NFData Word8 | |
NFData Word16 | |
NFData Word32 | |
NFData Word64 | |
NFData () | |
NFData Version | Since: 1.3.0.0 |
NFData IntSet | |
NFData LocalTime | |
NFData ZonedTime | |
NFData Text | |
NFData Text | |
NFData a => NFData [a] | |
(Integral a, NFData a) => NFData (Ratio a) | |
NFData (Fixed a) | Since: 1.3.0.0 |
(RealFloat a, NFData a) => NFData (Complex a) | |
NFData a => NFData (Maybe a) | |
NFData a => NFData (IntMap a) | |
NFData a => NFData (Set a) | |
NFData a => NFData (Tree a) | |
NFData a => NFData (HashSet a) | |
NFData (a -> b) | This instance is for convenience and consistency with Since: 1.3.0.0 |
(NFData a, NFData b) => NFData (Either a b) | |
(NFData a, NFData b) => NFData (a, b) | |
(Ix a, NFData a, NFData b) => NFData (Array a b) | |
(NFData k, NFData a) => NFData (Map k a) | |
(NFData k, NFData v) => NFData (Leaf k v) | |
(NFData k, NFData v) => NFData (HashMap k v) | |
(NFData a, NFData b, NFData c) => NFData (a, b, c) | |
(NFDataF f, NFData a) => NFData (Cxt h f a) | |
(NFData a, NFData b, NFData c, NFData d) => NFData (a, b, c, d) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData (a1, a2, a3, a4, a5, a6, a7, a8) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8, NFData a9) => NFData (a1, a2, a3, a4, a5, a6, a7, a8, a9) |
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 first-order
kind taking at least one argument.
Smart Constructors
smartConstructors :: Name -> Q [Dec] Source
Derive smart constructors for a type constructor of any first-order 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.