Strange error in show for datatype

Patrik Jansson patrikj@cs.chalmers.se
Fri, 5 Oct 2001 12:13:34 +0200 (MET DST)


On Fri, 5 Oct 2001, Bjorn Lisper wrote:
> My question about my student's problem surely stirred an interesting and
> clarifying discussion. Still, I have a question.
> Reconsider the example.
...
> only the first. So far as I can see there should relly be no ambiguity here!
> I'd really like to know what the cause of the problem is.

The first thing to note is that the compiler will not inspect the code in
your instance, so it has to take the safe route and assume that any
Haskell expression could be used to define show. And as there are examples
which give differenmt results depending on the type, the result will be
considered ambiguous even though your specific show instance would give
the same result for each type.

I just adjusted one the other examples in the thread to your specific case
showing how you can get different results depending on the type.

/Patrik


data LispList t = Atom t | LispList [LispList t] | Str [Char]

instance Show t => Show (LispList t) where
    show x = case x of
      Atom t     -> show t
      LispList t -> show t
      Str t      -> show (f x)

f :: LispList a -> [a]
f _ = []

t = Str "hello"

test1 = show (t::LispList Int)
test2 = show (t::LispList Char)

main = print (test1, test2, test1==test2)