Safe Haskell | None |
---|---|

Language | Haskell2010 |

## Synopsis

- 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

- bracket :: forall r a c b. MemberWithError Resource r => Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
- bracketOnError :: forall r a c b. MemberWithError Resource r => Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b
- finally :: Member Resource r => Sem r a -> Sem r b -> Sem r a
- onException :: Member Resource r => Sem r a -> Sem r b -> Sem r a
- runResource :: forall r a. Sem (Resource ': r) a -> Sem r a
- resourceToIOFinal :: Member (Final IO) r => Sem (Resource ': r) a -> Sem r a
- resourceToIO :: forall r a. Member (Embed IO) r => Sem (Resource ': r) a -> Sem r a
- lowerResource :: forall r a. Member (Embed IO) r => (forall x. Sem r x -> IO x) -> Sem (Resource ': r) a -> Sem r a

# Effect

data Resource m a where Source #

An effect capable of providing `bracket`

semantics. Interpreters for this
will successfully run the deallocation action even in the presence of other
short-circuiting effects.

Bracket :: m a -> (a -> m c) -> (a -> m b) -> Resource m b | |

BracketOnError :: m a -> (a -> m c) -> (a -> m b) -> Resource m b |

## Instances

type DefiningModule Resource Source # | |

Defined in Polysemy.Resource |

# Actions

bracket :: forall r a c b. MemberWithError Resource r => Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b Source #

bracketOnError :: forall r a c b. MemberWithError Resource r => Sem r a -> (a -> Sem r c) -> (a -> Sem r b) -> Sem r b Source #

:: 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 |

Like `bracket`

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

*Since: 0.4.0.0*

:: 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 |

Like `bracketOnError`

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

*Since: 0.4.0.0*

# Interpretations

runResource :: forall r a. Sem (Resource ': r) a -> Sem r a Source #

Run a `Resource`

effect purely.

*Since: 1.0.0.0*

resourceToIOFinal :: Member (Final IO) r => Sem (Resource ': r) a -> Sem r a Source #

Run a `Resource`

effect in terms of `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
`State`

unless `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, `runState`

.)

*Since: 1.2.0.0*

resourceToIO :: forall r a. Member (Embed IO) r => Sem (Resource ': r) a -> Sem r a Source #

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 `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*