AD,使用约束类型Vector键入统一错误

时间:2016-04-27 00:18:42

标签: haskell

我需要找到V.Vector CDouble -> V.Vector CDouble函数的雅可比行列式。

在尝试从答案How to do automatic differentiation on complex datatypes?中产生一个最小的例子时,我仍然偶然发现了一类类型的统一问题。我到目前为止最接近(下面的完整代码)给出了以下错误:

    Could not deduce (t ~ CDouble)
    from the context (a ~ CDouble)
      bound by the type signature for
                 jf1 :: a ~ CDouble => V.Vector a -> V.Vector (V.Vector a)
      at ADMode.hs:17:8-69
    or from (AD.Scalar t ~ a, AD.Mode t)
      bound by the type signature for
                 go :: (AD.Scalar t ~ a, AD.Mode t) => V.Vector t -> V.Vector t
      at ADMode.hs:19:9-74
      ‘t’ is a rigid type variable bound by
          the type signature for
            go :: (AD.Scalar t ~ a, AD.Mode t) => V.Vector t -> V.Vector t
          at ADMode.hs:19:16
    Expected type: V.Vector t
      Actual type: V.Vector CDouble

注意:我在AD.Mode CDouble的类型上添加了实例(a ~ CDouble)和约束jf1;这些,AFAICT,是唯一相关的变化w.r.t.引用的SO答案。

{-# language TypeFamilies, RankNTypes, ScopedTypeVariables #-}
module ADMode where

import Foreign.C.Types

import qualified Numeric.AD as AD
import qualified Data.Vector as V

f1 :: V.Vector CDouble -> V.Vector CDouble
f1 = V.map (**2)

jf1 :: forall a. (a ~ CDouble) => V.Vector a -> V.Vector (V.Vector a)
jf1 = AD.jacobian go where
  go :: forall t. (AD.Scalar t ~ a, AD.Mode t) => V.Vector t -> V.Vector t
  go x = f1 (V.map AD.auto x)

instance AD.Mode CDouble where
  type Scalar CDouble = CDouble
  -- isKnownConstant _ = True
  -- isKnownZero x = 0 == x
  auto = id
  -- (^/) = (/)

我承认我仍然不了解双重存在量化的作用,以及类型约束提示(即~)如何与类型推断相互作用。另一方面,我只是希望暂时让这个小装置工作......

非常欢迎任何提示和解释。提前谢谢

0 个答案:

没有答案