type の関数を取得するために、(Floating a) => a -> a -> a
type の関数を使用してtype の関数を構成しようとしています。次のコードがあります。(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)
奇妙なことに、すべての型シグネチャから制約を削除するか、 andの代わりにtest1
take だけに変更すると、コンパイルされます。x
x
y
testBoth
StackOverflow、Haskell wiki、Google などを検索しましたが、この特定の状況に関連する関数構成の制限については何も見つかりませんでした。なぜこれが起こっているのか誰にも分かりますか?