|
|
|
# Replacing the Read Class
|
|
|
|
|
|
|
|
## Tickets
|
|
|
|
|
|
|
|
<table><tr><th>[\#61](https://gitlab.haskell.org//haskell/prime/issues/61)</th>
|
|
|
|
<td>replace the Read class</td></tr></table>
|
|
|
|
|
|
|
|
## Problems with the Haskell 98 class
|
|
|
|
|
|
|
|
|
|
|
|
- the backtracking parser is very inefficient
|
|
|
|
|
|
|
|
- writing instances is painful, compared to a monadic interface
|
| ... | ... | @@ -15,44 +12,19 @@ |
|
|
|
|
|
|
|
## Proposed replacements
|
|
|
|
|
|
|
|
- parsers based on [ ReadP](http://www.haskell.org/ghc/dist/current/docs/libraries/base/Text-ParserCombinators-ReadP.html), from [ Parallel Parsing Processes](http://www.md.chalmers.se/~koen/Papers/parsing-pearl.ps) by Koen Classen, JFP 2004.
|
|
|
|
|
|
|
|
- abstract type with monadic interface, invoked with
|
|
|
|
- parsers based on [ ReadP](http://www.haskell.org/ghc/docs/latest/html/libraries/base/Text-ParserCombinators-ReadP.html), from [ Parallel Parsing Processes](http://www.md.chalmers.se/~koen/Papers/parsing-pearl.ps) by Koen Classen, JFP 2004.
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
readP_to_S :: ReadP a -> String -> [(a, String)]
|
|
|
|
```
|
|
|
|
|
|
|
|
(The internal representation is more complex.)
|
|
|
|
- choice operator supports full nondeterminism by parsing alternatives in parallel.
|
|
|
|
(There is also a committed choice operator.)
|
|
|
|
- monadic interface
|
|
|
|
- supports nondeterminism without needing explicit committed choice
|
|
|
|
- mostly deterministic parsers are much more efficient than `ReadS`
|
|
|
|
- returns no information on failure, but could be altered to do so, as it knows the position at which all alternatives are exhausted.
|
|
|
|
- no error reporting
|
|
|
|
- requires [PolymorphicComponents](polymorphic-components)
|
|
|
|
|
|
|
|
GHC has used a new `Read` class based on these parsers since 5.04 (July 2002).
|
|
|
|
|
|
|
|
- Malcolm's experimental [ Poly TextParser](http://nhc98.blogspot.com/#113301726218243351) in the development version of [ HaXml](http://www.cs.york.ac.uk/fp/HaXml-1.14/).
|
|
|
|
|
|
|
|
- monadic interface, invoked with
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
runParser :: TextParser a -> String -> (Either String a, String)
|
|
|
|
```
|
|
|
|
|
|
|
|
(The internal representation is more general, parameterised on token type.)
|
|
|
|
- returns only the first successful alternative, but with internal backtracking,
|
|
|
|
and an explicit commit operator to prevent backtracking where necessary
|
|
|
|
- on failure, returns an error string and the unparsed input
|
|
|
|
- failure messages can be defined/combined/nested with convenient combinators
|
|
|
|
|
|
|
|
- Could also use something based on Daan's [ Parsec](http://www.cs.uu.nl/people/daan/parsec.html) combinators
|
|
|
|
|
|
|
|
- monadic interface, invoked with (simplifying slightly)
|
|
|
|
- Malcolm's experimental Poly parser in the CVS version of HaXml?.
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
parse :: Parser a -> SourceName -> String -> Either ParseError a
|
|
|
|
```
|
|
|
|
- mostly deterministic, with explicit 'try' operator for non-determinism
|
|
|
|
- on failure, reports an error message including the position at which the error occured.
|
|
|
|
- already widely used for other parsing tasks |
|
|
|
- monadic interface
|
|
|
|
- deterministic parser with committed choice
|
|
|
|
- returns an error string on failure |