{-# LANGUAGE TemplateHaskell #-}

module Polysemy.Resource
  ( -- * Effect
    Resource (..)

    -- * Actions
  , bracket
  , bracket_
  , bracketOnError
  , finally
  , onException

    -- * Interpretations
  , runResource
  , resourceToIOFinal
  , resourceToIO
  , lowerResource
  ) where

import qualified Control.Exception as X
import           Polysemy
import           Polysemy.Final


------------------------------------------------------------------------------
-- | An effect capable of providing 'X.bracket' semantics. Interpreters for this
-- will successfully run the deallocation action even in the presence of other
-- short-circuiting effects.
data Resource m a where
  Bracket
    :: m a
       -- Action to allocate a resource.
    -> (a -> m c)
       -- Action to cleanup the resource. This is guaranteed to be
       -- called.
    -> (a -> m b)
       -- Action which uses the resource.
    -> Resource m b
  BracketOnError
    :: m a
       -- Action to allocate a resource.
    -> (a -> m c)
       -- Action to cleanup the resource. This will only be called if the
       -- "use" block fails.
    -> (a -> m b)
       -- Action which uses the resource.
    -> Resource m b

makeSem ''Resource

------------------------------------------------------------------------------
-- | A variant of 'bracket' where the return value from the first computation
-- is not required.
--
-- cf. 'Control.Exception.bracket' and 'Control.Exception.bracket_'
--
-- @since 1.5.0.0
bracket_
    :: Member Resource r
    => Sem r a -- ^ computation to run first
    -> Sem r b -- ^ computation to run last (even if an exception was raised)
    -> Sem r c -- ^ computation to run in-between
    -> Sem r c
bracket_ :: Sem r a -> Sem r b -> Sem r c -> Sem r c
bracket_ Sem r a
begin Sem r b
end Sem r c
act = Sem r a -> (a -> Sem r b) -> (a -> Sem r c) -> Sem r c
forall (r :: [Effect]) a c b.
MemberWithError Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
bracket Sem r a
begin (Sem r b -> a -> Sem r b
forall a b. a -> b -> a
const Sem r b
end) (Sem r c -> a -> Sem r c
forall a b. a -> b -> a
const Sem r c
act)

------------------------------------------------------------------------------
-- | Like 'bracket', but for the simple case of one computation to run
-- afterward.
--
-- @since 0.4.0.0
finally
    :: Member Resource r
    => Sem r a -- ^ computation to run first
    -> Sem r b -- ^ computation to run afterward (even if an exception was raised)
    -> Sem r a
finally :: Sem r a -> Sem r b -> Sem r a
finally Sem r a
act Sem r b
end = Sem r () -> (() -> Sem r b) -> (() -> Sem r a) -> Sem r a
forall (r :: [Effect]) a c b.
MemberWithError Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
bracket (() -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (Sem r b -> () -> Sem r b
forall a b. a -> b -> a
const Sem r b
end) (Sem r a -> () -> Sem r a
forall a b. a -> b -> a
const Sem r a
act)


------------------------------------------------------------------------------
-- | Like 'bracketOnError', but for the simple case of one computation to run
-- afterward.
--
-- @since 0.4.0.0
onException
    :: Member Resource r
    => Sem r a -- ^ computation to run first
    -> Sem r b -- ^ computation to run afterward if an exception was raised
    -> Sem r a
onException :: Sem r a -> Sem r b -> Sem r a
onException Sem r a
act Sem r b
end = Sem r () -> (() -> Sem r b) -> (() -> Sem r a) -> Sem r a
forall (r :: [Effect]) a c b.
MemberWithError Resource r =>
Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
bracketOnError (() -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (Sem r b -> () -> Sem r b
forall a b. a -> b -> a
const Sem r b
end) (Sem r a -> () -> Sem r a
forall a b. a -> b -> a
const Sem r a
act)

------------------------------------------------------------------------------
-- | Run a 'Resource' effect in terms of 'X.bracket' through final 'IO'
--
-- /Beware/: Effects that aren't interpreted in terms of 'IO'
-- will have local state semantics in regards to 'Resource' effects
-- interpreted this way. See 'Final'.
--
-- Notably, unlike 'resourceToIO', this is not consistent with
-- 'Polysemy.State.State' unless 'Polysemy.State.runStateInIORef' is used.
-- State that seems like it should be threaded globally throughout 'bracket's
-- /will not be./
--
-- Use 'resourceToIO' instead if you need to run
-- pure, stateful interpreters after the interpreter for 'Resource'.
-- (Pure interpreters are interpreters that aren't expressed in terms of
-- another effect or monad; for example, 'Polysemy.State.runState'.)
--
-- @since 1.2.0.0
resourceToIOFinal :: Member (Final IO) r
                  => Sem (Resource ': r) a
                  -> Sem r a
resourceToIOFinal :: Sem (Resource : r) a -> Sem r a
resourceToIOFinal = (forall x (rInitial :: [Effect]).
 Resource (Sem rInitial) x -> Strategic IO (Sem rInitial) x)
-> Sem (Resource : r) a -> Sem r a
forall (m :: * -> *) (e :: Effect) (r :: [Effect]) a.
Member (Final m) r =>
(forall x (rInitial :: [Effect]).
 e (Sem rInitial) x -> Strategic m (Sem rInitial) x)
-> Sem (e : r) a -> Sem r a
interpretFinal ((forall x (rInitial :: [Effect]).
  Resource (Sem rInitial) x -> Strategic IO (Sem rInitial) x)
 -> Sem (Resource : r) a -> Sem r a)
-> (forall x (rInitial :: [Effect]).
    Resource (Sem rInitial) x -> Strategic IO (Sem rInitial) x)
-> Sem (Resource : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
  Bracket alloc dealloc use -> do
    IO (f a)
a <- Sem rInitial a -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f a))
forall (n :: * -> *) a (m :: * -> *) (f :: * -> *).
n a -> Sem (WithStrategy m f n) (m (f a))
runS  Sem rInitial a
alloc
    f a -> IO (f c)
d <- (a -> Sem rInitial c)
-> Sem (WithStrategy IO f (Sem rInitial)) (f a -> IO (f c))
forall a (n :: * -> *) b (m :: * -> *) (f :: * -> *).
(a -> n b) -> Sem (WithStrategy m f n) (f a -> m (f b))
bindS a -> Sem rInitial c
dealloc
    f a -> IO (f x)
u <- (a -> Sem rInitial x)
-> Sem (WithStrategy IO f (Sem rInitial)) (f a -> IO (f x))
forall a (n :: * -> *) b (m :: * -> *) (f :: * -> *).
(a -> n b) -> Sem (WithStrategy m f n) (f a -> m (f b))
bindS a -> Sem rInitial x
use
    IO (f x) -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f x))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IO (f x) -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f x)))
