2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[PrelIO]{Module @PrelIO@}
7 Input/output functions mandated by the standard Prelude.
10 {-# OPTIONS -fno-implicit-prelude #-}
13 IO, FilePath, IOError,
14 fail, userError, catch,
15 putChar, putStr, putStrLn, print,
16 getChar, getLine, getContents, interact,
17 readFile, writeFile, appendFile, readIO, readLn
29 putChar :: Char -> IO ()
30 putChar c = hPutChar stdout c
32 putStr :: String -> IO ()
33 putStr s = hPutStr stdout s
35 putStrLn :: String -> IO ()
36 putStrLn s = do putStr s
39 print :: Show a => a -> IO ()
40 print x = putStrLn (show x)
43 getChar = hGetChar stdin
46 getLine = hGetLine stdin
48 getContents :: IO String
49 getContents = hGetContents stdin
51 interact :: (String -> String) -> IO ()
52 interact f = do s <- getContents
55 readFile :: FilePath -> IO String
56 readFile name = openFile name ReadMode >>= hGetContents
58 writeFile :: FilePath -> String -> IO ()
60 = openFile name WriteMode >>= \hdl -> hPutStr hdl str >> hClose hdl
62 appendFile :: FilePath -> String -> IO ()
64 = openFile name AppendMode >>= \hdl -> hPutStr hdl str >> hClose hdl
66 readIO :: Read a => String -> IO a
67 -- raises an exception instead of an error
68 readIO s = case [x | (x,t) <- reads s, ("","") <- lex t] of
70 [] -> fail (userError "PreludeIO.readIO: no parse")
72 "PreludeIO.readIO: ambiguous parse")
74 readLn :: Read a => IO a
75 readLn = do l <- getLine