Skip to content

Commit

Permalink
Add MonadReader and MonadState instances
Browse files Browse the repository at this point in the history
* I've pretty well convinced myself that this instance obeys the `MonadReader`
  laws
  [proposed by Heinrich Apfelmus](haskell/mtl#5 (comment))
  and the ones
  [proposed by Li-yao Xia](haskell/mtl#5 (comment)),
  so I think it should be added.

* The `MonadState` instance is trivial.
  • Loading branch information
treeowl committed Aug 26, 2021
1 parent 54118a3 commit dced94b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 9 additions & 0 deletions library/ListT.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module ListT
(
ListT(..),
Expand Down Expand Up @@ -197,6 +198,14 @@ instance MonadError e m => MonadError e (ListT m) where
throwError = ListT . throwError
catchError m handler = ListT $ catchError (uncons m) $ uncons . handler

instance MonadReader e m => MonadReader e (ListT m) where
ask = lift ask
local r (ListT m) = ListT $ local r (fmap (fmap (secondPair' (local r))) m)

instance MonadState e m => MonadState e (ListT m) where
get = lift get
put = lift . put

instance Monad m => MonadLogic (ListT m) where
msplit (ListT m) = lift m

Expand Down
10 changes: 9 additions & 1 deletion library/ListT/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ListT.Prelude
(
module Exports,
bimapPair',
secondPair',
)
where

Expand All @@ -18,11 +19,12 @@ import Control.Monad.Fix as Exports hiding (fix)
import Control.Monad.IO.Class as Exports
import Control.Monad.Logic.Class as Exports
import Control.Monad.Morph as Exports hiding (MonadTrans(..))
import Control.Monad.Reader.Class as Exports
import Control.Monad.State.Class as Exports
import Control.Monad.ST as Exports
import Control.Monad.Trans.Class as Exports
import Control.Monad.Trans.Control as Exports hiding (embed, embed_)
import Control.Monad.Trans.Maybe as Exports hiding (liftCatch, liftCallCC)
import Control.Monad.Trans.Reader as Exports hiding (liftCatch, liftCallCC)
import Control.Monad.Zip as Exports
import Data.Bits as Exports
import Data.Bool as Exports
Expand Down Expand Up @@ -84,3 +86,9 @@ import Unsafe.Coerce as Exports
-- There's no benefit to producing lazy pairs here.
bimapPair' :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
bimapPair' f g = \(a,c) -> (f a, g c)


-- A slightly stricter version of Data.Bifunctor.second
-- that doesn't produce gratuitous lazy pairs.
secondPair' :: (b -> c) -> (a, b) -> (a, c)
secondPair' f = \(a,b) -> (a, f b)

0 comments on commit dced94b

Please sign in to comment.