Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Created April 29, 2022 21:37
Show Gist options
  • Save carymrobbins/ea549b4df313a7b736e378879a1fe319 to your computer and use it in GitHub Desktop.
Save carymrobbins/ea549b4df313a7b736e378879a1fe319 to your computer and use it in GitHub Desktop.

Revisions

  1. carymrobbins created this gist Apr 29, 2022.
    19 changes: 19 additions & 0 deletions ContextLifted.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    module ContextLifted where

    import Control.Monad.Catch (MonadMask)
    import Control.Monad.IO.Class

    import qualified Context
    import qualified Context.Internal
    import qualified Control.Monad.Catch as Monad.Catch

    adjust :: (MonadIO m, MonadMask m) => Context.Store ctx -> (ctx -> ctx) -> m a -> m a
    adjust store f action = do
    adjustedContext <- liftIO $ Context.mines store f
    use store adjustedContext action

    use :: (MonadIO m, MonadMask m) => Context.Store ctx -> ctx -> m a -> m a
    use store context =
    Monad.Catch.bracket_
    (liftIO $ Context.Internal.push store context)
    (liftIO $ Context.Internal.pop store)