{-# language AllowAmbiguousTypes #-}

module Polysemy.Scoped (
  -- * Effect
  Scoped,

  -- * Constructors
  scoped,
  scoped_,
  rescope,

  -- * Interpreters
  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

-- | Construct an interpreter for a higher-order effect wrapped in a 'Scoped',
-- given a resource allocation function and a parameterized handler for the
-- plain effect.
--
-- This combinator is analogous to 'interpretH' in that it allows the handler to
-- use the 'Tactical' environment and transforms the effect into other effects
-- on the stack.
interpretScopedH ::
   resource param effect r .
  -- | A callback function that allows the user to acquire a resource for each
  -- computation wrapped by 'scoped' using other effects, with an additional
  -- argument that contains the call site parameter passed to 'scoped'.
  ( x . param -> (resource -> Sem r x) -> Sem r x) ->
  -- | A handler like the one expected by 'interpretH' with an additional
  -- parameter that contains the @resource@ allocated by the first argument.
  ( 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 =
  -- TODO investigate whether loopbreaker optimization is effective here
  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 #-}

-- | Variant of 'interpretScopedH' that allows the resource acquisition function
-- to use 'Tactical'.
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' #-}

-- | First-order variant of '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 #-}

-- | Variant of 'interpretScoped' in which the resource allocator is a plain
-- action.
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 #-}

-- | Higher-order interpreter for 'Scoped' that allows the handler to use
-- additional effects that are interpreted by the resource allocator.
--
-- /Note/: It is necessary to specify the list of local interpreters with a type
-- application; GHC won't be able to figure them out from the type of
-- @withResource@.
--
-- As an example for a higher order effect, consider a mutexed concurrent state
-- effect, where an effectful function may lock write access to the state while
-- making it still possible to read it:
--
-- > data MState s :: Effect where
-- >   MState :: (s -> m (s, a)) -> MState s m a
-- >   MRead :: MState s m s
-- >
-- > makeSem ''MState
--
-- We can now use an 'Polysemy.AtomicState.AtomicState' to store the current
-- value and lock write access with an @MVar@. Since the state callback is
-- effectful, we need a higher order interpreter:
--
-- > withResource ::
-- >   Member (Embed IO) r =>
-- >   s ->
-- >   (MVar () -> Sem (AtomicState s : r) a) ->
-- >   Sem r a
-- > withResource initial use = do
-- >   tv <- embed (newTVarIO initial)
-- >   lock <- embed (newMVar ())
-- >   runAtomicStateTVar tv $ use lock
-- >
-- > interpretMState ::
-- >   ∀ s r .
-- >   Members [Resource, Embed IO] r =>
-- >   InterpreterFor (Scoped s (MState s)) r
-- > interpretMState =
-- >   interpretScopedWithH @'[AtomicState s] withResource \ lock -> \case
-- >     MState f ->
-- >       bracket_ (embed (takeMVar lock)) (embed (tryPutMVar lock ())) do
-- >         s0 <- atomicGet
-- >         res <- runTSimple (f s0)
-- >         Inspector ins <- getInspectorT
-- >         for_ (ins res) \ (s, _) -> atomicPut s
-- >         pure (snd <$> res)
-- >     MRead ->
-- >       liftT atomicGet
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 #-}

-- | First-order variant of 'interpretScopedWithH'.
--
-- /Note/: It is necessary to specify the list of local interpreters with a type
-- application; GHC won't be able to figure them out from the type of
-- @withResource@:
--
-- > data SomeAction :: Effect where
-- >   SomeAction :: SomeAction m ()
-- >
-- > foo :: InterpreterFor (Scoped () SomeAction) r
-- > foo =
-- >   interpretScopedWith @[Reader Int, State Bool] localEffects \ () -> \case
-- >     SomeAction -> put . (> 0) =<< ask @Int
-- >   where
-- >     localEffects () use = evalState False (runReader 5 (use ()))
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 #-}

-- | Variant of 'interpretScopedWith' in which no resource is used and the
-- resource allocator is a plain interpreter.
-- This is useful for scopes that only need local effects, but no resources in
-- the handler.
--
-- See the /Note/ on 'interpretScopedWithH'.
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_ #-}

-- | Variant of 'interpretScoped' that uses another interpreter instead of a
-- handler.
--
-- This is mostly useful if you want to reuse an interpreter that you cannot
-- easily rewrite (like from another library). If you have full control over the
-- implementation, 'interpretScoped' should be preferred.
--
-- /Note/: The wrapped interpreter will be executed fully, including the
-- initializing code surrounding its handler, for each action in the program, so
-- if the interpreter allocates any resources, they will be scoped to a single
-- action. Move them to @withResource@ instead.
--
-- For example, consider the following interpreter for
-- 'Polysemy.AtomicState.AtomicState':
--
-- > atomicTVar :: Member (Embed IO) r => a -> InterpreterFor (AtomicState a) r
-- > atomicTVar initial sem = do
-- >   tv <- embed (newTVarIO initial)
-- >   runAtomicStateTVar tv sem
--
-- If this interpreter were used for a scoped version of @AtomicState@ like
-- this:
--
-- > runScoped (\ initial use -> use initial) \ initial -> atomicTVar initial
--
-- Then the @TVar@ would be created every time an @AtomicState@ action is run,
-- not just when entering the scope.
--
-- The proper way to implement this would be to rewrite the resource allocation:
--
-- > runScoped (\ initial use -> use =<< embed (newTVarIO initial)) runAtomicStateTVar
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 #-}

-- | Variant of 'runScoped' in which the resource allocator returns the resource
-- rather tnen calling a continuation.
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 #-}