[Haskell-cafe] Decoupling OpenAL/ALUT packages from OpenGL

David Duke djd at comp.leeds.ac.uk
Mon May 11 09:30:55 EDT 2009

> Am Montag, 4. Mai 2009 13:33:33 schrieb David Duke:
>> Decoupling basic primitives for geometric modelling from OpenGL would be
>> useful. [...]
>> Even just data constructors and instances of these within Functor and
>> Applicative are a useful starting point. [...]
> This leads me to the conclusion that I should only lift the data types for 
> vectors and matrices out of the OpenGL package, including only instances for 
> standard type classes like Eq, Ord, Functor, etc. This means that the new 
> package will *not* include type classes for things like scalars, vector 
> spaces, etc. These can be defined by the other packages in their own "type 
> class language". 
That seems a reasonable step.  If and when consensus does emerge on 
packaging vector & matrix operations, that could be added as a further 
> Regarding Functor/Applicative: The obvious instances for e.g. a 2-dimensional 
> vertex are:
>    data Vertex2 a = Vertex2 a a
>    instance Functor Vertex2 where
>       fmap f (Vertex2 x y) = Vertex2 (f x) (f y)
>    instance Applicative Vertex2 where
>       pure a = Vertex2 a a
>       Vertex2 f g <*> Vertex2 x y = Vertex2 (f x) (g y)
> They fulfill all required laws, but are these the only possible instances? If 
> not, are they at least the most "canonical" ones in a given sense? And 
> finally: Does somebody have a real-world example where the Applicative 
> instance is useful? Usages of the Functor instance are much more obvious for 
> me.
The Vertex constructor and Applicative operators don't seem to admit 
anything different that is also sensible (unless someone has a use for 
<*> with function and/or args permuted).  As to real-world example, if 
you interpret a vertex as a (position) vector and want to apply that to 
another vertex, liftA2 (+) is neat.  For working with sampled data, we 
have something like

class Interp b where
  interpolate :: Float -> b -> b -> b

with suitable instances for types in the numeric hierarchy, and then

instance (Interp a, Applicative f) => Interp (f a) where
  interp t = liftA2 (interp t)

If vertex is an instance of applicative, we then immediately have 
interpolation between coordinates (we use it in contour and surface 
extraction, others may find it useful in animation or distortion).


Dr. David Duke                      E: djd at comp.leeds.ac.uk
School of Computing                 W: www.comp.leeds.ac.uk/djd/
University of Leeds                 T: +44 113 3436800
Leeds, LS2 9JT, U.K.

More information about the Haskell-Cafe mailing list