Safe Haskell | Safe-Inferred |
---|---|

Language | Haskell2010 |

Guarding monadic computations with cleanup actions.

## Synopsis

- class (Applicative μ, Monad μ) => MonadFinally μ where
- chainCleanups :: MonadFinally μ => [μ α] -> μ ()
- finallyMany :: MonadFinally μ => μ α -> [μ β] -> μ α
- onEscape :: MonadFinally μ => μ α -> μ β -> μ α
- onEscapeMany :: MonadFinally μ => μ α -> [μ β] -> μ α
- bracket_ :: MonadFinally μ => μ r -> μ β -> μ α -> μ α
- bracketOnEscape :: MonadFinally μ => μ r -> (r -> μ β) -> (r -> μ α) -> μ α

# Documentation

class (Applicative μ, Monad μ) => MonadFinally μ where Source #

Class of monads that support guarding computations with cleanup actions.

finally' :: μ α -> (Maybe α -> μ β) -> μ (α, β) Source #

runs computation `finally'`

m f`m`

and then

finally :: μ α -> μ β -> μ α Source #

A simplified version of `finally'`

in which the cleanup action
does not care about the result of the main computation. The default
implementation is

`finally`

m =`fmap`

`fst`

.`finally'`

m .`const`

:: μ r | Acquire resource |

-> (r -> Maybe α -> μ β) | Release resource |

-> (r -> μ α) | Main computation |

-> μ (α, β) |

Safely acquire a resource and use it in a computation, releasing it even when the computation does not produce a result.

#### Instances

MonadFinally Identity Source # | |

Defined in Control.Monad.Finally finally' :: Identity α -> (Maybe α -> Identity β) -> Identity (α, β) Source # finally :: Identity α -> Identity β -> Identity α Source # bracket' :: Identity r -> (r -> Maybe α -> Identity β) -> (r -> Identity α) -> Identity (α, β) Source # bracket :: Identity r -> (r -> Identity β) -> (r -> Identity α) -> Identity α Source # | |

MonadFinally IO Source # | |

MonadFinally μ => MonadFinally (MaybeT μ) Source # | |

Defined in Control.Monad.Finally finally' :: MaybeT μ α -> (Maybe α -> MaybeT μ β) -> MaybeT μ (α, β) Source # finally :: MaybeT μ α -> MaybeT μ β -> MaybeT μ α Source # bracket' :: MaybeT μ r -> (r -> Maybe α -> MaybeT μ β) -> (r -> MaybeT μ α) -> MaybeT μ (α, β) Source # bracket :: MaybeT μ r -> (r -> MaybeT μ β) -> (r -> MaybeT μ α) -> MaybeT μ α Source # | |

(MonadFinally μ, Monoid w) => MonadFinally (AccumT w μ) Source # | |

Defined in Control.Monad.Finally finally' :: AccumT w μ α -> (Maybe α -> AccumT w μ β) -> AccumT w μ (α, β) Source # finally :: AccumT w μ α -> AccumT w μ β -> AccumT w μ α Source # bracket' :: AccumT w μ r -> (r -> Maybe α -> AccumT w μ β) -> (r -> AccumT w μ α) -> AccumT w μ (α, β) Source # bracket :: AccumT w μ r -> (r -> AccumT w μ β) -> (r -> AccumT w μ α) -> AccumT w μ α Source # | |

(MonadFinally μ, Error e) => MonadFinally (ErrorT e μ) Source # | |

Defined in Control.Monad.Finally finally' :: ErrorT e μ α -> (Maybe α -> ErrorT e μ β) -> ErrorT e μ (α, β) Source # finally :: ErrorT e μ α -> ErrorT e μ β -> ErrorT e μ α Source # bracket' :: ErrorT e μ r -> (r -> Maybe α -> ErrorT e μ β) -> (r -> ErrorT e μ α) -> ErrorT e μ (α, β) Source # bracket :: ErrorT e μ r -> (r -> ErrorT e μ β) -> (r -> ErrorT e μ α) -> ErrorT e μ α Source # | |

