Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonadMask constraint absorber. #61

Open
KingoftheHomeless opened this issue Jan 2, 2020 · 1 comment
Open

MonadMask constraint absorber. #61

KingoftheHomeless opened this issue Jan 2, 2020 · 1 comment

Comments

@KingoftheHomeless
Copy link
Collaborator

KingoftheHomeless commented Jan 2, 2020

A MonadMask constraint absorber with the following type signature:

absorbMaskAsIO
    :: Member (Final IO) r
    => (MonadMask (Sem r) => Sem r a)
    -> Sem r a

Should be possible by lifting uninterruptible/Mask à la polysemy-research/polysemy#135 (comment) and lifting generalBracket through the following:

generalBracket
  :: Member (Final IO) r
  => Sem r a
  -> (a -> ExitCase b -> Sem r c)
  -> (a -> Sem r b)
  -> Sem r (b, c)
generalBracket alloc dealloc use = withStrategicToFinal $ do
  alloc'   <- runS alloc
  dSuccess <- bindS (\(a, b) -> (b ,) <$> dealloc a (ExitCaseSuccess b))
  dFailure <- bindS (uncurry dealloc)
  use'     <- bindS (\a -> (,) a <$> use a)
  ins      <- getInspectorS
  pure $ X.mask $ \restore -> do
    res <- alloc'
    fb   <- restore (use' res) `X.catch` \e -> do
      _ <- dFailure $ (, ExitCaseException e) <$> res
      X.throwIO e
    if isJust (inspect ins fb) then
      dSuccess fb
    else do
      _ <- dFailure $ (, ExitCaseAbort) <$> res
      return ((\(_,_) -> bomb "generalBracket") <$> fb)

bomb :: String -> a
bomb str = error $
    str ++ ": Uninspectable effectful state still carries a visible result.\
            \ You're probably using an interpreter\
            \ that uses 'weave' improperly.\
            \ See documentation for more information."
@KingoftheHomeless
Copy link
Collaborator Author

If we figure out polysemy-research/polysemy#304, and implement the Mask effect described there, we can also do:

absorbMask
    :: Member (Mask s) r
    => (MonadMask (Sem r) => Sem r a)
    -> Sem r a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant