Portability | non-portable |
---|---|
Stability | experimental |
Maintainer | sjoerd@w3future.com |
Safe Haskell | None |
- class Functor f => AlgebraSignature f where
- type Class f :: * -> Constraint
- evaluate :: Class f b => f b -> b
- class Algebra f a where
- algebra :: AlgebraSignature f => f a -> a
- deriveInstance :: Q Type -> Q [Dec]
- deriveSignature :: Name -> Q [Dec]
- data MonoidSignature a
- = Op_mempty
- | Op_mappend a a
- | Op_mconcat [a]
Classes
class Functor f => AlgebraSignature f whereSource
type Class f :: * -> ConstraintSource
The class for which f
is the signature.
algebra :: AlgebraSignature f => f a -> aSource
An algebra f a -> a
corresponds to an instance of a
of the class Class f
.
In some cases, for example for tuple types, you can give an algebra generically for every signature:
instance (Class f m, Class f n) => Algebra f (m, n) where algebra fmn = (evaluate (fmap fst fmn), evaluate (fmap snd fmn))
Template Haskell functions
deriveInstance :: Q Type -> Q [Dec]Source
Derive an instance for an algebraic class. For example:
deriveInstance [t| (Num m, Num n) => Num (m, n) |]
To be able to derive an instance for a
of class c
, we need an instance of
,
where Algebra
f af
is the signature of c
.
deriveInstance
will generate a signature for the class if there is no signature in scope.
deriveSignature :: Name -> Q [Dec]Source
Derive a signature for an algebraic class. For exaple:
deriveSignature ''Num
deriveSignature
creates the signature data type and an instance for it of the
AlgebraSignature
class. DeriveFunctor
is used the generate the Functor
instance of the signature.
This will do nothing if there is already a signature for the class in scope.
Example signature
data MonoidSignature a Source
The Monoid
signature has this AlgebraSignature
instance:
instance AlgebraSignature MonoidSignature where type Class MonoidSignature = Monoid evaluate Op_mempty = mempty evaluate (Op_mappend a b) = mappend a b evaluate (Op_mconcat ms) = mconcat ms
Op_mempty | |
Op_mappend a a | |
Op_mconcat [a] |