The class
Out is the equivalent of
Show
It provides conversion of values to pretty printable Pretty.Doc's.
Minimal complete definition:
docPrec or
doc.
Derived instances of
Out have the following properties
- The result of docPrec 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
docPrec 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 than
d (associativity is ignored). Thus, if d is
0 then the result is never surrounded in parentheses; if
d is 11 it is always surrounded in parentheses,
unless it is an atomic expression.
- If the constructor is defined using record syntax, then
docPrec will produce the record-syntax form, with the fields
given in the same order as the original declaration.
For example, given the declarations
data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic)
The derived instance of
Out is equivalent to:
instance (Out a) => Out (Tree a) where
docPrec d (Leaf m) = Pretty.sep $ wrapParens (d > appPrec) $
text "Leaf" : [nest (constrLen + parenLen) (docPrec (appPrec+1) m)]
where appPrec = 10
constrLen = 5;
parenLen = if(d > appPrec) then 1 else 0
docPrec d (Node u v) = Pretty.sep $ wrapParens (d > appPrec) $
text "Node" :
nest (constrLen + parenLen) (docPrec (appPrec+1) u) :
[nest (constrLen + parenLen) (docPrec (appPrec+1) v)]
where appPrec = 10
constrLen = 5
parenLen = if(d > appPrec) then 1 else 0