summaryrefslogtreecommitdiff
path: root/progs/prelude/PreludeIOMonad.hs
blob: 9a45606cb20ad50f6aa42141d88d94b8c164fe4f (about) (plain)
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
53
54
55
56
57
58
59
60
module IOMonad (State, IO(..)) where

import IOMonadPrims

{- I use data instead of type so that IO can be abstract. For efficiency,
   IO can be annotated as a strict constructor.
-}

type IO a = State -> (State, a)

data State = State

-- The rest of this file is unnecessary at the moment since
-- unitIO & bindIO are primitives and we're not using the rest of this

{- Implemented as a primitives: 
bindIO :: IO a -> (a -> IO b) -> IO b
bindIO (IO m) (IO k) = IO (\s0 -> let (s1, a) = m s0 in k a s1) -}

unitIO :: a -> IO a
unitIO x = IO (\s -> (s, x))

-}

{-  Not currently used:
pureIO :: IO a -> a
pureIO (IO m) = let (s, x) = m State in x

-- execIO executes a program of type IO ().
execIO :: IO () -> State
execIO (IO m) = let (s, x) = m State in s

infixr  1 =:
infixr  1 ?

-- assignment
(=:)      :: a -> Var a -> IO ()
x =: v  = IO (\s -> (update v x s, ()))

-- reader
(?)       :: Var a -> (a -> IO b) -> IO b
v ? k   = IO (\s -> (s, readVar v s)) `bindIO` k

-- new
newvar    :: IO (Var a)
newvar = IO allocVar

instance Eq (Var a) where
   x == y = eqVar x y
-}