Skip to content

Commit

Permalink
INLINE ReaderWriterIO
Browse files Browse the repository at this point in the history
  • Loading branch information
ocharles committed Sep 14, 2016
1 parent a563842 commit b96e85c
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions reactive-banana/src/Control/Monad/Trans/ReaderWriterIO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Control.Monad.Trans.ReaderWriterIO (
-- * Synopsis
-- | An implementation of the reader/writer monad transformer
-- using an 'IORef' for the writer.

-- * Documentation
ReaderWriterIOT, readerWriterIOT, runReaderWriterIOT, tell, listen, ask, local,
) where
Expand All @@ -21,23 +21,44 @@ import Data.Monoid
------------------------------------------------------------------------------}
newtype ReaderWriterIOT r w m a = ReaderWriterIOT { run :: r -> IORef w -> m a }

instance Functor m => Functor (ReaderWriterIOT r w m) where fmap = fmapR

instance Applicative m => Applicative (ReaderWriterIOT r w m) where
pure = pureR
(<*>) = apR

instance Monad m => Monad (ReaderWriterIOT r w m) where
return = returnR
(>>=) = bindR

instance MonadFix m => MonadFix (ReaderWriterIOT r w m) where mfix = mfixR
instance MonadIO m => MonadIO (ReaderWriterIOT r w m) where liftIO = liftIOR
instance MonadTrans (ReaderWriterIOT r w) where lift = liftR
instance Functor m =>
Functor (ReaderWriterIOT r w m) where
fmap = fmapR
{-# INLINE fmap #-}

instance Applicative m =>
Applicative (ReaderWriterIOT r w m) where
pure = pureR
{-# INLINE pure #-}
(<*>) = apR
{-# INLINE (<*>) #-}

instance Monad m =>
Monad (ReaderWriterIOT r w m) where
return = returnR
{-# INLINE return #-}
(>>=) = bindR
{-# INLINE (>>=) #-}

instance MonadFix m =>
MonadFix (ReaderWriterIOT r w m) where
mfix = mfixR
{-# INLINE mfix #-}

instance MonadIO m =>
MonadIO (ReaderWriterIOT r w m) where
liftIO = liftIOR
{-# INLINE liftIO #-}

instance MonadTrans (ReaderWriterIOT r w) where
lift = liftR
{-# INLINE lift #-}

instance (Monad m, a ~ ()) => Monoid (ReaderWriterIOT r w m a) where
mempty = return ()
{-# INLINE mempty #-}
mx `mappend` my = mx >> my
{-# INLINE mappend #-}

{-----------------------------------------------------------------------------
Functions
Expand Down Expand Up @@ -65,28 +86,34 @@ readerWriterIOT f = do
(a,w) <- liftIOR $ f r
tell w
return a
{-# INLINE readerWriterIOT #-}

runReaderWriterIOT :: (MonadIO m, Monoid w) => ReaderWriterIOT r w m a -> r -> m (a,w)
runReaderWriterIOT m r = do
ref <- liftIO $ newIORef mempty
a <- run m r ref
w <- liftIO $ readIORef ref
return (a,w)
{-# INLINE runReaderWriterIOT #-}

tell :: (MonadIO m, Monoid w) => w -> ReaderWriterIOT r w m ()
tell w = ReaderWriterIOT $ \_ ref -> liftIO $ modifyIORef ref (`mappend` w)
{-# INLINE tell #-}

listen :: (MonadIO m, Monoid w) => ReaderWriterIOT r w m a -> ReaderWriterIOT r w m (a, w)
listen m = ReaderWriterIOT $ \r ref -> do
a <- run m r ref
w <- liftIO $ readIORef ref
return (a,w)
{-# INLINE listen #-}

local :: MonadIO m => (r -> r) -> ReaderWriterIOT r w m a -> ReaderWriterIOT r w m a
local f m = ReaderWriterIOT $ \r ref -> run m (f r) ref
{-# INLINE local #-}

ask :: Monad m => ReaderWriterIOT r w m r
ask = ReaderWriterIOT $ \r _ -> return r
{-# INLINE ask #-}

test :: ReaderWriterIOT String String IO ()
test = do
Expand Down

0 comments on commit b96e85c

Please sign in to comment.