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

Added transformer instances for MonadInspectSTM and MonadTraceSTM #192

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions io-classes/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* Added `threadLabel` to `MonadThread`
* Added `MonadLabelledMVar` class.

### Non-breaking changes

* Added monad transformer instances for `MonadInspectSTM` & `MonadTraceSTM`
type classes.

### 1.7.0.0

### Breaking changes
Expand Down
107 changes: 107 additions & 0 deletions io-classes/mtl/Control/Monad/Class/MonadSTM/Trans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

-- undecidable instances needed for 'ContTSTM' instances of
Expand All @@ -31,6 +33,7 @@ import Control.Monad.Class.MonadThrow qualified as MonadThrow
import Data.Array.Base (MArray (..))
import Data.Function (on)
import Data.Kind (Type)
import Data.Proxy (Proxy (..))


-- | A newtype wrapper for an 'STM' monad for 'ContT'
Expand Down Expand Up @@ -161,6 +164,19 @@ instance MonadSTM m => MonadSTM (ContT r m) where
isEmptyTChan = ContTSTM . isEmptyTChan


instance MonadInspectSTM m => MonadInspectSTM (ContT r m) where
type InspectMonad (ContT r m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance MonadTraceSTM m => MonadTraceSTM (ContT r m) where
traceTVar _ = ContTSTM .: traceTVar (Proxy @m)
traceTMVar _ = ContTSTM .: traceTMVar (Proxy @m)
traceTQueue _ = ContTSTM .: traceTQueue (Proxy @m)
traceTBQueue _ = ContTSTM .: traceTBQueue (Proxy @m)
traceTSem _ = ContTSTM .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance (Monoid w, MonadSTM m) => MonadSTM (Lazy.WriterT w m) where
Expand Down Expand Up @@ -239,6 +255,19 @@ instance (Monoid w, MonadSTM m) => MonadSTM (Lazy.WriterT w m) where
isEmptyTChan = lift . isEmptyTChan


instance (Monoid w, MonadInspectSTM m) => MonadInspectSTM (Lazy.WriterT w m) where
type InspectMonad (Lazy.WriterT w m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance (Monoid w, MonadTraceSTM m) => MonadTraceSTM (Lazy.WriterT w m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance (Monoid w, MonadSTM m) => MonadSTM (Strict.WriterT w m) where
Expand Down Expand Up @@ -317,6 +346,19 @@ instance (Monoid w, MonadSTM m) => MonadSTM (Strict.WriterT w m) where
isEmptyTChan = lift . isEmptyTChan


instance (Monoid w, MonadInspectSTM m) => MonadInspectSTM (Strict.WriterT w m) where
type InspectMonad (Strict.WriterT w m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance (Monoid w, MonadTraceSTM m) => MonadTraceSTM (Strict.WriterT w m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance MonadSTM m => MonadSTM (Lazy.StateT s m) where
Expand Down Expand Up @@ -395,6 +437,19 @@ instance MonadSTM m => MonadSTM (Lazy.StateT s m) where
isEmptyTChan = lift . isEmptyTChan


instance MonadInspectSTM m => MonadInspectSTM (Lazy.StateT s m) where
type InspectMonad (Lazy.StateT s m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance MonadTraceSTM m => MonadTraceSTM (Lazy.StateT s m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance MonadSTM m => MonadSTM (Strict.StateT s m) where
Expand Down Expand Up @@ -473,6 +528,19 @@ instance MonadSTM m => MonadSTM (Strict.StateT s m) where
isEmptyTChan = lift . isEmptyTChan


instance MonadInspectSTM m => MonadInspectSTM (Strict.StateT s m) where
type InspectMonad (Strict.StateT s m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance MonadTraceSTM m => MonadTraceSTM (Strict.StateT s m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance MonadSTM m => MonadSTM (ExceptT e m) where
Expand Down Expand Up @@ -551,6 +619,19 @@ instance MonadSTM m => MonadSTM (ExceptT e m) where
isEmptyTChan = lift . isEmptyTChan


instance MonadInspectSTM m => MonadInspectSTM (ExceptT e m) where
type InspectMonad (ExceptT e m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance MonadTraceSTM m => MonadTraceSTM (ExceptT e m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance (Monoid w, MonadSTM m) => MonadSTM (Lazy.RWST r w s m) where
Expand Down Expand Up @@ -629,6 +710,19 @@ instance (Monoid w, MonadSTM m) => MonadSTM (Lazy.RWST r w s m) where
isEmptyTChan = lift . isEmptyTChan


instance (Monoid w, MonadInspectSTM m) => MonadInspectSTM (Lazy.RWST r w s m) where
type InspectMonad (Lazy.RWST r w s m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance (Monoid w, MonadTraceSTM m) => MonadTraceSTM (Lazy.RWST r w s m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


-- | The underlying stm monad is also transformed.
--
instance (Monoid w, MonadSTM m) => MonadSTM (Strict.RWST r w s m) where
Expand Down Expand Up @@ -707,5 +801,18 @@ instance (Monoid w, MonadSTM m) => MonadSTM (Strict.RWST r w s m) where
isEmptyTChan = lift . isEmptyTChan


instance (Monoid w, MonadInspectSTM m) => MonadInspectSTM (Strict.RWST r w s m) where
type InspectMonad (Strict.RWST r w s m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy @m)
inspectTMVar _ = inspectTMVar (Proxy @m)

instance (Monoid w, MonadTraceSTM m) => MonadTraceSTM (Strict.RWST r w s m) where
traceTVar _ = lift .: traceTVar (Proxy @m)
traceTMVar _ = lift .: traceTMVar (Proxy @m)
traceTQueue _ = lift .: traceTQueue (Proxy @m)
traceTBQueue _ = lift .: traceTBQueue (Proxy @m)
traceTSem _ = lift .: traceTSem (Proxy @m)


(.:) :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)
(f .: g) x y = f (g x y)
11 changes: 11 additions & 0 deletions io-classes/src/Control/Monad/Class/MonadSTM/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,17 @@ instance MonadSTM m => MonadSTM (ReaderT r m) where
unGetTChan = lift .: unGetTChan
isEmptyTChan = lift . isEmptyTChan

instance MonadInspectSTM m => MonadInspectSTM (ReaderT r m) where
type InspectMonad (ReaderT r m) = InspectMonad m
inspectTVar _ = inspectTVar (Proxy :: Proxy m)
inspectTMVar _ = inspectTMVar (Proxy :: Proxy m)

instance MonadTraceSTM m => MonadTraceSTM (ReaderT r m) where
traceTVar _ = lift .: traceTVar Proxy
traceTMVar _ = lift .: traceTMVar Proxy
traceTQueue _ = lift .: traceTQueue Proxy
traceTBQueue _ = lift .: traceTBQueue Proxy
traceTSem _ = lift .: traceTSem Proxy

(.:) :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)
(f .: g) x y = f (g x y)
Expand Down
Loading