【发布时间】:2010-12-02 23:14:31
【问题描述】:
我正在尝试将(Floating a) => a -> a -> a 类型的函数与(Floating a) => a -> a 类型的函数组合在一起,以获得(Floating a) => a -> a -> a 类型的函数。我有以下代码:
test1 :: (Floating a) => a -> a -> a
test1 x y = x
test2 :: (Floating a) => a -> a
test2 x = x
testBoth :: (Floating a) => a -> a -> a
testBoth = test2 . test1
--testBoth x y = test2 (test1 x y)
但是,当我在 GHCI 中编译它时,我收到以下错误:
/path/test.hs:8:11:
Could not deduce (Floating (a -> a)) from the context (Floating a)
arising from a use of `test2'
at /path/test.hs:8:11-15
Possible fix:
add (Floating (a -> a)) to the context of
the type signature for `testBoth'
or add an instance declaration for (Floating (a -> a))
In the first argument of `(.)', namely `test2'
In the expression: test2 . test1
In the definition of `testBoth': testBoth = test2 . test1
Failed, modules loaded: none.
请注意,testBoth 的注释掉版本可以编译。奇怪的是,如果我从所有类型签名中删除 (Floating a) 约束,或者如果我将 test1 更改为仅采用 x 而不是 x 和 y,testBoth 编译。
我搜索了 StackOverflow、Haskell wikis、Google 等,但没有发现任何与此特定情况相关的函数组合限制。有谁知道为什么会这样?
【问题讨论】:
标签: haskell function-composition