Re: [Haskell-cafe] How to define an operation in terms of itself (but of different type)?

Well if telepaths on vacation... class Vector v where (^+^) :: v -> v -> v data Vector3 = V3 !Double !Double !Double instance Vector Vector3 where (V3 x1 y1 z1) ^+^ (V3 x2 y2 z2) = V3 (x1 + x2) (y1 + y2) (z1 + z2) class Matrix m where (^+^) :: m -> m -> m data Matrix3 = M3 !Vector3 !Vector3 !Vector3 instance Matrix Matrix3 where (M3 r11 r12 r13) ^+^ (M3 r21 r22 r23) = M3 (r11 ^+^ r21) (r12 ^+^ r22) (r13 ^+^ r23) Hope this is better :) So yeah... r11 ^+^ r21 just doesn't work.

2009/1/23 Olex P
Well if telepaths on vacation...
class Vector v where (^+^) :: v -> v -> v
data Vector3 = V3 !Double !Double !Double
instance Vector Vector3 where (V3 x1 y1 z1) ^+^ (V3 x2 y2 z2) = V3 (x1 + x2) (y1 + y2) (z1 + z2)
class Matrix m where (^+^) :: m -> m -> m
data Matrix3 = M3 !Vector3 !Vector3 !Vector3
instance Matrix Matrix3 where (M3 r11 r12 r13) ^+^ (M3 r21 r22 r23) = M3 (r11 ^+^ r21) (r12 ^+^ r22) (r13 ^+^ r23)
Hope this is better :) So yeah... r11 ^+^ r21 just doesn't work. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Luke's story about Num and a supposed Vector class is repeated here between Vector and Matrix classes. Either you make a class Add instanciated by both Vector and Matrix, or you use different name for each of them. Cheers, Thu

2009/1/23 Olex P
class Vector v where (^+^) :: v -> v -> v
data Vector3 = V3 !Double !Double !Double
instance Vector Vector3 where (V3 x1 y1 z1) ^+^ (V3 x2 y2 z2) = V3 (x1 + x2) (y1 + y2) (z1 + z2)
class Matrix m where (^+^) :: m -> m -> m
data Matrix3 = M3 !Vector3 !Vector3 !Vector3
instance Matrix Matrix3 where (M3 r11 r12 r13) ^+^ (M3 r21 r22 r23) = M3 (r11 ^+^ r21) (r12 ^+^ r22) (r13 ^+^ r23)
Hope this is better :) So yeah... r11 ^+^ r21 just doesn't work.
It works fine if you make Matrix3 an instance of Vector. -- Dan

2009/1/24 Dan Piponi
2009/1/23 Olex P
: class Vector v where (^+^) :: v -> v -> v
data Vector3 = V3 !Double !Double !Double
instance Vector Vector3 where (V3 x1 y1 z1) ^+^ (V3 x2 y2 z2) = V3 (x1 + x2) (y1 + y2) (z1 + z2)
class Matrix m where (^+^) :: m -> m -> m
data Matrix3 = M3 !Vector3 !Vector3 !Vector3
instance Matrix Matrix3 where (M3 r11 r12 r13) ^+^ (M3 r21 r22 r23) = M3 (r11 ^+^ r21) (r12 ^+^ r22) (r13 ^+^ r23)
Hope this is better :) So yeah... r11 ^+^ r21 just doesn't work.
It works fine if you make Matrix3 an instance of Vector. -- Dan
To paraphrase a bit, your Vector class simply means : a type is a Vector if it provides an operation having a -> a -> a has its type (and called ^+^). The Matrix class says exactly the same thing, so don't bother repeat that... and simply make Vector3 and Matrix3 instances of Vector (which could then be renamed in Add or Plus or something). Thu

On Fri, 2009-01-23 at 22:58 +0000, Olex P wrote:
Well if telepaths on vacation...
class Vector v where (^+^) :: v -> v -> v
data Vector3 = V3 !Double !Double !Double
instance Vector Vector3 where (V3 x1 y1 z1) ^+^ (V3 x2 y2 z2) = V3 (x1 + x2) (y1 + y2) (z1 + z2)
class Matrix m where (^+^) :: m -> m -> m
data Matrix3 = M3 !Vector3 !Vector3 !Vector3
instance Matrix Matrix3 where
(M3 r11 r12 r13) ^+^ (M3 r21 r22 r23) = M3 (r11 ^+^ r21) (r12 ^+^ r22) (r13 ^+^ r23)
Hope this is better :) So yeah... r11 ^+^ r21 just doesn't work.
I think you mis-understand how classes work in Haskell. They aren't like classes in OO languages. They're a little bit like interfaces in OO languages, but here we see another difference: in OO, you know an object has a certain class, so you use that to find out what a method name means. In Haskell, it's the other way around: you know a method belongs to a certain class, so you know its argument (or result) must come from an instance of that class. You can't re-use method names for multiple classes, though - you have to either use different names (e.g., div vs. (/) in Prelude) or use a common base class. jcc

On 2009 Jan 23, at 17:58, Olex P wrote:
class Vector v where (^+^) :: v -> v -> v
class Matrix m where (^+^) :: m -> m -> m
You can't reuse the same operator in different classes. Vector "owns" (^+^), so Matrix can't use it itself. You could say
instance Matrix m => Vector m where (^+^) = ...
allowing Matrix to "inherit" Vector's operator (or turn it around the other way, make it Matrix then make Vector a Matrix), but that's only linguistically possible, not necessarily mathematically sane. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

2009/1/23 Brandon S. Allbery KF8NH
On 2009 Jan 23, at 17:58, Olex P wrote:
class Vector v where (^+^) :: v -> v -> v
class Matrix m where (^+^) :: m -> m -> m
You can't reuse the same operator in different classes. Vector "owns" (^+^), so Matrix can't use it itself. You could say
instance Matrix m => Vector m where (^+^) = ...
No you can't! Stop thinking you can do that! It would be sane to do: class Vector m => Matrix m where -- matrix ops that don't make sense on vector Thus anything that implements Matrix must first implement Vector. Which is sane because matrices are square vectors with some additional structure, in some sense. Luke

Yeah guys. I confused myself. I forgot why I had to implement several "+"
operators (^+^, ^+, ^+. etc.) for Vector class. Now I've got an idea again.
Different names make a perfect sense.
Thanks a lot.
On Sat, Jan 24, 2009 at 6:34 AM, Luke Palmer
2009/1/23 Brandon S. Allbery KF8NH
On 2009 Jan 23, at 17:58, Olex P wrote:
class Vector v where (^+^) :: v -> v -> v
class Matrix m where (^+^) :: m -> m -> m
You can't reuse the same operator in different classes. Vector "owns" (^+^), so Matrix can't use it itself. You could say
instance Matrix m => Vector m where (^+^) = ...
No you can't! Stop thinking you can do that!
It would be sane to do:
class Vector m => Matrix m where -- matrix ops that don't make sense on vector
Thus anything that implements Matrix must first implement Vector. Which is sane because matrices are square vectors with some additional structure, in some sense.
Luke
participants (6)
-
Brandon S. Allbery KF8NH
-
Dan Piponi
-
Jonathan Cast
-
Luke Palmer
-
minh thu
-
Olex P