66e7b4e66c0514974780aa8a063d67be784acdb5
[packages/random.git] / System / IO.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 --
4 -- Module : System.IO
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- $Id: IO.hs,v 1.5 2002/02/27 14:32:23 simonmar Exp $
13 --
14 -- The standard IO library.
15 --
16 -----------------------------------------------------------------------------
17
18 module System.IO (
19 Handle, -- abstract, instance of: Eq, Show.
20 HandlePosn(..), -- abstract, instance of: Eq, Show.
21
22 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
23 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
24 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
25
26 stdin, stdout, stderr, -- :: Handle
27
28 openFile, -- :: FilePath -> IOMode -> IO Handle
29 hClose, -- :: Handle -> IO ()
30 hFileSize, -- :: Handle -> IO Integer
31 hIsEOF, -- :: Handle -> IO Bool
32 isEOF, -- :: IO Bool
33
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 hTell, -- :: Handle -> IO Integer
41 hWaitForInput, -- :: Handle -> Int -> IO Bool
42 hReady, -- :: Handle -> IO Bool
43 hGetChar, -- :: Handle -> IO Char
44 hGetLine, -- :: Handle -> IO [Char]
45 hLookAhead, -- :: Handle -> IO Char
46 hGetContents, -- :: Handle -> IO [Char]
47 hPutChar, -- :: Handle -> Char -> IO ()
48 hPutStr, -- :: Handle -> [Char] -> IO ()
49 hPutStrLn, -- :: Handle -> [Char] -> IO ()
50 hPrint, -- :: Show a => Handle -> a -> IO ()
51 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
52 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
53 hIsSeekable, -- :: Handle -> IO Bool
54
55 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
56 isAlreadyInUseError, isFullError,
57 isEOFError, isIllegalOperation,
58 isPermissionError, isUserError,
59
60 ioeGetErrorString, -- :: IOError -> String
61 ioeGetHandle, -- :: IOError -> Maybe Handle
62 ioeGetFileName, -- :: IOError -> Maybe FilePath
63
64 try, -- :: IO a -> IO (Either IOError a)
65 bracket, -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
66 bracket_, -- :: IO a -> (a -> IO b) -> IO c -> IO c
67
68 -- Non-standard extension (but will hopefully become standard with 1.5) is
69 -- to export the Prelude io functions via IO (in addition to exporting them
70 -- from the prelude...for now.)
71 IO, -- instance MonadFix
72 FilePath, -- :: String
73 IOError,
74 ioError, -- :: IOError -> IO a
75 userError, -- :: String -> IOError
76 catch, -- :: IO a -> (IOError -> IO a) -> IO a
77 interact, -- :: (String -> String) -> IO ()
78
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
91
92 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
93 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
94
95 fixIO, -- :: (a -> IO a) -> IO a
96
97 hSetEcho, -- :: Handle -> Bool -> IO ()
98 hGetEcho, -- :: Handle -> IO Bool
99
100 hIsTerminalDevice, -- :: Handle -> IO Bool
101 ) where
102
103 #ifdef __GLASGOW_HASKELL__
104 import GHC.Base
105 import GHC.IOBase -- Together these four Prelude modules define
106 import GHC.Handle -- all the stuff exported by IO for the GHC version
107 import GHC.IO
108 import GHC.ST ( fixST )
109 import GHC.Exception
110 import GHC.Num
111 import GHC.Read
112 import GHC.Show
113 #endif
114
115 import Data.Dynamic
116 import Control.Monad.Fix
117 import System.IO.Error
118
119 -- -----------------------------------------------------------------------------
120 -- MonadFix instance
121
122 instance MonadFix IO where
123 mfix = fixIO
124
125 -- -----------------------------------------------------------------------------
126 -- Typeable instance for Handle
127
128 #include "Dynamic.h"
129 INSTANCE_TYPEABLE0(Handle,handleTc,"Handle")
130
131 -- -----------------------------------------------------------------------------
132 -- Standard IO
133
134 putChar :: Char -> IO ()
135 putChar c = hPutChar stdout c
136
137 putStr :: String -> IO ()
138 putStr s = hPutStr stdout s
139
140 putStrLn :: String -> IO ()
141 putStrLn s = do putStr s
142 putChar '\n'
143
144 print :: Show a => a -> IO ()
145 print x = putStrLn (show x)
146
147 getChar :: IO Char
148 getChar = hGetChar stdin
149
150 getLine :: IO String
151 getLine = hGetLine stdin
152
153 getContents :: IO String
154 getContents = hGetContents stdin
155
156 interact :: (String -> String) -> IO ()
157 interact f = do s <- getContents
158 putStr (f s)
159
160 readFile :: FilePath -> IO String
161 readFile name = openFile name ReadMode >>= hGetContents
162
163 writeFile :: FilePath -> String -> IO ()
164 writeFile name str = do
165 hdl <- openFile name WriteMode
166 hPutStr hdl str
167 hClose hdl
168
169 appendFile :: FilePath -> String -> IO ()
170 appendFile name str = do
171 hdl <- openFile name AppendMode
172 hPutStr hdl str
173 hClose hdl
174
175 readLn :: Read a => IO a
176 readLn = do l <- getLine
177 r <- readIO l
178 return r
179
180 -- raises an exception instead of an error
181 readIO :: Read a => String -> IO a
182 readIO s = case (do { (x,t) <- reads s ;
183 ("","") <- lex t ;
184 return x }) of
185 [x] -> return x
186 [] -> ioError (userError "Prelude.readIO: no parse")
187 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
188
189 hReady :: Handle -> IO Bool
190 hReady h = hWaitForInput h 0
191
192 hPutStrLn :: Handle -> String -> IO ()
193 hPutStrLn hndl str = do
194 hPutStr hndl str
195 hPutChar hndl '\n'
196
197 hPrint :: Show a => Handle -> a -> IO ()
198 hPrint hdl = hPutStrLn hdl . show
199
200 -- ---------------------------------------------------------------------------
201 -- fixIO
202
203 #ifdef __GLASGOW_HASKELL__
204 fixIO :: (a -> IO a) -> IO a
205 fixIO m = stToIO (fixST (ioToST . m))
206 #endif