Safe Haskell | Safe |
---|---|
Language | Haskell98 |
GenericPretty is a Haskell library that supports automatic derivation of pretty printing functions on user defined data types.
The output provided is a pretty printed version of that provided by
show
. That is, rendering the document provided by this pretty
printer yields an output identical to that of show
, except
for extra whitespace.
For examples of usage please see the README file included in the package.
For more information see the HackageDB project page: http://hackage.haskell.org/package/GenericPretty
- class Out a where
- pp :: Out a => a -> IO ()
- ppLen :: Out a => Int -> a -> IO ()
- ppStyle :: Out a => Style -> a -> IO ()
- pretty :: Out a => a -> String
- prettyLen :: Out a => Int -> a -> String
- prettyStyle :: Out a => Style -> a -> String
- fullPP :: Out a => (TextDetails -> b -> b) -> b -> Style -> a -> b
- class Generic a
- outputIO :: TextDetails -> IO () -> IO ()
- outputStr :: TextDetails -> String -> String
Documentation
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 thand
(associativity is ignored). Thus, ifd
is0
then the result is never surrounded in parentheses; ifd
is11
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
:: Int | the operator precedence of the enclosing
context (a number from |
-> a | the value to be converted to a |
-> Doc | the resulting Doc |
:: (Generic a, GOut (Rep a)) | |
=> Int | the operator precedence of the enclosing
context (a number from |
-> a | the value to be converted to a |
-> Doc | the resulting Doc |
This is a specialised variant of docPrec
, using precedence context zero.
doc :: (Generic a, GOut (Rep a)) => a -> Doc Source #
This is a specialised variant of docPrec
, using precedence context zero.
docList :: [a] -> Doc Source #
docList
is the equivalent of showList
.
The method docList
is provided to allow the programmer to
give a specialised way of showing lists of values.
For example, this is used by the predefined Out
instance of
the Char
type, where values of type String
should be shown
in double quotes, rather than between square brackets.
Out Bool Source # | |
Out Char Source # | |
Out Double Source # | |
Out Float Source # | |
Out Int Source # | |
Out Integer Source # | |
Out Rational Source # | |
Out () Source # | |
Out a => Out [a] Source # | |
Out a => Out (Maybe a) Source # | |
(Out a, Out b) => Out (Either a b) Source # | |
(Out a, Out b) => Out (a, b) Source # | |
(Out a, Out b, Out c) => Out (a, b, c) Source # | |
(Out a, Out b, Out c, Out d) => Out (a, b, c, d) Source # | |
(Out a, Out b, Out c, Out d, Out e) => Out (a, b, c, d, e) Source # | |
(Out a, Out b, Out c, Out d, Out e, Out f) => Out (a, b, c, d, e, f) Source # | |
(Out a, Out b, Out c, Out d, Out e, Out f, Out g) => Out (a, b, c, d, e, f, g) Source # | |
pp :: Out a => a -> IO () Source #
The default Pretty Printer,
Equivalent to:
ppStyle defaultStyle
Where defaultStyle = (mode=PageMode, lineLength=80, ribbonsPerLine=1.5)
ppLen :: Out a => Int -> a -> IO () Source #
Semi-customizable pretty printer.
Equivalent to:
ppStyle customStyle
Where customStyle uses the specified line length, mode = PageMode and ribbonsPerLine = 1.
pretty :: Out a => a -> String Source #
The default pretty printer returning String
s
Equivalent to
prettyStyle defaultStyle
Where defaultStyle = (mode=PageMode, lineLength=80, ribbonsPerLine=1.5)
prettyLen :: Out a => Int -> a -> String Source #
Semi-customizable pretty printer.
Equivalent to:
prettyStyle customStyle
Where customStyle uses the specified line length, mode = PageMode and ribbonsPerLine = 1.
:: Out a | |
=> (TextDetails -> b -> b) | Function that handles the text conversion (eg: |
-> b | The end element of the result ( eg: "" or putChar('\n') ) |
-> Style | The pretty printing |
-> a | The value to pretty print |
-> b | The pretty printed result |
Representable types of kind *. This class is derivable in GHC with the DeriveGeneric flag on.
Generic Bool | |
Generic Ordering | |
Generic () | |
Generic Fixity | |
Generic Associativity | |
Generic SourceUnpackedness | |
Generic SourceStrictness | |
Generic DecidedStrictness | |
Generic Doc | |
Generic TextDetails | |
Generic Style | |
Generic Mode | |
Generic [a] | |
Generic (Maybe a) | |
Generic (Par1 p) | |
Generic (Doc a) | |
Generic (Either a b) | |
Generic (V1 k p) | |
Generic (U1 k p) | |
Generic (a, b) | |
Generic (Proxy k t) | |
Generic (Rec1 k f p) | |
Generic (URec k (Ptr ()) p) | |
Generic (URec k Char p) | |
Generic (URec k Double p) | |
Generic (URec k Float p) | |
Generic (URec k Int p) | |
Generic (URec k Word p) | |
Generic (a, b, c) | |
Generic (K1 k i c p) | |
Generic ((:+:) k f g p) | |
Generic ((:*:) k f g p) | |
Generic (a, b, c, d) | |
Generic (M1 k i c f p) | |
Generic ((:.:) k2 k1 f g p) | |
Generic (a, b, c, d, e) | |
Generic (a, b, c, d, e, f) | |
Generic (a, b, c, d, e, f, g) | |