After the long-winded discussion at Write this Scala Matrix multiplication in Haskell, I was left wondering…what would a type-safe matrix multiplication look like? So here’s your challenge: either link to a Haskell implementation, or implement yourself, the following:
data Matrix ... = ...
matrixMult :: Matrix ... -> Matrix ... -> Matrix ...
matrixMult ... = ...
Where matrixMult produces a type error at compile time if you try to multiply two matricies with incompatible dimensions. Brownie points if you link to papers or books that discuss this precise topic, and/or discuss yourself how useful/useless this functionality is.
There are a number of packages that implement this:
The Repa papers in particular have a really nice discussion of the design space and choices made: http://repa.ouroborus.net/
Of historical interest is McBride’s “Faking It” from 2001 which describes strongly typed vectors. The techniques he employs are fairly similar to those used in the above packages. They were obviously known in circles doing dependently typed programming, but my impression is that the “Faking It” paper is one of the earlier instances where these were used in Haskell. Oleg’s 2005 Monad Reader article on number-parameterized types has some good discussion on the history of these techniques as well.