[Haskell-beginners] Heterogeneous Lists
fuuzetsu at fuuzetsu.co.uk
Tue May 28 16:28:44 CEST 2013
-----BEGIN PGP SIGNED MESSAGE-----
On 28/05/13 15:00, harry wrote:
> Brent Yorgey <byorgey <at> seas.upenn.edu> writes:
>> On the other hand, if you sincerely want to know why, it is
>> simply that this kind of feature does not fit nicely within the
>> space of the design decisions that were made when creating
>> Haskell, for example: (1) All the elements of a list must have
>> the same type. (2) A type class cannot be used as a type. (3)
>> Haskell has no subtyping. And there are good reasons behind
>> these decisions as well. For example, subtyping makes type
>> inference nigh impossible.
> Yes, (2) is what I don't understand. I recently ran into this with
> a GUI application, where I needed to process a list of widgets that
> were members of the same typeclass, but I had to wrap them all
> because they were different types.
> So so rephrase my question, why can't type classes be used as a
> type? Is this an implementation issues, or is there a semantic
> problem with this?
Type classes can't be used as types because they are not types. It's
just that simple. To achieve what you want you actually create a new
type (with `data') and put a type class constraint on the types it
accepts. We can then use that type for our collection.
> Creating an existential type and packing all the values seems like
> busy work which shouldn't be necessary, or at least something that
> the compiler should be doing for me.
Yes, it is in fact busy work and yes, it could probably be done for
you by the compiler. I imagine the reason it isn't is simply because
forcing heterogeneous collections where only homogeneous ones were
intended is usually a sign of the fact that you are doing it wrong and
usually there's a better solution. Putting in direct support for it in
the compiler would just make the compiler more complex and effectively
encourage bad programming. It's just not something that comes up often
enough to warrant a compiler extension.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
-----END PGP SIGNATURE-----
More information about the Beginners