Skip to content

Commit

Permalink
Add new MonadInspectMVar class with an instance for IO.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisdral committed May 31, 2023
1 parent dccf0a7 commit fbfe11f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 9 additions & 0 deletions io-classes/CHANGELOG.md
@@ -1,5 +1,14 @@
# Revsion history of io-classes

## next version

### Non-breaking changes

* Add new `MonadInspectMVar` class with an `inspectMVar` function for accessing
an `MVar` in an underlying monad (if applicable). This is mainly useful for
`io-sim`, since the underlying monad is `ST`. `IO` has no underlying monad, so
the provided instance for `IO` defaults `inspectMVar` to `tryReadMVar`.

## 1.1.0.0

### Breaking changes
Expand Down
26 changes: 23 additions & 3 deletions io-classes/src/Control/Concurrent/Class/MonadMVar.hs
@@ -1,9 +1,12 @@
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeOperators #-}

module Control.Concurrent.Class.MonadMVar (MonadMVar (..)) where
module Control.Concurrent.Class.MonadMVar
( MonadMVar (..)
, MonadInspectMVar (..)
) where

import qualified Control.Concurrent.MVar as IO
import Control.Monad.Class.MonadThrow
Expand Down Expand Up @@ -127,6 +130,9 @@ class Monad m => MonadMVar m where
return b
{-# INLINE modifyMVarMasked #-}

--
-- IO instance
--

instance MonadMVar IO where
type MVar IO = IO.MVar
Expand Down Expand Up @@ -181,8 +187,22 @@ instance ( MonadMask m
modifyMVarMasked (WrappedMVar v) f = ReaderT $ \r ->
modifyMVarMasked v (\a -> runReaderT (f a) r)

--
-- MonadInspectMVar
--


-- | This type class is intended for
-- ['io-sim'](https://hackage.haskell.org/package/io-sim), where one might want
-- to access an 'MVar' in the underlying 'ST' monad.
class (MonadMVar m, Monad (InspectMVarMonad m)) => MonadInspectMVar m where
type InspectMVarMonad m :: Type -> Type
-- | Return the value of an 'MVar' as an 'InspectMVarMonad' computation. Can
-- be 'Nothing' if the 'MVar' is empty.
inspectMVar :: proxy m -> MVar m a -> InspectMVarMonad m (Maybe a)

instance MonadInspectMVar IO where
type InspectMVarMonad IO = IO
inspectMVar _ = tryReadMVar

--
-- Utilities
Expand Down

0 comments on commit fbfe11f

Please sign in to comment.