Re: Strange error in show for datatype
Hi again, My question about my student's problem surely stirred an interesting and clarifying discussion. Still, I have a question. Reconsider the example. There's a data type data LispList t = Atom t | LispList [LispList t] | Str [Char] and an instance declaration instance Show t => Show (LispList t) where show (Atom t) = show t show (LispList t) = show t show (Str t) = show t So, hypothetically there could have been an additional, overlapping instance declaration, say instance Show (LispList Int) where show (Atom t) = show t show (LispList t) = show t show (Str t) = "Blahonga!" Then we cannot know whether show (Str "HEJ") should yield "HEJ" or "Blahonga!". However, my student never gave such an overlapping instance, 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. Björn Lisper
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)
participants (2)
-
Bjorn Lisper -
Patrik Jansson