{-# language AllowAmbiguousTypes #-}
module Polysemy.Scoped (
Scoped,
scoped,
scoped_,
rescope,
interpretScopedH,
interpretScopedH',
interpretScoped,
interpretScopedAs,
interpretScopedWithH,
interpretScopedWith,
interpretScopedWith_,
runScoped,
runScopedAs,
) where
import Polysemy.Internal
import Polysemy.Internal.Sing
import Polysemy.Internal.Union
import Polysemy.Internal.Combinators
import Polysemy.Internal.Scoped
import Polysemy.Internal.Tactics
interpretScopedH ::
∀ resource param effect r .
(∀ x . param -> (resource -> Sem r x) -> Sem r x) ->
(∀ r0 x . resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x) ->
InterpreterFor (Scoped param effect) r
interpretScopedH :: forall resource param (effect :: Effect) (r :: [Effect]).
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x)
-> InterpreterFor (Scoped param effect) r
interpretScopedH forall x. param -> (resource -> Sem r x) -> Sem r x
withResource forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x
scopedHandler =
resource -> InterpreterFor (Scoped param effect) r
go ([Char] -> resource
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"top level run")
where
go :: resource -> InterpreterFor (Scoped param effect) r
go :: resource -> InterpreterFor (Scoped param effect) r
go resource
resource =
(forall x.
Weaving (Scoped param effect) (Sem (Scoped param effect : r)) x
-> Sem r x)
-> InterpreterFor (Scoped param effect) r
forall (e :: Effect) (r :: [Effect]).
(forall x. Weaving e (Sem (e : r)) x -> Sem r x)
-> InterpreterFor e r
interpretWeaving \ (Weaving Scoped param effect (Sem rInitial) a
effect f ()
s forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
ins) -> case Scoped param effect (Sem rInitial) a
effect of
Run effect (Sem rInitial) a
act ->
f a -> x
ex (f a -> x) -> Sem r (f a) -> Sem r x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ()
-> (forall x. f (Sem rInitial x) -> Sem (effect : r) (f x))
-> (forall x. f x -> Maybe x)
-> (forall x. f (Sem rInitial x) -> Sem r (f x))
-> Sem (Tactics f (Sem rInitial) (effect : r) : r) (f a)
-> Sem r (f a)
forall (f :: * -> *) (m :: * -> *) (r2 :: [Effect]) (r :: [Effect])
a.
Functor f =>
f ()
-> (forall x. f (m x) -> Sem r2 (f x))
-> (forall x. f x -> Maybe x)
-> (forall x. f (m x) -> Sem r (f x))
-> Sem (Tactics f m r2 : r) a
-> Sem r a
runTactics f ()
s (Sem r (f x) -> Sem (effect : r) (f x)
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r (f x) -> Sem (effect : r) (f x))
-> (f (Sem rInitial x) -> Sem r (f x))
-> f (Sem rInitial x)
-> Sem (effect : r) (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. resource -> InterpreterFor (Scoped param effect) r
go resource
resource (Sem (Scoped param effect : r) (f x) -> Sem r (f x))
-> (f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x))
-> f (Sem rInitial x)
-> Sem r (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv) forall x. f x -> Maybe x
ins (resource -> InterpreterFor (Scoped param effect) r
go resource
resource (Sem (Scoped param effect : r) (f x) -> Sem r (f x))
-> (f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x))
-> f (Sem rInitial x)
-> Sem r (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv)
(resource
-> effect (Sem rInitial) a -> Tactical effect (Sem rInitial) r a
forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x
scopedHandler resource
resource effect (Sem rInitial) a
act)
InScope param
param Sem rInitial a
main ->
param -> (resource -> Sem r x) -> Sem r x
forall x. param -> (resource -> Sem r x) -> Sem r x
withResource param
param \ resource
resource' -> f a -> x
ex (f a -> x) -> Sem r (f a) -> Sem r x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> resource -> InterpreterFor (Scoped param effect) r
go resource
resource' (f (Sem rInitial a) -> Sem (Scoped param effect : r) (f a)
forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv (Sem rInitial a
main Sem rInitial a -> f () -> f (Sem rInitial a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
s))
{-# inline interpretScopedH #-}
interpretScopedH' ::
∀ resource param effect r .
(∀ e r0 x . param -> (resource -> Tactical e (Sem r0) r x) ->
Tactical e (Sem r0) r x) ->
(∀ r0 x .
resource -> effect (Sem r0) x ->
Tactical (Scoped param effect) (Sem r0) r x) ->
InterpreterFor (Scoped param effect) r
interpretScopedH' :: forall resource param (effect :: Effect) (r :: [Effect]).
(forall (e :: Effect) (r0 :: [Effect]) x.
param
-> (resource -> Tactical e (Sem r0) r x)
-> Tactical e (Sem r0) r x)
-> (forall (r0 :: [Effect]) x.
resource
-> effect (Sem r0) x
-> Tactical (Scoped param effect) (Sem r0) r x)
-> InterpreterFor (Scoped param effect) r
interpretScopedH' forall (e :: Effect) (r0 :: [Effect]) x.
param
-> (resource -> Tactical e (Sem r0) r x) -> Tactical e (Sem r0) r x
withResource forall (r0 :: [Effect]) x.
resource
-> effect (Sem r0) x -> Tactical (Scoped param effect) (Sem r0) r x
scopedHandler =
resource -> InterpreterFor (Scoped param effect) r
go ([Char] -> resource
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"top level run")
where
go :: resource -> InterpreterFor (Scoped param effect) r
go :: resource -> InterpreterFor (Scoped param effect) r
go resource
resource =
(forall (rInitial :: [Effect]) x.
Scoped param effect (Sem rInitial) x
-> Tactical (Scoped param effect) (Sem rInitial) r x)
-> Sem (Scoped param effect : r) a -> Sem r a
forall (e :: Effect) (r :: [Effect]) a.
(forall (rInitial :: [Effect]) x.
e (Sem rInitial) x -> Tactical e (Sem rInitial) r x)
-> Sem (e : r) a -> Sem r a
interpretH \case
Run effect (Sem rInitial) x
act ->
resource
-> effect (Sem rInitial) x
-> Tactical (Scoped param effect) (Sem rInitial) r x
forall (r0 :: [Effect]) x.
resource
-> effect (Sem r0) x -> Tactical (Scoped param effect) (Sem r0) r x
scopedHandler resource
resource effect (Sem rInitial) x
act
InScope param
param Sem rInitial x
main ->
param
-> (resource -> Tactical (Scoped param effect) (Sem rInitial) r x)
-> Tactical (Scoped param effect) (Sem rInitial) r x
forall (e :: Effect) (r0 :: [Effect]) x.
param
-> (resource -> Tactical e (Sem r0) r x) -> Tactical e (Sem r0) r x
withResource param
param \ resource
resource' ->
Sem r (f x)
-> Sem (WithTactics (Scoped param effect) f (Sem rInitial) r) (f x)
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r (f x)
-> Sem
(WithTactics (Scoped param effect) f (Sem rInitial) r) (f x))
-> (Sem (Scoped param effect : r) (f x) -> Sem r (f x))
-> Sem (Scoped param effect : r) (f x)
-> Sem (WithTactics (Scoped param effect) f (Sem rInitial) r) (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. resource -> InterpreterFor (Scoped param effect) r
go resource
resource' (Sem (Scoped param effect : r) (f x)
-> Sem
(WithTactics (Scoped param effect) f (Sem rInitial) r) (f x))
-> Sem
(WithTactics (Scoped param effect) f (Sem rInitial) r)
(Sem (Scoped param effect : r) (f x))
-> Sem (WithTactics (Scoped param effect) f (Sem rInitial) r) (f x)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Sem rInitial x
-> Sem
(WithTactics (Scoped param effect) f (Sem rInitial) r)
(Sem (Scoped param effect : r) (f x))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT Sem rInitial x
main
{-# inline interpretScopedH' #-}
interpretScoped ::
∀ resource param effect r .
(∀ x . param -> (resource -> Sem r x) -> Sem r x) ->
(∀ m x . resource -> effect m x -> Sem r x) ->
InterpreterFor (Scoped param effect) r
interpretScoped :: forall resource param (effect :: Effect) (r :: [Effect]).
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (forall (m :: * -> *) x. resource -> effect m x -> Sem r x)
-> InterpreterFor (Scoped param effect) r
interpretScoped forall x. param -> (resource -> Sem r x) -> Sem r x
withResource forall (m :: * -> *) x. resource -> effect m x -> Sem r x
scopedHandler =
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x)
-> InterpreterFor (Scoped param effect) r
forall resource param (effect :: Effect) (r :: [Effect]).
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r x)
-> InterpreterFor (Scoped param effect) r
interpretScopedH forall x. param -> (resource -> Sem r x) -> Sem r x
withResource \ resource
r effect (Sem r0) x
e -> Sem r x -> Sem (WithTactics effect f (Sem r0) r) (f x)
forall (m :: * -> *) (f :: * -> *) (r :: [Effect]) (e :: Effect) a.
Functor f =>
Sem r a -> Sem (WithTactics e f m r) (f a)
liftT (resource -> effect (Sem r0) x -> Sem r x
forall (m :: * -> *) x. resource -> effect m x -> Sem r x
scopedHandler resource
r effect (Sem r0) x
e)
{-# inline interpretScoped #-}
interpretScopedAs ::
∀ resource param effect r .
(param -> Sem r resource) ->
(∀ m x . resource -> effect m x -> Sem r x) ->
InterpreterFor (Scoped param effect) r
interpretScopedAs :: forall resource param (effect :: Effect) (r :: [Effect]).
(param -> Sem r resource)
-> (forall (m :: * -> *) x. resource -> effect m x -> Sem r x)
-> InterpreterFor (Scoped param effect) r
interpretScopedAs param -> Sem r resource
resource =
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (forall (m :: * -> *) x. resource -> effect m x -> Sem r x)
-> InterpreterFor (Scoped param effect) r
forall resource param (effect :: Effect) (r :: [Effect]).
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (forall (m :: * -> *) x. resource -> effect m x -> Sem r x)
-> InterpreterFor (Scoped param effect) r
interpretScoped \ param
p resource -> Sem r x
use -> resource -> Sem r x
use (resource -> Sem r x) -> Sem r resource -> Sem r x
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< param -> Sem r resource
resource param
p
{-# inline interpretScopedAs #-}
interpretScopedWithH ::
∀ extra resource param effect r r1 .
(KnownList extra, r1 ~ Append extra r) =>
(∀ x . param -> (resource -> Sem r1 x) -> Sem r x) ->
(∀ r0 x . resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x) ->
InterpreterFor (Scoped param effect) r
interpretScopedWithH :: forall (extra :: [Effect]) resource param (effect :: Effect)
(r :: [Effect]) (r1 :: [Effect]).
(KnownList extra, r1 ~ Append extra r) =>
(forall x. param -> (resource -> Sem r1 x) -> Sem r x)
-> (forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x)
-> InterpreterFor (Scoped param effect) r
interpretScopedWithH forall x. param -> (resource -> Sem r1 x) -> Sem r x
withResource forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x
scopedHandler =
(forall x.
Weaving (Scoped param effect) (Sem (Scoped param effect : r)) x
-> Sem r x)
-> InterpreterFor (Scoped param effect) r
forall (e :: Effect) (r :: [Effect]).
(forall x. Weaving e (Sem (e : r)) x -> Sem r x)
-> InterpreterFor e r
interpretWeaving \case
Weaving (InScope param
param Sem rInitial a
main) f ()
s forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
_ ->
f a -> x
ex (f a -> x) -> Sem r (f a) -> Sem r x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> param -> (resource -> Sem r1 (f a)) -> Sem r (f a)
forall x. param -> (resource -> Sem r1 x) -> Sem r x
withResource param
param \ resource
resource -> resource -> InterpreterFor (Scoped param effect) r1
inScope resource
resource (Sem (Scoped param effect : r1) (f a) -> Sem r1 (f a))
-> Sem (Scoped param effect : r1) (f a) -> Sem r1 (f a)
forall a b. (a -> b) -> a -> b
$
(forall (e :: Effect).
ElemOf e (Scoped param effect : r)
-> ElemOf e (Scoped param effect : r1))
-> Sem (Scoped param effect : r) (f a)
-> Sem (Scoped param effect : r1) (f a)
forall (r :: [Effect]) (r' :: [Effect]) a.
(forall (e :: Effect). ElemOf e r -> ElemOf e r')
-> Sem r a -> Sem r' a
restack
(SList '[Scoped param effect]
-> SList extra
-> ElemOf e (Append '[Scoped param effect] r)
-> ElemOf e (Append '[Scoped param effect] (Append extra r))
forall {a} (right :: [a]) (e :: a) (left :: [a]) (mid :: [a]).
SList left
-> SList mid
-> ElemOf e (Append left right)
-> ElemOf e (Append left (Append mid right))
injectMembership
(forall (l :: [Effect]). KnownList l => SList l
forall {a} (l :: [a]). KnownList l => SList l
singList @'[Scoped param effect])
(forall (l :: [Effect]). KnownList l => SList l
forall {a} (l :: [a]). KnownList l => SList l
singList @extra)) (Sem (Scoped param effect : r) (f a)
-> Sem (Scoped param effect : r1) (f a))
-> Sem (Scoped param effect : r) (f a)
-> Sem (Scoped param effect : r1) (f a)
forall a b. (a -> b) -> a -> b
$ f (Sem rInitial a) -> Sem (Scoped param effect : r) (f a)
forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv (Sem rInitial a
main Sem rInitial a -> f () -> f (Sem rInitial a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
s)
Weaving (Scoped param effect) (Sem (Scoped param effect : r)) x
_ ->
[Char] -> Sem r x
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"top level Run"
where
inScope :: resource -> InterpreterFor (Scoped param effect) r1
inScope :: resource -> InterpreterFor (Scoped param effect) r1
inScope resource
resource =
(forall x.
Weaving (Scoped param effect) (Sem (Scoped param effect : r1)) x
-> Sem r1 x)
-> InterpreterFor (Scoped param effect) r1
forall (e :: Effect) (r :: [Effect]).
(forall x. Weaving e (Sem (e : r)) x -> Sem r x)
-> InterpreterFor e r
interpretWeaving \case
Weaving (InScope param
param Sem rInitial a
main) f ()
s forall x.
f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
_ ->
(forall (e :: Effect). ElemOf e r -> ElemOf e r1)
-> Sem r x -> Sem r1 x
forall (r :: [Effect]) (r' :: [Effect]) a.
(forall (e :: Effect). ElemOf e r -> ElemOf e r')
-> Sem r a -> Sem r' a
restack (SList extra -> ElemOf e r -> ElemOf e (Append extra r)
forall {a} (l :: [a]) (r :: [a]) (e :: a).
SList l -> ElemOf e r -> ElemOf e (Append l r)
extendMembershipLeft (forall (l :: [Effect]). KnownList l => SList l
forall {a} (l :: [a]). KnownList l => SList l
singList @extra))
(f a -> x
ex (f a -> x) -> Sem r (f a) -> Sem r x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> param -> (resource -> Sem r1 (f a)) -> Sem r (f a)
forall x. param -> (resource -> Sem r1 x) -> Sem r x
withResource param
param \resource
resource' ->
resource -> InterpreterFor (Scoped param effect) r1
inScope resource
resource' (f (Sem rInitial a) -> Sem (Scoped param effect : r1) (f a)
forall x.
f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
wv (Sem rInitial a
main Sem rInitial a -> f () -> f (Sem rInitial a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
s)))
Weaving (Run effect (Sem rInitial) a
act) f ()
s forall x.
f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
ins ->
f a -> x
ex (f a -> x) -> Sem r1 (f a) -> Sem r1 x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f ()
-> (forall x. f (Sem rInitial x) -> Sem (effect : r1) (f x))
-> (forall x. f x -> Maybe x)
-> (forall x. f (Sem rInitial x) -> Sem r1 (f x))
-> Sem (Tactics f (Sem rInitial) (effect : r1) : r1) (f a)
-> Sem r1 (f a)
forall (f :: * -> *) (m :: * -> *) (r2 :: [Effect]) (r :: [Effect])
a.
Functor f =>
f ()
-> (forall x. f (m x) -> Sem r2 (f x))
-> (forall x. f x -> Maybe x)
-> (forall x. f (m x) -> Sem r (f x))
-> Sem (Tactics f m r2 : r) a
-> Sem r a
runTactics f ()
s (Sem r1 (f x) -> Sem (effect : r1) (f x)
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r1 (f x) -> Sem (effect : r1) (f x))
-> (f (Sem rInitial x) -> Sem r1 (f x))
-> f (Sem rInitial x)
-> Sem (effect : r1) (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. resource -> InterpreterFor (Scoped param effect) r1
inScope resource
resource (Sem (Scoped param effect : r1) (f x) -> Sem r1 (f x))
-> (f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x))
-> f (Sem rInitial x)
-> Sem r1 (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
forall x.
f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
wv) forall x. f x -> Maybe x
ins (resource -> InterpreterFor (Scoped param effect) r1
inScope resource
resource (Sem (Scoped param effect : r1) (f x) -> Sem r1 (f x))
-> (f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x))
-> f (Sem rInitial x)
-> Sem r1 (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
forall x.
f (Sem rInitial x) -> Sem (Scoped param effect : r1) (f x)
wv)
(resource
-> effect (Sem rInitial) a -> Tactical effect (Sem rInitial) r1 a
forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x
scopedHandler resource
resource effect (Sem rInitial) a
act)
{-# inline interpretScopedWithH #-}
interpretScopedWith ::
∀ extra param resource effect r r1 .
(r1 ~ Append extra r, KnownList extra) =>
(∀ x . param -> (resource -> Sem r1 x) -> Sem r x) ->
(∀ m x . resource -> effect m x -> Sem r1 x) ->
InterpreterFor (Scoped param effect) r
interpretScopedWith :: forall (extra :: [Effect]) param resource (effect :: Effect)
(r :: [Effect]) (r1 :: [Effect]).
(r1 ~ Append extra r, KnownList extra) =>
(forall x. param -> (resource -> Sem r1 x) -> Sem r x)
-> (forall (m :: * -> *) x. resource -> effect m x -> Sem r1 x)
-> InterpreterFor (Scoped param effect) r
interpretScopedWith forall x. param -> (resource -> Sem r1 x) -> Sem r x
withResource forall (m :: * -> *) x. resource -> effect m x -> Sem r1 x
scopedHandler =
forall (extra :: [Effect]) resource param (effect :: Effect)
(r :: [Effect]) (r1 :: [Effect]).
(KnownList extra, r1 ~ Append extra r) =>
(forall x. param -> (resource -> Sem r1 x) -> Sem r x)
-> (forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x)
-> InterpreterFor (Scoped param effect) r
interpretScopedWithH @extra forall x. param -> (resource -> Sem r1 x) -> Sem r x
withResource \ resource
r effect (Sem r0) x
e -> Sem r1 x -> Sem (WithTactics effect f (Sem r0) r1) (f x)
forall (m :: * -> *) (f :: * -> *) (r :: [Effect]) (e :: Effect) a.
Functor f =>
Sem r a -> Sem (WithTactics e f m r) (f a)
liftT (resource -> effect (Sem r0) x -> Sem r1 x
forall (m :: * -> *) x. resource -> effect m x -> Sem r1 x
scopedHandler resource
r effect (Sem r0) x
e)
{-# inline interpretScopedWith #-}
interpretScopedWith_ ::
∀ extra param effect r r1 .
(r1 ~ Append extra r, KnownList extra) =>
(∀ x . param -> Sem r1 x -> Sem r x) ->
(∀ m x . effect m x -> Sem r1 x) ->
InterpreterFor (Scoped param effect) r
interpretScopedWith_ :: forall (extra :: [Effect]) param (effect :: Effect) (r :: [Effect])
(r1 :: [Effect]).
(r1 ~ Append extra r, KnownList extra) =>
(forall x. param -> Sem r1 x -> Sem r x)
-> (forall (m :: * -> *) x. effect m x -> Sem r1 x)
-> InterpreterFor (Scoped param effect) r
interpretScopedWith_ forall x. param -> Sem r1 x -> Sem r x
withResource forall (m :: * -> *) x. effect m x -> Sem r1 x
scopedHandler =
forall (extra :: [Effect]) resource param (effect :: Effect)
(r :: [Effect]) (r1 :: [Effect]).
(KnownList extra, r1 ~ Append extra r) =>
(forall x. param -> (resource -> Sem r1 x) -> Sem r x)
-> (forall (r0 :: [Effect]) x.
resource -> effect (Sem r0) x -> Tactical effect (Sem r0) r1 x)
-> InterpreterFor (Scoped param effect) r
interpretScopedWithH @extra (\ param
p () -> Sem r1 x
f -> param -> Sem r1 x -> Sem r x
forall x. param -> Sem r1 x -> Sem r x
withResource param
p (() -> Sem r1 x
f ())) \ () effect (Sem r0) x
e -> Sem r1 x -> Sem (WithTactics effect f (Sem r0) r1) (f x)
forall (m :: * -> *) (f :: * -> *) (r :: [Effect]) (e :: Effect) a.
Functor f =>
Sem r a -> Sem (WithTactics e f m r) (f a)
liftT (effect (Sem r0) x -> Sem r1 x
forall (m :: * -> *) x. effect m x -> Sem r1 x
scopedHandler effect (Sem r0) x
e)
{-# inline interpretScopedWith_ #-}
runScoped ::
∀ resource param effect r .
(∀ x . param -> (resource -> Sem r x) -> Sem r x) ->
(resource -> InterpreterFor effect r) ->
InterpreterFor (Scoped param effect) r
runScoped :: forall resource param (effect :: Effect) (r :: [Effect]).
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (resource -> InterpreterFor effect r)
-> InterpreterFor (Scoped param effect) r
runScoped forall x. param -> (resource -> Sem r x) -> Sem r x
withResource resource -> InterpreterFor effect r
scopedInterpreter =
resource -> InterpreterFor (Scoped param effect) r
go ([Char] -> resource
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"top level run")
where
go :: resource -> InterpreterFor (Scoped param effect) r
go :: resource -> InterpreterFor (Scoped param effect) r
go resource
resource =
(forall x.
Weaving (Scoped param effect) (Sem (Scoped param effect : r)) x
-> Sem r x)
-> InterpreterFor (Scoped param effect) r
forall (e :: Effect) (r :: [Effect]).
(forall x. Weaving e (Sem (e : r)) x -> Sem r x)
-> InterpreterFor e r
interpretWeaving \ (Weaving Scoped param effect (Sem rInitial) a
effect f ()
s forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv f a -> x
ex forall x. f x -> Maybe x
ins) -> case Scoped param effect (Sem rInitial) a
effect of
Run effect (Sem rInitial) a
act ->
resource -> InterpreterFor effect r
scopedInterpreter resource
resource
(Sem (effect : r) x -> Sem r x) -> Sem (effect : r) x -> Sem r x
forall a b. (a -> b) -> a -> b
$ Union (effect : r) (Sem (effect : r)) x -> Sem (effect : r) x
forall (r :: [Effect]) a. Union r (Sem r) a -> Sem r a
liftSem (Union (effect : r) (Sem (effect : r)) x -> Sem (effect : r) x)
-> Union (effect : r) (Sem (effect : r)) x -> Sem (effect : r) x
forall a b. (a -> b) -> a -> b
$ Weaving effect (Sem (effect : r)) x
-> Union (effect : r) (Sem (effect : r)) x
forall (e :: Effect) (r :: [Effect]) (m :: * -> *) a.
Member e r =>
Weaving e m a -> Union r m a
injWeaving (Weaving effect (Sem (effect : r)) x
-> Union (effect : r) (Sem (effect : r)) x)
-> Weaving effect (Sem (effect : r)) x
-> Union (effect : r) (Sem (effect : r)) x
forall a b. (a -> b) -> a -> b
$ effect (Sem rInitial) a
-> f ()
-> (forall x. f (Sem rInitial x) -> Sem (effect : r) (f x))
-> (f a -> x)
-> (forall x. f x -> Maybe x)
-> Weaving effect (Sem (effect : r)) x
forall (f :: * -> *) (e :: Effect) (rInitial :: [Effect]) 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 effect (Sem rInitial) a
act f ()
s (Sem r (f x) -> Sem (effect : r) (f x)
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r (f x) -> Sem (effect : r) (f x))
-> (f (Sem rInitial x) -> Sem r (f x))
-> f (Sem rInitial x)
-> Sem (effect : r) (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. resource -> InterpreterFor (Scoped param effect) r
go resource
resource (Sem (Scoped param effect : r) (f x) -> Sem r (f x))
-> (f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x))
-> f (Sem rInitial x)
-> Sem r (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv) f a -> x
ex forall x. f x -> Maybe x
ins
InScope param
param Sem rInitial a
main ->
param -> (resource -> Sem r x) -> Sem r x
forall x. param -> (resource -> Sem r x) -> Sem r x
withResource param
param \ resource
resource' -> f a -> x
ex (f a -> x) -> Sem r (f a) -> Sem r x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> resource -> InterpreterFor (Scoped param effect) r
go resource
resource' (f (Sem rInitial a) -> Sem (Scoped param effect : r) (f a)
forall x. f (Sem rInitial x) -> Sem (Scoped param effect : r) (f x)
wv (Sem rInitial a
main Sem rInitial a -> f () -> f (Sem rInitial a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
s))
{-# inline runScoped #-}
runScopedAs ::
∀ resource param effect r .
(param -> Sem r resource) ->
(resource -> InterpreterFor effect r) ->
InterpreterFor (Scoped param effect) r
runScopedAs :: forall resource param (effect :: Effect) (r :: [Effect]).
(param -> Sem r resource)
-> (resource -> InterpreterFor effect r)
-> InterpreterFor (Scoped param effect) r
runScopedAs param -> Sem r resource
resource = (forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (resource -> InterpreterFor effect r)
-> InterpreterFor (Scoped param effect) r
forall resource param (effect :: Effect) (r :: [Effect]).
(forall x. param -> (resource -> Sem r x) -> Sem r x)
-> (resource -> InterpreterFor effect r)
-> InterpreterFor (Scoped param effect) r
runScoped \ param
p resource -> Sem r x
use -> resource -> Sem r x
use (resource -> Sem r x) -> Sem r resource -> Sem r x
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< param -> Sem r resource
resource param
p
{-# inline runScopedAs #-}