switch to new Haddock-generated library docs
[haskell-report.git] / report / PreludeIO.hs
1 module PreludeIO (
2 FilePath, IOError, ioError, userError, catch,
3 putChar, putStr, putStrLn, print,
4 getChar, getLine, getContents, interact,
5 readFile, writeFile, appendFile, readIO, readLn
6 ) where
7
8 import PreludeBuiltin
9
10
11 type FilePath = String
12
13 data IOError -- The internals of this type are system dependent
14
15 instance Show IOError where ...
16 instance Eq IOError where ...
17
18 ioError :: IOError -> IO a
19 ioError = primIOError
20
21 userError :: String -> IOError
22 userError = primUserError
23
24 catch :: IO a -> (IOError -> IO a) -> IO a
25 catch = primCatch
26
27 putChar :: Char -> IO ()
28 putChar = primPutChar
29
30 putStr :: String -> IO ()
31 putStr s = mapM_ putChar s
32
33 putStrLn :: String -> IO ()
34 putStrLn s = do putStr s
35 putStr "\n"
36
37 print :: Show a => a -> IO ()
38 print x = putStrLn (show x)
39
40 getChar :: IO Char
41 getChar = primGetChar
42
43 getLine :: IO String
44 getLine = do c <- getChar
45 if c == '\n' then return "" else
46 do s <- getLine
47 return (c:s)
48
49 getContents :: IO String
50 getContents = primGetContents
51
52 interact :: (String -> String) -> IO ()
53 -- The hSetBuffering ensures the expected interactive behaviour
54 interact f = do hSetBuffering stdin NoBuffering
55 hSetBuffering stdout NoBuffering
56 s <- getContents
57 putStr (f s)
58
59 readFile :: FilePath -> IO String
60 readFile = primReadFile
61
62 writeFile :: FilePath -> String -> IO ()
63 writeFile = primWriteFile
64
65 appendFile :: FilePath -> String -> IO ()
66 appendFile = primAppendFile
67
68 -- raises an exception instead of an error
69 readIO :: Read a => String -> IO a
70 readIO s = case [x | (x,t) <- reads s, ("","") <- lex t] of
71 [x] -> return x
72 [] -> ioError (userError "Prelude.readIO: no parse")
73 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
74
75 readLn :: Read a => IO a
76 readLn = do l <- getLine
77 r <- readIO l
78 return r