haskell - 如何处理 “Inferred type is less polymorphic than expected” ?

标签 haskell types numerical fad

我需要 Numeric.FAD 库,尽管仍然对存在类型感到完全困惑。

这是代码:

error_diffs :: [Double] -> NetworkState [(Int, Int, Double)]
error_diffs desired_outputs = do diff_error <- (diff_op $ error' $ map FAD.lift desired_outputs)::(NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double))
                                 weights <- link_weights
                                 let diffs = FAD.grad (diff_error::([FAD.Dual tag a] -> FAD.Dual tag b)) weights

                                 links <- link_list
                                 return $ zipWith (\link diff ->
                                                       (linkFrom link, linkTo link, diff)
                                                  ) links diffs

error' 在 Reader monad 中运行,由 diff_op 运行,它反过来生成一个匿名函数来获取当前的 NetworkState 和来自 FAD.grad 的差分输入,并将它们填充到 Reader 中。

Haskell 让我对以下内容感到困惑:

Inferred type is less polymorphic than expected
  Quantified type variable `tag' is mentioned in the environment:
    diff_error :: [FAD.Dual tag Double] -> FAD.Dual tag Double
      (bound at Operations.hs:100:33)
In the first argument of `FAD.grad', namely
    `(diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b)'
In the expression:
    FAD.grad (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights
In the definition of `diffs':
    diffs = FAD.grad
              (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights

最佳答案

此代码给出与您得到的相同的错误:

test :: Int
test =
  (res :: Num a => a)
  where
    res = 5

编译器认为 res 始终为 Int 类型,并且由于某种原因您认为 res 是多态的而感到困扰。

但是,这段代码运行良好:

test :: Int
test =
  res
  where
    res :: Num a => a
    res = 5

这里,res 也被定义为多态,但仅用作 Int。仅当您以这种方式键入嵌套表达式时,编译器才会受到干扰。在这种情况下,res 可以被重用,并且其中一个用途可能不会将其用作 Int,这与您键入嵌套表达式时不同,嵌套表达式本身无法重用.

关于haskell - 如何处理 “Inferred type is less polymorphic than expected” ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/983633/

相关文章:

python - numpy 独特的奇怪行为

SQL ORDER 字符数字

c++ - 计算数值误差

haskell - 如何将自由的一元 DSL 与状态交错,但在程序中解释状态?

haskell - 使用 State Monad 插入树

sql-server - 将 int 转换为时间(奇怪的值)

用作数组索引时的规范无符号文字后缀

haskell - 懒惰的评估如何迫使 Haskell 变得纯粹

haskell 镜头。获取最后一个之前的元素

c++ - 什么 C++ 类型用于最快的 "for cycles"?