[Haskell-cafe] How to define an operation in terms of itself (but
of different type)?
Olex P
hoknamahn at gmail.com
Sat Jan 24 06:37:15 EST 2009
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 <lrpalmer at gmail.com> wrote:
> 2009/1/23 Brandon S. Allbery KF8NH <allbery at ece.cmu.edu>
>
>> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090124/97d12640/attachment.htm
More information about the Haskell-Cafe
mailing list