Text.Read.Compat

base-compat-0.14.1: A compatibility layer for base

Safe Haskell | Safe-Inferred
Language | Haskell2010

Text.Read.Compat

Contents

- The `Read` class
- Haskell 2010 functions
- New parsing functions

Synopsis

- class Read a where
  - readsPrec :: Int -> ReadS a
  - readList :: ReadS [a]
  - readPrec :: ReadPrec a
  - readListPrec :: ReadPrec [a]
- type ReadS a = String -> [(a, String)]
- reads :: Read a => ReadS a
- read :: Read a => String -> a
- readParen :: Bool -> ReadS a -> ReadS a
- lex :: ReadS String
- module Text.ParserCombinators.ReadPrec
- data Lexeme
  - = Char Char
  - | String String
  - | Punc String
  - | Ident String
  - | Symbol String
  - | Number Number
  - | EOF
- lexP :: ReadPrec Lexeme
- parens :: ReadPrec a -> ReadPrec a
- readListDefault :: Read a => ReadS [a]
- readListPrecDefault :: Read a => ReadPrec [a]
- readEither :: Read a => String -> Either String a
- readMaybe :: Read a => String -> Maybe a

# The `Read` class

class Read a where #

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:

    instance Read T where
      readPrec     = ...
      readListPrec = readListPrecDefault

Minimal complete definition

readsPrec | readPrec

Methods

readsPrec #

Arguments

:: Int | the operator precedence of the enclosing context (a number from `0` to `11`). Function application has precedence `10`.
-> 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:

- `(x,"")` is an element of `(`readsPrec` d (`showsPrec` d x ""))`.

That is, `readsPrec` parses the string produced by `showsPrec`, and delivers the value that `showsPrec` started with.

