[Haskell-cafe] Re: type class question

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Mon Dec 10 10:45:45 EST 2007

Peter Padawitz wrote:
> Jules Bean wrote:
>> I don't see why!
>> In the class
>> class Foo a where
>>   f :: a -> Int
>>   g :: b -> Integer
>>   g = fromIntegral . f
>> The equations within the class are defaults, not equations. 
> I must admit that I didn't know this... Nevertheless, won't you agree that 
> the default and the actual instance should be semantically equivalent?

It depends on the class, or maybe on your notion of semantical
equivalence. As an example, look at the Show class. Its interface is

> class Show a where
>   showsPrec :: Int -> a -> ShowS
>   show :: a -> String
>   showList :: [a] -> ShowS

showsPrec has a default implementation in terms of show, and show
a default implementation in terms of showsPrec. Instances may refine
showsPrec but should still satisfy  show x = shows x "".

However, the most interesting function here is showList. It comes
with a default implementation that renders a list as "[item1,...]".

showList is used in the Show instance for lists:

> instance Show a => Show [a]  where
>     showsPrec _         = showList

By redefining showList for Char, we get a prettier representation
for String values.


More information about the Haskell-Cafe mailing list