-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Compat.ReadP
-- Copyright   :  (c) The University of Glasgow 2002
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Portability :  portable
--
-- This is a library of parser combinators, originally written by Koen Claessen.
-- It parses all alternatives in parallel, so it never keeps hold of
-- the beginning of the input string, a common source of space leaks with
-- other parsers.  The '(+++)' choice combinator is genuinely commutative;
-- it makes no difference which branch is \"shorter\".
--
-- See also Koen's paper /Parallel Parsing Processes/
-- (<http://www.cs.chalmers.se/~koen/publications.html>).
--
-- This version of ReadP has been locally hacked to make it H98, by
-- Martin Sj&#xF6;gren <mailto:msjogren@gmail.com>
--
-----------------------------------------------------------------------------

module Distribution.Compat.ReadP
  (
  -- * The 'ReadP' type
  ReadP,      -- :: * -> *; instance Functor, Monad, MonadPlus

  -- * Primitive operations
  get,        -- :: ReadP Char
  look,       -- :: ReadP String
  (+++),      -- :: ReadP a -> ReadP a -> ReadP a
  (<++),      -- :: ReadP a -> ReadP a -> ReadP a
  gather,     -- :: ReadP a -> ReadP (String, a)

  -- * Other operations
  pfail,      -- :: ReadP a
  satisfy,    -- :: (Char -> Bool) -> ReadP Char
  char,       -- :: Char -> ReadP Char
  string,     -- :: String -> ReadP String
  munch,      -- :: (Char -> Bool) -> ReadP String
  munch1,     -- :: (Char -> Bool) -> ReadP String
  skipSpaces, -- :: ReadP ()
  choice,     -- :: [ReadP a] -> ReadP a
  count,      -- :: Int -> ReadP a -> ReadP [a]
  between,    -- :: ReadP open -> ReadP close -> ReadP a -> ReadP a
  option,     -- :: a -> ReadP a -> ReadP a
  optional,   -- :: ReadP a -> ReadP ()
  many,       -- :: ReadP a -> ReadP [a]
  many1,      -- :: ReadP a -> ReadP [a]
  skipMany,   -- :: ReadP a -> ReadP ()
  skipMany1,  -- :: ReadP a -> ReadP ()
  sepBy,      -- :: ReadP a -> ReadP sep -> ReadP [a]
  sepBy1,     -- :: ReadP a -> ReadP sep -> ReadP [a]
  endBy,      -- :: ReadP a -> ReadP sep -> ReadP [a]
  endBy1,     -- :: ReadP a -> ReadP sep -> ReadP [a]
  chainr,     -- :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
  chainl,     -- :: ReadP a -> ReadP (a -> a -> a) -> a -> ReadP a
  chainl1,    -- :: ReadP a -> ReadP (a -> a -> a) -> ReadP a
  chainr1,    -- :: ReadP a -> ReadP (a -> a -> a) -> ReadP a
  manyTill,   -- :: ReadP a -> ReadP end -> ReadP [a]

  -- * Running a parser
  ReadS,      -- :: *; = String -> [(a,String)]
  readP_to_S, -- :: ReadP a -> ReadS a
  readS_to_P  -- :: ReadS a -> ReadP a

  -- * Properties
  -- $properties
  )
 where

import Control.Monad( MonadPlus(..), liftM2 )
import Data.Char (isSpace)

infixr 5 +++, <++

-- ---------------------------------------------------------------------------
-- The P type
-- is representation type -- should be kept abstract

data P s a
  = Get (s -> P s a)
  | Look ([s] -> P s a)
  | Fail
  | Result a (P s a)
  | Final [(a,[s])] -- invariant: list is non-empty!

-- Monad, MonadPlus

instance D:Monad ::
  (forall a b. m a -> (a -> m b) -> m b)
  -> (forall a b. m a -> m b -> m b)
  -> (forall a. a -> m a)
  -> (forall a. String -> m a)
  -> T:Monad mMonad (P s) where
  return x = Result :: a -> P s a -> P s aResult x :: ax Fail :: P s aFail

  (Get f)      >>= k = Get :: (s -> P s a) -> P s aGet (\c -> f :: [s] -> P s af c :: sc (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= k :: a -> P s bk)
  (Look f)     >>= k = Look :: ([s] -> P s a) -> P s aLook (\s -> f :: [s] -> P s af s :: [s]s (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= k :: a -> P s bk)
  Fail         >>= _ = Fail :: P s aFail
  (Result x p) >>= k = k :: a -> P s bk x :: ax mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` (p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= k :: a -> P s bk)
  (Final r)    >>= k = final :: [(a, [s])] -> P s afinal [ys' :: (b, [s])ys' | (x,s) <- r :: [(a, [s])]r, ys' <- run :: P c a -> [c] -> [(a, [c])]run (k :: a -> P s bk x :: ax) s :: [s]s]

  fail _ = Fail :: P s aFail

instance D:MonadPlus ::
  Monad m =>
  (forall a. m a) -> (forall a. m a -> m a -> m a) -> T:MonadPlus mMonadPlus (P s) where
  mzero = Fail :: P s aFail

  -- most common case: two gets are combined
  Get f1     `mplus` Get f2     = Get :: (s -> P s a) -> P s aGet (\c -> f1 :: s -> P s af1 c :: sc mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` f2 :: s -> P s af2 c :: sc)

  -- results are delivered as soon as possible
  Result x p `mplus` q          = Result :: a -> P s a -> P s aResult x :: ax (p :: P s ap mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` q :: P s aq)
  p          `mplus` Result x q = Result :: a -> P s a -> P s aResult x :: ax (p :: P s ap mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` q :: P s aq)

  -- fail disappears
  Fail       `mplus` p          = p :: P s ap
  p          `mplus` Fail       = p :: P s ap

  -- two finals are combined
  -- final + look becomes one look and one final (=optimization)
  -- final + sthg else becomes one look and one final
  Final r    `mplus` Final t    = Final :: [(a, [s])] -> P s aFinal (r :: [(a, [s])]r (++) :: [a] -> [a] -> [a]++ t :: [(a, [s])]t)
  Final r    `mplus` Look f     = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (r :: [(a, [s])]r (++) :: [a] -> [a] -> [a]++ run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af s :: [s]s) s :: [s]s))
  Final r    `mplus` p          = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (r :: [(a, [s])]r (++) :: [a] -> [a] -> [a]++ run :: P c a -> [c] -> [(a, [c])]run p :: P s ap s :: [s]s))
  Look f     `mplus` Final r    = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af s :: [s]s) s :: [s]s (++) :: [a] -> [a] -> [a]++ r :: [(a, [s])]r))
  p          `mplus` Final r    = Look :: ([s] -> P s a) -> P s aLook (\s -> Final :: [(a, [s])] -> P s aFinal (run :: P c a -> [c] -> [(a, [c])]run p :: P s ap s :: [s]s (++) :: [a] -> [a] -> [a]++ r :: [(a, [s])]r))

  -- two looks are combined (=optimization)
  -- look + sthg else floats upwards
  Look f     `mplus` Look g     = Look :: ([s] -> P s a) -> P s aLook (\s -> f :: [s] -> P s af s :: [s]s mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` g :: [s] -> P s ag s :: [s]s)
  Look f     `mplus` p          = Look :: ([s] -> P s a) -> P s aLook (\s -> f :: [s] -> P s af s :: [s]s mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` p :: P s ap)
  p          `mplus` Look f     = Look :: ([s] -> P s a) -> P s aLook (\s -> p :: P s ap mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` f :: [s] -> P s af s :: [s]s)

-- ---------------------------------------------------------------------------
-- The ReadP type

newtype Parser r s a = R ((a -> P s r) -> P s r)
type ReadP r a = Parser r Char a

-- Functor, Monad, MonadPlus

instance D:Functor ::
  (forall a b. (a -> b) -> f a -> f b)
  -> (forall a b. a -> f b -> f a)
  -> T:Functor fFunctor (Parser r s) where
  fmap h (R f) = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> f :: [s] -> P s af (k :: a -> P s bk (.) :: (b -> c) -> (a -> b) -> a -> c. h :: a -> bh))

instance D:Monad ::
  (forall a b. m a -> (a -> m b) -> m b)
  -> (forall a b. m a -> m b -> m b)
  -> (forall a. a -> m a)
  -> (forall a. String -> m a)
  -> T:Monad mMonad (Parser r s) where
  return x  = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> k :: a -> P s bk x :: ax)
  fail _    = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\_ -> Fail :: P s aFail)
  R m >>= f = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> m :: (a -> P s r) -> P s rm (\a -> let R m' = f :: [s] -> P s af a :: aa in m' :: (b -> P s r) -> P s rm' k :: a -> P s bk))

--instance MonadPlus (Parser r s) where
--  mzero = pfail
--  mplus = (+++)

-- ---------------------------------------------------------------------------
-- Operations over P

final :: [(a,[s])] -> P s a
-- Maintains invariant for Final constructor
final [] = Fail :: P s aFail
final r  = Final :: [(a, [s])] -> P s aFinal r :: [(a, [s])]r

run :: P c a -> ([c] -> [(a, [c])])
run (Get f)      (c:s) = run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af c :: sc) s :: [s]s
run (Look f)     s     = run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af s :: [s]s) s :: [s]s
run (Result x p) s     = (x :: ax,s :: [s]s) (:) :: a -> [a] -> [a]: run :: P c a -> [c] -> [(a, [c])]run p :: P s ap s :: [s]s
run (Final r)    _     = r :: [(a, [s])]r
run _            _     = [] :: [a][]

-- ---------------------------------------------------------------------------
-- Operations over ReadP

get :: ReadP r Char
-- ^ Consumes and returns the next character.
--   Fails if there is no input left.
get = R :: ((a -> P s r) -> P s r) -> Parser r s aR Get :: (s -> P s a) -> P s aGet

look :: ReadP r String
-- ^ Look-ahead: returns the part of the input that is left, without
--   consuming it.
look = R :: ((a -> P s r) -> P s r) -> Parser r s aR Look :: ([s] -> P s a) -> P s aLook

pfail :: ReadP r a
-- ^ Always fails.
pfail = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\_ -> Fail :: P s aFail)

(+++) :: ReadP r a -> ReadP r a -> ReadP r a
-- ^ Symmetric choice.
R f1 +++ R f2 = R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> f1 :: s -> P s af1 k :: a -> P s bk mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` f2 :: s -> P s af2 k :: a -> P s bk)

