haskell - haskell 和 Arrows 中的类型推断

标签 haskell types arrows

我正在尝试使用箭头并遇到恼人的问题 - 我必须为我实现的所有函数提供显式类型。 如果我不提供它,ghc 会输出一些错误,例如

No instance for (Arrow a0) arising from a use of ‘...’
The type variable ‘a0’ is ambiguous

我可以提供显式类型,但这非常烦人,因为每次我更改某些函数时,我都可能必须手动更改依赖于更改的每个函数的类型。

是否可以强制 ghc 自动推断函数类型?

小事

import Control.Arrow

ss = arr

原因

No instance for (Arrow a0) arising from a use of ‘arr’
    The type variable ‘a0’ is ambiguous
    Relevant bindings include
      ss :: (b -> c) -> a0 b c (bound at src/Main.hs:62:1)
    Note: there are several potential instances:
      instance Arrow Coroutine -- Defined at src/Main.hs:33:10
      instance Arrow (->) -- Defined in ‘Control.Arrow’
      instance Monad m => Arrow (Kleisli m) -- Defined in ‘Control.Arrow’
    In the expression: arr
    In an equation for ‘ss’: ss = arr

while 代码具有完全相同的语义

import Control.Arrow

ss :: forall a b c. (Arrow a) => (b -> c) -> a b c
ss = arr

编译得很好。

最佳答案

最简单的事情是关闭单态限制 - 将其放在源文件的顶部:

{-# LANGUAGE NoMonomorphismRestriction #-}

错误的原因是,虽然 Haskell 可以很好地推断 ss 的类型,但单态性限制要求在值的顶级定义中,该类型不是多态的-class(例如Arrow),除非有显式类型签名。

关于haskell - haskell 和 Arrows 中的类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25581957/

相关文章:

java - Guice - 以编程方式创建的绑定(bind)

haskell - 箭头/HXT 和类型签名

haskell - 让 Haskell 仿函数沉入其中。

haskell - 如何在 Haskell 中表示图表?

haskell - 在 Haskell 中,在此列表理解中使用 takeWhile 或使用 "regular"不等式有什么区别?

animation - SVG 动画贝塞尔曲线跟随箭头增长

r - 如何在ggplot2中绘制漂亮的箭头

haskell - 尝试显示数字时 GHCI 中的堆栈溢出

java - 如何使用类型删除来使用继承方法?

scala - scala中 `type`和 `#`关键字的调查