You are in luck!<br><br>Such an instance is very simple with Applicative. If the type you want a Num instance for is a member of the Applicative type class you can define it like this:<br><br>instance (Num a) => Num (Vector2 a) where<br>
a + b = pure (+) <*> a <*> b<br> a - b = pure (-) <*> a <*> b<br> a * b = pure (*) <*> a <*> b<br> negate a = pure negate <*> a<br> abs a = pure abs <*> a<br> signum = fmap signum<br>
fromInteger = pure . fromInteger<br><br>If you want to define a Num instance for _all_ applicatives, you can do this (you'll need a couple extensions):<br><br>instance (Num a, Applicative f, Eq (f a), Show (f a)) => Num (f a) where<br>
a + b = pure (+) <*> a <*> b<br>
a - b = pure (-) <*> a <*> b<br>
a * b = pure (*) <*> a <*> b<br>
negate a = pure negate <*> a<br>
abs a = pure abs <*> a<br>
signum = fmap signum<br>
fromInteger = pure . fromInteger<br><br>I am currently working on a vector and matrix library for haskell that uses instances of this form, which you can find here: <a href="http://github.com/jvranish/VectorMatix">http://github.com/jvranish/VectorMatix</a>. The matrix half is very unfinished, but the vector half is pretty much done.<br>
<br>Applicative is a pretty fantastic typeclass, it's definitly worth the time to figure out how it works.<br><br>However, this technique won't work with tuples as they don't behave as Functors in the way you would like. (too many type parameters, tuples don't force all elements to be the same type so maps don't work, etc...)<br>
<br>Hope that helps :)<br><br>- Job<br><br><br><br><div class="gmail_quote">On Mon, Oct 5, 2009 at 8:40 AM, Sönke Hahn <span dir="ltr"><<a href="mailto:shahn@cs.tu-berlin.de">shahn@cs.tu-berlin.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi!<br>
<br>
I often stumble upon 2- (or 3-) dimensional numerical data types like<br>
<br>
(Double, Double)<br>
<br>
or similar self defined ones. I like the idea of creating instances for Num for<br>
these types. The meaning of (+), (-) and negate is clear and very intuitive, i<br>
think. I don't feel sure about (*), abs, signum and fromInteger. I used to<br>
implement<br>
<br>
fromInteger n = (r, r) where r = fromInteger n<br>
<br>
, but thinking about it,<br>
<br>
fromInteger n = (fromInteger n, 0)<br>
<br>
seems very reasonable, too.<br>
<br>
Any thoughts on that? How would you do it?<br>
<br>
btw: These are two examples i looked at in hackage:<br>
Data.Complex.Complex (which is special, cause it is mathematically defined,<br>
what (*), abs, signum and fromInteger should do (i think))<br>
<br>
and<br>
<br>
Physics.Hipmunk.Common.Vector<br>
(<a href="http://hackage.haskell.org/packages/archive/Hipmunk/5.0.0/doc/html/Physics-%0AHipmunk-Common.html#9" target="_blank">http://hackage.haskell.org/packages/archive/Hipmunk/5.0.0/doc/html/Physics-<br>
Hipmunk-Common.html#9</a>)<br>
<br>
<br>
Sönke<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>
</blockquote></div><br>