Agda-2.4.0.2: A dependently typed functional programming language and proof assistant

Safe HaskellNone
LanguageHaskell98

Agda.Utils.ReadP

Contents

Description

 

Synopsis

The ReadP type

data ReadP t a Source

Primitive operations

get :: ReadP t t Source

Consumes and returns the next character. Fails if there is no input left.

look :: ReadP t [t] Source

Look-ahead: returns the part of the input that is left, without consuming it.

(+++) :: ReadP t a -> ReadP t a -> ReadP t a infixr 5 Source

Symmetric choice.

(<++) :: ReadP t a -> ReadP t a -> ReadP t a infixr 5 Source

Local, exclusive, left-biased choice: If left parser locally produces any result at all, then right parser is not used.

gather :: ReadP t a -> ReadP t ([t], a) Source

Transforms a parser into one that does the same, but in addition returns the exact characters read. IMPORTANT NOTE: gather gives a runtime error if its first argument is built using any occurrences of readS_to_P.

Parse

parse :: ReadP t a -> [t] -> [a] Source

Run a parser on a list of tokens. Returns the list of complete matches.

parse' :: ReadP t a -> [t] -> Either a [t] Source

Other operations

pfail :: ReadP t a Source

Always fails.

satisfy :: (t -> Bool) -> ReadP t t Source

Consumes and returns the next character, if it satisfies the specified predicate.

char :: Eq t => t -> ReadP t t Source

Parses and returns the specified character.

string :: Eq t => [t] -> ReadP t [t] Source

Parses and returns the specified string.

munch :: (t -> Bool) -> ReadP t [t] Source

Parses the first zero or more characters satisfying the predicate.

munch1 :: (t -> Bool) -> ReadP t [t] Source

Parses the first one or more characters satisfying the predicate.

skipSpaces :: ReadP Char () Source

Skips all whitespace.

choice :: [ReadP t a] -> ReadP t a Source

Combines all parsers in the specified list.

count :: Int -> ReadP t a -> ReadP t [a] Source

count n p parses n occurrences of p in sequence. A list of results is returned.

between :: ReadP t open -> ReadP t close -> ReadP t a -> ReadP t a Source

between open close p parses open, followed by p and finally close. Only the value of p is returned.

option :: a -> ReadP t a -> ReadP t a Source

option x p will either parse p or return x without consuming any input.

optional :: ReadP t a -> ReadP t () Source

optional p optionally parses p and always returns ().

many :: ReadP t a -> ReadP t [a] Source

Parses zero or more occurrences of the given parser.

many1 :: ReadP t a -> ReadP t [a] Source

Parses one or more occurrences of the given parser.

skipMany :: ReadP t a -> ReadP t () Source

Like many, but discards the result.

skipMany1 :: ReadP t a -> ReadP t () Source

Like many1, but discards the result.

sepBy :: ReadP t a -> ReadP t sep -> ReadP t [a] Source

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

sepBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a] Source

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

endBy :: ReadP t a -> ReadP t sep -> ReadP t [a] Source

endBy p sep parses zero or more occurrences of p, separated and ended by sep.

endBy1 :: ReadP t a -> ReadP t sep -> ReadP t [a] Source

endBy p sep parses one or more occurrences of p, separated and ended by sep.

chainr :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t a Source

chainr p op x parses zero or more occurrences of p, separated by op. Returns a value produced by a right associative application of all functions returned by op. If there are no occurrences of p, x is returned.

chainl :: ReadP t a -> ReadP t (a -> a -> a) -> a -> ReadP t a Source

chainl p op x parses zero or more occurrences of p, separated by op. Returns a value produced by a left associative application of all functions returned by op. If there are no occurrences of p, x is returned.

chainl1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t a Source

Like chainl, but parses one or more occurrences of p.

chainr1 :: ReadP t a -> ReadP t (a -> a -> a) -> ReadP t a Source

Like chainr, but parses one or more occurrences of p.

manyTill :: ReadP t a -> ReadP t end -> ReadP t [a] Source

manyTill p end parses zero or more occurrences of p, until end succeeds. Returns a list of values returned by p.

Properties

The following are QuickCheck specifications of what the combinators do. These can be seen as formal specifications of the behavior of the combinators.

We use bags to give semantics to the combinators.

 type Bag a = [a]

Equality on bags does not care about the order of elements.

 (=~) :: Ord a => Bag a -> Bag a -> Bool
 xs =~ ys = sort xs == sort ys

A special equality operator to avoid unresolved overloading when testing the properties.

 (=~.) :: Bag (Int,String) -> Bag (Int,String) -> Bool
 (=~.) = (=~)

Here follow the properties:

  prop_Get_Nil =
    readP_to_S get [] =~ []

  prop_Get_Cons c s =
    readP_to_S get (c:s) =~ [(c,s)]

  prop_Look s =
    readP_to_S look s =~ [(s,s)]

  prop_Fail s =
    readP_to_S pfail s =~. []

  prop_Return x s =
    readP_to_S (return x) s =~. [(x,s)]

  prop_Bind p k s =
    readP_to_S (p >>= k) s =~.
      [ ys''
      | (x,s') <- readP_to_S p s
      , ys''   <- readP_to_S (k (x::Int)) s'
      ]

  prop_Plus p q s =
    readP_to_S (p +++ q) s =~.
      (readP_to_S p s ++ readP_to_S q s)

  prop_LeftPlus p q s =
    readP_to_S (p <++ q) s =~.
      (readP_to_S p s +<+ readP_to_S q s)
   where
    [] +<+ ys = ys
    xs +<+ _  = xs

  prop_Gather s =
    forAll readPWithoutReadS $ \p ->
      readP_to_S (gather p) s =~
	 [ ((pre,x::Int),s')
	 | (x,s') <- readP_to_S p s
	 , let pre = take (length s - length s') s
	 ]

  prop_String_Yes this s =
    readP_to_S (string this) (this ++ s) =~
      [(this,s)]

  prop_String_Maybe this s =
    readP_to_S (string this) s =~
      [(this, drop (length this) s) | this `isPrefixOf` s]

  prop_Munch p s =
    readP_to_S (munch p) s =~
      [(takeWhile p s, dropWhile p s)]

  prop_Munch1 p s =
    readP_to_S (munch1 p) s =~
      [(res,s') | let (res,s') = (takeWhile p s, dropWhile p s), not (null res)]

  prop_Choice ps s =
    readP_to_S (choice ps) s =~.
      readP_to_S (foldr (+++) pfail ps) s

  prop_ReadS r s =
    readP_to_S (readS_to_P r) s =~. r s