MonadFinally μ => MonadFinally (ExceptT e μ) Source # | |

Defined in Control.Monad.Finally finally' :: ExceptT e μ α -> (Maybe α -> ExceptT e μ β) -> ExceptT e μ (α, β) Source # finally :: ExceptT e μ α -> ExceptT e μ β -> ExceptT e μ α Source # bracket' :: ExceptT e μ r -> (r -> Maybe α -> ExceptT e μ β) -> (r -> ExceptT e μ α) -> ExceptT e μ (α, β) Source # bracket :: ExceptT e μ r -> (r -> ExceptT e μ β) -> (r -> ExceptT e μ α) -> ExceptT e μ α Source # | |

MonadFinally μ => MonadFinally (ReaderT r μ) Source # | |

Defined in Control.Monad.Finally finally' :: ReaderT r μ α -> (Maybe α -> ReaderT r μ β) -> ReaderT r μ (α, β) Source # finally :: ReaderT r μ α -> ReaderT r μ β -> ReaderT r μ α Source # bracket' :: ReaderT r μ r0 -> (r0 -> Maybe α -> ReaderT r μ β) -> (r0 -> ReaderT r μ α) -> ReaderT r μ (α, β) Source # bracket :: ReaderT r μ r0 -> (r0 -> ReaderT r μ β) -> (r0 -> ReaderT r μ α) -> ReaderT r μ α Source # | |

MonadFinally μ => MonadFinally (StateT s μ) Source # | |

Defined in Control.Monad.Finally finally' :: StateT s μ α -> (Maybe α -> StateT s μ β) -> StateT s μ (α, β) Source # finally :: StateT s μ α -> StateT s μ β -> StateT s μ α Source # bracket' :: StateT s μ r -> (r -> Maybe α -> StateT s μ β) -> (r -> StateT s μ α) -> StateT s μ (α, β) Source # bracket :: StateT s μ r -> (r -> StateT s μ β) -> (r -> StateT s μ α) -> StateT s μ α Source # | |

MonadFinally μ => MonadFinally (StateT s μ) Source # | |

Defined in Control.Monad.Finally finally' :: StateT s μ α -> (Maybe α -> StateT s μ β) -> StateT s μ (α, β) Source # finally :: StateT s μ α -> StateT s μ β -> StateT s μ α Source # bracket' :: StateT s μ r -> (r -> Maybe α -> StateT s μ β) -> (r -> StateT s μ α) -> StateT s μ (α, β) Source # bracket :: StateT s μ r -> (r -> StateT s μ β) -> (r -> StateT s μ α) -> StateT s μ α Source # | |

(MonadFinally μ, Monoid w) => MonadFinally (WriterT w μ) Source # | |

Defined in Control.Monad.Finally finally' :: WriterT w μ α -> (Maybe α -> WriterT w μ β) -> WriterT w μ (α, β) Source # finally :: WriterT w μ α -> WriterT w μ β -> WriterT w μ α Source # bracket' :: WriterT w μ r -> (r -> Maybe α -> WriterT w μ β) -> (r -> WriterT w μ α) -> WriterT w μ (α, β) Source # bracket :: WriterT w μ r -> (r -> WriterT w μ β) -> (r -> WriterT w μ α) -> WriterT w μ α Source # | |

(MonadFinally μ, Monoid w) => MonadFinally (WriterT w μ) Source # | |

Defined in Control.Monad.Finally finally' :: WriterT w μ α -> (Maybe α -> WriterT w μ β) -> WriterT w μ (α, β) Source # finally :: WriterT w μ α -> WriterT w μ β -> WriterT w μ α Source # bracket' :: WriterT w μ r -> (r -> Maybe α -> WriterT w μ β) -> (r -> WriterT w μ α) -> WriterT w μ (α, β) Source # bracket :: WriterT w μ r -> (r -> WriterT w μ β) -> (r -> WriterT w μ α) -> WriterT w μ α Source # | |

