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 Outputable library is used underneath, the work is done over SDoc types.

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

This requires the use of the new GHC.Generics features: http://www.haskell.org/haskellwiki/Generics. These seem to be present in the GHC HEAD development snapshots >= 7.1.20110601.

For more info and examples of usage please see the README file and the API at http://haggismcmutton.github.com/GenericPretty/


[Skip to Readme]

Modules

[Index]

Downloads

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
Dependencies base (>=3 && <5), ghc (>=7.1.20110601), ghc-prim [details]
License BSD-3-Clause
Author Razvan Ranca
Maintainer ranca.razvan@gmail.com
Category Text, Generics, Pretty Printer
Home page https://github.com/HaggisMcMutton/GenericPretty
Source repo head: git clone git@github.com:HaggisMcMutton/GenericPretty.git
Uploaded by RazvanRanca at 2011-07-11T23:10:17Z
Distributions LTSHaskell:1.2.2, NixOS:1.2.2, Stackage:1.2.2
Reverse Dependencies 18 direct, 14 indirect [details]
Downloads 20709 total (63 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for GenericPretty-0.1.3

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

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

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

I find examples are the best aid in understanding. So, here is a possible 
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
	out = 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 'prettyP'.

Compiling and running the file is simple and gives the following result.
-----------------------------
$ ghc SimpleTest.hs
[1 of 1] Compiling Main             ( SimpleTest.hs, SimpleTest.o )
Linking SimpleTest.exe ...

$ 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 = prettyP 30 1 tree1', 
the result would instead be:

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 maintaining correct indentation.

------Note---------
The defined pretty printing functions work with any type that implements
'Out' OR 'Outputable'. 
This is so that if the data type that is using the deriving mechanism
relies on some other data type that can't, if the inner type implements 
'Outputable', then the deriving still works.
-------------------

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

Further information can be found in the API at 
http://haggismcmutton.github.com/GenericPretty/ and in the source code itself.
===============================================================================

Please send any questions/suggestions to:
Razvan Ranca <ranca.razvan@gmail.com>