[Prev] [Up] [Next]


Standard I/O "PreludeStdIO"

This module defines Haskell handles and the operations which are supported for them.

Haskell interfaces to the external world through an abstract file system. This file system is a collection of named file system objects, which may be organised in directories (see LibDirectory). In some implementations, directories may themselves be file system objects and could be entries in other directories. For simplicity, any non-directory file system object is termed a file, although it could in fact be a communication channel, or any other object recognised by the operating system. Physical files are persistent, ordered files, and normally reside on disk.

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.


> interface PreludeStdIO where

> import PreludeMonadicIO
> import PreludeIOError

> type Handle
> type FilePath = String


Handles

The standard defines operations to read/write finite sequences of items from/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 operating system objects.

A handle has at least the following properties:

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 (Rationale).

Semi-Closed Handles

The operation hGetContents puts a handle hdl 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 stream returned by hGetContents hdl. (Rationale)

Any operation except for hClose that fails because a handle is closed, also fails if a handle is semi-closed. A semi-closed handle becomes closed:

Once a semi-closed handle becomes closed, the contents of the associated stream becomes fixed, and is the list of those items which were successfully read from that handle. Any I/O errors encountered when a handle is semi-closed are simply discarded. (Rationale)

Standard Handles

> stdin, stdout, stderr :: Handle

Three handles are allocated during program initialisation . The first two manage input or output from the Haskell program's standard input or output channel respectively (Rationale). The third manages output to the standard error channel (Rationale). These handles are initially open.

Opening and Closing Files

Opening Files

(Rationale)

> data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode

> openFile      :: FilePath -> IOMode -> IO Handle

Computation openFile file mode allocates and returns a new, open handle to manage the file file. It manages input if mode is ReadMode, output if mode is WriteMode or AppendMode, and both input and output if mode is ReadWriteMode (Rationale).

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 (note: some operating systems delete empty files, so there is no guarantee that the file will exist following an openFile with mode WriteMode 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 I/O position is 0). The initial buffer mode is implementation-dependent.

The computation may fail with:

If openFile fails on a file opened for output, the file may still have been created if it did not already exist.

Implementations should enforce, locally to the Haskell process, multiple-reader single-writer locking on files, which is to say that 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.

Two physical files are the same if they have the same absolute name. An implementation is free to impose stricter conditions. (Rationale)

Closing Files

> hClose        :: Handle -> IO () 

Computation hClose hdl makes handle hdl closed. Before the computation finishes, if hdl is writable its buffer is flushed as for hFlush.

The computation may fail with:

If the operation fails for any reason, any further operations on the handle will still fail as for a closed handle.

Determining the Size of a File

> hFileSize     :: Handle -> IO Integer

For a handle hdl which attached to a physical file, hFileSize hdl returns the size of that file in 8-bit bytes (>= 0).

The computation may fail with:

Detecting the End of Input

> hIsEOF        :: Handle -> IO Bool
> isEOF         ::           IO Bool
> isEOF         =  hIsEOF stdin

For a readable handle hdl, computation hIsEOF hdl 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.

The computation may fail with:

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 from the internal buffer according to the buffer mode:
line-buffering
the entire buffer is written out whenever a newline is output, the buffer overflows, a flush is issued, or the handle is closed.
block-buffering
the entire buffer is written out whenever it overflows, a flush is issued, or the handle is closed.
no-buffering
output is written immediately, and never stored in the buffer.
The buffer is emptied as soon as it has been written out. (Rationale)

Similarly, input occurs according to the buffer mode for handle hdl.

line-buffering
when the buffer for hdl 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.
block-buffering
when the buffer for hdl becomes empty, the next block of data is read into the buffer.
no-buffering
the next input item is read and returned.

For most implementations, physical files will normally be block-buffered and terminals will normally be line-buffered.

> data BufferMode = NoBuffering | LineBuffering | BlockBuffering (Maybe Int)

> hSetBuffering :: Handle -> BufferMode     -> IO ()

Computation hSetBuffering hdl mode sets the mode of buffering for handle hdl on subsequent reads and writes.

If the buffer mode is changed from BlockBuffering or LineBuffering to NoBuffering, then

The default buffering mode when a handle is opened is implementation-dependent and may depend on the object which is attached to that handle.

The computation may fail with:

Flushing Buffers

> hFlush        :: Handle -> IO () 

Computation hFlush hdl causes any items buffered for output in handle hdl to be sent immediately to the operating system.

The computation may fail with:

Repositioning Handles

(Rationale)

Revisiting an I/O Position

> data HandlePosn

> hGetPosn      :: Handle                 -> IO HandlePosn
> hSetPosn      :: HandlePosn             -> IO () 

Computation hGetPosn hdl returns the current I/O position of hdl as an abstract value. Computation hSetPosn p sets the position of hdl to a previously obtained position p.

The computation hSetPosn may fail with:

Seeking to a new Position

> data SeekMode =  AbsoluteSeek | RelativeSeek | SeekFromEnd
> hSeek         :: Handle -> SeekMode -> Integer      -> IO () 

Computation hSeek hdl mode i sets the position of handle hdl depending on mode. If mode is

AbsoluteSeek
the position of hdl is set to i.
RelativeSeek
The position of hdl is set to offset i from the current position.
SeekFromEnd
The position of hdl is set to offset i from the end of the file.
The offset 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 (e.g. 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.

The computation may fail with:

Handle Properties

> hIsOpen          :: Handle -> IO Bool
> hIsClosed        :: Handle -> IO Bool
> hIsReadable      :: Handle -> IO Bool
> hIsWritable      :: Handle -> IO Bool
> hIsBlockBuffered :: Handle -> IO (Bool,Maybe Int)
> hIsLineBuffered  :: Handle -> IO Bool
> hIsNotBuffered   :: Handle -> IO Bool
> hIsSeekable      :: Handle -> IO Bool

A number of operations return information about the properties of a handle. Each of these operations except hIsBlockBuffered returns True if the handle has the specified property, and False otherwise. (Rationale)

Computation hIsBlockBuffered hdl returns ( False, Nothing ) if hdl is not block-buffered. Otherwise it returns ( True, size ), where size is Nothing for default buffering, and ( Just n ) for block-buffering of n 8-bit bytes.

Any of the latter six computations may fail with


[Prev] [Up] [Next]


The Definition of Monadic I/O in Haskell 1.3
Haskell 1.3 Committee
haskell1.3@comp.vuw.ac.nz