Pure and impure Haskell bindings to C functions

Alain Cremieux alcremi at pobox.com
Fri May 9 17:31:21 EDT 2003


Hi,
I am writing a binding to GraphViz/AGraph. I've used C2HS (excellent 
tool) and everything has been working in a satisfying way so far.
To test the usability of the binding, I translated a c program among the 
examples, to Haskell. And then, after some modifications, I began to 
hesitate : which Haskell functions should be made pure ?

An example :

static void
nodeInduce(Agraph_t *g)
{
   Agnode_t        *n, *rootn;
   Agedge_t        *e;

   for (n = agfstnode(g); n; n = agnxtnode(n)) {
     rootn = agsubnode(agroot(g),n,FALSE);
     for (e = agfstout(rootn); e; e = agnxtout(e)) {
       if (agsubnode(g,aghead(e),FALSE)) agsubedge(g,e,TRUE);
       else {
         if (getscc(aghead(e)) && getscc(agtail(e)))
           agedge(getrep(getscc(agtail(e))),getrep(getscc(aghead(e))),
             NIL(char*),TRUE);
       }
     }
   }
}

In C, many accessors functions are "inlined", but in my first attempt, 
every functions having a result in the IO Monad, the code is a little 
boring.

So I decided to transform simple functions, those returning Int and 
Bool, to pure functions. For exemple 'agIsDirected' (is a graph 
directed) or 'agNNodes' (number of nodes in a graph) seem to be pure 
function.
But what about a function returning a String, such as an object name 
('agNameOf'). To make it pure, I will probably have to use 
unsafePerformIO, which I am reluctant to do. But if a node's seq number 
is a pure function (agSeq), why a node's name would not be pure to ?

So where do I have to put the limit, between actions (create graph, 
impure) and state queries (number of nodes), or between functions 
returning Bool/Int and others ?

Thank you for your thoughts on that,
Alain




More information about the FFI mailing list