Novice Haskell query
Hamilton Richards
hrichrds@swbell.net
Tue, 31 Jul 2001 18:34:41 -0500
At 4:17 PM -0500 7/31/01, Mark Carroll wrote:
>Let's say I have:
>
> data Tree a = Branch a (Tree a) (Tree a)
> | Leaf
>
> instance Show a => Show (Tree a) where
> show (Branch datum left right) =
> show left ++ " <- " ++ show datum ++ " -> " ++ show right
> show Leaf = "*"
>
> y = Leaf
> x = (Branch 3 Leaf Leaf)
>
>Now, "show x" will work, and "show y" won't. How can I write things so
>that I can show both x and y?
The error was something like this, was it not?
Script> show y
ERROR - Unresolved overloading
*** Type : Show a => [Char]
*** Expression : show y
The problem is that the context Show a => is not satisfied in the case of
y = Leaf
because the datum type isn't specified, and therefore is not an instance of
Show.
Declaring y's type, say
y :: Tree ()
solves the problem:
Script> y
*
--HR
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer Mail Code C0500
512-471-9525 The University of Texas at Austin
Taylor Hall 5.138 Austin, Texas 78712-1188
ham@cs.utexas.edu hrichrds@swbell.net
------------------------------------------------------------------