free-functors-0.7.2: Free functors, adjoint to functors that forget class constraints.

LicenseBSD-style (see the file LICENSE)
Maintainersjoerd@w3future.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Functor.Free

Contents

Description

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

Synopsis

Documentation

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.

Constructors

Free 

Fields

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

Instances

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

Methods

algebra :: f (Free c a) -> Free c a #

Monad (Free c) Source # 

Methods

(>>=) :: Free c a -> (a -> Free c b) -> Free c b #

(>>) :: Free c a -> Free c b -> Free c b #

return :: a -> Free c a #

fail :: String -> Free c a #

Functor (Free c) Source # 

Methods

fmap :: (a -> b) -> Free c a -> Free c b #

(<$) :: a -> Free c b -> Free c a #

Applicative (Free c) Source # 

Methods

pure :: a -> Free c a #

(<*>) :: Free c (a -> b) -> Free c a -> Free c b #

(*>) :: Free c a -> Free c b -> Free c b #

(<*) :: Free c a -> Free c b -> Free c a #

ForallLifted c => Foldable (Free c) Source # 

Methods

fold :: Monoid m => Free c m -> m #

foldMap :: Monoid m => (a -> m) -> Free c a -> m #

foldr :: (a -> b -> b) -> b -> Free c a -> b #

foldr' :: (a -> b -> b) -> b -> Free c a -> b #

foldl :: (b -> a -> b) -> b -> Free c a -> b #

foldl' :: (b -> a -> b) -> b -> Free c a -> b #

foldr1 :: (a -> a -> a) -> Free c a -> a #

foldl1 :: (a -> a -> a) -> Free c a -> a #

toList :: Free c a -> [a] #

null :: Free c a -> Bool #

length :: Free c a -> Int #

elem :: Eq a => a -> Free c a -> Bool #

maximum :: Ord a => Free c a -> a #

minimum :: Ord a => Free c a -> a #

sum :: Num a => Free c a -> a #

product :: Num a => Free c a -> a #

ForallLifted c => Traversable (Free c) Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Free c a -> f (Free c b) #

sequenceA :: Applicative f => Free c (f a) -> f (Free c a) #

mapM :: Monad m => (a -> m b) -> Free c a -> m (Free c b) #

sequence :: Monad m => Free c (m a) -> m (Free c a) #

(ForallF * * c Extract, ForallF * * c (Duplicate (Free c))) => Comonad (Free c) Source # 

Methods

extract :: Free c a -> a #

duplicate :: Free c a -> Free c (Free c a) #

extend :: (Free c a -> b) -> Free c a -> Free c b #

(Show a, Show (Signature c (ShowHelper (Signature c) a)), c (ShowHelper (Signature c) a)) => Show (Free c a) Source # 

Methods

showsPrec :: Int -> Free c a -> ShowS #

show :: Free c a -> String #

showList :: [Free c a] -> ShowS #

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 a Source #

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

rightAdjunct :: c b => (a -> b) -> Free c a -> b Source #

rightAdjunct is the destructor of Free c values.

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

counit :: c a => Free c a -> a Source #

counit = rightAdjunct id

leftAdjunct :: (Free c a -> b) -> a -> b Source #

leftAdjunct f = f . unit

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

transform f as = as >>= f unit
transform f . transform g = transform (g . f)

unfold :: (b -> Coproduct c b a) -> b -> Free c a Source #

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 a Source #

convert = rightAdjunct pure

convertClosed :: c r => Free c Void -> r Source #

convertClosed = rightAdjunct absurd

newtype Extract a Source #

Constructors

Extract 

Fields

newtype Duplicate f a Source #

Constructors

Duplicate 

Fields

Coproducts

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 -> r Source #

inL :: m -> Coproduct c m n Source #

inR :: n -> Coproduct c m n Source #

initial :: c r => InitialObject c -> r Source #