[Haskell-beginners] (Implicit) equality testing using multiple function definitions
Brandon Allbery
allbery.b at gmail.com
Wed Jul 20 21:05:07 CEST 2011
On Wed, Jul 20, 2011 at 14:38, Tom Murphy <amindfv at gmail.com> wrote:
> There has to be a reason why we've all typed "deriving (Eq)"
> again and again: because sometimes we don't want, for some OurType, to
> be able to express:
> (a :: OurType) == (b :: OurType).
>
There is a good reason, but that's not it. You're still thinking in terms
of objects that carry method dictionaries around; Haskell *is not object
oriented*, values are just values. Operations are defined by types, not by
values. You can't ask a value how it should be compared.
Typeclasses, which superficially look like objects/classes but aren't, are a
way around this. A typeclass actually defines a dictionary mapping types to
functions; but because values are not objects, some way needs to be provided
to pass these dictionaries where they are needed. This is the function of
contexts: they're actually function arguments.
> f :: Eq a => a -> a -> a
is a function that takes three arguments: a map of implementations, and two
values of some type. Whenever this function is invoked, the compiler passes
it the Eq dictionary entry for the appropriate type a. If there were
multiple typeclass contexts, the map would combine all of them.
It's not about hiding anything. The only reason you think there is
something to hide is because an object would carry around that information
and something would therefore have to hide it for it to not be available;
but there are no objects here, so something has to be added to carry it
around.
--
brandon s allbery allbery.b at gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110720/4538663d/attachment.htm>
More information about the Beginners
mailing list