summaryrefslogtreecommitdiff
path: root/paper/While/Simple.icl
blob: 7ef042fb433400734498a8e3bf6754e5fba3d33a (plain) (blame)
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
implementation module Simple

import StdString

import WhileCommon
import Common

instance toString Stm
where
	toString (Ass v a) = v <+ " := " <+ a
	toString (If b s1 s2) = "if " <+ b <+ " then " <+ s1 <+ " else " <+ s2
	toString (While b s) = "while " <+ b <+ " do " <+ s
	toString Skip = "skip"
	toString (Compose s1 s2) = s1 <+ "; " <+ s2

instance run Stm
where
	run :: Stm State -> Either Error State
	run (Ass v e) st       // Assign e to v
		= pure (\w -> if (w == v) (eval e st) (st w))
	run (If b s1 s2) st    // If b then s1 else s2
		= eval b st >>= \r -> run (if r s1 s2) st
	run w=:(While b s) st // While b do s
		= eval b st >>= \r -> if r
			(run s st >>= run w)
			(pure st)
	run Skip st            // Skip
		= pure st
	run (Compose s1 s2) st // s1 ; s2
		= run s1 st >>= run s2