-> IO (f x) -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f x))
forall a b. (a -> b) -> a -> b
$ IO (f a) -> (f a -> IO (f c)) -> (f a -> IO (f x)) -> IO (f x)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
X.bracket IO (f a)
a f a -> IO (f c)
d f a -> IO (f x)
u

  BracketOnError alloc dealloc use -> do
    Inspector f
ins <- Sem (WithStrategy IO f (Sem rInitial)) (Inspector f)
forall (m :: * -> *) (f :: * -> *) (n :: * -> *).
Sem (WithStrategy m f n) (Inspector f)
getInspectorS
    IO (f a)
a <- Sem rInitial a -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f a))
forall (n :: * -> *) a (m :: * -> *) (f :: * -> *).
n a -> Sem (WithStrategy m f n) (m (f a))
runS  Sem rInitial a
alloc
    f a -> IO (f c)
d <- (a -> Sem rInitial c)
-> Sem (WithStrategy IO f (Sem rInitial)) (f a -> IO (f c))
forall a (n :: * -> *) b (m :: * -> *) (f :: * -> *).
(a -> n b) -> Sem (WithStrategy m f n) (f a -> m (f b))
bindS a -> Sem rInitial c
dealloc
    f a -> IO (f x)
u <- (a -> Sem rInitial x)
-> Sem (WithStrategy IO f (Sem rInitial)) (f a -> IO (f x))
forall a (n :: * -> *) b (m :: * -> *) (f :: * -> *).
(a -> n b) -> Sem (WithStrategy m f n) (f a -> m (f b))
bindS a -> Sem rInitial x
use
    IO (f x) -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f x))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IO (f x) -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f x)))
