Description
CPS resource allocation but as a Monad and completely safe.
README.md
scoped-codensity
This implements a Monad just like Codensity from the kan-extensions package, but it uses a skolem trap just like the ST s monad to track resources allocated in the monad.
The package wraps around different "scoped" resources that cannot escape a scoped block and are safely deallocated when the block is left.
How to make your own resources safe
- for acquiring your resources:
- use the
bracketScopedfunction where possible - if you're using the raw
UnsafeMkScopedconstructor, make sure, the scope of yourScopedResourceadheres to the scope you're in, by making your function return something of the shapeScoped (s : ss) m (ScopedResource s)
- use the
- of working with your resources:
- if you want to use the resource only in the same scope that it was created in, your function should be of the form:
ScopedResource s -> ... -> Scoped (s : ss) m a - if you want to use the resource in the same scope and scopes that are contained within the scope (this is the more common option), relate the scope of the resource to the scopes of the
Scopedmonad, by specifying `s :< ss` like so: `(s :< ss) => ScopedResource s -> .. -> Scoped ss m a`
- if you want to use the resource only in the same scope that it was created in, your function should be of the form:
- make sure that your actions are always in the
Scopedmonad and your resources always relate to the scopes that the monad is parametrized over, otherwise you might escape resources.