Skip to content

Commit a1adc35

Browse files
Tom Hardingpaf31
Tom Harding
authored andcommitted
Add Semigroup and Monoid instances for Parsers (#52)
Instances have been added to the Parser monad transformer newtype for Monoid and Semigroup.
1 parent f66c163 commit a1adc35

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/Text/Parsing/Parser.purs

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Text.Parsing.Parser
1414

1515
import Prelude
1616
import Control.Alt (class Alt)
17+
import Control.Apply (lift2)
1718
import Control.Lazy (defer, class Lazy)
1819
import Control.Monad.Error.Class (class MonadThrow, throwError)
1920
import Control.Monad.Except (class MonadError, ExceptT(..), runExceptT, mapExceptT)
@@ -23,6 +24,7 @@ import Control.Monad.Trans.Class (class MonadTrans, lift)
2324
import Control.MonadPlus (class Alternative, class MonadZero, class MonadPlus, class Plus)
2425
import Data.Either (Either(..))
2526
import Data.Identity (Identity)
27+
import Data.Monoid (class Monoid, mempty)
2628
import Data.Newtype (class Newtype, unwrap)
2729
import Data.Tuple (Tuple(..))
2830
import Text.Parsing.Parser.Pos (Position, initialPos)
@@ -72,6 +74,12 @@ hoistParserT f (ParserT m) = ParserT (mapExceptT (mapStateT f) m)
7274
instance lazyParserT :: Lazy (ParserT s m a) where
7375
defer f = ParserT (ExceptT (defer (runExceptT <<< unwrap <<< f)))
7476

77+
instance semigroupParserT :: (Monad m, Semigroup a) => Semigroup (ParserT s m a) where
78+
append = lift2 (<>)
79+
80+
instance monoidParserT :: (Monad m, Monoid a) => Monoid (ParserT s m a) where
81+
mempty = pure mempty
82+
7583
derive newtype instance functorParserT :: Functor m => Functor (ParserT s m)
7684
derive newtype instance applyParserT :: Monad m => Apply (ParserT s m)
7785
derive newtype instance applicativeParserT :: Monad m => Applicative (ParserT s m)

0 commit comments

Comments
 (0)