Difference between revisions of "Unsafe functions"

From HaskellWiki
Jump to navigation Jump to search
m (Exclude seq and pseq: see Seq for details)
m (Initial quote replaced)
 
Line 1: Line 1:
  +
<blockquote>
<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
 
  +
[Using the FFI] you can import any C function with a pure type,
A colleague [...] asked me today whether I know how to use <code>unsafePerformIO</code> safely. And I realized I have no idea. [...]
 
  +
which also allows you to wreak arbitrary havoc. We enable
  +
the user to disguise arbitrary machine code as a Haskell
  +
function of essentially arbitrary type. In comparison,
  +
<code>unsafePerformIO</code> seems angelic.
   
  +
:<small>[[QuotesPage|Manuel Chakravarty]]</small>
<small>[https://discourse.haskell.org/t/using-unsafeperformio-safely/4146 Richard Eisenberg.]</small>
 
</div>
+
</blockquote>
   
<sub> </sub>
 
 
There are a number of '''unsafe functions''' in the libraries.
 
There are a number of '''unsafe functions''' in the libraries.
   
Line 27: Line 30:
   
 
<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].
 
   
 
{{stub}}
 
{{stub}}

Latest revision as of 12:02, 24 September 2024

[Using the FFI] you can import any C function with a pure type, which also allows you to wreak arbitrary havoc. We enable the user to disguise arbitrary machine code as a Haskell function of essentially arbitrary type. In comparison, unsafePerformIO seems angelic.

Manuel Chakravarty

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.