forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.hs
More file actions
59 lines (46 loc) · 1.73 KB
/
Logger.hs
File metadata and controls
59 lines (46 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- A replacement for WriterT IO which uses mutable references.
--
module Control.Monad.Logger where
import Prelude.Compat
import Control.Monad (ap)
import Control.Monad.Base (MonadBase(..))
import Control.Monad.IO.Class
import Control.Monad.Trans.Control (MonadBaseControl(..))
import Control.Monad.Writer.Class
import Data.IORef
-- | A replacement for WriterT IO which uses mutable references.
newtype Logger w a = Logger { runLogger :: IORef w -> IO a }
-- | Run a Logger computation, starting with an empty log.
runLogger' :: (Monoid w) => Logger w a -> IO (a, w)
runLogger' l = do
r <- newIORef mempty
a <- runLogger l r
w <- readIORef r
return (a, w)
instance Functor (Logger w) where
fmap f (Logger l) = Logger $ \r -> fmap f (l r)
instance (Monoid w) => Applicative (Logger w) where
pure = Logger . const . pure
(<*>) = ap
instance (Monoid w) => Monad (Logger w) where
return = pure
Logger l >>= f = Logger $ \r -> l r >>= \a -> runLogger (f a) r
instance (Monoid w) => MonadIO (Logger w) where
liftIO = Logger . const
instance (Monoid w) => MonadWriter w (Logger w) where
tell w = Logger $ \r -> atomicModifyIORef' r $ \w' -> (mappend w' w, ())
listen l = Logger $ \r -> do
(a, w) <- liftIO (runLogger' l)
atomicModifyIORef' r $ \w' -> (mappend w' w, (a, w))
pass l = Logger $ \r -> do
((a, f), w) <- liftIO (runLogger' l)
atomicModifyIORef' r $ \w' -> (mappend w' (f w), a)
instance (Monoid w) => MonadBase IO (Logger w) where
liftBase = liftIO
instance (Monoid w) => MonadBaseControl IO (Logger w) where
type StM (Logger w) a = a
liftBaseWith f = Logger $ \r -> liftBaseWith $ \q -> f (q . flip runLogger r)
restoreM = return