[Haskell-cafe] Re: Wondering if this could be done.
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
instance Num A where
_ * _ = error "Cannot multiply A values"
nightmare = unsafePerformIO (getWrongWife >>= sex)
More information about the Haskell-Cafe