The question says it all. More specifically, I am writing bindings to a C library, and I’m wondering what c functions I can use unsafePerformIO with. I assume using unsafePerformIO with anything involving pointers is a big no-no.
It would be great to see other cases where it is acceptable to use unsafePerformIO too.
In the specific case of the FFI,
unsafePerformIOis meant to be used for calling things that are mathematical functions, i.e. the output depends solely on the input parameters, and every time the function is called with the same inputs, it will return the same output. Also, the function shouldn’t have side effects, such as modifying data on disk, or mutating memory.Most functions from
<math.h>could be called withunsafePerformIO, for example.You’re correct that
unsafePerformIOand pointers don’t usually mix. For example, suppose you haveEven though you’re just reading a value from a pointer, it’s not safe to use
unsafePerformIO. If you wrapp_sin, multiple calls can use the pointer argument, but get different results. It’s necessary to keep the function inIOto ensure that it’s sequenced properly in relation to pointer updates.This example should make clear one reason why this is unsafe:
When compiled, this program outputs
Even though the value referenced by the pointer has changed between the two references to “sin1”, the expression isn’t re-evaluated, leading to stale data being used. Since
safeSin(and hencesin2) is in IO, the program is forced to re-evaluate the expression, so the updated pointer data is used instead.