Well, there are valid uses for unsafePerformIO
. It's not there just to be decorative, or as a temptation to test your virtue. None of those uses, however, involve adding meaningful side effects to everyday code. Here's a few examples of uses that can potentially be justified, with various degrees of suspicion:
Wrapping a function that's impure internally, but has no externally observable side effects. This is the same basic idea as the ST
monad, except that here the burden is on the programmer to show that the impurity doesn't "leak".
Disguising a function that's deliberately impure in some restricted way. For instance, write-only impurity looks the same as total purity "from the inside", since there's no way to observe the output that's produced. This can be useful for some kinds of logging or debugging, where you explicitly don't want the consistency and well-defined ordering required by the IO
monad. An example of this is Debug.Trace.trace
, which I sometimes refer to as unsafePerformPrintfDebugging
.
Introspection on pure computations, producing a pure result. A classic example is something like the unambiguous choice operator, which can run two equivalent pure functions in parallel in order to get an answer quicker.
Internally unobservable breaking of referential transparency, such as introducing nondeterminism when initializing data. As long as each impure function is evaluated only once, referential transparency will be effectively preserved during any single run of the program, even if the same faux-pure function called with the same arguments gives different results on different runs.
The important thing to note about all of the above is that the resulting impurity is carefully controlled and limited in scope. Given a more fine-grained system of controlling side-effects than the all-purpose IO
monad, these would all be obvious candidates for slicing off bits of semi-purity, much like the controlled mutable state in the aforementioned ST
monad.
Post scriptum: If a hard-line stance against any non-required use of unsafePerformIO
is being considered, I strongly encourage
extending the prohibition to include unsafeInterleaveIO
and any functions that allow observation of its behavior. It's at least as sketchy as some of the unsafePerformIO
examples I listed above, if you ask me.
unsafePerformIO
strikes me as worrying on many levels. I'm not sure which is worse--completely prohibiting a tool useful for FFI, debugging, etc., or that something has presumably motivated this policy... – Bert