blob: 5af5bbc4aa95b0ecc8cada3523681b35430f4c8d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign hiding ( unsafePerformIO )
import Foreign.C
import System.IO.Unsafe
d f x = unsafePerformIO $ do
g <- mkfun f
r <- deriv g x 1
return r
main = do
print $ d (\x -> x * 2) 3
print $ d (\x -> x * d (\y -> x + y) 5) 7
foreign import ccall safe "deriv"
deriv :: FunPtr (CDouble -> CDouble) -> CDouble -> CDouble -> IO CDouble
foreign import ccall safe "wrapper"
mkfun :: (CDouble -> CDouble) -> IO (FunPtr (CDouble -> CDouble))
|