(<++) :: ReadP a a -> ReadP r a -> ReadP r a
-- ^ Local, exclusive, left-biased choice: If left parser
--   locally produces any result at all, then right parser is
--   not used.
R f <++ q =
  do s <- look :: ReadP r Stringlook
     probe :: P Char a -> [Char] -> Int -> Parser r Char aprobe (f :: [s] -> P s af return :: Monad m => forall a. a -> m areturn) s :: [s]s 0
 where
  probe (Get f')       (c:s) n = probe :: P Char a -> [Char] -> Int -> Parser r Char aprobe (f' :: [Char] -> P Char af' c :: sc) s :: [s]s (n :: Intn(+) :: Num a => a -> a -> a+1 :: Int)
  probe (Look f')      s     n = probe :: P Char a -> [Char] -> Int -> Parser r Char aprobe (f' :: [Char] -> P Char af' s :: [s]s) s :: [s]s n :: Intn
  probe p@(Result _ _) _     n = discard :: Int -> Parser r Char ()discard n :: Intn (>>) :: Monad m => forall a b. m a -> m b -> m b>> R :: ((a -> P s r) -> P s r) -> Parser r s aR (p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>=)
  probe (Final r)      _     _ = R :: ((a -> P s r) -> P s r) -> Parser r s aR (Final :: [(a, [s])] -> P s aFinal r :: [(a, [s])]r (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>=)
  probe _              _     _ = q :: P s aq

  discard 0 = return :: Monad m => forall a. a -> m areturn ()
  discard n  = get :: ReadP r Charget (>>) :: Monad m => forall a b. m a -> m b -> m b>> discard :: Int -> Parser r Char ()discard (n :: Intn(-) :: Num a => a -> a -> a-1 :: Int)

gather :: ReadP (String -> P Char r) a -> ReadP r (String, a)
-- ^ 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.
gather (R m) =
  R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath id :: a -> aid (m :: (a -> P s r) -> P s rm (\a -> return :: Monad m => forall a. a -> m areturn (\s -> k :: a -> P s bk (s :: [s]s,a :: aa)))))
 where
  gath l (Get f)      = Get :: (s -> P s a) -> P s aGet (\c -> gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath (l :: [t] -> cl(.) :: (b -> c) -> (a -> b) -> a -> c.(c :: sc(:) :: a -> [a] -> [a]:)) (f :: [s] -> P s af c :: sc))
  gath _ Fail         = Fail :: P s aFail
  gath l (Look f)     = Look :: ([s] -> P s a) -> P s aLook (\s -> gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath l :: [t] -> cl (f :: [s] -> P s af s :: [s]s))
  gath l (Result k p) = k :: a -> P s bk (l :: [t] -> cl [] :: [a][]) mplus :: MonadPlus m => forall a. m a -> m a -> m a`mplus` gath :: ([t] -> c) -> P t (c -> P t a) -> P t agath l :: [t] -> cl p :: P s ap
  gath _ (Final _)    = error :: [Char] -> aerror "do not use readS_to_P in gather!"

-- ---------------------------------------------------------------------------
-- Derived operations

satisfy :: (Char -> Bool) -> ReadP r Char
-- ^ Consumes and returns the next character, if it satisfies the
--   specified predicate.
satisfy p = do c <- get :: ReadP r Charget; if p :: P s ap c :: sc then return :: Monad m => forall a. a -> m areturn c :: sc else pfail :: ReadP r apfail

char :: Char -> ReadP r Char
-- ^ Parses and returns the specified character.
char c = satisfy :: (Char -> Bool) -> ReadP r Charsatisfy (c :: sc (==) :: Eq a => a -> a -> Bool==)

string :: String -> ReadP r String
-- ^ Parses and returns the specified string.
string this = do s <- look :: ReadP r Stringlook; scan :: [Char] -> Parser r Char [Char]scan this :: Stringthis s :: [s]s
 where
  scan []     _               = do return :: Monad m => forall a. a -> m areturn this :: Stringthis
  scan (x:xs) (y:ys) | x :: ax (==) :: Eq a => a -> a -> Bool== y :: ay = do get :: ReadP r Charget (>>) :: Monad m => forall a b. m a -> m b -> m b>> scan :: [Char] -> Parser r Char [Char]scan xs :: [a]xs ys :: [a]ys
  scan _      _               = do pfail :: ReadP r apfail

munch :: (Char -> Bool) -> ReadP r String
-- ^ Parses the first zero or more characters satisfying the predicate.
munch p =
  do s <- look :: ReadP r Stringlook
     scan :: [Char] -> Parser r Char [Char]scan s :: [s]s
 where
  scan (c:cs) | p :: P s ap c :: sc = do _ <- get :: ReadP r Charget; s <- scan :: [Char] -> Parser r Char [Char]scan cs :: [Char]cs; return :: Monad m => forall a. a -> m areturn (c :: sc(:) :: a -> [a] -> [a]:s :: [s]s)
  scan _            = do return :: Monad m => forall a. a -> m areturn ""

munch1 :: (Char -> Bool) -> ReadP r String
-- ^ Parses the first one or more characters satisfying the predicate.
munch1 p =
  do c <- get :: ReadP r Charget
     if p :: P s ap c :: sc then do s <- munch :: (Char -> Bool) -> ReadP r Stringmunch p :: P s ap; return :: Monad m => forall a. a -> m areturn (c :: sc(:) :: a -> [a] -> [a]:s :: [s]s)
            else pfail :: ReadP r apfail

choice :: [ReadP r a] -> ReadP r a
-- ^ Combines all parsers in the specified list.
choice []     = pfail :: ReadP r apfail
choice [p]    = p :: P s ap
choice (p:ps) = p :: P s ap (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ choice :: [ReadP r a] -> ReadP r achoice ps :: [ReadP r a]ps

skipSpaces :: ReadP r ()
-- ^ Skips all whitespace.
skipSpaces =
  do s <- look :: ReadP r Stringlook
     skip :: [Char] -> Parser r Char ()skip s :: [s]s
 where
  skip (c:s) | isSpace :: Char -> BoolisSpace c :: sc = do _ <- get :: ReadP r Charget; skip :: [Char] -> Parser r Char ()skip s :: [s]s
  skip _                 = do return :: Monad m => forall a. a -> m areturn ()

count :: Int -> ReadP r a -> ReadP r [a]
-- ^ @ count n p @ parses @n@ occurrences of @p@ in sequence. A list of
--   results is returned.
count n p = sequence :: Monad m => [m a] -> m [a]sequence (replicate :: Int -> a -> [a]replicate n :: Intn p :: P s ap)

between :: ReadP r open -> ReadP r close -> ReadP r a -> ReadP r a
-- ^ @ between open close p @ parses @open@, followed by @p@ and finally
--   @close@. Only the value of @p@ is returned.
between open close p = do _ <- open :: ReadP r openopen
                          x <- p :: P s ap
                          _ <- close :: ReadP r closeclose
                          return :: Monad m => forall a. a -> m areturn x :: ax

option :: a -> ReadP r a -> ReadP r a
-- ^ @option x p@ will either parse @p@ or return @x@ without consuming
--   any input.
option x p = p :: P s ap (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax

optional :: ReadP r a -> ReadP r ()
-- ^ @optional p@ optionally parses @p@ and always returns @()@.
optional p = (p :: P s ap (>>) :: Monad m => forall a b. m a -> m b -> m b>> return :: Monad m => forall a. a -> m areturn ()) (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn ()

many :: ReadP r a -> ReadP r [a]
-- ^ Parses zero or more occurrences of the given parser.
many p = return :: Monad m => forall a. a -> m areturn [] :: [a][] (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ many1 :: ReadP r a -> ReadP r [a]many1 p :: P s ap

many1 :: ReadP r a -> ReadP r [a]
-- ^ Parses one or more occurrences of the given parser.
many1 p = liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2 (:) :: a -> [a] -> [a](:) p :: P s ap (many :: ReadP r a -> ReadP r [a]many p :: P s ap)

skipMany :: ReadP r a -> ReadP r ()
-- ^ Like 'many', but discards the result.
skipMany p = many :: ReadP r a -> ReadP r [a]many p :: P s ap (>>) :: Monad m => forall a b. m a -> m b -> m b>> return :: Monad m => forall a. a -> m areturn ()

skipMany1 :: ReadP r a -> ReadP r ()
-- ^ Like 'many1', but discards the result.
skipMany1 p = p :: P s ap (>>) :: Monad m => forall a b. m a -> m b -> m b>> skipMany :: ReadP r a -> ReadP r ()skipMany p :: P s ap

sepBy :: ReadP r a -> ReadP r sep -> ReadP r [a]
-- ^ @sepBy p sep@ parses zero or more occurrences of @p@, separated by @sep@.
--   Returns a list of values returned by @p@.
sepBy p sep = sepBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]sepBy1 p :: P s ap sep :: ReadP r sepsep (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn [] :: [a][]

sepBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]
-- ^ @sepBy1 p sep@ parses one or more occurrences of @p@, separated by @sep@.
--   Returns a list of values returned by @p@.
sepBy1 p sep = liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2 (:) :: a -> [a] -> [a](:) p :: P s ap (many :: ReadP r a -> ReadP r [a]many (sep :: ReadP r sepsep (>>) :: Monad m => forall a b. m a -> m b -> m b>> p :: P s ap))

endBy :: ReadP r a -> ReadP r sep -> ReadP r [a]
-- ^ @endBy p sep@ parses zero or more occurrences of @p@, separated and ended
--   by @sep@.
endBy p sep = many :: ReadP r a -> ReadP r [a]many (do x <- p :: P s ap ; _ <- sep :: ReadP r sepsep ; return :: Monad m => forall a. a -> m areturn x :: ax)

endBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]
-- ^ @endBy p sep@ parses one or more occurrences of @p@, separated and ended
--   by @sep@.
endBy1 p sep = many1 :: ReadP r a -> ReadP r [a]many1 (do x <- p :: P s ap ; _ <- sep :: ReadP r sepsep ; return :: Monad m => forall a. a -> m areturn x :: ax)

chainr :: ReadP r a -> ReadP r (a -> a -> a) -> a -> ReadP r a
-- ^ @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.
chainr p op x = chainr1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r achainr1 p :: P s ap op :: ReadP r (a -> a -> a)op (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax

chainl :: ReadP r a -> ReadP r (a -> a -> a) -> a -> ReadP r a
-- ^ @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.
chainl p op x = chainl1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r achainl1 p :: P s ap op :: ReadP r (a -> a -> a)op (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax

chainr1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r a
-- ^ Like 'chainr', but parses one or more occurrences of @p@.
chainr1 p op = scan :: [Char] -> Parser r Char [Char]scan
  where scan   = p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= rest :: a -> ReadP r arest
        rest x = do f <- op :: ReadP r (a -> a -> a)op
                    y <- scan :: [Char] -> Parser r Char [Char]scan
                    return :: Monad m => forall a. a -> m areturn (f :: [s] -> P s af x :: ax y :: ay)
                 (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax

chainl1 :: ReadP r a -> ReadP r (a -> a -> a) -> ReadP r a
-- ^ Like 'chainl', but parses one or more occurrences of @p@.
chainl1 p op = p :: P s ap (>>=) :: Monad m => forall a b. m a -> (a -> m b) -> m b>>= rest :: a -> ReadP r arest
  where rest x = do f <- op :: ReadP r (a -> a -> a)op
                    y <- p :: P s ap
                    rest :: a -> ReadP r arest (f :: [s] -> P s af x :: ax y :: ay)
                 (+++) :: ReadP r a -> ReadP r a -> ReadP r a+++ return :: Monad m => forall a. a -> m areturn x :: ax

manyTill :: ReadP r a -> ReadP [a] end -> ReadP r [a]
-- ^ @manyTill p end@ parses zero or more occurrences of @p@, until @end@
--   succeeds. Returns a list of values returned by @p@.
manyTill p end = scan :: [Char] -> Parser r Char [Char]scan
  where scan = (end :: ReadP [a] endend (>>) :: Monad m => forall a b. m a -> m b -> m b>> return :: Monad m => forall a. a -> m areturn [] :: [a][]) (<++) :: ReadP a a -> ReadP r a -> ReadP r a<++ (liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m rliftM2 (:) :: a -> [a] -> [a](:) p :: P s ap scan :: [Char] -> Parser r Char [Char]scan)

-- ---------------------------------------------------------------------------
-- Converting between ReadP and Read

readP_to_S :: ReadP a a -> ReadS a
-- ^ Converts a parser into a Haskell ReadS-style function.
--   This is the main way in which you can \"run\" a 'ReadP' parser:
--   the expanded type is
-- @ readP_to_S :: ReadP a -> String -> [(a,String)] @
readP_to_S (R f) = run :: P c a -> [c] -> [(a, [c])]run (f :: [s] -> P s af return :: Monad m => forall a. a -> m areturn)

readS_to_P :: ReadS a -> ReadP r a
-- ^ Converts a Haskell ReadS-style function into a parser.
--   Warning: This introduces local backtracking in the resulting
--   parser, and therefore a possible inefficiency.
readS_to_P r =
  R :: ((a -> P s r) -> P s r) -> Parser r s aR (\k -> Look :: ([s] -> P s a) -> P s aLook (\s -> final :: [(a, [s])] -> P s afinal [bs'' :: (r, [Char])bs'' | (a,s') <- r :: [(a, [s])]r s :: [s]s, bs'' <- run :: P c a -> [c] -> [(a, [c])]run (k :: a -> P s bk a :: aa) s' :: Strings']))

-- ---------------------------------------------------------------------------
-- QuickCheck properties that hold for the combinators

{- $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
-}