A class for pretty-printing values in a configurable manner.
A basic example:
data Case = UpperCase | LowerCasedata D = Dinstance PrettyBy Case D where prettyBy UpperCase D = "D"; prettyBy LowerCase D = "d"prettyBy UpperCase DDprettyBy LowerCase Dd
The library provides instances for common types like Integer or Bool, so you can't define
your own PrettyBy SomeConfig Integer instance. And for the same reason you should not define
instances like PrettyBy SomeAnotherConfig a for universally quantified a, because such an
instance would overlap with the existing ones. Take for example
data ViaShow = ViaShowinstance Show a => PrettyBy ViaShow a where prettyBy ViaShow = pretty . show
with such an instance prettyBy ViaShow (1 :: Int) throws an error about overlapping instances:
• Overlapping instances for PrettyBy ViaShow Int
arising from a use of ‘prettyBy’
Matching instances:
instance PrettyDefaultBy config Int => PrettyBy config Int
instance [safe] Show a => PrettyBy ViaShow aThere's a newtype provided specifically for the purpose of defining a PrettyBy instance for
any a: PrettyAny. Read its docs for details on when you might want to use it.
The PrettyBy instance for common types is defined in a way that allows to override default pretty-printing behaviour, read the docs of HasPrettyDefaults for details.
Methods
prettyBy :: config -> a -> Doc annPretty-print a value of type
athe way aconfigspecifies it. The default implementation of prettyBy is in terms of pretty, defaultPrettyFunctorBy or defaultPrettyBifunctorBy depending on the kind of the data type that you're providing an instance for. For example, the default implementation of prettyBy for a monomorphic type is going to be "ignore the config and call pretty over the value":Example3 expressions newtype N = N Int deriving newtype (Pretty)instance PrettyBy () NprettyBy () (N 42)42
The default implementation of prettyBy for a Functor is going to be in terms of defaultPrettyFunctorBy:
Example3 expressions newtype N a = N a deriving stock (Functor) deriving newtype (Pretty)instance PrettyBy () a => PrettyBy () (N a)prettyBy () (N (42 :: Int))42
It's fine for the data type to have a phantom parameter as long as the data type is still a Functor (i.e. the parameter has to be of kind
Type). Then defaultPrettyFunctorBy is used again:Example3 expressions newtype N a = N Int deriving stock (Functor) deriving newtype (Pretty)instance PrettyBy () (N b)prettyBy () (N 42)42
If the data type has a single parameter of any other kind, then it's not a functor and so like in the monomorphic case pretty is used:
Example3 expressions newtype N (b :: Bool) = N Int deriving newtype (Pretty)instance PrettyBy () (N b)prettyBy () (N 42)42
Same applies to a data type with two parameters: if both the parameters are of kind
Type, then the data type is assumed to be a Bifunctor and hence defaultPrettyBifunctorBy is used. If the right parameter is of kindTypeand the left parameter is of any other kind, then we fallback to assuming the data type is a Functor and defining prettyBy as defaultPrettyFunctorBy. If both the parameters are not of kindType, we fallback to implementing prettyBy in terms of pretty like in the monomorphic case.Note that in all those cases a Pretty instance for the data type has to already exist, so that we can derive a PrettyBy one in terms of it. If it doesn't exist or if your data type is not supported (for example, if it has three or more parameters of kind
Type), then you'll need to provide the implementation manually.prettyListBy :: config -> [a] -> Doc annprettyListBy is used to define the default PrettyBy instance for
[a]andNonEmpty a. In normal circumstances only the prettyBy function is used. The default implementation of prettyListBy is in terms of defaultPrettyFunctorBy.
Instances29PrettyBy, …
PrettyDefaultBy config Void => PrettyBy config VoidDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () ([] :: [Void])[]
PrettyDefaultBy config Integer => PrettyBy config IntegerDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (2^(123 :: Int) :: Integer)10633823966279326983230456482242756608
PrettyDefaultBy config Natural => PrettyBy config NaturalDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (123 :: Natural)123
PrettyDefaultBy config Int16 => PrettyBy config Int16Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Int32 => PrettyBy config Int32Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Int64 => PrettyBy config Int64Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Int8 => PrettyBy config Int8Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Bool => PrettyBy config BoolDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () TrueTrue
PrettyDefaultBy config Char => PrettyBy config CharDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalBy default a String (i.e.
[Char]) is converted to aTextfirst and then pretty-printed. So make sure that if you have any non-default pretty-printing forCharorText, they're in sync.Example2 expressions prettyBy () 'a'aprettyBy () "abc"abc
PrettyDefaultBy config Double => PrettyBy config DoubleDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (pi :: Double)3.141592653589793
PrettyDefaultBy config Float => PrettyBy config FloatDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (pi :: Float)3.1415927
PrettyDefaultBy config Int => PrettyBy config IntDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (123 :: Int)123
PrettyDefaultBy config Word => PrettyBy config WordDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Word16 => PrettyBy config Word16Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Word32 => PrettyBy config Word32Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Word64 => PrettyBy config Word64Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Word8 => PrettyBy config Word8Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config Text => PrettyBy config TextDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalAutomatically converts all newlines to
line.Example1 expression prettyBy () ("hello\nworld" :: Strict.Text)helloworld
PrettyDefaultBy config Text => PrettyBy config TextDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalAn instance for lazy
Text. Identitical to the strict one.PrettyDefaultBy config () => PrettyBy config ()Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () ()()
The argument is not used:
Example1 expression prettyBy () (error "Strict?" :: ())()
Pretty a => PrettyBy config (IgnorePrettyConfig a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample4 expressions data Cfg = Cfgdata D = Dinstance Pretty D where pretty D = "D"prettyBy Cfg $ IgnorePrettyConfig DD
PrettyDefaultBy config (NonEmpty a) => PrettyBy config (NonEmpty a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalprettyBy for
NonEmpty ais defined in terms of prettyListBy by default.Example3 expressions prettyBy () (True :| [False])[True, False]prettyBy () ('a' :| "bc")abcprettyBy () (Just False :| [Nothing, Just True])[False, True]
PrettyDefaultBy config (Identity a) => PrettyBy config (Identity a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (Identity True)True
PrettyDefaultBy config (Maybe a) => PrettyBy config (Maybe a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalBy default a
[Maybe a]is converted to[a]first and only then pretty-printed.Example4 expressions braces $ prettyBy () (Just True){True}braces $ prettyBy () (Nothing :: Maybe Bool){}prettyBy () [Just False, Nothing, Just True][False, True]prettyBy () [Nothing, Just 'a', Just 'b', Nothing, Just 'c']abc
PrettyDefaultBy config [a] => PrettyBy config [a]Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalprettyBy for
[a]is defined in terms of prettyListBy by default.Example3 expressions prettyBy () [True, False][True, False]prettyBy () "abc"abcprettyBy () [Just False, Nothing, Just True][False, True]
PrettyDefaultBy config a => PrettyBy config (PrettyCommon a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalPrettyDefaultBy config (a, b) => PrettyBy config (a, b)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () (False, "abc")(False, abc)
PrettyDefaultBy config (Const a b) => PrettyBy config (Const a b)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalNon-polykinded, because
Pretty (Const a b)is not polykinded either.Example1 expression prettyBy () (Const 1 :: Const Integer Bool)1
PrettyDefaultBy config (a, b, c) => PrettyBy config (a, b, c)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.InternalExample1 expression prettyBy () ('a', "bcd", True)(a, bcd, True)