Haskell: função de composição com dois argumentos flutuantes falha
Estou tentando compor uma função do tipo(Floating a) => a -> a -> a
com uma função do tipo(Floating a) => a -> a
para obter uma função do tipo(Floating a) => a -> a -> a
. Eu tenho o seguinte código:
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)
No entanto, quando eu o compilo no GHCI, recebo o seguinte erro:
/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.
Observe que a versão comentada dotestBoth
compila. O estranho é que, se eu remover o(Floating a)
restrições de todas as assinaturas de tipo ou se eu mudartest1
apenas levarx
ao invés dex
ey
, testBoth
compila.
Pesquisei StackOverflow, Haskell wikis, Google, etc. e não encontrei nada sobre uma restrição na composição da função relevante para essa situação específica. Alguém sabe por que isso está acontecendo?