GHC.IO (original) (raw)

Documentation

newtype IO a Source #

A value of type `[IO](GHC-IO.html#t:IO "GHC.IO")` a is a computation which, when performed, does some I/O before returning a value of type a.

There is really only one way to "perform" an I/O action: bind it toMain.main in your program. When your program is run, the I/O will be performed. It isn't possible to perform I/O from an arbitrary function, unless that function is itself in the [IO](GHC-IO.html#t:IO "GHC.IO") monad and called at some point, directly or indirectly, from Main.main.

[IO](GHC-IO.html#t:IO "GHC.IO") is a monad, so [IO](GHC-IO.html#t:IO "GHC.IO") actions can be combined using either the do-notation or the [>>](Prelude.html#v:-62--62- "Prelude") and [>>=](Prelude.html#v:-62--62--61- "Prelude") operations from the [Monad](Prelude.html#v:Monad "Prelude")class.

unsafePerformIO :: IO a -> a Source #

This is the "back door" into the [IO](GHC-IO.html#t:IO "GHC.IO") monad, allowing[IO](GHC-IO.html#t:IO "GHC.IO") computation to be performed at any time. For this to be safe, the [IO](GHC-IO.html#t:IO "GHC.IO") computation should be free of side effects and independent of its environment.

If the I/O computation wrapped in [unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO") performs side effects, then the relative order in which those side effects take place (relative to the main I/O trunk, or other calls to[unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO")) is indeterminate. Furthermore, when using[unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO") to cause side-effects, you should take the following precautions to ensure the side effects are performed as many times as you expect them to be. Note that these precautions are necessary for GHC, but may not be sufficient, and other compilers may require different precautions:

It is less well known that[unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO") is not type safe. For example:

test :: IORef [a]
test = unsafePerformIO $ newIORef []

main = do
        writeIORef test [42]
        bang <- readIORef test
        print (bang :: [Char])

This program will core dump. This problem with polymorphic references is well known in the ML community, and does not arise with normal monadic use of references. There is no easy way to make it impossible once you use [unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO"). Indeed, it is possible to write coerce :: a -> b with the help of [unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO"). So be careful!

WARNING: If you're looking for "a way to get a [String](Data-String.html#t:String "Data.String") from an 'IO String'", then [unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO") is not the way to go. Learn about do-notation and the<- syntax element before you proceed.

unsafeDupablePerformIO :: IO a -> a Source #

This version of [unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO") is more efficient because it omits the check that the IO is only being performed by a single thread. Hence, when you use [unsafeDupablePerformIO](GHC-IO.html#v:unsafeDupablePerformIO "GHC.IO"), there is a possibility that the IO action may be performed multiple times (on a multiprocessor), and you should therefore ensure that it gives the same results each time. It may even happen that one of the duplicated IO actions is only run partially, and then interrupted in the middle without an exception being raised. Therefore, functions like [bracket](Control-Exception.html#v:bracket "Control.Exception") cannot be used safely within[unsafeDupablePerformIO](GHC-IO.html#v:unsafeDupablePerformIO "GHC.IO").

Since: base-4.4.0.0

noDuplicate :: IO () Source #

Ensures that the suspensions under evaluation by the current thread are unique; that is, the current thread is not evaluating anything that is also under evaluation by another thread that has also executed[noDuplicate](GHC-IO.html#v:noDuplicate "GHC.IO").

This operation is used in the definition of [unsafePerformIO](GHC-IO.html#v:unsafePerformIO "GHC.IO") to prevent the IO action from being executed multiple times, which is usually undesirable.

stToIO :: ST RealWorld a -> IO a Source #

Embed a strict state thread in an [IO](GHC-IO.html#t:IO "GHC.IO") action. The [RealWorld](Control-Monad-ST-Safe.html#t:RealWorld "Control.Monad.ST.Safe") parameter indicates that the internal state used by the [ST](Control-Monad-ST-Safe.html#t:ST "Control.Monad.ST.Safe") computation is a special one supplied by the [IO](GHC-IO.html#t:IO "GHC.IO") monad, and thus distinct from those used by invocations of [runST](Control-Monad-ST-Safe.html#v:runST "Control.Monad.ST.Safe").

unsafeIOToST :: IO a -> ST s a Source #

Convert an [IO](GHC-IO.html#t:IO "GHC.IO") action to an [ST](Control-Monad-ST-Safe.html#t:ST "Control.Monad.ST.Safe") action. This relies on [IO](GHC-IO.html#t:IO "GHC.IO") and [ST](Control-Monad-ST-Safe.html#t:ST "Control.Monad.ST.Safe") having the same representation modulo the constraint on the state thread type parameter.

type FilePath = String Source #

File and directory names are values of type [String](Data-String.html#t:String "Data.String"), whose precise meaning is operating system dependent. Files can be opened, yielding a handle which can then be used to operate on the contents of that file.

catch Source #

Arguments

:: Exception e
=> IO a The computation to run
-> (e -> IO a) Handler to invoke if an exception is raised
-> IO a

This is the simplest of the exception-catching functions. It takes a single argument, runs it, and if an exception is raised the "handler" is executed, with the value of the exception passed as an argument. Otherwise, the result is returned as normal. For example:

catch (readFile f) (\e -> do let err = show (e :: IOException) hPutStr stderr ("Warning: Couldn't open " ++ f ++ ": " ++ err) return "")

Note that we have to give a type signature to e, or the program will not typecheck as the type is ambiguous. While it is possible to catch exceptions of any type, see the section "Catching all exceptions" (in Control.Exception) for an explanation of the problems with doing so.

For catching exceptions in pure (non-[IO](GHC-IO.html#t:IO "GHC.IO")) expressions, see the function [evaluate](GHC-IO.html#v:evaluate "GHC.IO").

Note that due to Haskell's unspecified evaluation order, an expression may throw one of several possible exceptions: consider the expression (error "urk") + (1 `div` 0). Does the expression throwErrorCall "urk", or DivideByZero?

The answer is "it might throw either"; the choice is non-deterministic. If you are catching any type of exception then you might catch either. If you are calling catch with typeIO Int -> (ArithException -> IO Int) -> IO Int then the handler may get run with DivideByZero as an argument, or an ErrorCall "urk" exception may be propagated further up. If you call it again, you might get the opposite behaviour. This is ok, because [catch](GHC-IO.html#v:catch "GHC.IO") is an[IO](GHC-IO.html#t:IO "GHC.IO") computation.

catchException :: Exception e => IO a -> (e -> IO a) -> IO a Source #

Catch an exception in the [IO](GHC-IO.html#t:IO "GHC.IO") monad.

Note that this function is strict in the action. That is,catchException undefined b == _|_. See for details.

catchAny :: IO a -> (forall e. Exception e => e -> IO a) -> IO a Source #

Catch any [Exception](Control-Exception-Base.html#t:Exception "Control.Exception.Base") type in the [IO](GHC-IO.html#t:IO "GHC.IO") monad.

Note that this function is strict in the action. That is,catchAny undefined b == _|_. See for details.

throwIO :: Exception e => e -> IO a Source #

A variant of [throw](Control-Exception-Base.html#v:throw "Control.Exception.Base") that can only be used within the [IO](GHC-IO.html#t:IO "GHC.IO") monad.

Although [throwIO](GHC-IO.html#v:throwIO "GHC.IO") has a type that is an instance of the type of [throw](Control-Exception-Base.html#v:throw "Control.Exception.Base"), the two functions are subtly different:

throw e seq () ===> throw e throwIO e seq () ===> ()

The first example will cause the exception e to be raised, whereas the second one won't. In fact, [throwIO](GHC-IO.html#v:throwIO "GHC.IO") will only cause an exception to be raised when it is used within the [IO](GHC-IO.html#t:IO "GHC.IO") monad.

The [throwIO](GHC-IO.html#v:throwIO "GHC.IO") variant should be used in preference to [throw](Control-Exception-Base.html#v:throw "Control.Exception.Base") to raise an exception within the [IO](GHC-IO.html#t:IO "GHC.IO") monad because it guarantees ordering with respect to other operations, whereas [throw](Control-Exception-Base.html#v:throw "Control.Exception.Base") does not. We say that [throwIO](GHC-IO.html#v:throwIO "GHC.IO") throws *precise* exceptions and[throw](Control-Exception-Base.html#v:throw "Control.Exception.Base"), [error](Prelude.html#v:error "Prelude"), etc. all throw *imprecise* exceptions. For example

throw e + error "boom" ===> error "boom" throw e + error "boom" ===> throw e

are both valid reductions and the compiler may pick any (loop, even), whereas

throwIO e >> error "boom" ===> throwIO e

will always throw e when executed.

See also theGHC wiki page on precise exceptions for a more technical introduction to how GHC optimises around precise vs. imprecise exceptions.

mask :: ((forall a. IO a -> IO a) -> IO b) -> IO b Source #

Executes an IO computation with asynchronous exceptions masked. That is, any thread which attempts to raise an exception in the current thread with [throwTo](Control-Exception.html#v:throwTo "Control.Exception") will be blocked until asynchronous exceptions are unmasked again.

The argument passed to [mask](GHC-IO.html#v:mask "GHC.IO") is a function that takes as its argument another function, which can be used to restore the prevailing masking state within the context of the masked computation. For example, a common way to use [mask](GHC-IO.html#v:mask "GHC.IO") is to protect the acquisition of a resource:

mask $ \restore -> do x <- acquire restore (do_something_with x) onException release release

This code guarantees that acquire is paired with release, by masking asynchronous exceptions for the critical parts. (Rather than write this code yourself, it would be better to use[bracket](Control-Exception.html#v:bracket "Control.Exception") which abstracts the general pattern).

Note that the restore action passed to the argument to [mask](GHC-IO.html#v:mask "GHC.IO") does not necessarily unmask asynchronous exceptions, it just restores the masking state to that of the enclosing context. Thus if asynchronous exceptions are already masked, [mask](GHC-IO.html#v:mask "GHC.IO") cannot be used to unmask exceptions again. This is so that if you call a library function with exceptions masked, you can be sure that the library call will not be able to unmask exceptions again. If you are writing library code and need to use asynchronous exceptions, the only way is to create a new thread; see [forkIOWithUnmask](Control-Concurrent.html#v:forkIOWithUnmask "Control.Concurrent").

Asynchronous exceptions may still be received while in the masked state if the masked thread blocks in certain ways; seeControl.Exception.

Threads created by [forkIO](Control-Concurrent.html#v:forkIO "Control.Concurrent") inherit the[MaskingState](GHC-IO.html#t:MaskingState "GHC.IO") from the parent; that is, to start a thread in the[MaskedInterruptible](GHC-IO.html#v:MaskedInterruptible "GHC.IO") state, use mask_ $ forkIO .... This is particularly useful if you need to establish an exception handler in the forked thread before any asynchronous exceptions are received. To create a new thread in an unmasked state use [forkIOWithUnmask](Control-Concurrent.html#v:forkIOWithUnmask "Control.Concurrent").

uninterruptibleMask :: ((forall a. IO a -> IO a) -> IO b) -> IO b Source #

Like [mask](GHC-IO.html#v:mask "GHC.IO"), but the masked computation is not interruptible (seeControl.Exception). THIS SHOULD BE USED WITH GREAT CARE, because if a thread executing in [uninterruptibleMask](GHC-IO.html#v:uninterruptibleMask "GHC.IO") blocks for any reason, then the thread (and possibly the program, if this is the main thread) will be unresponsive and unkillable. This function should only be necessary if you need to mask exceptions around an interruptible operation, and you can guarantee that the interruptible operation will only block for a short period of time.

data MaskingState Source #

Describes the behaviour of a thread when an asynchronous exception is received.

Instances

Instances details

interruptible :: IO a -> IO a Source #

Allow asynchronous exceptions to be raised even inside [mask](GHC-IO.html#v:mask "GHC.IO"), making the operation interruptible (see the discussion of "Interruptible operations" in [Exception](Control.html#v:Exception "Control")).

When called outside [mask](GHC-IO.html#v:mask "GHC.IO"), or inside [uninterruptibleMask](GHC-IO.html#v:uninterruptibleMask "GHC.IO"), this function has no effect.

Since: base-4.9.0.0

bracket Source #

Arguments

:: IO a computation to run first ("acquire resource")
-> (a -> IO b) computation to run last ("release resource")
-> (a -> IO c) computation to run in-between
-> IO c

finally Source #

Arguments

:: IO a computation to run first
-> IO b computation to run afterward (even if an exception was raised)
-> IO a

evaluate :: a -> IO a Source #

Evaluate the argument to weak head normal form.

[evaluate](GHC-IO.html#v:evaluate "GHC.IO") is typically used to uncover any exceptions that a lazy value may contain, and possibly handle them.

[evaluate](GHC-IO.html#v:evaluate "GHC.IO") only evaluates to weak head normal form. If deeper evaluation is needed, the force function from Control.DeepSeq may be handy:

evaluate $ force x

There is a subtle difference between `[evaluate](GHC-IO.html#v:evaluate "GHC.IO")` x and `[return](Control-Monad.html#v:return "Control.Monad")` `[$!](Prelude.html#v:-36--33- "Prelude")` x, analogous to the difference between [throwIO](GHC-IO.html#v:throwIO "GHC.IO") and [throw](Control-Exception-Base.html#v:throw "Control.Exception.Base"). If the lazy value x throws an exception, `[return](Control-Monad.html#v:return "Control.Monad")` `[$!](Prelude.html#v:-36--33- "Prelude")` x will fail to return an[IO](GHC-IO.html#t:IO "GHC.IO") action and will throw an exception instead. `[evaluate](GHC-IO.html#v:evaluate "GHC.IO")` x, on the other hand, always produces an [IO](GHC-IO.html#t:IO "GHC.IO") action; that action will throw an exception upon execution iff x throws an exception upon evaluation.

The practical implication of this difference is that due to the_imprecise exceptions_ semantics,

(return $! error "foo") >> error "bar"

may throw either "foo" or "bar", depending on the optimizations performed by the compiler. On the other hand,

evaluate (error "foo") >> error "bar"

is guaranteed to throw "foo".

The rule of thumb is to use [evaluate](GHC-IO.html#v:evaluate "GHC.IO") to force or handle exceptions in lazy values. If, on the other hand, you are forcing a lazy value for efficiency reasons only and do not care about exceptions, you may use `[return](Control-Monad.html#v:return "Control.Monad")` `[$!](Prelude.html#v:-36--33- "Prelude")` x.