GenericPretty 1.1.6 → 1.1.7
raw patch · 7 files changed
+130/−91 lines, 7 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.PrettyPrint.GenericPretty: genOut :: (Generic a, GOut (Rep a)) => Int -> a -> Doc
- Text.PrettyPrint.GenericPretty: wrapParens :: Bool -> [Doc] -> [Doc]
+ Text.PrettyPrint.GenericPretty: docPrecDefault :: (Generic a, GOut (Rep a)) => Int -> a -> Doc
+ Text.PrettyPrint.GenericPretty: instance Out ()
+ Text.PrettyPrint.GenericPretty: instance Out Double
+ Text.PrettyPrint.GenericPretty: instance Out Float
- Text.PrettyPrint.GenericPretty: fullPP :: Out a => Mode -> Int -> Float -> (TextDetails -> b -> b) -> b -> a -> b
+ Text.PrettyPrint.GenericPretty: fullPP :: Out a => (TextDetails -> b -> b) -> b -> Style -> a -> b
Files
- GenericPretty.cabal +1/−1
- README +6/−6
- TestSuite/SimpleTest.hs +1/−1
- TestSuite/Tests.hs +6/−6
- TestSuite/ZigZagTest.hs +1/−1
- Text/PrettyPrint/GenericPretty.hs +112/−73
- Text/PrettyPrint/MyPretty.hs +3/−3
GenericPretty.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented. -Version: 1.1.6 +Version: 1.1.7 -- A short (one-line) description of the package. Synopsis: A generic, derivable, haskell pretty printer.
README view
@@ -55,9 +55,9 @@ The package is installed in the same way as any other package. The steps are: 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 and unpack "GenericPretty-1.1.6.tar.gz" + 1. Download and unpack "GenericPretty-1.1.7.tar.gz" 2. Move to the correct directory: - $ cd GenericPretty-1.1.6 + $ cd GenericPretty-1.1.7 3. Run the following haskell commands to install the library globally: $ runhaskell Setup configure $ runhaskell Setup build @@ -83,7 +83,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) instance (Out a) => Out (Tree a) where - docPrec = genOut + docPrec = docPrecDefault tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888) @@ -98,7 +98,7 @@ on the custom data type by writing "deriving (Generic)" and write an instance of Out implementing 'docPrec' as - "docPrec = genOut" + "docPrec = docPrecDefault" Then you can use the pretty printing functions, such as 'pp' and 'pretty'. @@ -157,7 +157,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) instance (Out a) => Out (Tree a) where - docPrec = genOut + docPrec = docPrecDefault tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888) @@ -253,7 +253,7 @@ examples integrated with QuickCheck can be found in 'TestSuite/Tests.hs'. Further information can be found in the API: - http://hackage.haskell.org/packages/archive/GenericPretty/1.1.6/doc/html/Text-PrettyPrint-GenericPretty.html + http://hackage.haskell.org/packages/archive/GenericPretty/1.1.7/doc/html/Text-PrettyPrint-GenericPretty.html and in the source code itself. ===============================================================================
TestSuite/SimpleTest.hs view
@@ -5,7 +5,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) instance (Out a) => Out (Tree a) where - docPrec = genOut + docPrec = docPrecDefault tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
TestSuite/Tests.hs view
@@ -31,7 +31,7 @@ -- implement 'Out' so we can pretty print instance (Out a) => Out (FSM a) where - docPrec = genOut + docPrec = docPrecDefault --implementation needed for quickCheck generation of random values instance Arbitrary a => Arbitrary (FSM a) where @@ -55,7 +55,7 @@ data BinaryTree a = EmptyBTree | BNode a (BinaryTree a) (BinaryTree a) deriving (Show, Generic) instance (Out a) => Out (BinaryTree a) where - docPrec = genOut + docPrec = docPrecDefault instance (Arbitrary a) => Arbitrary (BinaryTree a) where arbitrary = sized arbitTree @@ -96,7 +96,7 @@ data RecordTree a = RNode {val :: a, children :: [RecordTree a]} deriving (Show, Generic) instance (Out a) => Out (RecordTree a) where - docPrec = genOut + docPrec = docPrecDefault instance (Arbitrary a) => Arbitrary (RecordTree a) where arbitrary = sized arbitTree @@ -121,7 +121,7 @@ deriving (Show, Generic) instance (Out a) => Out (InfixTree a) where - docPrec = genOut + docPrec = docPrecDefault instance (Arbitrary a) => Arbitrary (InfixTree a) where arbitrary = sized arbitTree @@ -146,7 +146,7 @@ deriving (Show, Generic) instance (Out a) => Out (InfixRecordTree a) where - docPrec = genOut + docPrec = docPrecDefault instance (Arbitrary a) => Arbitrary (InfixRecordTree a) where arbitrary = sized arbitTree @@ -169,7 +169,7 @@ data Wrap a = Wrap a deriving (Show, Generic) instance Out a => Out (Wrap a) where - docPrec = genOut + docPrec = docPrecDefault instance Arbitrary a => Arbitrary (Wrap a) where arbitrary = liftM Wrap arbitrary
TestSuite/ZigZagTest.hs view
@@ -6,7 +6,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) instance (Out a) => Out (Tree a) where - docPrec = genOut + docPrec = docPrecDefault tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
Text/PrettyPrint/GenericPretty.hs view
@@ -10,12 +10,18 @@ printer yields an output identical to that of 'Prelude.show', except for extra whitespace. - For examples of usage please see the README file included in the package + 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-1.1.7> -} -module Text.PrettyPrint.GenericPretty(pp, ppLen, ppStyle, pretty, prettyLen, prettyStyle, fullPP, - genOut, wrapParens, outputIO, outputStr, - Out(..), Generic) where +module Text.PrettyPrint.GenericPretty + ( + Out(..), docPrecDefault, + pp, ppLen, ppStyle, pretty, prettyLen, prettyStyle, fullPP, + Generic, + outputIO, outputStr + ) where import Data.List import GHC.Generics @@ -116,12 +122,12 @@ -- The user would need to write an instance declaration like: -- -- > instance (Out a) => Out (Tree a) where --- > docPrec = genOut +-- > docPrec = docPrecDefault -- -- After doing this, the user can now pretty printing function like 'pp' and 'pretty' -- on data of type Tree -genOut :: (Generic a ,GOut (Rep a)) => Int -> a -> Doc -genOut n x = sep $ out1 (from x) Pref n False +docPrecDefault :: (Generic a ,GOut (Rep a)) => Int -> a -> Doc +docPrecDefault n x = sep $ out1 (from x) Pref n False -- used to define docList, creates output identical to that of show for general list types docListWith :: (a -> Doc) -> [a] -> Doc @@ -267,23 +273,20 @@ -- | 'fullPP' is a fully customizable Pretty Printer -- -- Every other pretty printer just gives some default values to 'fullPP' -fullPP :: (Out a) => Mode -- ^The 'Pretty' mode to use /(eg: Pretty.PageMode)/ - -> Int -- ^The maximum line length - -> Float -- ^The number of ribbons per line /(the fraction of line length over the/ - -- /max length of non-indentation text per line; eg: lineLength = 80 and/ - -- /ribbonsPerLine = 1.5 => max of 53 non-indentation characters per line)/ - -> (TextDetails -> b -> b) -- ^Function that handles the text conversion /(eg: 'outputIO')/ +fullPP :: (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 'Text.PrettyPrint.MyPretty.Style' to use -> a -- ^The value to pretty print -> b -- ^The pretty printed result -fullPP mode len rib td end a = fullRender mode len rib td end doc +fullPP td end s a = fullRender (mode s) (lineLength s) (ribbonsPerLine s) td end doc where doc = docPrec 0 a --- | 'outputIO' transforms the text into strings and outputs it directly. +-- | Utility function that handles the text conversion for 'fullPP'. -- --- This is one example of a function that can handle the text conversion for 'fullPP'. +-- 'outputIO' transforms the text into 'String's and outputs it directly. outputIO :: TextDetails -> IO() -> IO() outputIO td act = do putStr $ decode td @@ -295,10 +298,10 @@ decode (Chr c) = [c] decode (Str s) = s --- | 'outputStr' just leaves the text as a string which is usefull if you want --- to further process the pretty printed result. +-- | Utility function that handles the text conversion for 'fullPP'. -- --- This is another example of a function that can handle 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. outputStr :: TextDetails -> String -> String outputStr td str = decode td ++ str where @@ -311,61 +314,97 @@ -- | Customizable pretty printer -- -- Takes a user defined 'Text.PrettyPrint.MyPretty.Style' as a parameter and uses 'outputStr' to obtain the result +-- Equivalent to: +-- +-- > fullPP outputStr "" prettyStyle :: (Out a) => Style -> a -> String -prettyStyle s = fullPP (mode s) (lineLength s) (ribbonsPerLine s) outputStr "" +prettyStyle = fullPP outputStr "" -- | Semi-customizable pretty printer. -- --- Takes the lineLength as a parameter and uses mode = Pretty.PageMode and ribbonsPerLine = 1 +-- Equivalent to: +-- +-- > prettyStyle customStyle +-- +-- Where customStyle uses the specified line length, mode = PageMode and ribbonsPerLine = 1. prettyLen :: (Out a) => Int -> a -> String -prettyLen l = fullPP PageMode l 1 outputStr "" +prettyLen l = prettyStyle customStyle + where + customStyle = Style {mode = PageMode, lineLength = l, ribbonsPerLine = 1} -- | The default pretty printer returning 'String's -- --- Uses the default 'Text.PrettyPrint.MyPretty.Style', called 'style' +-- Uses the default 'Text.PrettyPrint.MyPretty.Style', called 'style. Equivalent to +-- +-- > prettyStyle style pretty :: (Out a) => a -> String pretty = prettyStyle style -- | Customizable pretty printer. -- -- Takes a user defined 'Text.PrettyPrint.MyPretty.Style' as a parameter and uses 'outputIO' to obtain the result +-- Equivalent to: +-- +-- > fullPP outputIO (putChar '\n') ppStyle :: (Out a) => Style -> a -> IO() -ppStyle s = fullPP (mode s) (lineLength s) (ribbonsPerLine s) outputIO (putChar '\n') +ppStyle = fullPP outputIO (putChar '\n') -- | Semi-customizable pretty printer. -- --- Takes the lineLength as a parameter and uses mode = Pretty.PageMode and ribbonsPerLine = 1 +-- Equivalent to: +-- +-- > ppStyle customStyle +-- +-- Where customStyle uses the specified line length, mode = PageMode and ribbonsPerLine = 1. ppLen :: (Out a) => Int -> a -> IO() -ppLen l = fullPP PageMode l 1 outputIO (putChar '\n') +ppLen l = ppStyle customStyle + where + customStyle = Style {mode = PageMode, lineLength = l, ribbonsPerLine = 1} -- | The default Pretty Printer, -- --- Uses the default 'Text.PrettyPrint.MyPretty.Style', called 'style' +-- Uses the default 'Text.PrettyPrint.MyPretty.Style', called 'style'. Equivalent to +-- +-- > ppStyle style pp :: (Out a) => a -> IO() pp = ppStyle style + -- define some instances of Out making sure to generate output identical to 'show' modulo the extra whitespace +instance Out () where + doc _ = text "()" + instance Out Char where - docPrec _ a = char '\'' <> (text.middle.show $ a) <> char '\'' + doc a = char '\'' <> (text.middle.show $ a) <> char '\'' docList xs = text $ show xs instance Out Integer where docPrec n x | n/=0 && x<0 = parens $ integer x | otherwise = integer x - -instance Out a => Out [a] where - docPrec _ = docList - -instance Out Bool where - docPrec _ True = text "True" - docPrec _ False = text "False" instance Out Int where docPrec n x | n/=0 && x<0 = parens $ int x | otherwise = int x +instance Out Float where + docPrec n x + | n/=0 && x<0 = parens $ float x + | otherwise = float x + +instance Out Double where + docPrec n x + | n/=0 && x<0 = parens $ double x + | otherwise = double x + +instance Out a => Out [a] where + doc = docList + +instance Out Bool where + doc True = text "True" + doc False = text "False" + instance Out a => Out (Maybe a) where docPrec n Nothing = text "Nothing" docPrec n (Just x) @@ -387,77 +426,77 @@ result = text "Right" <+> docPrec 10 y instance (Out a, Out b) => Out (a, b) where - docPrec _ (a,b) = parens (sep [docPrec 0 a <> comma, docPrec 0 b]) + doc (a,b) = parens (sep [doc a <> comma, doc b]) instance (Out a, Out b, Out c) => Out (a, b, c) where - docPrec _ (a,b,c) = parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c]) + doc (a,b,c) = parens (sep [doc a <> comma, doc b <> comma, doc c]) instance (Out a, Out b, Out c, Out d) => Out (a, b, c, d) where - docPrec _ (a,b,c,d) = parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d]) + doc (a,b,c,d) = parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d]) instance (Out a, Out b, Out c, Out d, Out e) => Out (a, b, c, d, e) where - docPrec _ (a,b,c,d,e) = parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, docPrec 0 e]) + doc (a,b,c,d,e) = parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e]) instance (Out a, Out b, Out c, Out d, Out e, Out f) => Out (a, b, c, d, e, f) where - docPrec _ (a, b, c, d, e, f) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, - docPrec 0 d <> comma, docPrec 0 e <> comma, docPrec 0 f]) + doc (a, b, c, d, e, f) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, + doc d <> comma, doc e <> comma, doc f]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g) => Out (a, b, c, d, e, f, g) where - docPrec _ (a, b, c, d, e, f, g) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, - docPrec 0 d <> comma, docPrec 0 e <> comma, docPrec 0 f <> comma, docPrec 0 g]) + doc (a, b, c, d, e, f, g) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, + doc d <> comma, doc e <> comma, doc f <> comma, doc g]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h) => Out (a, b, c, d, e, f, g, h) where - docPrec _ (a, b, c, d, e, f, g, h) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, - docPrec 0 d <> comma, docPrec 0 e <> comma, docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h]) + doc (a, b, c, d, e, f, g, h) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, + doc d <> comma, doc e <> comma, doc f <> comma, doc g <> comma, doc h]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i) => Out (a, b, c, d, e, f, g, h, i) where - docPrec _ (a, b, c, d, e, f, g, h, i) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, - docPrec 0 e <> comma, docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i]) + doc (a, b, c, d, e, f, g, h, i) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, + doc e <> comma, doc f <> comma, doc g <> comma, doc h <> comma, doc i]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i, Out j) => Out (a, b, c, d, e, f, g, h, i, j) where - docPrec _ (a, b, c, d, e, f, g, h, i, j) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, - docPrec 0 e <> comma, docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i <> comma, docPrec 0 j]) + doc (a, b, c, d, e, f, g, h, i, j) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, + doc e <> comma, doc f <> comma, doc g <> comma, doc h <> comma, doc i <> comma, doc j]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i, Out j, Out k) => Out (a, b, c, d, e, f, g, h, i, j, k) where - docPrec _ (a, b, c, d, e, f, g, h, i, j, k) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, docPrec 0 e<> comma, - docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i <> comma, docPrec 0 j <> comma, docPrec 0 k]) + doc (a, b, c, d, e, f, g, h, i, j, k) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e<> comma, + doc f <> comma, doc g <> comma, doc h <> comma, doc i <> comma, doc j <> comma, doc k]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i, Out j, Out k, Out l) => Out (a, b, c, d, e, f, g, h, i, j, k, l) where - docPrec _ (a, b, c, d, e, f, g, h, i, j, k, l) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, docPrec 0 e <> comma, - docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i <> comma, docPrec 0 j <> comma, - docPrec 0 k <> comma, docPrec 0 l]) + doc (a, b, c, d, e, f, g, h, i, j, k, l) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e <> comma, + doc f <> comma, doc g <> comma, doc h <> comma, doc i <> comma, doc j <> comma, + doc k <> comma, doc l]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i, Out j, Out k, Out l, Out m) => Out (a, b, c, d, e, f, g, h, i, j, k, l, m) where - docPrec _ (a, b, c, d, e, f, g, h, i, j, k, l, m) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, docPrec 0 e <> comma, - docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i <> comma, docPrec 0 j <> comma, - docPrec 0 k <> comma, docPrec 0 l <> comma, docPrec 0 m]) + doc (a, b, c, d, e, f, g, h, i, j, k, l, m) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e <> comma, + doc f <> comma, doc g <> comma, doc h <> comma, doc i <> comma, doc j <> comma, + doc k <> comma, doc l <> comma, doc m]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i, Out j, Out k, Out l, Out m, Out n) => Out (a, b, c, d, e, f, g, h, i, j, k, l, m, n) where - docPrec _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, docPrec 0 e <> comma, - docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i <> comma, docPrec 0 j <> comma, - docPrec 0 k <> comma, docPrec 0 l <> comma, docPrec 0 m <> comma, docPrec 0 n]) + doc (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e <> comma, + doc f <> comma, doc g <> comma, doc h <> comma, doc i <> comma, doc j <> comma, + doc k <> comma, doc l <> comma, doc m <> comma, doc n]) instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g, Out h, Out i, Out j, Out k, Out l, Out m, Out n, Out o) => Out (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) where - docPrec _ (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = - parens (sep [docPrec 0 a <> comma, docPrec 0 b <> comma, docPrec 0 c <> comma, docPrec 0 d <> comma, docPrec 0 e <> comma, - docPrec 0 f <> comma, docPrec 0 g <> comma, docPrec 0 h <> comma, docPrec 0 i <> comma, docPrec 0 j <> comma, - docPrec 0 k <> comma, docPrec 0 l <> comma, docPrec 0 m <> comma, docPrec 0 n <> comma, docPrec 0 o])+ doc (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = + parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e <> comma, + doc f <> comma, doc g <> comma, doc h <> comma, doc i <> comma, doc j <> comma, + doc k <> comma, doc l <> comma, doc m <> comma, doc n <> comma, doc o])
Text/PrettyPrint/MyPretty.hs view
@@ -19,7 +19,7 @@ Style(..), renderStyle, style )where -import Pretty +import Pretty hiding (render) import FastString -- | A rendering style @@ -29,7 +29,7 @@ , ribbonsPerLine :: Float -- ^ Ratio of ribbon length to line length } --- | Render a document using a particular style. +-- | Render a document using a particular Style. renderStyle :: Style -> Doc -> String renderStyle s = fullRender (mode s) (lineLength s) (ribbonsPerLine s) outputStr "" where @@ -41,7 +41,7 @@ decode (LStr s1 _) = unpackLitString s1 decode (Chr c) = [c] decode (Str s) = s - + -- | The default 'Style' used for "Text.PrettyPrint.GenericPretty" -- (mode=PageMode, lineLength=80, ribbonsPerLine=1.5) style :: Style