下面的代码犯规编译:哈斯克尔类依赖性
class Foo f where
getInt :: f -> Int
class Bar b where
getFooFromBar :: Foo f => b -> f
someFunction :: Bar b => b -> Int
someFunction bar = getInt $ getFooFromBar bar
的错误是Could not deduce (Foo f) arising from a use of 'getInt' from the context (Bar b)
我知道我可以通过改变类Bar
修复错误如下:
class Foo f => Bar f b where
getFooFromBar :: b -> f
但我宁愿如果我不必将f
添加到Bar
的所有实例签名。
有没有办法做到这一点,只保留Foo f
约束只有getFooFromBar
签名,而不是整个班?
我建议你重新考虑getFooFromBar'的'类型 - 它看起来蓄势的'暧昧类型变量“错误。 – Alec
@Alec完整的错误确实说'类型变量'f0'是不明确的。 – duplode
[理解一个Haskell类型歧义的案例]的可能的重复(http://stackoverflow.com/questions/21220655/understanding-a-case-of-haskell-type-ambiguity) – Cactus