forked from nasa/pvslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool_expr.pvs
300 lines (219 loc) · 8.42 KB
/
bool_expr.pvs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
bool_expr : THEORY
BEGIN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%| Some basic boolean expressions and |%
%| functions where inputs are boolean |%
%| expressions. |%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IMPORTING hp_expr
DLBOOL(b:bool)(env:Environment) : bool = b
DLTRUE : MACRO BoolExpr = DLBOOL(TRUE)
DLFALSE : MACRO BoolExpr = DLBOOL(FALSE)
DIFF(odes:ODEs) : MACRO HP = DIFF(odes,DLTRUE)
%--------------------------------
%% Equalities and Inequalities
%--------------------------------
dl_eq(r1,r2:RealExpr)(env:Environment) : MACRO bool = (r1(env) = r2(env))
;=(r1,r2: RealExpr): BoolExpr = dl_eq(r1,r2)
DLEQ(r1,r2:RealExpr) : MACRO BoolExpr = (r1 = r2)
dl_neq(r1,r2:RealExpr)(env:Environment) : MACRO bool = (r1(env) /= r2(env))
;/=(r1,r2: RealExpr): BoolExpr = dl_neq(r1,r2)
DLNEQ(r1,r2: RealExpr): MACRO BoolExpr = (r1 /= r2)
dl_le(r1,r2:RealExpr)(env:Environment) : MACRO bool = r1(env) <= r2(env)
;<=(r1,r2:RealExpr) : BoolExpr = dl_le(r1,r2)
DLLE(r1,r2:RealExpr) : MACRO BoolExpr = r1 <= r2
dl_lt(r1,r2:RealExpr)(env:Environment) : MACRO bool = r1(env) < r2(env)
;<(r1,r2:RealExpr) : BoolExpr = dl_lt(r1,r2)
DLLT(r1,r2:RealExpr) : MACRO BoolExpr = r1 < r2
dl_ge(r1,r2:RealExpr)(env:Environment) : MACRO bool = r1(env) >= r2(env)
;>=(r1,r2:RealExpr) : BoolExpr = dl_ge(r1,r2)
DLGE(r1,r2:RealExpr) : MACRO BoolExpr = r1 >= r2
dl_gt(r1,r2:RealExpr)(env:Environment) : MACRO bool = r1(env) > r2(env)
;>(r1,r2:RealExpr) : BoolExpr = dl_gt(r1,r2)
DLGT(r1,r2:RealExpr) : MACRO BoolExpr = r1 > r2
%--------------------------------
%% Freshness of a variable index
%% in a BoolExpr list.
%--------------------------------
Formulas : TYPE = list[BoolExpr]
fresh?(f:Formulas)(i:nat): RECURSIVE bool =
IF f = null THEN TRUE
ELSE fresh?(car(f))(i) AND fresh?(cdr(f))(i)
ENDIF
MEASURE f by <<
fresh_formula_nth: LEMMA
FORALL(f:Formulas,i:(fresh?(f)))(j:below(length(f))):
fresh?(nth(f,j))(i)
fresh_be_invar: LEMMA
FORALL(P:BoolExpr,envi,envo:Environment,k:nat):
(FORALL(m:upfrom(k)): envi(m) = envo(m)) AND (FORALL(i:below(k)): envi(i)/=envo(i) IMPLIES fresh?(P)(i))
IMPLIES
(P(envi) = P(envo))
%-----------------------------------
%% Connectives for BoolExpr and
%% the definitions for DLFORALL,
%% DLEXISTS, ALLRUNS, SOMERUNS.
%-----------------------------------
BoolExpr2list(P:BoolExpr): MACRO Formulas = (: P :)
CONVERSION BoolExpr2list
DLOR(P,Q:BoolExpr)(env:Environment): bool =
P(env) OR Q(env)
;OR(P,Q:BoolExpr): MACRO BoolExpr =
DLOR(P,Q)
DLAND(P,Q:BoolExpr)(env:Environment): bool =
P(env) AND Q(env)
;AND(P,Q:BoolExpr): MACRO BoolExpr =
DLAND(P,Q)
DLNOT(P:BoolExpr)(env:Environment): bool =
NOT P(env)
;NOT(P:BoolExpr): MACRO BoolExpr =
DLNOT(P)
DLIMPLIES(P,Q: BoolExpr)(env:Environment): bool =
P(env) IMPLIES Q(env)
;IMPLIES(P,Q:BoolExpr): MACRO BoolExpr =
DLIMPLIES(P,Q)
A_DLIMPLIES(P,Q:BoolExpr): bool =
FORALL(env:Environment):
DLIMPLIES(P,Q)(env)
;=>(P,Q:BoolExpr): MACRO bool = A_DLIMPLIES(P,Q)
DLFORALL(qQ:QBoolExpr)(env:Environment): bool =
FORALL(x:real):qQ(x)(env)
DLFORALL_sub(P:[real -> bool ])(qQ: QBoolExpr)(env: Environment): bool =
FORALL(t:(P)): qQ(t)(env)
DLEXISTS(qQ:QBoolExpr)(env:Environment): bool =
EXISTS(x:real):qQ(x)(env)
ALLRUNS(hp:HP,P:BoolExpr)(env:Environment): bool =
FORALL (envo:Environment): semantic_rel(hp)(env)(envo) IMPLIES
P(envo)
ALLRUNS_E(hp:HP,P:BoolExpr)(env:Environment): bool =
(EXISTS(envo:Environment ): semantic_rel(hp)(env)(envo)) AND
(FORALL (envo:Environment): semantic_rel(hp)(env)(envo) IMPLIES
P(envo))
SOMERUNS(hp:HP,P:BoolExpr)(env:Environment): bool =
EXISTS (envo:Environment): semantic_rel(hp)(env)(envo) AND P(envo)
QHP: TYPE = [ real -> HP ]
UPTO(qhp:QHP,Q:BoolExpr)
(P:BoolExpr)(t:real):
BoolExpr =
DLIMPLIES(DLAND(cnst(t) >= cnst(0),DLFORALL(LAMBDA(s:real):
DLIMPLIES(DLAND(cnst(s) >= cnst(0), cnst(t) >= cnst(s)),ALLRUNS(qhp(s),Q)))), ALLRUNS(qhp(t),P))
%--------------------------------
%% Boolean rules for rewriting
%--------------------------------
P,Q : VAR BoolExpr
b : VAR bool
A : VAR HP
dl_not_not : LEMMA
(NOT (NOT P)) = P
dl_not_true : LEMMA
(NOT DLTRUE) = DLFALSE
dl_not_false : LEMMA
(NOT DLFALSE) = DLTRUE
dl_true_and: LEMMA
(DLTRUE AND P) = P
dl_and_true: LEMMA
(P AND DLTRUE) = P
dl_false_and: LEMMA
(DLFALSE AND P) = DLFALSE
dl_and_false: LEMMA
(P AND DLFALSE) = DLFALSE
dl_true_or: LEMMA
(DLTRUE OR P) = DLTRUE
dl_or_true: LEMMA
(P OR DLTRUE) = DLTRUE
dl_false_or: LEMMA
(DLFALSE OR P) = P
dl_or_false: LEMMA
(P OR DLFALSE) = P
dl_true_implies: LEMMA
(DLTRUE IMPLIES P) = P
dl_implies_true: LEMMA
(P IMPLIES DLTRUE) = DLTRUE
dl_false_implies: LEMMA
(DLFALSE IMPLIES P) = DLTRUE
dl_implies_false: LEMMA
(P IMPLIES DLFALSE) = (NOT P)
dl_forall_bool : LEMMA
DLFORALL(LAMBDA(x:real):DLBOOL(b)) = DLBOOL(b)
dl_exists_bool : LEMMA
DLEXISTS(LAMBDA(x:real):DLBOOL(b)) = DLBOOL(b)
%------------------
%% ABREVIATIONS
%------------------
SKIP : HP = TEST(DLTRUE)
FAIL : HP = TEST(DLFALSE)
IFT(testcnd:BoolExpr,action1:HP): MACRO HP = UNION(SEQ(TEST(testcnd),action1),TEST(DLNOT(testcnd)))
IFTE(testcnd:BoolExpr,action1:HP,action2:HP): MACRO HP = UNION(SEQ(TEST(testcnd),action1),SEQ(TEST(DLNOT(testcnd)),action2))
DLRANDOM(x:real): MACRO BoolExpr = DLTRUE
DLIFF(P,Q:BoolExpr): BoolExpr =
(P IMPLIES Q) AND (Q IMPLIES P)
;IFF(P,Q:BoolExpr): MACRO BoolExpr =
DLIFF(P,Q)
%----------------------------------------------------------------------------
%% Existential quantifier over RealExpr (only for differential ghost rule)
%----------------------------------------------------------------------------
DLEXISTSR(Qe:[RealExpr->BoolExpr])(env:Environment) : bool =
EXISTS (X:RealExpr) : Qe(X)(env)
DLEXISTSRf(Qe:[RealExpr->BoolExpr],i:nat)(env:Environment) : bool =
EXISTS (X:RealExpr | fresh?(X)(i) ) : Qe(X)(env)
DLFORALLR(Qe:[RealExpr->BoolExpr])(env:Environment) : bool =
FORALL (X:RealExpr) : Qe(X)(env)
%--------------------------
%% Non-Quatified Booleans
%--------------------------
nqb_rel(rel:NQB_REL)(r1,r2:RealExpr) : BoolExpr =
CASES rel OF
nqb_eq : DLEQ(r1,r2),
nqb_neq : DLNEQ(r1,r2),
nqb_le : DLLE(r1,r2),
nqb_lt : DLLT(r1,r2),
nqb_ge : DLGE(r1,r2),
nqb_gt : DLGT(r1,r2)
ENDCASES
IMPORTING NQBool
nqb_to_be(nqbe:NQBool): RECURSIVE BoolExpr =
CASES nqbe OF
NQB_rel(rel,r1,r2) : nqb_rel(rel)(r1,r2),
NQB_AND(nqb1,nqb2) : nqb_to_be(nqb1) AND nqb_to_be(nqb2),
NQB_OR(nqb1,nqb2) : nqb_to_be(nqb1) OR nqb_to_be(nqb2),
NQB_NOT(nqb) : NOT nqb_to_be(nqb)
ENDCASES
MEASURE nqbe BY <<
% Non-quantified booleans without negations
nqb_normal?(nqbe:NQBool) : INDUCTIVE bool =
nqb_rel?(nqbe) OR
(nqb_and?(nqbe) AND nqb_normal?(nqb1(nqbe)) AND nqb_normal?(nqb2(nqbe))) OR
(nqb_or?(nqbe) AND nqb_normal?(nqb1(nqbe)) AND nqb_normal?(nqb2(nqbe)))
NormNQBool : TYPE = (nqb_normal?)
neg_rel(rel:NQB_REL) : NQB_REL =
CASES rel OF
nqb_eq : nqb_neq,
nqb_neq : nqb_eq,
nqb_le : nqb_gt,
nqb_lt : nqb_ge,
nqb_ge : nqb_lt,
nqb_gt : nqb_le
ENDCASES
normalize_rec(nqbe:NQBool,pos:bool) : RECURSIVE {nnqbe : NormNQBool |
FORALL(env:Environment):
nqb_to_be(nnqbe)(env) = (pos IFF nqb_to_be(nqbe)(env))} =
CASES nqbe OF
NQB_rel(rel,r1,r2) : IF pos THEN nqbe ELSE NQB_rel(neg_rel(rel),r1,r2) ENDIF,
NQB_AND(nqb1,nqb2) : IF pos THEN NQB_AND(normalize_rec(nqb1,true),normalize_rec(nqb2,true))
ELSE NQB_OR(normalize_rec(nqb1,false),normalize_rec(nqb2,false))
ENDIF,
NQB_OR(nqb1,nqb2) : IF pos THEN NQB_OR(normalize_rec(nqb1,true),normalize_rec(nqb2,true))
ELSE NQB_AND(normalize_rec(nqb1,false),normalize_rec(nqb2,false))
ENDIF,
NQB_NOT(nqb) : normalize_rec(nqb,NOT pos)
ENDCASES
MEASURE nqbe BY <<
normalize(nqbe:NQBool) : {nnqbe : NormNQBool | nqb_to_be(nnqbe) = nqb_to_be(nqbe)} =
normalize_rec(nqbe,true)
%% Definition of a while loop
WHILE(be:BoolExpr,hp:HP) : HP =
SEQ(STAR(SEQ(TEST(be),hp)),TEST(DLNOT(be)))
%% Definition fo any with a boolean expressions
ANY(anyvar:dLVar,be:BoolExpr) : HP =
SEQ(ANY(anyvar),TEST(be))
END bool_expr