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
108 #ifdef __GLASGOW_HASKELL__
109 hShow, -- :: Handle -> IO String
113 #ifdef __GLASGOW_HASKELL__
115 import GHC.IOBase -- Together these four Prelude modules define
116 import GHC.Handle -- all the stuff exported by IO for the GHC version
118 import GHC.ST ( fixST )
134 , IOMode (ReadMode,WriteMode,AppendMode,ReadWriteMode)
135 , BufferMode (NoBuffering,LineBuffering,BlockBuffering)
136 , SeekMode (AbsoluteSeek,RelativeSeek,SeekFromEnd)
137 , stdin, stdout, stderr
138 , openFile -- :: FilePath -> IOMode -> IO Handle
139 , hClose -- :: Handle -> IO ()
140 , hFileSize -- :: Handle -> IO Integer
141 , hIsEOF -- :: Handle -> IO Bool
142 , isEOF -- :: IO Bool
143 , hSetBuffering -- :: Handle -> BufferMode -> IO ()
144 , hGetBuffering -- :: Handle -> IO BufferMode
145 , hFlush -- :: Handle -> IO ()
146 , hGetPosn -- :: Handle -> IO HandlePosn
147 , hSetPosn -- :: HandlePosn -> IO ()
148 , hSeek -- :: Handle -> SeekMode -> Integer -> IO ()
149 , hWaitForInput -- :: Handle -> Int -> IO Bool
150 , hGetChar -- :: Handle -> IO Char
151 , hGetLine -- :: Handle -> IO [Char]
152 , hLookAhead -- :: Handle -> IO Char
153 , hGetContents -- :: Handle -> IO [Char]
154 , hPutChar -- :: Handle -> Char -> IO ()
155 , hPutStr -- :: Handle -> [Char] -> IO ()
156 , hIsOpen, hIsClosed -- :: Handle -> IO Bool
157 , hIsReadable, hIsWritable -- :: Handle -> IO Bool
158 , hIsSeekable -- :: Handle -> IO Bool
159 , isAlreadyExistsError, isDoesNotExistError -- :: IOError -> Bool
160 , isAlreadyInUseError, isFullError
161 , isEOFError, isIllegalOperation
162 , isPermissionError, isUserError
163 , ioeGetErrorString -- :: IOError -> String
164 , ioeGetHandle -- :: IOError -> Maybe Handle
165 , ioeGetFileName -- :: IOError -> Maybe FilePath
168 , FilePath -- :: String
170 , ioError -- :: IOError -> IO a
171 , userError -- :: String -> IOError
172 , catch -- :: IO a -> (IOError -> IO a) -> IO a
174 import NHC.Internal (unsafePerformIO)
177 import System.IO.Error
179 -- -----------------------------------------------------------------------------
183 putChar :: Char -> IO ()
184 putChar c = hPutChar stdout c
186 putStr :: String -> IO ()
187 putStr s = hPutStr stdout s
189 putStrLn :: String -> IO ()
190 putStrLn s = do putStr s
193 print :: Show a => a -> IO ()
194 print x = putStrLn (show x)
197 getChar = hGetChar stdin
200 getLine = hGetLine stdin
202 getContents :: IO String
203 getContents = hGetContents stdin
205 interact :: (String -> String) -> IO ()
206 interact f = do s <- getContents
209 readFile :: FilePath -> IO String
210 readFile name = openFile name ReadMode >>= hGetContents
212 writeFile :: FilePath -> String -> IO ()
213 writeFile name str = do
214 hdl <- openFile name WriteMode
218 appendFile :: FilePath -> String -> IO ()
219 appendFile name str = do
220 hdl <- openFile name AppendMode
224 readLn :: Read a => IO a
225 readLn = do l <- getLine
229 -- raises an exception instead of an error
230 readIO :: Read a => String -> IO a
231 readIO s = case (do { (x,t) <- reads s ;
235 [] -> ioError (userError "Prelude.readIO: no parse")
236 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
237 #endif /* __HUGS__ */
239 hReady :: Handle -> IO Bool
240 hReady h = hWaitForInput h 0
242 hPutStrLn :: Handle -> String -> IO ()
243 hPutStrLn hndl str = do
247 hPrint :: Show a => Handle -> a -> IO ()
248 hPrint hdl = hPutStrLn hdl . show
250 -- ---------------------------------------------------------------------------
253 #ifdef __GLASGOW_HASKELL__
254 fixIO :: (a -> IO a) -> IO a
255 fixIO m = stToIO (fixST (ioToST . m))
258 fixIO :: (a -> IO a) -> IO a
259 fixIO f = let x = unsafePerformIO (f x) in return x