[Haskell-cafe] [Haskell] Defining Cg, HLSL style vectors in Haskell

Slavomir Kaslev slavomir.kaslev at gmail.com
Wed Nov 29 14:53:41 EST 2006


On 11/29/06, Krasimir Angelov <kr.angelov at gmail.com> wrote:
> Hi Slavomir,
>
> On 11/28/06, Slavomir Kaslev <slavomir.kaslev at gmail.com> wrote:
> > instance Num Float3 where
> >    .....
> >    signum a | a == Float3 0 0 0 = 0
> >                  | otherwise = 1
>
> signum has a natural generalization for vectors.
>
> signum v = vector with the same direction as v but with |v| = 1
>
> where |v| is the absolute length of v. The problematic function in Num
> is abs. Ideally abs should be defined as:
>
> abs v = |v|
>
> but its type is Float3 -> Float while the Num class requires Float3 -> Float3.
>

You mean signum = normalize? What do you think of my comments here:

> After giving some thought on signum, I got to the point, that signum
> should be defined so that abs x * signum x = x holds. So it can be
> defined as signum (Vec2 x y) = Vec 2 (signum x) (signum y).

> It turns out that all the functions in Num, Floating, etc. classes can
> be given meaningful definitions for vectors in this pattern. That is f
> (Vecn x1 x2 .. xn) = Vecn (f x1) ... (f xn). And all expected laws
> just work. One can think of that like the way SIMD processor works, it
> does the same operations as on floats but on four floats at parallel.

I think this is the way to define vector instances for Num, Floating,
etc. For vector specific operations, such as normalize, len, dot,
cross, etc. are declared in class Vector.

-- 
Slavomir Kaslev


More information about the Haskell-Cafe mailing list