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 creating.<div><br></div><div>I have a collection of type classes like the ones below (minus fundeps, and other elements to make it compile).</div>
<div><br></div><div>class Vertex a ...</div><div><br></div><div>class (Coordinate b, Vertex a) =&gt; Positionable a b where</div><div>     getPosition :: a -&gt; b</div><div><br></div><div>class (Vertex a) =&gt; Colorable a where</div>
<div>     getColor :: a -&gt; Color</div><div><br></div><div>class (Vertex a) =&gt; Textureable a where</div><div>     getUVs :: a -&gt; (Double, Double)</div><div><br></div><div>etc... Basically I have a bunch of type classes with one method getSomeType.</div>
<div><br></div><div>The following would be an instance of all of the type classes:</div><div>data ColorVertex = ColorVertex Cart3Coordinate Color (Double, Double)</div><div><br></div><div>Additionally I have collection type classes as follows:</div>
<div><br></div><div>class (Coordinate b) =&gt; PositionCollection a b where</div><div>     getPositionList :: a -&gt; [b]</div><div><br></div><div>class ColorList a where</div><div>     getColorList :: a -&gt; [Color]</div>
<div><br></div><div>class UVList a where</div><div>     getUVList :: a -&gt; [(Double, Double)]</div><div><br></div><div>So, for every getSomeType class, there is a getSomeTypeList class. An instance of these types would look like</div>
<div><br></div><div>data StreamCollection3 = StreamCollection3 [Cart3Coordinate] [Color] [(Double, Double)]</div><div><br></div><div>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.</div>
<div><br></div><div>class (Vertex b) =&gt; VertexCollection  a b where</div><div>    getElement :: a -&gt; Int -&gt; b</div><div>    getVertexList :: a -&gt; [b]</div><div>    fromVertexList :: [b] -&gt; a</div><div><br></div>
<div>The Vertex in this case would be ColorVertex.</div><div><br></div><div>I was thinking I could reduce the code by making the classes</div><div><br></div><div>class Gets a b where</div><div>     gets :: a -&gt; b</div>
<div><br></div><div>class GetsList a b where</div><div>     getsList :: a -&gt; [b]</div><div><br></div><div>But I think I could go further. I&#39;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</div>
<div><br></div><div>data PositionVertex = PositionVertex Cart3Coordinate</div><div><br></div><div>and everything else would get generated. </div><div><br></div><div>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?</div>
<div><br></div><div>-Jonathan Fischoff</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>