2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \section[PrelMonIO]{Monadic I/O Primitives}
6 This module defines the basic monadic framework for Haskell 1.3 I/O.
10 module PreludeMonadicIO (
33 import Prel ( (.), not )
34 import PS ( _PackedString, _unpackPS )
46 \subsection[IOMonad]{The IO Monad}
48 I/O operations may need to indicate errors, and implementations may
49 need to handle these errors. The $IO$ monad extends existing practice
50 by making this functionality primitive. The exact errors which may
51 occur are defined in $PreludeIOError13$.
55 type IO a = PrimIO (Either IOError13 a)
57 data Either a b = Left a | Right b deriving (Text, Eq, Ord)
61 An expression of type $IO a$, for some type {\em a}, denotes a
62 computation whose answer is either a result of type {\em a} or an
63 <em>error</em> of type $IOError13$. The computation succeeds with
64 result {\em succ} if its answer is $Right succ$, and fails with
65 result {\em fail} if its answer is $Left fail$. Note that the
66 type system delimits the possibility of failure: only expressions of
67 some type $IO a$ can <em>fail</em> in the sense defined here.
72 {-# INLINE failWith #-}
75 failWith :: IOError13 -> IO a
77 return = returnPrimIO . Right
78 failWith = returnPrimIO . Left
82 There are two primitives to create trivial computations, one for
83 each of the two possibilities, success or failure.
85 $return result$ is a computation that succeeds with result
88 $failWith fail$ is a computation that fails with the error
95 (>>=) :: IO a -> (a -> IO b) -> IO b
96 m >>= k = m `thenPrimIO` \ r -> k' r
99 k' (Left err) = returnPrimIO (Left err)
103 The $>>=$ operation is used to sequence two computations, where the
104 second computation is parameterised on the result of the first.
110 (>>) :: IO a -> IO b -> IO b
111 m >> k = m >>= \ _ -> k
115 The restricted form of $>>=$, $>>$, is used when the result of the
116 first computation is uninteresting.
118 \subsection[Error-Handling]{Error Handling}
122 handle :: IO a -> (IOError13 -> IO a) -> IO a
123 handle m k = m `thenPrimIO` \ r -> k' r
125 k' (Left err) = k err
126 k' result = returnPrimIO result
130 The construct $handle comp handler$ can be used to handle a
131 simple error during a computation {\em comp}. Its usefulness is
132 limited in that the replacement value must be of the same type as the
133 result of {\em comp}.
137 try :: IO a -> IO (Either IOError13 a)
138 try p = handle (p >>= (return . Right)) (return . Left)
142 The construct $try comp$ exposes errors which occur within a
143 computation, and which are not fully handled. It always succeeds.
145 \subsection[UserErrors]{User-Defined Errors}
149 fail :: String -> IO a
150 fail = failWith . UserError
154 As a convention for user-generated errors, to return an error message
155 $msg :: String$, return the error value $UserError msg$
156 via the computation $fail msg$.
158 This construct should be used instead of Haskell's $error :: String -> a$
159 operation wherever convenient.
161 \subsection[HOFs]{Higher-Order Utility Functions}
165 either :: (a -> c) -> (b -> c) -> (Either a b) -> c
166 either kl kr x = case x of {Left a -> kl a; Right b -> kr b}
170 The construct $either a b$ can be used to generate functions on types
171 of the form $Either a b$.
175 accumulate :: [IO a] -> IO [a]
177 accumulate [] = return []
180 accumulate fs >>= \ xs ->
183 {- partain: this may be right, but I'm going w/ a more-certainly-right version
184 accumulate = foldr mcons (return [])
186 mcons :: IO a -> IO [a] -> IO [a]
187 mcons p q = p >>= \x -> q >>= \y -> return (x : y)
192 The $accumulate$ computation is used to process a list of computations
193 of the same type, and to return a list of their results when executed
198 sequence :: [IO a] -> IO ()
199 sequence = foldr (>>) (return ())
203 The $sequence$ computation is used for the simpler case when the
204 computations are executed entirely for their external effect, and the
205 results are therefore uninteresting.