[Haskell-beginners] newtype with a typclass
allbery.b at gmail.com
Mon Mar 18 17:34:29 CET 2013
On Mon, Mar 18, 2013 at 11:43 AM, Răzvan Rotaru <razvan.rotaru at gmail.com>wrote:
> It seems that type constructors don't allow usage of typeclasses. Is it
> not possible to create polymorphic types in Haskell by declaration?
Not directly, no. It is possible to use existentials to do this — at the
cost that you lose the ability to do anything but what you have declared
about it. For example, if you were to use this to make a map of Show-able
values, the *only* thing you can do with the value is invoke "show" on it
(in particular: you cannot pull out the value directly, because the only
thing you know about it is that "show" works on it; you otherwise have no
idea what it is!). And with such a value, referential transparency means
there is no difference between this and storing the *result* of invoking
"show" on it (that is, a String) instead, and laziness means that it may
well actually store a thunk that will invoke "show" when the value is
needed instead of computing and storing the value, so that there is in fact
no difference between the two except that the type is much more complicated
and both the type and the code are correspondingly more difficult to
What problem are you trying to solve by obfuscating things in this manner?
Also note that most times when someone is trying to do this, they have
confused typeclasses with OOP; they are not, and trying to treat them as
such *will* lead you into dead ends.
brandon s allbery kf8nh sine nomine associates
allbery.b at gmail.com ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners