{-# LANGUAGE AllowAmbiguousTypes #-}
module Data.Effect.Resource where
import Data.Functor (void)
data Resource f a where
Bracket :: f a -> (a -> f ()) -> (a -> f b) -> Resource f b
BracketOnExcept :: f a -> (a -> f ()) -> (a -> f b) -> Resource f b
makeEffectH [''Resource]
bracket_ :: (Resource <<: f, Functor f) => f a -> f b -> f c -> f c
bracket_ :: forall (f :: * -> *) a b c.
(Resource <<: f, Functor f) =>
f a -> f b -> f c -> f c
bracket_ f a
acquire f b
release f c
thing =
forall a b (f :: * -> *).
SendSig Resource f =>
f a -> (a -> f ()) -> (a -> f b) -> f b
bracket f a
acquire (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Functor f => f a -> f ()
void f b
release) (forall a b. a -> b -> a
const f c
thing)
bracketOnExcept_ :: (Resource <<: f, Functor f) => f a -> f b -> f c -> f c
bracketOnExcept_ :: forall (f :: * -> *) a b c.
(Resource <<: f, Functor f) =>
f a -> f b -> f c -> f c
bracketOnExcept_ f a
acquire f b
onExc f c
thing =
forall a b (f :: * -> *).
SendSig Resource f =>
f a -> (a -> f ()) -> (a -> f b) -> f b
bracketOnExcept f a
acquire (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Functor f => f a -> f ()
void f b
onExc) (forall a b. a -> b -> a
const f c
thing)
finally :: (Resource <<: f, Applicative f) => f a -> f () -> f a
finally :: forall (f :: * -> *) a.
(Resource <<: f, Applicative f) =>
f a -> f () -> f a
finally f a
thing f ()
release = forall a b (f :: * -> *).
SendSig Resource f =>
f a -> (a -> f ()) -> (a -> f b) -> f b
bracket (forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (forall a b. a -> b -> a
const f ()
release) (forall a b. a -> b -> a
const f a
thing)
finally_ :: (Resource <<: f, Applicative f) => f a -> f b -> f a
finally_ :: forall (f :: * -> *) a b.
(Resource <<: f, Applicative f) =>
f a -> f b -> f a
finally_ f a
thing f b
release = forall (f :: * -> *) a.
(Resource <<: f, Applicative f) =>
f a -> f () -> f a
finally f a
thing (forall (f :: * -> *) a. Functor f => f a -> f ()
void f b
release)
onException :: (Resource <<: f, Applicative f) => f a -> f () -> f a
onException :: forall (f :: * -> *) a.
(Resource <<: f, Applicative f) =>
f a -> f () -> f a
onException f a
thing f ()
onExc = forall a b (f :: * -> *).
SendSig Resource f =>
f a -> (a -> f ()) -> (a -> f b) -> f b
bracketOnExcept (forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (forall a b. a -> b -> a
const f ()
onExc) (forall a b. a -> b -> a
const f a
thing)
onException_ :: (Resource <<: f, Applicative f) => f a -> f b -> f a
onException_ :: forall (f :: * -> *) a b.
(Resource <<: f, Applicative f) =>
f a -> f b -> f a
onException_ f a
thing f b
onExc = forall (f :: * -> *) a.
(Resource <<: f, Applicative f) =>
f a -> f () -> f a
onException f a
thing (forall (f :: * -> *) a. Functor f => f a -> f ()
void f b
onExc)