Questions tagged [unsafe-perform-io]

This tag is for questions concerning the function `unsafePerformIO` in Haskell and Scala, which lets you do arbitrary IO inside a pure expression.

29 questions
46
votes
8 answers

Is Haskell really a purely functional language considering unsafePerformIO?

Haskell is generally referenced as an example of a purely functional language. How can this be justified given the existence of System.IO.Unsafe.unsafePerformIO ? Edit: I thought with "purely functional" it was meant that it is impossible to…
26
votes
4 answers

Am I abusing unsafePerformIO?

To get acquainted with unsafePerformIO (how to use it and when to use it), I've implemented a module for generating unique values. Here's what I have: module Unique (newUnique) where import Data.IORef import System.IO.Unsafe (unsafePerformIO) --…
Alexander Vieth
  • 876
  • 6
  • 9
21
votes
1 answer

What is the difference between unsafeDupablePerformIO and accursedUnutterablePerformIO?

I was wandering in the Restricted Section of the Haskell Library and found these two vile spells: {- System.IO.Unsafe -} unsafeDupablePerformIO :: IO a -> a unsafeDupablePerformIO (IO m) = case runRW# m of (# _, a #) -> a {-…
radrow
  • 6,419
  • 4
  • 26
  • 53
19
votes
5 answers

A way to avoid a common use of unsafePerformIO

I often find this pattern in Haskell code: options :: MVar OptionRecord options = unsafePerformIO $ newEmptyMVar ... doSomething :: Foo -> Bar doSomething = unsafePerformIO $ do opt <- readMVar options doSomething' where ... Basically, one…
fuz
  • 88,405
  • 25
  • 200
  • 352
13
votes
2 answers

unsafePerformIO and FFI library initialization

I'm creating an FFI module to a library in C which wants a 1-time, non-reentrant function to be called before anything else is. This call is idempotent, but stateful, so I could just call it in every Haskell call. But it's slow and due to…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
10
votes
6 answers

Generate a random string at compile time or run time and use it in the rest of the program

What would be the best way to do this? unsafePerformIO? Template Haskell? Something else? I have never used either of those so I don't know many of the details of using them. Note that the program will be compiled every time it is run, so it doesn't…
Drew
  • 12,578
  • 11
  • 58
  • 98
8
votes
2 answers

Show for IO types

I have a data type which contains an IORef as an important element. This means there is not a clean way to make it a member of the show type class. This is not too bad as I have a print function in the IO monad for this type. But it is annoying in…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
7
votes
3 answers

How to write a haskell function without IO in type sig by hiding 'state' changes

I wrote a function in haskell that takes a few parameters like Word32, String (ignore currying) and outputs IO Word32. Now, this is a function in the true sense: for the same inputs, the output will always be the same. There are no side-effects. The…
7
votes
2 answers

How to know when an apparently pure Haskell interface hides unsafe operations?

I have been reading about unsafePerformIO lately, and I would like to ask you something. I'm OK with the fact that a real language should be able to interact with the external environment, so unsafePerformIO is somewhat justified. However, at the…
Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
6
votes
1 answer

How can unsafePerformIO be used to write unsafeCoerce?

It's widely understood that unsafePerformIO is not type safe. This is typically demonstrated by using it to implement unsafeCoerce: box :: IORef a box = unsafePerformIO (newIORef undefined) {-# NOINLINE box #-} unsafeCoerce :: a -> b unsafeCoerce a…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
6
votes
5 answers

Departmental restriction against unsafePerformIO

There has been some talk at work about making it a department-wide policy of prohibiting the use of unsafePerformIO and its ilk. Personally, I don't really mind as I've always maintained that if I found myself wanting to use it, it usually meant…
clintm
  • 1,259
  • 10
  • 23
6
votes
1 answer

Safe to use unsafeIOToSTM to read from database?

In this pseudocode block: atomically $ do if valueInLocalStorage key then readValueFromLocalStorage key else do value <- unsafeIOToSTM $ fetchValueFromDatabase key writeValueToLocalStorage key value Is it safe to use…
Philip Kamenarsky
  • 2,757
  • 2
  • 24
  • 30
6
votes
2 answers

Risks of using unsafeperformIO on randomIO

I am creating a Haskell application that generates a random number on an infinite loop (only when requested by a client). However, I should only use pure functions for that purpose. Is it safe to wrap randomIO with unsafeperformIO without any…
Axel Advento
  • 2,995
  • 3
  • 24
  • 32
5
votes
2 answers

How do use putStrLn for tracing (Haskell)

I am trying to get a Haskell function to show whenever it is applied by adding a call to "putStrLn": isPrime2 1 = False isPrime2 n = do putStrLn n null (filter (==0) (map (mod n) (filter isPrime2 [2..(floor (sqrt(fromIntegral…
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
4
votes
3 answers

Confusion over IORefs to make a counter

I found some sample code, and changed it a little counter = unsafePerform $ newIORef 0 newNode _ = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i Which…
Squidly
  • 2,707
  • 19
  • 43
1
2