The IO monad

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.

Files and handles

File and directory names are values of type 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.

Haskell defines operations to read and write characters from and to files,
represented by values of type Handle. Each value of this type is a
handle: a record used by the Haskell run-time system to manage I/O
with file system objects. A handle has at least the following properties:

whether it manages input or output or both;

whether it is open, closed or semi-closed;

whether the object is seekable;

whether buffering is disabled, or enabled on a line or block basis;

a buffer (whose length may be zero).

Most handles will also have a current I/O position indicating where the next
input or output operation will occur. A handle is readable if it
manages only input or both input and output; likewise, it is writable if
it manages only output or both input and output. A handle is open when
first allocated.
Once it is closed it can no longer be used for either input or output,
though an implementation cannot re-use its storage while references
remain to it. Handles are in the Show and Eq classes. The string
produced by showing a handle is system dependent; it should include
enough information to identify the handle for debugging. A handle is
equal according to == only to itself; no attempt
is made to compare the internal state of different handles for equality.

GHC note: a Handle will be automatically closed when the garbage
collector detects that it has become unreferenced by the program.
However, relying on this behaviour is not generally recommended:
the garbage collector is unpredictable. If possible, use
an explicit hClose to close Handles when they are no longer
required. GHC does not currently attempt to free up file
descriptors when they have run out, it is your responsibility to
ensure that this doesn't happen.

Standard handles

Three handles are allocated during program initialisation,
and are initially open.

Opening and closing files

Opening files

withFile name mode act opens a file using openFile and passes
the resulting handle to the computation act. The handle will be
closed on exit from withFile, whether by normal termination or by
raising an exception. If closing the handle raises an exception, then
this exception will be raised by withFile rather than any exception
raised by act.

If the file does not exist and it is opened for output, it should be
created as a new file. If mode is WriteMode and the file
already exists, then it should be truncated to zero length.
Some operating systems delete empty files, so there is no guarantee
that the file will exist following an openFile with modeWriteMode unless it is subsequently written to successfully.
The handle is positioned at the end of the file if mode is
AppendMode, and otherwise at the beginning (in which case its
internal position is 0).
The initial buffer mode is implementation-dependent.

This operation may fail with:

isAlreadyInUseError if the file is already open and cannot be reopened;

isDoesNotExistError if the file does not exist; or

isPermissionError if the user does not have permission to open the file.

Note: if you will be working with files containing binary data, you'll want to
be using openBinaryFile.

Closing files

Computation hClosehdl makes handle hdl closed. Before the
computation finishes, if hdl is writable its buffer is flushed as
for hFlush.
Performing hClose on a handle that has already been closed has no effect;
doing so is not an error. All other operations on a closed handle will fail.
If hClose fails for any reason, any further operations (apart from
hClose) on the handle will still fail as if hdl had been successfully
closed.

The computation appendFilefile str function appends the string str,
to the file file.

Note that writeFile and appendFile write a literal string
to a file. To write a value of any printable type, as with print,
use the show function to convert the value to a string first.

main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])

File locking

Implementations should enforce as far as possible, at least locally to the
Haskell process, multiple-reader single-writer locking on files.
That is, there may either be many handles on the same file which manage input, or just one handle on the file which manages output. If any
open or semi-closed handle is managing a file for output, no new
handle can be allocated for that file. If any open or semi-closed
handle is managing a file for input, new handles can only be allocated
if they do not manage output. Whether two files are the same is
implementation-dependent, but they should normally be the same if they
have the same absolute path name and neither has been renamed, for
example.

Warning: the readFile operation holds a semi-closed handle on
the file until the entire contents of the file have been consumed.
It follows that an attempt to write to a file (using writeFile, for
example) that was earlier opened by readFile will usually result in
failure with isAlreadyInUseError.

Detecting the end of input

For a readable handle hdl, hIsEOFhdl returns
True if no further input can be taken from hdl or for a
physical file, if the current I/O position is equal to the length of
the file. Otherwise, it returns False.

NOTE: hIsEOF may block, because it has to attempt to read from
the stream to determine whether there is any more data to be read.

Buffering operations

Three kinds of buffering are supported: line-buffering,
block-buffering or no-buffering. These modes have the following
effects. For output, items are written out, or flushed,
from the internal buffer according to the buffer mode:

line-buffering: the entire output buffer is flushed
whenever a newline is output, the buffer overflows,
a hFlush is issued, or the handle is closed.

block-buffering: the entire buffer is written out whenever it
overflows, a hFlush is issued, or the handle is closed.

no-buffering: output is written immediately, and never stored
in the buffer.

