1

stopLoss 関数により、次のエラーが発生します。

Could not deduce (Text.Printf.PrintfType (m a0))
arising from the ambiguity check for `stopLoss'
from the context (Monad m,
                Text.Printf.PrintfType (m b),
                Text.Printf.PrintfType (m a))
bound by the inferred type for `stopLoss':
  (Monad m, Text.Printf.PrintfType (m b), 
   Text.Printf.PrintfType (m a)) =>
   Float -> Float -> Float -> m b
Possible fix:
   add an instance declaration for (Text.Printf.PrintfType (m a0))

When checking that `stopLoss'
has the inferred type `forall (m :: * -> *) a b.
  (Monad m, Text.Printf.PrintfType (m b),
   Text.Printf.PrintfType (m a)) =>
   Float -> Float -> Float -> m b'
Probable cause: the inferred type is ambiguous

関数:

stopLoss qty pb lossRate = do
    let t = qty * pb * (1 + sxf)
    printf "Stop Loss at: %.2f\n" ((pb - (t * lossRate) / qty) :: Float)
    printf "Lost Money: %.2f\n"  ((t * lossRate) :: Float)

どんな提案でも大歓迎です!

4

1 に答える 1

5

の型はprintfですPrintfType r => String -> r。の次のインスタンスをPrintfType使用できます。

IsChar c => PrintfType [c]   
PrintfType (IO a)    
(PrintfArg a, PrintfType r) => PrintfType (a -> r)

(最後のものは、printf多変量であるかのように振る舞うことです。)

これは多相stopLoss関数です。型IO ()は自動的に推論できず、GHC は関数がどのモナドに対しても機能すると想定しています。したがって、GHC はPrintfTypeジェネリック モナドの for のインスタンスが存在しないと不平を言っています。

のような型シグネチャを提供するFloat -> Float -> Float -> IO ()と役立ちます。

于 2012-07-23T03:13:05.543 に答える