-> IO (f x) -> Sem (WithStrategy IO f (Sem rInitial)) (IO (f x))
forall a b. (a -> b) -> a -> b
$
      IO (f a) -> (f a -> IO (f c)) -> (f a -> IO (f x)) -> IO (f x)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
X.bracketOnError
        IO (f a)
a
        f a -> IO (f c)
d
        (\f a
x -> do
          f x
result <- f a -> IO (f x)
u f a
x
          case Inspector f -> f x -> Maybe x
forall (f :: * -> *). Inspector f -> forall x. f x -> Maybe x
inspect Inspector f
ins f x
result of
            Just x
_ -> f x -> IO (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result
            Maybe x
Nothing -> do
              f c
_ <- f a -> IO (f c)
d f a
x
              f x -> IO (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result
        )

{-# INLINE resourceToIOFinal #-}


------------------------------------------------------------------------------
-- | Run a 'Resource' effect in terms of 'X.bracket'.
--
-- @since 1.0.0.0
lowerResource
    ::  r a
     . Member (Embed IO) r
    => ( x. Sem r x -> IO x)
       -- ^ Strategy for lowering a 'Sem' action down to 'IO'. This is likely
       -- some combination of 'runM' and other interpreters composed via '.@'.
    -> Sem (Resource ': r) a
    -> Sem r a
lowerResource :: (forall x. Sem r x -> IO x) -> Sem (Resource : r) a -> Sem r a
lowerResource forall x. Sem r x -> IO x
finish = (forall x (rInitial :: [Effect]).
 Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a -> Sem r a
forall (e :: Effect) (r :: [Effect]) a.
(forall x (rInitial :: [Effect]).
 e (Sem rInitial) x -> Tactical e (Sem rInitial) r x)
-> Sem (e : r) a -> Sem r a
interpretH ((forall x (rInitial :: [Effect]).
  Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
 -> Sem (Resource : r) a -> Sem r a)
-> (forall x (rInitial :: [Effect]).
    Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
  Bracket alloc dealloc use -> do
    Sem (Resource : r) (f a)
a <- Sem rInitial a
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (Sem (Resource : r) (f a))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT  Sem rInitial a
alloc
    f a -> Sem (Resource : r) (f c)
d <- (a -> Sem rInitial c)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f c))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial c
dealloc
    f a -> Sem (Resource : r) (f x)
u <- (a -> Sem rInitial x)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f x))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial x
use

    let run_it :: Sem (Resource ': r) x -> IO x
        run_it :: Sem (Resource : r) x -> IO x
run_it = forall x. Sem r x -> IO x
finish (forall x. Sem r x -> IO x)
-> (forall y.
    (forall x. Sem r x -> IO x) -> Sem (Resource : r) y -> Sem r y)
-> Sem (Resource : r) x
-> IO x
forall (m :: * -> *) (r :: [Effect]) (e :: Effect) z.
Monad m =>
(forall x. Sem r x -> m x)
-> (forall y.
    (forall x. Sem r x -> m x) -> Sem (e : r) y -> Sem r y)
-> Sem (e : r) z
-> m z
.@ forall (r :: [Effect]) a.
Member (Embed IO) r =>
(forall x. Sem r x -> IO x) -> Sem (Resource : r) a -> Sem r a
forall y.
(forall x. Sem r x -> IO x) -> Sem (Resource : r) y -> Sem r y
lowerResource

    IO (f x) -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (m :: * -> *) (r :: [Effect]) a.
Member (Embed m) r =>
m a -> Sem r a
embed (IO (f x) -> Sem (WithTactics Resource f (Sem rInitial) r) (f x))
-> IO (f x) -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall a b. (a -> b) -> a -> b
$ IO (f a) -> (f a -> IO (f c)) -> (f a -> IO (f x)) -> IO (f x)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
X.bracket (Sem (Resource : r) (f a) -> IO (f a)
forall x. Sem (Resource : r) x -> IO x
run_it Sem (Resource : r) (f a)
a) (Sem (Resource : r) (f c) -> IO (f c)
forall x. Sem (Resource : r) x -> IO x
run_it (Sem (Resource : r) (f c) -> IO (f c))
-> (f a -> Sem (Resource : r) (f c)) -> f a -> IO (f c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Sem (Resource : r) (f c)
d) (Sem (Resource : r) (f x) -> IO (f x)
forall x. Sem (Resource : r) x -> IO x
run_it (Sem (Resource : r) (f x) -> IO (f x))
-> (f a -> Sem (Resource : r) (f x)) -> f a -> IO (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Sem (Resource : r) (f x)
u)

  BracketOnError alloc dealloc use -> do
    Sem (Resource : r) (f a)
a <- Sem rInitial a
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (Sem (Resource : r) (f a))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT  Sem rInitial a
alloc
    f a -> Sem (Resource : r) (f c)
d <- (a -> Sem rInitial c)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f c))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial c
dealloc
    f a -> Sem (Resource : r) (f x)
u <- (a -> Sem rInitial x)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f x))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial x
use

    let run_it :: Sem (Resource ': r) x -> IO x
        run_it :: Sem (Resource : r) x -> IO x
run_it = forall x. Sem r x -> IO x
finish (forall x. Sem r x -> IO x)
-> (forall y.
    (forall x. Sem r x -> IO x) -> Sem (Resource : r) y -> Sem r y)
-> Sem (Resource : r) x
-> IO x
forall (m :: * -> *) (r :: [Effect]) (e :: Effect) z.
Monad m =>
(forall x. Sem r x -> m x)
-> (forall y.
    (forall x. Sem r x -> m x) -> Sem (e : r) y -> Sem r y)
-> Sem (e : r) z
-> m z
.@ forall (r :: [Effect]) a.
Member (Embed IO) r =>
(forall x. Sem r x -> IO x) -> Sem (Resource : r) a -> Sem r a
forall y.
(forall x. Sem r x -> IO x) -> Sem (Resource : r) y -> Sem r y
lowerResource

    IO (f x) -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (m :: * -> *) (r :: [Effect]) a.
Member (Embed m) r =>
m a -> Sem r a
embed (IO (f x) -> Sem (WithTactics Resource f (Sem rInitial) r) (f x))
-> IO (f x) -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall a b. (a -> b) -> a -> b
$ IO (f a) -> (f a -> IO (f c)) -> (f a -> IO (f x)) -> IO (f x)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
X.bracketOnError (Sem (Resource : r) (f a) -> IO (f a)
forall x. Sem (Resource : r) x -> IO x
run_it Sem (Resource : r) (f a)
a) (Sem (Resource : r) (f c) -> IO (f c)
forall x. Sem (Resource : r) x -> IO x
run_it (Sem (Resource : r) (f c) -> IO (f c))
-> (f a -> Sem (Resource : r) (f c)) -> f a -> IO (f c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Sem (Resource : r) (f c)
d) (Sem (Resource : r) (f x) -> IO (f x)
forall x. Sem (Resource : r) x -> IO x
run_it (Sem (Resource : r) (f x) -> IO (f x))
-> (f a -> Sem (Resource : r) (f x)) -> f a -> IO (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Sem (Resource : r) (f x)
u)
{-# INLINE lowerResource #-}
{-# DEPRECATED lowerResource "Use 'resourceToIOFinal' instead" #-}


------------------------------------------------------------------------------
-- | Run a 'Resource' effect purely.
--
-- @since 1.0.0.0
runResource
    ::  r a
     . Sem (Resource ': r) a
    -> Sem r a
runResource :: Sem (Resource : r) a -> Sem r a
runResource = (forall x (rInitial :: [Effect]).
 Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a -> Sem r a
forall (e :: Effect) (r :: [Effect]) a.
(forall x (rInitial :: [Effect]).
 e (Sem rInitial) x -> Tactical e (Sem rInitial) r x)
-> Sem (e : r) a -> Sem r a
interpretH ((forall x (rInitial :: [Effect]).
  Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
 -> Sem (Resource : r) a -> Sem r a)
-> (forall x (rInitial :: [Effect]).
    Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
  Bracket alloc dealloc use -> do
    Sem (Resource : r) (f a)
a <- Sem rInitial a
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (Sem (Resource : r) (f a))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT  Sem rInitial a
alloc
    f a -> Sem (Resource : r) (f c)
d <- (a -> Sem rInitial c)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f c))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial c
dealloc
    f a -> Sem (Resource : r) (f x)
u <- (a -> Sem rInitial x)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f x))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial x
use

    let run_it :: Sem (Resource : r) a -> Sem (e : r) a
run_it = Sem r a -> Sem (e : r) a
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r a -> Sem (e : r) a)
-> (Sem (Resource : r) a -> Sem r a)
-> Sem (Resource : r) a
-> Sem (e : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (Resource : r) a -> Sem r a
forall (r :: [Effect]) a. Sem (Resource : r) a -> Sem r a
runResource
    f a
resource <- Sem (Resource : r) (f a)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f a)
forall (r :: [Effect]) a (e :: Effect).
Sem (Resource : r) a -> Sem (e : r) a
run_it Sem (Resource : r) (f a)
a
    f x
result <- Sem (Resource : r) (f x)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (r :: [Effect]) a (e :: Effect).
Sem (Resource : r) a -> Sem (e : r) a
run_it (Sem (Resource : r) (f x)
 -> Sem (WithTactics Resource f (Sem rInitial) r) (f x))
-> Sem (Resource : r) (f x)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall a b. (a -> b) -> a -> b
$ f a -> Sem (Resource : r) (f x)
u f a
resource
    f c
_ <- Sem (Resource : r) (f c)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f c)
forall (r :: [Effect]) a (e :: Effect).
Sem (Resource : r) a -> Sem (e : r) a
run_it (Sem (Resource : r) (f c)
 -> Sem (WithTactics Resource f (Sem rInitial) r) (f c))
-> Sem (Resource : r) (f c)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f c)
forall a b. (a -> b) -> a -> b
$ f a -> Sem (Resource : r) (f c)
d f a
resource
    f x -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result

  BracketOnError alloc dealloc use -> do
    Sem (Resource : r) (f a)
a <- Sem rInitial a
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (Sem (Resource : r) (f a))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT  Sem rInitial a
alloc
    f a -> Sem (Resource : r) (f c)
d <- (a -> Sem rInitial c)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f c))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial c
dealloc
    f a -> Sem (Resource : r) (f x)
u <- (a -> Sem rInitial x)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f x))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial x
use

    let run_it :: Sem (Resource : r) a -> Sem (e : r) a
