Domanda

Sto cercando di lavorare con Numeric.ad e un tipo EXPR personalizzato. Desidero calcolare il gradiente simbolico dell'espressione inserita dall'utente. La prima prova con un'espressione costante funziona bene:

calcGrad0 :: [Expr Double]
calcGrad0 = grad df vars
  where
   df [x,y] = eval (env [x,y]) (EVar "x"*EVar "y")
   env vs = zip varNames vs
   varNames = ["x","y"]
   vars = map EVar varNames

Questo funziona:

>calcGrad0
[Const 0.0 :+ (Const 0.0 :+ (EVar "y" :* Const 1.0)),Const 0.0 :+ (Const 0.0 :+ (EVar "x" :* Const 1.0))]

Tuttavia, se estraggo l'espressione come parametro:

calcGrad1 :: [Expr Double]
calcGrad1 = calcGrad1' (EVar "x"*EVar "y")
calcGrad1' e = grad df vars
  where
   df [x,y] = eval (env [x,y]) e
   env vs = zip varNames vs
   varNames = ["x","y"]
   vars = map EVar varNames

ottengo

Could not deduce (a ~ AD s (Expr a1))
from the context (Num a1, Floating a)
  bound by the inferred type of
           calcGrad1' :: (Num a1, Floating a) => Expr a -> [Expr a1]
  at Symbolics.hs:(60,1)-(65,29)
or from (Mode s)
  bound by a type expected by the context:
             Mode s => [AD s (Expr a1)] -> AD s (Expr a1)
  at Symbolics.hs:60:16-27
  `a' is a rigid type variable bound by
      the inferred type of
      calcGrad1' :: (Num a1, Floating a) => Expr a -> [Expr a1]
      at Symbolics.hs:60:1
Expected type: [AD s (Expr a1)] -> AD s (Expr a1)
  Actual type: [a] -> a
In the first argument of `grad', namely `df'
In the expression: grad df vars

Come faccio a convincere GHC ad accettarlo?

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top