-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples.hs
118 lines (102 loc) · 2.54 KB
/
Examples.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--------------------------------------------------------------------------------
-- Pepe Gallardo, January 2004
--
-- A monadic interpreter for Dijkstra's Guarded Command Language:
-- https://en.wikipedia.org/wiki/Guarded_Command_Language
--
-- Some programming examples
--------------------------------------------------------------------------------
module Examples
( simple
, divCero
, nonDeterm
, nonDetermError
, gcDiv
, urn
, examples
) where
import Expression
import Command
integer = Const . I
bool = Const . B
zero = integer 0
one = integer 1
two = integer 2
three = integer 3
varX = Var "X"
varY = Var "Y"
varZ = Var "Z"
varB = Var "B"
varW = Var "W"
-- Simple example
simple =
"X" := one :$
"Y" := varX :+ two :$
"Z" := two :* varY :- varY
-- Division by zero error
divCero =
"X" := zero :$
"Y" := one :/ varX :$
"Z" := three
-- Non deterministic selection
--
-- (3,3)
-- / \
-- (2,3) (3,2)
-- / \ / \
-- (1,3) (2,2)(2,2) (3,1)
nonDeterm =
"X" := three :$
"Y" := three :$
nonDetermIf :$
nonDetermIf
where
nonDetermIf =
If [ varX :> zero :-> "X" := varX :- one
, varY :> zero :-> "Y" := varY :- one
]
-- Non deterministic repetition (with errors)
--
-- (2,1)
-- |
-- -----------------+----
-- / | \
-- / | \
-- (1,1) (2,0) Error <--- First iteration
-- / \ / \ \
-- (0,1) (1,0) (1,0) Error \ <--- Second iteration
-- | / \ / \ \ \
-- (0,0) (0,0) Error (0,0) Error Error Error <--- Third iteration
nonDetermError =
"X" := two :$
"Y" := one :$
Do [ varX :> zero :-> "X" := varX :- one
, varY :> zero :-> "Y" := varY :- one
, varX :> varY :-> "X" := varY :/ zero
]
-- Deterministic repetition
gcDiv x y =
"X" := integer x :$
"Y" := integer y :$
Do [ varX :> varY :-> "X" := varX :- varY
, varY :> varX :-> "Y" := varY :- varX
]
-- Non deterministic repetition. Urns' problem
urn w b =
"W" := integer w :$
"B" := integer b :$
Do [ varB :> one :-> "B" := varB :- one
, varW :> one :-> "B" := varB :+ one :$
"W" := varW :- two
, varW :> zero
:/\
varB :> zero :-> "B" := varB :- one
]
examples =
[ simple
, divCero
, nonDeterm
, nonDetermError
, gcDiv 100 25
, urn 2 2
]