2

所以我在玩 y-combinator 和匿名函数,我遇到了这个奇怪的错误:

Couldn't match expected type `t0 -> t1 -> t2'
            with actual type `forall b. b -> [b] -> [b]'
The lambda expression `\ (n :: Int) newVal xs -> ...'
has three arguments,
but its type `Int -> forall b. b -> [b] -> [b]' has only one

产生错误的源代码,以及我最终开始工作的版本

如果我稍微修改类型以避免 Rank N polymorphism (use forall b. Int -> b -> [b] -> [b]),错误是相似的:

Couldn't match expected type `t0 -> t1 -> t2 -> t3'
            with actual type `forall b. Int -> b -> [b] -> [b]'
The lambda expression `\ (n :: Int) newVal xs -> ...'
has three arguments,
but its type `forall b. Int -> b -> [b] -> [b]' has none

有人可以向我解释为什么 forall b. b -> [b] -> [b]没有论据吗?

4

3 回答 3

2

由于您使用的是 GHC 7,这似乎与http://hackage.haskell.org/trac/ghc/ticket/4347中报告的错误具有相同的根本原因。虽然该错误报告谈到了隐含的多态性,但它似乎最有可能出现在高级多态性的统一问题中。在您的情况下,它是由您放置的 触发的forall,这使得该类型在语法上排名第二。

请注意,这并不是真正的错误。提供的进一步说明清楚地表明这是预期的行为,因为不会推断类型的多态实例化,包括 rank-N 类型和谓词类型。仔细添加类型签名可以使其工作。

但是,由于该类型根本不是为了更高级别的,所以在您的情况下,最好摆脱它。

于 2011-05-04T18:17:37.210 回答
0

你更愿意

forall b. Int -> b -> [b] -> [b]

或者真的

Int -> forall b . b -> [b] -> [b]

我会读后者:一个函数,它接受一个 Int 并返回SOMETHING OPAQUE THAT IS MOST LIKELY NOT WHAT YOU THINK IT IS

于 2011-05-04T15:46:18.063 回答
0

我的猜测是你写错了类型。

删除类型注释会有所帮助,从而减少令人困惑的错误:

A.hs:7:76:
    Occurs check: cannot construct the infinite type: a0 = [a0]
    In the third argument of `replaceNth', namely `arg'
    In the expression: replaceNth m (replaceNth n v (arg !! m)) arg

所以

 \m n v arg -> replaceNth m (replaceNth n v (arg !! m)) arg

已经有问题了。


排名 N 类型和词法范围的类型变量

通过在最外面的位置使用forallnot,您偶然发现了N类型。The forallon your innerb表示它必须是不透明的、多态的,并且与您对该b类型的其他用途无关。这可能不是您打算做的。

这与词法范围的类型变量有细微的不同,后者也可以由最外层位置的 forall 引入,如此处所述

通过删除forall非最外层位置的(我认为)错误的 s,您将得到更简单的类型错误。

于 2011-05-04T15:49:45.320 回答