Difference between revisions of "Unsafe functions"

From HaskellWiki
Jump to: navigation, search
m
m
Line 22: Line 22:
 
* type safety (<code>unsafeCoerce#</code>, <code>unsafePerformIO</code>),
 
* type safety (<code>unsafeCoerce#</code>, <code>unsafePerformIO</code>),
  
* [https://okmij.org/ftp/Haskell/index.html#lazyIO-not-True breaks equational reasoning] (<code>unsafeInterleaveIO</code>),
+
* [https://okmij.org/ftp/Haskell/index.html#lazyIO-not-True equational reasoning] (<code>unsafeInterleaveIO</code>),
  
* or break [http://en.wikipedia.org/wiki/Parametricity parametricity] (<code>seq</code>).
+
* or [http://en.wikipedia.org/wiki/Parametricity parametricity] (<code>seq</code>).
  
 
Their use (except in the case of <code>seq</code>) would require some
 
Their use (except in the case of <code>seq</code>) would require some

Revision as of 05:57, 11 August 2022

A colleague [...] asked me today whether I know how to use unsafePerformIO safely. And I realized I have no idea.

Richard Eisenberg.

There are a number of unsafe functions in the libraries.

  • unsafePerformIO :: IO a -> a
  • unsafeInterleaveIO :: IO a -> IO a
  • unsafeInterleaveST :: ST s a -> ST s a
  • unsafeIOToST :: IO a -> ST s a
  • unsafeIOToSTM :: IO a -> STM a
  • unsafeFreeze, unsafeThaw
  • unsafeCoerce# :: a -> b
  • seq :: a -> b -> b

Unsafe functions can break:

  • type safety (unsafeCoerce#, unsafePerformIO),

Their use (except in the case of seq) would require some kind of assurance on the part of the programmer that what they're doing is safe.

unsafe is also a keyword which can be used in a foreign import declaration.


This article is a stub. You can help by expanding it.