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

Henning Thielemann lemming at henning-thielemann.de
Thu Nov 29 03:19:15 EST 2007


On Thu, 29 Nov 2007, Henning Thielemann wrote:

> On Wed, 28 Nov 2007, Chris Smith wrote:
>
> > > diffNum        :: Num b        => (forall a. Num a        => a -> a) -> b -> b
> > > diffFractional :: Fractional b => (forall a. Fractional a => a -> a) -> b -> b
> > > diffFloating   :: Floating b   => (forall a. Floating a   => a -> a) -> b -> b
> > >
> > > diffNum f x        = let AD y dy = f (AD x 1) in dy
> > > diffFractional f x = let AD y dy = f (AD x 1) in dy
> > > diffFloating f x   = let AD y dy = f (AD x 1) in dy
>
> Why do the functions have different number types after differentiation?
> I thought, that just 'diff'
>
> diff :: (AD a -> AD a) -> (a -> a)
> diff f x   = let AD y dy = f (AD x 1) in dy
>
> must work. What you do, looks like numbers with errors, but I suspect you
> are right that 'automatic differentiation' is the term used for those
> kinds of computations.

I like to add that I have written some code for working with Taylor
expansions of functions, that is, with all derivatives of a function. You
can get the derivatives of composed functions by composing the Taylor
series of the elementary functions:
  http://darcs.haskell.org/htam/src/PowerSeries/Taylor.hs

E.g.

*PowerSeries.Taylor> take 10 $ exp `compose` sin (pi/2) :: [Double]
[2.718281828459045,1.664412599305428e-16,-1.3591409142295225,-1.109608399536952e-16,0.45304697140984085,4.299732548205689e-17,-0.11703713428087555,-1.2516118554300737e-17,2.5551309845882386e-2,3.0070240853853573e-18]

Computes the (truncated) Taylor series for (exp . sin) at pi/2.


More information about the Haskell-Cafe mailing list