HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Moduleprettyprinter-configurable-1.36.0.0Haskell2010

Text.PrettyBy

The main module of the library.

  • 4 types
  • 3 classes
  • 5 values
classclass PrettyBy config a where
#

A class for pretty-printing values in a configurable manner.

A basic example:

Example5 expressions
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

Example2 expressions
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 a

There'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 ann

    Pretty-print a value of type a the way a config specifies 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 kind Type and 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 kind Type, 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 ann

    prettyListBy is used to define the default PrettyBy instance for [a] and NonEmpty 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.Internal
    Example1 expression
    prettyBy () ([] :: [Void])[]
  • PrettyDefaultBy config Integer => PrettyBy config IntegerDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 expression
    prettyBy () (2^(123 :: Int) :: Integer)10633823966279326983230456482242756608
  • PrettyDefaultBy config Natural => PrettyBy config NaturalDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 expression
    prettyBy () (123 :: Natural)123
  • PrettyDefaultBy config Int16 => PrettyBy config Int16Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Int32 => PrettyBy config Int32Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Int64 => PrettyBy config Int64Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Int8 => PrettyBy config Int8Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Bool => PrettyBy config BoolDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 expression
    prettyBy () TrueTrue
  • PrettyDefaultBy config Char => PrettyBy config CharDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal

    By default a String (i.e. [Char]) is converted to a Text first and then pretty-printed. So make sure that if you have any non-default pretty-printing for Char or Text, 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.Internal
    Example1 expression
    prettyBy () (pi :: Double)3.141592653589793
  • PrettyDefaultBy config Float => PrettyBy config FloatDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 expression
    prettyBy () (pi :: Float)3.1415927
  • PrettyDefaultBy config Int => PrettyBy config IntDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 expression
    prettyBy () (123 :: Int)123
  • PrettyDefaultBy config Word => PrettyBy config WordDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Word16 => PrettyBy config Word16Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Word32 => PrettyBy config Word32Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Word64 => PrettyBy config Word64Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Word8 => PrettyBy config Word8Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
  • PrettyDefaultBy config Text => PrettyBy config TextDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal

    Automatically 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.Internal

    An instance for lazy Text. Identitical to the strict one.

  • PrettyDefaultBy config () => PrettyBy config ()Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 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.Internal
    Example4 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.Internal

    prettyBy for NonEmpty a is 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.Internal
    Example1 expression
    prettyBy () (Identity True)True
  • PrettyDefaultBy config (Maybe a) => PrettyBy config (Maybe a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal

    By 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.Internal

    prettyBy 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.Internal
  • PrettyDefaultBy config (a, b) => PrettyBy config (a, b)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example1 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.Internal

    Non-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.Internal
    Example1 expression
    prettyBy () ('a', "bcd", True)(a, bcd, True)
newtypenewtype IgnorePrettyConfig a
#

A newtype wrapper around a whose point is to provide a PrettyBy config instance for anything that has a Pretty instance.

Instances1PrettyBy
  • Pretty a => PrettyBy config (IgnorePrettyConfig a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example4 expressions
    data Cfg = Cfgdata D = Dinstance Pretty D where pretty D = "D"prettyBy Cfg $ IgnorePrettyConfig DD
datadata AttachPrettyConfig config a
#

A config together with some value. The point is to provide a Pretty instance for anything that has a PrettyBy config instance.

Constructors

Instances1Pretty
  • PrettyBy config a => Pretty (AttachPrettyConfig config a)Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal
    Example4 expressions
    data Cfg = Cfgdata D = Dinstance PrettyBy Cfg D where prettyBy Cfg D = "D"pretty $ AttachPrettyConfig Cfg DD
newtypenewtype PrettyAny a
#

A newtype wrapper around a provided for the purporse of defining PrettyBy instances handling any a. For example you can wrap values with the PrettyAny constructor directly like in this last line of

Example3 expressions
data ViaShow = ViaShowinstance Show a => PrettyBy ViaShow (PrettyAny a) where prettyBy ViaShow = pretty . show . unPrettyAnyprettyBy ViaShow $ PrettyAny TrueTrue

or you can use the type to via-derive instances:

Example3 expressions
data D = D deriving stock (Show)deriving via PrettyAny D instance PrettyBy ViaShow DprettyBy ViaShow DD

One important use case is handling sum-type configs. For example having two configs you can define their sum and derive PrettyBy for the unified config in terms of its components:

Example4 expressions
data UpperCase = UpperCasedata LowerCase = LowerCasedata Case = CaseUpperCase UpperCase | CaseLowerCase LowerCaseinstance (PrettyBy UpperCase a, PrettyBy LowerCase a) => PrettyBy Case (PrettyAny a) where prettyBy (CaseUpperCase upper) = prettyBy upper . unPrettyAny; prettyBy (CaseLowerCase lower) = prettyBy lower . unPrettyAny

Then having a data type implementing both PrettyBy UpperCase and PrettyBy LowerCase you can derive PrettyBy Case for that data type:

Example6 expressions
data D = Dinstance PrettyBy UpperCase D where prettyBy UpperCase D = "D"instance PrettyBy LowerCase D where prettyBy LowerCase D = "d"deriving via PrettyAny D instance PrettyBy Case DprettyBy UpperCase DDprettyBy LowerCase Dd

Look into test/Universal.hs for an extended example.

Constructors

classclass NonDefaultPrettyBy config a where
#

A class for overriding default pretty-printing behavior for types having it. Read the docs of HasPrettyDefaults for how to use the class.

Methods

familytype family HasPrettyDefaults config :: Bool
#

Determines whether a pretty-printing config allows default pretty-printing for types that support it. I.e. it's possible to create a new config and get access to pretty-printing for all types supporting default pretty-printing just by providing the right type instance. Example:

Example3 expressions
data DefCfg = DefCfgtype instance HasPrettyDefaults DefCfg = 'TrueprettyBy DefCfg (['a', 'b', 'c'], (1 :: Int), Just True)(abc, 1, True)

The set of types supporting default pretty-printing is determined by the prettyprinter library: whatever there has a Pretty instance also supports default pretty-printing in this library and the behavior of pretty x and prettyBy config_with_defaults x must be identical when x is one of such types.

It is possible to override default pretty-printing. For this you need to specify that HasPrettyDefaults is 'False for your config and then define a NonDefaultPrettyBy config instance for each of the types supporting default pretty-printing that you want to pretty-print values of. Note that once HasPrettyDefaults is specified to be 'False, all defaults are lost for your config, so you can't override default pretty-printing for one type and keep the defaults for all the others. I.e. if you have

Example2 expressions
data NonDefCfg = NonDefCfgtype instance HasPrettyDefaults NonDefCfg = 'False

then you have no defaults available and an attempt to pretty-print a value of a type supporting default pretty-printing

prettyBy NonDefCfg True

results in a type error:

• No instance for (NonDefaultPrettyBy NonDef Bool)
     arising from a use of ‘prettyBy’

As the error suggests you need to provide a NonDefaultPrettyBy instance explicitly:

Example2 expressions
instance NonDefaultPrettyBy NonDefCfg Bool where nonDefaultPrettyBy _ b = if b then "t" else "f"prettyBy NonDefCfg Truet

It is also possible not to provide any implementation for nonDefaultPrettyBy, in which case it defaults to being the default pretty-printing for the given type. This can be useful to recover default pretty-printing for types pretty-printing of which you don't want to override:

Example2 expressions
instance NonDefaultPrettyBy NonDefCfg IntprettyBy NonDefCfg (42 :: Int)42

Look into test/NonDefault.hs for an extended example.

We could give the user more fine-grained control over what defaults to override instead of requiring to explicitly provide all the instances whenever there's a need to override any default behavior, but that would complicate the library even more, so we opted for not doing that at the moment.

Note that you can always override default behavior by wrapping a type in newtype and providing a PrettyBy config_name instance for that newtype.

Also note that if you want to extend the set of types supporting default pretty-printing it's not enough to provide a Pretty instance for your type (such logic is hardly expressible in present day Haskell). Read the docs of DefaultPrettyBy for how to extend the set of types supporting default pretty-printing.

Instances1HasPrettyDefaults
  • type HasPrettyDefaults () = 'TrueDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Internal

    prettyBy () works like pretty for types supporting default pretty-printing.

PrettyDefaultBy config a is the same thing as PrettyBy config a, when a supports default pretty-printing. Thus PrettyDefaultBy config a and PrettyBy config a are interchangeable constraints for such types, but the latter throws an annoying "this makes type inference for inner bindings fragile" warning, unlike the former. PrettyDefaultBy config a reads as "a supports default pretty-printing and can be pretty-printed via config in either default or non-default manner depending on whether config supports default pretty-printing".

classclass Render str where
#

A class for rendering Docs as string types.

Methods

Instances3Render
  • Render TextDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Default
  • Render TextDefined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Default
  • a ~ Char => Render [a]Defined in prettyprinter-configurable-1.36.0.0 · Text.PrettyBy.Default
valuedisplay :: (Pretty a, Render str) => a -> str
#

Pretty-print and render a value as a string type.

valuedisplayBy :: (PrettyBy config a, Render str) => config -> a -> str
#

Pretty-print and render a value as a string type in a configurable way.