How do I know when an explicitly clean Haskell interface hides unsafe operations?

I read unsafePerformIO about unsafePerformIO and I would like to ask something. I am fine with the fact that the real language should be able to interact with the external environment, so unsafePerformIO somewhat justified.

However, as far as I know, I don’t know what a quick way to find out if a clean (judging by type) interface / library is really clean without checking the code when looking for calls on unsafePerformIO (the documentation may not mention this). I know that it should be used only when you are sure that link transparency is guaranteed, but I would like to know about it nonetheless.

+7
source share
2 answers

Unable to verify source code. But this is not so difficult, since Haddock provides a good link directly to the syntax-highlighted definitions directly in the documentation. See the Source links to the right of the definitions on this page for an example.

Safe Haskell matters here; it was used to compile Haskell code in situations where you want to prohibit the use of unsafe functions. If a module uses an unsafe module (for example, System.IO.Unsafe ) and is not specifically marked as Trustworthy , it inherits its unsafe status. But modules that use unsafePerformIO will usually use it safely and thus declare themselves Trustworthy .

+10
source

In the case you are thinking about, using unsafePerformIO unjustified. The documentation for unsafePerformIO explains this: it is intended only for cases where the developer can prove that it is not possible to break link transparency , i.e. "purely functional" semantics. If someone uses unsafePerformIO in such a way that a purely functional program can detect it (for example, write a function whose result depends not only on its arguments), then this is forbidden use.

If you encounter such a case, most likely you have found an error.

+5
source

All Articles