An implementation is free to flush the buffer more frequently,
but not less frequently, than specified above.
The output buffer is emptied as soon as it has been written out.

Similarly, input occurs according to the buffer mode for the handle:

line-buffering: when the buffer for the handle is not empty,
the next item is obtained from the buffer; otherwise, when the
buffer is empty, characters up to and including the next newline
character are read into the buffer. No characters are available
until the newline character is available or the buffer is full.

block-buffering: when the buffer for the handle becomes empty,
the next block of data is read into the buffer.

no-buffering: the next input item is read and returned.
The hLookAhead operation implies that even a no-buffered
handle may require a one-character buffer.

The default buffering mode when a handle is opened is
implementation-dependent and may depend on the file system object
which is attached to that handle.
For most implementations, physical files will normally be block-buffered
and terminals will normally be line-buffered.

Computation hSeekhdl mode i sets the position of handle
hdl depending on mode.
The offset i is given in terms of 8-bit bytes.

If hdl is block- or line-buffered, then seeking to a position which is not
in the current buffer will first cause any items in the output buffer to be
written to the device, and then cause the input buffer to be discarded.
Some handles may not be seekable (see hIsSeekable), or only support a
subset of the possible positioning operations (for instance, it may only
be possible to seek to the end of a tape, or to a positive offset from
the beginning or current position).
It is not possible to set a negative I/O position, or for
a physical file, an I/O position beyond the current end-of-file.

This operation may fail with:

isIllegalOperationError if the Handle is not seekable, or does
not support the requested seek mode.

Computation hTellhdl returns the current position of the
handle hdl, as the number of bytes from the beginning of
the file. The value returned may be subsequently passed to
hSeek to reposition the handle to the current position.

Text input and output

Text input

Computation hWaitForInputhdl t
waits until input is available on handle hdl.
It returns True as soon as input is available on hdl,
or False if no input is available within t milliseconds. Note that
hWaitForInput waits until one or more full characters are available,
which means that it needs to do decoding, and hence may fail
with a decoding error.

a decoding error, if the input begins with an invalid byte sequence
in this Handle's encoding.

NOTE for GHC users: unless you use the -threaded flag,
hWaitForInput t where t >= 0 will block all other Haskell
threads for the duration of the call. It behaves like a
safe foreign call in this respect.

Computation hGetContentshdl returns the list of characters
corresponding to the unread portion of the channel or file managed
by hdl, which is put into an intermediate state, semi-closed.
In this state, hdl is effectively closed,
but items are read from hdl on demand and accumulated in a special
list returned by hGetContentshdl.

Any operation that fails because a handle is closed,
also fails if a handle is semi-closed. The only exception is hClose.
A semi-closed handle becomes closed:

if hClose is applied to it;

if an I/O error occurs when reading an item from the handle;

or once the entire contents of the handle has been read.

Once a semi-closed handle becomes closed, the contents of the
associated list becomes fixed. The contents of this final list is
only partially specified: it will contain at least all the items of
the stream that were evaluated prior to the handle becoming closed.

Any I/O errors encountered while a handle is semi-closed are simply
discarded.

Special cases for standard input and output

The interact function takes a function of type String->String
as its argument. The entire input from the standard input device is
passed to this function as its argument, and the resulting string is
output on the standard output device.

The print function outputs a value of any printable type to the
standard output device.
Printable types are those that are instances of class Show; print
converts values to strings for output using the show operation and
adds a newline.

For example, a program to print the first 20 integers and their
powers of 2 could be written as:

Binary input and output

withBinaryFile name mode act opens a file using openBinaryFile
and passes the resulting handle to the computation act. The handle
will be closed on exit from withBinaryFile, whether by normal
termination or by raising an exception.

Like openFile, but open the file in binary mode.
On Windows, reading a file in text mode (which is the default)
will translate CRLF to LF, and writing will translate LF to CRLF.
This is usually what you want with text files. With binary files
this is undesirable; also, as usual under Microsoft operating systems,
text mode treats control-Z as EOF. Binary mode turns off all special
treatment of end-of-line and end-of-file characters.
(See also hSetBinaryMode.)

ResourceVanished if the handle is a pipe or socket, and the
reading end is closed. (If this is a POSIX system, and the program
has not asked to ignore SIGPIPE, then a SIGPIPE may be delivered
instead, whose default action is to terminate the program).

hGetBufhdl buf count reads data from the handle hdl
into the buffer buf until either EOF is reached or
count 8-bit bytes have been read.
It returns the number of bytes actually read. This may be zero if
EOF was reached before any data was read (or if count is zero).

hGetBuf never raises an EOF exception, instead it returns a value
smaller than count.

