GenericPretty-1.2.2: A generic, derivable, haskell pretty printer.

Safe HaskellSafe
LanguageHaskell98

Text.PrettyPrint.GenericPretty

Description

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

Synopsis

Documentation

class Out a where Source #

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

Methods

docPrec Source #

Arguments

:: Int

the operator precedence of the enclosing context (a number from 0 to 11). Function application has precedence 10.

-> a

the value to be converted to a String

-> Doc

the resulting Doc

docPrec is the equivalent of showsPrec.

Convert a value to a pretty printable Doc.

docPrec Source #

Arguments

:: (Generic a, GOut (Rep a)) 
=> Int

the operator precedence of the enclosing context (a number from 0 to 11). Function application has precedence 10.

-> a

the value to be converted to a String

-> Doc

the resulting Doc

docPrec is the equivalent of showsPrec.

Convert a value to a pretty printable Doc.

doc :: a -> Doc Source #

doc is the equivalent of show

This is a specialised variant of docPrec, using precedence context zero.

doc :: (Generic a, GOut (Rep a)) => a -> Doc Source #

doc is the equivalent of show

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.

Instances

Out Bool Source # 

Methods

docPrec :: Int -> Bool -> Doc Source #

doc :: Bool -> Doc Source #

docList :: [Bool] -> Doc Source #

Out Char Source # 

Methods

docPrec :: Int -> Char -> Doc Source #

doc :: Char -> Doc Source #

docList :: [Char] -> Doc Source #

Out Double Source # 
Out Float Source # 
Out Int Source # 

Methods

docPrec :: Int -> Int -> Doc Source #

doc :: Int -> Doc Source #

docList :: [Int] -> Doc Source #

Out Integer Source # 
Out Rational Source # 
Out () Source # 

Methods

docPrec :: Int -> () -> Doc Source #

doc :: () -> Doc Source #

docList :: [()] -> Doc Source #

Out a => Out [a] Source # 

Methods

docPrec :: Int -> [a] -> Doc Source #

doc :: [a] -> Doc Source #

docList :: [[a]] -> Doc Source #

Out a => Out (Maybe a) Source # 

Methods

docPrec :: Int -> Maybe a -> Doc Source #

doc :: Maybe a -> Doc Source #

docList :: [Maybe a] -> Doc Source #

(Out a, Out b) => Out (Either a b) Source # 

Methods

docPrec :: Int -> Either a b -> Doc Source #

doc :: Either a b -> Doc Source #

docList :: [Either a b] -> Doc Source #

(Out a, Out b) => Out (a, b) Source # 

Methods

docPrec :: Int -> (a, b) -> Doc Source #

doc :: (a, b) -> Doc Source #

docList :: [(a, b)] -> Doc Source #

(Out a, Out b, Out c) => Out (a, b, c) Source # 

Methods

docPrec :: Int -> (a, b, c) -> Doc Source #

doc :: (a, b, c) -> Doc Source #

docList :: [(a, b, c)] -> Doc Source #

(Out a, Out b, Out c, Out d) => Out (a, b, c, d) Source # 

Methods

docPrec :: Int -> (a, b, c, d) -> Doc Source #

doc :: (a, b, c, d) -> Doc Source #

docList :: [(a, b, c, d)] -> Doc Source #

(Out a, Out b, Out c, Out d, Out e) => Out (a, b, c, d, e) Source # 

Methods

docPrec :: Int -> (a, b, c, d, e) -> Doc Source #

doc :: (a, b, c, d, e) -> Doc Source #

docList :: [(a, b, c, d, e)] -> Doc Source #

(Out a, Out b, Out c, Out d, Out e, Out f) => Out (a, b, c, d, e, f) Source # 

Methods

docPrec :: Int -> (a, b, c, d, e, f) -> Doc Source #

doc :: (a, b, c, d, e, f) -> Doc Source #

docList :: [(a, b, c, d, e, f)] -> Doc Source #

(Out a, Out b, Out c, Out d, Out e, Out f, Out g) => Out (a, b, c, d, e, f, g) Source # 

Methods

docPrec :: Int -> (a, b, c, d, e, f, g) -> Doc Source #

doc :: (a, b, c, d, e, f, g) -> Doc Source #

docList :: [(a, b, c, d, e, f, g)] -> Doc 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.

ppStyle :: Out a => Style -> a -> IO () Source #

Customizable pretty printer.

Takes a user defined Style as a parameter and uses outputIO to obtain the result Equivalent to:

fullPP outputIO (putChar '\n')

pretty :: Out a => a -> String Source #

