0
我觉得这应该很容易,但我坚持了很长一段时间。为数据类型列表创建实例 - Haskell
haskell有可能为某些数据类型的列表创建一个类型的实例吗?
我想实现的是以下内容。
class Rename a where
findSub :: a -> a
-- A 'normal' instance would look like this
instance Rename Atom where
findSub ......
-- Now i want to acchieve something like this
instance Rename ([] Atom) where
findSub ......
当我写这篇文章如何过我得到以下错误:
* Illegal instance declaration for `Rename [Atom]'
(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 `Rename ([] Atom)'
Failed, modules loaded: none.
我想知道如何解决这个问题,为什么这是不允许的。
在此先感谢。
我不倾向于看到'UndecidableInstances'出现在错误的代码中;我倾向于在完美的代码中看到更多,Haskell的极其有限的终止标准不足以证明是好的。 – dfeuer
@dfeuer,一旦你对类型类很熟悉,我同意,但是通常初学者往往会认为类型类的分辨率倒退或者比它强大得多(例如''实例向量=>数量类'等),这很快导致这些扩展被“需要”(尽管当它们被启用时它仍然不能按预期工作)。没有? – luqui
我倾向于更多地考虑'OverlappingInstances','IncoherentInstances','AllowAmbiguousTypes'和(当不使用多参数类型类时)'FlexibleInstances'。但你也许是对的。 – dfeuer