{-# LANGUAGE TemplateHaskell #-}
module Polysemy.Final
(
Final(..)
, ThroughWeavingToFinal
, withWeavingToFinal
, withStrategicToFinal
, embedFinal
, interpretFinal
, Strategic
, WithStrategy
, pureS
, liftS
, runS
, bindS
, getInspectorS
, getInitialStateS
, runFinal
, finalToFinal
, embedToFinal
) where
import Polysemy.Internal
import Polysemy.Internal.Combinators
import Polysemy.Internal.Union
import Polysemy.Internal.Strategy
import Polysemy.Internal.TH.Effect
type ThroughWeavingToFinal m z a =
forall f
. Functor f
=> f ()
-> (forall x. f (z x) -> m (f x))
-> (forall x. f x -> Maybe x)
-> m (f a)
newtype Final m z a where
WithWeavingToFinal
:: ThroughWeavingToFinal m z a
-> Final m z a
makeSem_ ''Final
withWeavingToFinal
:: forall m r a
. Member (Final m) r
=> ThroughWeavingToFinal m (Sem r) a
-> Sem r a
embedFinal :: (Member (Final m) r, Functor m) => m a -> Sem r a
embedFinal :: forall (m :: * -> *) (r :: EffectRow) a.
(Member (Final m) r, Functor m) =>
m a -> Sem r a
embedFinal m a
m = forall (m :: * -> *) (r :: EffectRow) a.
Member (Final m) r =>
ThroughWeavingToFinal m (Sem r) a -> Sem r a
withWeavingToFinal forall a b. (a -> b) -> a -> b
$ \f ()
s forall x. f (Sem r x) -> m (f x)
_ forall x. f x -> Maybe x
_ -> (forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
s) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
m
{-# INLINE embedFinal #-}
withStrategicToFinal :: Member (Final m) r
=> Strategic m (Sem r) a
-> Sem r a
withStrategicToFinal :: forall (m :: * -> *) (r :: EffectRow) a.
Member (Final m) r =>
Strategic m (Sem r) a -> Sem r a
withStrategicToFinal Strategic m (Sem r) a
strat = forall (m :: * -> *) (r :: EffectRow) a.
Member (Final m) r =>
ThroughWeavingToFinal m (Sem r) a -> Sem r a
withWeavingToFinal (forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
Functor f =>
Sem '[Strategy m f n] a
-> f ()
-> (forall x. f (n x) -> m (f x))
-> (forall x. f x -> Maybe x)
-> a
runStrategy Strategic m (Sem r) a
strat)
{-# INLINE withStrategicToFinal #-}
interpretFinal
:: forall m e r a
. Member (Final m) r
=> (forall x rInitial. e (Sem rInitial) x -> Strategic m (Sem rInitial) x)
-> Sem (e ': r) a
-> Sem r a
interpretFinal :: forall (m :: * -> *) (e :: Effect) (r :: EffectRow) a.
Member (Final m) r =>
(forall x (rInitial :: EffectRow).
e (Sem rInitial) x -> Strategic m (Sem rInitial) x)
-> Sem (e : r) a -> Sem r a
interpretFinal forall x (rInitial :: EffectRow).
e (Sem rInitial) x -> Strategic m (Sem rInitial) x
n =
let
go :: Sem (e ': r) x -> Sem r x
go :: forall x. Sem (e : r) x -> Sem r x
go = forall (r :: EffectRow) (r' :: EffectRow) a.
(forall x. Union r (Sem r) x -> Union r' (Sem r') x)
-> Sem r a -> Sem r' a
hoistSem forall a b. (a -> b) -> a -> b
$ \Union (e : r) (Sem (e : r)) x
u -> case forall (e :: Effect) (r :: EffectRow) (m :: * -> *) a.
Union (e : r) m a -> Either (Union r m a) (Weaving e m a)
decomp Union (e : r) (Sem (e : r)) x
u of
Right (Weaving e (Sem rInitial) a
e f ()
s forall x. f (Sem rInitial x) -> Sem (e : r) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
ins) ->
forall (e :: Effect) (r :: EffectRow) (m :: * -> *) a.
Member e r =>
Weaving e m a -> Union r m a
injWeaving forall a b. (a -> b) -> a -> b
$
forall (f :: * -> *) (e :: Effect) (rInitial :: EffectRow) a
resultType (mAfter :: * -> *).
Functor f =>
e (Sem rInitial) a
-> f ()
-> (forall x. f (Sem rInitial x) -> mAfter (f x))
-> (f a -> resultType)
-> (forall x. f x -> Maybe x)
-> Weaving e mAfter resultType
Weaving
(forall (m :: * -> *) (z :: * -> *) a.
ThroughWeavingToFinal m z a -> Final m z a
WithWeavingToFinal (forall (f :: * -> *) (m :: * -> *) (n :: * -> *) a.
Functor f =>
Sem '[Strategy m f n] a
-> f ()
-> (forall x. f (n x) -> m (f x))
-> (forall x. f x -> Maybe x)
-> a
runStrategy (forall x (rInitial :: EffectRow).
e (Sem rInitial) x -> Strategic m (Sem rInitial) x
n e (Sem rInitial) a
e)))
f ()
s
(forall x. Sem (e : r) x -> Sem r x
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f (Sem rInitial x) -> Sem (e : r) (f x)
wv)
f a -> x
ex
forall x. f x -> Maybe x
ins
Left Union r (Sem (e : r)) x
g -> forall (m :: * -> *) (n :: * -> *) (r :: EffectRow) a.
(forall x. m x -> n x) -> Union r m a -> Union r n a
hoist forall x. Sem (e : r) x -> Sem r x
go Union r (Sem (e : r)) x
g
{-# INLINE go #-}
in
forall x. Sem (e : r) x -> Sem r x
go
{-# INLINE interpretFinal #-}
runFinal :: Monad m => Sem '[Final m] a -> m a
runFinal :: forall (m :: * -> *) a. Monad m => Sem '[Final m] a -> m a
runFinal = forall (m :: * -> *) (r :: EffectRow) a.
Monad m =>
(forall x. Union r (Sem r) x -> m x) -> Sem r a -> m a
usingSem forall a b. (a -> b) -> a -> b
$ \Union '[Final m] (Sem '[Final m]) x
u -> case forall (e :: Effect) (m :: * -> *) a.
Union '[e] m a -> Weaving e m a
extract Union '[Final m] (Sem '[Final m]) x
u of
Weaving (WithWeavingToFinal ThroughWeavingToFinal m (Sem rInitial) a
wav) f ()
s forall x. f (Sem rInitial x) -> Sem '[Final m] (f x)
wv f a -> x
ex forall x. f x -> Maybe x
ins ->
f a -> x
ex forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ThroughWeavingToFinal m (Sem rInitial) a
wav f ()
s (forall (m :: * -> *) a. Monad m => Sem '[Final m] a -> m a
runFinal forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f (Sem rInitial x) -> Sem '[Final m] (f x)
wv) forall x. f x -> Maybe x
ins
{-# INLINE runFinal #-}
finalToFinal :: forall m1 m2 r a
. Member (Final m2) r
=> (forall x. m1 x -> m2 x)
-> (forall x. m2 x -> m1 x)
-> Sem (Final m1 ': r) a
-> Sem r a
finalToFinal :: forall (m1 :: * -> *) (m2 :: * -> *) (r :: EffectRow) a.
Member (Final m2) r =>
(forall x. m1 x -> m2 x)
-> (forall x. m2 x -> m1 x) -> Sem (Final m1 : r) a -> Sem r a
finalToFinal forall x. m1 x -> m2 x
to forall x. m2 x -> m1 x
from =
let
go :: Sem (Final m1 ': r) x -> Sem r x
go :: forall x. Sem (Final m1 : r) x -> Sem r x
go = forall (r :: EffectRow) (r' :: EffectRow) a.
(forall x. Union r (Sem r) x -> Union r' (Sem r') x)
-> Sem r a -> Sem r' a
hoistSem forall a b. (a -> b) -> a -> b
$ \Union (Final m1 : r) (Sem (Final m1 : r)) x
u -> case forall (e :: Effect) (r :: EffectRow) (m :: * -> *) a.
Union (e : r) m a -> Either (Union r m a) (Weaving e m a)
decomp Union (Final m1 : r) (Sem (Final m1 : r)) x
u of
Right (Weaving (WithWeavingToFinal ThroughWeavingToFinal m1 (Sem rInitial) a
wav) f ()
s forall x. f (Sem rInitial x) -> Sem (Final m1 : r) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
ins) ->
forall (e :: Effect) (r :: EffectRow) (m :: * -> *) a.
Member e r =>
Weaving e m a -> Union r m a
injWeaving forall a b. (a -> b) -> a -> b
$
forall (f :: * -> *) (e :: Effect) (rInitial :: EffectRow) a
resultType (mAfter :: * -> *).
Functor f =>
e (Sem rInitial) a
-> f ()
-> (forall x. f (Sem rInitial x) -> mAfter (f x))
-> (f a -> resultType)
-> (forall x. f x -> Maybe x)
-> Weaving e mAfter resultType
Weaving
(forall (m :: * -> *) (z :: * -> *) a.
ThroughWeavingToFinal m z a -> Final m z a
WithWeavingToFinal forall a b. (a -> b) -> a -> b
$ \f ()
s' forall x. f (Sem rInitial x) -> m2 (f x)
wv' forall x. f x -> Maybe x
ins' ->
forall x. m1 x -> m2 x
to forall a b. (a -> b) -> a -> b
$ ThroughWeavingToFinal m1 (Sem rInitial) a
wav f ()
s' (forall x. m2 x -> m1 x
from forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f (Sem rInitial x) -> m2 (f x)
wv') forall x. f x -> Maybe x
ins'
)
f ()
s
(forall x. Sem (Final m1 : r) x -> Sem r x
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f (Sem rInitial x) -> Sem (Final m1 : r) (f x)
wv)
f a -> x
ex
forall x. f x -> Maybe x
ins
Left Union r (Sem (Final m1 : r)) x
g -> forall (m :: * -> *) (n :: * -> *) (r :: EffectRow) a.
(forall x. m x -> n x) -> Union r m a -> Union r n a
hoist forall x. Sem (Final m1 : r) x -> Sem r x
go Union r (Sem (Final m1 : r)) x
g
{-# INLINE go #-}
in
forall x. Sem (Final m1 : r) x -> Sem r x
go
{-# INLINE finalToFinal #-}
embedToFinal :: (Member (Final m) r, Functor m)
=> Sem (Embed m ': r) a
-> Sem r a
embedToFinal :: forall (m :: * -> *) (r :: EffectRow) a.
(Member (Final m) r, Functor m) =>
Sem (Embed m : r) a -> Sem r a
embedToFinal = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \(Embed m x
m) -> forall (m :: * -> *) (r :: EffectRow) a.
(Member (Final m) r, Functor m) =>
m a -> Sem r a
embedFinal m x
m
{-# INLINE embedToFinal #-}