Copyright | (c) Erich Gut |
---|---|
License | BSD3 |
Maintainer | zerich.gut@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
showing data with some auxiliary functions.
Synopsis
Show
Conversion of values to readable String
s.
Derived instances of Show
have the following properties, which
are compatible with derived instances of Read
:
- The result of
show
is a syntactically correct Haskell expression containing only constants, given the fixity declarations in force at the point where the type is declared. It contains only the constructor names defined in the data type, parentheses, and spaces. When labelled constructor fields are used, braces, commas, field names, and equal signs are also used. - If the constructor is defined to be an infix operator, then
showsPrec
will produce infix applications of the constructor. - the representation will be enclosed in parentheses if the
precedence of the top-level constructor in
x
is less thand
(associativity is ignored). Thus, ifd
is0
then the result is never surrounded in parentheses; ifd
is11
it is always surrounded in parentheses, unless it is an atomic expression. - If the constructor is defined using record syntax, then
show
will produce the record-syntax form, with the fields given in the same order as the original declaration.
For example, given the declarations
infixr 5 :^: data Tree a = Leaf a | Tree a :^: Tree a
the derived instance of Show
is equivalent to
instance (Show a) => Show (Tree a) where showsPrec d (Leaf m) = showParen (d > app_prec) $ showString "Leaf " . showsPrec (app_prec+1) m where app_prec = 10 showsPrec d (u :^: v) = showParen (d > up_prec) $ showsPrec (up_prec+1) u . showString " :^: " . showsPrec (up_prec+1) v where up_prec = 5
Note that right-associativity of :^:
is ignored. For example,
produces the stringshow
(Leaf 1 :^: Leaf 2 :^: Leaf 3)"Leaf 1 :^: (Leaf 2 :^: Leaf 3)"
.
:: Int | the operator precedence of the enclosing
context (a number from |
-> a | the value to be converted to a |
-> ShowS |
Convert a value to a readable String
.
showsPrec
should satisfy the law
showsPrec d x r ++ s == showsPrec d x (r ++ s)
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.
Instances
tween :: a -> [a] -> [a] Source #
inserting the given value in between the elements of the given list.
Examples
>>>
tween ',' "12345"
"1,2,3,4,5"
>>>
tween ',' ""
""
>>>
tween ',' "1"
"1"
jtween :: [a] -> [[a]] -> [a] Source #
inserting the given list in between the elements of the given list and joining the result.
Example
>>>
jtween ";" ["abcd","efg"]
"abcd;efg"
showable for one parameterized types.
Nothing
Instances
showable for two parameterized types.
Note We use this class mearly in the context of Path
.
Nothing
Instances
Show2 Empty2 Source # | |
Show2 GLApp Source # | |
Show2 TrApp Source # | |
Show2 (Homomorphous m) Source # | |
Defined in OAlg.Category.Definition show2 :: Homomorphous m a b -> String Source # | |
Show2 m => Show2 (Path m) Source # | |
Show2 h => Show2 (Op2 h) Source # | |
Semiring r => Show2 (HomSymbol r) Source # | |
Show2 (SliceFactorDrop s) Source # | |
Defined in OAlg.Entity.Slice.Definition show2 :: SliceFactorDrop s a b -> String Source # | |
Show2 (HomOp s) Source # | |
Show2 (IdHom s) Source # | |
Show2 (IsoOp s) Source # | |
Show2 h => Show2 (OpHom h) Source # | |
Show2 m => Show2 (Forget t m) Source # | |
(Show2 f, Show2 g) => Show2 (Either2 f g) Source # | |
Show2 (SliceCokernelKernel i c) Source # | |
Defined in OAlg.Entity.Slice.Adjunction show2 :: SliceCokernelKernel i c a b -> String Source # | |
Show2 (IsoOpMap f s) Source # | |
Show2 (OpMap f s) Source # | |
The character type Char
is an enumeration whose values represent
Unicode (or equivalently ISO/IEC 10646) code points (i.e. characters, see
http://www.unicode.org/ for details). This set extends the ISO 8859-1
(Latin-1) character set (the first 256 characters), which is itself an extension
of the ASCII character set (the first 128 characters). A character literal in
Haskell has type Char
.
To convert a Char
to or from the corresponding Int
value defined
by Unicode, use toEnum
and fromEnum
from the
Enum
class respectively (or equivalently ord
and
chr
).
Instances
Bounded Char | Since: base-2.1 |
Enum Char | Since: base-2.1 |
Ix Char | Since: base-2.1 |
Read Char | Since: base-2.1 |
Show Char | Since: base-2.1 |
NFData Char | |
Defined in Control.DeepSeq | |
Eq Char | |
Ord Char | |
Verbose Char Source # | |
Validable Char Source # | |
Entity Char Source # | |
Defined in OAlg.Entity.Definition | |
Random Char | |
Uniform Char | |
Defined in System.Random.Internal uniformM :: StatefulGen g m => g -> m Char # | |
UniformRange Char | |
Defined in System.Random.Internal | |
ErrorList Char | |
Defined in Control.Monad.Trans.Error | |
Lift Char | |
Generic1 (URec Char :: k -> Type) | |
Foldable (UChar :: TYPE LiftedRep -> Type) | Since: base-4.9.0.0 |
Defined in Data.Foldable fold :: Monoid m => UChar m -> m # foldMap :: Monoid m => (a -> m) -> UChar a -> m # foldMap' :: Monoid m => (a -> m) -> UChar a -> m # foldr :: (a -> b -> b) -> b -> UChar a -> b # foldr' :: (a -> b -> b) -> b -> UChar a -> b # foldl :: (b -> a -> b) -> b -> UChar a -> b # foldl' :: (b -> a -> b) -> b -> UChar a -> b # foldr1 :: (a -> a -> a) -> UChar a -> a # foldl1 :: (a -> a -> a) -> UChar a -> a # elem :: Eq a => a -> UChar a -> Bool # maximum :: Ord a => UChar a -> a # minimum :: Ord a => UChar a -> a # | |
Traversable (UChar :: Type -> Type) | Since: base-4.9.0.0 |
Functor (URec Char :: TYPE LiftedRep -> Type) | Since: base-4.9.0.0 |
Generic (URec Char p) | |
Show (URec Char p) | Since: base-4.9.0.0 |
Eq (URec Char p) | Since: base-4.9.0.0 |
Ord (URec Char p) | Since: base-4.9.0.0 |
data URec Char (p :: k) | Used for marking occurrences of Since: base-4.9.0.0 |
type Compare (a :: Char) (b :: Char) | |
Defined in Data.Type.Ord | |
type Rep1 (URec Char :: k -> Type) | Since: base-4.9.0.0 |
Defined in GHC.Generics | |
type Rep (URec Char p) | Since: base-4.9.0.0 |
Defined in GHC.Generics |
Read
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
Instances
Read All | Since: base-2.1 |
Read Any | Since: base-2.1 |
Read Version | Since: base-2.1 |
Read Void | Reading a Since: base-4.8.0.0 |
Read CBool | |
Read CChar | |
Read CClock | |
Read CDouble | |
Read CFloat | |
Read CInt | |
Read CIntMax | |
Read CIntPtr | |
Read CLLong | |
Read CLong | |
Read CPtrdiff | |
Read CSChar | |
Read CSUSeconds | |
Defined in Foreign.C.Types readsPrec :: Int -> ReadS CSUSeconds # readList :: ReadS [CSUSeconds] # readPrec :: ReadPrec CSUSeconds # readListPrec :: ReadPrec [CSUSeconds] # | |
Read CShort | |
Read CSigAtomic | |
Defined in Foreign.C.Types readsPrec :: Int -> ReadS CSigAtomic # readList :: ReadS [CSigAtomic] # readPrec :: ReadPrec CSigAtomic # readListPrec :: ReadPrec [CSigAtomic] # | |
Read CSize | |
Read CTime | |
Read CUChar | |
Read CUInt | |
Read CUIntMax | |
Read CUIntPtr | |
Read CULLong | |
Read CULong | |
Read CUSeconds | |
Read CUShort | |
Read CWchar | |
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 SeekMode | Since: base-4.2.0.0 |
Read ExitCode | |
Read BufferMode | Since: base-4.2.0.0 |
Defined in GHC.IO.Handle.Types readsPrec :: Int -> ReadS BufferMode # readList :: ReadS [BufferMode] # readPrec :: ReadPrec BufferMode # readListPrec :: ReadPrec [BufferMode] # | |
Read Newline | Since: base-4.3.0.0 |
Read NewlineMode | Since: base-4.3.0.0 |
Defined in GHC.IO.Handle.Types readsPrec :: Int -> ReadS NewlineMode # readList :: ReadS [NewlineMode] # readPrec :: ReadPrec NewlineMode # readListPrec :: ReadPrec [NewlineMode] # | |
Read IOMode | Since: base-4.2.0.0 |
Read Int16 | Since: base-2.1 |
Read Int32 | Since: base-2.1 |
Read Int64 | Since: base-2.1 |
Read Int8 | Since: base-2.1 |
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 ByteString | |
Defined in Data.ByteString.Internal readsPrec :: Int -> ReadS ByteString # readList :: ReadS [ByteString] # readPrec :: ReadPrec ByteString # readListPrec :: ReadPrec [ByteString] # | |
Read ByteString | |
Defined in Data.ByteString.Lazy.Internal readsPrec :: Int -> ReadS ByteString # readList :: ReadS [ByteString] # readPrec :: ReadPrec ByteString # readListPrec :: ReadPrec [ByteString] # | |
Read ShortByteString | |
Defined in Data.ByteString.Short.Internal | |
Read Ordering | Since: base-2.1 |
Read Stochastic Source # | |
Defined in OAlg.Control.Validate readsPrec :: Int -> ReadS Stochastic # readList :: ReadS [Stochastic] # readPrec :: ReadPrec Stochastic # readListPrec :: ReadPrec [Stochastic] # | |
Read RdcState Source # | |
Read Symbol Source # | |
Read DayOfWeek | |
Read DiffTime | |
Read NominalDiffTime | |
Defined in Data.Time.Clock.Internal.NominalDiffTime | |
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 a => Read (ZipList a) | Since: base-4.7.0.0 |
Read a => Read (Complex a) | Since: base-2.1 |
Read a => Read (Identity a) | This instance would be equivalent to the derived instances of the
Since: base-4.8.0.0 |
Read a => Read (First a) | Since: base-2.1 |
Read a => Read (Last a) | Since: base-2.1 |
Read a => Read (Down a) | This instance would be equivalent to the derived instances of the
Since: base-4.7.0.0 |
Read a => Read (First a) | Since: base-4.9.0.0 |
Read a => Read (Last a) | Since: base-4.9.0.0 |
Read a => Read (Max a) | Since: base-4.9.0.0 |
Read a => Read (Min a) | Since: base-4.9.0.0 |
Read m => Read (WrappedMonoid m) | Since: base-4.9.0.0 |
Defined in Data.Semigroup readsPrec :: Int -> ReadS (WrappedMonoid m) # readList :: ReadS [WrappedMonoid m] # readPrec :: ReadPrec (WrappedMonoid m) # readListPrec :: ReadPrec [WrappedMonoid m] # | |
Read a => Read (Dual a) | Since: base-2.1 |
Read a => Read (Product a) | Since: base-2.1 |
Read a => Read (Sum a) | Since: base-2.1 |
Read p => Read (Par1 p) | Since: base-4.7.0.0 |
(Integral a, Read a) => Read (Ratio a) | Since: base-2.1 |
Read x => Read (Id x) Source # | |
Read x => Read (Op x) Source # | |
Read x => Read (Closer x) Source # | |
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 a, Read b) => Read (Either a b) | Since: base-3.0 |
HasResolution a => Read (Fixed a) | Since: base-4.3.0.0 |
Read (Proxy t) | Since: base-4.7.0.0 |
(Read a, Read b) => Read (Arg a b) | Since: base-4.9.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 a => Read (Const a b) | This instance would be equivalent to the derived instances of the
Since: base-4.8.0.0 |
Read (f a) => Read (Ap f a) | Since: base-4.12.0.0 |
Read (f a) => Read (Alt f a) | Since: base-4.8.0.0 |
a ~ b => Read (a :~: b) | Since: base-4.7.0.0 |
Read (f p) => Read (Rec1 f p) | Since: base-4.7.0.0 |
(Read e, Read1 m, Read a) => Read (ErrorT e m a) | |
(Read a, Read b, Read c) => Read (a, b, c) | Since: base-2.1 |
(Read1 f, Read1 g, Read a) => Read (Product f g a) | Since: base-4.9.0.0 |
(Read1 f, Read1 g, Read a) => Read (Sum f g a) | Since: base-4.9.0.0 |
a ~~ b => Read (a :~~: b) | Since: base-4.10.0.0 |
(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 |
(Read1 f, Read1 g, Read a) => Read (Compose f g a) | Since: base-4.9.0.0 |
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 |
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