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

Moduleappar-0.1.8Haskell98

Text.Appar.String

Simple Applicative parser whose input is String. The usage is the same as parsec.

Parsec 3 provides features which Parsec 2 does not provide:

But Haskell Platform includes Parsec 2, not Parsec 3. Installing Parsec 3 to Haskell Platform environment makes it mess. So, this library was implemented.

  • 2 types
  • 1 class
  • 20 values
  • Packageappar-0.1.8
  • Exports31
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceString.hs

Documentation

0 declarations

Parser type

classclass Eq inp => Input inp where
#

The class for parser input.

Methods

  • car :: inp -> Char

    The head function for input

  • cdr :: inp -> inp

    The tail function for input

  • nil :: inp

    The end of input

  • isNil :: inp -> Bool

    The function to check the end of input

Instances3Input
valueoneOf :: Input inp => String -> MkParser inp Char
#

oneOf cs succeeds if the current character is in the supplied list of characters cs. Returns the parsed character.

valuenoneOf :: Input inp => String -> MkParser inp Char
#

As the dual of oneOf, noneOf cs succeeds if the current character not in the supplied list of characters cs. Returns the parsed character.

valuealphaNum :: Input inp => MkParser inp Char
#

Parses a letter or digit (a character between '0' and '9'). Returns the parsed character.

valuehexDigit :: Input inp => MkParser inp Char
#

Parses a hexadecimal digit (a digit or a letter between 'a' and 'f' or 'A' and 'F'). Returns the parsed character.

valuespace :: Input inp => MkParser inp Char
#

Parses a white space character (any character which satisfies isSpace) Returns the parsed character.

valuetry :: MkParser inp a -> MkParser inp a
#

The parser try p behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs.

valuechoice :: [MkParser inp a] -> MkParser inp 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.

valueoption :: a -> MkParser inp a -> MkParser inp a
#

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

valueskipMany :: MkParser inp a -> MkParser inp ()
#

skipMany p applies the parser p zero or more times, skipping its result.

valueskipSome :: MkParser inp a -> MkParser inp ()
#

skipSome p applies the parser p one or more times, skipping its result.

valuesepBy1 :: MkParser inp a -> MkParser inp b -> MkParser inp [a]
#

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

valuemanyTill :: MkParser inp a -> MkParser inp b -> MkParser inp [a]
#

manyTill p end applies parser p zero or more times until parser end succeeds. Returns the list of values returned by p.

value(<$>) :: Functor f => (a -> b) -> f a -> f b
#

An infix synonym for fmap.

The name of this operator is an allusion to Prelude.$. Note the similarities between their types:

 ($)  ::              (a -> b) ->   a ->   b
(<$>) :: Functor f => (a -> b) -> f a -> f b

Whereas Prelude.$ is function application, <$> is function application lifted over a Functor.

Examples

Convert from a Maybe Int to a Maybe String using show:

Example1 expression
show <$> NothingNothing
Example1 expression
show <$> Just 3Just "3"

Convert from an Either Int Int to an Either Int String using show:

Example1 expression
show <$> Left 17Left 17
Example1 expression
show <$> Right 17Right "17"

Double each element of a list:

Example1 expression
(*2) <$> [1,2,3][2,4,6]

Apply even to the second element of a pair:

Example1 expression
even <$> (2,2)(2,True)
method(<$) :: a -> f b -> f a
#

Replace all locations in the input with the same value. The default definition is fmap . const, but this may be overridden with a more efficient version.

Examples

Perform a computation with Maybe and replace the result with a constant value if it is Just:

Example2 expressions
'a' <$ Just 2Just 'a''a' <$ NothingNothing
method(<*>) :: f (a -> b) -> f a -> f b
#

Sequential application.

A few functors support an implementation of <*> that is more efficient than the default one.

Example

Used in combination with (Data.Functor.<$>), (<*>) can be used to build a record.

Example1 expression
data MyState = MyState {arg1 :: Foo, arg2 :: Bar, arg3 :: Baz}
Example3 expressions
produceFoo :: Applicative f => f FooproduceBar :: Applicative f => f BarproduceBaz :: Applicative f => f Baz
Example2 expressions
mkState :: Applicative f => f MyStatemkState = MyState <$> produceFoo <*> produceBar <*> produceBaz
method(*>) :: f a -> f b -> f b
#

Sequence actions, discarding the value of the first argument.

Examples

If used in conjunction with the Applicative instance for Maybe, you can chain Maybe computations, with a possible "early return" in case of Nothing.

Example1 expression
Just 2 *> Just 3Just 3
Example1 expression
Nothing *> Just 3Nothing

Of course a more interesting use case would be to have effectful computations instead of just returning pure values.

Example4 expressions
import Data.Charimport GHC.Internal.Text.ParserCombinators.ReadPlet p = string "my name is " *> munch1 isAlpha <* eofreadP_to_S p "my name is Simon"[("Simon","")]
method(<*) :: f a -> f b -> f a
#

Sequence actions, discarding the value of the second argument.

methodpure :: a -> f a
#

Lift a value into the Structure.

Examples
Example1 expression
pure 1 :: Maybe IntJust 1
Example1 expression
pure 'z' :: [Char]"z"
Example1 expression
pure (pure ":D") :: Maybe [String]Just [":D"]
value(<**>) :: Applicative f => f a -> f (a -> b) -> f b
#

A variant of <*> with the types of the arguments reversed. It differs from flip (<*>) in that the effects are resolved in the order the arguments are presented.

Examples
Example1 expression
(<**>) (print 1) (id <$ print 2)12
Example1 expression
flip (<*>) (print 1) (id <$ print 2)21
Example1 expression
ZipList [4, 5, 6] <**> ZipList [(+1), (*2), (/3)]ZipList {getZipList = [5.0,10.0,2.0]}
method(<|>) :: f a -> f a -> f a
#

An associative binary operation

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.

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.

datadata MkParser inp a
#

Constructors

Instances6Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus
valuesatisfy :: Input inp => (Char -> Bool) -> MkParser inp Char
#

The parser satisfy f succeeds for any character for which the supplied function f returns True. Returns the character that is actually parsed.