【发布时间】:2021-10-28 16:55:06
【问题描述】:
我正在尝试将这个 JS 定点运算符翻译成 Haskell。
JS:
const fix = F => {
const D = X => F(
t => X(X)(t)
)
return D(D)
};
我的尝试是(Haskell):
fix' f = d d
where
d x = f (\t -> x x t)
但是,我收到以下错误:
Couldn't match expected type ‘(t2 -> t3) -> t4’
with actual type ‘p’
because type variables ‘t2’, ‘t3’, ‘t4’ would escape their scope
These (rigid, skolem) type variables are bound by the inferred type of d :: (t1 -> t2 -> t3) -> t4
有人知道这里发生了什么吗?
【问题讨论】:
-
一个众所周知的问题是像 Y 这样的定点组合子不能在 Haskell 中很好地键入。具体来说,
x x形式的东西要求x同时具有两种冲突的类型。在动态语言中,这无关紧要,因为您只是不关心类型是什么,只是您可以以某种方式使用该值。但是 Haskell 编译器确实在乎。但是不需要这样的组合器,因为 Haskell 的解决方案fix f = let x = f x in x更优雅,并且没有打字困难(但确实需要惰性求值)。 -
像
x x这样的自我应用程序在haskell 中失败,因为它不能很好地键入。有一些魔法可以让它的一些变体起作用,但你不需要那个。要定义一个有效的定点运算符,只需使用递归,例如fix f = f (fix f)(有更高效的,但这是最简单的)。
标签: javascript haskell types code-translation fixpoint-combinators