I am learning Haskell. Imagine I have the following:
data Coordinate = Coordinate double
I wish to implement a semigroup instance for Coordinate.
instance Semigroup (Coordinate a) where
Coordinate a <> Coordinate b = Coordinate (a+b)
The typechecker is displeased at me:
• Expected kind ‘* -> *’, but ‘Coordinate’ has kind ‘*’
• In the first argument of ‘Semigroup’, namely ‘(Coordinate a)’
In the instance declaration for ‘Semigroup (Coordinate a)’
|
175 | instance (Num a) => Semigroup (Coordinate a) where
(I know that this is just an empty container for a double and I could already be using just the double itself, but I am learning Haskell and I would like to understand how this works.)
The way you specified
Coordinate, it doesn't have any type parameters. So the semigroup instance head should be simplyAlternatively, you can give it a parameter to allow including different number types:
In this case, the
Semigroupinstance will need to mention the parameter, however just calling itawon't be enough because you can't perform+on arbitrary types. You need to either restrict it toDoublethereor to an arbitrary numerical type
Note that in either case,
Semigroupmay not be the best class for the purpose, consider usingAdditiveGroupand then you can also make it aVectorSpace.