Haskell: la función de composición con dos argumentos flotantes falla
Estoy tratando de componer una función de tipo(Floating a) => a -> a -> a
con una función de tipo(Floating a) => a -> a
para obtener una función de tipo(Floating a) => a -> a -> a
. Tengo el siguiente 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)
Sin embargo, cuando lo compilo en GHCI, aparece el siguiente 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.
Tenga en cuenta que la versión comentada detestBoth
compila Lo extraño es que si elimino el(Floating a)
restricciones de todas las firmas tipo o si cambiotest1
solo tomarx
en lugar dex
yy
, testBoth
compila
He buscado en StackOverflow, wikis de Haskell, Google, etc. y no he encontrado nada sobre una restricción en la composición de funciones relevante para esta situación particular. ¿Alguien sabe por qué ocurre esto?