-
Notifications
You must be signed in to change notification settings - Fork 2
/
iomonad.gs
52 lines (43 loc) · 2.11 KB
/
iomonad.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
------------------------------------------------------------------------------
-- This file contains a Gofer implementation of Lazy state threads, as
-- described in the PLDI '94 paper by John Launchbury and Simon Peyton
-- Jones, using new Gofer primitives added in Gofer 2.30.
--
-- This file is included for the benefit of those interested in
-- experimenting with the use of lazy functional state threads.
-- You may expect to see changes to the definitions in this file,
-- to track future proposals for monadic I/O in Haskell.
--
-- This file requires the standard, or cc prelude.
-- You will not be able to use this file unless the version of Gofer that
-- is installed on your machine has been compiled with the IO_MONAD flag
-- set to 1.
--
-- Mark P Jones, 1994
------------------------------------------------------------------------------
module LazyStateThd( thenST, thenST_, returnST, newVar, readVar, WriteVar,
mutVarEq, getch, putchar, thenIO, seqST, putString,
getchar, interleaveST
) where
infixr `thenST_`, `thenST`
primitive returnST "primSTReturn" :: a -> ST s a
primitive thenST "primSTBind" :: ST s a -> (a -> ST s b) -> ST s b
primitive newVar "primSTNew" :: a -> ST s (MutVar s a)
primitive readVar "primSTDeref" :: MutVar s a -> ST s a
primitive writeVar "primSTAssign" :: MutVar s a -> a -> ST s ()
primitive mutvarEq "primSTMutVarEq" :: MutVar s a -> MutVar s a -> Bool
primitive getch "primIOGetch" :: IO Char
primitive putchar "primIOPutchar" :: Char -> IO ()
primitive thenIO "primIOBind" :: IO a -> (a -> IO b) -> IO b
primitive interleaveST "primSTInter" :: ST s a -> ST s a
instance Eq (MutVar s a) where (==) = mutvarEq
thenST_ :: ST s () -> ST s b -> ST s b
p `thenST_` q = p `thenST` \() -> q
seqST :: [ST s ()] -> ST s ()
seqST = foldr thenST_ (returnST ())
putString :: String -> IO ()
putString = seqST . map putchar
getchar = getch `thenST` \c ->
putchar c `thenST_`
returnST c
------------------------------------------------------------------------------