run_it = Sem r a -> Sem (e : r) a
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r a -> Sem (e : r) a)
-> (Sem (Resource : r) a -> Sem r a)
-> Sem (Resource : r) a
-> Sem (e : r) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (Resource : r) a -> Sem r a
forall (r :: [Effect]) a. Sem (Resource : r) a -> Sem r a
runResource

    f a
resource <- Sem (Resource : r) (f a)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f a)
forall (r :: [Effect]) a (e :: Effect).
Sem (Resource : r) a -> Sem (e : r) a
run_it Sem (Resource : r) (f a)
a
    f x
result <- Sem (Resource : r) (f x)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (r :: [Effect]) a (e :: Effect).
Sem (Resource : r) a -> Sem (e : r) a
run_it (Sem (Resource : r) (f x)
 -> Sem (WithTactics Resource f (Sem rInitial) r) (f x))
-> Sem (Resource : r) (f x)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall a b. (a -> b) -> a -> b
$ f a -> Sem (Resource : r) (f x)
u f a
resource

    Inspector f
ins <- Sem (WithTactics Resource f (Sem rInitial) r) (Inspector f)
forall (e :: Effect) (f :: * -> *) (m :: * -> *) (r :: [Effect]).
Sem (WithTactics e f m r) (Inspector f)
getInspectorT
    case Inspector f -> f x -> Maybe x
