Copyright | (c) Justin Le 2019 |
---|---|
License | BSD3 |
Maintainer | justin@jle.im |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
The church-encoded Freer Monad. Basically provides the free monad in
a way that is compatible with HFunctor
and
Interpret
. We also have the "semigroup" version
Free1
, which is the free Bind
.
The module also provides a version of :.:
(or
Compose
), Comp
, in a way that is compatible with
HBifunctor
and the related typeclasses.
Synopsis
- newtype Free f a = Free {
- runFree :: forall r. (a -> r) -> (forall s. f s -> (s -> r) -> r) -> r
- reFree :: (MonadFree f m, Functor f) => Free f a -> m a
- liftFree :: f ~> Free f
- interpretFree :: Monad g => (f ~> g) -> Free f ~> g
- retractFree :: Monad f => Free f ~> f
- hoistFree :: (f ~> g) -> Free f ~> Free g
- foldFree :: Functor f => (a -> r) -> (f r -> r) -> Free f a -> r
- foldFree' :: (a -> r) -> (forall s. f s -> (s -> r) -> r) -> Free f a -> r
- foldFreeC :: (a -> r) -> (Coyoneda f r -> r) -> Free f a -> r
- newtype Free1 f a where
- reFree1 :: (MonadFree f m, Functor f) => Free1 f a -> m a
- toFree :: Free1 f ~> Free f
- liftFree1 :: f ~> Free1 f
- interpretFree1 :: Bind g => (f ~> g) -> Free1 f ~> g
- retractFree1 :: Bind f => Free1 f ~> f
- hoistFree1 :: (f ~> g) -> Free1 f ~> Free1 g
- free1Comp :: Free1 f ~> Comp f (Free f)
- matchFree1 :: forall f. Functor f => Free1 f ~> (f :+: Comp f (Free1 f))
- foldFree1 :: Functor f => (f a -> r) -> (f r -> r) -> Free1 f a -> r
- foldFree1' :: (forall s. f s -> (s -> a) -> r) -> (forall s. f s -> (s -> r) -> r) -> Free1 f a -> r
- foldFree1C :: (Coyoneda f a -> r) -> (Coyoneda f r -> r) -> Free1 f a -> r
- data Comp f g a where
- comp :: f (g a) -> Comp f g a
Free
A
is Free
ff
enhanced with "sequential binding" capabilities.
It allows you to sequence multiple f
s one after the other, and also to
determine "what f
to sequence" based on the result of the computation
so far.
Essentially, you can think of this as "giving f
a Monad
instance",
with all that that entails (return
, >>=
, etc.).
Lift f
into it with
. When you finally want to "use" it, you can interpret it into any
monadic context:inject
:: f a -> Free
f a
interpret
::Monad
g => (forall x. f x -> g x) ->Free
f a -> g a
Structurally, this is equivalent to many "nested" f's. A value of type
is either:Free
f a
a
f a
f (f a)
f (f (f a))
- .. etc.
Under the hood, this is the Church-encoded Freer monad. It's
Free
, or F
, but in
a way that is compatible with HFunctor
and
Interpret
.
Instances
Interpretation
Folding
Free1
The Free Bind
. Imbues any functor f
with a Bind
instance.
Conceptually, this is "Free
without pure". That is, while normally
is an Free
f aa
, a f a
, a f (f a)
, etc., a
is
an Free1
f af a
, f (f a)
, f (f (f a))
, etc. It's a Free
with "at least
one layer of f
", excluding the a
case.
It can be useful as the semigroup formed by :.:
(functor composition):
Sometimes we want an f :.: f
, or an f :.: f :.: f
, or an f :.:
f :.: f :.: f
...just as long as we have at least one f
.
Free1 | |
|
pattern DoneF1 :: Functor f => f a -> Free1 f a | Constructor matching on the case that a |
pattern MoreF1 :: Functor f => f (Free1 f a) -> Free1 f a | Constructor matching on the case that a As a constructor, this is equivalent to |
Instances
Interpretation
Conversion
Folding
foldFree1' :: (forall s. f s -> (s -> a) -> r) -> (forall s. f s -> (s -> r) -> r) -> Free1 f a -> r Source #
Comp
Functor composition.
is equivalent to Comp
f g af (g a)
, and
the Comp
pattern synonym is a way of getting the f (g a)
in
a
.Comp
f g a
For example,
is Maybe
(IO
Bool
)
.Comp
Maybe
IO
Bool
This is mostly useful for its typeclass instances: in particular,
Functor
, Applicative
, HBifunctor
, and
Monoidal
.
This is essentially a version of :.:
and
Compose
that allows for an
HBifunctor
instance.
It is slightly less performant. Using
every once in
a while will concretize a comp
. unComp
Comp
value (if you have
)
and remove some indirection if you have a lot of chained operations.Functor
f
The "free monoid" over Comp
is Free
, and the "free semigroup" over
Comp
is Free1
.
forall x. (f x) :>>= (x -> g a) |
pattern Comp :: Functor f => f (g a) -> Comp f g a | Pattern match on and construct a |
Instances
HFunctor (Comp f :: (k -> Type) -> k -> Type) Source # | |
HBifunctor (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Source # | |
Defined in Data.HFunctor.Internal hleft :: forall (f :: k -> Type) (j :: k -> Type) (g :: k -> Type). (f ~> j) -> Comp f g ~> Comp j g Source # hright :: forall (g :: k -> Type) (l :: k -> Type) (f :: k -> Type). (g ~> l) -> Comp f g ~> Comp f l Source # hbimap :: forall (f :: k -> Type) (j :: k -> Type) (g :: k -> Type) (l :: k -> Type). (f ~> j) -> (g ~> l) -> Comp f g ~> Comp j l Source # | |
Applicative f => Inject (Comp f :: (Type -> Type) -> Type -> Type) Source # | |
Associative (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Source # | |
Defined in Data.HBifunctor.Associative type NonEmptyBy Comp :: (Type -> Type) -> Type -> Type Source # type FunctorBy Comp :: (Type -> Type) -> Constraint Source # associating :: forall (f :: Type -> Type) (g :: Type -> Type) (h :: Type -> Type). (FunctorBy Comp f, FunctorBy Comp g, FunctorBy Comp h) => Comp f (Comp g h) <~> Comp (Comp f g) h Source # appendNE :: forall (f :: Type -> Type). Comp (NonEmptyBy Comp f) (NonEmptyBy Comp f) ~> NonEmptyBy Comp f Source # matchNE :: forall (f :: Type -> Type). FunctorBy Comp f => NonEmptyBy Comp f ~> (f :+: Comp f (NonEmptyBy Comp f)) Source # consNE :: forall (f :: Type -> Type). Comp f (NonEmptyBy Comp f) ~> NonEmptyBy Comp f Source # toNonEmptyBy :: forall (f :: Type -> Type). Comp f f ~> NonEmptyBy Comp f Source # | |
Bind f => SemigroupIn (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) f Source # | Instances of |
Tensor (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Identity Source # | |
Defined in Data.HBifunctor.Tensor intro1 :: forall (f :: Type -> Type). f ~> Comp f Identity Source # intro2 :: forall (g :: Type -> Type). g ~> Comp Identity g Source # elim1 :: forall (f :: Type -> Type). FunctorBy Comp f => Comp f Identity ~> f Source # elim2 :: forall (g :: Type -> Type). FunctorBy Comp g => Comp Identity g ~> g Source # appendLB :: forall (f :: Type -> Type). Comp (ListBy Comp f) (ListBy Comp f) ~> ListBy Comp f Source # splitNE :: forall (f :: Type -> Type). NonEmptyBy Comp f ~> Comp f (ListBy Comp f) Source # splittingLB :: forall (f :: Type -> Type). ListBy Comp f <~> (Identity :+: Comp f (ListBy Comp f)) Source # toListBy :: forall (f :: Type -> Type). Comp f f ~> ListBy Comp f Source # fromNE :: forall (f :: Type -> Type). NonEmptyBy Comp f ~> ListBy Comp f Source # | |
(Bind f, Monad f) => MonoidIn (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Identity f Source # | Instances of This instance is the "proof" that "monads are the monoids in the
category of endofunctors (enriched with Note that because of typeclass constraints, this requires |
(Foldable f, Foldable g) => Foldable (Comp f g) Source # | |
Defined in Control.Monad.Freer.Church fold :: Monoid m => Comp f g m -> m # foldMap :: Monoid m => (a -> m) -> Comp f g a -> m # foldMap' :: Monoid m => (a -> m) -> Comp f g a -> m # foldr :: (a -> b -> b) -> b -> Comp f g a -> b # foldr' :: (a -> b -> b) -> b -> Comp f g a -> b # foldl :: (b -> a -> b) -> b -> Comp f g a -> b # foldl' :: (b -> a -> b) -> b -> Comp f g a -> b # foldr1 :: (a -> a -> a) -> Comp f g a -> a # foldl1 :: (a -> a -> a) -> Comp f g a -> a # elem :: Eq a => a -> Comp f g a -> Bool # maximum :: Ord a => Comp f g a -> a # minimum :: Ord a => Comp f g a -> a # | |
(Functor f, Eq1 f, Eq1 g) => Eq1 (Comp f g) Source # | |
(Functor f, Ord1 f, Ord1 g) => Ord1 (Comp f g) Source # | |
Defined in Control.Monad.Freer.Church | |
(Functor f, Read1 f, Read1 g) => Read1 (Comp f g) Source # | |
Defined in Control.Monad.Freer.Church | |
(Functor f, Show1 f, Show1 g) => Show1 (Comp f g) Source # | |
(Traversable f, Traversable g) => Traversable (Comp f g) Source # | |
Defined in Control.Monad.Freer.Church | |
(Alternative f, Alternative g) => Alternative (Comp f g) Source # | |
(Applicative f, Applicative g) => Applicative (Comp f g) Source # | |
Functor g => Functor (Comp f g) Source # | |
Invariant g => Invariant (Comp f g) Source # | Since: 0.4.1.2 |
Defined in Control.Monad.Freer.Church | |
(Alt f, Alt g) => Alt (Comp f g) Source # | Since: 0.3.6.0 |
(Apply f, Apply g) => Apply (Comp f g) Source # | Since: 0.3.6.0 |
Functor f => Apply (Chain1 (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) f) Source # | |
Defined in Data.HFunctor.Chain | |
Functor f => Bind (Chain1 (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) f) Source # |
|
(Plus f, Plus g) => Plus (Comp f g) Source # | Since: 0.3.6.0 |
Defined in Control.Monad.Freer.Church | |
(Functor f, Read1 f, Read1 g, Read a) => Read (Comp f g a) Source # | |
(Functor f, Show1 f, Show1 g, Show a) => Show (Comp f g a) Source # | |
(Functor f, Eq1 f, Eq1 g, Eq a) => Eq (Comp f g a) Source # | |
(Functor f, Ord1 f, Ord1 g, Ord a) => Ord (Comp f g a) Source # | |
Defined in Control.Monad.Freer.Church | |
Applicative (Chain (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Identity f) Source # | |
Defined in Data.HFunctor.Chain pure :: a -> Chain Comp Identity f a # (<*>) :: Chain Comp Identity f (a -> b) -> Chain Comp Identity f a -> Chain Comp Identity f b # liftA2 :: (a -> b -> c) -> Chain Comp Identity f a -> Chain Comp Identity f b -> Chain Comp Identity f c # (*>) :: Chain Comp Identity f a -> Chain Comp Identity f b -> Chain Comp Identity f b # (<*) :: Chain Comp Identity f a -> Chain Comp Identity f b -> Chain Comp Identity f a # | |
Monad (Chain (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Identity f) Source # |
|
Apply (Chain (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Identity f) Source # | |
Defined in Data.HFunctor.Chain (<.>) :: Chain Comp Identity f (a -> b) -> Chain Comp Identity f a -> Chain Comp Identity f b # (.>) :: Chain Comp Identity f a -> Chain Comp Identity f b -> Chain Comp Identity f b # (<.) :: Chain Comp Identity f a -> Chain Comp Identity f b -> Chain Comp Identity f a # liftF2 :: (a -> b -> c) -> Chain Comp Identity f a -> Chain Comp Identity f b -> Chain Comp Identity f c # | |
Bind (Chain (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Identity f) Source # | |
type FunctorBy (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Source # | |
type NonEmptyBy (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Source # | |
Defined in Data.HBifunctor.Associative | |
type ListBy (Comp :: (Type -> Type) -> (Type -> Type) -> Type -> Type) Source # | |