free-functors- Provides free functors that are adjoint to functors that forget class constraints.

Safe HaskellNone




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



newtype Free c a Source

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.




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


~ (* -> 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 Free c a for the class c, Show, Foldable and Traversable.

For example:

deriveInstances ''Num

unit :: a -> Free c aSource

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

counit :: c a => Free c a -> aSource

counit = rightAdjunct id

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)

unfold :: (b -> Coproduct c b a) -> b -> Free c aSource

unfold f = coproduct (unfold f) unit . f

inL and inR are useful here. For example, the following creates the list [1..10] as a Free Monoid:

unfold (b -> if b == 0 then mempty else inL (b - 1) <> inR b) 10

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

convert = rightAdjunct pure

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

convertClosed = rightAdjunct absurd


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

Products of Monoids are Monoids themselves. But coproducts of Monoids are not. However, the free Monoid applied to the coproduct is a Monoid, and it is the coproduct in the category of Monoids. This is also called the free product, and generalizes to any algebraic class.

coproduct :: c r => (m -> r) -> (n -> r) -> Coproduct c m n -> rSource

inL :: m -> Coproduct c m nSource

inR :: n -> Coproduct c m nSource

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