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?
types algebraic-data-types naming-conventions haskell type-alias
mk12
source share