> so I want to create a proper set of linear algebra tools to
> write a ray-tracer
> in haskell, this includes a few types, a Point and a Vector
> both of which can be
> represented by 3 Floats, I would like the following operations..
> Vector+Vector = Vector
> Point + Vector = Point
> Point + Point = [Invalid should be compile time error]
> Point - Point = Vector
> Vector - Vector = Vector
> Vector * Float = Vector
> Point * Float = [Error!]
Haskell doesn't let you do this. The Num class has a signature
like
class Num a where
(+) :: a -> a -> a
(*) :: a -> a -> a
...
That is, (+) is overloaded, but only over operations that take
two arguments, and return a result, all of the same type.
If you had multi-parameter type classes (which both GHC and Hugs
support) ou could try
class MyNum a b where
myplus :: a -> b -> b
...
but that template doesn't fit all your examples either. And if
you go all the way to
class MyNum a b c where
myplus :: a -> b -> c
you'll get horrible ambiguity problems... what does it mean to
say
x + (y + z)
Many people would like Haskell to support 'ad hoc' overloading
in addition to its current type-class-based overloading ... but
that seems a big step to take.
> also on a more general question... how much slower can i
> expect a say raytracer
> to be in haskell than C? I am experementing with new
> algorithms so Haskell seems
> very attractive in that reguard, however speed is very
> important to as this is
> the type of application that would run for several hours in
> optimized C... how
> much of of hit can i expect? is it always a constant overhead
> or are there cases
> where the Haskell runtime can cause worse overhead? thanks..
I think you'd get a fairly big hit compared with a reasonably
optimised C program. A factor of 3-10 I expect. Conceivably
more. But don't let that put you off. A common reaction is that
performance didn't turn out to be a problem in the end ... or even
if it did then the C program was vastly easier to write because
all the algorithmic debugging had been done.
If you get something and it's slow I'll take a look at it.
I'm always keen to find niches where we can improve GHC's peformance.
Simon