1{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE TypeFamilies #-}
5-- A replacement for WriterT IO which uses mutable references.
7module Control.Monad.Logger where
11import Control.Monad (ap)
12import Control.Monad.Base (MonadBase(..))
13import Control.Monad.IO.Class
14import Control.Monad.Trans.Control (MonadBaseControl(..))
15import Control.Monad.Writer.Class
19-- | A replacement for WriterT IO which uses mutable references.
20newtype Logger w a = Logger { runLogger :: IORef w -> IO a }
22-- | Run a Logger computation, starting with an empty log.
23runLogger' :: (Monoid w) => Logger w a -> IO (a, w)
30instance Functor (Logger w) where
31 fmap f (Logger l) = Logger $ \r -> fmap f (l r)
33instance (Monoid w) => Applicative (Logger w) where
34 pure = Logger . const . pure
37instance (Monoid w) => Monad (Logger w) where
39 Logger l >>= f = Logger $ \r -> l r >>= \a -> runLogger (f a) r
41instance (Monoid w) => MonadIO (Logger w) where
42 liftIO = Logger . const
44instance (Monoid w) => MonadWriter w (Logger w) where
45 tell w = Logger $ \r -> atomicModifyIORef' r $ \w' -> (mappend w' w, ())
46 listen l = Logger $ \r -> do
47 (a, w) <- liftIO (runLogger' l)
48 atomicModifyIORef' r $ \w' -> (mappend w' w, (a, w))
49 pass l = Logger $ \r -> do
50 ((a, f), w) <- liftIO (runLogger' l)
51 atomicModifyIORef' r $ \w' -> (mappend w' (f w), a)
53instance (Monoid w) => MonadBase IO (Logger w) where
56instance (Monoid w) => MonadBaseControl IO (Logger w) where
57 type StM (Logger w) a = a
58 liftBaseWith f = Logger $ \r -> liftBaseWith $ \q -> f (q . flip runLogger r)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list