1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- $Id: IO.hs,v 1.3 2002/02/05 17:32:27 simonmar Exp $
14 -- The standard IO library.
16 -----------------------------------------------------------------------------
19 Handle, -- abstract, instance of: Eq, Show.
20 HandlePosn(..), -- abstract, instance of: Eq, Show.
22 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
23 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
24 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
26 stdin, stdout, stderr, -- :: Handle
28 openFile, -- :: FilePath -> IOMode -> IO Handle
29 hClose, -- :: Handle -> IO ()
30 hFileSize, -- :: Handle -> IO Integer
31 hIsEOF, -- :: Handle -> IO Bool
34 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
35 hGetBuffering, -- :: Handle -> IO BufferMode
36 hFlush, -- :: Handle -> IO ()
37 hGetPosn, -- :: Handle -> IO HandlePosn
38 hSetPosn, -- :: HandlePosn -> IO ()
39 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
40 hWaitForInput, -- :: Handle -> Int -> IO Bool
41 hReady, -- :: Handle -> IO Bool
42 hGetChar, -- :: Handle -> IO Char
43 hGetLine, -- :: Handle -> IO [Char]
44 hLookAhead, -- :: Handle -> IO Char
45 hGetContents, -- :: Handle -> IO [Char]
46 hPutChar, -- :: Handle -> Char -> IO ()
47 hPutStr, -- :: Handle -> [Char] -> IO ()
48 hPutStrLn, -- :: Handle -> [Char] -> IO ()
49 hPrint, -- :: Show a => Handle -> a -> IO ()
50 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
51 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
52 hIsSeekable, -- :: Handle -> IO Bool
54 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
55 isAlreadyInUseError, isFullError,
56 isEOFError, isIllegalOperation,
57 isPermissionError, isUserError,
59 ioeGetErrorString, -- :: IOError -> String
60 ioeGetHandle, -- :: IOError -> Maybe Handle
61 ioeGetFileName, -- :: IOError -> Maybe FilePath
63 try, -- :: IO a -> IO (Either IOError a)
64 bracket, -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
65 bracket_, -- :: IO a -> (a -> IO b) -> IO c -> IO c
67 -- Non-standard extension (but will hopefully become standard with 1.5) is
68 -- to export the Prelude io functions via IO (in addition to exporting them
69 -- from the prelude...for now.)
70 IO, -- instance MonadFix
71 FilePath, -- :: String
73 ioError, -- :: IOError -> IO a
74 userError, -- :: String -> IOError
75 catch, -- :: IO a -> (IOError -> IO a) -> IO a
76 interact, -- :: (String -> String) -> IO ()
78 putChar, -- :: Char -> IO ()
79 putStr, -- :: String -> IO ()
80 putStrLn, -- :: String -> IO ()
81 print, -- :: Show a => a -> IO ()
82 getChar, -- :: IO Char
83 getLine, -- :: IO String
84 getContents, -- :: IO String
85 readFile, -- :: FilePath -> IO String
86 writeFile, -- :: FilePath -> String -> IO ()
87 appendFile, -- :: FilePath -> String -> IO ()
88 readIO, -- :: Read a => String -> IO a
89 readLn, -- :: Read a => IO a
91 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
92 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
94 fixIO, -- :: (a -> IO a) -> IO a
97 #ifdef __GLASGOW_HASKELL__
99 import GHC.IOBase -- Together these four Prelude modules define
100 import GHC.Handle -- all the stuff exported by IO for the GHC version
102 import GHC.ST ( fixST )
110 import Control.Monad.Fix
111 import System.IO.Error
113 -- -----------------------------------------------------------------------------
116 instance MonadFix IO where
119 -- -----------------------------------------------------------------------------
120 -- Typeable instance for Handle
123 INSTANCE_TYPEABLE0(Handle,handleTc,"Handle")
125 -- -----------------------------------------------------------------------------
128 putChar :: Char -> IO ()
129 putChar c = hPutChar stdout c
131 putStr :: String -> IO ()
132 putStr s = hPutStr stdout s
134 putStrLn :: String -> IO ()
135 putStrLn s = do putStr s
138 print :: Show a => a -> IO ()
139 print x = putStrLn (show x)
142 getChar = hGetChar stdin
145 getLine = hGetLine stdin
147 getContents :: IO String
148 getContents = hGetContents stdin
150 interact :: (String -> String) -> IO ()
151 interact f = do s <- getContents
154 readFile :: FilePath -> IO String
155 readFile name = openFile name ReadMode >>= hGetContents
157 writeFile :: FilePath -> String -> IO ()
158 writeFile name str = do
159 hdl <- openFile name WriteMode
163 appendFile :: FilePath -> String -> IO ()
164 appendFile name str = do
165 hdl <- openFile name AppendMode
169 readLn :: Read a => IO a
170 readLn = do l <- getLine
174 -- raises an exception instead of an error
175 readIO :: Read a => String -> IO a
176 readIO s = case (do { (x,t) <- reads s ;
180 [] -> ioError (userError "Prelude.readIO: no parse")
181 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
183 hReady :: Handle -> IO Bool
184 hReady h = hWaitForInput h 0
186 hPutStrLn :: Handle -> String -> IO ()
187 hPutStrLn hndl str = do
191 hPrint :: Show a => Handle -> a -> IO ()
192 hPrint hdl = hPutStrLn hdl . show
194 -- ---------------------------------------------------------------------------
197 #ifdef __GLASGOW_HASKELL__
198 fixIO :: (a -> IO a) -> IO a
199 fixIO m = stToIO (fixST (ioToST . m))