-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex_prog_sub.py
536 lines (481 loc) · 16.3 KB
/
ex_prog_sub.py
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import random
from scipy.stats import bernoulli
from scipy.stats.morestats import WilcoxonResult
'''
Given:
[scope]: a python environment obtained by calling local()
[task]: a dictionary {"guard": -- the loop guard --,
"loopbody": -- the loop body --,
"post": -- the post-expectation given -- ,
"pre": -- the pre-expectation given }
Return:
a boolean specifying whether the loop guard specified in [task] is True in
[scope]
'''
def G(scope, task):
guard = task["guard"].replace("[", "(").replace("]", ")").replace("&", "*")
guard_bool = eval(guard, scope)
assert (type(guard_bool) == bool) or (guard_bool == 0) or (guard_bool) == 1
return bool(guard_bool)
'''
Given:
[state]: a dictionary that maps features to values
[scope], [task] are the same as in [G(scope, task)]
Return:
an updated state with additional keys ["pre"], ["post"], and ["G"] and map
them respectively to the values of preexpectation, postexpectation and the
loop guard in the [scope]
'''
def get_post_and_pre(state, scope, task):
state["post"] = eval(task["post"].replace("[", "(").replace("]", ")"), scope)
state["pre"] = eval(task["pre"].replace("[", "(").replace("]", ")"), scope)
state["G"] = G(scope, task)
return state
'''
Given:
[state], [scope], [task] are the same as in [G(scope, task)]
Return:
update [state]'s binding to keys ["pre"], ["post"], and ["G"] to the values
of preexpectation, postexpectation and the loop guard in the [scope],
then return the updated dictionary.
'''
def update_pre_post_state(scope, inpt, task):
keys = [
key
for key in inpt.keys()
if not ((key == "pre") or (key == "post") or (key == "G"))
]
post_state = {key: eval(key, scope) for key in keys}
post_state = get_post_and_pre(post_state, scope, task)
return post_state
"""
Given:
[inpt]: dictionary that maps program variables to their initial values
we use that to represent one initial states
[task]: the same as in [get_post_and_pre]
[terms]: a list of features
[NUM_runs]: an integer indicating the number of runs
[assumption]: a string indicating the assumed shape of the invariants.
Currently, the parameter is unused because we always assume
the shape to be "post + [G] * model"
Return ([G], [data])
[G]: A boolean that indicates whether the loop guard is satisfied on the
initial state [inpt]
[data]:
If [G] is false, then [data] is an empty list []
If [G] true, then [data] is [(inpt', post_states_lst, 1) # weight = 1]
[inpt'] is the dictionary [inpt] with ["pre"], ["post"], and ["G"]
associated with the preexpectation, postexpectation and the loop guard
evaluated on the [inpt] state. [post_states_lst] is the set of post
states sampled from running the loop body from [inpt]
"""
def template_for_new_benchmarks(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
# if loop guard is false, then we just return
if not G(inpt, task):
return False, inpt
# sampling
post_states_lst = []
for _ in range(NUM_runs):
# TODO 1: initialize variables to their values in inpt
# TODO 2: code the probabilistic loop in python
# TODO 3: add the postexpectation to the variable [post]
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Geo0(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
# if loop guard is false, then we just return
if not G(inpt, task):
return False, inpt
# sampling
post_states_lst = []
for _ in range(NUM_runs):
p1 = inpt["p1"]
z = inpt["z"]
flip = inpt["flip"]
# start of the loop body
d = bernoulli.rvs(size=1, p=p1)[0]
if d:
flip = 1
else:
z = z + 1
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Geo1(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
# sampling
post_states_lst = []
for _ in range(NUM_runs):
p1 = inpt["p1"]
z, x = inpt["z"], inpt["x"]
flip = inpt["flip"]
# start of the loop body
d = bernoulli.rvs(size=1, p=p1)[0]
if d:
flip = 1
else:
x = x * 2
z = z + 1
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Geo2(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
# sampling
post_states_lst = []
for _ in range(NUM_runs):
p1 = inpt["p1"]
z, x = inpt["z"], inpt["x"]
flip = inpt["flip"]
# start of the loop body
d = bernoulli.rvs(size=1, p=p1)[0]
if d:
flip = 1
else:
x = x + 1
z = z + 1
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Fair(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
# sampling
post_states_lst = []
for _ in range(NUM_runs):
p1, p2, count = inpt["p1"], inpt["p2"], inpt["count"]
c1, c2 = inpt["c1"], inpt["c2"]
# start the loop body
c1 = bernoulli.rvs(size=1, p=p1)[0]
if c1:
count = count + 1
c2 = bernoulli.rvs(size=1, p=p2)[0]
if c2:
count = count + 1
# end the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Mart(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
# sampling
post_states_lst = []
for _ in range(NUM_runs):
p, rounds, b = inpt["p"], inpt["rounds"], inpt["b"]
c = inpt["c"]
# start the loop body
d = bernoulli.rvs(size=1, p=p)
if d:
c = c + b
b = 0
else:
c = c - b
b = 2 * b
rounds += 1
# end the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Gambler0(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
z, x, y = inpt["z"], inpt["x"], inpt["y"]
# start the loop body
d = bernoulli.rvs(size=1, p=0.5)[0]
if d:
x = x + 1
else:
x = x - 1
z = z + 1
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def GeoAr0(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
p, z, x, y = inpt["p"], inpt["z"], inpt["x"], inpt["y"]
d = bernoulli.rvs(size=1, p=p)[0]
y = y + 1
if d:
z = 0
else:
x = x + y
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Bin0(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
p, n, x, y = inpt["p"], inpt["n"], inpt["x"], inpt["y"]
# start of the loop body
d = bernoulli.rvs(p, size=1)[0]
if d:
x = x + y
n = n - 1
# end of the loop body
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Bin2(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, n, y, p = inpt["x"], inpt["n"], inpt["y"], inpt["p"]
d = bernoulli.rvs(size=1, p=p)[0]
if d:
x = x + n
else:
x = x + y
n = n - 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Sum0(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, n, p = inpt["x"], inpt["n"], inpt["p"]
d = bernoulli.rvs(size=1, p=p)[0]
if d:
x = x + n
n = n - 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def DepRV(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, n, y = inpt["x"], inpt["n"], inpt["y"]
d = bernoulli.rvs(size=1, p=0.5)[0]
if d:
x = x + 1
else:
y = y + 1
n = n - 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def BiasDir(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, y, p = inpt["x"], inpt["y"], inpt["p"]
d1 = bernoulli.rvs(size=1, p=p)[0]
if d1:
x = 1
else:
x = 0
d2 = bernoulli.rvs(size=1, p=p)[0]
if d2:
y = 1
else:
y = 0
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Prinsys(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, p1, p2 = inpt["x"], inpt["p1"], inpt["p2"]
d1 = bernoulli.rvs(size=1, p=p1)[0]
if d1:
x = 0
else:
d2 = bernoulli.rvs(size=1, p=p2)[0]
if d2:
x = -1
else:
x = 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Bin1(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, n, M, p = inpt["x"], inpt["n"], inpt["M"], inpt["p"]
d = bernoulli.rvs(size=1, p=p)[0]
if d:
x = x + 1
n = n + 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Duel(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
t, c, p1, p2 = (
inpt["t"],
inpt["c"],
inpt["p1"],
inpt["p2"],
)
if t == 1:
d1 = bernoulli.rvs(size=1, p=p1)[0]
if d1:
c = 0
else:
t = 0
else:
d2 = bernoulli.rvs(size=1, p=p2)[0]
if d2:
c = 0
else:
t = 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def LinExp(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
n, z = inpt["n"], inpt["z"]
x1 = bernoulli.rvs(size=1, p=0.5)[0]
x2 = bernoulli.rvs(size=1, p=0.5)[0]
x3 = bernoulli.rvs(size=1, p=0.5)[0]
c1 = x1 + x2 + x3 # c1 = "x1 or x2 or x3"
if c1 >= 1:
c1 = 1
c2 = x1 + (1 - x2) + x3 # c2 = "not x1 or x2 or x3"
if c2 >= 1:
c2 = 1
c3 = (1 - x1) + x2 + x3 # c3 = "not x2 or x1 or x3"
if c3 >= 1:
c3 = 1
n = n - 1
z += c1 + c2 + c3
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def RevBin(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, z, p = inpt["x"], inpt["z"], inpt["p"]
d = bernoulli.rvs(size=1, p=p)[0]
if d:
x = x - 1
z = z + 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Geo0c(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
p, z, flip = inpt["p"], inpt["z"], inpt["flip"]
d = bernoulli.rvs(size=1, p=p)[0]
if d:
flip = 1
else:
z = z + 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Unif(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, count = inpt["x"], inpt["count"]
x = x + random.uniform(0, 2)
count = count + 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1
def Detm(inpt, task, NUM_runs, assumption):
inpt = get_post_and_pre(inpt, inpt, task)
if not G(inpt, task):
return False, inpt
post_states_lst = []
# sampling
for _ in range(NUM_runs):
x, count = inpt["x"], inpt["count"]
x = x + 1
count = count + 1
scope = locals()
post_state = update_pre_post_state(scope, inpt, task)
post_states_lst.append(post_state)
return True, (inpt, post_states_lst, 1) # weight = 1