Skip to content

Commit fbfe11f

Browse files
committed
Add new MonadInspectMVar class with an instance for IO.
1 parent dccf0a7 commit fbfe11f

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

io-classes/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Revsion history of io-classes
22

3+
## next version
4+
5+
### Non-breaking changes
6+
7+
* Add new `MonadInspectMVar` class with an `inspectMVar` function for accessing
8+
an `MVar` in an underlying monad (if applicable). This is mainly useful for
9+
`io-sim`, since the underlying monad is `ST`. `IO` has no underlying monad, so
10+
the provided instance for `IO` defaults `inspectMVar` to `tryReadMVar`.
11+
312
## 1.1.0.0
413

514
### Breaking changes

io-classes/src/Control/Concurrent/Class/MonadMVar.hs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
{-# LANGUAGE DefaultSignatures #-}
2+
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE QuantifiedConstraints #-}
34
{-# LANGUAGE TypeFamilyDependencies #-}
4-
{-# LANGUAGE TypeOperators #-}
55

6-
module Control.Concurrent.Class.MonadMVar (MonadMVar (..)) where
6+
module Control.Concurrent.Class.MonadMVar
7+
( MonadMVar (..)
8+
, MonadInspectMVar (..)
9+
) where
710

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

133+
--
134+
-- IO instance
135+
--
130136

131137
instance MonadMVar IO where
132138
type MVar IO = IO.MVar
@@ -181,8 +187,22 @@ instance ( MonadMask m
181187
modifyMVarMasked (WrappedMVar v) f = ReaderT $ \r ->
182188
modifyMVarMasked v (\a -> runReaderT (f a) r)
183189

190+
--
191+
-- MonadInspectMVar
192+
--
184193

185-
194+
-- | This type class is intended for
195+
-- ['io-sim'](https://hackage.haskell.org/package/io-sim), where one might want
196+
-- to access an 'MVar' in the underlying 'ST' monad.
197+
class (MonadMVar m, Monad (InspectMVarMonad m)) => MonadInspectMVar m where
198+
type InspectMVarMonad m :: Type -> Type
199+
-- | Return the value of an 'MVar' as an 'InspectMVarMonad' computation. Can
200+
-- be 'Nothing' if the 'MVar' is empty.
201+
inspectMVar :: proxy m -> MVar m a -> InspectMVarMonad m (Maybe a)
202+
203+
instance MonadInspectMVar IO where
204+
type InspectMVarMonad IO = IO
205+
inspectMVar _ = tryReadMVar
186206

187207
--
188208
-- Utilities

0 commit comments

Comments
 (0)