Skip to content

possible error in foreign wrapper reentering Haskell

The following simple code works as expected in 6.12.1, 6.10, and all previous ghc versions:

fun.c:

double deriv(double f(double), double x, double h)
{
    return (f(x+h)-f(x))/h;
}

pru.hs:

{-# LANGUAGE ForeignFunctionInterface #-}

import Foreign

d f x = unsafePerformIO $ do
    g <- mkfun f
    r <- deriv g x 0.01
    return r

main = do
    print $ d sin 0
    print $ d (\x-> x * d (\y-> x+y) 1) 1


foreign import ccall safe "deriv" deriv :: FunPtr (Double->Double) -> Double -> Double -> IO Double

foreign import ccall safe "wrapper" mkfun:: (Double -> Double) -> IO( FunPtr (Double -> Double)) 

$ ghc -V

The Glorious Glasgow Haskell Compilation System, version 6.12.1

$ ghc --make pru.hs fun.c -fforce-recomp

[1 of 1] Compiling Main ( pru.hs, pru.o )

Linking pru ...

$ ./pru

  1. 9999833334166665
  2. 000000000004464

But it gives a wrong result in ghc-6.12.2:

$ ghc -V

The Glorious Glasgow Haskell Compilation System, version 6.12.2

$ ghc --make pru.hs fun.c -fforce-recomp

[1 of 1] Compiling Main ( pru.hs, pru.o )

Linking pru ...

$ ./pru

  1. 9999833334166665
  2. 0

I have discovered this problem when a similar test in my hmatrix library failed in 6.12.2. As shown above, it seems that the wrapper only fails on C functions which call back to Haskell.

Linux 2.6.28-17-generic #58 (closed)-Ubuntu SMP

ghc-6.12.2-i386-unknown-linux-n.tar.bz2

Trac metadata
Trac field Value
Version 6.12.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (FFI)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information