Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- newtype Reflected1 f a = Reflected1 (f a)
- newtype Reflected2 f a b = Reflected2 (f a b)
- class Show1 (f :: TYPE LiftedRep -> TYPE LiftedRep) where
- class Read a where
- class Read1 (f :: Type -> Type) where
- liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
- liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
- liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (f a)
- liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [f a]
- data ReadPrec a
Documentation
newtype Reflected1 f a Source #
A newtype wrapper to derive
and Show1
f
from the following,
often derivable instance.Read1
f
instance Functor f instance Show a => Show (f a) instance Read a => Read (f a)
Unlike Reflected1
from AutoLift.Coercible module, this wrapper
requires Functor
instance too.
Example
Suppose you define a new type constructor Foo
, and
derived its Show
and Functor
instance.
>>>
:set -XDeriveFunctor
>>>
data Foo a = Foo [a] Int a deriving (Show, Functor)
The derived Show (Foo a)
instance is defined for all a
with Show a
instance.
instance Show a => Show (Foo a)
Reflected1
allows you to derive
instance from the above instance.Show1
Foo
>>>
:set -XStandaloneDeriving -XDerivingVia
>>>
deriving via (Reflected1 Foo) instance Show1 Foo
Let's try the derived Show1
instance, by showing Foo Bool
, where
True
is shown as yes
and False
as no
, instead of the normal Show Bool
instance.
>>>
import Text.Show (showListWith)
>>>
let yesno b = (++) (if b then "yes" else "no")
>>>
liftShowsPrec (const yesno) (showListWith yesno) 0 (Foo [True, False] 5 False) ""
"Foo [yes,no] 5 no"
Reflected1 (f a) |
Instances
newtype Reflected2 f a b Source #
A newtype wrapper to derive
and Show2
f
from the following,
often derivable instance.Read2
f
instance (Show a, Show b) => Show (f a b) instance (Read a, Read b) => Read (f a b)
Unlike Reflected2
from AutoLift.Coercible module, this wrapper
requires Bifunctor
instance too.
instance Bifunctor f
Example
Suppose you define a new type constructor Bar
, and
derived its Show
instance.
>>>
data Bar a b = Bar [(Int,a,b)] deriving Show
The derived Show (Bar a b)
instance is defined for all a
and b
with Show
instances.
instance (Show a, Show b) => Show (Bar a b)
By providing Bifunctor
instance, Reflected2
allows you to derive
instance
from the above instance.Show2
Bar
>>>
import Data.Bifunctor
>>>
:set -XStandaloneDeriving -XDeriveFunctor -XDerivingVia
>>>
deriving instance Functor (Bar a)
>>>
instance Bifunctor Bar where bimap f g (Bar content) = Bar [ (i, f a, g b) | (i,a,b) <- content ]
>>>
deriving via (Reflected2 Bar a) instance (Show a) => Show1 (Bar a)
>>>
deriving via (Reflected2 Bar) instance Show2 Bar
Let's try the derived Show2
instance by showing Bar Bool Char
, where
True
is shown as yes
and False
as no
, instead of the normal Show Bool
instance.
>>>
import Text.Show (showListWith)
>>>
let yesno b = (++) (if b then "yes" else "no")
>>>
liftShowsPrec2 (const yesno) (showListWith yesno) showsPrec showList 0 (Bar [(1, True, 'A'), (2, False, 'B')]) ""
"Bar [(1,yes,'A'),(2,no,'B')]"
Reflected2 (f a b) |
Instances
Reexports
class Show1 (f :: TYPE LiftedRep -> TYPE LiftedRep) where #
Lifting of the Show
class to unary type constructors.
Since: base-4.9.0.0
liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS #
showsPrec
function for an application of the type constructor
based on showsPrec
and showList
functions for the argument type.
Since: base-4.9.0.0
liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS #
Instances
Show1 Complex |
Since: base-4.16.0.0 |
Show1 Identity | Since: base-4.9.0.0 |
Show1 Down | Since: base-4.12.0.0 |
Show1 NonEmpty | Since: base-4.10.0.0 |
Show1 Maybe | Since: base-4.9.0.0 |
Show1 Solo | Since: base-4.15 |
Show1 [] | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
(forall a. Show a => Show (f a), forall xx yy. Coercible xx yy => Coercible (f xx) (f yy)) => Show1 (Reflected1 f) Source # | |
Defined in AutoLift.Coercible liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Reflected1 f a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Reflected1 f a] -> ShowS # | |
(forall a. Show a => Show (f a), Functor f) => Show1 (Reflected1 f) Source # | |
Defined in AutoLift.Functor liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Reflected1 f a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Reflected1 f a] -> ShowS # | |
Show a => Show1 (Either a) | Since: base-4.9.0.0 |
Show1 (Proxy :: TYPE LiftedRep -> Type) | Since: base-4.9.0.0 |
Show a => Show1 ((,) a) | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
(forall y. Show y => Show (f a y), forall x y. Coercible x y => Coercible (f a x) (f a y)) => Show1 (Reflected2 f a) Source # | |
Defined in AutoLift.Coercible liftShowsPrec :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> Int -> Reflected2 f a a0 -> ShowS # liftShowList :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> [Reflected2 f a a0] -> ShowS # | |
(forall y. Show y => Show (f a y), Functor (f a)) => Show1 (Reflected2 f a) Source # | |
Defined in AutoLift.Functor liftShowsPrec :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> Int -> Reflected2 f a a0 -> ShowS # liftShowList :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> [Reflected2 f a a0] -> ShowS # | |
Show a => Show1 (Const a :: TYPE LiftedRep -> Type) | Since: base-4.9.0.0 |
(Show a, Show b) => Show1 ((,,) a b) | Since: base-4.16.0.0 |
Defined in Data.Functor.Classes | |
(Show a, Show b, Show c) => Show1 ((,,,) a b c) | Since: base-4.16.0.0 |
Defined in Data.Functor.Classes |
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
Read Associativity | Since: base-4.6.0.0 |
Defined in GHC.Generics readsPrec :: Int -> ReadS Associativity # readList :: ReadS [Associativity] # | |
Read DecidedStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
Read Fixity | Since: base-4.6.0.0 |
Read SourceStrictness | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
Read SourceUnpackedness | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
Read ExitCode | |
Read SomeChar | |
Read SomeSymbol | Since: base-4.7.0.0 |
Defined in GHC.TypeLits readsPrec :: Int -> ReadS SomeSymbol # readList :: ReadS [SomeSymbol] # readPrec :: ReadPrec SomeSymbol # readListPrec :: ReadPrec [SomeSymbol] # | |
Read SomeNat | Since: base-4.7.0.0 |
Read GeneralCategory | Since: base-2.1 |
Defined in GHC.Read | |
Read Word16 | Since: base-2.1 |
Read Word32 | Since: base-2.1 |
Read Word64 | Since: base-2.1 |
Read Word8 | Since: base-2.1 |
Read Lexeme | Since: base-2.1 |
Read Ordering | Since: base-2.1 |
Read Integer | Since: base-2.1 |
Read Natural | Since: base-4.8.0.0 |
Read () | Since: base-2.1 |
Read Bool | Since: base-2.1 |
Read Char | Since: base-2.1 |
Read Double | Since: base-2.1 |
Read Float | Since: base-2.1 |
Read Int | Since: base-2.1 |
Read Word | Since: base-4.5.0.0 |
Read p => Read (Par1 p) | Since: base-4.7.0.0 |
(Integral a, Read a) => Read (Ratio a) | Since: base-2.1 |
Read a => Read (NonEmpty a) | Since: base-4.11.0.0 |
Read a => Read (Maybe a) | Since: base-2.1 |
Read a => Read (a) | Since: base-4.15 |
Read a => Read [a] | Since: base-2.1 |
Read (f a) => Read (Reflected1 f a) Source # | |
Defined in AutoLift.Coercible readsPrec :: Int -> ReadS (Reflected1 f a) # readList :: ReadS [Reflected1 f a] # readPrec :: ReadPrec (Reflected1 f a) # readListPrec :: ReadPrec [Reflected1 f a] # | |
Read (f a) => Read (Reflected1 f a) Source # | |
Defined in AutoLift.Functor readsPrec :: Int -> ReadS (Reflected1 f a) # readList :: ReadS [Reflected1 f a] # readPrec :: ReadPrec (Reflected1 f a) # readListPrec :: ReadPrec [Reflected1 f a] # | |
Reifies s (ReadDict a) => Read (AdHoc s a) Source # | |
(Read a, Read b) => Read (Either a b) | Since: base-3.0 |
Read (Proxy t) | Since: base-4.7.0.0 |
(Ix a, Read a, Read b) => Read (Array a b) | Since: base-2.1 |
Read (U1 p) | Since: base-4.9.0.0 |
Read (V1 p) | Since: base-4.9.0.0 |
(Read a, Read b) => Read (a, b) | Since: base-2.1 |
Read (f a b) => Read (Reflected2 f a b) Source # | |
Defined in AutoLift.Coercible readsPrec :: Int -> ReadS (Reflected2 f a b) # readList :: ReadS [Reflected2 f a b] # readPrec :: ReadPrec (Reflected2 f a b) # readListPrec :: ReadPrec [Reflected2 f a b] # | |
Read (f a b) => Read (Reflected2 f a b) Source # | |
Defined in AutoLift.Functor readsPrec :: Int -> ReadS (Reflected2 f a b) # readList :: ReadS [Reflected2 f a b] # readPrec :: ReadPrec (Reflected2 f a b) # readListPrec :: ReadPrec [Reflected2 f a b] # | |
Read a => Read (Const a b) | This instance would be equivalent to the derived instances of the
Since: base-4.8.0.0 |
Read (f p) => Read (Rec1 f p) | Since: base-4.7.0.0 |
(Read a, Read b, Read c) => Read (a, b, c) | Since: base-2.1 |
(Read (f p), Read (g p)) => Read ((f :*: g) p) | Since: base-4.7.0.0 |
(Read (f p), Read (g p)) => Read ((f :+: g) p) | Since: base-4.7.0.0 |
Read c => Read (K1 i c p) | Since: base-4.7.0.0 |
(Read a, Read b, Read c, Read d) => Read (a, b, c, d) | Since: base-2.1 |
Read (f (g p)) => Read ((f :.: g) p) | Since: base-4.7.0.0 |
Read (f p) => Read (M1 i c f p) | Since: base-4.7.0.0 |
(Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) | Since: base-2.1 |
(Read a, Read b, Read c, Read d, Read e, Read f) => Read (a, b, c, d, e, f) | Since: base-2.1 |
(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.1 |
(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.1 |
(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.1 |
(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.1 |
(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.1 |
(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.1 |
(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.1 |
(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.1 |
(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.1 |
Defined in GHC.Read |
class Read1 (f :: Type -> Type) 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
Instances
MonadFail ReadPrec | Since: base-4.9.0.0 |
Defined in Text.ParserCombinators.ReadPrec | |
Alternative ReadPrec | Since: base-4.6.0.0 |
Applicative ReadPrec | Since: base-4.6.0.0 |
Functor ReadPrec | Since: base-2.1 |
Monad ReadPrec | Since: base-2.1 |
MonadPlus ReadPrec | Since: base-2.1 |