Green Card support for Haskell calls/callbacks is definitely useful
(and on the ToDo list, I believe.) 

However, GHC has for a long time supported the calling Haskell code
from C, here's a little sample demonstrating how to do it:

foo.lhs

  ...
  type Callback = StablePtr (PrimIO ())

  mkCallback :: (Int -> PrimIO Int) -> IO Callback
  mkCallback act = do
   makeStablePtr  ( do
      v   <- _casm_ `` %r = cback_arg1; '' 
      res <- act v
      _casm_ `` cback_res = (int)%0; '' res )

foo.c

  typedef long HaskellCodePtr;
  typedef void *(*(*FunPtr)())();
  extern FunPtr startPerformIO();
  extern void enterStablePtr (HaskellCodePtr, FunPtr);

  static int cback_arg1;
  static int cback_res;

  int invokeHaskellCallback(cback, i)
  HaskellCodePtr cback;
  int i;
  {
   cback_ arg1 = res1;
   enterStablePtr (cback, startPerformIO);
   return (cback_res);
  }
  

Not Pretty, But It Works (sm).

--Sigbjorn

Graeme Moss writes:
> Dave Tweed wrote:
> > David Elworthy wrote:
> > > The Green Card manual says there is zero demand for calling Haskell from
> > > C. Well, here is an increment to that: there is now 1 demand.
> > 
> > Better make that 1 1/2. I'd like to be able to call Haskell from C (well,
> 
> I can't resist: make that 2 1/2.  My benchmarking kit for functional
> data structures, Auburn, see
> 
> http://www.cs.york.ac.uk/~gem/auburn/
> 
> works a lot better at the moment if we use a Green Card extension that
> allows C to call Haskell.  The York release of nhc does support such
> an extension, see
> 
> http://www.cs.york.ac.uk/fp/
> 
> Simon L Peyton Jones wrote:
> |Something will happen here, although not that fast.  Getting C to
> |call Haskell  is part of getting COM to call Haskell, which we want
> |to do.
> 
> Good.  
> 
> Graeme.

Reply via email to