How overload operator in Haskell?
Andrew J Bromage
ajb@spamcop.net
Thu, 10 Jul 2003 10:58:51 +1000
G'day all.
On Wed, Jul 09, 2003 at 05:25:20PM +0200, Jerzy Karczmarczuk wrote:
> While this is a possible solution, I would shout loudly: "Arrest this man,
> he is disrespectful wrt math!". Actually, this shows once more that the Num
> class and its relatives is a horror...
Yup.
I recently discovered, to my delight, that single-method typeclasses
with no superclasses are represented as a single unboxed function in
GHC (i.e. the type dictionary is "newtype" rather than "data").
This suggests that wrapping each "standard" mathemtaical
function/operator in its own typeclass would have literally
no run-time performance penalty:
class Plus a b c | a b -> c where
(+) :: a -> b -> c
class Mult a b c | a b -> c where
(*) :: a -> b -> c
{- etc -}
class (Eq a, Show a,
Plus a a a, Mult a a a, {- etc -}
) => Num a
Apart from the possibility of naming these typeclasses better, this
reorganisation gets my vote for Haskell 2.
> Signum in this context has no sense.
An even weirder example is that of the two real number libraries
in haskell-libs. Computable reals are definitely numbers, but they're
not even members of Eq.
Cheers,
Andrew Bromage