{-# LANGUAGE TemplateHaskell #-}
module Polysemy.Resource
(
Resource (..)
, bracket
, bracket_
, bracketOnError
, finally
, onException
, runResource
, resourceToIOFinal
, resourceToIO
, lowerResource
) where
import qualified Control.Exception as X
import Polysemy
import Polysemy.Final
data Resource m a where
Bracket
:: m a
-> (a -> m c)
-> (a -> m b)
-> Resource m b
BracketOnError
:: m a
-> (a -> m c)
-> (a -> m b)
-> Resource m b
makeSem ''Resource
bracket_
:: Member Resource r
=> Sem r a
-> Sem r b
-> Sem r c
-> 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)
finally
:: Member Resource r
=> Sem r a
-> Sem r b
-> 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)
onException
:: Member Resource r
=> Sem r a
-> Sem r b
-> 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)
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 #-}
lowerResource
:: ∀ r a
. Member (Embed IO) r
=> (∀ x. Sem r x -> IO x)
-> 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 (rInitial :: [Effect]) x.
Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : 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 ((forall (rInitial :: [Effect]) x.
Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a -> Sem r a)
-> (forall (rInitial :: [Effect]) x.
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" #-}
runResource
:: ∀ r a
. Sem (Resource ': r) a
-> Sem r a
runResource :: Sem (Resource : r) a -> Sem r a
runResource = (forall (rInitial :: [Effect]) x.
Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : 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 ((forall (rInitial :: [Effect]) x.
Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a -> Sem r a)
-> (forall (rInitial :: [Effect]) x.
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 #-}
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 (rInitial :: [Effect]) x.
Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : 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 ((forall (rInitial :: [Effect]) x.
Resource (Sem rInitial) x -> Tactical Resource (Sem rInitial) r x)
-> Sem (Resource : r) a -> Sem r a)
-> (forall (rInitial :: [Effect]) x.
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 #-}