Stability | experimental |
---|---|

Maintainer | Sebastian Fischer <mailto:sebf@informatik.uni-kiel.de> |

Automatic deriving of monadic data types and corresponding instances.

- derive :: Derivation -> Name -> Q [Dec]
- monadic :: Derivation
- mdata :: Derivation
- shareable :: Derivation
- convertible :: Derivation

# Documentation

derive :: Derivation -> Name -> Q [Dec]

Derive an instance of some class. `derive`

only derives instances
for the type of the argument.

Derives monadic datatypes and instances for explicit sharing as well as conversion. Combines the other three derivations which provide the same functionality split into different parts.

You usually need the following preamble when deriving monadic code:

{-# LANGUAGE TemplateHaskell , KindSignatures , MultiParamTypeClasses , FlexibleInstances #-} import Control.Monad.Sharing import Data.Monadic.Derive import Data.DeriveTH

If your types contain lists, you also need to

import Data.Monadic.List

With this prerequisites, you can derive a monadic `Maybe`

type by

$(derive monadic ''Maybe)

Generates a monadic datatype and corresponding con- and destructor functions from a Haskell datatype.

For example, the datatype

data Maybe a = Nothing | Just a

can be translated into its monadic counterpart by typing

$(derive mdata ''Maybe)

This call generates the following datatype

data MMaybe m a = MNothing | MJust (m a)

and the following auxiliary functions for constructing and matching monadic values:

mNothing :: Monad m => m (MMaybe m a) mNothing = return MNothing

mJust :: Monad m => m a -> m (MMaybe m a) mJust a = return (MJust a)

matchMMaybe :: Monad m => m (MMaybe m a) -> m b -> (m a -> m b) -> m b matchMMaybe x n j = x >>= \x -> case x of { MNothing -> n; MJust a -> j a }

Generates a `Shareable`

instance for a monadic datatype.

For example the call

$(derive shareable ''Maybe)

generates the following instance:

instance (Monad m, Shareable m a) => Shareable (Maybe m a) where shareArgs fun MNothing = return MNothing shareArgs fun (MJust a) = fun a >>= \a -> mJust a

convertible :: DerivationSource

Generates `Convertible`

instances to convert between monadic and
non-monadic datatypes.

For example, the call

$(derive convertible ''Maybe)

generates the following instances:

instance (Monad m, Convertible m a a') => Convertible m (Maybe a) (MMaybe m a') where convArgs fun Nothing = mNothing convArgs fun (Just a) = mJust (fun a) instance (Monad m, Convertible m a' a) => Convertible m (MMaybe m a') (Maybe a) where convArgs fun MNothing = return Nothing convArgs fun (MJust a) = (a >>= fun) >>= \a -> return (Just a)