[Haskell-cafe] Wondering if this could be done.
miguelimo38 at yandex.ru
Mon Nov 22 03:29:23 EST 2010
Sure, you can define your own type class like that:
import Prelude hiding ((+), (-)) -- usual (+) and (-) shouldn't be here...
import qualified Prelude as P -- but they still are accessible with a prefix
class Group a where
(+) :: a -> a -> a
(-) :: a -> a -> a
instance Group Integer where
(+) = (P.+)
(-) = (P.-)
instance Group A where
(+) a b = A (elem1 a + elem1 b) (elem2 a + elem2 b) -- works for elem1 and elem2 being of class Group - say, Integer's
(-) a b = ...
22.11.2010 9:48, Magicloud Magiclouds пишет:
> 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?
More information about the Haskell-Cafe