1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- The standard IO library.
14 -----------------------------------------------------------------------------
17 Handle, -- abstract, instance of: Eq, Show.
18 HandlePosn(..), -- abstract, instance of: Eq, Show.
20 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
21 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
22 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
24 stdin, stdout, stderr, -- :: Handle
26 openFile, -- :: FilePath -> IOMode -> IO Handle
27 hClose, -- :: Handle -> IO ()
28 hFileSize, -- :: Handle -> IO Integer
29 hIsEOF, -- :: Handle -> IO Bool
32 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
33 hGetBuffering, -- :: Handle -> IO BufferMode
34 hFlush, -- :: Handle -> IO ()
35 hGetPosn, -- :: Handle -> IO HandlePosn
36 hSetPosn, -- :: HandlePosn -> IO ()
37 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
38 hTell, -- :: Handle -> IO Integer
39 hWaitForInput, -- :: Handle -> Int -> IO Bool
40 hReady, -- :: Handle -> IO Bool
41 hGetChar, -- :: Handle -> IO Char
42 hGetLine, -- :: Handle -> IO [Char]
43 hLookAhead, -- :: Handle -> IO Char
44 hGetContents, -- :: Handle -> IO [Char]
45 hPutChar, -- :: Handle -> Char -> IO ()
46 hPutStr, -- :: Handle -> [Char] -> IO ()
47 hPutStrLn, -- :: Handle -> [Char] -> IO ()
48 hPrint, -- :: Show a => Handle -> a -> IO ()
49 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
50 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
51 hIsSeekable, -- :: Handle -> IO Bool
53 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
54 isAlreadyInUseError, isFullError,
55 isEOFError, isIllegalOperation,
56 isPermissionError, isUserError,
58 ioeGetErrorString, -- :: IOError -> String
59 ioeGetHandle, -- :: IOError -> Maybe Handle
60 ioeGetFileName, -- :: IOError -> Maybe FilePath
62 try, -- :: IO a -> IO (Either IOError a)
63 bracket, -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
64 bracket_, -- :: IO a -> (a -> IO b) -> IO c -> IO c
66 -- Non-standard extension (but will hopefully become standard with 1.5) is
67 -- to export the Prelude io functions via IO (in addition to exporting them
68 -- from the prelude...for now.)
69 IO, -- instance MonadFix
70 FilePath, -- :: String
72 ioError, -- :: IOError -> IO a
73 userError, -- :: String -> IOError
74 catch, -- :: IO a -> (IOError -> IO a) -> IO a
75 interact, -- :: (String -> String) -> IO ()
77 putChar, -- :: Char -> IO ()
78 putStr, -- :: String -> IO ()
79 putStrLn, -- :: String -> IO ()
80 print, -- :: Show a => a -> IO ()
81 getChar, -- :: IO Char
82 getLine, -- :: IO String
83 getContents, -- :: IO String
84 readFile, -- :: FilePath -> IO String
85 writeFile, -- :: FilePath -> String -> IO ()
86 appendFile, -- :: FilePath -> String -> IO ()
87 readIO, -- :: Read a => String -> IO a
88 readLn, -- :: Read a => IO a
90 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
91 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
93 fixIO, -- :: (a -> IO a) -> IO a
95 hSetEcho, -- :: Handle -> Bool -> IO ()
96 hGetEcho, -- :: Handle -> IO Bool
98 hIsTerminalDevice, -- :: Handle -> IO Bool
101 #ifdef __GLASGOW_HASKELL__
103 import GHC.IOBase -- Together these four Prelude modules define
104 import GHC.Handle -- all the stuff exported by IO for the GHC version
106 import GHC.ST ( fixST )
113 import System.IO.Error
115 -- -----------------------------------------------------------------------------
118 putChar :: Char -> IO ()
119 putChar c = hPutChar stdout c
121 putStr :: String -> IO ()
122 putStr s = hPutStr stdout s
124 putStrLn :: String -> IO ()
125 putStrLn s = do putStr s
128 print :: Show a => a -> IO ()
129 print x = putStrLn (show x)
132 getChar = hGetChar stdin
135 getLine = hGetLine stdin
137 getContents :: IO String
138 getContents = hGetContents stdin
140 interact :: (String -> String) -> IO ()
141 interact f = do s <- getContents
144 readFile :: FilePath -> IO String
145 readFile name = openFile name ReadMode >>= hGetContents
147 writeFile :: FilePath -> String -> IO ()
148 writeFile name str = do
149 hdl <- openFile name WriteMode
153 appendFile :: FilePath -> String -> IO ()
154 appendFile name str = do
155 hdl <- openFile name AppendMode
159 readLn :: Read a => IO a
160 readLn = do l <- getLine
164 -- raises an exception instead of an error
165 readIO :: Read a => String -> IO a
166 readIO s = case (do { (x,t) <- reads s ;
170 [] -> ioError (userError "Prelude.readIO: no parse")
171 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
173 hReady :: Handle -> IO Bool
174 hReady h = hWaitForInput h 0
176 hPutStrLn :: Handle -> String -> IO ()
177 hPutStrLn hndl str = do
181 hPrint :: Show a => Handle -> a -> IO ()
182 hPrint hdl = hPutStrLn hdl . show
184 -- ---------------------------------------------------------------------------
187 #ifdef __GLASGOW_HASKELL__
188 fixIO :: (a -> IO a) -> IO a
189 fixIO m = stToIO (fixST (ioToST . m))