Copyright | (c) Justin Le 2019 |
---|---|
License | BSD3 |
Maintainer | justin@jle.im |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
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
.
(f x) :>>= (x -> g a) |
pattern Comp :: Functor f => f (g a) -> Comp f g a | Pattern match on and construct a |