[Haskell-beginners] A few really short beginners questions
Klaus Gy
klausgy at gmail.com
Sun Oct 3 18:05:55 EDT 2010
2010/10/3, Isaac Dupree <ml at isaac.cedarswampstudios.org>:
> On 10/03/10 14:39, Klaus Gy wrote:
>> Thank You very much for the fast replies! I think I expressed myself
>> badly in the first question. What does not work is the following:
>>
>> class Test a
>>
>> instance Num a => Test a
>
> For a reason that seems pretty odd until you get used to it.
> In the instance, "Test a", or the "a" thereof, is called the "instance
> head". "Num a" is called the "context". When the compiler looks for an
> instance, it works by looking just at the "instance head": and "a"
> refers to all types. "instance Num a => Test a" says that the one and
> only instance that the class has is the one described in that instance.
> (And it only works when "a" is a Num type; for other attempted uses
> you'd get a compile error.) For various reasons, such instances aren't
> allowed by default.
>
> Note that instance Num a => Test [a] might also mean something
> different than you intended, even though it's allowed: it defines the
> one and only instance of Test on lists.
>
> -Isaac
Thanks! I could have avoided this question because I've just found out
that the details of instance declarations are all described in the
Haskell 98 report along with other restrictions I was not aware of.
So, can I assume that there is no need in praxis to declare all
instances of an existing class to instances of a new class?
fweth
More information about the Beginners
mailing list