我正在尝试将 type(Floating a) => a -> a -> a
的函数与 type 的函数组合(Floating a) => a -> a
以获得 type 的函数(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
and y
,则testBoth
编译。
我搜索了 StackOverflow、Haskell wikis、Google 等,但没有发现任何与此特定情况相关的函数组合限制。有谁知道为什么会这样?