Copyright | (c) 2020 Composewell Technologies |
---|---|
License | BSD-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
CPS style implementation of parsers.
The CPS representation allows linear performance for Applicative, sequenceA, Monad, sequence, and Alternative, choice operations compared to the quadratic complexity of the corresponding direct style operations. However, direct style operations allow fusion with ~10x better performance than CPS.
The direct style representation does not allow for recursive definitions of "some" and "many" whereas CPS allows that.
Synopsis
- newtype Parser m a b = MkParser {}
- fromPure :: b -> Parser m a b
- fromEffect :: Monad m => m b -> Parser m a b
- die :: String -> Parser m a b
- toParserK :: MonadCatch m => Parser m a b -> Parser m a b
- fromParserK :: MonadThrow m => Parser m a b -> Parser m a b
Documentation
A continuation passing style parser representation.
Instances
(MonadThrow m, MonadReader r m, MonadCatch m) => MonadReader r (Parser m a) Source # | |
(MonadThrow m, MonadState s m) => MonadState s (Parser m a) Source # | |
Monad m => MonadFail (Parser m a) Source # | |
Defined in Streamly.Internal.Data.Parser.ParserK.Type | |
(MonadThrow m, MonadIO m) => MonadIO (Parser m a) Source # | |
Defined in Streamly.Internal.Data.Parser.ParserK.Type | |
Monad m => Alternative (Parser m a) Source # |
The "some" and "many" operations of alternative accumulate results in a pure
list which is not scalable and streaming. Instead use
See also |
Monad m => Applicative (Parser m a) Source # |
|
Defined in Streamly.Internal.Data.Parser.ParserK.Type | |
Functor m => Functor (Parser m a) Source # | Maps a function over the output of the parser. |
Monad m => Monad (Parser m a) Source # | Monad composition can be used for lookbehind parsers, we can make the future parses depend on the previously parsed values. If we have to parse "a9" or "9a" but not "99" or "aa" we can use the following parser: backtracking :: MonadCatch m => PR.Parser m Char String
backtracking =
sequence [PR.satisfy isDigit, PR.satisfy isAlpha]
We know that if the first parse resulted in a digit at the first place then
the second parse is going to fail. However, we waste that information and
parse the first character again in the second parse only to know that it is
not an alphabetic char. By using lookbehind in a data DigitOrAlpha = Digit Char | Alpha Char lookbehind :: MonadCatch m => PR.Parser m Char String lookbehind = do x1 <- Digit See also |
Monad m => MonadPlus (Parser m a) Source # |
Pre-release |
fromPure :: b -> Parser m a b Source #
A parser that always yields a pure value without consuming any input.
Pre-release
fromEffect :: Monad m => m b -> Parser m a b Source #
See fromEffect
.
Pre-release
die :: String -> Parser m a b Source #
A parser that always fails with an error message without consuming any input.
Pre-release
Conversion
fromParserK :: MonadThrow m => Parser m a b -> Parser m a b Source #