GenericPretty 0.1.1 → 0.1.2
raw patch · 5 files changed
+33/−17 lines, 5 files
Files
- GenericPretty.cabal +1/−1
- README +4/−2
- TestSuite/SimpleTest.hs +2/−1
- TestSuite/Tests.hs +17/−6
- Text/PrettyPrint/GenericPretty.hs +9/−7
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: 0.1.1 +Version: 0.1.2 -- A short (one-line) description of the package. Synopsis: A generic, derivable, haskell pretty printer.
README view
@@ -15,8 +15,10 @@ import Text.PrettyPrint.GenericPretty data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) -instance (Out a) => Out (Tree a) +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)) @@ -27,7 +29,7 @@ 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 empty instance of 'Out'. +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'.
TestSuite/SimpleTest.hs view
@@ -4,7 +4,8 @@ data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Generic) -instance (Out a) => Out (Tree a) +instance (Out a) => Out (Tree a) where + out = genOut tree1 :: Tree Int tree1 = Node (Node (Leaf 333333) (Leaf (-555555)))(Node (Node (Node (Leaf 888888)
TestSuite/Tests.hs view
@@ -30,7 +30,8 @@ type Transition q = (q, Char, q) -- implement 'Out' so we can pretty print -instance (Out a) => Out (FSM a) +instance (Out a) => Out (FSM a) where + out = genOut --implementation needed for quickCheck generation of random values instance Arbitrary a => Arbitrary (FSM a) where @@ -52,8 +53,10 @@ -- Binary Tree data type data BinaryTree a = EmptyBTree | BNode a (BinaryTree a) (BinaryTree a) deriving (Show, Generic) -instance (Out a) => Out (BinaryTree a) +instance (Out a) => Out (BinaryTree a) where + out = genOut + instance (Arbitrary a) => Arbitrary (BinaryTree a) where arbitrary = sized arbitTree where @@ -91,8 +94,10 @@ -- Tree using record syntax data RecordTree a = RNode {val :: a, children :: [RecordTree a]} deriving (Show, Generic) -instance (Out a) => Out (RecordTree a) +instance (Out a) => Out (RecordTree a) where + out = genOut + instance (Arbitrary a) => Arbitrary (RecordTree a) where arbitrary = sized arbitTree where @@ -114,7 +119,9 @@ -- tree using infix notation data InfixTree a = ILeaf a a | (InfixTree a) :*: (InfixTree a) | (InfixTree a) :+: (InfixTree a) deriving (Show, Generic) -instance (Out a) => Out (InfixTree a) + +instance (Out a) => Out (InfixTree a) where + out = genOut instance (Arbitrary a) => Arbitrary (InfixTree a) where arbitrary = sized arbitTree @@ -137,7 +144,9 @@ -- infix and record tree, also uses a second user defined type in it's definition, 'Wrap' data InfixRecordTree a = IRLeaf (Wrap a) | (:^:) {left :: InfixRecordTree a, right :: InfixRecordTree a} deriving (Show, Generic) -instance (Out a) => Out (InfixRecordTree a) + +instance (Out a) => Out (InfixRecordTree a) where + out = genOut instance (Arbitrary a) => Arbitrary (InfixRecordTree a) where arbitrary = sized arbitTree @@ -161,7 +170,9 @@ -- deriving 'Out' and the code would still work, -- but the output wouldn't be identical to show because of how 'Outputable' is implemented data Wrap a = Wrap a deriving (Show, Generic) -instance Out a => Out (Wrap a) + +instance Out a => Out (Wrap a) where + out = genOut instance Arbitrary a => Arbitrary (Wrap a) where arbitrary = liftM Wrap arbitrary
Text/PrettyPrint/GenericPretty.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE TypeOperators, FlexibleInstances, FlexibleContexts, DefaultSignatures, +{-# LANGUAGE TypeOperators, FlexibleInstances, FlexibleContexts, OverlappingInstances, UndecidableInstances #-} {-| @@ -11,7 +11,7 @@ For examples of usage please see the README file. -} -module Text.PrettyPrint.GenericPretty(pp, prettyP, prettyStr, fullPP, outputTxt, outputStr, Generic, Out(..)) where +module Text.PrettyPrint.GenericPretty(pp, prettyP, prettyStr, fullPP, outputTxt, outputStr, Generic, Out(..), genOut) where import Data.List import Outputable @@ -26,15 +26,17 @@ -- | 'out' is the equivalent of Prelude.showsPrec -- it generates output identical to show, except for the extra whitespace out :: Int -> a -> SDoc - -- default out 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 SDoc's - default out :: (Generic a ,GOut (Rep a)) => Int -> a -> SDoc - out n x = sep $ out1 (from x) Pref n False - + -- | 'outList' mimics the behaviour of Prelude.showList -- used mainly to output strings correctly, and not as lists of characters outList :: Int -> [a] -> SDoc outList n xs = brackets (fsep (punctuate comma (map (out n) xs))) + +-- | default out 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 SDoc's +-- method user must use when implementing 'Out' by saying 'out = genOut' +genOut :: (Generic a ,GOut (Rep a)) => Int -> a -> SDoc +genOut n x = sep $ out1 (from x) Pref n False -- user-defined types that directly implement Outputable are handled here -- n marks wether the type needs to be surrounded by parens or not