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

Reimplemented Functor & Applicative for LoggingT & NoLoggingT #125 #126

Merged
merged 2 commits into from
Mar 16, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions monad-logger/Control/Monad/Logger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,15 @@ logOtherS = [|\src level msg -> monadLoggerLog $(qLocation >>= liftLoc) src (Lev
-- Since 0.2.4
newtype NoLoggingT m a = NoLoggingT { runNoLoggingT :: m a }

instance Monad m => Functor (NoLoggingT m) where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change in constraints is a non-breaking change since the AMP proposal went through (GHC 7.10?), but represents a breaking API change for earlier GHCs. I think it's better to only introduce the Functor/Applicative constraints conditionally for GHC 7.10 and newer.

fmap = liftM
instance Functor m => Functor (NoLoggingT m) where
fmap f = NoLoggingT . fmap f . runNoLoggingT
{-# INLINE fmap #-}

instance Monad m => Applicative (NoLoggingT m) where
pure = return
(<*>) = ap
instance Applicative m => Applicative (NoLoggingT m) where
pure = NoLoggingT . pure
{-# INLINE pure #-}
f <*> a = NoLoggingT (runNoLoggingT f <*> runNoLoggingT a)
{-# INLINE (<*>) #-}

instance Monad m => Monad (NoLoggingT m) where
return = NoLoggingT . return
Expand Down Expand Up @@ -452,12 +455,17 @@ instance MonadIO m => MonadLoggerIO (NoLoggingT m) where
-- Since 0.2.2
newtype LoggingT m a = LoggingT { runLoggingT :: (Loc -> LogSource -> LogLevel -> LogStr -> IO ()) -> m a }

instance Monad m => Functor (LoggingT m) where
fmap = liftM

instance Monad m => Applicative (LoggingT m) where
pure = return
(<*>) = ap
instance Functor m => Functor (LoggingT m) where
fmap f logger = LoggingT $ \loggerFn -> fmap f $ (runLoggingT logger) loggerFn
{-# INLINE fmap #-}

instance Applicative m => Applicative (LoggingT m) where
pure = LoggingT . const . pure
{-# INLINE pure #-}
loggerF <*> loggerA = LoggingT $ \loggerFn ->
(runLoggingT loggerF) loggerFn
<*> (runLoggingT loggerA) loggerFn
{-# INLINE (<*>) #-}

instance Monad m => Monad (LoggingT m) where
return = LoggingT . const . return
Expand Down