How to partially apply a function to yield a polymorphic rank-1 type?

95 Views Asked by At

I want to partially apply a function f :: T to a value x :: [Double] to get a resulting function f' :: forall a . Floating a => [a] -> a. What should T be? I can't figure it out.

One difficulty is that, inside f, I need to combine x :: [Double] with the first parameter of f' (let's call it y) via some math (e.g. adding each element of the lists).

I know that internally, x and y will both be Doubles, so I ended up using the type f :: forall a . Floating a => [a] -> (forall b . Floating b => [b] -> b), and using unsafeCoerce (I know, I know...) inside f whenever a value of type a is combined with type b.

Any thoughts on a better type for f?

Context: I'm forced to produce the more general forall a . Floating a => [a] -> a type after partially applying f because I need to take the gradient of the resulting function using the Haskell autodiff library ad. The function grad in that library requires that its input function have that general type.

(Why do I need to partially apply an objective function? You can imagine that such a function would have internal constants that should not be treated by the optimizer as part of a changing state vector. A hack might be to treat the constants as part of the state and just not update the constants, but in that case the norm of the gradient wouldn't go to zero at the local minima, and other things might go wrong.)

0

There are 0 best solutions below