[Haskell-cafe] Troubles with FFI

Donald Bruce Stewart dons at cse.unsw.edu.au
Sat May 13 03:47:34 EDT 2006


mattcbro:
> I am a newcomer to Haskell and I'm offering my perspective on the troubles I
> endured trying to make Haskell interface to some simple C programs.
> It is my hope, that by documenting this, someone else will benefit from my
> mistakes and errors and climb the learning curve somewhat faster than me.
> 
> I was attracted to Haskell, after playing around with OCAML,
> and realizing that functional programming offers some
> nice advantages to scientific programming. Mainly because the notation and
> thinking is very mathematical.
> 
> But first, in order to evaluate the suitability of this language for my 
> purposes, I had to verify that the foreign function interface (FFI) was 
> suitable.
> There are a number of numerical libraries, all in imperative languages, 
> that I
> want to link to.
> 
> I quickly discovered that the documentation on Haskell's FFI
> is rather poor currently.  This topic is simply not of interest to the 
> academics I suppose. It took many hours of searching to find this document:
> http://www.cs.caltech.edu/courses/cs11/material/haskell/papers/tackling_the_awkward_squad.pdf
> This little tutorial was a goldmine of information about monads and an
> introduction to the FFI.  (No template solutions for my problem sorry to 
> say.)

haskell.org is the center of all things haskell.

 *  Visit haskell.org
 *  Click on "Books and tutorials"
 *  Scroll to "Using Monads"
 *  First entry in the list is this paper.

You'll also find many many more articles, tutorials and code examples on haskell.org
Please let us know if things aren't arranged in the most obvious manner.

> It was certainly dismaying to see that even the simplest IO requires
> at least a minimal understanding of monads, and adds a bunch of extra
> semantics to the problem.  However none of the solutions offered, even

Hmm. Monads are fairly easy, but have a scary name. Perhaps check out
the "Yet Another Haskell Tutorial" ? Though by the soudns of it you
might be passed this stage already :)

> the automated ones, seemed to handle the case of passing numerical
> arrays to C, and having them get updated and passed back.  That is my
> primary interest in the FFI and yet no obvious solution or example
> could be found after days of internet searching and pouring over
> tutorials etc.

Regarding arrays, generally you'd use a Foreign array, allocate it on
the C side, and access it via a Foreign Ptr, no marshalling.

Also, feel free to ask advice on haskell-cafe at haskell.org, or on the
#haskell irc channel. There's lots of experts only too happy to help
out.

-- Don


More information about the Haskell-Cafe mailing list