[Haskell-cafe] Typeclasses question in "Real World Haskell" book

Angel de Vicente angelv at iac.es
Mon Jul 26 17:25:01 EDT 2010


Hi,

>> And now that we are at it... In the next page, 152 there is the
>> following instance definition, but no explanation is (I think) given of
>> what it means:
>>
>> instance (JSON a) =>  JSON [a] where
>>
>> until then all instance definitions where of the type
>>
>> instance JSON Int where ...
>>
>> How should I read that definition?
>
> As a first approximation, read it as
>
> "for all types a which are instances of JSON, the type [a] is also an
> instance of JSON, with the following methods ..."
>
> However, instance resolution in GHC is done without taking the context
> (JSON a) into account, so for GHC it reads more like
>
> "I will view all list types as instances of the JSON class, but if you try
> to use the class instance for a list type where the element type is not an
> instance of JSON, the programme will not compile. Nor will it compile if
> you try to define another instance of JSON for any list type [e.g. String]
> - at least, if you don't turn on some language extension(s)."
>
> That is the cause of many puzzlements and problems.


thanks for this, and for the detailed explanation on my previous 
question. I keep a frosty Canarian beer for you in the fridge for 
whenever we meet... :-)

I think that I can now move on with the rest of the book. Cheers,
Ángel de Vicente
-- 
http://www.iac.es/galeria/angelv/

High Performance Computing Support PostDoc
Instituto de Astrofísica de Canarias
---------------------------------------------------------------------------------------------
ADVERTENCIA: Sobre la privacidad y cumplimiento de la Ley de Protección de Datos, acceda a http://www.iac.es/disclaimer.php
WARNING: For more information on privacy and fulfilment of the Law concerning the Protection of Data, consult http://www.iac.es/disclaimer.php?lang=en



More information about the Haskell-Cafe mailing list