# GenericPretty: A generic, derivable, haskell pretty printer.

[ bsd3, generics, library, pretty-printer, text ] [ Propose Tags ]
• GenericPretty is a haskell library that provides support for automatic derivation of pretty printing functions on user defined data types.

• The Pretty library http://www.haskell.org/ghc/docs/7.0.4/html/libraries/ghc-7.0.4/Pretty.html is used underneath, the work is done over Pretty.Doc types. The library MyPretty is also provided. This library is a thin wrapper around the Pretty library and implements only Style related features. These features are planned to be added to the Pretty library itself. When that happens MyPretty will become obsolete and will be replaced by Pretty.

• The output provided by the library functions is identical to that of Prelude.show, except it has extra whitespace.

• This package requires the use of the new GHC.Generics features: http://www.haskell.org/haskellwiki/Generics. These features are present in versions of GHC >= 7.2.

• The Generics used are based on those described in the paper "A Generic Deriving Mechanism for Haskell" - by Magalhaes, Dijkstra, Jeuring and Loh in Proceedings of the third ACM Haskell symposium on Haskell (Haskell'2010), pp. 37-48, ACM, 2010: http://dreixel.net/research/pdf/gdmh.pdf There are several changes from the original paper in the ghc implementation which are described here: http://www.haskell.org/haskellwiki/Generics#Changes_from_the_paper.

• This generics mechanism supports deriving for all haskell datatypes EXCEPT for constrained datatypes. That is to say, datatypes which have a context will fail.

For instance,

"data (Eq a) => Constr a = Constr a"

will fail because of the (Eq a) context.

• Instalation instructions

The package is installed in the same way as any other package. If needed, instructions are provided below.

1. Make sure you have a version of ghc >= 7.2 installed and that you can use the runhaskell command from the command line.

3. Unpack the file. If using a UNIX system, run

tar xzf GenericPretty-1.1.0.tar.gz

If on windows use your preffered unpacking utility(for instance, 7zip : http://www.7-zip.org/)

1. Move to the correct directory:

cd GenericPretty-1.0.1

1. Run the following haskell commands to install the library globally

• Basic example of usage

Here is a source file demonstrating the GenericPretty usage

import Text.PrettyPrint.GenericPretty

data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic)

instance (Out a) => Out (Tree a) where docPrec = genOut

tree :: Tree Int

tree = Node (Node (Leaf 333333333) (Leaf (-555555555))) (Leaf 777777777)

main = pp tree

• For the above program to run the -XDeriveGeneric flag needs to be set.

This can be done either directly at the command line, by compiling with "ghc -XDeriveGeneric" or in the source code by using the LANGUAGE pragma (it seems I can't demonstrate the LANGUAGE pragma since cabal hates special characters. An example however is provided in the README file included in the package). Alternatively, for more information on the LANGUAGE pragma see here: http://www.haskell.org/ghc/docs/7.0.4/html/users_guide/pragmas.html

Besides setting the flag, one must derive Generic for the desired datatype by typing "deriving (Generic)" and write an instance of Out defining docPrec as "docPrec = genOut". Then the pretty printing functions such as "pp" can be used on any data of that type.

## Modules

• Text
• PrettyPrint
• Text.PrettyPrint.GenericPretty
• Text.PrettyPrint.MyPretty

#### Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

• No Candidates
Versions [RSS] 0.1.0, 0.1.1, 0.1.2, 0.1.3, 1.0.0, 1.0.1, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5, 1.1.6, 1.1.7, 1.1.8, 1.1.9, 1.2.0, 1.2.1, 1.2.2 base (>=3 && <5), ghc (>=7.2), ghc-prim [details] BSD-3-Clause Razvan Ranca ranca.razvan@gmail.com Text, Generics, Pretty Printer https://github.com/HaggisMcMutton/GenericPretty head: git clone git@github.com:HaggisMcMutton/GenericPretty.git by RazvanRanca at 2011-08-11T21:26:06Z LTSHaskell:1.2.2, NixOS:1.2.2, Stackage:1.2.2 18 direct, 14 indirect [details] 20274 total (43 in the last 30 days) (no votes yet) [estimated by Bayesian average] λ λ λ Docs not available Last success reported on 2015-12-07

[back to package description]
*******************************************************************************
*     GenericPretty.
*     A Generic, Derivable, Haskell Pretty Printer
*******************************************************************************

========================== Description ========================================

GenericPretty is a haskell library that provides support for automatic
derivation of pretty printing functions on user defined data types.

The Pretty library [1] is used underneath, the work is done over "Doc" types.

The library "MyPretty" is also provided. This library is a thin wrapper around
the "Pretty" library and implements only "Style" related features.
These features are planned to be added to the Pretty library itself. When
that happens "MyPretty" will become obsolete and will be replaced by "Pretty".

The output provided by the library functions is identical to that of
Prelude.show, except it has extra whitespace.

This library requires the use of the new GHC.Generics features [2]
These features are present in versions of GHC >= 7.2.

The Generics used are based on those described in the paper "A Generic Deriving
Mechanism for Haskell" - by Magalh�es, Dijkstra, Jeuring and L�h in Proceedings
2010 [3].
There are however several changes between the mechanism described in
the paper and the one implemented in GHC [4].

This generics mechanism supports deriving for all haskell datatypes EXCEPT for
constrained datatypes.
That is to say, datatypes which have a context will fail. For instance,
"data (Eq a) => Constr a = Constr a" will fail because of the (Eq a) context.

============================== Basic Example ==================================

Here is a haskell source file, called 'SimpleTest.hs'
----------------------------------------------------
{-# LANGUAGE DeriveGeneric #-}

import Text.PrettyPrint.GenericPretty

data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic)

instance (Out a) => Out (Tree a) where
docPrec = genOut

tree1 :: Tree Int
tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
(Leaf 57575757))(Leaf (-14141414)))(Leaf 7777777))

