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 openBinaryFile, -- :: FilePath -> IOMode -> IO Handle
28 hClose, -- :: Handle -> IO ()
29 hFileSize, -- :: Handle -> IO Integer
30 hIsEOF, -- :: Handle -> IO Bool
33 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
34 hGetBuffering, -- :: Handle -> IO BufferMode
36 hSetBinaryMode, -- :: Handle -> Bool -> IO ()
38 hFlush, -- :: Handle -> IO ()
39 hGetPosn, -- :: Handle -> IO HandlePosn
40 hSetPosn, -- :: HandlePosn -> IO ()
41 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
43 hTell, -- :: Handle -> IO Integer
45 hWaitForInput, -- :: Handle -> Int -> IO Bool
46 hReady, -- :: Handle -> IO Bool
47 hGetChar, -- :: Handle -> IO Char
48 hGetLine, -- :: Handle -> IO [Char]
49 hLookAhead, -- :: Handle -> IO Char
50 hGetContents, -- :: Handle -> IO [Char]
51 hPutChar, -- :: Handle -> Char -> IO ()
52 hPutStr, -- :: Handle -> [Char] -> IO ()
53 hPutStrLn, -- :: Handle -> [Char] -> IO ()
54 hPrint, -- :: Show a => Handle -> a -> IO ()
55 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
56 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
57 hIsSeekable, -- :: Handle -> IO Bool
59 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
60 isAlreadyInUseError, isFullError,
61 isEOFError, isIllegalOperation,
62 isPermissionError, isUserError,
64 ioeGetErrorString, -- :: IOError -> String
65 ioeGetHandle, -- :: IOError -> Maybe Handle
66 ioeGetFileName, -- :: IOError -> Maybe FilePath
68 try, -- :: IO a -> IO (Either IOError a)
70 -- re-exports of Prelude names
71 IO, -- instance MonadFix
72 FilePath, -- :: String
74 ioError, -- :: IOError -> IO a
75 userError, -- :: String -> IOError
76 catch, -- :: IO a -> (IOError -> IO a) -> IO a
77 interact, -- :: (String -> String) -> IO ()
79 putChar, -- :: Char -> IO ()
80 putStr, -- :: String -> IO ()
81 putStrLn, -- :: String -> IO ()
82 print, -- :: Show a => a -> IO ()
83 getChar, -- :: IO Char
84 getLine, -- :: IO String
85 getContents, -- :: IO String
86 readFile, -- :: FilePath -> IO String
87 writeFile, -- :: FilePath -> String -> IO ()
88 appendFile, -- :: FilePath -> String -> IO ()
89 readIO, -- :: Read a => String -> IO a
90 readLn, -- :: Read a => IO a
92 #if !defined(__HUGS__) && !defined(__NHC__)
93 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
94 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
97 fixIO, -- :: (a -> IO a) -> IO a
99 #if !defined(__HUGS__) && !defined(__NHC__)
100 hSetEcho, -- :: Handle -> Bool -> IO ()
101 hGetEcho, -- :: Handle -> IO Bool
103 hIsTerminalDevice, -- :: Handle -> IO Bool
107 #ifdef __GLASGOW_HASKELL__
109 import GHC.IOBase -- Together these four Prelude modules define
110 import GHC.Handle -- all the stuff exported by IO for the GHC version
112 import GHC.ST ( fixST )
128 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
129 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
130 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
131 , stdin, stdout, stderr
132 , openFile -- :: FilePath -> IOMode -> IO Handle
133 , hClose -- :: Handle -> IO ()
134 , hFileSize -- :: Handle -> IO Integer
135 , hIsEOF -- :: Handle -> IO Bool
136 , isEOF -- :: IO Bool
137 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
138 , hGetBuffering -- :: Handle -> IO BufferMode
139 , hFlush -- :: Handle -> IO ()
140 , hGetPosn -- :: Handle -> IO HandlePosn
141 , hSetPosn -- :: HandlePosn -> IO ()
142 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
143 , hWaitForInput -- :: Handle -> Int -> IO Bool
144 , hGetChar -- :: Handle -> IO Char
145 , hGetLine -- :: Handle -> IO [Char]
146 , hLookAhead -- :: Handle -> IO Char
147 , hGetContents -- :: Handle -> IO [Char]
148 , hPutChar -- :: Handle -> Char -> IO ()
149 , hPutStr -- :: Handle -> [Char] -> IO ()
150 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
151 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
152 , hIsSeekable -- :: Handle -> IO Bool
153 , isAlreadyExistsError, isDoesNotExistError -- :: IOError -> Bool
154 , isAlreadyInUseError, isFullError
155 , isEOFError, isIllegalOperation
156 , isPermissionError, isUserError
157 , ioeGetErrorString -- :: IOError -> String
158 , ioeGetHandle -- :: IOError -> Maybe Handle
159 , ioeGetFileName -- :: IOError -> Maybe FilePath
162 , FilePath -- :: String
164 , ioError -- :: IOError -> IO a
165 , userError -- :: String -> IOError
166 , catch -- :: IO a -> (IOError -> IO a) -> IO a
168 import NHC.Internal (unsafePerformIO)
171 import System.IO.Error
173 -- -----------------------------------------------------------------------------
177 putChar :: Char -> IO ()
178 putChar c = hPutChar stdout c
180 putStr :: String -> IO ()
181 putStr s = hPutStr stdout s
183 putStrLn :: String -> IO ()
184 putStrLn s = do putStr s
187 print :: Show a => a -> IO ()
188 print x = putStrLn (show x)
191 getChar = hGetChar stdin
194 getLine = hGetLine stdin
196 getContents :: IO String
197 getContents = hGetContents stdin
199 interact :: (String -> String) -> IO ()
200 interact f = do s <- getContents
203 readFile :: FilePath -> IO String
204 readFile name = openFile name ReadMode >>= hGetContents
206 writeFile :: FilePath -> String -> IO ()
207 writeFile name str = do
208 hdl <- openFile name WriteMode
212 appendFile :: FilePath -> String -> IO ()
213 appendFile name str = do
214 hdl <- openFile name AppendMode
218 readLn :: Read a => IO a
219 readLn = do l <- getLine
223 -- raises an exception instead of an error
224 readIO :: Read a => String -> IO a
225 readIO s = case (do { (x,t) <- reads s ;
229 [] -> ioError (userError "Prelude.readIO: no parse")
230 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
231 #endif /* __HUGS__ */
233 hReady :: Handle -> IO Bool
234 hReady h = hWaitForInput h 0
236 hPutStrLn :: Handle -> String -> IO ()
237 hPutStrLn hndl str = do
241 hPrint :: Show a => Handle -> a -> IO ()
242 hPrint hdl = hPutStrLn hdl . show
244 -- ---------------------------------------------------------------------------
247 #ifdef __GLASGOW_HASKELL__
248 fixIO :: (a -> IO a) -> IO a
249 fixIO m = stToIO (fixST (ioToST . m))
252 fixIO :: (a -> IO a) -> IO a
253 fixIO f = let x = unsafePerformIO (f x) in return x