Good afternoon. I am new to Haskell. It is not clear to me that you need to declare and instantiate some custom classes.
Haskell has a standard Integral class. According to the hack, Integral declares the required method quot :: a -> a -> a . So this means that every instance of this class must have an implementation of this method, right?
We can declare some function using Integral as an argument, for example:
proba :: (Integral a) => a -> a -> a proba xy = x `quot` y
So far so good
- Now let's declare our own Proba class:
class Proba a where proba :: a -> a -> a
I can implement an instance of Int or Integer (or another data type), for example:
instance Proba Integer where proba xy = x `quot` y instance Proba Int where proba xy = x `quot` y
But I dont want. I want one instance for each Integral. But when I try to do this, I get an error message:
instance (Integral a) => Proba a where proba xy = x `quot` y Illegal instance declaration for `Proba a' (All instance types must be of the form (T a1 ... an) where a1 ... an are *distinct type variables*, and each type variable appears at most once in the instance head. Use FlexibleInstances if you want to disable this.) In the instance declaration for `Proba a'
Well, it seems like it is asking me for different type variables instead of classes. But why?! Why is this not enough to just have Integral here? Since quot declared for each Integral , this instance must be valid for each Integral , shoudn't it?
Maybe there is a way to achieve the same effect?
source share