readList :: ReadS [a] #

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` are expected to use double quotes, rather than square brackets.

readPrec :: ReadPrec a #

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

Instances details

Read Void | Reading a `Void` value is always a parse error, considering `Void` as a data type with no constructors. @since base-4.8.0.0
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Void # readList :: ReadS [Void] # readPrec :: ReadPrec Void # readListPrec :: ReadPrec [Void] #
Read All | @since base-2.01
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods readsPrec :: Int -> ReadS All # readList :: ReadS [All] # readPrec :: ReadPrec All # readListPrec :: ReadPrec [All] #
Read Any | @since base-2.01
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods readsPrec :: Int -> ReadS Any # readList :: ReadS [Any] # readPrec :: ReadPrec Any # readListPrec :: ReadPrec [Any] #
Read Version | @since base-2.01
Instance details Defined in GHC.Internal.Data.Version Methods readsPrec :: Int -> ReadS Version # readList :: ReadS [Version] # readPrec :: ReadPrec Version # readListPrec :: ReadPrec [Version] #
Read IntPtr |
Instance details Defined in GHC.Internal.Foreign.Ptr Methods readsPrec :: Int -> ReadS IntPtr # readList :: ReadS [IntPtr] # readPrec :: ReadPrec IntPtr # readListPrec :: ReadPrec [IntPtr] #
Read WordPtr |
Instance details Defined in GHC.Internal.Foreign.Ptr Methods readsPrec :: Int -> ReadS WordPtr # readList :: ReadS [WordPtr] # readPrec :: ReadPrec WordPtr # readListPrec :: ReadPrec [WordPtr] #
Read Associativity | @since base-4.6.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS Associativity # readList :: ReadS [Associativity] # readPrec :: ReadPrec Associativity # readListPrec :: ReadPrec [Associativity] #
Read DecidedStrictness | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS DecidedStrictness # readList :: ReadS [DecidedStrictness] # readPrec :: ReadPrec DecidedStrictness # readListPrec :: ReadPrec [DecidedStrictness] #
Read Fixity | @since base-4.6.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS Fixity # readList :: ReadS [Fixity] # readPrec :: ReadPrec Fixity # readListPrec :: ReadPrec [Fixity] #
Read SourceStrictness | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS SourceStrictness # readList :: ReadS [SourceStrictness] # readPrec :: ReadPrec SourceStrictness # readListPrec :: ReadPrec [SourceStrictness] #
Read SourceUnpackedness | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS SourceUnpackedness # readList :: ReadS [SourceUnpackedness] # readPrec :: ReadPrec SourceUnpackedness # readListPrec :: ReadPrec [SourceUnpackedness] #
Read SeekMode | @since base-4.2.0.0
Instance details Defined in GHC.Internal.IO.Device Methods readsPrec :: Int -> ReadS SeekMode # readList :: ReadS [SeekMode] # readPrec :: ReadPrec SeekMode # readListPrec :: ReadPrec [SeekMode] #
Read ExitCode |
Instance details Defined in GHC.Internal.IO.Exception Methods readsPrec :: Int -> ReadS ExitCode # readList :: ReadS [ExitCode] # readPrec :: ReadPrec ExitCode # readListPrec :: ReadPrec [ExitCode] #
Read BufferMode | @since base-4.2.0.0
Instance details Defined in GHC.Internal.IO.Handle.Types Methods readsPrec :: Int -> ReadS BufferMode # readList :: ReadS [BufferMode] # readPrec :: ReadPrec BufferMode # readListPrec :: ReadPrec [BufferMode] #
Read Newline | @since base-4.3.0.0
Instance details Defined in GHC.Internal.IO.Handle.Types Methods readsPrec :: Int -> ReadS Newline # readList :: ReadS [Newline] # readPrec :: ReadPrec Newline # readListPrec :: ReadPrec [Newline] #
Read NewlineMode | @since base-4.3.0.0
Instance details Defined in GHC.Internal.IO.Handle.Types Methods readsPrec :: Int -> ReadS NewlineMode # readList :: ReadS [NewlineMode] # readPrec :: ReadPrec NewlineMode # readListPrec :: ReadPrec [NewlineMode] #
Read IOMode | @since base-4.2.0.0
Instance details Defined in GHC.Internal.IO.IOMode Methods readsPrec :: Int -> ReadS IOMode # readList :: ReadS [IOMode] # readPrec :: ReadPrec IOMode # readListPrec :: ReadPrec [IOMode] #
Read Int16 | @since base-2.01
Instance details Defined in GHC.Internal.Int Methods readsPrec :: Int -> ReadS Int16 # readList :: ReadS [Int16] # readPrec :: ReadPrec Int16 # readListPrec :: ReadPrec [Int16] #
Read Int32 | @since base-2.01
Instance details Defined in GHC.Internal.Int Methods readsPrec :: Int -> ReadS Int32 # readList :: ReadS [Int32] # readPrec :: ReadPrec Int32 # readListPrec :: ReadPrec [Int32] #
Read Int64 | @since base-2.01
Instance details Defined in GHC.Internal.Int Methods readsPrec :: Int -> ReadS Int64 # readList :: ReadS [Int64] # readPrec :: ReadPrec Int64 # readListPrec :: ReadPrec [Int64] #
Read Int8 | @since base-2.01
Instance details Defined in GHC.Internal.Int Methods readsPrec :: Int -> ReadS Int8 # readList :: ReadS [Int8] # readPrec :: ReadPrec Int8 # readListPrec :: ReadPrec [Int8] #
Read Lexeme | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Lexeme # readList :: ReadS [Lexeme] # readPrec :: ReadPrec Lexeme # readListPrec :: ReadPrec [Lexeme] #
Read GeneralCategory | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS GeneralCategory # readList :: ReadS [GeneralCategory] # readPrec :: ReadPrec GeneralCategory # readListPrec :: ReadPrec [GeneralCategory] #
Read Word16 | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Word16 # readList :: ReadS [Word16] # readPrec :: ReadPrec Word16 # readListPrec :: ReadPrec [Word16] #
Read Word32 | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Word32 # readList :: ReadS [Word32] # readPrec :: ReadPrec Word32 # readListPrec :: ReadPrec [Word32] #
Read Word64 | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Word64 # readList :: ReadS [Word64] # readPrec :: ReadPrec Word64 # readListPrec :: ReadPrec [Word64] #
Read Word8 | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Word8 # readList :: ReadS [Word8] # readPrec :: ReadPrec Word8 # readListPrec :: ReadPrec [Word8] #
Read Ordering | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Ordering # readList :: ReadS [Ordering] # readPrec :: ReadPrec Ordering # readListPrec :: ReadPrec [Ordering] #
Read RTLDFlags |
Instance details Defined in System.Posix.DynamicLinker.Prim Methods readsPrec :: Int -> ReadS RTLDFlags # readList :: ReadS [RTLDFlags] # readPrec :: ReadPrec RTLDFlags # readListPrec :: ReadPrec [RTLDFlags] #
Read Integer | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Integer # readList :: ReadS [Integer] # readPrec :: ReadPrec Integer # readListPrec :: ReadPrec [Integer] #
Read Natural | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Natural # readList :: ReadS [Natural] # readPrec :: ReadPrec Natural # readListPrec :: ReadPrec [Natural] #
Read () | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS () # readList :: ReadS [()] # readPrec :: ReadPrec () # readListPrec :: ReadPrec [()] #
Read Bool | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Bool # readList :: ReadS [Bool] # readPrec :: ReadPrec Bool # readListPrec :: ReadPrec [Bool] #
Read Char | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Char # readList :: ReadS [Char] # readPrec :: ReadPrec Char # readListPrec :: ReadPrec [Char] #
Read Double | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Double # readList :: ReadS [Double] # readPrec :: ReadPrec Double # readListPrec :: ReadPrec [Double] #
Read Float | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Float # readList :: ReadS [Float] # readPrec :: ReadPrec Float # readListPrec :: ReadPrec [Float] #
Read Int | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Int # readList :: ReadS [Int] # readPrec :: ReadPrec Int # readListPrec :: ReadPrec [Int] #
Read Word | @since base-4.5.0.0
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Word # readList :: ReadS [Word] # readPrec :: ReadPrec Word # readListPrec :: ReadPrec [Word] #
Read a => Read (Complex a) | Since: base-2.1
Instance details Defined in Data.Complex Methods readsPrec :: Int -> ReadS (Complex a) # readList :: ReadS [Complex a] # readPrec :: ReadPrec (Complex a) # readListPrec :: ReadPrec [Complex a] #
Read a => Read (First a) | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods readsPrec :: Int -> ReadS (First a) # readList :: ReadS [First a] # readPrec :: ReadPrec (First a) # readListPrec :: ReadPrec [First a] #
Read a => Read (Last a) | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods readsPrec :: Int -> ReadS (Last a) # readList :: ReadS [Last a] # readPrec :: ReadPrec (Last a) # readListPrec :: ReadPrec [Last a] #
Read a => Read (Max a) | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods readsPrec :: Int -> ReadS (Max a) # readList :: ReadS [Max a] # readPrec :: ReadPrec (Max a) # readListPrec :: ReadPrec [Max a] #
Read a => Read (Min a) | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods readsPrec :: Int -> ReadS (Min a) # readList :: ReadS [Min a] # readPrec :: ReadPrec (Min a) # readListPrec :: ReadPrec [Min a] #
Read m => Read (WrappedMonoid m) | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods readsPrec :: Int -> ReadS (WrappedMonoid m) # readList :: ReadS [WrappedMonoid m] # readPrec :: ReadPrec (WrappedMonoid m) # readListPrec :: ReadPrec [WrappedMonoid m] #
Read a => Read (NonEmpty a) | @since base-4.11.0.0
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (NonEmpty a) # readList :: ReadS [NonEmpty a] # readPrec :: ReadPrec (NonEmpty a) # readListPrec :: ReadPrec [NonEmpty a] #
Read a => Read (And a) | @since base-4.16
Instance details Defined in GHC.Internal.Data.Bits Methods readsPrec :: Int -> ReadS (And a) # readList :: ReadS [And a] # readPrec :: ReadPrec (And a) # readListPrec :: ReadPrec [And a] #
Read a => Read (Iff a) | @since base-4.16
Instance details Defined in GHC.Internal.Data.Bits Methods readsPrec :: Int -> ReadS (Iff a) # readList :: ReadS [Iff a] # readPrec :: ReadPrec (Iff a) # readListPrec :: ReadPrec [Iff a] #
Read a => Read (Ior a) | @since base-4.16
Instance details Defined in GHC.Internal.Data.Bits Methods readsPrec :: Int -> ReadS (Ior a) # readList :: ReadS [Ior a] # readPrec :: ReadPrec (Ior a) # readListPrec :: ReadPrec [Ior a] #
Read a => Read (Xor a) | @since base-4.16
Instance details Defined in GHC.Internal.Data.Bits Methods readsPrec :: Int -> ReadS (Xor a) # readList :: ReadS [Xor a] # readPrec :: ReadPrec (Xor a) # readListPrec :: ReadPrec [Xor a] #
Read a => Read (Identity a) | This instance would be equivalent to the derived instances of the `Identity` newtype if the `runIdentity` field were removed @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Functor.Identity Methods readsPrec :: Int -> ReadS (Identity a) # readList :: ReadS [Identity a] # readPrec :: ReadPrec (Identity a) # readListPrec :: ReadPrec [Identity a] #
Read a => Read (First a) | @since base-2.01
Instance details Defined in GHC.Internal.Data.Monoid Methods readsPrec :: Int -> ReadS (First a) # readList :: ReadS [First a] # readPrec :: ReadPrec (First a) # readListPrec :: ReadPrec [First a] #
Read a => Read (Last a) | @since base-2.01
Instance details Defined in GHC.Internal.Data.Monoid Methods readsPrec :: Int -> ReadS (Last a) # readList :: ReadS [Last a] # readPrec :: ReadPrec (Last a) # readListPrec :: ReadPrec [Last a] #
Read a => Read (Dual a) | @since base-2.01
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods readsPrec :: Int -> ReadS (Dual a) # readList :: ReadS [Dual a] # readPrec :: ReadPrec (Dual a) # readListPrec :: ReadPrec [Dual a] #
Read a => Read (Product a) | @since base-2.01
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods readsPrec :: Int -> ReadS (Product a) # readList :: ReadS [Product a] # readPrec :: ReadPrec (Product a) # readListPrec :: ReadPrec [Product a] #
Read a => Read (Sum a) | @since base-2.01
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods readsPrec :: Int -> ReadS (Sum a) # readList :: ReadS [Sum a] # readPrec :: ReadPrec (Sum a) # readListPrec :: ReadPrec [Sum a] #
Read p => Read (Par1 p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS (Par1 p) # readList :: ReadS [Par1 p] # readPrec :: ReadPrec (Par1 p) # readListPrec :: ReadPrec [Par1 p] #
(Integral a, Read a) => Read (Ratio a) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (Ratio a) # readList :: ReadS [Ratio a] # readPrec :: ReadPrec (Ratio a) # readListPrec :: ReadPrec [Ratio a] #
Read a => Read (Maybe a) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (Maybe a) # readList :: ReadS [Maybe a] # readPrec :: ReadPrec (Maybe a) # readListPrec :: ReadPrec [Maybe a] #
Read a => Read (Solo a) | @since base-4.15
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (Solo a) # readList :: ReadS [Solo a] # readPrec :: ReadPrec (Solo a) # readListPrec :: ReadPrec [Solo a] #
Read a => Read [a] | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS [a] # readList :: ReadS [[a]] # readPrec :: ReadPrec [a] # readListPrec :: ReadPrec [[a]] #
HasResolution a => Read (Fixed a) | Since: base-4.3.0.0
Instance details Defined in Data.Fixed Methods readsPrec :: Int -> ReadS (Fixed a) # readList :: ReadS [Fixed a] # readPrec :: ReadPrec (Fixed a) # readListPrec :: ReadPrec [Fixed a] #
(Read a, Read b) => Read (Arg a b) | Since: base-4.9.0.0
Instance details Defined in Data.Semigroup Methods readsPrec :: Int -> ReadS (Arg a b) # readList :: ReadS [Arg a b] # readPrec :: ReadPrec (Arg a b) # readListPrec :: ReadPrec [Arg a b] #
(Ix a, Read a, Read b) => Read (Array a b) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (Array a b) # readList :: ReadS [Array a b] # readPrec :: ReadPrec (Array a b) # readListPrec :: ReadPrec [Array a b] #
(Read a, Read b) => Read (Either a b) | @since base-3.0
Instance details Defined in GHC.Internal.Data.Either Methods readsPrec :: Int -> ReadS (Either a b) # readList :: ReadS [Either a b] # readPrec :: ReadPrec (Either a b) # readListPrec :: ReadPrec [Either a b] #
Read (Proxy t) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Data.Proxy Methods readsPrec :: Int -> ReadS (Proxy t) # readList :: ReadS [Proxy t] # readPrec :: ReadPrec (Proxy t) # readListPrec :: ReadPrec [Proxy t] #
Read (U1 p) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS (U1 p) # readList :: ReadS [U1 p] # readPrec :: ReadPrec (U1 p) # readListPrec :: ReadPrec [U1 p] #
Read (V1 p) | @since base-4.9.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS (V1 p) # readList :: ReadS [V1 p] # readPrec :: ReadPrec (V1 p) # readListPrec :: ReadPrec [V1 p] #
(Read a, Read b) => Read (a, b) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b) # readList :: ReadS [(a, b)] # readPrec :: ReadPrec (a, b) # readListPrec :: ReadPrec [(a, b)] #
Read a => Read (Const a b) | This instance would be equivalent to the derived instances of the `Const` newtype if the `getConst` field were removed @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Functor.Const Methods readsPrec :: Int -> ReadS (Const a b) # readList :: ReadS [Const a b] # readPrec :: ReadPrec (Const a b) # readListPrec :: ReadPrec [Const a b] #
Read (f a) => Read (Ap f a) | @since base-4.12.0.0
Instance details Defined in GHC.Internal.Data.Monoid Methods readsPrec :: Int -> ReadS (Ap f a) # readList :: ReadS [Ap f a] # readPrec :: ReadPrec (Ap f a) # readListPrec :: ReadPrec [Ap f a] #
Read (f a) => Read (Alt f a) | @since base-4.8.0.0
Instance details Defined in GHC.Internal.Data.Semigroup.Internal Methods readsPrec :: Int -> ReadS (Alt f a) # readList :: ReadS [Alt f a] # readPrec :: ReadPrec (Alt f a) # readListPrec :: ReadPrec [Alt f a] #
Coercible a b => Read (Coercion a b) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Data.Type.Coercion Methods readsPrec :: Int -> ReadS (Coercion a b) # readList :: ReadS [Coercion a b] # readPrec :: ReadPrec (Coercion a b) # readListPrec :: ReadPrec [Coercion a b] #
a ~ b => Read (a :~: b) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Data.Type.Equality Methods readsPrec :: Int -> ReadS (a :~: b) # readList :: ReadS [a :~: b] # readPrec :: ReadPrec (a :~: b) # readListPrec :: ReadPrec [a :~: b] #
Read (f p) => Read (Rec1 f p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS (Rec1 f p) # readList :: ReadS [Rec1 f p] # readPrec :: ReadPrec (Rec1 f p) # readListPrec :: ReadPrec [Rec1 f p] #
(Read a, Read b, Read c) => Read (a, b, c) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c) # readList :: ReadS [(a, b, c)] # readPrec :: ReadPrec (a, b, c) # readListPrec :: ReadPrec [(a, b, c)] #
(Read (f a), Read (g a)) => Read (Product f g a) | Since: base-4.18.0.0
Instance details Defined in Data.Functor.Product Methods readsPrec :: Int -> ReadS (Product f g a) # readList :: ReadS [Product f g a] # readPrec :: ReadPrec (Product f g a) # readListPrec :: ReadPrec [Product f g a] #
(Read (f a), Read (g a)) => Read (Sum f g a) | Since: base-4.18.0.0
Instance details Defined in Data.Functor.Sum Methods readsPrec :: Int -> ReadS (Sum f g a) # readList :: ReadS [Sum f g a] # readPrec :: ReadPrec (Sum f g a) # readListPrec :: ReadPrec [Sum f g a] #
a ~~ b => Read (a :~~: b) | @since base-4.10.0.0
Instance details Defined in GHC.Internal.Data.Type.Equality Methods readsPrec :: Int -> ReadS (a :~~: b) # readList :: ReadS [a :~~: b] # readPrec :: ReadPrec (a :~~: b) # readListPrec :: ReadPrec [a :~~: b] #
(Read (f p), Read (g p)) => Read ((f :*: g) p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS ((f :*: g) p) # readList :: ReadS [(f :*: g) p] # readPrec :: ReadPrec ((f :*: g) p) # readListPrec :: ReadPrec [(f :*: g) p] #
(Read (f p), Read (g p)) => Read ((f :+: g) p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS ((f :+: g) p) # readList :: ReadS [(f :+: g) p] # readPrec :: ReadPrec ((f :+: g) p) # readListPrec :: ReadPrec [(f :+: g) p] #
Read c => Read (K1 i c p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS (K1 i c p) # readList :: ReadS [K1 i c p] # readPrec :: ReadPrec (K1 i c p) # readListPrec :: ReadPrec [K1 i c p] #
(Read a, Read b, Read c, Read d) => Read (a, b, c, d) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d) # readList :: ReadS [(a, b, c, d)] # readPrec :: ReadPrec (a, b, c, d) # readListPrec :: ReadPrec [(a, b, c, d)] #
Read (f (g a)) => Read (Compose f g a) | Since: base-4.18.0.0
Instance details Defined in Data.Functor.Compose Methods readsPrec :: Int -> ReadS (Compose f g a) # readList :: ReadS [Compose f g a] # readPrec :: ReadPrec (Compose f g a) # readListPrec :: ReadPrec [Compose f g a] #
Read (f (g p)) => Read ((f :.: g) p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS ((f :.: g) p) # readList :: ReadS [(f :.: g) p] # readPrec :: ReadPrec ((f :.: g) p) # readListPrec :: ReadPrec [(f :.: g) p] #
Read (f p) => Read (M1 i c f p) | @since base-4.7.0.0
Instance details Defined in GHC.Internal.Generics Methods readsPrec :: Int -> ReadS (M1 i c f p) # readList :: ReadS [M1 i c f p] # readPrec :: ReadPrec (M1 i c f p) # readListPrec :: ReadPrec [M1 i c f p] #
(Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e) # readList :: ReadS [(a, b, c, d, e)] # readPrec :: ReadPrec (a, b, c, d, e) # readListPrec :: ReadPrec [(a, b, c, d, e)] #
(Read a, Read b, Read c, Read d, Read e, Read f) => Read (a, b, c, d, e, f) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f) # readList :: ReadS [(a, b, c, d, e, f)] # readPrec :: ReadPrec (a, b, c, d, e, f) # readListPrec :: ReadPrec [(a, b, c, d, e, f)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g) => Read (a, b, c, d, e, f, g) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g) # readList :: ReadS [(a, b, c, d, e, f, g)] # readPrec :: ReadPrec (a, b, c, d, e, f, g) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h) => Read (a, b, c, d, e, f, g, h) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h) # readList :: ReadS [(a, b, c, d, e, f, g, h)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i) => Read (a, b, c, d, e, f, g, h, i) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i) # readList :: ReadS [(a, b, c, d, e, f, g, h, i)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j) => Read (a, b, c, d, e, f, g, h, i, j) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i, j) # readList :: ReadS [(a, b, c, d, e, f, g, h, i, j)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i, j) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i, j)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k) => Read (a, b, c, d, e, f, g, h, i, j, k) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i, j, k) # readList :: ReadS [(a, b, c, d, e, f, g, h, i, j, k)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i, j, k) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i, j, k)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l) => Read (a, b, c, d, e, f, g, h, i, j, k, l) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i, j, k, l) # readList :: ReadS [(a, b, c, d, e, f, g, h, i, j, k, l)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i, j, k, l) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i, j, k, l)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i, j, k, l, m) # readList :: ReadS [(a, b, c, d, e, f, g, h, i, j, k, l, m)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i, j, k, l, m) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i, j, k, l, m)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m, Read n) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i, j, k, l, m, n) # readList :: ReadS [(a, b, c, d, e, f, g, h, i, j, k, l, m, n)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i, j, k, l, m, n) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i, j, k, l, m, n)] #
(Read a, Read b, Read c, Read d, Read e, Read f, Read g, Read h, Read i, Read j, Read k, Read l, Read m, Read n, Read o) => Read (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) # readList :: ReadS [(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)] # readPrec :: ReadPrec (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) # readListPrec :: ReadPrec [(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)] #

type ReadS a = String -> [(a, String)] #

A parser for a type `a`, represented as a function that takes a `String` and returns a list of possible parses as `(a,`String`)` pairs.

Note that this kind of backtracking parser is very inefficient; reading a large structure may be quite slow (cf `ReadP`).

# Haskell 2010 functions

reads :: Read a => ReadS a #

equivalent to `readsPrec` with a precedence of 0.

read :: Read a => String -> a #

The `read` function reads input from a string, which must be completely consumed by the input process. `read` fails with an `error` if the parse is unsuccessful, and it is therefore discouraged from being used in real applications. Use `readMaybe` or `readEither` for safe alternatives.

    >>> read "123" :: Int
    123

    >>> read "hello" :: Int
    *** Exception: Prelude.read: no parse

readParen :: Bool -> ReadS a -> ReadS a #

``readParen` `True` p` parses what `p` parses, but surrounded with parentheses.

``readParen` `False` p` parses what `p` parses, but optionally surrounded with parentheses.

lex :: ReadS String #

The `lex` function reads a single lexeme from the input, discarding initial white space, and returning the characters that constitute the lexeme. If the input string contains only white space, `lex` returns a single successful `lexeme' consisting of the empty string. (Thus ``lex` "" = [("","")]`.) If there is no legal lexeme at the beginning of the input string, `lex` fails (i.e. returns `[]`).

