-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.hs
37 lines (31 loc) · 1.06 KB
/
Command.hs
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
--------------------------------------------------------------------------------
-- Pepe Gallardo, January 2004
--
-- A monadic interpreter for Dijkstra's Guarded Command Language:
-- https://en.wikipedia.org/wiki/Guarded_Command_Language
--
--
-- Abstract Syntax Tree of language
--------------------------------------------------------------------------------
module Command
( Command(..)
, GuardedCommand(..)
, select
) where
import Expression
infix 3 :=
infixr 2 :$
infix 1 :->
data Command = Skip -- No operation
| Abort -- Abort program
| Variable := Expr -- Variable assigment
| Command :$ Command -- Sequence
| If [GuardedCommand] -- Guarded sentence
| Do [GuardedCommand] -- Guarded repetition
data GuardedCommand = Expr :-> Command
select :: (Monad m) => Environment -> [GuardedCommand] -> m [Command]
select rho [] = return []
select rho ((b :-> s):gs) = do
B b <- eval rho b
ss <- select rho gs
return (if b then (s:ss) else ss)