[Haskell-cafe] vector-space and standard API for vectors

Alexey Khudyakov alexey.skladnoy at gmail.com
Fri Oct 22 08:46:26 EDT 2010


Hello everyone!

It's well known that Num & Co type classes are not adequate for vectors 
(I don't mean arrays). I have an idea how to address this problem.

Conal Elliott wrote very nice set of type classes for vectors. 
(Definition below). I used them for some time and quite pleased. Code is 
concise and readable.

 > class AdditiveGroup v where
 >   zeroV :: v
 >   (^+^) :: v -> v -> v
 >   negateV :: v -> v
 >
 > class AdditiveGroup v => VectorSpace v where
 >   type Scalar v :: *
 >   (*^) :: Scalar v -> v -> v
 >
 > class VectorSpace v => InnerSpace v where
 >   (<.>) :: v -> v -> Scalar v

There are few problems though. Fisrt one is inlining. Functions are not 
inlined so if client code uses uses stream fusion it breaks. Second is 
dependecies. Vector-space doesn't have big chain of dependencies. People 
may be reluctant to use vector-spaces just for API for small packages.

My proposal is to split type classes above into separate package which 
provide only type classes, doesn't have any dependencies.
Second point is to lean more on performance side in performance vs 
elegance. Add INLINE pragmas everywhere (unfortunate feature of stream 
fusion), move ^-^ to AdditiveGroup with default implementation.

I'd like to know opinion of haskellers on this and specifically opinion 
of Conal Eliott as author and maintainer (I CC'ed him)


More information about the Haskell-Cafe mailing list