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

Slavomir Kaslev slavomir.kaslev at gmail.com
Tue Nov 28 07:46:13 EST 2006


I have to define a couple of float2, float3, float4 Cg, HLSL style
vectors in Haskell. At first I was tempted to make them instances of
Num, Floating, RealFrac, etc. but some of the functions defined in
those classes have no sense for vectors. One such example is signum
from class Num.

There are several workarounds for this. One may come up with some
meaning for vectors of such functions, for example:

instance Num Float3 where
    signum a | a == Float3 0 0 0 = 0
                  | otherwise = 1

This is silly. Other option, which I prefer, is to leave such
functions undefined (that is signum=undefined, not just not defining
them). Is this ok? Are there any other options?

Another bugging thing is that some of the functions do have meaning
for vectors but they need different signatures. For example (**) ::
Floating a => a -> a -> a, for vectors should be (**) :: (Floating a,
Vector v) => v -> a -> v, that is (**) applied for every component of
the vector. Any workarounds for that?

I know that I can scrap all those Num, Floating, RealFrac, etc.
classes and define class Vector from scratch, but I really don't want
to come up and use different names for +, -, etc. that will bloat the

Last question: Does haskell have something like C++ templates? For
example, some time in the future I may need types like int2, short3,
etc., that behave just like float2, float3, but use different types
for their components. I really, really wouldn't like to copy-paste the
definitions of floatn and manually change their types to intn


Slavomir Kaslev

More information about the Haskell-Cafe mailing list