HORIZON HASKELLDocslts/ghc-10.0.x2b276322026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 2b27632 · 2026-10-31

Moduleparser-combinators-1.3.1Haskell2010

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.

Contents
  1. Re-exports from Control.Applicative
  2. Original combinators
Synopsis

Re-exports from Control.Applicative

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

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' :: 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.

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 '_')
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.