Difference between revisions of "Unsafe functions"

From HaskellWiki
Jump to navigation Jump to search
m
m (Exclude seq and pseq: see Seq for details)
Line 8: Line 8:
 
There are a number of '''unsafe functions''' in the libraries.
 
There are a number of '''unsafe functions''' in the libraries.
   
 
* <hask>unsafeLocalState :: IO a -> a</hask>
 
* <hask>unsafePerformIO :: IO a -> a</hask>
 
* <hask>unsafePerformIO :: IO a -> a</hask>
  +
* <hask>inlinePerformIO :: IO a -> a</hask>
 
* <hask>unsafeInterleaveIO :: IO a -> IO a</hask>
 
* <hask>unsafeInterleaveIO :: IO a -> IO a</hask>
 
* <hask>unsafeInterleaveST :: ST s a -> ST s a</hask>
 
* <hask>unsafeInterleaveST :: ST s a -> ST s a</hask>
Line 15: Line 17:
 
* <hask>unsafeFreeze</hask>, <hask>unsafeThaw</hask>
 
* <hask>unsafeFreeze</hask>, <hask>unsafeThaw</hask>
 
* <hask>unsafeCoerce# :: a -> b</hask>
 
* <hask>unsafeCoerce# :: a -> b</hask>
* <hask>seq :: a -> b -> b</hask>
 
   
 
Unsafe functions can:
 
Unsafe functions can:
   
* break type safety (<code>unsafeCoerce#</code>, <code>unsafePerformIO</code>),
+
* break type safety (<code>unsafeCoerce#</code>, <code>unsafeLocalState</code>, <code>unsafePerformIO</code>),
   
* break [https://okmij.org/ftp/Haskell/index.html#lazyIO-not-True equational reasoning] (<code>unsafeInterleaveIO</code>),
+
* or break [https://okmij.org/ftp/Haskell/index.html#lazyIO-not-True equational reasoning] (<code>unsafeInterleaveIO</code>).
   
 
Their use would require some kind of assurance on the part of the programmer that what they're doing is safe.
* or weaken[https://arxiv.org/pdf/2105.03389v3 <span></span>][https://web.archive.org/web/20210506015257/https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.71.1777&rep=rep1&type=pdf <span></span>] [http://en.wikipedia.org/wiki/Parametricity parametricity] ([[Seq|<code>seq</code>]]).
 
 
Their use (except in the case of <code>seq</code>) would require some
 
kind of assurance on the part of the programmer that what they're doing
 
is safe.
 
   
 
<code>unsafe</code> is also a keyword which can be used in a [http://haskell.org/haskellwiki/Performance/FFI foreign import declaration].
 
<code>unsafe</code> is also a keyword which can be used in a [http://haskell.org/haskellwiki/Performance/FFI foreign import declaration].

Revision as of 12:00, 24 September 2024

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.

  • unsafeLocalState :: IO a -> a
  • unsafePerformIO :: IO a -> a
  • inlinePerformIO :: 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

Unsafe functions can:

  • break type safety (unsafeCoerce#, unsafeLocalState, unsafePerformIO),

Their use 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.