In Haskell, if you have a "family" of types (say, matrices of N-on-N-elements, for some N values) and a parallel family of "related" types (say, N-element vectors, for the same N values) and an operation that requires one specific type from each family (for example, multiplying the N-by-N-element matrix and the N-element column vector), is it then possible to declare the type a class for this operation?
In this specific example, I assume that it will look something like this:
class MatrixNxN m where --| Multiplication of two N-by-N-element matrices mmul :: Num a => ma -> ma -> ma --| Multiplication of an N-by-N-element matrix and an N-element column vector vmul :: Num a => ma -> va -> va
I do not know how to restrict type v . Is it possible to do something like this?
Please note that I welcome both answers to the general question about declaring a type class from several related types, as well as the answers to the specific question about declaring a type class to multiply matrix vectors. In my particular case, there is only a small, well-known set of N values โโ(2, 3, and 4), but I'm usually interested in understanding what can be encoded in a system like Haskell.
EDIT: I implemented this using MultiParamTypeClasses and FunctionalDependencies , as suggested by Gabriel Gonzalez and Mflamer below. This is what the relevant bits of my implementation looked like this:
class MatrixVectorMultiplication mv | m -> v, v -> m where vmul :: Num a => ma -> va -> va data Matrix3x3 a = ... data Vector3 a = ... instance MatrixVectorMultiplication Matrix3x3 Vector3 where vmul = ...
This is a signature of type vmul , in itself and partially applied:
vmul :: (Num a, MatrixVectorMultiplication mv) => ma -> va -> va (`vmul` v) :: Matrix3x3 Integer -> Vector3 Integer (m `vmul`) :: Vector3 Integer -> Vector3 Integer
I find it very elegant. Thanks for answers!:)