newtype MyNewtype1 f v = MyNewtype1 { getVal1 :: f v } deriving Eq -- OK
newtype MyNewtype2 f maybe v = MyNewtype2 { getVal2 :: f (maybe v) } deriving Eq --OK
newtype MyNewtype3 f v = MyNewtype3 { getVal3 :: f (Maybe v) } -- OK
newtype MyNewtype4 f v = MyNewtype4 { getVal4 :: f (Maybe v) } deriving Eq --NOT OK
我有这些新类型。前三个按预期工作,但第四个给出:
• No instance for (Eq (f (Maybe v)))
arising from the 'deriving' clause of a data type declaration
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (Eq (MyNewtype4 f v))
我不明白问题出在哪里。在我看来,第二种新类型严格来说更一般,因此必须遇到同样的问题。那么如果MyNewtype2
能推导出Eq,为什么MyNewtype2
不能呢?这可能是最让我困惑的地方。有人可以向我解释一下吗?另外,如果我想要这样的新类型,首选的解决方案是什么?