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
35 hSetBinaryMode, -- :: Handle -> Bool -> IO ()
36 hFlush, -- :: Handle -> IO ()
37 hGetPosn, -- :: Handle -> IO HandlePosn
38 hSetPosn, -- :: HandlePosn -> IO ()
39 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
41 hTell, -- :: Handle -> IO Integer
43 hWaitForInput, -- :: Handle -> Int -> IO Bool
44 hReady, -- :: Handle -> IO Bool
45 hGetChar, -- :: Handle -> IO Char
46 hGetLine, -- :: Handle -> IO [Char]
47 hLookAhead, -- :: Handle -> IO Char
48 hGetContents, -- :: Handle -> IO [Char]
49 hPutChar, -- :: Handle -> Char -> IO ()
50 hPutStr, -- :: Handle -> [Char] -> IO ()
51 hPutStrLn, -- :: Handle -> [Char] -> IO ()
52 hPrint, -- :: Show a => Handle -> a -> IO ()
53 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
54 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
55 hIsSeekable, -- :: Handle -> IO Bool
57 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
58 isAlreadyInUseError, isFullError,
59 isEOFError, isIllegalOperation,
60 isPermissionError, isUserError,
62 ioeGetErrorString, -- :: IOError -> String
63 ioeGetHandle, -- :: IOError -> Maybe Handle
64 ioeGetFileName, -- :: IOError -> Maybe FilePath
66 try, -- :: IO a -> IO (Either IOError a)
68 -- re-exports of Prelude names
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 #if !defined(__HUGS__) && !defined(__NHC__)
91 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
92 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
95 fixIO, -- :: (a -> IO a) -> IO a
97 #if !defined(__HUGS__) && !defined(__NHC__)
98 hSetEcho, -- :: Handle -> Bool -> IO ()
99 hGetEcho, -- :: Handle -> IO Bool
101 hIsTerminalDevice, -- :: Handle -> IO Bool
105 #ifdef __GLASGOW_HASKELL__
107 import GHC.IOBase -- Together these four Prelude modules define
108 import GHC.Handle -- all the stuff exported by IO for the GHC version
110 import GHC.ST ( fixST )
126 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
127 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
128 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
129 , stdin, stdout, stderr
130 , openFile -- :: FilePath -> IOMode -> IO Handle
131 , hClose -- :: Handle -> IO ()
132 , hFileSize -- :: Handle -> IO Integer
133 , hIsEOF -- :: Handle -> IO Bool
134 , isEOF -- :: IO Bool
135 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
136 , hGetBuffering -- :: Handle -> IO BufferMode
137 , hFlush -- :: Handle -> IO ()
138 , hGetPosn -- :: Handle -> IO HandlePosn
139 , hSetPosn -- :: HandlePosn -> IO ()
140 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
141 , hWaitForInput -- :: Handle -> Int -> IO Bool
142 , hGetChar -- :: Handle -> IO Char
143 , hGetLine -- :: Handle -> IO [Char]
144 , hLookAhead -- :: Handle -> IO Char
145 , hGetContents -- :: Handle -> IO [Char]
146 , hPutChar -- :: Handle -> Char -> IO ()
147 , hPutStr -- :: Handle -> [Char] -> IO ()
148 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
149 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
150 , hIsSeekable -- :: Handle -> IO Bool
151 , isAlreadyExistsError, isDoesNotExistError -- :: IOError -> Bool
152 , isAlreadyInUseError, isFullError
153 , isEOFError, isIllegalOperation
154 , isPermissionError, isUserError
155 , ioeGetErrorString -- :: IOError -> String
156 , ioeGetHandle -- :: IOError -> Maybe Handle
157 , ioeGetFileName -- :: IOError -> Maybe FilePath
160 , FilePath -- :: String
162 , ioError -- :: IOError -> IO a
163 , userError -- :: String -> IOError
164 , catch -- :: IO a -> (IOError -> IO a) -> IO a
166 import NHC.Internal (unsafePerformIO)
169 import System.IO.Error
171 -- -----------------------------------------------------------------------------
175 putChar :: Char -> IO ()
176 putChar c = hPutChar stdout c
178 putStr :: String -> IO ()
179 putStr s = hPutStr stdout s
181 putStrLn :: String -> IO ()
182 putStrLn s = do putStr s
185 print :: Show a => a -> IO ()
186 print x = putStrLn (show x)
189 getChar = hGetChar stdin
192 getLine = hGetLine stdin
194 getContents :: IO String
195 getContents = hGetContents stdin
197 interact :: (String -> String) -> IO ()
198 interact f = do s <- getContents
201 readFile :: FilePath -> IO String
202 readFile name = openFile name ReadMode >>= hGetContents
204 writeFile :: FilePath -> String -> IO ()
205 writeFile name str = do
206 hdl <- openFile name WriteMode
210 appendFile :: FilePath -> String -> IO ()
211 appendFile name str = do
212 hdl <- openFile name AppendMode
216 readLn :: Read a => IO a
217 readLn = do l <- getLine
221 -- raises an exception instead of an error
222 readIO :: Read a => String -> IO a
223 readIO s = case (do { (x,t) <- reads s ;
227 [] -> ioError (userError "Prelude.readIO: no parse")
228 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
229 #endif /* __HUGS__ */
231 hReady :: Handle -> IO Bool
232 hReady h = hWaitForInput h 0
234 hPutStrLn :: Handle -> String -> IO ()
235 hPutStrLn hndl str = do
239 hPrint :: Show a => Handle -> a -> IO ()
240 hPrint hdl = hPutStrLn hdl . show
242 -- ---------------------------------------------------------------------------
245 #ifdef __GLASGOW_HASKELL__
246 fixIO :: (a -> IO a) -> IO a
247 fixIO m = stToIO (fixST (ioToST . m))
250 fixIO :: (a -> IO a) -> IO a
251 fixIO f = let x = unsafePerformIO (f x) in return x