4

実存的なタイプに完全に戸惑っていますが、Numeric.FADライブラリが必要です。

これはコードです:

error_diffs :: [Double] -> NetworkState [(Int, Int, Double)]
error_diffs desired_outputs = do diff_error <- (diff_op $ error' $ map FAD.lift desired_outputs)::(NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double))
                                 weights <- link_weights
                                 let diffs = FAD.grad (diff_error::([FAD.Dual tag a] -> FAD.Dual tag b)) weights

                                 links <- link_list
                                 return $ zipWith (\link diff ->
                                                       (linkFrom link, linkTo link, diff)
                                                  ) links diffs

error'は、diff_opによって実行されるReaderモナドで実行されます。これにより、匿名関数が生成され、現在のNetworkStateとFAD.gradからの差分入力が取得され、Readerに詰め込まれます。

Haskellは私を次のように混乱させます:

Inferred type is less polymorphic than expected
  Quantified type variable `tag' is mentioned in the environment:
    diff_error :: [FAD.Dual tag Double] -> FAD.Dual tag Double
      (bound at Operations.hs:100:33)
In the first argument of `FAD.grad', namely
    `(diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b)'
In the expression:
    FAD.grad (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights
In the definition of `diffs':
    diffs = FAD.grad
              (diff_error :: [FAD.Dual tag a] -> FAD.Dual tag b) weights
4

2 に答える 2

4

このコードは、次のエラーと同じエラーを返します。

test :: Int
test =
  (res :: Num a => a)
  where
    res = 5

コンパイラは、それresが常に型Intであると判断し、何らかの理由resでポリモーフィックであると考えています。

ただし、このコードは正常に機能します。

test :: Int
test =
  res
  where
    res :: Num a => a
    res = 5

ここでも、resはポリモーフィックとして定義されていますが、 としてのみ使用されていIntます。このようにネストされた式を入力する場合にのみ、コンパイラが問題になります。この場合、再利用できますが、ネストされた式を入力した場合とは対照的に、resそれらの使用のいずれかがそれを as として使用しない可能性があります。Int

于 2009-06-11T22:59:31.520 に答える
3

私が書いたら、

bigNumber :: (Num a) => a
bigNumber = product [1..100]

bigNumber :: Int評価されると、評価され
ます(product :: [Int] -> Int) [(1 :: Int) .. (100 :: Int)]

が評価されると、bigNumber :: Integerが評価され
ます(product :: [Integer] -> Integer) [(1 :: Integer) .. (100 :: Integer)]

両者の間で共有されるものは何もありません。

error_diffs単一の型、つまり: を持ちます[Double] -> NetworkState [(Int, Int, Double)]。正確に 1 つの方法で評価する必要があります。

しかし、あなたが中に持っているもの:

... :: NetworkState ([FAD.Dual tag Double] -> FAD.Dual tag Double)

内容に応じて、さまざまな方法で評価できますtag

問題が見えますか?

于 2009-06-11T22:45:02.973 に答える