Saturday, June 20, 2009

I `heart` TypeFamilies! (Wumpus)

Copperbox revision 636.

(New typeclass for Wumpus Pointwise, with an associated type).

With HNotate and then Mullein, I had a long standing problem where my data types where parametric on 'note' (usually this would really be 'pitch', sometimes drum pitch - notes themselves are actually one level up in the hierarchy). In Haskell the nice traversal type classes (Functor, Foldable, Traversable) work on the parametric tip, implementations of these type classes are just scaffolding to focus the traversal on the tip.

Unfortunately in HNotate I mostly wanted to traverse the layer above, where the syntax tree represents notes, chords, rests etc. But it wasn't useful to make this the parametric tip - I'd have to implement notes, chords and rests for all note types when they are common (I don't suppose drummers call simultaneous drum notes chords but LilyPond groups them as chords).

I had a similar problem with Wumpus - drawables (polygons, polylines etc.) are commonly built from collections of points which can be operated on with affine transformations. But Point is parametric on its number type (so they can be represented by doubles, rationals etc), this meant Wumpus had the same problem as HNotate / Mullein - the parametric tip wasn't the focus I commonly wanted to operate on.

Wumpus has simpler needs than Mullein (no Traversable, or Foldable), and I think I've solved the problem with a type class and associated type. Drawables can implement the Pointwise class which is similar to Functor except the associated type specifies the tip to operate on and needn't be the functorial parameter. Type families have won a new convert.

Blog Archive

About Me

My photo
Disambiguating biog as there are a few Stephen Tetley's in the world. I'm neither a cage fighter or yachtsman. I studied Fine Art in the nineties (foundation Bradford 1992, degree Cheltenham 1992 - 95) then Computing part-time at Leeds Met graduating in 2003. I'm the Stephen Tetley on Haskell Cafe and Stackoverflow.