Novice Haskell query

Mark Carroll mark@chaos.x-philes.com
Tue, 31 Jul 2001 17:17:10 -0400 (EDT)


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? I don't seem to be able to write, say,

  instance Show a => Show (Tree a) where
      show (Branch datum left right) =
        show left ++ " <- " ++ show datum ++ " -> " ++ show right
  
  instance Show (Tree a) where
      show Leaf = "*"

or, perhaps,

  instance Show (Tree a) where
      Show a => show (Branch datum left right) =
        show left ++ " <- " ++ show datum ++ " -> " ++ show right
      show Leaf = "*"

...or whatever other things come to mind.

-- Mark