Skip to content

Commit 9a04f8a

Browse files
authored
add Monoid instance (#115)
1 parent 57577a7 commit 9a04f8a

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

src/Control/Monad/Cont/Trans.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Control.Monad.Cont.Trans
88

99
import Prelude
1010

11+
import Control.Apply (lift2)
1112
import Control.Monad.Cont.Class (class MonadCont, callCC)
1213
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
1314
import Control.Monad.State.Class (class MonadState, state)
@@ -67,3 +68,10 @@ instance monadReaderContT :: MonadReader r1 m => MonadReader r1 (ContT r m) wher
6768

6869
instance monadStateContT :: MonadState s m => MonadState s (ContT r m) where
6970
state = lift <<< state
71+
72+
instance semigroupContT :: (Apply m, Semigroup a) => Semigroup (ContT r m a) where
73+
append = lift2 (<>)
74+
75+
instance monoidContT :: (Applicative m, Monoid a) => Monoid (ContT r m a) where
76+
mempty = pure mempty
77+

src/Control/Monad/Except/Trans.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Prelude
1010

1111
import Control.Alt (class Alt)
1212
import Control.Alternative (class Alternative)
13+
import Control.Apply (lift2)
1314
import Control.Monad.Cont.Class (class MonadCont, callCC)
1415
import Control.Monad.Error.Class (class MonadThrow, class MonadError, throwError, catchError)
1516
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
@@ -136,3 +137,10 @@ instance monadWriterExceptT :: MonadWriter w m => MonadWriter w (ExceptT e m) wh
136137
pure case a of
137138
Left e -> Tuple (Left e) identity
138139
Right (Tuple r f) -> Tuple (Right r) f
140+
141+
instance semigroupExceptT :: (Monad m, Semigroup a) => Semigroup (ExceptT e m a) where
142+
append = lift2 (<>)
143+
144+
instance monoidExceptT :: (Monad m, Monoid a) => Monoid (ExceptT e m a) where
145+
mempty = pure mempty
146+

src/Control/Monad/Maybe/Trans.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Prelude
99

1010
import Control.Alt (class Alt)
1111
import Control.Alternative (class Alternative)
12+
import Control.Apply (lift2)
1213
import Control.Monad.Cont.Class (class MonadCont, callCC)
1314
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
1415
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
@@ -121,3 +122,10 @@ instance monadWriterMaybeT :: MonadWriter w m => MonadWriter w (MaybeT m) where
121122
pure case a of
122123
Nothing -> Tuple Nothing identity
123124
Just (Tuple v f) -> Tuple (Just v) f
125+
126+
instance semigroupMaybeT :: (Monad m, Semigroup a) => Semigroup (MaybeT m a) where
127+
append = lift2 (<>)
128+
129+
instance monoidMaybeT :: (Monad m, Monoid a) => Monoid (MaybeT m a) where
130+
mempty = pure mempty
131+

src/Control/Monad/RWS/Trans.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Prelude
1010

1111
import Control.Alt (class Alt, (<|>))
1212
import Control.Alternative (class Alternative)
13+
import Control.Apply (lift2)
1314
import Control.Lazy (class Lazy)
1415
import Control.Monad.Error.Class (class MonadThrow, class MonadError, throwError, catchError)
1516
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader)
@@ -130,3 +131,10 @@ instance monadRecRWST :: (MonadRec m, Monoid w) => MonadRec (RWST r w s m) where
130131

131132
instance plusRWST :: Plus m => Plus (RWST r w s m) where
132133
empty = RWST \ _ _ -> empty
134+
135+
instance semigroupRWST :: (Bind m, Monoid w, Semigroup a) => Semigroup (RWST r w s m a) where
136+
append = lift2 (<>)
137+
138+
instance monoidRWST :: (Monad m, Monoid w, Monoid a) => Monoid (RWST r w s m a) where
139+
mempty = pure mempty
140+

src/Control/Monad/State/Trans.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Prelude
1010

1111
import Control.Alt (class Alt, (<|>))
1212
import Control.Alternative (class Alternative)
13+
import Control.Apply (lift2)
1314
import Control.Lazy (class Lazy)
1415
import Control.Monad.Cont.Class (class MonadCont, callCC)
1516
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
@@ -137,3 +138,10 @@ instance monadWriterStateT :: MonadWriter w m => MonadWriter w (StateT s m) wher
137138
StateT m' -> do
138139
Tuple (Tuple a f) s' <- m' s
139140
pure $ Tuple (Tuple a s') f
141+
142+
instance semigroupStateT :: (Monad m, Semigroup a) => Semigroup (StateT s m a) where
143+
append = lift2 (<>)
144+
145+
instance monoidStateT :: (Monad m, Monoid a) => Monoid (StateT s m a) where
146+
mempty = pure mempty
147+

src/Control/Monad/Writer/Trans.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Prelude
1010

1111
import Control.Alt (class Alt, (<|>))
1212
import Control.Alternative (class Alternative)
13+
import Control.Apply (lift2)
1314
import Control.Monad.Cont.Class (class MonadCont, callCC)
1415
import Control.Monad.Error.Class (class MonadThrow, class MonadError, catchError, throwError)
1516
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
@@ -125,3 +126,10 @@ instance monadWriterWriterT :: (Monoid w, Monad m) => MonadWriter w (WriterT w m
125126
pass (WriterT m) = WriterT do
126127
Tuple (Tuple a f) w <- m
127128
pure $ Tuple a (f w)
129+
130+
instance semigroupWriterT :: (Apply m, Semigroup w, Semigroup a) => Semigroup (WriterT w m a) where
131+
append = lift2 (<>)
132+
133+
instance monoidWriterT :: (Applicative m, Monoid w, Monoid a) => Monoid (WriterT w m a) where
134+
mempty = pure mempty
135+

0 commit comments

Comments
 (0)