Difference between revisions of "Unsafe functions"

From HaskellWiki
Jump to: navigation, search
 
m (Added initial quote)
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 +
I think <code>unsafePerformIO</code> might actually be a pretty good alternative, we just need to understand how to use it safely [...]
 +
<tt>[https://discourse.haskell.org/t/using-unsafeperformio-safely/4146/49 Victor Miraldo.]</tt>
 +
</div>
 +
 +
<sub> </sub>
 
There are a number of '''unsafe functions''' in the libraries.
 
There are a number of '''unsafe functions''' in the libraries.
  
* <hask>unsafePerformIO</hask>
+
* <hask>unsafePerformIO :: IO a -> a</hask>
* <hask>unsafeInterleaveIO</hask>
+
* <hask>unsafeInterleaveIO :: IO a -> IO a</hask>
 +
* <hask>unsafeInterleaveST :: ST s a -> ST s a</hask>
 +
* <hask>unsafeIOToST :: IO a -> ST s a</hask>
 +
* <hask>unsafeIOToSTM :: IO a -> STM a</hask>
 +
* <hask>unsafeFreeze, unsafeThaw</hask>
 +
* <hask>unsafeCoerce# :: a -> b</hask>
 +
* <hask>seq :: a -> b -> b</hask>
 +
 
 +
Unsafe functions can break type safety (unsafeCoerce#, unsafePerformIO),
 +
interfere with lazy IO (unsafeInterleaveIO), or break [http://en.wikipedia.org/wiki/Parametricity parametricity] (seq).
 +
Their use (except in the case of <hask>seq</hask>) 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 [http://haskell.org/haskellwiki/Performance/FFI foreign import declaration].
  
 
{{stub}}
 
{{stub}}
 +
 +
[[Category:Idioms]]

Latest revision as of 23:50, 10 March 2022

I think unsafePerformIO might actually be a pretty good alternative, we just need to understand how to use it safely [...] Victor Miraldo.

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), interfere with lazy IO (unsafeInterleaveIO), or break parametricity (seq). 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.