This lexer is not completely faithful to the Haskell lexical syntax in the following respects:

- Qualified names are not handled properly
- Octal and hexadecimal numerics are not recognized as a single token
- Comments are not treated properly

# New parsing functions

module Text.ParserCombinators.ReadPrec

data Lexeme #

Constructors

Char Char | Character literal
String String | String literal, with escapes interpreted
Punc String | Punctuation or reserved symbol, e.g. `(`, `::`
Ident String | Haskell identifier, e.g. `foo`, `Baz`
Symbol String | Haskell symbol, e.g. `>>`, `:%`
Number Number | @since base-4.6.0.0
EOF |

#### Instances

Instances details

Read Lexeme | @since base-2.01
Instance details Defined in GHC.Internal.Read Methods readsPrec :: Int -> ReadS Lexeme # readList :: ReadS [Lexeme] # readPrec :: ReadPrec Lexeme # readListPrec :: ReadPrec [Lexeme] #
Show Lexeme | @since base-2.01
Instance details Defined in GHC.Internal.Text.Read.Lex Methods showsPrec :: Int -> Lexeme -> ShowS # show :: Lexeme -> String # showList :: [Lexeme] -> ShowS #
Eq Lexeme | @since base-2.01
Instance details Defined in GHC.Internal.Text.Read.Lex Methods (==) :: Lexeme -> Lexeme -> Bool # (/=) :: Lexeme -> Lexeme -> Bool #

lexP :: ReadPrec Lexeme #

Parse a single lexeme

parens :: ReadPrec a -> ReadPrec a #

`(parens p)` parses "P", "(P0)", "((P0))", etc, where `p` parses "P" in the current precedence context and parses "P0" in precedence context zero

readListDefault :: Read a => ReadS [a] #

A possible replacement definition for the `readList` method (GHC only). This is only needed for GHC, and even then only for `Read` instances where `readListPrec` isn't defined as `readListPrecDefault`.

readListPrecDefault :: Read a => ReadPrec [a] #

A possible replacement definition for the `readListPrec` method, defined using `readPrec` (GHC only).

readEither :: Read a => String -> Either String a #

Parse a string using the `Read` instance. Succeeds if there is exactly one valid result. A `Left` value indicates a parse error.

    >>> readEither "123" :: Either String Int
    Right 123

    >>> readEither "hello" :: Either String Int
    Left "Prelude.read: no parse"

@since base-4.6.0.0

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