Portability | non-portable |
---|---|

Stability | experimental |

Maintainer | sjoerd@w3future.com |

Safe Haskell | None |

A free functor is left adjoint to a forgetful functor. In this package the forgetful functor forgets class constraints.

- newtype Free c a = Free {
- runFree :: forall b. c b => (a -> b) -> b

- deriveInstances :: Name -> Q [Dec]
- unit :: a -> Free c a
- rightAdjunct :: c b => (a -> b) -> Free c a -> b
- rightAdjunctF :: ForallF c f => (a -> f b) -> Free c a -> f b
- rightAdjunctT :: ForallT c t => (a -> t f b) -> Free c a -> t f b
- counit :: c a => Free c a -> a
- leftAdjunct :: (Free c a -> b) -> a -> b
- transform :: (forall r. c r => (b -> r) -> a -> r) -> Free c a -> Free c b
- unfold :: (b -> Coproduct c b a) -> b -> Free c a
- convert :: (c (f a), Applicative f) => Free c a -> f a
- convertClosed :: c r => Free c Void -> r
- type Coproduct c m n = Free c (Either m n)
- coproduct :: c r => (m -> r) -> (n -> r) -> Coproduct c m n -> r
- inL :: m -> Coproduct c m n
- inR :: n -> Coproduct c m n
- type InitialObject c = Free c Void
- initial :: c r => InitialObject c -> r

# Documentation

The free functor for class `c`

.

`Free c a`

is basically an expression tree with operations from class `c`

and variables/placeholders of type `a`

, created with `unit`

.
Monadic bind allows you to replace each of these variables with another sub-expression.

~ (* -> Constraint) c (Class f) => Algebra f (Free c a) | |

Monad (Free c) | |

Functor (Free c) | |

Applicative (Free c) | |

ForallT c (LiftAFree c) => Foldable (Free c) | |

ForallT c (LiftAFree c) => Traversable (Free c) | |

(ForallF c Identity, ForallF c (Compose (Free c) (Free c))) => Comonad (Free c) | |

(Show a, Show (Signature c (ShowHelper (Signature c) a)), c (ShowHelper (Signature c) a)) => Show (Free c a) |

deriveInstances :: Name -> Q [Dec]Source

Derive the instances of

for the class `Free`

c a`c`

, `Show`

, `Foldable`

and `Traversable`

.

For example:

deriveInstances ''Num

`unit`

allows you to create `Free c` values, together with the operations from the class `c`

.

rightAdjunct :: c b => (a -> b) -> Free c a -> bSource

`rightAdjunct`

is the destructor of `Free c` values.

rightAdjunctF :: ForallF c f => (a -> f b) -> Free c a -> f bSource

rightAdjunctT :: ForallT c t => (a -> t f b) -> Free c a -> t f bSource

leftAdjunct :: (Free c a -> b) -> a -> bSource

leftAdjunct f = f . unit

transform :: (forall r. c r => (b -> r) -> a -> r) -> Free c a -> Free c bSource

transform f as = as >>= f unit

transform f . transform g = transform (g . f)

convert :: (c (f a), Applicative f) => Free c a -> f aSource

convert = rightAdjunct pure

convertClosed :: c r => Free c Void -> rSource

convertClosed = rightAdjunct absurd

# Coproducts

type Coproduct c m n = Free c (Either m n)Source

Products of `Monoid`

s are `Monoid`

s themselves. But coproducts of `Monoid`

s are not.
However, the free `Monoid`

applied to the coproduct *is* a `Monoid`

, and it is the coproduct in the category of `Monoid`

s.
This is also called the free product, and generalizes to any algebraic class.

type InitialObject c = Free c VoidSource

initial :: c r => InitialObject c -> rSource