forall (f :: * -> *). Inspector f -> forall x. f x -> Maybe x
inspect Inspector f
ins f x
result of
      Just x
_ -> f x -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result
      Maybe x
Nothing -> do
        f c
_ <- Sem (Resource : r) (f c)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f c)
forall (r :: [Effect]) a (e :: Effect).
Sem (Resource : r) a -> Sem (e : r) a
run_it (Sem (Resource : r) (f c)
 -> Sem (WithTactics Resource f (Sem rInitial) r) (f c))
-> Sem (Resource : r) (f c)
-> Sem (WithTactics Resource f (Sem rInitial) r) (f c)
forall a b. (a -> b) -> a -> b
$ f a -> Sem (Resource : r) (f c)
d f a
resource
        f x -> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result
{-# INLINE runResource #-}


------------------------------------------------------------------------------
-- | A more flexible --- though less safe ---  version of 'resourceToIOFinal'
--
-- This function is capable of running 'Resource' effects anywhere within an
-- effect stack, without relying on an explicit function to lower it into 'IO'.
-- Notably, this means that 'Polysemy.State.State' effects will be consistent
-- in the presence of 'Resource'.
--
-- ResourceToIO' is safe whenever you're concerned about exceptions thrown
-- by effects _already handled_ in your effect stack, or in 'IO' code run
-- directly inside of 'bracket'. It is not safe against exceptions thrown
-- explicitly at the main thread. If this is not safe enough for your use-case,
-- use 'resourceToIOFinal' instead.
--
-- This function creates a thread, and so should be compiled with @-threaded@.
--
-- @since 1.0.0.0
resourceToIO
    :: forall r a
     . Member (Embed IO) r
    => Sem (Resource ': r) a
    -> Sem r a
resourceToIO :: Sem (Resource : r) a -> Sem r a
resourceToIO = (forall x (rInitial :: [Effect]).
 Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a -> Sem r a
forall (e :: Effect) (r :: [Effect]) a.
(forall x (rInitial :: [Effect]).
 e (Sem rInitial) x -> Tactical e (Sem rInitial) r x)
-> Sem (e : r) a -> Sem r a
interpretH ((forall x (rInitial :: [Effect]).
  Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
 -> Sem (Resource : r) a -> Sem r a)
-> (forall x (rInitial :: [Effect]).
    Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
  Bracket a b c -> do
    Sem (Resource : r) (f a)
ma <- Sem rInitial a
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (Sem (Resource : r) (f a))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT Sem rInitial a
a
    f a -> Sem (Resource : r) (f c)
mb <- (a -> Sem rInitial c)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f c))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial c
b
    f a -> Sem (Resource : r) (f x)
mc <- (a -> Sem rInitial x)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f x))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial x
c

    ((forall x.
  Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
 -> IO () -> IO (f x))
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (r :: [Effect]) a.
Member (Embed IO) r =>
((forall x. Sem r x -> IO x) -> IO () -> IO a) -> Sem r a
withLowerToIO (((forall x.
   Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
  -> IO () -> IO (f x))
 -> Sem (WithTactics Resource f (Sem rInitial) r) (f x))
-> ((forall x.
     Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
    -> IO () -> IO (f x))
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall a b. (a -> b) -> a -> b
$ \forall x. Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x
lower IO ()
finish -> do
      let done :: Sem (Resource ': r) x -> IO x
          done :: Sem (Resource : r) x -> IO x
done = Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x
forall x. Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x
lower (Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
-> (Sem (Resource : r) x
    -> Sem (WithTactics Resource f (Sem rInitial) r) x)
-> Sem (Resource : r) x
-> IO x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem r x -> Sem (WithTactics Resource f (Sem rInitial) r) x
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r x -> Sem (WithTactics Resource f (Sem rInitial) r) x)
-> (Sem (Resource : r) x -> Sem r x)
-> Sem (Resource : r) x
-> Sem (WithTactics Resource f (Sem rInitial) r) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (Resource : r) x -> Sem r x
forall (r :: [Effect]) a.
Member (Embed IO) r =>
Sem (Resource : r) a -> Sem r a
resourceToIO
      IO (f a) -> (f a -> IO ()) -> (f a -> IO (f x)) -> IO (f x)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
X.bracket
          (Sem (Resource : r) (f a) -> IO (f a)
forall x. Sem (Resource : r) x -> IO x
done Sem (Resource : r) (f a)
ma)
          (\f a
x -> Sem (Resource : r) (f c) -> IO (f c)
forall x. Sem (Resource : r) x -> IO x
done (f a -> Sem (Resource : r) (f c)
mb f a
x) IO (f c) -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
finish)
          (Sem (Resource : r) (f x) -> IO (f x)
forall x. Sem (Resource : r) x -> IO x
done (Sem (Resource : r) (f x) -> IO (f x))
-> (f a -> Sem (Resource : r) (f x)) -> f a -> IO (f x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Sem (Resource : r) (f x)
mc)

  BracketOnError a b c -> do
    Inspector f
ins <- Sem (WithTactics Resource f (Sem rInitial) r) (Inspector f)
forall (e :: Effect) (f :: * -> *) (m :: * -> *) (r :: [Effect]).
Sem (WithTactics e f m r) (Inspector f)
getInspectorT
    Sem (Resource : r) (f a)
ma <- Sem rInitial a
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (Sem (Resource : r) (f a))
forall (m :: * -> *) a (e :: Effect) (f :: * -> *) (r :: [Effect]).
m a -> Sem (WithTactics e f m r) (Sem (e : r) (f a))
runT Sem rInitial a
a
    f a -> Sem (Resource : r) (f c)
mb <- (a -> Sem rInitial c)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f c))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial c
b
    f a -> Sem (Resource : r) (f x)
mc <- (a -> Sem rInitial x)
-> Sem
     (WithTactics Resource f (Sem rInitial) r)
     (f a -> Sem (Resource : r) (f x))
forall a (m :: * -> *) b (e :: Effect) (f :: * -> *)
       (r :: [Effect]).
(a -> m b) -> Sem (WithTactics e f m r) (f a -> Sem (e : r) (f b))
bindT a -> Sem rInitial x
c

    ((forall x.
  Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
 -> IO () -> IO (f x))
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall (r :: [Effect]) a.
Member (Embed IO) r =>
((forall x. Sem r x -> IO x) -> IO () -> IO a) -> Sem r a
withLowerToIO (((forall x.
   Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
  -> IO () -> IO (f x))
 -> Sem (WithTactics Resource f (Sem rInitial) r) (f x))
-> ((forall x.
     Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
    -> IO () -> IO (f x))
-> Sem (WithTactics Resource f (Sem rInitial) r) (f x)
forall a b. (a -> b) -> a -> b
$ \forall x. Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x
lower IO ()
finish -> do
      let done :: Sem (Resource ': r) x -> IO x
          done :: Sem (Resource : r) x -> IO x
done = Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x
forall x. Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x
lower (Sem (WithTactics Resource f (Sem rInitial) r) x -> IO x)
-> (Sem (Resource : r) x
    -> Sem (WithTactics Resource f (Sem rInitial) r) x)
-> Sem (Resource : r) x
-> IO x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem r x -> Sem (WithTactics Resource f (Sem rInitial) r) x
forall (e :: Effect) (r :: [Effect]) a. Sem r a -> Sem (e : r) a
raise (Sem r x -> Sem (WithTactics Resource f (Sem rInitial) r) x)
-> (Sem (Resource : r) x -> Sem r x)
-> Sem (Resource : r) x
-> Sem (WithTactics Resource f (Sem rInitial) r) x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sem (Resource : r) x -> Sem r x
forall (r :: [Effect]) a.
Member (Embed IO) r =>
Sem (Resource : r) a -> Sem r a
resourceToIO
      IO (f a) -> (f a -> IO ()) -> (f a -> IO (f x)) -> IO (f x)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
X.bracketOnError
          (Sem (Resource : r) (f a) -> IO (f a)
forall x. Sem (Resource : r) x -> IO x
done Sem (Resource : r) (f a)
ma)
          (\f a
x -> Sem (Resource : r) (f c) -> IO (f c)
forall x. Sem (Resource : r) x -> IO x
done (f a -> Sem (Resource : r) (f c)
mb f a
x) IO (f c) -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
finish)
          (\f a
x -> do
            f x
result <- Sem (Resource : r) (f x) -> IO (f x)
forall x. Sem (Resource : r) x -> IO x
done (Sem (Resource : r) (f x) -> IO (f x))
-> Sem (Resource : r) (f x) -> IO (f x)
forall a b. (a -> b) -> a -> b
$ f a -> Sem (Resource : r) (f x)
mc f a
x
            case Inspector f -> f x -> Maybe x
forall (f :: * -> *). Inspector f -> forall x. f x -> Maybe x
inspect Inspector f
ins f x
result of
              Just x
_ -> f x -> IO (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result
              Maybe x
Nothing -> do
                f c
_ <- Sem (Resource : r) (f c) -> IO (f c)
forall x. Sem (Resource : r) x -> IO x
done (Sem (Resource : r) (f c) -> IO (f c))
-> Sem (Resource : r) (f c) -> IO (f c)
forall a b. (a -> b) -> a -> b
$ f a -> Sem (Resource : r) (f c)
mb f a
x
                f x -> IO (f x)
forall (f :: * -> *) a. Applicative f => a -> f a
pure f x
result
          )
{-# INLINE resourceToIO #-}