Haskell - fmap fmap 不起作用

发布于 2024-11-04 16:57:28 字数 1000 浏览 6 评论 0原文

我正在使用 GHCi(版本 6.12.3)来玩一下 Haskell。我最近读到了有关函子和应用函子的内容,我想如果你不能仅使用函子的原语来实现类似于应用函子的 <*> 的东西。经过一番思考,我想出了 fmap fmap ,它具有(几乎)理想的

Functor f => 类型。 f(a→b)→ f (fa -> fb) 或更一般地说

(Functor f1, Functor f2) => f1(a→b)→ f1 (f2 a -> f2 b)

我尝试了

let q = fmap fmap

以下错误,

<interactive>:1:8:
    Ambiguous type variable `f1' in the constraint:
      `Functor f1' arising from a use of `fmap' at <interactive>:1:8-16
    Probable fix: add a type signature that fixes these type variable(s)

<interactive>:1:13:
    Ambiguous type variable `f' in the constraint:
      `Functor f' arising from a use of `fmap' at <interactive>:1:13-16
    Probable fix: add a type signature that fixes these type variable(s)

按照建议编写上述类型签名没有帮助。 最疯狂的是,当我输入 :t fmap fmap 时,我得到了与上面相同的类型。

我做错了什么?尽管 GHCi 找到了类型,但为什么 fmap fmap 会给出类型错误?

I'm using GHCi (version 6.12.3) to play a bit with Haskell. I recently read about functors and applicative functors thought if you couldn't something similar to <*> of applicative functors be implemented only using functor's primitives. After some thinking I came up with fmap fmap which would have a (nearly) ideal type of

Functor f => f (a -> b) -> f (f a -> f b)
or more generically

(Functor f1, Functor f2) => f1 (a -> b) -> f1 (f2 a -> f2 b)

I tried

let q = fmap fmap

I got the following error

<interactive>:1:8:
    Ambiguous type variable `f1' in the constraint:
      `Functor f1' arising from a use of `fmap' at <interactive>:1:8-16
    Probable fix: add a type signature that fixes these type variable(s)

<interactive>:1:13:
    Ambiguous type variable `f' in the constraint:
      `Functor f' arising from a use of `fmap' at <interactive>:1:13-16
    Probable fix: add a type signature that fixes these type variable(s)

Writing the above type signature as suggested didn't help.
The craziest thing is when i typed :t fmap fmap I got an equivalent type as the above.

What am I doing wrong? Why does fmap fmap give a type error although GHCi finds a type for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚气少女 2024-11-11 16:57:28

看来您遇到了单态限制

在 GHCi 中使用 -XNoMonomorphismRestriction 尝试示例可以得到预期的结果。

您还可以通过编写 let fx = fmap fmap $ x 来颠覆这一点。单态限制仅适用于“看起来像”值的顶级定义,即 f = some,因此引入显式参数会导致它不再适用。如果这不是顶层(例如在 where 子句中),它也不适用。有关更多详细信息,请参阅链接。

Looks like you're running into the monomorphism restriction.

Trying your example in GHCi with -XNoMonomorphismRestriction gives your expected result.

You can also subvert this by writing let f x = fmap fmap $ x. The monomorphism restriction only applies to top-level definitions which "look like" values, i.e. f = something, so introducing an explicit argument causes it not to apply anymore. It would also not apply if this was not at the top level (for example in a where clause). For more details, see the link.

长伴 2024-11-11 16:57:28

我还不能到处发表评论,所以我会发布答案。如前所述,您收到的错误是由于单态限制造成的。将类型签名修复为原始问题中给出的两个中的任何一个确实会让 ghci 满意,就像您所希望的那样,也许您只是语法略有错误?

Prelude> let q :: (Functor f) => f (a -> b) -> f (f a -> f b); q = fmap fmap
Prelude> :t q
q :: (Functor f) => f (a -> b) -> f (f a -> f b)

Prelude> let q :: (Functor f1, Functor f2) => f1 (a -> b) -> f1 (f2 a -> f2 b); q = fmap fmap
Prelude> :t q
q :: (Functor f1, Functor f2) => f1 (a -> b) -> f1 (f2 a -> f2 b)

I can't comment everywhere yet so I'll post an answer. As mentioned earlier the error you're receiving is due to the monomorphism restriction. Fixing the type signature to either of the two given in the original question does indeed make ghci happy like you were hoping, maybe you just got the syntax slightly wrong?

Prelude> let q :: (Functor f) => f (a -> b) -> f (f a -> f b); q = fmap fmap
Prelude> :t q
q :: (Functor f) => f (a -> b) -> f (f a -> f b)

Prelude> let q :: (Functor f1, Functor f2) => f1 (a -> b) -> f1 (f2 a -> f2 b); q = fmap fmap
Prelude> :t q
q :: (Functor f1, Functor f2) => f1 (a -> b) -> f1 (f2 a -> f2 b)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文