Possible to call Haskell function from C and return a struct by value?

sframe scott.frame at gmail.com
Wed Mar 30 06:41:08 CEST 2011


I am a Haskell novice, but I have written some Haskell functions and would
like to call them from C++.

The functions perform world coordinate system conversions like the

data Geocentric = Geocentric
        ecefX :: Double,
        ecefY :: Double,
        ecefZ :: Double
    deriving (Show)

data Geodetic = Geodetic
        latitude  :: Double,
        longitude :: Double,
        height    :: Double
    deriving (Show)

geocentricToGeodetic :: Geocentric -> Geodetic
geocentricToGeodetic geocentric = Geodetic lat lon height
    where ...
          lat = ...
          lon = ...
          height = ...

I would like to call these functions from C++ using FFI, and have figured
out how to wrap using FFI to accept and return primitive types:

xGeocentricFromGeodetic :: CDouble -> CDouble -> CDouble -> CDouble
xGeocentricFromGeodetic lat lon height = realToFrac (ecefX
(geodeticToGeocentric (Geodetic (realToFrac lat)
(realToFrac lon)
(realToFrac height))))

foreign export ccall xGeocentricFromGeodetic :: CDouble -> CDouble ->
CDouble -> CDouble

Would there be a way to accept a C struct and return a C struct by value,
something like this:

struct Geocentric
    double ecefX;
    double ecefY;
    double ecefZ;

struct Geodetic
    double lat;
    double lon;
    double height;

With a method like the following: 

Geocentric geocentricToGeodetic (const Geodetic & geod);

I have investigated the Storable class, but it seems as though this is
intended to be used with pointers.
In addition, it seems that most of the examples I have found deal with
calling C code from Haskell and not vice-versa.

Does anyone know if this can be achieved, and if so how? Any help is much

View this message in context: http://haskell.1045720.n5.nabble.com/Possible-to-call-Haskell-function-from-C-and-return-a-struct-by-value-tp4270735p4270735.html
Sent from the Haskell - FFI mailing list archive at Nabble.com.

More information about the FFI mailing list