tom*_*tom 4 haskell purescript
我有两个我认为等效的类型类和实例定义的实现。PureScript 版本构建时没有错误,但 Haskell 版本失败并出现错误Un-determined variables: e, f。
我可以在 Haskell 中完成这个工作吗?
哈斯克尔:
class Foo a b c | a b -> c where
newtype Bar a b = Bar (a, b)
instance (Foo a c e, Foo b d f) => Foo (Bar a b) (Bar c d) (Bar e f) where
Run Code Online (Sandbox Code Playgroud)
纯脚本:
class Foo a b c | a b -> c
newtype Bar a b = Bar (Tuple a b)
instance (Foo a c e, Foo b d f) => Foo (Bar a b) (Bar c d) (Bar e f)
Run Code Online (Sandbox Code Playgroud)
您必须启用UndecidableInstances;那么你的声明就会起作用。为什么需要这样做有点微妙,即不启用它会如何导致类型检查器中的循环。但是假设在您的程序中,您以某种方式导致需要解决以下约束:
Foo (Bar a b) (Bar c d) a
Run Code Online (Sandbox Code Playgroud)
约束求解器会很高兴地观察到我们必须选择您编写的实例,并决定因此a ~ Bar a0 a1对于某些a0和a1,这意味着我们想要求解约束:
Foo (Bar (Bar a0 a1) b) (Bar c d) (Bar a0 a1)
Run Code Online (Sandbox Code Playgroud)
现在我们可以调度到上下文,这意味着我们现在需要解决以下两个约束:
( Foo (Bar a0 a1) c a0
, Foo b d a1
)
Run Code Online (Sandbox Code Playgroud)
可是等等!第一个约束与我们开始时的约束具有相同的形式,但具有不同的类型变量。一个循环!