FFI Help

Matthew Donadio m.p.donadio@ieee.org
Tue, 03 Jun 2003 18:48:14 -0400

Hi all,

I am just starting to experiment with FFI, and am running into a
problem.  I was to create an FFI to the lgamma(3) found in many of the
newer libm implementations.  My code follows the sig.

The lgamma function works.  The gamma function core dumps (I am using
ghc 5.04.3) on me.  gdb reports a SIGSEGV in signgam(), but I'm not sure
why.  I believe that I need to use the monad because signgam is only
valid after lgamma returns.

Does anyone have an idea what I am doing wrong?


Matthew Donadio <m.p.donadio@ieee.org>

> module Gamma (gamma, lgamma) where

> import System.IO.Unsafe

> foreign import ccall "math.h lgamma"  lgammaC  :: Double -> IO Double 
> foreign import ccall "math.h signgam" signgamC :: IO Int

> lgamma :: Double -> Double
> lgamma x = unsafePerformIO $ lgammaC x

> gamma :: Double -> Double
> gamma x = unsafePerformIO $ gammaIO x

> gammaIO :: Double -> IO Double
> gammaIO x = do lg <- lgammaC x
> 	       s  <- signgamC
> 	       return $ fromIntegral s * exp lg