HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleparser-combinators-1.3.0Haskell2010

Control.Monad.Combinators

The module provides more efficient versions of the combinators from Control.Applicative.Combinators defined in terms of Monad and MonadPlus instead of Applicative and Alternative. When there is no difference in performance we just re-export the combinators from Control.Applicative.Combinators.

  • 24 values

Re-exports from Control.Applicative

3 declarations
method(<|>) :: f a -> f a -> f a
#

An associative binary operation

This combinator implements choice. The parser p <|> q first applies p. If it succeeds, the value of p is returned. If p fails, parser q is tried.

valueoptional :: Alternative f => f a -> f (Maybe a)
#

One or none.

It is useful for modelling any computation that is allowed to fail.

Examples

Using the Alternative instance of Control.Monad.Except, the following functions:

Example1 expression
import Control.Monad.Except
Example2 expressions
canFail = throwError "it failed" :: Except String Intfinal = return 42                :: Except String Int

Can be combined by allowing the first function to fail:

Example1 expression
runExcept $ canFail *> finalLeft "it failed"
Example1 expression
runExcept $ optional canFail *> finalRight 42

optional p tries to apply the parser p. It will parse p or Nothing. It only fails if p fails after consuming input. On success result of p is returned inside of Just, on failure Nothing is returned.

See also: option.

methodempty :: f a
#

The identity of <|>

empty <|> a     == a
a     <|> empty == a

This parser fails unconditionally without providing any information about the cause of the failure.

Original combinators

23 declarations
valuebetween :: Applicative m => m open -> m close -> m a -> m a
#

between open close p parses open, followed by p and close. Returns the value returned by p.

braces = between (symbol "{") (symbol "}")
valuechoice :: (Foldable f, Alternative m) => f (m a) -> m a
#

choice ps tries to apply the parsers in the list ps in order, until one of them succeeds. Returns the value of the succeeding parser.

choice = asum
valuecount :: Monad m => Int -> m a -> m [a]
#

count n p parses n occurrences of p. If n is smaller or equal to zero, the parser equals to return []. Returns a list of n values.

See also: skipCount, count'.

valuecount' :: MonadPlus m => Int -> Int -> m a -> m [a]
#

count' m n p parses from m to n occurrences of p. If n is not positive or m > n, the parser equals to return []. Returns a list of parsed values.

Please note that m may be negative, in this case effect is the same as if it were equal to zero.

See also: skipCount, count.

valueeitherP :: Alternative m => m a -> m b -> m (Either a b)
#

Combine two alternatives.

eitherP a b = (Left <$> a) <|> (Right <$> b)
valueendBy :: MonadPlus m => m a -> m sep -> m [a]
#

endBy p sep parses zero or more occurrences of p, separated and ended by sep. Returns a list of values returned by p.

cStatements = cStatement `endBy` semicolon
valueendBy1 :: MonadPlus m => m a -> m sep -> m [a]
#

endBy1 p sep parses one or more occurrences of p, separated and ended by sep. Returns a list of values returned by p.

valuemany :: MonadPlus m => m a -> m [a]
#

many p applies the parser p zero or more times and returns a list of the values returned by p.

identifier = (:) <$> letter <*> many (alphaNumChar <|> char '_')
valuemanyTill :: MonadPlus m => m a -> m end -> m [a]
#

manyTill p end applies parser p zero or more times until parser end succeeds. Returns the list of values returned by p. Note that end result is consumed and lost. Use manyTill_ if you wish to keep it.

See also: skipMany, skipManyTill.

valuemanyTill_ :: MonadPlus m => m a -> m end -> m ([a], end)
#

manyTill_ p end applies parser p zero or more times until parser end succeeds. Returns the list of values returned by p and the end result. Use manyTill if you have no need in the result of the end.

See also: skipMany, skipManyTill.

valuesome :: MonadPlus m => m a -> m [a]
#

some p applies the parser p one or more times and returns a list of the values returned by p.

word = some letter
valueoption :: Alternative m => a -> m a -> m a
#

option x p tries to apply the parser p. If p fails without consuming input, it returns the value x, otherwise the value returned by p.

option x p = p <|> pure x

See also: optional.

valuesepBy :: MonadPlus m => m a -> m sep -> m [a]
#

sepBy p sep parses zero or more occurrences of p, separated by sep. Returns a list of values returned by p.

commaSep p = p `sepBy` comma
valuesepBy1 :: MonadPlus m => m a -> m sep -> m [a]
#

sepBy1 p sep parses one or more occurrences of p, separated by sep. Returns a list of values returned by p.

valuesepEndBy :: MonadPlus m => m a -> m sep -> m [a]
#

sepEndBy p sep parses zero or more occurrences of p, separated and optionally ended by sep. Returns a list of values returned by p.

valuesepEndBy1 :: MonadPlus m => m a -> m sep -> m [a]
#

sepEndBy1 p sep parses one or more occurrences of p, separated and optionally ended by sep. Returns a list of values returned by p.