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

Dan Piponi dpiponi at gmail.com
Thu Nov 29 15:24:50 EST 2007

On Nov 28, 2007 9:20 PM, Chris Smith <cdsmith at twu.net> wrote:
> I intend to naively treat each function as being from the reals to the
> reals, and then take advantage of the fact (which is proven by the type
> system in the code I posted) that when the derivative is evaluated at
> integer inputs for functions defined using only ring operations, the
> result is an integer (and similarly for rationals and field operations).

I must be missing the point of something. What's wrong with

> diff f x = let AD y dy = f (AD x 1) in dy


In ghci we get

*Main> :t diff (\x -> 2*x) (2::Int)
diff (\x -> 2*x) (2::Int) :: Int
*Main> :t diff (\x -> 2*x) (2::Float)
diff (\x -> 2*x) (2::Float) :: Float

I've used almost exactly that line of code myself a few times.

More information about the Haskell-Cafe mailing list