The GenericPretty package
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.
data (Eq a) => Constr a = Constr a
will fail because of the (Eq a) context.
The package is installed in the same way as any other package. If needed, instructions are provided below.
0. Make sure you have a version of ghc >= 7.2 installed and that you can use the runhaskell command from the command line.
1. Download the file GenericPretty-1.1.3.tar.gz from this page.
2. Unpack the file. If using a UNIX system, run
tar xzf GenericPretty-1.1.3.tar.gz
If on windows use your preferred unpacking utility(for instance, 7zip : http://www.7-zip.org/)
3. Move to the correct directory:
4. Run the following haskell commands to install the library globally
runhaskell Setup configure
runhaskell Setup build
runhaskell Setup install
If something went wrong, you can check this page for more info, look at manual installation: http://www.haskell.org/haskellwiki/Cabal/How_to_install_a_Cabal_package
Basic example of usage
Here is a source file demonstrating the GenericPretty usage
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.
For more details about the above example as well as examples of customizing the pretty printing please check the README file included in the package. For more information about the library itself and what it exports check the API linked further down this page.
- No changelog available
|Versions||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|
|Dependencies||base (>=3 && <5), ghc (>=7.2), ghc-prim|
|Category||Text, Generics, Pretty Printer|
|Source repository||head: git clone firstname.lastname@example.org:HaggisMcMutton/GenericPretty.git|
|Upload date||Fri Aug 12 07:39:48 UTC 2011|
|Downloads||1928 total (216 in last 30 days)|
- GenericPretty-1.1.3.tar.gz [browse] (Cabal source package)
- Package description (included in the package)
For package maintainers and hackage trustees