Haskell type naming aliases

I wrote a mathematical Vector module in Haskell.

So, I started with:

 data Vector a = Vector !a !a !a deriving (Eq, Show) 

Fine - this allows me to use any type of numeric data that I want. The problem is that I do not want to write Double and Vector Double everywhere for the simple reason that I do not need. Therefore, I add:

 type Scalar = Double type Vector = Vector Scalar 

But, of course, the second line is incorrect, because now there are two Vector declarations. So what should I change? I think to myself, no, I'm going to write this all over my code, so I want to leave the type alias just like Vector . This means that I need to change the name of the data type. But if I change this, then I feel that I should also change the constructor, which is becoming more confusing. But if this is inconvenient, then the constructor has the same name as the type alias.

Now I have this:

 type Scalar = Double type Vector = VectorT Scalar data VectorT a = Vector !a !a !a deriving (Eq, Show) 

I chose T arbitrarily (I think it means "type"), but I'm not sure about that. Usually, when I document functions, I would say -- Calculate the magnitude of a Vector , but with VectorT it seems to me that I really should use this type name. Therefore, I resort to referring to them as vectors (not capital) - in addition, it seems to me that I should apply this convention to each comment for each data type.

Has anyone been in a similar situation? Can anyone think of a more elegant solution in this case?

+8
types algebraic-data-types naming-conventions haskell type-alias
source share
2 answers

One solution to your particular problem would be to have a data type in another module from a type synonym. That is, there is a Math.Vector module that contains data declarations and some common functions (that is, functions that work for all numeric types). Then, when you really use Vector Double in your code, just create a type synonym using qualified import:

 import qualified Math.Vector as MV type Scalar = Double type Vector = MV.Vector Scalar 

I think this makes sense in terms of organizing the code. In particular, if you have defined the Vector type to work with all numeric types, I expect that the functions of this module will also work with all numeric types. The fact that you use Vector Double lot in any other part of the code should not affect the module where Vector actually defined. In the end, it’s wise to imagine that using Vector Int you can use another part of your program.

As an aside, I'm not sure it is called Vector - the best idea. A vector does not have to have three dimensions, so I would call your data type something like Vector3D . In fact, this name is used in some other APIs (for example, the Java 3D API), so this is probably a good choice.

+8
source share

Is there any specific reason for not just doing

 data Vector = Vector !Double !Double !Double deriving (Eq, Show) 

It seems to me that this is the simplest idea ...

+2
source share

All Articles