[Haskell-beginners] Design Question

Jonathan Fischoff jonathangfischoff at gmail.com
Wed Jan 13 20:54:51 EST 2010

I am writing graphics code, and there is a pattern that I am repeating. I
imagine that there could be some way to automate the code that I am

I have a collection of type classes like the ones below (minus fundeps, and
other elements to make it compile).

class Vertex a ...

class (Coordinate b, Vertex a) => Positionable a b where
     getPosition :: a -> b

class (Vertex a) => Colorable a where
     getColor :: a -> Color

class (Vertex a) => Textureable a where
     getUVs :: a -> (Double, Double)

etc... Basically I have a bunch of type classes with one method getSomeType.

The following would be an instance of all of the type classes:
data ColorVertex = ColorVertex Cart3Coordinate Color (Double, Double)

Additionally I have collection type classes as follows:

class (Coordinate b) => PositionCollection a b where
     getPositionList :: a -> [b]

class ColorList a where
     getColorList :: a -> [Color]

class UVList a where
     getUVList :: a -> [(Double, Double)]

So, for every getSomeType class, there is a getSomeTypeList class. An
instance of these types would look like

data StreamCollection3 = StreamCollection3 [Cart3Coordinate] [Color]
[(Double, Double)]

add it would also an instance of the following class, and I would like to
figure out how to generate the functions for the class.

class (Vertex b) => VertexCollection  a b where
    getElement :: a -> Int -> b
    getVertexList :: a -> [b]
    fromVertexList :: [b] -> a

The Vertex in this case would be ColorVertex.

I was thinking I could reduce the code by making the classes

class Gets a b where
     gets :: a -> b

class GetsList a b where
     getsList :: a -> [b]

But I think I could go further. I'm wondering if there is a way to
completely automate the construction of these classes, data types and
methods, such that all I would provide is definition of the element type
such as

data PositionVertex = PositionVertex Cart3Coordinate

and everything else would get generated.

I can see how I would write code to generate this. Is the time to learn
Template Haskell, or is there another way to generate my boilerplate?

-Jonathan Fischoff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100113/ade6c41f/attachment.html

More information about the Beginners mailing list