[Haskell-cafe] Re: Wondering if this could be done.

Ertugrul Soeylemez es at ertes.de
Mon Nov 22 02:07:35 EST 2010


Magicloud Magiclouds <magicloud.magiclouds at gmail.com> wrote:

>   For example, I have a data A defined. Then I want to add (+) and (-)
> operators to it, as a sugar (compared to addA/minusA). But * or other
> stuff defined in class Num is meanless to A. So I just do:
> (+) :: A -> A -> A
> (+) a b =
>   A (elem1 a + elem1 b) (elem2 a + elem2 b) -- I got errors here, for
> the (+) is ambiguous.
>
>   So, just wondering, does this way work in Haskell?

The Monoid class represents monoids, but it has no inversion function,
so you can only express addition with it.  Also its member function
names are unfortunate for almost anything other than lists.

Anyway, whenever your set is at most countably infinite, you can always
make sense of the multiplication function.  Note that multiplication is
not required to be commutative.  In the worst case you can leave it
undefined:

  instance Num A where
    _ * _ = error "Cannot multiply A values"


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/




More information about the Haskell-Cafe mailing list