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
28 openBinaryFile, -- :: FilePath -> IOMode -> IO Handle
30 hClose, -- :: Handle -> IO ()
31 hFileSize, -- :: Handle -> IO Integer
32 hIsEOF, -- :: Handle -> IO Bool
35 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
36 hGetBuffering, -- :: Handle -> IO BufferMode
37 #if !defined(__HUGS__) && !defined(__NHC__)
38 hSetBinaryMode, -- :: Handle -> Bool -> IO ()
40 hFlush, -- :: Handle -> IO ()
41 hGetPosn, -- :: Handle -> IO HandlePosn
42 hSetPosn, -- :: HandlePosn -> IO ()
43 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
45 hTell, -- :: Handle -> IO Integer
47 hWaitForInput, -- :: Handle -> Int -> IO Bool
48 hReady, -- :: Handle -> IO Bool
49 hGetChar, -- :: Handle -> IO Char
50 hGetLine, -- :: Handle -> IO [Char]
51 hLookAhead, -- :: Handle -> IO Char
52 hGetContents, -- :: Handle -> IO [Char]
53 hPutChar, -- :: Handle -> Char -> IO ()
54 hPutStr, -- :: Handle -> [Char] -> IO ()
55 hPutStrLn, -- :: Handle -> [Char] -> IO ()
56 hPrint, -- :: Show a => Handle -> a -> IO ()
57 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
58 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
59 hIsSeekable, -- :: Handle -> IO Bool
61 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
62 isAlreadyInUseError, isFullError,
63 isEOFError, isIllegalOperation,
64 isPermissionError, isUserError,
66 ioeGetErrorString, -- :: IOError -> String
67 ioeGetHandle, -- :: IOError -> Maybe Handle
68 ioeGetFileName, -- :: IOError -> Maybe FilePath
70 try, -- :: IO a -> IO (Either IOError a)
72 -- re-exports of Prelude names
73 IO, -- instance MonadFix
74 FilePath, -- :: String
76 ioError, -- :: IOError -> IO a
77 userError, -- :: String -> IOError
78 catch, -- :: IO a -> (IOError -> IO a) -> IO a
79 interact, -- :: (String -> String) -> IO ()
81 putChar, -- :: Char -> IO ()
82 putStr, -- :: String -> IO ()
83 putStrLn, -- :: String -> IO ()
84 print, -- :: Show a => a -> IO ()
85 getChar, -- :: IO Char
86 getLine, -- :: IO String
87 getContents, -- :: IO String
88 readFile, -- :: FilePath -> IO String
89 writeFile, -- :: FilePath -> String -> IO ()
90 appendFile, -- :: FilePath -> String -> IO ()
91 readIO, -- :: Read a => String -> IO a
92 readLn, -- :: Read a => IO a
94 #if !defined(__HUGS__) && !defined(__NHC__)
95 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
96 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
99 fixIO, -- :: (a -> IO a) -> IO a
101 #if !defined(__HUGS__) && !defined(__NHC__)
102 hSetEcho, -- :: Handle -> Bool -> IO ()
103 hGetEcho, -- :: Handle -> IO Bool
105 hIsTerminalDevice, -- :: Handle -> IO Bool
109 #ifdef __GLASGOW_HASKELL__
111 import GHC.IOBase -- Together these four Prelude modules define
112 import GHC.Handle -- all the stuff exported by IO for the GHC version
114 import GHC.ST ( fixST )
130 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
131 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
132 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
133 , stdin, stdout, stderr
134 , openFile -- :: FilePath -> IOMode -> IO Handle
135 , hClose -- :: Handle -> IO ()
136 , hFileSize -- :: Handle -> IO Integer
137 , hIsEOF -- :: Handle -> IO Bool
138 , isEOF -- :: IO Bool
139 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
140 , hGetBuffering -- :: Handle -> IO BufferMode
141 , hFlush -- :: Handle -> IO ()
142 , hGetPosn -- :: Handle -> IO HandlePosn
143 , hSetPosn -- :: HandlePosn -> IO ()
144 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
145 , hWaitForInput -- :: Handle -> Int -> IO Bool
146 , hGetChar -- :: Handle -> IO Char
147 , hGetLine -- :: Handle -> IO [Char]
148 , hLookAhead -- :: Handle -> IO Char
149 , hGetContents -- :: Handle -> IO [Char]
150 , hPutChar -- :: Handle -> Char -> IO ()
151 , hPutStr -- :: Handle -> [Char] -> IO ()
152 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
153 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
154 , hIsSeekable -- :: Handle -> IO Bool
155 , isAlreadyExistsError, isDoesNotExistError -- :: IOError -> Bool
156 , isAlreadyInUseError, isFullError
157 , isEOFError, isIllegalOperation
158 , isPermissionError, isUserError
159 , ioeGetErrorString -- :: IOError -> String
160 , ioeGetHandle -- :: IOError -> Maybe Handle
161 , ioeGetFileName -- :: IOError -> Maybe FilePath
164 , FilePath -- :: String
166 , ioError -- :: IOError -> IO a
167 , userError -- :: String -> IOError
168 , catch -- :: IO a -> (IOError -> IO a) -> IO a
170 import NHC.Internal (unsafePerformIO)
173 import System.IO.Error
175 -- -----------------------------------------------------------------------------
179 putChar :: Char -> IO ()
180 putChar c = hPutChar stdout c
182 putStr :: String -> IO ()
183 putStr s = hPutStr stdout s
185 putStrLn :: String -> IO ()
186 putStrLn s = do putStr s
189 print :: Show a => a -> IO ()
190 print x = putStrLn (show x)
193 getChar = hGetChar stdin
196 getLine = hGetLine stdin
198 getContents :: IO String
199 getContents = hGetContents stdin
201 interact :: (String -> String) -> IO ()
202 interact f = do s <- getContents
205 readFile :: FilePath -> IO String
206 readFile name = openFile name ReadMode >>= hGetContents
208 writeFile :: FilePath -> String -> IO ()
209 writeFile name str = do
210 hdl <- openFile name WriteMode
214 appendFile :: FilePath -> String -> IO ()
215 appendFile name str = do
216 hdl <- openFile name AppendMode
220 readLn :: Read a => IO a
221 readLn = do l <- getLine
225 -- raises an exception instead of an error
226 readIO :: Read a => String -> IO a
227 readIO s = case (do { (x,t) <- reads s ;
231 [] -> ioError (userError "Prelude.readIO: no parse")
232 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
233 #endif /* __HUGS__ */
235 hReady :: Handle -> IO Bool
236 hReady h = hWaitForInput h 0
238 hPutStrLn :: Handle -> String -> IO ()
239 hPutStrLn hndl str = do
243 hPrint :: Show a => Handle -> a -> IO ()
244 hPrint hdl = hPutStrLn hdl . show
246 -- ---------------------------------------------------------------------------
249 #ifdef __GLASGOW_HASKELL__
250 fixIO :: (a -> IO a) -> IO a
251 fixIO m = stToIO (fixST (ioToST . m))
254 fixIO :: (a -> IO a) -> IO a
255 fixIO f = let x = unsafePerformIO (f x) in return x