unsafePerformIO around FFI calls

Simon Marlow simonmar@microsoft.com
Tue, 9 Jul 2002 12:04:27 +0100


> Hal Daume <hdaume@ISI.EDU> writes:
> > I'm curious exactly what is "safe" and what is "unsafe" to wrap
> > unsafePerformIO around when it comes to FFI calls.=20
>=20
> Here's a simple test:
>=20
>  Could you imagine an alternative implementation of the same API in
>  pure Haskell?  (Don't consider efficiency or effort required to write
>  the implementation, just whether it can be done.)
>=20
>  If so, then it is ok to use unsafePerformIO and the ffi to implement
>  the API instead.
>=20
> If it fails that test, it is incredibly unlikely that it is ok and a
> proof that it is ok is likely to be pretty complex - maybe worth a
> PLDI paper or some such.

That's a nice succinct way to describe it.  Another way, which boils
down to the same thing but which is a little more concrete, is to ask:

  - Does the function's result depend only on the values of its
    arguments?

(obviously only makes sense for a top-level IO function which you want
to wrap in unsafePerformIO - for a non-top-level function or expression
just replace 'arguments' with 'arguments and free variables').

Cheers,
	Simon