Copyright | (C) 2014-2017 Ryan Scott |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Ryan Scott |
Stability | Provisional |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
Generic versions of TextShow
and TextShow1
class functions, as an alternative to
TextShow.TH, which uses Template Haskell. Because there is no Generic2
class, TextShow2
cannot be implemented generically.
This implementation is loosely based off of the Generics.Deriving.Show
module
from the generic-deriving
library.
Since: 2
Synopsis
- newtype FromGeneric a = FromGeneric {
- fromGeneric :: a
- newtype FromGeneric1 f a = FromGeneric1 {
- fromGeneric1 :: f a
- genericShowt :: (Generic a, GTextShowT Zero (Rep a)) => a -> Text
- genericShowtl :: (Generic a, GTextShowTL Zero (Rep a)) => a -> Text
- genericShowtPrec :: (Generic a, GTextShowT Zero (Rep a)) => Int -> a -> Text
- genericShowtlPrec :: (Generic a, GTextShowTL Zero (Rep a)) => Int -> a -> Text
- genericShowtList :: (Generic a, GTextShowT Zero (Rep a)) => [a] -> Text
- genericShowtlList :: (Generic a, GTextShowTL Zero (Rep a)) => [a] -> Text
- genericShowb :: (Generic a, GTextShowB Zero (Rep a)) => a -> Builder
- genericShowbPrec :: (Generic a, GTextShowB Zero (Rep a)) => Int -> a -> Builder
- genericShowbList :: (Generic a, GTextShowB Zero (Rep a)) => [a] -> Builder
- genericPrintT :: (Generic a, GTextShowT Zero (Rep a)) => a -> IO ()
- genericPrintTL :: (Generic a, GTextShowTL Zero (Rep a)) => a -> IO ()
- genericHPrintT :: (Generic a, GTextShowT Zero (Rep a)) => Handle -> a -> IO ()
- genericHPrintTL :: (Generic a, GTextShowTL Zero (Rep a)) => Handle -> a -> IO ()
- genericLiftShowbPrec :: (Generic1 f, GTextShowB One (Rep1 f)) => (Int -> a -> Builder) -> ([a] -> Builder) -> Int -> f a -> Builder
- genericShowbPrec1 :: (Generic a, Generic1 f, GTextShowB Zero (Rep a), GTextShowB One (Rep1 f)) => Int -> f a -> Builder
- class GTextShowB arity f where
- gShowbPrec :: ShowFunsB arity a -> Int -> f a -> Builder
- class GTextShowConB arity f where
- gShowbPrecCon :: ConType -> ShowFunsB arity a -> Int -> f a -> Builder
- data ShowFunsB arity a where
- NoShowFunsB :: ShowFunsB Zero a
- Show1FunsB :: (Int -> a -> Builder) -> ([a] -> Builder) -> ShowFunsB One a
- class GTextShowT arity f where
- gShowtPrec :: ShowFunsT arity a -> Int -> f a -> Text
- class GTextShowConT arity f where
- gShowtPrecCon :: ConType -> ShowFunsT arity a -> Int -> f a -> Text
- data ShowFunsT arity a where
- NoShowFunsT :: ShowFunsT Zero a
- Show1FunsT :: (Int -> a -> Text) -> ([a] -> Text) -> ShowFunsT One a
- class GTextShowTL arity f where
- gShowtlPrec :: ShowFunsTL arity a -> Int -> f a -> Text
- class GTextShowConTL arity f where
- gShowtlPrecCon :: ConType -> ShowFunsTL arity a -> Int -> f a -> Text
- data ShowFunsTL arity a where
- NoShowFunsTL :: ShowFunsTL Zero a
- Show1FunsTL :: (Int -> a -> Text) -> ([a] -> Text) -> ShowFunsTL One a
- class IsNullary f where
- data ConType
- data Zero
- data One
Generic adapter newtypes
newtype FromGeneric a Source #
An adapter newtype, suitable for DerivingVia
.
The TextShow
instance for FromGeneric
leverages a Generic
-based
default. That is,
showbPrec
p (FromGeneric
x) =genericShowbPrec
p x
Since: 3.7.4
FromGeneric | |
|
Instances
newtype FromGeneric1 f a Source #
An adapter newtype, suitable for DerivingVia
.
The TextShow1
instance for FromGeneric1
leverages a Generic1
-based
default. That is,
liftShowbPrec
sp sl p (FromGeneric1
x) =genericLiftShowbPrec
sp sl p x
Since: 3.7.4
FromGeneric1 | |
|
Instances
Generic show
functions
TextShow
instances can be easily defined for data types that are Generic
instances.
If you are using GHC 8.6 or later, the easiest way to do this is to use the
DerivingVia
extension.
{-# LANGUAGE DeriveGeneric, DerivingVia #-} import GHC.Generics import TextShow import TextShow.Generic data D a = D a deriving (Generic
,Generic1
) derivingTextShow
viaFromGeneric
(D a) derivingTextShow1
viaFromGeneric1
D
Or, if you are using a version of GHC older than 8.6, one can alternatively define these instances like so:
instanceTextShow
a =>TextShow
(D a) whereshowbPrec
=genericShowbPrec
instanceTextShow1
D whereliftShowbPrec
=genericLiftShowbPrec
Understanding a compiler error
Suppose you intend to define a TextShow
instance via FromGeneric
:
data Oops = Oops derivingTextShow
viaFromGeneric
Oops -- forgot to add "deriving Generic" here!
If you forget to add a deriving
clause to your data type, at
compile-time, you might get an error message that begins roughly as follows:Generic
No instance for (GTextShowB
Zero
(Rep
Oops))
This error can be confusing, but don't let it intimidate you. The correct fix is
simply to add the missing "deriving
" clause.Generic
Similarly, if the compiler complains about not having an instance for (
, add a "GTextShowB
One
(Rep1
Oops1))deriving
" clause.Generic1
genericShowt :: (Generic a, GTextShowT Zero (Rep a)) => a -> Text Source #
genericShowtl :: (Generic a, GTextShowTL Zero (Rep a)) => a -> Text Source #
genericShowtPrec :: (Generic a, GTextShowT Zero (Rep a)) => Int -> a -> Text Source #
A Generic
implementation of showPrect
.
Since: 2
genericShowtlPrec :: (Generic a, GTextShowTL Zero (Rep a)) => Int -> a -> Text Source #
A Generic
implementation of showtlPrec
.
Since: 2
genericShowtList :: (Generic a, GTextShowT Zero (Rep a)) => [a] -> Text Source #
genericShowtlList :: (Generic a, GTextShowTL Zero (Rep a)) => [a] -> Text Source #
A Generic
implementation of showtlList
.
Since: 2
genericShowb :: (Generic a, GTextShowB Zero (Rep a)) => a -> Builder Source #
genericShowbPrec :: (Generic a, GTextShowB Zero (Rep a)) => Int -> a -> Builder Source #
genericShowbList :: (Generic a, GTextShowB Zero (Rep a)) => [a] -> Builder Source #
genericPrintT :: (Generic a, GTextShowT Zero (Rep a)) => a -> IO () Source #
A Generic
implementation of printT
.
Since: 2
genericPrintTL :: (Generic a, GTextShowTL Zero (Rep a)) => a -> IO () Source #
A Generic
implementation of printTL
.
Since: 2
genericHPrintT :: (Generic a, GTextShowT Zero (Rep a)) => Handle -> a -> IO () Source #
A Generic
implementation of hPrintT
.
Since: 2
genericHPrintTL :: (Generic a, GTextShowTL Zero (Rep a)) => Handle -> a -> IO () Source #
A Generic
implementation of hPrintTL
.
Since: 2
genericLiftShowbPrec :: (Generic1 f, GTextShowB One (Rep1 f)) => (Int -> a -> Builder) -> ([a] -> Builder) -> Int -> f a -> Builder Source #
A Generic1
implementation of genericLiftShowbPrec
.
Since: 2
genericShowbPrec1 :: (Generic a, Generic1 f, GTextShowB Zero (Rep a), GTextShowB One (Rep1 f)) => Int -> f a -> Builder Source #
Internals
Builder
class GTextShowB arity f where Source #
Class of generic representation types that can be converted to a Builder
. The arity
type variable indicates which type class is used.
indicates GTextShowB
Zero
TextShow
behavior, and
indicates GTextShowB
One
TextShow1
behavior. Since: 3.4
gShowbPrec :: ShowFunsB arity a -> Int -> f a -> Builder Source #
This is used as the default generic implementation of showbPrec
(if the arity
is Zero
) or liftShowbPrec
(if the arity
is One
).
Instances
GTextShowB arity (V1 :: Type -> Type) Source # | |
Defined in TextShow.Generic | |
(Constructor c, GTextShowConB arity f, IsNullary f) => GTextShowB arity (C1 c f) Source # | |
Defined in TextShow.Generic | |
(GTextShowB arity f, GTextShowB arity g) => GTextShowB arity (f :+: g) Source # | |
Defined in TextShow.Generic | |
GTextShowB arity f => GTextShowB arity (D1 d f) Source # | |
Defined in TextShow.Generic |
class GTextShowConB arity f where Source #
Class of generic representation types for which the ConType
has been determined. The arity
type variable indicates which type class is used.
indicates GTextShowConB
Zero
TextShow
behavior, and
indicates GTextShowConB
One
TextShow1
behavior.
Instances
data ShowFunsB arity a where Source #
A ShowFunsB
value either stores nothing (for TextShow
) or it stores the two function arguments that show occurrences of the type parameter (for TextShow1
). Since: 3.4
NoShowFunsB :: ShowFunsB Zero a | |
Show1FunsB :: (Int -> a -> Builder) -> ([a] -> Builder) -> ShowFunsB One a |
Strict Text
class GTextShowT arity f where Source #
Class of generic representation types that can be converted to a Text
. The arity
type variable indicates which type class is used.
indicates GTextShowT
Zero
TextShow
behavior, and
indicates GTextShowT
One
TextShow1
behavior. Since: 3.4
gShowtPrec :: ShowFunsT arity a -> Int -> f a -> Text Source #
This is used as the default generic implementation of showtPrec
(if the arity
is Zero
) or liftShowtPrec
(if the arity
is One
).
Instances
GTextShowT arity (V1 :: Type -> Type) Source # | |
Defined in TextShow.Generic | |
(Constructor c, GTextShowConT arity f, IsNullary f) => GTextShowT arity (C1 c f) Source # | |
Defined in TextShow.Generic | |
(GTextShowT arity f, GTextShowT arity g) => GTextShowT arity (f :+: g) Source # | |
Defined in TextShow.Generic | |
GTextShowT arity f => GTextShowT arity (D1 d f) Source # | |
Defined in TextShow.Generic |
class GTextShowConT arity f where Source #
Class of generic representation types for which the ConType
has been determined. The arity
type variable indicates which type class is used.
indicates GTextShowConT
Zero
TextShow
behavior, and
indicates GTextShowConT
One
TextShow1
behavior.
Instances
data ShowFunsT arity a where Source #
A ShowFunsT
value either stores nothing (for TextShow
) or it stores the two function arguments that show occurrences of the type parameter (for TextShow1
). Since: 3.4
NoShowFunsT :: ShowFunsT Zero a | |
Show1FunsT :: (Int -> a -> Text) -> ([a] -> Text) -> ShowFunsT One a |
Lazy Text
class GTextShowTL arity f where Source #
Class of generic representation types that can be converted to a Text
. The arity
type variable indicates which type class is used.
indicates GTextShowTL
Zero
TextShow
behavior, and
indicates GTextShowTL
One
TextShow1
behavior. Since: 3.4
gShowtlPrec :: ShowFunsTL arity a -> Int -> f a -> Text Source #
This is used as the default generic implementation of showtlPrec
(if the arity
is Zero
) or liftShowtlPrec
(if the arity
is One
).
Instances
GTextShowTL arity (V1 :: Type -> Type) Source # | |
Defined in TextShow.Generic gShowtlPrec :: ShowFunsTL arity a -> Int -> V1 a -> Text Source # | |
(Constructor c, GTextShowConTL arity f, IsNullary f) => GTextShowTL arity (C1 c f) Source # | |
Defined in TextShow.Generic gShowtlPrec :: ShowFunsTL arity a -> Int -> C1 c f a -> Text Source # | |
(GTextShowTL arity f, GTextShowTL arity g) => GTextShowTL arity (f :+: g) Source # | |
Defined in TextShow.Generic gShowtlPrec :: ShowFunsTL arity a -> Int -> (f :+: g) a -> Text Source # | |
GTextShowTL arity f => GTextShowTL arity (D1 d f) Source # | |
Defined in TextShow.Generic gShowtlPrec :: ShowFunsTL arity a -> Int -> D1 d f a -> Text Source # |
class GTextShowConTL arity f where Source #
Class of generic representation types for which the ConType
has been determined. The arity
type variable indicates which type class is used.
indicates GTextShowConTL
Zero
TextShow
behavior, and
indicates GTextShowConTL
One
TextShow1
behavior.
gShowtlPrecCon :: ConType -> ShowFunsTL arity a -> Int -> f a -> Text Source #
Instances
data ShowFunsTL arity a where Source #
A ShowFunsTL
value either stores nothing (for TextShow
) or it stores the two function arguments that show occurrences of the type parameter (for TextShow1
). Since: 3.4
NoShowFunsTL :: ShowFunsTL Zero a | |
Show1FunsTL :: (Int -> a -> Text) -> ([a] -> Text) -> ShowFunsTL One a |
Instances
Contravariant (ShowFunsTL arity) Source # | |
Defined in TextShow.Generic contramap :: (a -> b) -> ShowFunsTL arity b -> ShowFunsTL arity a # (>$) :: b -> ShowFunsTL arity b -> ShowFunsTL arity a # |
Other internals
class IsNullary f where Source #
Class of generic representation types that represent a constructor with zero or more fields.
Instances
IsNullary (UWord :: k -> Type) Source # | |
IsNullary (UInt :: k -> Type) Source # | |
IsNullary (UFloat :: k -> Type) Source # | |
IsNullary (UDouble :: k -> Type) Source # | |
IsNullary (UChar :: k -> Type) Source # | |
IsNullary (U1 :: k -> Type) Source # | |
IsNullary (Rec1 f :: k -> Type) Source # | |
IsNullary (f :*: g :: k -> Type) Source # | |
IsNullary f => IsNullary (S1 s f :: k -> Type) Source # | |
IsNullary (K1 i c :: k -> Type) Source # | |
IsNullary (f :.: g :: k -> Type) Source # | |
IsNullary Par1 Source # | |
Instances
Eq ConType Source # | |
Data ConType Source # | |
Defined in TextShow.Generic gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ConType -> c ConType # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c ConType # toConstr :: ConType -> Constr # dataTypeOf :: ConType -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c ConType) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c ConType) # gmapT :: (forall b. Data b => b -> b) -> ConType -> ConType # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ConType -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ConType -> r # gmapQ :: (forall d. Data d => d -> u) -> ConType -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ConType -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ConType -> m ConType # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ConType -> m ConType # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ConType -> m ConType # | |
Ord ConType Source # | |
Read ConType Source # | |
Show ConType Source # | |
Generic ConType Source # | |
TextShow ConType Source # | |
Defined in TextShow.Generic showbPrec :: Int -> ConType -> Builder Source # showb :: ConType -> Builder Source # showbList :: [ConType] -> Builder Source # showtPrec :: Int -> ConType -> Text Source # showt :: ConType -> Text Source # showtList :: [ConType] -> Text Source # showtlPrec :: Int -> ConType -> Text Source # showtl :: ConType -> Text Source # showtlList :: [ConType] -> Text Source # | |
Lift ConType Source # | |
type Rep ConType Source # | |
Defined in TextShow.Generic type Rep ConType = D1 ('MetaData "ConType" "TextShow.Generic" "text-show-3.9.7-3HqfCZ2EhfQ6YY909KzVGe" 'False) ((C1 ('MetaCons "Rec" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Tup" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "Pref" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Inf" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String)))) |
A type-level indicator that TextShow1
is being derived generically.
Since: 3.2
Instances
GTextShowConTL One Par1 Source # | |
Defined in TextShow.Generic gShowtlPrecCon :: ConType -> ShowFunsTL One a -> Int -> Par1 a -> Text Source # | |
GTextShowConT One Par1 Source # | |
Defined in TextShow.Generic | |
GTextShowConB One Par1 Source # | |
Defined in TextShow.Generic | |
TextShow1 f => GTextShowConTL One (Rec1 f) Source # | |
Defined in TextShow.Generic gShowtlPrecCon :: ConType -> ShowFunsTL One a -> Int -> Rec1 f a -> Text Source # | |
TextShow1 f => GTextShowConT One (Rec1 f) Source # | |
Defined in TextShow.Generic | |
TextShow1 f => GTextShowConB One (Rec1 f) Source # | |
Defined in TextShow.Generic | |
(TextShow1 f, GTextShowConTL One g) => GTextShowConTL One (f :.: g) Source # | |
Defined in TextShow.Generic gShowtlPrecCon :: ConType -> ShowFunsTL One a -> Int -> (f :.: g) a -> Text Source # | |
(TextShow1 f, GTextShowConT One g) => GTextShowConT One (f :.: g) Source # | |
Defined in TextShow.Generic | |
(TextShow1 f, GTextShowConB One g) => GTextShowConB One (f :.: g) Source # | |
Defined in TextShow.Generic |