algebraic-classes-0.4: Conversions between algebraic classes and F-algebras.

Portabilitynon-portable
Stabilityexperimental
Maintainersjoerd@w3future.com
Safe HaskellNone

Data.Algebra.TH

Contents

Description

 

Synopsis

Documentation

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 Algebra f a, where f is the signature of c.

deriveInstance will generate a signature for the class if there is no signature in scope.

deriveInstanceWith :: Q Type -> Q [Dec] -> Q [Dec]Source

Derive an instance for an algebraic class with a given partial implementation. For example:

 deriveInstanceWith [t| Num n => Num (Integer -> n) |] 
   [d|
     fromInteger x y = fromInteger (x + y)
   |]

deriveInstanceWith_skipSignature :: Q Type -> Q [Dec] -> Q [Dec]Source

Derive an instance for an algebraic class with a given partial implementation, but don't generate the signature. This is for when you want to derive several instances of the same class, but can't splice the results directly. In that case deriveSignature can't detect it has already generated the signature earlier.

deriveSignature :: Name -> Q [Dec]Source

Derive a signature for an algebraic class. For example:

 deriveSignature ''Monoid

The above would generate the following:

 data MonoidSignature a = Op_mempty | Op_mappend a a | Op_mconcat [a]
   deriving (Functor, Foldable, Traversable, Show, Eq, Ord)
 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  

deriveSignature creates the signature data type and an instance for it of the AlgebraSignature class. DeriveTraversable is used the generate the Traversable instance of the signature.

This will do nothing if there is already a signature for the class in scope.

Possibly useful internals