[project @ 2001-08-22 11:45:06 by sewardj]
[ghc-hetmet.git] / ghc / tests / programs / ipoole_spec_class / Io.lhs
1 \input{LiterateUtils}
2 {
3 \DownLevel
4 \filetitle{IO.lgs --- Implementation of the basic I/O monad}
5 \author{Andy Gill \\ University of Glasgow.\\(edited by IP)}
6 \maybemaketitle
7 %
8 %
9 % SCCS: %W% %G%
10 %
11 % Modifications
12 % -------------
13 % 15-01-94      ipoole         IO --> Io  (to keep ghc happy)
14 % 04-09-93      ipoole         #ifdef Gofer, so we can compile with hbc
15 %                              (and maybe ghc)
16 % 02-09-93      ipoole         extracted from Andy's prelude, name changes:
17 %                              returnIO --> unitIO, thenIO --> bindIO.
18
19 \begin{vb}
20
21 > module Io where
22
23 \end{verbatim}\end{vb}
24
25 This is the basic monad upon which the \verb@Job s1 s2 a@ monad is defined.
26
27 \begin{Dec}{Io}         
28 The Io monad, defined in terms of a Haskell Dialogue.
29 \begin{vb}
30
31 > type Io a = (a -> Dialogue) -> Dialogue
32
33 #ifdef Gofer
34
35 >       in unitIo,  bindIo,
36 >          ioToDialogue, processRequestIo, doneIo
37 >
38
39 #endif
40
41 \end{verbatim}\end{vb}\end{Dec}
42
43 \begin{Def}{unitIo}
44 The operation which returns a result without performing I/O.
45
46 \begin{vb}
47
48 > unitIo :: x -> Io x
49 > unitIo x cont = cont x
50
51 \end{verbatim}\end{vb}\end{Def}
52
53 \begin{Def}{bindIo}
54 Connect an Io operation to a continuation.
55 \begin{vb}
56
57 > bindIo :: Io a -> (a -> Io b) -> Io b
58 > bindIo m k cont = m (\ a -> k a cont)
59
60 \end{verbatim}\end{vb}\end{Def}
61
62 \begin{Def}{ioToDialogue}
63 Convert an Io to a runable Haskell Dialogue
64 \begin{vb}
65
66 > ioToDialogue :: Io a -> Dialogue
67 > ioToDialogue io = io (const (const []))
68
69 \end{verbatim}\end{vb}\end{Def}
70
71 \begin{Def}{processRequestIo}
72 Output a Haskell Request and get back the response.
73 \begin{vb}
74
75 > processRequestIo   :: Request -> Io Response
76 > processRequestIo req cont ~(resp:resps) = req : cont resp resps
77
78 \end{verbatim}\end{vb}\end{Def}
79
80 \begin{Def}{doneIo}
81 Terminate the Io.
82 \begin{vb}
83
84 > doneIo :: Io a
85 > doneIo cont = \ _ -> []
86
87 \end{verbatim}\end{vb}\end{Def}
88 }
89 \EndFile
90
91