Pure and impure Haskell bindings to C functions

Wolfgang Thaller wolfgang.thaller at gmx.net
Sat May 10 03:14:07 EDT 2003


Alain Cremieux wrote:

> 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 ?
> [...]
> 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 ?

State queries are not pure, as they depend on state, and state could 
change over time.
Remember that, due to lazy evaluation, a pure functions can always be 
evaluated later than you expect.

For every function in question, you need to ask yourself:
Does the function always return the same value if you pass the same 
parameter? If no, it has to be in the IO monad.
For example, if you have an action to change the object name, you can't 
have a pure function that gets the object name.
If you have an action to free the graph (as opposed to using 
ForeignPtrs and the Garbage Collector to clean up after yourself), then 
you can't have any pure functions that operate on the graph; otherwise, 
the graph could already have been freed when the pure function is 
finally evaluated.

I hope that helps a bit,

Cheers,

Wolfgang




More information about the FFI mailing list