注: 完全なソース コードはこちら: https://gist.github.com/anonymous/7085509
私は次の機能を持っています:
tournament n p pop = do
winner <- (\w -> min (n - 1) (floor (log w / log (1-p)))) <$> gaRandom
(flip S.index) winner <$> S.sort <$> seqChoose n pop
型シグネチャがない場合、コンパイラはtournament
シグネチャが次のとおりであることを通知します。
tournament
:: (Floating a, Ord a1, RealFrac a, Random a) =>
Int -> a -> S.Seq a1 -> StateT GA Data.Functor.Identity.Identity a1
これは私には問題ないようです。しかし、私がそれを使用するとき:
t2 = do
g <- newStdGen
let a = evalState (tournament 5 0.9 (S.fromList [1..10])) (GA g)
return ()
エラーが発生します:
GA.hs:85:37:
No instance for (Fractional a0) arising from the literal `0.9'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Fractional Double -- Defined in `GHC.Float'
instance Fractional Float -- Defined in `GHC.Float'
instance Integral a => Fractional (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus three others
In the second argument of `tournament', namely `0.9'
In the first argument of `evalState', namely
`(tournament 5 0.9 (S.fromList [1 .. 10]))'
In the expression:
evalState (tournament 5 0.9 (S.fromList [1 .. 10])) (GA g)
これが私の最初の質問につながります。なぜRealFrac
意味しないのFractional
ですか? 型シグネチャには RealFrac がありますが、Fractional のインスタンスがないというエラーが表示されます。
次に、型シグネチャをコピーしてコードに貼り付け、次を追加しFractional a
ます。
tournament
:: (Floating a, Ord a1, RealFrac a, Fractional a, Random a) =>
Int -> a -> S.Seq a1 -> State GA a1
tournament n p pop = do
winner <- (\w -> min (n - 1) (floor (log w / log (1-p)))) <$> gaRandom
(flip S.index) winner <$> S.sort <$> seqChoose n pop
そして今、私が得るエラーは次のとおりです。
GA.hs:88:24:
No instance for (Random a0) arising from a use of `tournament'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Random Bool -- Defined in `System.Random'
instance Random Foreign.C.Types.CChar -- Defined in `System.Random'
instance Random Foreign.C.Types.CDouble
-- Defined in `System.Random'
...plus 33 others
In the first argument of `evalState', namely
`(tournament 5 0.9 (S.fromList [1 .. 10]))'
In the expression:
evalState (tournament 5 0.9 (S.fromList [1 .. 10])) (GA g)
In an equation for `a':
a = evalState (tournament 5 0.9 (S.fromList [1 .. 10])) (GA g)
型変数がないため、さらに混乱しますa0
。これは私の 2 番目の質問につながります: 明らかに私は何かを誤解していますが、何ですか?