以下代码段
data Tree k v = ETree | Node { leftTreeOf :: Tree k v,
rightTreeOf :: Tree k v,
tKey :: k,
tVal :: v
}
instance Show s => Show (Tree s s) where
show = showTree 0
产量
Illegal instance declaration for `Show (Tree s s)'
(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 -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Show (Tree s s)'
我查了一下,-XFlexibleInstances
提升的限制就是为了防止模糊实例的声明。
有两个类型变量如何允许一个含糊不清的案例?
instance Show s => Show (Tree s) where
show = showTree 0
当我只需要一个类型变量时,工作正常。
答案 0 :(得分:4)
对不起,我没想到。
如果其他人遇到此问题,则需要提供2个不同的类型变量才能允许2种不同的可显示类型:
instance (Show sk, Show sv) => Show (Tree sk sv) where
show = showTree 0
然后任何包含的函数(在本例中为showTree)都需要类似的签名。