free-functors-0.8.3: 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

SuperClass1 * (Class f) c => 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 #

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

(*>) :: 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 #

SuperClass1 * Floating c => Floating (Free c a) Source # 

Methods

pi :: Free c a #

exp :: Free c a -> Free c a #

log :: Free c a -> Free c a #

sqrt :: Free c a -> Free c a #

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

logBase :: Free c a -> Free c a -> Free c a #

sin :: Free c a -> Free c a #

cos :: Free c a -> Free c a #

tan :: Free c a -> Free c a #

asin :: Free c a -> Free c a #

acos :: Free c a -> Free c a #

atan :: Free c a -> Free c a #

sinh :: Free c a -> Free c a #

cosh :: Free c a -> Free c a #

tanh :: Free c a -> Free c a #

asinh :: Free c a -> Free c a #

acosh :: Free c a -> Free c a #

atanh :: Free c a -> Free c a #

log1p :: Free c a -> Free c a #

expm1 :: Free c a -> Free c a #

log1pexp :: Free c a -> Free c a #

log1mexp :: Free c a -> Free c a #

SuperClass1 * Fractional c => Fractional (Free c a) Source # 

Methods

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

recip :: Free c a -> Free c a #

fromRational :: Rational -> Free c a #

SuperClass1 * Num c => Num (Free c a) Source # 

Methods

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

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

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

negate :: Free c a -> Free c a #

abs :: Free c a -> Free c a #

signum :: Free c a -> Free c a #

fromInteger :: Integer -> Free c a #

(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 #

SuperClass1 * Semigroup c => Semigroup (Free c a) Source # 

Methods

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

sconcat :: NonEmpty (Free c a) -> Free c a #

stimes :: Integral b => b -> Free c a -> Free c a #

SuperClass1 * Monoid c => Monoid (Free c a) Source # 

Methods

mempty :: Free c a #

mappend :: Free c a -> Free c a -> Free c a #

mconcat :: [Free c a] -> 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 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 #

Internal

data ShowHelper f a Source #

Constructors

ShowUnit a 
ShowRec (f (ShowHelper f a)) 

Instances

Algebra f (ShowHelper f a) Source # 

Methods

algebra :: f (ShowHelper f a) -> ShowHelper f a #

(Show a, Show (f (ShowHelper f a))) => Show (ShowHelper f a) Source # 

Methods

showsPrec :: Int -> ShowHelper f a -> ShowS #

show :: ShowHelper f a -> String #

showList :: [ShowHelper f a] -> ShowS #