Учитывая следующие типы данных из TypeClassopedia:Типы Алгебраические данных и равенство
data Cons a = Cons a (Cons a) | Empty deriving (Show, Eq)
Я осуществил свою злую реализацию Functor:
instance Functor Cons where
fmap _ Empty = Empty
fmap f (Cons x xs) = Cons (f x) (Cons (f x) (fmap f xs))
Затем я попытался написать функцию (QuickCheck недвижимость), который принимает Cons a
и возвращает Bool
:
prop_id_functor_law :: Cons a -> Bool
prop_id_functor_law x = fmap id x == id x
Однако я получаю ошибку компиляции:
Prelude> :l EvilFunctor
[1 of 1] Compiling EvilFunctor (EvilFunctor.hs, interpreted)
EvilFunctor.hs:18:23:
No instance for (Eq a) arising from a use of `=='
Possible fix:
add (Eq a) to the context of
the type signature for prop_id :: Cons a -> Bool
In the expression: fmap id x == id x
In an equation for `prop_id': prop_id x = fmap id x == id x
Failed, modules loaded: none.
Моей грубая интуиции, что это ошибка времени компиляции имеет смысл. Как можно сравнить два a
, если они не использовали Eq
?
Однако, что сделал deriving ... Eq
, даже если я определил data Cons a
?