If the handle is a pipe or socket, and the writing end
is closed, hGetBuf will behave as if EOF was reached.

hGetBufSomehdl buf count reads data from the handle hdl
into the buffer buf. If there is any data available to read,
then hGetBufSome returns it immediately; it only blocks if there
is no data to be read.

It returns the number of bytes actually read. This may be zero if
EOF was reached before any data was read (or if count is zero).

hGetBufSome never raises an EOF exception, instead it returns a value
smaller than count.

If the handle is a pipe or socket, and the writing end
is closed, hGetBufSome will behave as if EOF was reached.

hGetBufNonBlockinghdl buf count reads data from the handle hdl
into the buffer buf until either EOF is reached, or
count 8-bit bytes have been read, or there is no more data available
to read immediately.

The function creates a temporary file in ReadWrite mode.
The created file isn't deleted automatically, so you need to delete it manually.

The file is creates with permissions such that only the current
user can read/write it.

With some exceptions (see below), the file will be created securely
in the sense that an attacker should not be able to cause
openTempFile to overwrite another file on the filesystem using your
credentials, by putting symbolic links (on Unix) in the place where
the temporary file is to be created. On Unix the O_CREAT and
O_EXCL flags are used to prevent this attack, but note that
O_EXCL is sometimes not supported on NFS filesystems, so if you
rely on this behaviour it is best to use local filesystems only.

Unicode encoding/decoding

A text-mode Handle has an associated TextEncoding, which
is used to decode bytes into Unicode characters when reading,
and encode Unicode characters into bytes when writing.

The default TextEncoding is the same as the default encoding
on your system, which is also available as localeEncoding.
(GHC note: on Windows, we currently do not support double-byte
encodings; if the console's code page is unsupported, then
localeEncoding will be latin1.)

Encoding and decoding errors are always detected and reported,
except during lazy I/O (hGetContents, getContents, and
readFile), where a decoding error merely results in
termination of the character stream, as with other I/O errors.

The action hSetEncodinghdlencoding changes the text encoding
for the handle hdl to encoding. The default encoding when a Handle is
created is localeEncoding, namely the default encoding for the current
locale.

To create a Handle with no encoding at all, use openBinaryFile. To
stop further encoding or decoding on an existing Handle, use
hSetBinaryMode.

hSetEncoding may need to flush buffered data in order to change
the encoding.

Note that the TextEncoding remembers nothing about the state of
the encoder/decoder in use on this Handle. For example, if the
encoding in use is UTF-16, then using hGetEncoding and
hSetEncoding to save and restore the encoding may result in an
extra byte-order-mark being written to the file.

The Latin1 (ISO8859-1) encoding. This encoding maps bytes
directly to the first 256 Unicode code points, and is thus not a
complete Unicode encoding. An attempt to write a character greater than
'\255' to a Handle using the latin1 encoding will result in an error.

The UTF-8 Unicode encoding, with a byte-order-mark (BOM; the byte
sequence 0xEF 0xBB 0xBF). This encoding behaves like utf8,
except that on input, the BOM sequence is ignored at the beginning
of the stream, and on output, the BOM sequence is prepended.

The byte-order-mark is strictly unnecessary in UTF-8, but is
sometimes used to identify the encoding of a file.

The set of known encodings is system-dependent, but includes at least:

UTF-8

UTF-16, UTF-16BE, UTF-16LE

UTF-32, UTF-32BE, UTF-32LE

On systems using GNU iconv (e.g. Linux), there is additional
notation for specifying how illegal characters are handled:

a suffix of //IGNORE, e.g. UTF-8//IGNORE, will cause
all illegal sequences on input to be ignored, and on output
will drop all code points that have no representation in the
target encoding.

a suffix of //TRANSLIT will choose a replacement character
for illegal sequences or code points.

On Windows, you can access supported code pages with the prefix
CP; for example, "CP1250".

Newline conversion

In Haskell, a newline is always represented by the character
'\n'. However, in files and external character streams, a
newline may be represented by another character sequence, such
as '\r\n'.

A text-mode Handle has an associated NewlineMode that
specifies how to transate newline characters. The
NewlineMode specifies the input and output translation
separately, so that for instance you can translate '\r\n'
to '\n' on input, but leave newlines as '\n' on output.

Specifies the translation, if any, of newline characters between
internal Strings and the external file or stream. Haskell Strings
are assumed to represent newlines with the '\n' character; the
newline mode specifies how to translate '\n' on output, and what to
translate into '\n' on input.

Map '\r\n' into '\n' on input, and '\n' to the native newline
represetnation on output. This mode can be used on any platform, and
works with text files using any newline convention. The downside is
that readFile >>= writeFile might yield a different file.