问题描述
我试图编写一个类型为(Floating a)=>的函数。 a - > a - >一个带有
来获得类型为类型的函数的
(Floating a)=> a - > a (Floating 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 - >
testBoth = test2。 test1
--testBoth xy = test2(test1 xy)
然而,当我编译它时在GHCI中,我得到以下错误:
/path/test.hs:8:11:
无法从/path/test.hs:8:11-15 $ b $处使用`test2'
引起的上下文(Floating a)
中的浮点数(a-> a) b可能的修正:将
add(浮动(a - > a))添加到
的上下文中`testBoth'
的类型签名或者添加一个实例声明(Floating(a - > ; a))
在`(。)'的第一个参数中,即`test2'
在表达式:test2中。 test1
在`testBoth'的定义中:testBoth = test2。 test1
失败,模块加载:无。
请注意,注释版本 testBoth
编译。奇怪的是,如果我从所有类型签名中删除(Floating a)
约束,或者如果将 test1
更改为只需 x
而不是 x
和 y
, testBoth
编译。
我搜索了StackOverflow,Haskell wiki,Google等,功能组成与这种特定情况有关。有人知道为什么会这样吗?
test2(test1 x y)== \x y - > test2((test1 x)y)
== \ x y - > (test2。(test1 x))y
== \x - > test2。 (test1 x)
== \x - > (test2。)(test1 x)
== \x - > ((test2。)。test1)x
==(test2。)。 test1
这两样东西并不相同。
test2。 test1
== \x - > (test2。test1)x
== \x - > test2(test1 x)
== \x y - > (test2(test1 x))y
== \x y - > test2(test1 x)y
I am trying to compose a function of type (Floating a) => a -> a -> a
with a function of type (Floating a) => a -> a
to obtain a function of type (Floating a) => a -> a -> a
. I have the following code:
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)
However, when I compile it in GHCI, I get the following error:
/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.
Note that the commented-out version of testBoth
compiles. The strange thing is that if I remove the (Floating a)
constraints from all type signatures or if I change test1
to just take x
instead of x
and y
, testBoth
compiles.
I've searched StackOverflow, Haskell wikis, Google, etc. and not found anything about a restriction on function composition relevant to this particular situation. Does anyone know why this is happening?
\x y -> test2 (test1 x y)
== \x y -> test2 ((test1 x) y)
== \x y -> (test2 . (test1 x)) y
== \x -> test2 . (test1 x)
== \x -> (test2 .) (test1 x)
== \x -> ((test2 .) . test1) x
== (test2 .) . test1
These two things are not like each other.
test2 . test1
== \x -> (test2 . test1) x
== \x -> test2 (test1 x)
== \x y -> (test2 (test1 x)) y
== \x y -> test2 (test1 x) y
这篇关于Haskell:具有两个浮动参数的组合函数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!