IO package:relude

A value of type 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 to Main.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 monad and called at some point, directly or indirectly, from Main.main. IO is a monad, so IO actions can be combined using either the do-notation or the >> and >>= operations from the Monad class.
Lifted reexports from Data.IORef module.
A mutable variable in the IO monad.
>>> import GHC.Internal.Data.IORef

>>> r <- newIORef 0

>>> readIORef r
0

>>> writeIORef r 1

>>> readIORef r
1

>>> atomicWriteIORef r 2

>>> readIORef r
2

>>> modifyIORef' r (+ 1)

>>> readIORef r
3

>>> atomicModifyIORef' r (\a -> (a + 1, ()))

>>> readIORef r
4
See also STRef and MVar.
One or none. It is useful for modelling any computation that is allowed to fail.

Examples

Using the Alternative instance of Control.Monad.Except, the following functions:
>>> import Control.Monad.Except
>>> canFail = throwError "it failed" :: Except String Int

>>> final = return 42                :: Except String Int
Can be combined by allowing the first function to fail:
>>> runExcept $ canFail *> final
Left "it failed"
>>> runExcept $ optional canFail *> final
Right 42
Re-exports most useful functionality from the Control.Exception module. Also provides some convenient utilities to throw and handle exceptions.
Any type that you wish to throw or catch as an exception must be an instance of the Exception class. The simplest case is a new exception type directly below the root:
data MyException = ThisException | ThatException
deriving Show

instance Exception MyException
The default method definitions in the Exception class do what we need in this case. You can now throw and catch ThisException and ThatException as exceptions:
*Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException))
Caught ThisException
In more complicated examples, you may wish to define a whole hierarchy of exceptions:
---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compiler

data SomeCompilerException = forall e . Exception e => SomeCompilerException e

instance Show SomeCompilerException where
show (SomeCompilerException e) = show e

instance Exception SomeCompilerException

compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerException

compilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
SomeCompilerException a <- fromException x
cast a

---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compiler

data SomeFrontendException = forall e . Exception e => SomeFrontendException e

instance Show SomeFrontendException where
show (SomeFrontendException e) = show e

instance Exception SomeFrontendException where
toException = compilerExceptionToException
fromException = compilerExceptionFromException

frontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendException

frontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
SomeFrontendException a <- fromException x
cast a

---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exception

data MismatchedParentheses = MismatchedParentheses
deriving Show

instance Exception MismatchedParentheses where
toException   = frontendExceptionToException
fromException = frontendExceptionFromException
We can now catch a MismatchedParentheses exception as MismatchedParentheses, SomeFrontendException or SomeCompilerException, but not other types, e.g. IOException:
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: IOException))
*** Exception: MismatchedParentheses
The SomeException type is the root of the exception type hierarchy. When an exception of type e is thrown, behind the scenes it is encapsulated in a SomeException.
Render this exception value in a human-friendly manner. Default implementation: show. @since base-4.8.0.0
toException should produce a SomeException with no attached ExceptionContext.
bior returns the disjunction of a container of Bools. For the result to be False, the container must be finite; True, however, results from a True value finitely far from the left end.

Examples

Basic usage:
>>> bior (True, False)
True
>>> bior (False, False)
False
>>> bior (Left True)
True
Empty structures yield False:
>>> bior (BiList [] [])
False
A True value finitely far from the left end yields True (short circuit):
>>> bior (BiList [False, False, True, False] (repeat False))
True
A True value infinitely far from the left end hangs:
> bior (BiList (repeat False) [True])
* Hangs forever *
An infinitely False value hangs:
> bior (BiList (repeat False) [])
* Hangs forever *
This module reexports very basic and primitive functions and function combinators.
Lifted to MonadIO version of newEmptyTMVarIO.
Lifted to MonadIO version of newTMVarIO.
Lifted to MonadIO version of newTVarIO.
Lifted to MonadIO version of readTVarIO.
Lifted version of atomicModifyIORef.
>>> ref <- newIORef 42

>>> atomicModifyIORef ref (\a -> (a, a + 3))
45

>>> readIORef ref
42
Lifted version of atomicModifyIORef'.
>>> ref <- newIORef 42

>>> atomicModifyIORef' ref (\a -> (a, a + 3))
45

>>> readIORef ref
42
Version of atomicModifyIORef' that discards return value. Useful when you want to update IORef but not interested in the returning result.
>>> ref <- newIORef 42

>>> atomicModifyIORef'_ ref (`div` 2)

>>> readIORef ref
21
Version of atomicModifyIORef that discards return value. Useful when you want to update IORef but not interested in the returning result.
>>> ref <- newIORef 42

>>> atomicModifyIORef_ ref (`div` 2)

>>> readIORef ref
21
Lifted version of atomicWriteIORef.
>>> ref <- newIORef 42

>>> atomicWriteIORef ref 45

>>> readIORef ref
45
Lifted version of modifyIORef.
>>> ref <- newIORef 42

>>> modifyIORef ref (\a -> a + 6)

>>> readIORef ref
48
Lifted version of modifyIORef'.
>>> ref <- newIORef 42

>>> modifyIORef' ref (\a -> a + 3)

>>> readIORef ref
45