9 Input/Output


module IO (
    Handle, HandlePosn,
    IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
    BufferMode(NoBuffering,LineBuffering,BlockBuffering),
    SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
    stdin, stdout, stderr, openFile, hClose, hFileSize, hIsEOF, isEOF,
    hSetBuffering, hGetBuffering, hFlush, hGetPosn, hSetPosn, hSeek, 
    hIsOpen, hIsClosed, hIsReadable, hIsWritable, hIsSeekable, hReady, 
    hGetChar, hLookAhead, hGetContents, hPutChar, hPutStr, hPrint,
    isAlreadyExistsError, isAlreadyInUseError, isFullError, isEOFError,
    isIllegalOperation, isPermissionError, isUserError, 
    ioeGetHandle, ioeGetFileName ) where
import Ix

data Handle = ...
instance Eq Handle where ...
data HandlePosn = ...
instance Eq HandlePosn where ...

data IOMode      =  ReadMode | WriteMode | AppendMode | ReadWriteMode
                    deriving (Eq, Ord, Ix, Enum, Read, Show)
data BufferMode  =  NoBuffering | LineBuffering | BlockBuffering (Maybe Int)
                    deriving (Eq, Ord, Read, Show)
data SeekMode    =  AbsoluteSeek | RelativeSeek | SeekFromEnd
                    deriving (Eq, Ord, Ix, Enum, Read, Show)

stdin, stdout, stderr :: Handle
openFile              :: FilePath -> IOMode -> IO Handle
hClose                :: Handle -> IO () 
hFileSize             :: Handle -> IO Integer
hIsEOF                :: Handle -> IO Bool
isEOF                 :: IO Bool
isEOF                 =  hIsEOF stdin
hSetBuffering         :: Handle  -> BufferMode -> IO ()
hGetBuffering         :: Handle  -> IO BufferMode
hFlush                :: Handle -> IO () 
hGetPosn              :: Handle -> IO HandlePosn
hSetPosn              :: HandlePosn -> IO () 
hSeek                 :: Handle -> SeekMode -> Integer -> IO () 
hIsOpen               :: Handle -> IO Bool
hIsClosed             :: Handle -> IO Bool
hIsReadable           :: Handle -> IO Bool
hIsWritable           :: Handle -> IO Bool
hIsSeekable           :: Handle -> IO Bool
hReady                :: Handle -> IO Bool 
hGetChar              :: Handle -> IO Char
hLookAhead            :: Handle -> IO Char
hGetContents          :: Handle -> IO String
hPutChar              :: Handle -> Char -> IO ()
hPutStr               :: Handle -> String -> IO ()
hPrint                :: Show a => Handle -> a -> IO ()
isAlreadyExistsError  :: IOError -> Bool
isAlreadyInUseError   :: IOError -> Bool
isFullError           :: IOError -> Bool
isEOFError            :: IOError -> Bool
isIllegalOperation    :: IOError -> Bool
isPermissionError     :: IOError -> Bool
isUserError           :: IOError -> Maybe String
ioeGetHandle          :: IOError -> Maybe Handle
ioeGetFileName        :: IOError -> Maybe FilePath

The monadic I/O system used in Haskell is described by the Haskell language report. Commonly used I/O functions such as print are part of the standard prelude and need not be explicitly imported. This library contain more advanced I/O features.

9.1 I/O Errors

Errors of type IOError are used by the I/O monad. This is an abstract type; the library provides functions to interrogate and construct values in IOError: All of these except isUserError return a Bool, which is True if its argument is the corresponding kind of error, and False otherwise. The isUserError function returns Just err if its argument is a programmer-defined error value created by userError err, and Nothing otherwise.

Two additional functions are provided to obtain information about an error value. These are ioeGetHandle which returns Just hdl if the error value refers to handle hdl and Nothing otherwise; and ioeGetFileName which returns Just name if the error value refers to file name, and Nothing otherwise.

9.2 Files and Handles

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 Directory). 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.

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:

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.

9.2.1 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. 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.

9.2.2 Standard Handles

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

9.3 Opening and Closing Files

9.3.1 Opening Files

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.

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 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.

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 as far as possible, locally to the Haskell process, multiple-reader single-writer locking on files. Thus 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.

9.3.2 Closing Files

Computation hClose hdl makes handle hdl closed. Before the computation finishes, if hdl is writable its buffer is flushed as for hFlush. If the operation fails for any reason, any further operations on the handle will still fail as if hdl had been successfully closed.

9.4 Determining the Size of a File

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

9.4.1 Detecting the End of Input

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 isEOF is identical, except that it works only on stdin.

9.4.2 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:

The buffer is emptied as soon as it has been written out.

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

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

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

Computation hGetBuffering hdl returns the current buffering mode for hdl.

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.

9.4.3 Flushing Buffers

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

9.5 Repositioning Handles

9.5.1 Revisiting an I/O Position

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

9.5.2 Seeking to a new Position

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

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

9.6 Handle Properties

The functions hIsOpen, hIsClosed, hIsReadable, hIsWritable and hIsSeekable return information about the properties of a handle. Each of these returns True if the handle has the specified property, and False otherwise.

9.7 Text Input and Output

This module defines the standard set of input operations for reading characters and strings from text files, using handles. Many of these functions are generalizations of Prelude functions. I/O in the Prelude generally uses stdin and stdout; here, handles are explicitly specified by the I/O operation.

9.7.1 Checking for Input

Computation hReady hdl indicates whether at least one item is available for input from handle hdl.

Computation hGetChar hdl reads a character from the file or channel managed by hdl.

9.7.2 Reading Ahead

Computation hLookAhead hdl returns the next character from handle hdl without removing it from the input buffer, blocking until a character is available.

Computation hGetContents hdl returns the list of characters corresponding to the unread portion of the channel or file managed by hdl, which is made semi-closed.

Computation hPutChar hdl c writes the character c to the file or channel managed by hdl. Characters may be buffered if buffering is enabled for hdl.

Computation hPutStr hdl s writes the string s to the file or channel managed by hdl.

Computation hPrint hdl t writes the string representation of t given by the shows function to the file or channel managed by hdl and appends a newline.

9.8 Examples

Here are some simple examples to illustrate Haskell I/O.

9.8.1 Summing Two Numbers

This program reads and sums two integers (default overloading resolution is used to resolve the types of x1 and x2 to be Int).


import IO

main = do
         hSetBuffering stdout NoBuffering            
         putStr   "Enter an integer: "        
         x1 <- readNum 
         putStr   "Enter another integer: "          
         x2 <- readNum                          
         putStr  ("Their sum is " ++ show (x1+x2) ++ "\n")
       where readNum :: IO Integer
         readNum =  do { line <- getLine; readIO line }

9.8.2 Copying Files

A simple program to create a copy of a file, with all lower-case characters translated to upper-case. This program will not allow a file to be copied to itself. This version uses character-level I/O.


import IO
import System

main = do 
         ~[f1,f2] <- getArgs   -- the ~ makes the pattern failure-free
         h1 <- openFile f1 ReadMode     
         h2 <- openFile f2 WriteMode 
         copyFile h1 h2            
         hClose h1                  
         hClose h2

copyFile h1 h2 = do
                   eof <- hIsEOF h1
                   if eof then return () else
                      do
                        c <- hGetChar h1
                        hPutChar h2 (toUpper c)   
                        copyFile h1 h2

An equivalent but much shorter version, using string I/O is:


import System

main = do
         ~[f1,f2] <- getArgs
         s <- readFile f1
         writeFile f2 (map toUpper s)

Next section: System
The Haskell 1.3 Library Report