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 ()
39 hTell, -- :: Handle -> IO Integer
41 hWaitForInput, -- :: Handle -> Int -> IO Bool
42 hReady, -- :: Handle -> IO Bool
43 hGetChar, -- :: Handle -> IO Char
44 hGetLine, -- :: Handle -> IO [Char]
45 hLookAhead, -- :: Handle -> IO Char
46 hGetContents, -- :: Handle -> IO [Char]
47 hPutChar, -- :: Handle -> Char -> IO ()
48 hPutStr, -- :: Handle -> [Char] -> IO ()
49 hPutStrLn, -- :: Handle -> [Char] -> IO ()
50 hPrint, -- :: Show a => Handle -> a -> IO ()
51 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
52 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
53 hIsSeekable, -- :: Handle -> IO Bool
55 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
56 isAlreadyInUseError, isFullError,
57 isEOFError, isIllegalOperation,
58 isPermissionError, isUserError,
60 ioeGetErrorString, -- :: IOError -> String
61 ioeGetHandle, -- :: IOError -> Maybe Handle
62 ioeGetFileName, -- :: IOError -> Maybe FilePath
64 try, -- :: IO a -> IO (Either IOError a)
66 -- re-exports of Prelude names
67 IO, -- instance MonadFix
68 FilePath, -- :: String
70 ioError, -- :: IOError -> IO a
71 userError, -- :: String -> IOError
72 catch, -- :: IO a -> (IOError -> IO a) -> IO a
73 interact, -- :: (String -> String) -> IO ()
75 putChar, -- :: Char -> IO ()
76 putStr, -- :: String -> IO ()
77 putStrLn, -- :: String -> IO ()
78 print, -- :: Show a => a -> IO ()
79 getChar, -- :: IO Char
80 getLine, -- :: IO String
81 getContents, -- :: IO String
82 readFile, -- :: FilePath -> IO String
83 writeFile, -- :: FilePath -> String -> IO ()
84 appendFile, -- :: FilePath -> String -> IO ()
85 readIO, -- :: Read a => String -> IO a
86 readLn, -- :: Read a => IO a
88 #if !defined(__HUGS__) && !defined(__NHC__)
89 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
90 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
93 fixIO, -- :: (a -> IO a) -> IO a
95 #if !defined(__HUGS__) && !defined(__NHC__)
96 hSetEcho, -- :: Handle -> Bool -> IO ()
97 hGetEcho, -- :: Handle -> IO Bool
99 hIsTerminalDevice, -- :: Handle -> IO Bool
103 #ifdef __GLASGOW_HASKELL__
105 import GHC.IOBase -- Together these four Prelude modules define
106 import GHC.Handle -- all the stuff exported by IO for the GHC version
108 import GHC.ST ( fixST )
124 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
125 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
126 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
127 , stdin, stdout, stderr
128 , openFile -- :: FilePath -> IOMode -> IO Handle
129 , hClose -- :: Handle -> IO ()
130 , hFileSize -- :: Handle -> IO Integer
131 , hIsEOF -- :: Handle -> IO Bool
132 , isEOF -- :: IO Bool
133 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
134 , hGetBuffering -- :: Handle -> IO BufferMode
135 , hFlush -- :: Handle -> IO ()
136 , hGetPosn -- :: Handle -> IO HandlePosn
137 , hSetPosn -- :: HandlePosn -> IO ()
138 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
139 , hWaitForInput -- :: Handle -> Int -> IO Bool
140 , hGetChar -- :: Handle -> IO Char
141 , hGetLine -- :: Handle -> IO [Char]
142 , hLookAhead -- :: Handle -> IO Char
143 , hGetContents -- :: Handle -> IO [Char]
144 , hPutChar -- :: Handle -> Char -> IO ()
145 , hPutStr -- :: Handle -> [Char] -> IO ()
146 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
147 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
148 , hIsSeekable -- :: Handle -> IO Bool
149 , isAlreadyExistsError, isDoesNotExistError -- :: IOError -> Bool
150 , isAlreadyInUseError, isFullError
151 , isEOFError, isIllegalOperation
152 , isPermissionError, isUserError
153 , ioeGetErrorString -- :: IOError -> String
154 , ioeGetHandle -- :: IOError -> Maybe Handle
155 , ioeGetFileName -- :: IOError -> Maybe FilePath
158 , FilePath -- :: String
160 , ioError -- :: IOError -> IO a
161 , userError -- :: String -> IOError
162 , catch -- :: IO a -> (IOError -> IO a) -> IO a
164 import NHC.Internal (unsafePerformIO)
167 import System.IO.Error
169 -- -----------------------------------------------------------------------------
173 putChar :: Char -> IO ()
174 putChar c = hPutChar stdout c
176 putStr :: String -> IO ()
177 putStr s = hPutStr stdout s
179 putStrLn :: String -> IO ()
180 putStrLn s = do putStr s
183 print :: Show a => a -> IO ()
184 print x = putStrLn (show x)
187 getChar = hGetChar stdin
190 getLine = hGetLine stdin
192 getContents :: IO String
193 getContents = hGetContents stdin
195 interact :: (String -> String) -> IO ()
196 interact f = do s <- getContents
199 readFile :: FilePath -> IO String
200 readFile name = openFile name ReadMode >>= hGetContents
202 writeFile :: FilePath -> String -> IO ()
203 writeFile name str = do
204 hdl <- openFile name WriteMode
208 appendFile :: FilePath -> String -> IO ()
209 appendFile name str = do
210 hdl <- openFile name AppendMode
214 readLn :: Read a => IO a
215 readLn = do l <- getLine
219 -- raises an exception instead of an error
220 readIO :: Read a => String -> IO a
221 readIO s = case (do { (x,t) <- reads s ;
225 [] -> ioError (userError "Prelude.readIO: no parse")
226 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
227 #endif /* __HUGS__ */
229 hReady :: Handle -> IO Bool
230 hReady h = hWaitForInput h 0
232 hPutStrLn :: Handle -> String -> IO ()
233 hPutStrLn hndl str = do
237 hPrint :: Show a => Handle -> a -> IO ()
238 hPrint hdl = hPutStrLn hdl . show
240 -- ---------------------------------------------------------------------------
243 #ifdef __GLASGOW_HASKELL__
244 fixIO :: (a -> IO a) -> IO a
245 fixIO m = stToIO (fixST (ioToST . m))
248 fixIO :: (a -> IO a) -> IO a
249 fixIO f = let x = unsafePerformIO (f x) in return x