GenericPretty 1.1.8 → 1.1.9
raw patch · 7 files changed
+76/−138 lines, 7 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.PrettyPrint.GenericPretty: docPrecDefault :: (Generic a, GOut (Rep a)) => Int -> a -> Doc
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: 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)
- Text.PrettyPrint.GenericPretty: instance Out Integer
Files
- GenericPretty.cabal +1/−1
- README +21/−14
- TestSuite/CustomTest.hs +1/−0
- TestSuite/SimpleTest.hs +1/−2
- TestSuite/Tests.hs +8/−14
- TestSuite/ZigZagTest.hs +1/−2
- Text/PrettyPrint/GenericPretty.hs +43/−105
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.8 +Version: 1.1.9 -- 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.8.tar.gz" + 1. Download and unpack "GenericPretty-1.1.9.tar.gz" 2. Move to the correct directory: - $ cd GenericPretty-1.1.8 + $ cd GenericPretty-1.1.9 3. Run the following haskell commands to install the library globally: $ runhaskell Setup configure $ runhaskell Setup build @@ -82,8 +82,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) -instance (Out a) => Out (Tree a) where - docPrec = docPrecDefault +instance (Out a) => Out (Tree a) tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888) @@ -95,10 +94,9 @@ in a 'LANGUAGE' pragma, or manually by compiling with 'ghc -XDeriveGeneric'. As can be seen, to use the library you must simply import it, derive Generic -on the custom data type by writing +on the custom data type by typing "deriving (Generic)" -and write an instance of Out implementing 'docPrec' as - "docPrec = docPrecDefault" +and writing an empty instance of Out. Then you can use the pretty printing functions, such as 'pp' and 'pretty'. @@ -156,8 +154,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) -instance (Out a) => Out (Tree a) where - docPrec = docPrecDefault +instance (Out a) => Out (Tree a) tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888) @@ -213,7 +210,8 @@ doc (Leaf a) = parens $ text "customLeaf" <+> doc a doc (Node a b) = parens $ text "customNode" $$ nest 1 (doc a) $$ nest 1 (doc b) - + docPrec _ = doc + tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888) (Leaf 57575757))(Leaf (-14141414)))(Leaf 7777777)) @@ -221,9 +219,19 @@ main = pp tree1 ------------------------------ Here we import the library 'MyPretty' and use it directly to define doc. -We could have manually defined 'docPrec' or 'docList' as well if we wanted. -As it is now they are inferred from our definition of doc. +We then define 'docPrec' in terms of 'doc', ignoring the precedence parameter. +We could have manually defined 'docList' as well if we wanted. As it is now, +'docList' is inferred from 'doc'. +*****Note***** +As opposed to the 'Show' class, 'doc' and 'docPrec' are NOT defined in term +of one another. This means that even after giving a custom definition for +one of them, the other will retain the default behaviour. +If both are to be used, then custom definitions must be provided for both. + +For instance, in the above example, if we had ommited 'docPrec's definition, +the result would have been identical to that of 'SimpleTest.hs'. + By running the above custom definition we get a tree with little indentation: ----------------------------------- (customNode @@ -242,7 +250,6 @@ Text.PrettyPrint.HughesPJ libraries: http://www.haskell.org/ghc/docs/7.0.4/html/libraries/ghc-7.0.4/Pretty.html http://hackage.haskell.org/packages/archive/pretty/1.1.0.0/doc/html/Text-PrettyPrint-HughesPJ.html -The second one however is better documented. =============================================================================== ========================= Further Info ======================================== @@ -253,7 +260,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.8/doc/html/Text-PrettyPrint-GenericPretty.html + http://hackage.haskell.org/packages/archive/GenericPretty/1.1.9/doc/html/Text-PrettyPrint-GenericPretty.html and in the source code itself. ===============================================================================
TestSuite/CustomTest.hs view
@@ -9,6 +9,7 @@ doc (Leaf a) = parens $ text "customLeaf" <+> doc a doc (Node a b) = parens $ text "customNode" $$ nest 1 (doc a) $$ nest 1 (doc b) + docPrec _ = doc tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
TestSuite/SimpleTest.hs view
@@ -4,8 +4,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) -instance (Out a) => Out (Tree a) where - docPrec = docPrecDefault +instance (Out a) => Out (Tree a) tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
TestSuite/Tests.hs view
@@ -30,8 +30,7 @@ type Transition q = (q, Char, q) -- implement 'Out' so we can pretty print -instance (Out a) => Out (FSM a) where - docPrec = docPrecDefault +instance (Out a) => Out (FSM a) --implementation needed for quickCheck generation of random values instance Arbitrary a => Arbitrary (FSM a) where @@ -54,8 +53,7 @@ -- Binary Tree data type data BinaryTree a = EmptyBTree | BNode a (BinaryTree a) (BinaryTree a) deriving (Show, Generic) -instance (Out a) => Out (BinaryTree a) where - docPrec = docPrecDefault +instance (Out a) => Out (BinaryTree a) instance (Arbitrary a) => Arbitrary (BinaryTree a) where arbitrary = sized arbitTree @@ -95,8 +93,7 @@ -- Tree using record syntax data RecordTree a = RNode {val :: a, children :: [RecordTree a]} deriving (Show, Generic) -instance (Out a) => Out (RecordTree a) where - docPrec = docPrecDefault +instance (Out a) => Out (RecordTree a) instance (Arbitrary a) => Arbitrary (RecordTree a) where arbitrary = sized arbitTree @@ -120,8 +117,7 @@ data InfixTree a = ILeaf a a | (InfixTree a) :*: (InfixTree a) | (InfixTree a) :+: (InfixTree a) deriving (Show, Generic) -instance (Out a) => Out (InfixTree a) where - docPrec = docPrecDefault +instance (Out a) => Out (InfixTree a) instance (Arbitrary a) => Arbitrary (InfixTree a) where arbitrary = sized arbitTree @@ -145,8 +141,7 @@ data InfixRecordTree a = IRLeaf (Wrap a) | (:^:) {left :: InfixRecordTree a, right :: InfixRecordTree a} deriving (Show, Generic) -instance (Out a) => Out (InfixRecordTree a) where - docPrec = docPrecDefault +instance (Out a) => Out (InfixRecordTree a) instance (Arbitrary a) => Arbitrary (InfixRecordTree a) where arbitrary = sized arbitTree @@ -162,14 +157,13 @@ irt = IRLeaf (Wrap 5454544) :^: (IRLeaf (Wrap (-5375738)) :^: ((IRLeaf (Wrap 699879) :^: (IRLeaf (Wrap (-2332323)) :^: IRLeaf (Wrap 676765))) :^: IRLeaf (Wrap 99999))) -checkInfixRecordTree :: InfixRecordTree Int -> InfixRecordTree Int -> Bool +checkInfixRecordTree :: InfixRecordTree Float -> InfixRecordTree Float -> Bool checkInfixRecordTree _ a = removeSpaces (pretty a) == removeSpaces (show a) -- just a very simple user defined type that is used in IRTree data Wrap a = Wrap a deriving (Show, Generic) -instance Out a => Out (Wrap a) where - docPrec = docPrecDefault +instance Out a => Out (Wrap a) instance Arbitrary a => Arbitrary (Wrap a) where arbitrary = liftM Wrap arbitrary @@ -178,6 +172,6 @@ ("BinaryTree (Char)", quickCheck checkBinaryTree), ("RecordTree (String)", quickCheck checkRecordTree), ("InfixTree (Either Int Char)", quickCheck checkInfixTree), - ("InfixRecordTree (Int)", quickCheck checkInfixRecordTree)] + ("InfixRecordTree (Float)", quickCheck checkInfixRecordTree)] main = mapM_ (\(s,r) -> printf "%-30s: " s >> r) allTests
TestSuite/ZigZagTest.hs view
@@ -5,8 +5,7 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) -instance (Out a) => Out (Tree a) where - docPrec = docPrecDefault +instance (Out a) => Out (Tree a) tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node(Node(Leaf 888888)
Text/PrettyPrint/GenericPretty.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE TypeOperators, FlexibleInstances, FlexibleContexts #-} +{-# LANGUAGE TypeOperators, FlexibleInstances, FlexibleContexts, DefaultSignatures #-} {-| GenericPretty is a Haskell library that supports automatic @@ -12,12 +12,12 @@ 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.8> + For more information see the HackageDB project page: <http://hackage.haskell.org/package/GenericPretty-1.1.9> -} module Text.PrettyPrint.GenericPretty ( - Out(..), docPrecDefault, + Out(..), pp, ppLen, ppStyle, pretty, prettyLen, prettyStyle, fullPP, Generic, outputIO, outputStr @@ -89,11 +89,15 @@ -- Function application has precedence @10@. -> a -- ^ the value to be converted to a 'String' -> Doc -- ^ the resulting Doc + default docPrec :: (Generic a ,GOut (Rep a)) => Int -> a -> Doc + docPrec n x = sep $ out1 (from x) Pref n False -- | 'doc' is the equivalent of 'Prelude.show' -- -- This is a specialised variant of 'docPrec', using precedence context zero. doc :: a -> Doc + default doc :: (Generic a ,GOut (Rep a)) => a -> Doc + doc x = sep $ out1 (from x) Pref 0 False -- | 'docList' is the equivalent of 'Prelude.showList'. -- @@ -103,32 +107,8 @@ -- the 'Char' type, where values of type 'String' should be shown -- in double quotes, rather than between square brackets. docList :: [a] -> Doc - - doc = docPrec 0 - docPrec _ = doc docList = docListWith doc --- | Default generic 'docPrec' method. --- --- Converts the type into a sum of products and passes it on to the generic --- pretty printing functions, finally it concatenates all of the Doc's --- --- It can be used in code to define the instance for 'Out'. --- --- For instance, given the declaration: --- --- > data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) --- --- The user would need to write an instance declaration like: --- --- > instance (Out a) => Out (Tree a) where --- > docPrec = docPrecDefault --- --- After doing this, the user can now pretty printing function like 'pp' and 'pretty' --- on data of type Tree -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 docListWith f = brackets . fcat . punctuate comma . map f @@ -160,12 +140,12 @@ -- so can't be an instance of 'Out' class GOut f where -- |'out1' is the (*->*) kind equivalent of 'docPrec' - out1 :: f x -- The sum of products representation of the user's custom type + out1 :: f x -- The sum of products representation of the user's custom type -> Type -- The type of multiplication. Record, Prefix or Infix. -> Int -- The operator precedence, determines wether to wrap stuff in parens. -> Bool -- A flag, marks wether the constructor directly above was wrapped in parens. - -- Used to determine correct indentation - -> [Doc] -- The result. Each Doc could be on a newline, depending on available space. + -- Used to determine correct indentation + -> [Doc] -- The result. Each Doc could be on a newline, depending on available space. -- |'isNullary' marks nullary constructors, so that we don't put parens around them isNullary :: f x -> Bool @@ -378,37 +358,39 @@ -- define some instances of Out making sure to generate output identical to 'show' modulo the extra whitespace instance Out () where doc _ = text "()" + docPrec _ = doc instance Out Char where - 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 - + doc a = char '\'' <> (text.middle.show $ a) <> char '\'' + docPrec _ = doc + docList xs = text $ show xs + instance Out Int where - docPrec n x - | n/=0 && x<0 = parens $ int x - | otherwise = int x + docPrec n x + | n/=0 && x<0 = parens $ int x + | otherwise = int x + doc = docPrec 0 instance Out Float where - docPrec n x - | n/=0 && x<0 = parens $ float x - | otherwise = float x + docPrec n x + | n/=0 && x<0 = parens $ float x + | otherwise = float x + doc = docPrec 0 instance Out Double where - docPrec n x - | n/=0 && x<0 = parens $ double x - | otherwise = double x + docPrec n x + | n/=0 && x<0 = parens $ double x + | otherwise = double x + doc = docPrec 0 instance Out a => Out [a] where doc = docList + docPrec _ = doc instance Out Bool where doc True = text "True" doc False = text "False" + docPrec _ = doc instance Out a => Out (Maybe a) where docPrec n Nothing = text "Nothing" @@ -417,6 +399,7 @@ |otherwise = result where result = text "Just" <+> docPrec 10 x + doc = docPrec 0 instance (Out a, Out b) => Out (Either a b) where docPrec n (Left x) @@ -429,79 +412,34 @@ | otherwise = result where result = text "Right" <+> docPrec 10 y + doc = docPrec 0 instance (Out a, Out b) => Out (a, b) where doc (a,b) = parens (sep [doc a <> comma, doc b]) + docPrec _ = doc instance (Out a, Out b, Out c) => Out (a, b, c) where doc (a,b,c) = parens (sep [doc a <> comma, doc b <> comma, doc c]) + docPrec _ = doc instance (Out a, Out b, Out c, Out d) => Out (a, b, c, d) where doc (a,b,c,d) = parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d]) + docPrec _ = doc instance (Out a, Out b, Out c, Out d, Out e) => Out (a, b, c, d, e) where doc (a,b,c,d,e) = parens (sep [doc a <> comma, doc b <> comma, doc c <> comma, doc d <> comma, doc e]) + docPrec _ = doc instance (Out a, Out b, Out c, Out d, Out e, Out f) => Out (a, b, c, d, e, f) where - 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]) + 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]) + docPrec _ = doc instance (Out a, Out b, Out c, Out d, Out e, Out f, Out g) => Out (a, b, c, d, e, f, g) where - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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])+ 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]) + docPrec _ = doc