-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbabelsberg.rml
151 lines (120 loc) · 4.47 KB
/
babelsberg.rml
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(* file assignment.rml *)
module babelsberg:
(* Abstract syntax for the BabelsbergP language *)
datatype Program = PROGRAM of Statement
datatype Statement = SKIP
| ASSIGN of Variable * Exp
| ALWAYS of Constraint
| ONCE of Constraint
| SEQ of Statement * Statement
| IF of Exp * Statement * Statement
| WHILE of Exp * Statement
datatype Constraint = CONSTRAINT of Rho * Exp
| COMPOUNDCONSTRAINT of Constraint * Constraint
datatype Rho = WEAK | MEDIUM | REQUIRED
datatype Exp = VALUE of Value
| VARIABLE of Variable
| OP of Exp * Op * Exp
datatype Constant = REAL of real | TRUE | FALSE | NIL | STRING of string
type Variable = string
datatype Op = ADD | SUB | MUL | DIV | LESSTHAN | LEQUAL | EQUAL | NEQUAL | GEQUAL | GREATERTHAN | AND | OR
(* Values stored in environments *)
type Value = Constant
(* Bindings and environments *)
type VarBnd = (Variable * Value)
type Env = VarBnd list
type Cstore = Constraint
relation evalprogram: Program => ()
end
relation evalprogram: Program => () =
rule print "starting to evaluate\n" &
step([], CONSTRAINT(REQUIRED, VALUE(TRUE)), statement) => (Env, Cstore)
-------------------------------------
evalprogram(PROGRAM(statement))
end
relation eval: (Env, Exp) => Value =
axiom eval(_, VALUE(c)) => c
rule print "E-Var\n" &
Util.lookupEnv(Env, x) => v
----------------------
eval(Env, VARIABLE(x)) => v
rule eval(Env, e1) => v1 &
Util.should_short_circuit(op,v1) => (true, v) &
print "E-Op (short circuit)\n"
------------------------------
eval(Env, OP(e1, op, e2)) => v
rule print "E-Op\n" &
eval(Env, e1) => v1 &
eval(Env, e2) => v2 &
Util.apply_binop(op,v1,v2) => v
------------------------------
eval(Env, OP(e1, op, e2)) => v
end
relation models: Constraint => Env =
rule Print.printC(C) => Cstring &
Solver.solve(Cstring) => Esolution &
Print.parseEnvironment(Esolution, []) => E &
print "\n"
------------------------------
models(C) => E
end
relation stayEnv: (Env, Rho) => Constraint =
axiom stayEnv([], rho) => CONSTRAINT(REQUIRED, VALUE(TRUE))
rule print "StayEnv\n" &
stayEnv(E0, rho) => C0 &
stay(OP(VARIABLE(x), EQUAL, VALUE(v)), rho) => C1
-------------------------------------------------
stayEnv((x, v) :: E0, rho) => COMPOUNDCONSTRAINT(C0, C1)
end
relation stay: (Exp, Rho) => Constraint =
axiom stay(OP(VARIABLE(x), EQUAL, VALUE(v)), rho) => CONSTRAINT(WEAK, OP(VARIABLE(x), EQUAL, VALUE(v)))
end
relation step: (Env, Cstore, Statement) => (Env, Cstore) =
rule print "S-Asgn\n" &
eval(Env,e) => v &
stayEnv(Env, WEAK) => Cs &
models(COMPOUNDCONSTRAINT(COMPOUNDCONSTRAINT(Cstore, Cs),
CONSTRAINT(REQUIRED, OP(VARIABLE(x), EQUAL, VALUE(v))))) => Env'
-----------------------------------------------------------------------------------------------
step(Env, Cstore, ASSIGN(x, e)) => (Env', Cstore)
rule print "S-Once\n" &
stayEnv(Env, WEAK) => Cs &
models(COMPOUNDCONSTRAINT(COMPOUNDCONSTRAINT(Cstore, Cs), C0)) => Env'
----------------------------------------------------------------------
step(Env, Cstore, ONCE(C0)) => (Env', Cstore)
rule print "S-Always\n" &
step(Env, Cstore, ONCE(C0)) => (Env', Cstore)
---------------------------------------------
step(Env, Cstore, ALWAYS(C0)) => (Env', COMPOUNDCONSTRAINT(Cstore, C0))
axiom step(Env, Cstore, SKIP) => (Env, Cstore)
rule print "S-SeqStep\n" &
step(Env, Cstore, s1) => (Env', Cstore') &
step(Env', Cstore', s2) => (Env'', Cstore'')
--------------------------------------------------
step(Env, Cstore, SEQ(s1, s2)) => (Env'', Cstore'')
rule eval(Env, e) => TRUE &
print "S-IfThen\n" &
step(Env, Cstore, s1) => (Env', Cstore')
----------------------------------------
step(Env, Cstore, IF(e, s1, s2)) => (Env', Cstore')
rule eval(Env, e) => v &
not v = TRUE &
print "S-IfElse\n" &
step(Env, Cstore, s2) => (Env', Cstore')
----------------------------------------
step(Env, Cstore, IF(e, s1, s2)) => (Env', Cstore')
rule eval(Env, e) => TRUE &
print "S-WhileDo\n" &
step(Env, Cstore, s) => (Env', Cstore') &
step(Env', Cstore', WHILE(e, s)) => (Env'', Cstore'')
-----------------------------------------------------
step(Env, Cstore, WHILE(e, s)) => (Env'', Cstore'')
rule eval(Env, e) => v &
not v = TRUE &
print "S-WhileSkip\n"
-----------------------------------------------------
step(Env, Cstore, WHILE(e, s)) => (Env, Cstore)
end
with "solver.rml"
with "printer.rml"
with "helper.rml"