MonadFinally μ => MonadFinally (AbortT e μ) Source # | |

Defined in Control.Monad.Finally finally' :: AbortT e μ α -> (Maybe α -> AbortT e μ β) -> AbortT e μ (α, β) Source # finally :: AbortT e μ α -> AbortT e μ β -> AbortT e μ α Source # bracket' :: AbortT e μ r -> (r -> Maybe α -> AbortT e μ β) -> (r -> AbortT e μ α) -> AbortT e μ (α, β) Source # bracket :: AbortT e μ r -> (r -> AbortT e μ β) -> (r -> AbortT e μ α) -> AbortT e μ α Source # | |

MonadFinally μ => MonadFinally (FinishT β μ) Source # | |

Defined in Control.Monad.Finally finally' :: FinishT β μ α -> (Maybe α -> FinishT β μ β0) -> FinishT β μ (α, β0) Source # finally :: FinishT β μ α -> FinishT β μ β0 -> FinishT β μ α Source # bracket' :: FinishT β μ r -> (r -> Maybe α -> FinishT β μ β0) -> (r -> FinishT β μ α) -> FinishT β μ (α, β0) Source # bracket :: FinishT β μ r -> (r -> FinishT β μ β0) -> (r -> FinishT β μ α) -> FinishT β μ α Source # | |

(MonadFinally μ, Monoid w) => MonadFinally (RWST r w s μ) Source # | |

Defined in Control.Monad.Finally finally' :: RWST r w s μ α -> (Maybe α -> RWST r w s μ β) -> RWST r w s μ (α, β) Source # finally :: RWST r w s μ α -> RWST r w s μ β -> RWST r w s μ α Source # bracket' :: RWST r w s μ r0 -> (r0 -> Maybe α -> RWST r w s μ β) -> (r0 -> RWST r w s μ α) -> RWST r w s μ (α, β) Source # bracket :: RWST r w s μ r0 -> (r0 -> RWST r w s μ β) -> (r0 -> RWST r w s μ α) -> RWST r w s μ α Source # | |

(MonadFinally μ, Monoid w) => MonadFinally (RWST r w s μ) Source # | |

Defined in Control.Monad.Finally finally' :: RWST r w s μ α -> (Maybe α -> RWST r w s μ β) -> RWST r w s μ (α, β) Source # finally :: RWST r w s μ α -> RWST r w s μ β -> RWST r w s μ α Source # bracket' :: RWST r w s μ r0 -> (r0 -> Maybe α -> RWST r w s μ β) -> (r0 -> RWST r w s μ α) -> RWST r w s μ (α, β) Source # bracket :: RWST r w s μ r0 -> (r0 -> RWST r w s μ β) -> (r0 -> RWST r w s μ α) -> RWST r w s μ α Source # |

chainCleanups :: MonadFinally μ => [μ α] -> μ () Source #

Run the provided list of cleanup actions sequentually, attempting to run the next action even if the previous one did not produce a result.

finallyMany :: MonadFinally μ => μ α -> [μ β] -> μ α Source #

A variant of `finally`

that combines multiple cleanup actions with
`chainCleanups`

.

onEscape :: MonadFinally μ => μ α -> μ β -> μ α Source #

runs computation `onEscape`

m c`m`

and then, if it did not produce
a result, runs computation `c`

.

onEscapeMany :: MonadFinally μ => μ α -> [μ β] -> μ α Source #

A variant of `onEscape`

that combines multiple cleanup actions with
`chainCleanups`

.

:: MonadFinally μ | |

=> μ r | Acquire resource |

-> μ β | Release resource |

-> μ α | Main computation |

-> μ α |

A variant of `bracket`

where acquired value is not needed (e.g. using
a static resource).

:: MonadFinally μ | |

=> μ r | Acquire resource |

-> (r -> μ β) | Release resource |

-> (r -> μ α) | Main computation |

-> μ α |

A variant of `bracket`

that releases the acquired resource only when
the main computation does not produce a value.