[Haskell-cafe] Questions about Numeric.AD / Automatic differentiation

Li-yao Xia lysxia at gmail.com
Tue Nov 21 06:48:14 UTC 2017


Hi Sean,

AD relies on overloading to instrument functions so they can be 
differentiated.

f :: Num a => m1 (m2 a) -> m3 a -> a

By looking at the type of "grad", one can see that f will be specialized 
  at type "Reverse s Double":

f :: m1 (m2 (Reverse s Double)) -> m3 (Reverse s Double) -> Reverse s Double

So if "x :: m1 (m2 Double)", you will need to apply "auto :: Double -> 
Reverse s Double" to lift x to the right type.

df x theta = grad (f x') theta
   where x' = (fmap . fmap) auto x

Cheers,
Li-yao

On 11/20/2017 05:05 PM, Sean Matthews wrote:
> I'm having some problems with Numeric.AD (translation, things are not
> working for reason that I don't understand).  Note I don't have much
> experience with this package, so these are newbie questions, thus
> appropriate answers may involve pointing me to a document somewhere out
> there on the net.
> 
> Anyway,  here is my problem:
> 
> I have a function (call it f x theta) which I have defined purely in terms
> of basic arithmetic functions  (+/-/(/)/*/**) glued together using standard
> applicative functor operations has type
> 
> f  :: m1 (m2 Double) -> m3 Double -> Double
> f x theta = ...
> 
> m1 m2 and m3 are all Traversable.
> f is defined purely in terms of basic arithmetic operations, (+/-/(/)/*/**)
> glued together using standard applicative functor operations, and m1 m2 and
> m3 are all pretty trivial record types (no recursion, even).
> 
> I would like to write
> 
> df x theta = grad (f x) theta
> 
> But it refuses to type, even though (admittedly quite a lot) simpler
> versions do.
> 
> So what am I missing? Does AD not go through Applicative?  That seems
> unlikely to me.
> 
> Any advice / suggestions, etc. gratefully received.
> 
> Sean Matthews
> 
> 
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
> 


More information about the Haskell-Cafe mailing list