<div><div>Haskell does not play as well with overloading as one would do it in C++; every</div><div>name used must be fully qualified. &nbsp;Indeed, if we try something like</div></div><div><br></div><div>Indeed, if we try something like</div>
<div><br></div><div>data A = A Int deriving (Show, Eq)</div><div><br></div><div>test = A 3 unA (A i) = i</div><div><br></div><div>class Group a where (+) :: a -&gt; a -&gt; a</div><div><br></div><div>instance Group A where (+) x y = A $ unA x + unA y</div>
<div><br></div><div>we will get</div><div><br></div><div>Ambiguous occurrence `+&#39;&nbsp;</div><div><br></div><div>It could refer to either `Main.+&#39;, defined at ****.hs:7:1&nbsp;</div><div>or `Prelude.+&#39;, imported from Prelude&nbsp;</div>
<div><br></div><div>Failed, modules loaded: none.</div><div><br></div><div>Haskell has its own brand of &#39;overloading&#39;: type classes. Every (+) sign used</div><div>assumes that the operands are of the Num typeclass in particular. In order to</div>
<div>define (+) on something else you will need to instance the Num typeclass over</div><div>your A type.</div><div><br></div><div>I am not sure what you mean by &quot;the stuff defined in class Num is meanless to</div><div>
A.&quot; Strictly speaking nothing needs to be defined in a typeclass declaration</div><div>other than the required type signatures.</div><div><br></div><div>To instance the Num typeclass with A, though, assuming that A constructors take</div>
<div>something that works with Num, you would do something similar to what Miguel</div><div>posted:</div><div><br></div><div>data A = A Int deriving (Show, Eq)</div><div><br></div><div>test = A 3 unA (A i) = i</div><div><br>
</div><div>instance Num A where (+) x y = A $ (unA x) + (unA y) (-) x y = A $ (unA x) -</div><div>(unA y) (*) x y = A $ (unA x) * (unA y) abs x = A $ (unA $ abs x) signum y = A</div><div>$ (unA $ signum y) fromInteger i = A (fromInteger i)</div>
<div><br></div><div>Look at fromInteger, which must take Integer as as argument. That may be</div><div>inconvenient for you. The Awesome Prelude, referenced in Chris&#39;s post, is a way</div><div>of defining less specific version of basic types like Bool so that you have</div>
<div>more choices in defining things like fromInteger in the Num typeclass (which</div><div>must take an Integer; it is &#39;sad&#39; if that Integer refers to a grounded,</div><div>specific type).&nbsp;</div><div><br></div><div>
<div>Still, if not every one of the Num operations make sense for your A type, you</div><div>can leave them blank and get a warning.</div></div><div><br></div><div><div><div class="gmail_quote">On Sun, Nov 21, 2010 at 10:48 PM, Magicloud Magiclouds <span dir="ltr">&lt;<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br>
 &nbsp;For example, I have a data A defined. Then I want to add (+) and (-)<br>
operators to it, as a sugar (compared to addA/minusA). But * or other<br>
stuff defined in class Num is meanless to A. So I just do:<br>
(+) :: A -&gt; A -&gt; A<br>
(+) a b =<br>
 &nbsp;A (elem1 a + elem1 b) (elem2 a + elem2 b) -- I got errors here, for<br>
the (+) is ambiguous.<br>
<br>
 &nbsp;So, just wondering, does this way work in Haskell?<br>
<font color="#888888">--<br>
竹密岂妨流水过<br>
山高哪阻野云飞<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</font></blockquote></div><br></div></div>