Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- class Read a where
- type ReadS a = String -> [(a, String)]
- reads :: Read a => ReadS a
- readMaybe :: Read a => String -> Maybe a
- readEither :: Read a => String -> Either String a
- readParen :: Bool -> ReadS a -> ReadS a
- readsData :: (String -> ReadS a) -> Int -> ReadS a
- readData :: ReadPrec a -> ReadPrec a
- readsUnaryWith :: (Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
- readUnaryWith :: ReadPrec a -> String -> (a -> t) -> ReadPrec t
- readsBinaryWith :: (Int -> ReadS a) -> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t
- readBinaryWith :: ReadPrec a -> ReadPrec b -> String -> (a -> b -> t) -> ReadPrec t
- class Read1 (f :: * -> *) where
- readsPrec1 :: (Read1 f, Read a) => Int -> ReadS (f a)
- readPrec1 :: (Read1 f, Read a) => ReadPrec (f a)
- liftReadListDefault :: Read1 f => (Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
- liftReadListPrecDefault :: Read1 f => ReadPrec a -> ReadPrec [a] -> ReadPrec [f a]
- class Read2 (f :: * -> * -> *) where
- readsPrec2 :: (Read2 f, Read a, Read b) => Int -> ReadS (f a b)
- readPrec2 :: (Read2 f, Read a, Read b) => ReadPrec (f a b)
- liftReadList2Default :: Read2 f => (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [f a b]
- liftReadListPrec2Default :: Read2 f => ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [f a b]
Documentation
Parsing of String
s, producing values.
Derived instances of Read
make the following assumptions, which
derived instances of Show
obey:
- If the constructor is defined to be an infix operator, then the
derived
Read
instance will parse only infix applications of the constructor (not the prefix form). - Associativity is not used to reduce the occurrence of parentheses, although precedence may be.
- If the constructor is defined using record syntax, the derived
Read
will parse only the record-syntax form, and furthermore, the fields must be given in the same order as the original declaration. - The derived
Read
instance allows arbitrary Haskell whitespace between tokens of the input string. Extra parentheses are also allowed.
For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance of Read
in Haskell 2010 is equivalent to
instance (Read a) => Read (Tree a) where readsPrec d r = readParen (d > app_prec) (\r -> [(Leaf m,t) | ("Leaf",s) <- lex r, (m,t) <- readsPrec (app_prec+1) s]) r ++ readParen (d > up_prec) (\r -> [(u:^:v,w) | (u,s) <- readsPrec (up_prec+1) r, (":^:",t) <- lex s, (v,w) <- readsPrec (up_prec+1) t]) r where app_prec = 10 up_prec = 5
Note that right-associativity of :^:
is unused.
The derived instance in GHC is equivalent to
instance (Read a) => Read (Tree a) where readPrec = parens $ (prec app_prec $ do Ident "Leaf" <- lexP m <- step readPrec return (Leaf m)) +++ (prec up_prec $ do u <- step readPrec Symbol ":^:" <- lexP v <- step readPrec return (u :^: v)) where app_prec = 10 up_prec = 5 readListPrec = readListPrecDefault
Why do both readsPrec
and readPrec
exist, and why does GHC opt to
implement readPrec
in derived Read
instances instead of readsPrec
?
The reason is that readsPrec
is based on the ReadS
type, and although
ReadS
is mentioned in the Haskell 2010 Report, it is not a very efficient
parser data structure.
readPrec
, on the other hand, is based on a much more efficient ReadPrec
datatype (a.k.a "new-style parsers"), but its definition relies on the use
of the RankNTypes
language extension. Therefore, readPrec
(and its
cousin, readListPrec
) are marked as GHC-only. Nevertheless, it is
recommended to use readPrec
instead of readsPrec
whenever possible
for the efficiency improvements it brings.
As mentioned above, derived Read
instances in GHC will implement
readPrec
instead of readsPrec
. The default implementations of
readsPrec
(and its cousin, readList
) will simply use readPrec
under
the hood. If you are writing a Read
instance by hand, it is recommended
to write it like so:
instanceRead
T wherereadPrec
= ...readListPrec
=readListPrecDefault
:: Int | the operator precedence of the enclosing
context (a number from |
-> ReadS a |
attempts to parse a value from the front of the string, returning a list of (parsed value, remaining string) pairs. If there is no successful parse, the returned list is empty.
Derived instances of Read
and Show
satisfy the following:
That is, readsPrec
parses the string produced by
showsPrec
, and delivers the value that
showsPrec
started with.
The method readList
is provided to allow the programmer to
give a specialised way of parsing lists of values.
For example, this is used by the predefined Read
instance of
the Char
type, where values of type String
should be are
expected to use double quotes, rather than square brackets.
Proposed replacement for readsPrec
using new-style parsers (GHC only).
readListPrec :: ReadPrec [a] #
Proposed replacement for readList
using new-style parsers (GHC only).
The default definition uses readList
. Instances that define readPrec
should also define readListPrec
as readListPrecDefault
.
Instances
readMaybe :: Read a => String -> Maybe a #
Parse a string using the Read
instance.
Succeeds if there is exactly one valid result.
>>>
readMaybe "123" :: Maybe Int
Just 123
>>>
readMaybe "hello" :: Maybe Int
Nothing
Since: base-4.6.0.0
readsData :: (String -> ReadS a) -> Int -> ReadS a #
is a parser for datatypes where each alternative
begins with a data constructor. It parses the constructor and
passes it to readsData
p dp
. Parsers for various constructors can be constructed
with readsUnary
, readsUnary1
and readsBinary1
, and combined with
mappend
from the Monoid
class.
Since: base-4.9.0.0
readData :: ReadPrec a -> ReadPrec a #
is a parser for datatypes where each alternative
begins with a data constructor. It parses the constructor and
passes it to readData
pp
. Parsers for various constructors can be constructed
with readUnaryWith
and readBinaryWith
, and combined with
'(|)' from the Alternative
class.
Since: base-4.10.0.0
readsUnaryWith :: (Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t #
matches the name of a unary data constructor
and then parses its argument using readsUnaryWith
rp n c n'rp
.
Since: base-4.9.0.0
readUnaryWith :: ReadPrec a -> String -> (a -> t) -> ReadPrec t #
matches the name of a unary data constructor
and then parses its argument using readUnaryWith
rp n c'rp
.
Since: base-4.10.0.0
readsBinaryWith :: (Int -> ReadS a) -> (Int -> ReadS b) -> String -> (a -> b -> t) -> String -> ReadS t #
matches the name of a binary
data constructor and then parses its arguments using readsBinaryWith
rp1 rp2 n c n'rp1
and rp2
respectively.
Since: base-4.9.0.0
readBinaryWith :: ReadPrec a -> ReadPrec b -> String -> (a -> b -> t) -> ReadPrec t #
matches the name of a binary
data constructor and then parses its arguments using readBinaryWith
rp1 rp2 n c'rp1
and rp2
respectively.
Since: base-4.10.0.0
class Read1 (f :: * -> *) where #
Lifting of the Read
class to unary type constructors.
Both liftReadsPrec
and liftReadPrec
exist to match the interface
provided in the Read
type class, but it is recommended to implement
Read1
instances using liftReadPrec
as opposed to liftReadsPrec
, since
the former is more efficient than the latter. For example:
instanceRead1
T whereliftReadPrec
= ...liftReadListPrec
=liftReadListPrecDefault
For more information, refer to the documentation for the Read
class.
Since: base-4.9.0.0
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a) #
readsPrec
function for an application of the type constructor
based on readsPrec
and readList
functions for the argument type.
Since: base-4.9.0.0
liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [f a] #
readList
function for an application of the type constructor
based on readsPrec
and readList
functions for the argument type.
The default implementation using standard list syntax is correct
for most types.
Since: base-4.9.0.0
liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (f a) #
readPrec
function for an application of the type constructor
based on readPrec
and readListPrec
functions for the argument type.
Since: base-4.10.0.0
liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [f a] #
readListPrec
function for an application of the type constructor
based on readPrec
and readListPrec
functions for the argument type.
The default definition uses liftReadList
. Instances that define
liftReadPrec
should also define liftReadListPrec
as
liftReadListPrecDefault
.
Since: base-4.10.0.0
Instances
readPrec1 :: (Read1 f, Read a) => ReadPrec (f a) #
Lift the standard readPrec
and readListPrec
functions through the
type constructor.
Since: base-4.10.0.0
liftReadListDefault :: Read1 f => (Int -> ReadS a) -> ReadS [a] -> ReadS [f a] #
A possible replacement definition for the liftReadList
method.
This is only needed for Read1
instances where liftReadListPrec
isn't
defined as liftReadListPrecDefault
.
Since: base-4.10.0.0
liftReadListPrecDefault :: Read1 f => ReadPrec a -> ReadPrec [a] -> ReadPrec [f a] #
A possible replacement definition for the liftReadListPrec
method,
defined using liftReadPrec
.
Since: base-4.10.0.0
class Read2 (f :: * -> * -> *) where #
Lifting of the Read
class to binary type constructors.
Both liftReadsPrec2
and liftReadPrec2
exist to match the interface
provided in the Read
type class, but it is recommended to implement
Read2
instances using liftReadPrec2
as opposed to liftReadsPrec2
,
since the former is more efficient than the latter. For example:
instanceRead2
T whereliftReadPrec2
= ...liftReadListPrec2
=liftReadListPrec2Default
For more information, refer to the documentation for the Read
class.
@since 4.9.0.0
liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (f a b) #
readsPrec
function for an application of the type constructor
based on readsPrec
and readList
functions for the argument types.
Since: base-4.9.0.0
liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [f a b] #
readList
function for an application of the type constructor
based on readsPrec
and readList
functions for the argument types.
The default implementation using standard list syntax is correct
for most types.
Since: base-4.9.0.0
liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (f a b) #
readPrec
function for an application of the type constructor
based on readPrec
and readListPrec
functions for the argument types.
Since: base-4.10.0.0
liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [f a b] #
readListPrec
function for an application of the type constructor
based on readPrec
and readListPrec
functions for the argument types.
The default definition uses liftReadList2
. Instances that define
liftReadPrec2
should also define liftReadListPrec2
as
liftReadListPrec2Default
.
Since: base-4.10.0.0
Instances
readsPrec2 :: (Read2 f, Read a, Read b) => Int -> ReadS (f a b) #
Lift the standard readsPrec
function through the type constructor.
Since: base-4.9.0.0
readPrec2 :: (Read2 f, Read a, Read b) => ReadPrec (f a b) #
Lift the standard readPrec
function through the type constructor.
Since: base-4.10.0.0
liftReadList2Default :: Read2 f => (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [f a b] #
A possible replacement definition for the liftReadList2
method.
This is only needed for Read2
instances where liftReadListPrec2
isn't
defined as liftReadListPrec2Default
.
Since: base-4.10.0.0
liftReadListPrec2Default :: Read2 f => ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [f a b] #
A possible replacement definition for the liftReadListPrec2
method,
defined using liftReadPrec2
.
Since: base-4.10.0.0