The default pretty printer returning Strings

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.

prettyStyle :: Out a => Style -> a -> String Source #

Customizable pretty printer

Takes a user defined Style as a parameter and uses outputStr to obtain the result Equivalent to:

fullPP outputStr ""

fullPP Source #

Arguments

:: Out a 
=> (TextDetails -> b -> b)

Function that handles the text conversion (eg: outputIO)

-> b

The end element of the result ( eg: "" or putChar('\n') )

-> Style

The pretty printing Style to use

-> a

The value to pretty print

-> b

The pretty printed result

fullPP is a fully customizable Pretty Printer

Every other pretty printer just gives some default values to fullPP

class Generic a #

Representable types of kind *. This class is derivable in GHC with the DeriveGeneric flag on.

Minimal complete definition

from, to

Instances

Generic Bool 

Associated Types

type Rep Bool :: * -> * #

Methods

from :: Bool -> Rep Bool x #

to :: Rep Bool x -> Bool #

Generic Ordering 

Associated Types

type Rep Ordering :: * -> * #

Methods

from :: Ordering -> Rep Ordering x #

to :: Rep Ordering x -> Ordering #

Generic () 

Associated Types

type Rep () :: * -> * #

Methods

from :: () -> Rep () x #

to :: Rep () x -> () #

Generic Fixity 

Associated Types

type Rep Fixity :: * -> * #

Methods

from :: Fixity -> Rep Fixity x #

to :: Rep Fixity x -> Fixity #

Generic Associativity 

Associated Types

type Rep Associativity :: * -> * #

Generic SourceUnpackedness 
Generic SourceStrictness 
Generic DecidedStrictness 
Generic Doc 

Associated Types

type Rep Doc :: * -> * #

Methods

from :: Doc -> Rep Doc x #

to :: Rep Doc x -> Doc #

Generic TextDetails 

Associated Types

type Rep TextDetails :: * -> * #

Generic Style 

Associated Types

type Rep Style :: * -> * #

Methods

from :: Style -> Rep Style x #

to :: Rep Style x -> Style #

Generic Mode 

Associated Types

type Rep Mode :: * -> * #

Methods

from :: Mode -> Rep Mode x #

to :: Rep Mode x -> Mode #

Generic [a] 

Associated Types

type Rep [a] :: * -> * #

Methods

from :: [a] -> Rep [a] x #

to :: Rep [a] x -> [a] #

Generic (Maybe a) 

Associated Types

type Rep (Maybe a) :: * -> * #

Methods

from :: Maybe a -> Rep (Maybe a) x #

to :: Rep (Maybe a) x -> Maybe a #

Generic (Par1 p) 

Associated Types

type Rep (Par1 p) :: * -> * #

Methods

from :: Par1 p -> Rep (Par1 p) x #

to :: Rep (Par1 p) x -> Par1 p #

Generic (Doc a) 

Associated Types

type Rep (Doc a) :: * -> * #

Methods

from :: Doc a -> Rep (Doc a) x #

to :: Rep (Doc a) x -> Doc a #

Generic (Either a b) 

Associated Types

type Rep (Either a b) :: * -> * #

Methods

from :: Either a b -> Rep (Either a b) x #

to :: Rep (Either a b) x -> Either a b #

Generic (V1 k p) 

Associated Types

type Rep (V1 k p) :: * -> * #

Methods

from :: V1 k p -> Rep (V1 k p) x #

to :: Rep (V1 k p) x -> V1 k p #

Generic (U1 k p) 

Associated Types

type Rep (U1 k p) :: * -> * #

Methods

from :: U1 k p -> Rep (U1 k p) x #

to :: Rep (U1 k p) x -> U1 k p #

Generic (a, b) 

Associated Types

type Rep (a, b) :: * -> * #

Methods

from :: (a, b) -> Rep (a, b) x #

to :: Rep (a, b) x -> (a, b) #

Generic (Proxy k t) 

Associated Types

type Rep (Proxy k t) :: * -> * #

Methods

from :: Proxy k t -> Rep (Proxy k t) x #

to :: Rep (Proxy k t) x -> Proxy k t #

Generic (Rec1 k f p) 

Associated Types

type Rep (Rec1 k f p) :: * -> * #

Methods

from :: Rec1 k f p -> Rep (Rec1 k f p) x #

to :: Rep (Rec1 k f p) x -> Rec1 k f p #

Generic (URec k (Ptr ()) p) 

Associated Types

type Rep (URec k (Ptr ()) p) :: * -> * #

Methods

