On 20-May-1999, Patrick Logan <[EMAIL PROTECTED]> wrote:
> Fergus Henderson writes:
> >
> > For that kind of thing, you should use type classes and, if need be,
> > existential types.
>
> Hmm. Thanks. I misunderstood what "ad hoc polymorphism" is. I thought
> ad hoc polymorphism in the new Haskell was accomplished by the use of
> existential types.
>
> What is it that existential types allow me to do? I thought they allow
> me to define different arbitrary function bodies for the same type
> signature and dynamically invoke those functions from a single call
> site.
Almost. Type classes and existential types let you do almost that.
The signatures of the different bodies will not be exactly the same,
but they must all have compatible. (E.g. for a method with type
`t -> Int', all instances must have a type that is compatible
with that, but the actual types of instances might be `Char -> Int',
`Float -> Int', etc.)
> Is this not ad hoc polymorphism?
No; at least, that's not how I understand the term.
Ad hoc polymorphism doesn't give you dynamic binding.
Ad hoc overloading by itself doesn't even give you polymorphism
except in a very weak sense.
The terminology is not particularly consistent, though.
In particular the original paper on Haskell type classes
used the phrase "ad hoc polymorphism" in the title
(I think it was "type classes: a disciplined approach to
ad hoc polymorphism" or something along those lines),
and that term has often been used to describe Haskell type classes.
Personally I think it would be better to describe Haskell type classes
as an alternative to ad hoc polymorphism, not an instance of it.
The whole idea with Haskell type classes is that they have to be
used in a disciplined way rather than in an ad hoc way.
With ad-hoc polymorphism, you can get polymorphism between things
that just happen to be the same, but with Haskell type classes you
pretty much have to design it in in the first place.
If you combine parametric polymorphism and ad-hoc polymorphism
(e.g. Ada overloading + generics or C++ overloading + templates)
then the combination gives you something which is very close to
dynamic binding. But in Ada and C++ you don't actually get dynamic
binding, instead you only get compile-time (Ada) or link-time (C++)
binding. Haskell type classes go one step further and do give you
dynamic binding in certain cases (particularly, but not only, with
existential types).
--
Fergus Henderson <[EMAIL PROTECTED]> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED] | -- the last words of T. S. Garp.