main = pp tree1
------------------------------------------------
The flag DeriveGeneric must be given to GHC. This can be done as above,
in a 'LANGUAGE' pragma, or manually by compiling with 'ghc -XDeriveGeneric'.

As can be seen, to use the library one must simply import it, derive 'Generic'
on the custom data type, and write an instance of 'Out' using 'genOut'.

Then one can use the pretty printing functions, such as 'pp' and 'pretty'.

Compiling and running the file is simple and gives the following result.
-----------------------------
$ghc SimpleTest.hs$ SimpleTest

Node (Node (Leaf 333333) (Leaf (-555555)))
(Node (Node (Node (Leaf 888888) (Leaf 57575757)) (Leaf (-14141414)))
(Leaf 7777777))
---------------------------
If we replaced the main function with 'main = ppLen 30 tree1',

Node (Node (Leaf 333333)
(Leaf (-555555)))
(Node (Node (Node (Leaf 888888)
(Leaf 57575757))
(Leaf (-14141414)))
(Leaf 7777777))

In this case the output tries to remain under 30 characters/line, if possible,
while always maintaining correct indentation.

There also is a 'ppStyle' function which lets you further customize the output
by giving a 'Style' which consists of the line length, the number of ribbons
per line and the mode to us.

A ribbon length is the length of non-indentation text per line.
So if I used a line length of 80 and 2 ribbons per line than I would have a
maximum of 40 non-indentation characters on any line.

The mode tells 'Pretty' how to render the result. There are 4 options:
1. PageMode - the default rendering
2. ZigZagMode - zig-zag cuts
3. LeftMode - there is no indentation and no maximum line length
4. OneLineMode - everything is put on one line

The most interesting one is the ZigZagMode. Using the running example and adding:

--------------------------------------
zigStyle :: Style
zigStyle = Style {mode = ZigZagMode, lineLength = 30, ribbonsPerLine = 1.5}

main = ppStyle zigStyle tree1
--------------------------------------

We get the result:

Node (Node (Leaf 333333)

/////
(Leaf (-555555)))
(Node (Node (Node (Leaf 888888)

/////
(Leaf 57575757))
(Leaf (-14141414)))
(Leaf 7777777))

Notice that the "/" show us the direction in which the rows below have been moved
(left in this case) and the number of "/"s indicate the number of characters
that the rows were moved(in this case 5 characters to the left)

========================== Customization Example ==============================

Customizing the pretty printed results is also straightforward, as in the
following example called 'CustomTest.hs'
----------------------------
{-# LANGUAGE DeriveGeneric #-}

import Text.PrettyPrint.GenericPretty
import Text.PrettyPrint.MyPretty

data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic)

instance (Out a) => Out (Tree a) where
docPrec n (Leaf a) =  parens $text "customLeaf" <+> docPrec n a docPrec n (Node a b) = parens$ text "customNode" $$nest 1 (docPrec n a)$$ nest 1 (docPrec n b)

tree1 :: Tree Int
tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
(Leaf 57575757))(Leaf (-14141414)))(Leaf 7777777))

main = pp tree1
------------------------------
Here we import the library 'MyPretty' and use it directly to define docPrec.
The syntax is the one used in both the Pretty[1] and the HughesPJ [5] libraries
By running the above we get a tree with a minimum of indentation:

(customNode
(customNode
(customLeaf 333333)
(customLeaf -555555))
(customNode
(customNode
(customNode
(customLeaf 888888)
(customLeaf 57575757))
(customLeaf -14141414))
(customLeaf 7777777)))

-----------------------------------

========================= Further Info ========================================

The above 'Tree' examples can be found in 'TestSuite/SimpleTest.hs'
and 'TestSuite/CustomTest.hs'. More involved examples integrated
with QuickCheck can be found in 'TestSuite/Tests.hs'.

Further information can be found in the API [6] and in the source code itself.

============================ Contact ==========================================

[6] http://hackage.haskell.org/packages/archive/GenericPretty/1.0.1/doc/html/Text-PrettyPrint-GenericPretty.html