from :: URec k (Ptr ()) p -> Rep (URec k (Ptr ()) p) x #

to :: Rep (URec k (Ptr ()) p) x -> URec k (Ptr ()) p #

Generic (URec k Char p) 

Associated Types

type Rep (URec k Char p) :: * -> * #

Methods

from :: URec k Char p -> Rep (URec k Char p) x #

to :: Rep (URec k Char p) x -> URec k Char p #

Generic (URec k Double p) 

Associated Types

type Rep (URec k Double p) :: * -> * #

Methods

from :: URec k Double p -> Rep (URec k Double p) x #

to :: Rep (URec k Double p) x -> URec k Double p #

Generic (URec k Float p) 

Associated Types

type Rep (URec k Float p) :: * -> * #

Methods

from :: URec k Float p -> Rep (URec k Float p) x #

to :: Rep (URec k Float p) x -> URec k Float p #

Generic (URec k Int p) 

Associated Types

type Rep (URec k Int p) :: * -> * #

Methods

from :: URec k Int p -> Rep (URec k Int p) x #

to :: Rep (URec k Int p) x -> URec k Int p #

Generic (URec k Word p) 

Associated Types

type Rep (URec k Word p) :: * -> * #

Methods

from :: URec k Word p -> Rep (URec k Word p) x #

to :: Rep (URec k Word p) x -> URec k Word p #

Generic (a, b, c) 

Associated Types

type Rep (a, b, c) :: * -> * #

Methods

from :: (a, b, c) -> Rep (a, b, c) x #

to :: Rep (a, b, c) x -> (a, b, c) #

Generic (K1 k i c p) 

Associated Types

type Rep (K1 k i c p) :: * -> * #

Methods

from :: K1 k i c p -> Rep (K1 k i c p) x #

to :: Rep (K1 k i c p) x -> K1 k i c p #

Generic ((:+:) k f g p) 

Associated Types

type Rep ((k :+: f) g p) :: * -> * #

Methods

from :: (k :+: f) g p -> Rep ((k :+: f) g p) x #

to :: Rep ((k :+: f) g p) x -> (k :+: f) g p #

Generic ((:*:) k f g p) 

Associated Types

type Rep ((k :*: f) g p) :: * -> * #

Methods

from :: (k :*: f) g p -> Rep ((k :*: f) g p) x #

to :: Rep ((k :*: f) g p) x -> (k :*: f) g p #

Generic (a, b, c, d) 

Associated Types

type Rep (a, b, c, d) :: * -> * #

Methods

from :: (a, b, c, d) -> Rep (a, b, c, d) x #

to :: Rep (a, b, c, d) x -> (a, b, c, d) #

Generic (M1 k i c f p) 

Associated Types

type Rep (M1 k i c f p) :: * -> * #

Methods

from :: M1 k i c f p -> Rep (M1 k i c f p) x #

to :: Rep (M1 k i c f p) x -> M1 k i c f p #

Generic ((:.:) k2 k1 f g p) 

Associated Types

type Rep ((k2 :.: k1) f g p) :: * -> * #

Methods

from :: (k2 :.: k1) f g p -> Rep ((k2 :.: k1) f g p) x #

to :: Rep ((k2 :.: k1) f g p) x -> (k2 :.: k1) f g p #

Generic (a, b, c, d, e) 

Associated Types

type Rep (a, b, c, d, e) :: * -> * #

Methods

from :: (a, b, c, d, e) -> Rep (a, b, c, d, e) x #

to :: Rep (a, b, c, d, e) x -> (a, b, c, d, e) #

Generic (a, b, c, d, e, f) 

Associated Types

type Rep (a, b, c, d, e, f) :: * -> * #

Methods

from :: (a, b, c, d, e, f) -> Rep (a, b, c, d, e, f) x #

to :: Rep (a, b, c, d, e, f) x -> (a, b, c, d, e, f) #

Generic (a, b, c, d, e, f, g) 

Associated Types

type Rep (a, b, c, d, e, f, g) :: * -> * #

Methods

from :: (a, b, c, d, e, f, g) -> Rep (a, b, c, d, e, f, g) x #

to :: Rep (a, b, c, d, e, f, g) x -> (a, b, c, d, e, f, g) #

outputIO :: TextDetails -> IO () -> IO () Source #

Utility function that handles the text conversion for fullPP.

outputIO transforms the text into Strings and outputs it directly.

outputStr :: TextDetails -> String -> String Source #

Utility function that handles the text conversion for fullPP.

outputStr just leaves the text as a String which is usefull if you want to further process the pretty printed result.