Pure and impure Haskell bindings to C functions
Alastair Reid
alastair at reid-consulting-uk.ltd.uk
Mon May 12 12:07:04 EDT 2003
> Alain Cremieux wrote:
> [...] which Haskell functions should be made pure ? [...]
On Saturday 10 May 2003 8:14 am, Wolfgang Thaller wrote:
> 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.
This is an overly strong test. What matters is whether the result is
observably different with different calls. This will depend on what other
operations you include in the API since observational equivalence is all
about what you can do with an object not how you built it.
For example, if you only provide operations to create objects then you don't
need to use the IO monad. But, if you provide an equality operation, if
object creation is visible from outside the processs (e.g., if it displays a
window on the screen), etc. then you need to use the IO monad.
In this particular context (a graph API), side effects on the graph are likely
to force you to use the IO monad. If you can arrange that graphs can't be
modified after being built, and use ForeignPtrs you could probably drop most
uses of IO.
For example, you could pack all graph creation into a single operation
buildGraph :: [(Node,Node)] -> Graph
buildGraph _ = unsafePerformIO (...)
and then operations which read/ compute properties of the graph need not be in
the IO monad.
See these mesages for a previous discussion:
http://www.haskell.org/pipermail/glasgow-haskell-users/2002-July/003681.html
http://www.haskell.org/pipermail/glasgow-haskell-users/2002-July/003683.html
http://www.haskell.org/pipermail/glasgow-haskell-users/2002-July/003689.html
--
Alastair Reid
More information about the FFI
mailing list