fused-effects-0.5.0.1: A fast, flexible, fused effect system.
Safe HaskellNone
LanguageHaskell2010

Control.Effect.Resource

Synopsis

Resource effect

data Resource m k Source #

Constructors

forall resource any output. Resource (m resource) (resource -> m any) (resource -> m output) (output -> m k) 
forall resource any output. OnError (m resource) (resource -> m any) (resource -> m output) (output -> m k) 

Instances

Instances details
Effect Resource Source # 
Instance details

Defined in Control.Effect.Resource

Methods

handle :: (Functor f, Monad m) => f () -> (forall x. f (m x) -> n (f x)) -> Resource m a -> Resource n (f a) Source #

HFunctor Resource Source # 
Instance details

Defined in Control.Effect.Resource

Methods

fmap' :: forall (f :: Type -> Type) a b. Functor (Resource f) => (a -> b) -> Resource f a -> Resource f b Source #

hmap :: Functor m => (forall x. m x -> n x) -> Resource m a -> Resource n a Source #

Functor m => Functor (Resource m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

fmap :: (a -> b) -> Resource m a -> Resource m b #

(<$) :: a -> Resource m b -> Resource m a #

(Carrier sig m, MonadIO m) => Carrier (Resource :+: sig) (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

eff :: (Resource :+: sig) (ResourceC m) a -> ResourceC m a Source #

bracket Source #

Arguments

:: (Member Resource sig, Carrier sig m) 
=> m resource

computation to run first ("acquire resource")

-> (resource -> m any)

computation to run last ("release resource")

-> (resource -> m a)

computation to run in-between

-> m a 

Provides a safe idiom to acquire and release resources safely.

When acquiring and operating on a resource (such as opening and reading file handle with openFile or writing to a blob of memory with malloc), any exception thrown during the operation may mean that the resource is not properly released. bracket acquire release op ensures that release is run on the value returned from acquire even if op throws an exception.

bracket is safe in the presence of asynchronous exceptions.

bracketOnError Source #

Arguments

:: (Member Resource sig, Carrier sig m) 
=> m resource

computation to run first ("acquire resource")

-> (resource -> m any)

computation to run last ("release resource")

-> (resource -> m a)

computation to run in-between

-> m a 

Like bracket, but only performs the final action if there was an exception raised by the in-between computation.

finally Source #

Arguments

:: (Member Resource sig, Carrier sig m) 
=> m a

computation to run first

-> m b

computation to run afterward (even if an exception was raised)

-> m a 

Like bracket, but for the simple case of one computation to run afterward.

onException Source #

Arguments

:: (Member Resource sig, Carrier sig m) 
=> m a

computation to run first

-> m b

computation to run afterward if an exception was raised

-> m a 

Like bracketOnError, but for the simple case of one computation to run afterward.

Resource carrier

runResource :: MonadUnliftIO m => ResourceC m a -> m a Source #

Executes a Resource effect. Because this runs using MonadUnliftIO, invocations of runResource must happen at the "bottom" of a stack of effect invocations, i.e. before the use of any monads that lack such instances, such as StateC:

  runM
  . runResource
  . runState @Int 1
  $ myComputation

newtype ResourceC m a Source #

Constructors

ResourceC 

Fields

Instances

Instances details
MonadTrans ResourceC Source # 
Instance details

Defined in Control.Effect.Resource

Methods

lift :: Monad m => m a -> ResourceC m a #

Monad m => Monad (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

(>>=) :: ResourceC m a -> (a -> ResourceC m b) -> ResourceC m b #

(>>) :: ResourceC m a -> ResourceC m b -> ResourceC m b #

return :: a -> ResourceC m a #

Functor m => Functor (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

fmap :: (a -> b) -> ResourceC m a -> ResourceC m b #

(<$) :: a -> ResourceC m b -> ResourceC m a #

MonadFix m => MonadFix (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

mfix :: (a -> ResourceC m a) -> ResourceC m a #

MonadFail m => MonadFail (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

fail :: String -> ResourceC m a #

Applicative m => Applicative (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

pure :: a -> ResourceC m a #

(<*>) :: ResourceC m (a -> b) -> ResourceC m a -> ResourceC m b #

liftA2 :: (a -> b -> c) -> ResourceC m a -> ResourceC m b -> ResourceC m c #

(*>) :: ResourceC m a -> ResourceC m b -> ResourceC m b #

(<*) :: ResourceC m a -> ResourceC m b -> ResourceC m a #

MonadIO m => MonadIO (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

liftIO :: IO a -> ResourceC m a #

Alternative m => Alternative (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

empty :: ResourceC m a #

(<|>) :: ResourceC m a -> ResourceC m a -> ResourceC m a #

some :: ResourceC m a -> ResourceC m [a] #

many :: ResourceC m a -> ResourceC m [a] #

(Alternative m, Monad m) => MonadPlus (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

mzero :: ResourceC m a #

mplus :: ResourceC m a -> ResourceC m a -> ResourceC m a #

MonadUnliftIO m => MonadUnliftIO (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

askUnliftIO :: ResourceC m (UnliftIO (ResourceC m))

withRunInIO :: ((forall a. ResourceC m a -> IO a) -> IO b) -> ResourceC m b

(Carrier sig m, MonadIO m) => Carrier (Resource :+: sig) (ResourceC m) Source # 
Instance details

Defined in Control.Effect.Resource

Methods

eff :: (Resource :+: sig) (ResourceC m) a -> ResourceC m a Source #