Safe Haskell | None |
---|---|
Language | Haskell2010 |
PlutusCore.Pretty
Synopsis
- data Doc ann
- class Pretty a where
- pretty :: a -> Doc ann
- prettyList :: [a] -> Doc ann
- class PrettyBy config a where
- prettyBy :: config -> a -> Doc ann
- prettyListBy :: config -> [a] -> Doc ann
- newtype IgnorePrettyConfig a = IgnorePrettyConfig {
- unIgnorePrettyConfig :: a
- data AttachPrettyConfig config a = AttachPrettyConfig !config !a
- class Render str where
- display :: forall str a. (Pretty a, Render str) => a -> str
- displayBy :: forall str a config. (PrettyBy config a, Render str) => config -> a -> str
- prettyPlcDef :: PrettyPlc a => a -> Doc ann
- displayPlcDef :: (PrettyPlc a, Render str) => a -> str
- prettyPlcDebug :: PrettyPlc a => a -> Doc ann
- displayPlcDebug :: (PrettyPlc a, Render str) => a -> str
- data CondensedErrors
- newtype PrettyConfigPlcOptions = PrettyConfigPlcOptions {}
- data PrettyConfigPlcStrategy
- data PrettyConfigPlc = PrettyConfigPlc {}
- type PrettyPlc = PrettyBy PrettyConfigPlc
- defPrettyConfigPlcOptions :: PrettyConfigPlcOptions
- defPrettyConfigPlcClassic :: PrettyConfigPlcOptions -> PrettyConfigPlc
- debugPrettyConfigPlcClassic :: PrettyConfigPlcOptions -> PrettyConfigPlc
- defPrettyConfigPlcReadable :: PrettyConfigPlcOptions -> PrettyConfigPlc
- debugPrettyConfigPlcReadable :: PrettyConfigPlcOptions -> PrettyConfigPlc
- prettyPlcClassicDef :: PrettyPlc a => a -> Doc ann
- prettyPlcClassicDebug :: PrettyPlc a => a -> Doc ann
- prettyPlcReadableDef :: PrettyPlc a => a -> Doc ann
- prettyPlcReadableDebug :: PrettyPlc a => a -> Doc ann
- prettyPlcCondensedErrorBy :: PrettyPlc a => (PrettyConfigPlcOptions -> PrettyConfigPlc) -> a -> Doc ann
- displayPlcCondensedErrorClassic :: (PrettyPlc a, Render str) => a -> str
- newtype PrettyConfigName = PrettyConfigName {}
- class HasPrettyConfigName config where
- toPrettyConfigName :: config -> PrettyConfigName
- defPrettyConfigName :: PrettyConfigName
- debugPrettyConfigName :: PrettyConfigName
- data PrettyConfigClassic configName = PrettyConfigClassic {
- _pccConfigName :: configName
- _pccDisplayAnn :: Bool
- type PrettyClassicBy configName = PrettyBy (PrettyConfigClassic configName)
- type PrettyClassic = PrettyClassicBy PrettyConfigName
- consAnnIf :: Pretty ann => PrettyConfigClassic configName -> ann -> [Doc dann] -> [Doc dann]
- prettyClassicDef :: PrettyClassic a => a -> Doc ann
- prettyClassicDebug :: PrettyClassic a => a -> Doc ann
- data ShowKinds
- data PrettyConfigReadable configName = PrettyConfigReadable {
- _pcrConfigName :: configName
- _pcrRenderContext :: RenderContext
- _pcrShowKinds :: ShowKinds
- type PrettyReadableBy configName = PrettyBy (PrettyConfigReadable configName)
- type PrettyReadable = PrettyReadableBy PrettyConfigName
- topPrettyConfigReadable :: configName -> ShowKinds -> PrettyConfigReadable configName
- botPrettyConfigReadable :: configName -> ShowKinds -> PrettyConfigReadable configName
- prettyBytes :: ByteString -> Doc ann
- data ConstConfig = ConstConfig
- type PrettyConst = PrettyBy ConstConfig
- prettyConst :: PrettyConst a => a -> Doc ann
Basic types and functions
The abstract data type
represents pretty documents that have
been annotated with data of type Doc
annann
.
More specifically, a value of type
represents a non-empty set of
possible layouts of a document. The layout functions select one of these
possibilities, taking into account things like the width of the output
document.Doc
The annotation is an arbitrary piece of data associated with (part of) a document. Annotations may be used by the rendering backends in order to display output differently, such as
- color information (e.g. when rendering to the terminal)
- mouseover text (e.g. when rendering to rich HTML)
- whether to show something or not (to allow simple or detailed versions)
The simplest way to display a Doc
is via the Show
class.
>>>
putStrLn (show (vsep ["hello", "world"]))
hello world
Instances
Minimal complete definition
Methods
pretty :: a -> Doc ann Source #
>>>
pretty 1 <+> pretty "hello" <+> pretty 1.234
1 hello 1.234
prettyList :: [a] -> Doc ann Source #
is only used to define the prettyList
instance
. In normal circumstances only the Pretty
a => Pretty
[a]
function is used.pretty
>>>
prettyList [1, 23, 456]
[1, 23, 456]
Instances
class PrettyBy config a where Source #
A class for pretty-printing values in a configurable manner.
A basic example:
>>>
data Case = UpperCase | LowerCase
>>>
data D = D
>>>
instance PrettyBy Case D where prettyBy UpperCase D = "D"; prettyBy LowerCase D = "d"
>>>
prettyBy UpperCase D
D>>>
prettyBy LowerCase D
d
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 = ViaShow
>>>
instance 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.
Minimal complete definition
Nothing
Methods
prettyBy :: config -> a -> Doc ann Source #
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":
>>>
newtype N = N Int deriving newtype (Pretty)
>>>
instance PrettyBy () N
>>>
prettyBy () (N 42)
42
The default implementation of prettyBy
for a Functor
is going to be in terms of
defaultPrettyFunctorBy
:
>>>
newtype N a = N a deriving (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 *
). Then defaultPrettyFunctorBy
is used
again:
>>>
newtype N a = N Int deriving (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:
>>>
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 *
,
then the data type is assumed to be a Bifunctor
and hence defaultPrettyBifunctorBy
is
used. If the right parameter is of kind *
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 *
, 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 *
), then
you'll need to provide the implementation manually.
prettyListBy :: config -> [a] -> Doc ann Source #
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
.
Instances
newtype IgnorePrettyConfig a Source #
A newtype wrapper around a
whose point is to provide a PrettyBy config
instance
for anything that has a Pretty
instance.
Constructors
IgnorePrettyConfig | |
Fields
|
Instances
Pretty a => PrettyBy config (IgnorePrettyConfig a) |
|
Defined in Text.PrettyBy.Internal Methods prettyBy :: config -> IgnorePrettyConfig a -> Doc ann Source # prettyListBy :: config -> [IgnorePrettyConfig a] -> Doc ann Source # |
data AttachPrettyConfig config a Source #
A config together with some value. The point is to provide a Pretty
instance
for anything that has a PrettyBy config
instance.
Constructors
AttachPrettyConfig !config !a |
Instances
PrettyBy config a => Pretty (AttachPrettyConfig config a) |
|
Defined in Text.PrettyBy.Internal Methods pretty :: AttachPrettyConfig config a -> Doc ann Source # prettyList :: [AttachPrettyConfig config a] -> Doc ann Source # |
display :: forall str a. (Pretty a, Render str) => a -> str Source #
Pretty-print and render a value as a string type.
displayBy :: forall str a config. (PrettyBy config a, Render str) => config -> a -> str Source #
Pretty-print and render a value as a string type in a configurable way.
Defaults
prettyPlcDef :: PrettyPlc a => a -> Doc ann Source #
Pretty-print a value in the default mode using the classic view.
displayPlcDef :: (PrettyPlc a, Render str) => a -> str Source #
Render a value to String
in the default mode using the classic view.
prettyPlcDebug :: PrettyPlc a => a -> Doc ann Source #
Pretty-print a value in the debug mode using the classic view.
displayPlcDebug :: (PrettyPlc a, Render str) => a -> str Source #
Render a value to String
in the debug mode using the classic view.
Global configuration
data CondensedErrors Source #
Whether to pretty-print PLC errors in full or with some information omitted.
Constructors
CondensedErrorsYes | |
CondensedErrorsNo |
Instances
Eq CondensedErrors Source # | |
Defined in PlutusCore.Pretty.Plc Methods (==) :: CondensedErrors -> CondensedErrors -> Bool Source # (/=) :: CondensedErrors -> CondensedErrors -> Bool Source # | |
Show CondensedErrors Source # | |
Defined in PlutusCore.Pretty.Plc |
newtype PrettyConfigPlcOptions Source #
Options for pretty-printing PLC entities.
Constructors
PrettyConfigPlcOptions | |
Fields |
data PrettyConfigPlcStrategy Source #
Strategy for pretty-printing PLC entities.
Constructors
PrettyConfigPlcClassic (PrettyConfigClassic PrettyConfigName) | |
PrettyConfigPlcReadable (PrettyConfigReadable PrettyConfigName) |
Instances
HasPrettyConfigName PrettyConfigPlcStrategy Source # | |
Defined in PlutusCore.Pretty.Plc Methods toPrettyConfigName :: PrettyConfigPlcStrategy -> PrettyConfigName Source # | |
DefaultPrettyPlcStrategy a => PrettyBy PrettyConfigPlcStrategy (PrettyAny a) Source # | |
Defined in PlutusCore.Pretty.Plc Methods prettyBy :: PrettyConfigPlcStrategy -> PrettyAny a -> Doc ann Source # prettyListBy :: PrettyConfigPlcStrategy -> [PrettyAny a] -> Doc ann Source # |
data PrettyConfigPlc Source #
Global configuration used for pretty-printing PLC entities.
Constructors
PrettyConfigPlc | |
Instances
type PrettyPlc = PrettyBy PrettyConfigPlc Source #
The "pretty-printable PLC entity" constraint.
defPrettyConfigPlcOptions :: PrettyConfigPlcOptions Source #
The PrettyConfigPlcOptions
used by default:
print errors in full.
defPrettyConfigPlcClassic :: PrettyConfigPlcOptions -> PrettyConfigPlc Source #
The PrettyConfigPlc
used by default:
use the classic view and print neither Unique
s, nor name attachments.
debugPrettyConfigPlcClassic :: PrettyConfigPlcOptions -> PrettyConfigPlc Source #
The PrettyConfigPlc
used for debugging:
use the classic view and print Unique
s, but not name attachments.
defPrettyConfigPlcReadable :: PrettyConfigPlcOptions -> PrettyConfigPlc Source #
The PrettyConfigPlc
used by default and for readability:
use the refined view and print neither Unique
s, nor name attachments.
debugPrettyConfigPlcReadable :: PrettyConfigPlcOptions -> PrettyConfigPlc Source #
The PrettyConfigPlc
used for debugging and readability:
use the refined view and print Unique
s, but not name attachments.
Custom functions for PLC types.
prettyPlcClassicDef :: PrettyPlc a => a -> Doc ann Source #
Pretty-print a PLC value in the default mode using the classic view.
prettyPlcClassicDebug :: PrettyPlc a => a -> Doc ann Source #
Pretty-print a PLC value in the debug mode using the classic view.
prettyPlcReadableDef :: PrettyPlc a => a -> Doc ann Source #
Pretty-print a PLC value in the default mode using the readable view.
prettyPlcReadableDebug :: PrettyPlc a => a -> Doc ann Source #
Pretty-print a PLC value in the debug mode using the readable view.
prettyPlcCondensedErrorBy :: PrettyPlc a => (PrettyConfigPlcOptions -> PrettyConfigPlc) -> a -> Doc ann Source #
Pretty-print a PLC value using the condensed way (see CondensedErrors
)
of pretty-printing PLC errors (in case there are any).
displayPlcCondensedErrorClassic :: (PrettyPlc a, Render str) => a -> str Source #
Render an error to String
in the condensed manner using the classic view.
Names
newtype PrettyConfigName Source #
A config that determines how to pretty-print a PLC name.
Constructors
PrettyConfigName | |
Fields
|
class HasPrettyConfigName config where Source #
A class of configs from which a PrettyConfigName
can be extracted.
Methods
toPrettyConfigName :: config -> PrettyConfigName Source #
Instances
HasPrettyConfigName PrettyConfigPlc Source # | |
Defined in PlutusCore.Pretty.Plc Methods toPrettyConfigName :: PrettyConfigPlc -> PrettyConfigName Source # | |
HasPrettyConfigName PrettyConfigPlcStrategy Source # | |
Defined in PlutusCore.Pretty.Plc Methods toPrettyConfigName :: PrettyConfigPlcStrategy -> PrettyConfigName Source # | |
configName ~ PrettyConfigName => HasPrettyConfigName (PrettyConfigReadable configName) Source # | |
Defined in PlutusCore.Pretty.Readable Methods toPrettyConfigName :: PrettyConfigReadable configName -> PrettyConfigName Source # | |
configName ~ PrettyConfigName => HasPrettyConfigName (PrettyConfigClassic configName) Source # | |
Defined in PlutusCore.Pretty.Classic Methods toPrettyConfigName :: PrettyConfigClassic configName -> PrettyConfigName Source # |
defPrettyConfigName :: PrettyConfigName Source #
The PrettyConfigName
used by default: print neither Unique
s, nor name attachments.
debugPrettyConfigName :: PrettyConfigName Source #
The PrettyConfigName
used for debugging: print Unique
s, but not name attachments.
Classic view
data PrettyConfigClassic configName Source #
Configuration for the classic pretty-printing.
Constructors
PrettyConfigClassic | |
Fields
|
Instances
type PrettyClassicBy configName = PrettyBy (PrettyConfigClassic configName) Source #
The "classically pretty-printable" constraint.
consAnnIf :: Pretty ann => PrettyConfigClassic configName -> ann -> [Doc dann] -> [Doc dann] Source #
Add a pretty-printed annotation to a list of Doc
s if the given config enables pretty-printing
of annotations.
prettyClassicDef :: PrettyClassic a => a -> Doc ann Source #
Pretty-print a value in the default mode using the classic view.
prettyClassicDebug :: PrettyClassic a => a -> Doc ann Source #
Pretty-print a value in the debug mode using the classic view.
Readable view
Constructors
ShowKindsYes | |
ShowKindsNo |
data PrettyConfigReadable configName Source #
Configuration for the readable pretty-printing.
Constructors
PrettyConfigReadable | |
Fields
|
Instances
type PrettyReadableBy configName = PrettyBy (PrettyConfigReadable configName) Source #
The "readably pretty-printable" constraint.
topPrettyConfigReadable :: configName -> ShowKinds -> PrettyConfigReadable configName Source #
A PrettyConfigReadable
with the fixity specified to topFixity
.
botPrettyConfigReadable :: configName -> ShowKinds -> PrettyConfigReadable configName Source #
A PrettyConfigReadable
with the fixity specified to botFixity
.
Utils
prettyBytes :: ByteString -> Doc ann Source #
data ConstConfig Source #
Constructors
ConstConfig |
Instances
type PrettyConst = PrettyBy ConstConfig Source #
prettyConst :: PrettyConst a => a -> Doc ann Source #