HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleparser-combinators-1.3.0Haskell2010

Control.Applicative.Combinators

The module provides parser combinators defined for instances of Applicative and Alternative. It also re-exports functions that are commonly used in parsing from Control.Applicative with additional parsing-related comments added.

Due to the nature of the Applicative and Alternative abstractions, they are prone to memory leaks and not as efficient as their monadic counterparts. Although all the combinators we provide in this module are perfectly expressible in terms of Applicative and Alternative, please prefer Control.Monad.Combinators instead when possible.

If you wish that the combinators that cannot return empty lists return values of the NonEmpty data type, use the Control.Applicative.Combinators.NonEmpty module.

A note on backtracking

Certain parsing libraries, such as Megaparsec, do not backtrack every branch of parsing automatically for the sake of performance and better error messages. They typically backtrack only “atomic” parsers, e.g. those that match a token or several tokens in a row. To backtrack an arbitrary complex parser/branch, a special combinator should be used, typically called try. Combinators in this module are defined in terms Applicative and Alternative operations. Being quite abstract, they cannot know anything about inner workings of any concrete parsing library, and so they cannot use try.

The essential feature of the Alternative type class is the (<|>) operator that allows to express choice. In libraries that do not backtrack everything automatically, the choice operator and everything that is build on top of it require the parser on the left hand side to backtrack in order for the alternative branch of parsing to be tried. Thus it is the responsibility of the programmer to wrap more complex, composite parsers in try to achieve correct behavior.

  • 22 values

Re-exports from Control.Applicative

5 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.

methodmany :: f a -> f [a]
#

Zero or more.

Examples
Example1 expression
many (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression
many NothingJust []
Example1 expression
take 5 <$> many (Just 1)* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case many parser will attempt to parse parser zero or more times until it fails.

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 '_')
methodsome :: f a -> f [a]
#

One or more.

Examples
Example1 expression
some (putStr "la")lalalalalalalalala... * goes on forever *
Example1 expression
some Nothingnothing
Example1 expression
take 5 <$> some (Just 1)* hangs forever *

Note that this function can be used with Parsers based on Applicatives. In that case some parser will attempt to parse parser one or more times until it fails.

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

word = some letter
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

21 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 :: Applicative 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 pure []. Returns a list of n parsed values.

count = replicateM

See also: skipCount, count'.

valuecount' :: Alternative 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 pure []. 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 :: Alternative 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 :: Alternative 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.

valuemanyTill_ :: Alternative 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.

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