[Haskell-cafe] Hit a wall with the type system

David Roundy droundy at darcs.net
Fri Nov 30 11:53:05 EST 2007

On Thu, Nov 29, 2007 at 04:25:43PM -0800, Dan Weston wrote:
> I must be missing something, because to me the contract seems to be much 
> simpler to express (than the Functor + Isomorphism route you seem to me 
> to be heading towards):
> diff f a = if dx == dx' then error "Zero denom" else dydx
>   where a'   = addEpsilon  a
>         dx   = a'   `takeAway`   a
>         dx'  = a    `takeAway`   a'
>         dy   = f a' `takeAway` f a
>         dydx = dy   `divide`   dx

But this is just a finite-difference derivative, which is generally
accurate to... well, a lot less than an analytic derivative.  e.g try

diff cos 1e-30

you'll get a garbage answer, while AD or symbolic differentiation will give
you the correct answer, 1e-30.

So for derivatives you actually intend to use, it's much better to use AD
or symbolic differentiation, or just compute the derivatives by hand.
Anything but finite difference (except at a carefully examined check for
better derivatives).
David Roundy
Department of Physics
Oregon State University

More information about the Haskell-Cafe mailing list