mercredi 25 mars 2015

Is it possible to "bake dimension into a type" in haskell?


Suppose I want to write a library that deals with vectors and matrices. Is it possible to bake the dimensions into the types, so that operations of incompatible dimensions generate an error at compile time?


For example I would like the signature of dot product to be something like



dotprod :: Num a, VecDim d => Vector a d -> Vector a d -> a


where the d type contains a single integer value (representing the dimension of these Vectors).


I suppose this could be done by defining (by hand) a separate type for each integer, and group them in a type class called VecDim. Is there some mechanism to "generate" such types?


Or perhaps some better/simpler way of achieving the same thing?





Aucun commentaire:

Enregistrer un commentaire