Num instances for 2-dimensional types Classic List Threaded 44 messages 123
Open this post in threaded view
|

Num instances for 2-dimensional types

 Hi! I often stumble upon 2- (or 3-) dimensional numerical data types like     (Double, Double) or similar self defined ones. I like the idea of creating instances for Num for these types. The meaning of (+), (-) and negate is clear and very intuitive, i think. I don't feel sure about (*), abs, signum and fromInteger. I used to implement     fromInteger n = (r, r) where r = fromInteger n , but thinking about it,     fromInteger n = (fromInteger n, 0) seems very reasonable, too. Any thoughts on that? How would you do it? btw: These are two examples i looked at in hackage: Data.Complex.Complex (which is special, cause it is mathematically defined, what (*), abs, signum and fromInteger should do (i think)) and Physics.Hipmunk.Common.Vector (http://hackage.haskell.org/packages/archive/Hipmunk/5.0.0/doc/html/Physics-Hipmunk-Common.html#9) Sönke _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 You are in luck!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:instance (Num a) => Num (Vector2 a) where   a + b = pure (+) <*> a <*> b  a - b = pure (-) <*> a <*> b  a * b = pure (*) <*> a <*> b  negate a = pure negate <*> a  abs a = pure abs <*> a  signum = fmap signum   fromInteger = pure . fromIntegerIf you want to define a Num instance for _all_ applicatives, you can do this (you'll need a couple extensions):instance (Num a, Applicative f, Eq (f a), Show (f a)) => Num (f a) where   a + b = pure (+) <*> a <*> b   a - b = pure (-) <*> a <*> b   a * b = pure (*) <*> a <*> b   negate a = pure negate <*> a   abs a = pure abs <*> a   signum = fmap signum   fromInteger = pure . fromIntegerI am currently working on a vector and matrix library for haskell that uses instances of this form, which you can find here: http://github.com/jvranish/VectorMatix.  The matrix half is very unfinished, but the vector half is pretty much done. Applicative is a pretty fantastic typeclass, it's definitly worth the time to figure out how it works.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...) Hope that helps :)- JobOn Mon, Oct 5, 2009 at 8:40 AM, Sönke Hahn wrote: Hi! I often stumble upon 2- (or 3-) dimensional numerical data types like    (Double, Double) or similar self defined ones. I like the idea of creating instances for Num for these types. The meaning of (+), (-) and negate is clear and very intuitive, i think. I don't feel sure about (*), abs, signum and fromInteger. I used to implement    fromInteger n = (r, r) where r = fromInteger n , but thinking about it,    fromInteger n = (fromInteger n, 0) seems very reasonable, too. Any thoughts on that? How would you do it? btw: These are two examples i looked at in hackage: Data.Complex.Complex (which is special, cause it is mathematically defined, what (*), abs, signum and fromInteger should do (i think)) and Physics.Hipmunk.Common.Vector (http://hackage.haskell.org/packages/archive/Hipmunk/5.0.0/doc/html/Physics- Hipmunk-Common.html#9) Sönke _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 In reply to this post by Soenke Hahn Sönke Hahn wrote: > I used to implement > >     fromInteger n = (r, r) where r = fromInteger n > > , but thinking about it, > >     fromInteger n = (fromInteger n, 0) > > seems very reasonable, too. Stop pretending something is a number when it's not. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 That's not gonna happen until when/if Haskell supports name/operator   overloading. There's a scarcity of good symbols/function names and   everyone wants to use them. So naturally, type class abuse follows. Regards, John A. De Goes N-Brain, Inc. The Evolution of Collaboration http://www.n-brain.net    |    877-376-2724 x 101 On Oct 5, 2009, at 7:12 AM, Miguel Mitrofanov wrote: > > > Sönke Hahn wrote: > >> I used to implement >>    fromInteger n = (r, r) where r = fromInteger n >> , but thinking about it,     fromInteger n = (fromInteger n, 0) >> seems very reasonable, too. > > Stop pretending something is a number when it's not. > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe_______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 In reply to this post by MigMit And what is a number?  Are complex numbers numbers? On Mon, Oct 5, 2009 at 3:12 PM, Miguel Mitrofanov <[hidden email]> wrote: > > > Sönke Hahn wrote: > >> I used to implement >> >>    fromInteger n = (r, r) where r = fromInteger n >> >> , but thinking about it, >>    fromInteger n = (fromInteger n, 0) >> >> seems very reasonable, too. > > Stop pretending something is a number when it's not. > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe> _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 In reply to this post by MigMit In what way is it not a number?data MyNumber a = MyNum a a   deriving (Show, Eq)instance Functor MyNum where  fmap f (MyNum a b) = MyNum (f a) (f b)instance Applicative MyNum where  pure a = MyNum a a   MyNum f g <*> MyNum a b = MyNum (f a) (g b)  instance (Num a) => Num (MyNum a) where  a + b = pure (+) <*> a <*> b  a - b = pure (-) <*> a <*> b  a * b = pure (*) <*> a <*> b   negate a = pure negate <*> a  abs a = pure abs <*> a  signum = fmap signum  fromInteger = pure . fromIntegerThis instance obeys the commutative, distributive, and associative laws,  and the multiplicative, and additive identities. (at least, if the numbers it contains satisfy those laws) How is MyNum not a number?Sönke Hahn:   btw, I forgot to mention in my first email, but       fromInteger n = (r, r) where r = fromInteger n  is better than:      fromInteger n = (fromInteger n, 0)  as you get a lot of corner cases otherwise. I use fromInteger = pure . fromInteger, which when combined with my Applicative instance, is effectively the same as your:  fromInteger n = (r, r) where r = fromInteger n   - JobOn Mon, Oct 5, 2009 at 9:12 AM, Miguel Mitrofanov wrote: Sönke Hahn wrote: I used to implement    fromInteger n = (r, r) where r = fromInteger n , but thinking about it,    fromInteger n = (fromInteger n, 0) seems very reasonable, too. Stop pretending something is a number when it's not. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 In reply to this post by Lennart Augustsson Lennart Augustsson wrote: > And what is a number? Can't say. You know, it's kinda funny to ask a biologist what it means to be alive. > Are complex numbers numbers? Beyond any reasonable doubt. Just like you and me are most certainly alive. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 But complex numbers are just pairs of numbers.  So pairs of numbers can obviously be numbers then. On Mon, Oct 5, 2009 at 4:40 PM, Miguel Mitrofanov <[hidden email]> wrote: > Lennart Augustsson wrote: >> >> And what is a number? > > Can't say. You know, it's kinda funny to ask a biologist what it means to be > alive. > >> Are complex numbers numbers? > > Beyond any reasonable doubt. Just like you and me are most certainly alive. > _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 In reply to this post by Job Vranish Am Montag 05 Oktober 2009 16:29:02 schrieb Job Vranish: > In what way is it not a number? > If there's a natural implementation of fromInteger, good. If there isn't, *don't provide one*. fromInteger _ = error "Not sensible" is better than doing something strange.  In the case of residue class rings, you may choose between restricting the range of legitimate arguments for fromInteger or doing a modulo operation on the argument, both ways are natural and meet expectations sufficiently well. > > Sönke Hahn: >   btw, I forgot to mention in my first email, but >       fromInteger n = (r, r) where r = fromInteger n >   is better than: >       fromInteger n = (fromInteger n, 0) >  as you get a lot of corner cases otherwise. > >  I use fromInteger = pure . fromInteger, which when combined with my > Applicative instance, is effectively the same as your:  fromInteger n = (r, > r) where r = fromInteger n > > > - Job > > On Mon, Oct 5, 2009 at 9:12 AM, Miguel Mitrofanov <[hidden email]>wrote: > > Sönke Hahn wrote: > > > >  I used to implement > > > >>    fromInteger n = (r, r) where r = fromInteger n > >> > >> , but thinking about it, > >>    fromInteger n = (fromInteger n, 0) > >> > >> seems very reasonable, too. > > > > Stop pretending something is a number when it's not. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

Re: Num instances for 2-dimensional types

 In reply to this post by Lennart Augustsson Lennart Augustsson wrote: > Everyone agrees that the Haskell numeric hierarchy is flawed, but I've > yet to see a good replacement. >   That's because the "good replacement" which is mathematically sound would be a real mess to work with -- for exactly the same reason that Functor , Applicative and Monad are unrelated classes.  See . In a prototype language I am playing with, my version of Num depends on 15 'previous' classes, and that's likely to increase, not decrease. Jacques  http://repetae.net/recent/out/classalias.html_______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

Re: Num instances for 2-dimensional types

Open this post in threaded view
|

What is a number. (Was: Num instances for 2-dimensional types)

Open this post in threaded view
|

Re: What is a number. (Was: Num instances for 2-dimensional types)

 [hidden email] wrote: > American people will call it a discussion about "semantics", and > we, European will not understand why this word is used in a pejorative > context... "Semantics" *should* be a pejorative word unless it refers to something formally specified, and preferably executable or machine-checkable. Those subtle Contintental critics of informal semantics, namely Derrida, Irigaray, et al, showed us why. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe