[project @ 1997-08-25 22:40:33 by sof]
[ghc-hetmet.git] / ghc / lib / ghc / PrelIO.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[PrelIO]{Module @PrelIO@}
6
7 Input/output functions mandated by the standard Prelude.
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module PrelIO (
13         IO, FilePath, IOError, 
14         fail, userError, catch,
15         putChar, putStr, putStrLn, print,
16         getChar, getLine, getContents, interact,
17         readFile, writeFile, appendFile, readIO, readLn
18     ) where
19
20 import IO
21 import IOHandle
22 import IOBase
23 import PrelBase
24 import PrelRead
25
26 \end{code}
27
28 \begin{code}
29 putChar         :: Char -> IO ()
30 putChar c       =  hPutChar stdout c
31
32 putStr          :: String -> IO ()
33 putStr s        =  hPutStr stdout s
34
35 putStrLn        :: String -> IO ()
36 putStrLn s      =  do putStr s
37                       putChar '\n'
38
39 print           :: Show a => a -> IO ()
40 print x         =  putStrLn (show x)
41
42 getChar         :: IO Char
43 getChar         =  hGetChar stdin
44
45 getLine         :: IO String
46 getLine         =  hGetLine stdin
47             
48 getContents     :: IO String
49 getContents     =  hGetContents stdin
50
51 interact        ::  (String -> String) -> IO ()
52 interact f      =   do s <- getContents
53                        putStr (f s)
54
55 readFile        :: FilePath -> IO String
56 readFile name   =  openFile name ReadMode >>= hGetContents
57
58 writeFile       :: FilePath -> String -> IO ()
59 writeFile name str
60   = openFile name WriteMode >>= \hdl -> hPutStr hdl str >> hClose hdl
61
62 appendFile      :: FilePath -> String -> IO ()
63 appendFile name str
64   = openFile name AppendMode >>= \hdl -> hPutStr hdl str >> hClose hdl
65
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
69                         [x] -> return x
70                         []  -> fail (userError "PreludeIO.readIO: no parse")
71                         _   -> fail (userError 
72                                       "PreludeIO.readIO: ambiguous parse")
73
74 readLn          :: Read a => IO a
75 readLn          =  do l <- getLine
76                       r <- readIO l
77                       return r
78 \end{code}