
Suppose we have the following: data Foo x list_foo :: [x] -> Foo x foo_list :: Foo x -> [x] What would be the best way to write a Show instance? The thing that I came up with is instance (Show x) => Show (Foo x) where show foo = "list_foo " ++ show (foo_list foo) But apparently you're supposed to use the strange "showsPrec" method that I don't understand. So can somebody show me a correct instance definition for this type please?

On Tue, 2008-08-05 at 20:28 +0100, Andrew Coppin wrote:
Suppose we have the following:
data Foo x list_foo :: [x] -> Foo x foo_list :: Foo x -> [x]
What would be the best way to write a Show instance?
The thing that I came up with is
instance (Show x) => Show (Foo x) where show foo = "list_foo " ++ show (foo_list foo)
But apparently you're supposed to use the strange "showsPrec" method that I don't understand. So can somebody show me a correct instance definition for this type please?
instance Show x => Show (Foo x) where showsPrec n foo = ("list_foo "++) . shows (foo_list foo) You use the n parameter if you've got an infix operator in your syntax and you want to put parentheses around an instance of it if n has the wrong value (either too high or too low, I can never remember). jcc

You may find this helper function useful:
showAp :: Show a => Int -> String -> a -> ShowS showAp n f x = showParen (n > ap_prec) inside where inside = showString f . showString " " . showsPrec (app_prec+1) x ap_prec = 10 -- "precedence" of function application; higher than any infix operator.
You can use it like so:
instance Show x => Show (Foo x) where showsPrec n foo = showAp n "list_foo" (foo_list foo)
See the Haskell 98 report section 10.5 "Derived Instances: An Example":
http://www.haskell.org/onlinereport/derived.html
-- ryan
On Tue, Aug 5, 2008 at 8:41 PM, Jonathan Cast
On Tue, 2008-08-05 at 20:28 +0100, Andrew Coppin wrote:
Suppose we have the following:
data Foo x list_foo :: [x] -> Foo x foo_list :: Foo x -> [x]
What would be the best way to write a Show instance?
The thing that I came up with is
instance (Show x) => Show (Foo x) where show foo = "list_foo " ++ show (foo_list foo)
But apparently you're supposed to use the strange "showsPrec" method that I don't understand. So can somebody show me a correct instance definition for this type please?
instance Show x => Show (Foo x) where showsPrec n foo = ("list_foo "++) . shows (foo_list foo)
You use the n parameter if you've got an infix operator in your syntax and you want to put parentheses around an instance of it if n has the wrong value (either too high or too low, I can never remember).
jcc
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Jonathan Cast wrote:
instance Show x => Show (Foo x) where showsPrec n foo = ("list_foo "++) . shows (foo_list foo)
You use the n parameter if you've got an infix operator in your syntax and you want to put parentheses around an instance of it if n has the wrong value (either too high or too low, I can never remember).
OK. So... should the call to shows actually be showsPrec? Or does it not matter in this case? Actually, now that I think of it, my *next* problem is going to be writing a matching Read instance...

On Wed, 2008-08-06 at 19:06 +0100, Andrew Coppin wrote:
Jonathan Cast wrote:
instance Show x => Show (Foo x) where showsPrec n foo = ("list_foo "++) . shows (foo_list foo)
You use the n parameter if you've got an infix operator in your syntax and you want to put parentheses around an instance of it if n has the wrong value (either too high or too low, I can never remember).
OK. So... should the call to shows actually be showsPrec? Or does it not matter in this case?
According to http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Show.html..., shows = showsPrec 0 (which is a global function like show, not a class method). In this case, showsPrec will then further dispatch to your showList method, which has the default implementation, which will ignore the precedence parameter. So it doesn't matter in this case.
Actually, now that I think of it, my *next* problem is going to be writing a matching Read instance...
Are you familiar with Text.ParserCombinators.ReadP? http://www.haskell.org/ghc/docs/latest/html/libraries/base/Text-ParserCombin... It's an explicit monad, so it's rather easier to use that ReadS. jcc
participants (3)
-
Andrew Coppin
-
Jonathan Cast
-
Ryan Ingram