-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproofs.py
1581 lines (1299 loc) · 64.9 KB
/
proofs.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Classes and definitions used for constructing structured inductive invariant proof objects.
#
import random
import pickle
import graphviz
import logging
import json
import dot2tex
import subprocess
import sys
import multiprocessing
import time
import io
import contextlib
from mc import CTI
import mc
import tlaps
log = logging.getLogger("dot2tex")
log.setLevel(logging.WARNING)
def find_ind_containing(l,substr):
for ind in range(len(l)):
if substr in l[ind]:
return ind
return -1
# Count spec and proof LOC.
def count_spec_loc(specfile):
lines = open(specfile).read().splitlines()
# Remove commented lines.
lines = [l for l in lines if (not l.startswith("\*") or "START_PROOF" in l)]
spec_loc = find_ind_containing(lines, "START_PROOF")
proof_loc = len(lines) - spec_loc
return {"spec_loc": spec_loc, "proof_loc": proof_loc}
def runcmd(c):
print("RUNNING CMD:", c)
cmd = c[0]
expr = c[1]
start = time.time()
sys.stdout.flush()
proc = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, cwd="benchmarks")
exitcode = proc.wait()
duration = int(time.time() - start)
print("EXIT CODE:", exitcode)
return (expr, exitcode, duration)
def mean(S):
return sum(S) / len(S)
def median(S):
if len(S) == 0:
return -1
S.sort()
return S[len(S)//2]
class StructuredProofNode():
""" Single node (i.e. lemma) of a structured proof tree. """
def __init__(self, name="", expr="", children=None, parent=None, load_from_obj = None):
# Name used to identify the expression.
self.name = name
# Top level goal expression to be proven.
self.expr = expr
self.children = dict() if children is None else children
self.cti_view = None
# Allow nodes to optionally override TypeOK for potentially faster local CTI generation.
self.ctigen_typeok = None
# Set of variables to project to (i.e keep only these) for CTI generation and analysis.
self.cti_project_vars = None
# Each proof node/lemma can maintain a current set of CTIs, which are
# computed based on whether the lemma is inductive relative to its
# current set of direct children.
self.ctis = {}
# Flag telling whether CTIs were attempted to be generated yet
# for this proof node. Initially set to Flas,e it will go True
# once a call to set some CTIs on this node occur.
self.had_ctis_generated = False
# Set to true if we completed a full proof of this node's inductiveness
# relative to its supporting lemmas using Apalache model checker.
self.apalache_proof_check = False
# Set of CTIs eliminated by set of this node's direct children.
# self.ctis_eliminated = []
self.ctis_eliminated = {}
# CTIs eliminated uniquely by each child, per action.
self.ctis_eliminated_uniquely = {}
# Pointer to this node's parent, or None if it has no parent.
self.parent = parent
# Set of CTIs of parent that this lemma eliminates.
self.parent_ctis_eliminated = []
# Set of CTIs of parent that are eliminated by this predicate alone and
# none of its siblings.
self.parent_ctis_uniquely_eliminated = []
if load_from_obj:
self.load_from(load_from_obj)
def num_children(self):
cnt = 0
for a in self.children:
for c in self.children[a]:
cnt += 1
return cnt
def serialize_rec(self, include_ctis=True, cti_hashes_only=False, seen=set(), serialize_children=True):
# if self.expr in seen:
# None
# print(seen)
# Deal with cycles in the proof graph by tracking visited nodes.
seen.add(self.expr)
# If we have already visited this node in the proof graph, then retain it as a child,
# but don't recursively serialize its children.
def serialize_child(c):
if c.expr in seen:
return c.serialize_rec(include_ctis=include_ctis, cti_hashes_only=cti_hashes_only, seen=set(seen), serialize_children=False)
else:
return c.serialize_rec(include_ctis=include_ctis, cti_hashes_only=cti_hashes_only, seen=set(seen))
children_serialized = {}
if serialize_children:
children_serialized = {
a:[serialize_child(c) for c in self.children[a]]
for a in self.children
}
ret = {
"name": self.name,
"expr": self.expr,
"apalache_proof_check": self.apalache_proof_check,
"children": children_serialized,
"project_vars": self.cti_project_vars
}
cti_sort_key = lambda c : c.cost
remaining_ctis_cost_sorted = sorted(self.get_remaining_ctis(), key = cti_sort_key)
if cti_hashes_only:
ctis_serialized = {a:[str(hash(c)) for c in self.ctis[a]] for a in self.ctis}
else:
ctis_serialized = {a:[c.serialize() for c in self.ctis[a]] for a in self.ctis}
cti_info = {
"ctis": ctis_serialized,
"ctis_eliminated": self.ctis_eliminated, # eliminated CTIs are stored as CTI hashes, not full CTIs.
"ctis_eliminated_uniquely": {a:{c:list(v[c]) for c in v} for a,v in self.ctis_eliminated_uniquely.items()}, # eliminated CTIs are stored as CTI hashes, not full CTIs.
"ctis_remaining": {a:[str(hash(cti)) for cti in self.get_remaining_ctis(a)] for a in self.ctis},
"num_parent_ctis_eliminated": len(self.parent_ctis_eliminated),
# "parent_ctis_eliminated": [c for c in self.parent_ctis_eliminated],
"had_ctis_generated": self.had_ctis_generated
}
# print(type(cti_info["ctis"]))
if include_ctis:
for k in cti_info:
ret[k] = cti_info[k]
return ret
def serialize(self, include_ctis=True, cti_hashes_only=False, serialize_children=True):
seen_nodes = set()
return self.serialize_rec(include_ctis=include_ctis, cti_hashes_only=cti_hashes_only, serialize_children=serialize_children, seen=seen_nodes)
def load_from(self, obj):
""" Load serialized proof object into the current one after deserializing. """
self.name = obj["name"]
self.expr = obj["expr"]
self.apalache_proof_check = obj.get("apalache_proof_check", False)
self.ctis = [CTI(load_from_obj=c) for c in obj["ctis"]]
# self.ctis_eliminated = [c for c in obj["ctis_eliminated"]]
self.ctis_eliminated = obj["ctis_eliminated"]
self.parent_ctis_eliminated = obj.get("parent_ctis_eliminated", [])
self.children = [StructuredProofNode(load_from_obj=c, parent=self) for c in obj["children"]]
# <span style='color:{color}'>
# {self.expr} ({len(self.ctis)-len(self.ctis_eliminated)} CTIs remaining, eliminates {len(self.parent_ctis_eliminated)} parent CTIs)
# </span>
def list_elem_html(self):
all_ctis_eliminated = len(self.ctis) == len(self.ctis_eliminated)
color = "darkred" if len(self.ctis) > len(self.ctis_eliminated) else "green"
if not self.had_ctis_generated:
color = "goldenrod"
# if all_ctis_eliminated and self.apalache_proof_check:
# color = "blue"
if self.parent is not None and len(self.parent.ctis) > 0:
pct_parent_ctis_eliminated = float(len(self.parent_ctis_eliminated)) / len(self.parent.ctis)
else:
pct_parent_ctis_eliminated = 0.0
parent_info_text = ""
if self.parent is not None:
parent_info_text = f"(eliminates {len(self.parent_ctis_eliminated)} ({int(pct_parent_ctis_eliminated * 100.0)}% of) parent CTIs, {len(self.parent_ctis_uniquely_eliminated)} uniquely)"
local_rand = random.Random()
local_rand.seed(13)
cti_elim_viz = ""
sample_size = 50
if self.parent and len(self.parent.ctis) >= sample_size:
elim_cti_sample = local_rand.sample(self.parent.ctis, sample_size)
cti_elim_viz = "".join(["x" if str(hash(c)) in self.parent_ctis_eliminated else "-" for c in elim_cti_sample])
proof_check_badge = "✔" if self.apalache_proof_check else "<span style='color:darkred'>✗</span>"
# <td style='color:{color}'> FP:{proof_check_badge} </td>
return f"""
<table class='proof-struct-table'>
<tr>
<td style='color:{color}' class='proof-node-expr'>{self.expr}</td>
<td style='color:{color}' class='ctis-remaining-count'>({len(self.ctis)-len(self.ctis_eliminated)}/{len(self.ctis)} CTIs remaining) (Apalache proof? {proof_check_badge})</td>
<td class='proof-parent-cti-info'> {parent_info_text} </td>
<td class='gen-ctis-button' onclick='genCtis("{self.name}")'> Gen CTIs </td>
<td class='gen-ctis-button' onclick='genCtisSubtree("{self.name}")'> Gen CTIs (subtree) </td>
</tr>
</table>
"""
# CTI elim viz row.
# <td class='proof-cti-grid-row'>{cti_elim_viz}</td>
def to_html(self):
child_elems = "\n".join([f"<span>{c.to_html()}</span>" for c in self.children_list()])
return f"""
<li>{self.list_elem_html()}
<ul>{child_elems}</ul>
</li>
"""
def children_list(self):
if type(self.children) == dict:
out = sum(list(self.children.values()), [])
return out
return self.children
def set_ctis(self, ctis, action):
""" Set CTIs for this node and mark it as having CTIs generated. """
self.had_ctis_generated = True
self.ctis[action] = ctis
def get_ctis(self, action):
return self.ctis[action]
def reset_ctis(self):
""" Set CTIs for this node and mark it as having CTIs generated. """
logging.info(f"Resetting CTIs for node: {self.name}")
self.had_ctis_generated = False
self.ctis = {}
self.ctis_eliminated = {}
self.apalache_proof_check = False
# self.parent_ctis_eliminated = []
def get_remaining_ctis(self, action=None):
if action is None:
return []
return [c for c in self.ctis[action] if str(hash(c)) not in self.ctis_eliminated[action]]
def sample_remaining_ctis(self, max_num_ctis):
remaining_ctis = self.get_remaining_ctis()
num_ctis = min(len(remaining_ctis), max_num_ctis)
return random.sample(remaining_ctis, num_ctis)
def num_ctis_remaining(self):
return len(self.ctis) - len(self.ctis_eliminated)
def get_cti_clusters(self, serialize=False):
""" Cluster CTIs according to various metrics."""
# 1. Cluster by action name.
actions = set()
cti_clusters = {}
for c in self.ctis:
actions.add(c.action_name)
if c.action_name not in cti_clusters:
cti_clusters[c.action_name] = []
cti_clusters[c.action_name].append(c)
if serialize:
return {a:[str(hash(c)) for c in cti_clusters[a]] for a in cti_clusters}
return cti_clusters
def to_apalache_inductive_proof_obligation(self, modname, action=None):
""" Export this node and support lemmas as base for Apalache checking."""
# "THEOREM IndAuto /\ Next => IndAuto'"
metadir = f"apa_indcheck/{modname}"
out_str = "\n"
action_suffix = "" if action is None else action + "_"
def_name = f"{self.expr}_{action_suffix}IndCheck"
next_expr = "Next" if action is None else action
outdir = f"{metadir}/{self.expr}"
if action is not None:
outdir += "_" + action
smtprof = ""
# smtprof = "--smtprof" # can enable if you want.
apa_cmd = f"""JVM_ARGS="-Xss16m" ./apalache/bin/apalache-mc check --init={def_name} --next={next_expr} --inv={self.expr} --cinit=CInit --tuning-options='search.invariantFilter=1->.*' --no-deadlock --length=1 {smtprof} --debug --out-dir={outdir} --run-dir={outdir} {modname}.tla"""
out_str += f"(** \nApalache command:\n{apa_cmd}\n **)\n"
out_str += f"{def_name} == \n"
typeok = "ApaTypeOK"
land = "/\\"
out_str += f" {land} {typeok}\n"
for a in self.children:
supports = [c.expr for c in self.children[a]]
supports_conj = land.join(supports)
supports_list = ",".join(supports)
if action is not None and a != action:
continue
out_str += f" \* Action support lemmas: {a}\n"
for s in supports:
out_str += f" {land} {s}\n"
out_str += f" \* Target lemma.\n"
out_str += f" {land} {self.expr}\n"
return {
"out_str": out_str,
"cmd": apa_cmd
}
def to_tlaps_proof_obligation(self, actions, action_def_expands, lemma_def_expands, global_def_expands, assumes_list, theorem_name=None):
""" Export this node and support lemmas as TLAPS proof obligation skeleton."""
# "THEOREM IndAuto /\ Next => IndAuto'"
typeok = "TypeOK"
land = " /\\ "
oblig_source_map = {}
# Collect all support lemmas across all actions.
all_support_lemmas = []
for (ind,a) in enumerate(actions):
if a in self.children:
all_support_lemmas += [c.expr for c in self.children[a]]
out_str = ""
top_level_goal_anteced = [typeok] + all_support_lemmas + [self.expr]
top_level_goal_anteced_str = land.join(top_level_goal_anteced)
theorem_name_str = ""
if theorem_name is not None:
theorem_name_str = f"{theorem_name} =="
# Make the top level goal as proving this target lemma inductive, using all support lemmas
# concatenated from all actions.
out_str += f"THEOREM {theorem_name_str} {top_level_goal_anteced_str} /\\ Next => {self.expr}'\n"
if len(assumes_list) > 0:
assumes_str = ",".join(assumes_list)
out_str += f""" <1>. USE {assumes_str}\n"""
# Export obligations for all actions.
for (ind,a) in enumerate(actions):
support_set = []
# For actions without any children the support set will be empty.
if a in self.children:
support_set = [c.expr for c in self.children[a]]
supports_list = [typeok] + support_set + [self.expr]
supports_conj_str = land.join(supports_list)
defs_list = [typeok] + support_set + [a, a.replace('Action', ''), self.expr]
# If action is listed in custom proof def expands list, add those definitions here.
defs_list += global_def_expands
if a in action_def_expands:
defs_list += action_def_expands[a]
if self.expr in lemma_def_expands:
defs_list += lemma_def_expands[self.expr]
out_str += f" \* ({self.expr},{a})\n"
oblig_source_map[(self.expr, a)] = len(out_str.split("\n")) - 1
out_str += f""" <1>{ind+1}. {supports_conj_str}{land}{a} => {self.expr}'"""
out_str += f" BY DEF {','.join(defs_list)}\n"
out_str += ""
out_str += f"<1>{len(actions)+1}. " + "QED BY " + (",".join([f"<1>{ind}" for ind in range(1, len(actions)+1)])) + " DEF Next"
out_str += "\n"
return {
"out_str": out_str,
"source_map": oblig_source_map
}
class StructuredProof():
""" Structured safety proof of an inductive invariant.
May also represent a "partial" proof i.e. one in an incomplete state that is yet to be completed.
"""
def __init__(self, root=None, load_from_obj=None, specname=None, actions=None, spec_defs=None, nodes=None, safety=""):
# Top level goal expression to be proven.
self.safety_goal = safety
self.root = root
self.nodes = nodes
self.specname = specname
self.proof_base_filename = f"benchmarks/{self.specname}.proof"
self.actions = actions
self.spec_defs = spec_defs
self.vars_in_action = dict()
self.ctigen_state = "idle"
self.current_config_instance_index = -1
self.save_tex = True
if load_from_obj:
self.load_from(load_from_obj)
def walk_proof_graph(self, curr_node, visit_fn=None, seen = set(), all_nodes=[]):
if visit_fn is not None:
visit_fn(curr_node)
seen.add(curr_node.expr)
all_nodes.append(curr_node)
for a in curr_node.children:
for c in curr_node.children[a]:
# print(c)
if c is not None and c.expr not in seen:
self.walk_proof_graph(c, visit_fn, seen, all_nodes=all_nodes)
def to_tlaps_proof_skeleton(self, tlaps_proof_config, add_lemma_defs=None, seed=None, tag=None, workdir="benchmarks", include_typeok=True):
""" Export proof graph obligations to TLAPS proof structure."""
seed_str = ""
if seed is not None:
seed_str = f"_{seed}"
tag_str = ""
if tag is not None:
tag_str = f"_{tag}"
modname = self.specname + f"_IndDecompProof{seed_str}{tag_str}"
fname = f"{workdir}/" + modname + ".tla"
f = open(fname, 'w')
spec_lines = f"---- MODULE {modname} ----\n"
spec_lines += f"EXTENDS {self.specname},TLAPS\n"
# Maintain a source map which records lines that proof obligations live on.
lemma_source_map = {}
nodes = []
seen = set()
self.walk_proof_graph(self.root, seen=seen, all_nodes=nodes)
# print(nodes)
# Some proof graph info.
spec_lines += "\n"
spec_lines += f"\* Proof Graph Stats\n"
spec_lines += f"\* ==================\n"
spec_lines += f"\* seed: {seed}\n"
spec_lines += f"\* num proof graph nodes: {len(nodes)}\n"
spec_lines += f"\* num proof obligations: {len(nodes) * len(self.actions)}\n"
in_degrees = list(map(lambda n : n.num_children(), nodes))
mean_in_degree = sum(in_degrees)/len(nodes)
sorted_in_degrees = list(sorted(in_degrees))
median_in_degree = sorted_in_degrees[len(sorted_in_degrees)//2]
assumes_name_list = []
if add_lemma_defs is not None:
for (name,expr) in add_lemma_defs:
spec_lines += f"{name} == {expr}\n"
spec_lines += "\nIndGlobal == \n"
spec_lines += f" /\\ TypeOK\n"
for n in nodes:
spec_lines += f" /\\ {n.expr}\n"
assume_spec_lines = ""
if "assumes" in tlaps_proof_config:
for ind,assume in enumerate(tlaps_proof_config["assumes"]):
name = f"A{ind}"
assume_spec_lines += f"ASSUME {name} == {assume}\n"
assumes_name_list.append(name)
# Create a separate node for TypeOK to include as proof obligation.
if include_typeok:
typeok_node = StructuredProofNode("H_TypeOK", "TypeOK")
nodes = [typeok_node] + nodes
all_var_slices = []
proof_obligation_lines = ""
global_def_expands = []
if "global_def_expands" in tlaps_proof_config:
global_def_expands = tlaps_proof_config["global_def_expands"]
for ind,n in enumerate(nodes):
# if len(n.children.keys()) == 0:
# continue
# for a in n.children:
# if a in self.lemma_action_coi:
# slicevars = self.lemma_action_coi[a][n.expr]
# all_var_slices.append(slicevars)
if n.expr == self.root.expr:
proof_obligation_lines += "\n\* (ROOT SAFETY PROP)"
proof_obligation_lines += f"\n\*** {n.expr}\n"
curr_line = len(proof_obligation_lines.split("\n")) - 1
ret = n.to_tlaps_proof_obligation(self.actions,
tlaps_proof_config["action_def_expands"],
tlaps_proof_config["lemma_def_expands"],
global_def_expands,
assumes_name_list,
theorem_name=f"L_{ind}")
proof_obligation_lines += ret["out_str"]
# print(ret["source_map"])
for obl in ret["source_map"]:
lemma_source_map[ret["source_map"][obl] + curr_line] = obl
proof_obligation_lines += "\n"
var_slice_sizes = [len(s) for s in all_var_slices]
if len(var_slice_sizes):
mean_slice_size = mean(var_slice_sizes)
else:
mean_slice_size = 0
# Add lines to the spec.
spec_lines += "\n\n"
spec_lines += f"\* mean in-degree: {mean_in_degree}\n"
spec_lines += f"\* median in-degree: {median_in_degree}\n"
spec_lines += f"\* max in-degree: {max(in_degrees)}\n"
spec_lines += f"\* min in-degree: {min(in_degrees)}\n"
spec_lines += f"\* mean variable slice size: {mean_slice_size}\n"
spec_lines += "\n"
# Add assumes.
spec_lines += assume_spec_lines
# Re-adjust source map.
curr_line = len(spec_lines.split("\n"))
lemma_source_map_new = {}
for ln in lemma_source_map:
lemma_source_map_new[ln + curr_line] = lemma_source_map[ln]
lemma_source_map = lemma_source_map_new
# Add proof obligation lines.
spec_lines += proof_obligation_lines
#
# For a sanity check, add as a top level theorem the property that the conjunction of lemma nodes
# is inductive.
#
# Initiation.
spec_lines += "\* Initiation."
spec_lines += "\nTHEOREM Init => IndGlobal\n"
init_defs = ",".join([f"{n.expr}" for ind,n in enumerate(nodes)])
if len(assumes_name_list) > 0:
spec_lines += " <1> USE " + ",".join(assumes_name_list) + "\n"
if len(global_def_expands) > 0:
spec_lines += "<1> USE DEF " + ",".join(global_def_expands) + "\n"
for ind,n in enumerate(nodes):
# Decomposed proof obligation for each oncjunction.
spec_lines += f" <1>{ind}. Init => {n.expr} BY DEF Init, {n.expr}, IndGlobal\n"
spec_lines += " <1>a. QED BY " + ",".join([f"<1>{ind}" for ind in range(len(nodes))]) + " DEF IndGlobal\n"
spec_lines += "\n"
# Consecution.
spec_lines += "\* Consecution.\n"
spec_lines += "THEOREM IndGlobal /\\ Next => IndGlobal'\n"
spec_lines += " BY " + ",".join([f"L_{ind}" for ind,n in enumerate(nodes)]) + " DEF Next, IndGlobal\n"
spec_lines += "\n"
spec_lines += "===="
logging.info(f"Saving proof graph TLAPS obligations to file '{fname}'.")
f.write(spec_lines)
f.close()
return {
"tlaps_filename": fname,
"lemma_source_map": lemma_source_map
}
def apalache_check_all_nodes(self, save_dot=False, lemma_filter=None):
# Save Apalache proof obligations to own spec file.
self.to_apalache_proof_obligations()
# Check all the obligations.
nodes = []
seen = set()
self.walk_proof_graph(self.root, seen=seen, all_nodes=nodes)
modname = self.specname + "_ApaIndProofCheck"
cmds = []
node_exprs = []
metadir = f"apa_indcheck/{modname}"
clean_cmd = f"rm -rf {metadir}"
proc = subprocess.Popen(clean_cmd, shell=True, stderr=subprocess.PIPE, cwd="benchmarks")
exitcode = proc.wait()
# nodes = nodes[:6]
# Just the 3 nodes that we expect to have breakages in AsyncRaft.
if lemma_filter is not None:
# filtered = [
# "H_NoLogDivergence",
# "H_CommitIndexCoveredOnQuorum",
# "H_CommitIndexInAppendEntriesImpliesCommittedEntryExists"
# ]
nodes = [n for n in nodes if n.expr in lemma_filter]
do_per_action_checks = True
print(f"All {len(nodes)} lemmas to check:")
for n in nodes:
print(f"- `{n.expr}`")
# Gather all proof checking commands to run.
for n in nodes:
obl = n.to_apalache_inductive_proof_obligation(modname)
cmd = obl["cmd"]
if do_per_action_checks:
for a in self.actions:
obl = n.to_apalache_inductive_proof_obligation(modname, action=a)
cmd = obl["cmd"]
cmds.append(cmd)
node_exprs.append((n.expr, a))
else:
# proc = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, cwd="benchmarks")
# out = proc.stdout.read().decode(sys.stdout.encoding)
# exitcode = proc.wait()
# print("EXIT CODE:", exitcode)
# if exitcode != 0:
# raise Exception(f"Apalache proof check failed for node '{n.expr}'. Command: " + cmd)
cmds.append(cmd)
node_exprs.append(n.expr)
#
# Submit all commands to a multiprocessing pool to run in parallel.
#
num_threads = 4
cmds_to_run = list(zip(cmds, node_exprs))
# print("CMDS TO RUN:", cmds_to_run)
pool = multiprocessing.Pool(processes=num_threads)
results = pool.map(runcmd, cmds_to_run)
pool.close()
pool.join()
# Optionally save graph with proof status map.
status_map = {r[0]:r[1] for r in results}
# status_map = {}
# print("RESULTS:", results)
# If there are any obligations that failed, then now go back and check these per action.
# print("---- Checking to see if any obligations failed.")
# sys.stdout.flush()
# for ind,r in enumerate(results):
# # Error was found.
# if r[1] != 0:
# for a in self.actions:
# obl = nodes[ind].to_apalache_inductive_proof_obligation(modname, action=a)
# cmd = obl["cmd"]
# # Just run the command now.
# start = time.time()
# proc = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, cwd="benchmarks")
# exitcode = proc.wait()
# duration = int(time.time() - start)
# print("EXIT CODE:", exitcode)
# res = ((r[0],a), exitcode, duration)
# status_map[(r[0], a)] = res[1]
# sys.stdout.flush()
# Otherwise, just save the results on a per action basis.
# else:
# for a in self.actions:
# status_map[(r[0], a)] = r[1]
# print("- Proof status Map")
# for s in status_map:
# print(s, status_map[s])
if save_dot:
self.save_as_dot(f"benchmarks/{self.specname}_proof_with_status.dot", omit_labels=True, proof_status_map=status_map)
print(f"--- Proof checking RESULTS ({len(cmds)} total obligations checked, for {len(nodes)} lemmas):")
error_found = False
for r in results:
if r[1] != 0:
print(r, "ERROR")
error_found = True
else:
print(r)
if error_found:
print("*** Some ERRORs found:")
for r in results:
if r[1] != 0:
print(r, "ERROR")
error_found = True
else:
print(f"No errors found in proof checking! ({len(cmds)} obligations checked, for {len(nodes)} lemmas).")
def to_apalache_proof_obligations(self):
""" Export proof graph obligations to TLAPS proof structure."""
modname = self.specname + "_ApaIndProofCheck"
f = open("benchmarks/" + modname + ".tla", 'w')
spec_lines = f"---- MODULE {modname} ----\n"
spec_lines += f"EXTENDS {self.specname}\n"
nodes = []
seen = set()
self.walk_proof_graph(self.root, seen=seen, all_nodes=nodes)
# print(nodes)
stats = {}
# Some proof graph info.
spec_lines += "\n"
spec_lines += f"\* Proof Graph Stats\n"
spec_lines += f"\* ==================\n"
spec_lines += f"\* num proof graph nodes: {len(nodes)}\n"
in_degrees = list(map(lambda n : n.num_children(), nodes))
mean_in_degree = sum(in_degrees)/len(nodes)
median_in_degree = median(in_degrees)
sorted_in_degrees = list(sorted(in_degrees))
median_in_degree = sorted_in_degrees[len(sorted_in_degrees)//2]
spec_lines += f"\* mean in-degree: {mean_in_degree}\n"
spec_lines += f"\* median in-degree: {median_in_degree}\n"
spec_lines += f"\* max in-degree: {max(in_degrees)}\n"
spec_lines += f"\* min in-degree: {min(in_degrees)}\n"
all_var_slices = []
apa_cmds = []
for n in nodes:
# if len(n.children.keys()) == 0:
# continue
# if n.expr == self.root.expr:
# spec_lines += "\n\* (ROOT SAFETY PROP)"
# spec_lines += f"\n\* -- {n.expr}\n"
obl = n.to_apalache_inductive_proof_obligation(modname)
spec_lines += obl["out_str"]
# Also add a separate obligation for each action.
for a in self.actions:
obl = n.to_apalache_inductive_proof_obligation(modname, action=a)
spec_lines += obl["out_str"]
apa_cmds.append(obl["cmd"])
spec_lines += "\n"
for a in n.children:
if a in self.lemma_action_coi:
slicevars = self.lemma_action_coi[a][n.expr]
all_var_slices.append(list(slicevars))
spec_lines += "\n"
spec_lines += "===="
f.write(spec_lines)
f.close()
metadir = f"apa_indcheck/{modname}"
f = open(f"benchmarks/apacheck_{self.specname}.sh", 'w')
f.write("#!/bin/bash\n\n")
f.write(f"#\n# Inductive proof obligations for '{self.specname}'\n")
f.write(f"# Total num proof obligations: {len(nodes)}\n#\n\n")
f.write(f"rm -rf {metadir}\n")
for cmd in apa_cmds:
f.write(cmd)
f.write("\n")
f.close()
# Dump stats JSON file.
stats["mean_in_degree"] = mean_in_degree
stats["median_in_degree"] = median_in_degree
stats["all_var_slices"] = all_var_slices
stats["num_lemmas"] = len(nodes)
stats["num_actions"] = len(self.lemma_action_coi.keys())
# TODO: Could compute this more simply.
all_vars = set()
for x in all_var_slices:
all_vars.update(set(x))
stats["num_state_vars"] = len(all_vars)
stats["median_slice_size"] = median([len(s) for s in all_var_slices])
if len(all_vars) == 0:
stats["median_slice_pct"] = 0
else:
stats["median_slice_pct"] = stats["median_slice_size"] / len(all_vars)
f = open(f"benchmarks/{self.specname}_proofstats.json", 'w')
json.dump(stats, f, indent=2)
f.close()
# Also get some general spec stats for LaTeX stats.
spec_path = f"benchmarks/{self.specname}.tla"
res = count_spec_loc(spec_path)
stats["spec_loc"] = res["spec_loc"]
stats["proof_loc"] = res["proof_loc"]
# Save proof graph stats in LaTeX format too.
f = open(f"benchmarks/{self.specname}_proofstats.tex", 'w')
tex_stats = [
"num_state_vars", "mean_in_degree", "median_slice_size",
"median_in_degree", "num_lemmas", "num_actions", "spec_loc",
"proof_loc"
]
for stat in tex_stats:
f.write("\\newcommand*{\\%s%s}{%d}\n" % (self.specname.replace("_", ""), stat.replace("_", ""), stats[stat]))
stat = "median_slice_pct"
f.write("\\newcommand*{\\%s%s}{%.2f}\n" % (self.specname.replace("_", ""), stat.replace("_", ""), stats[stat]))
f.close()
def serialize(self, include_ctis=True, cti_hashes_only=False):
return {
"safety": self.safety_goal,
"root": self.root.serialize(include_ctis=include_ctis, cti_hashes_only=cti_hashes_only),
"nodes": [n.serialize(serialize_children=True, cti_hashes_only=cti_hashes_only) for n in self.nodes],
"spec_defs": self.spec_defs,
"vars_in_action": {a:list(v) for a,v in self.vars_in_action.items()},
"vars_in_lemma_defs": {a:list(v) for a,v in self.vars_in_lemma_defs.items()},
"lemma_action_coi": {a:{l:list(self.lemma_action_coi[a][l]) for l in self.lemma_action_coi[a]} for a in self.lemma_action_coi}
}
def save_proof(self, include_json=False, include_dot=False):
""" Visualize proof structure in HTML format for inspection, and serialize to JSON. """
filename = f"{self.proof_base_filename}.html"
pickle_filename = f"{self.proof_base_filename}.pickle"
dot_filename = f"{self.proof_base_filename}.dot"
print(f"Saving latest proof HTML to '{filename}'")
with open(filename, 'w') as f:
html = self.gen_html(self, self.specname)
f.write(html)
# Save structured proof object.
if include_json:
json_filename = f"{self.proof_base_filename}.json"
print(f"Saving latest proof JSON to '{json_filename}'")
with open(json_filename, 'w') as f:
proof_json = self.serialize()
json.dump(proof_json, f, indent=2)
print(f"Saving latest proof pickled to '{pickle_filename}'")
# Save pickled proof object.
with open(pickle_filename, 'wb') as f:
pickle.dump(self, f)
if include_dot:
print(f"Saving latest proof as DOT to '{dot_filename}'")
self.save_as_dot(dot_filename)
self.save_as_dot(dot_filename, omit_labels=True)
print(f"Finished saving proof objects.")
def load_from(self, obj):
self.safety_goal = obj["safety"]
self.root = StructuredProofNode(load_from_obj=obj["root"])
def root_to_html(self):
html = "<ul>"+self.root.to_html()+"</ul>"
return html
def node_cti_html(self, node):
html = ""
max_ctis_to_show = 30
cti_sort_key = lambda c : c.cost
# remaining_ctis_sampled = node.sample_remaining_ctis(max_ctis_to_show, sort_by = cti_sort_key)
# Sort remaining CTIs by cost.
remaining_ctis = sorted(node.get_remaining_ctis(), key = cti_sort_key)
remaining_ctis_sampled = remaining_ctis[:max_ctis_to_show]
# remaining_ctis_sampled = node.sample_remaining_ctis(max_ctis_to_show, sort_by = cti_sort_key)
# Aggregate statistics.
all_remaining_ctis = node.get_remaining_ctis()
cti_action_names = [cti.getActionName() for cti in all_remaining_ctis]
action_names_set = set(cti_action_names)
action_counts = {a:cti_action_names.count(a) for a in action_names_set}
html += f"<div class='cti-container cti_{node.expr}'>"
action_name_info = ','.join([a + f"({action_counts[a]})" for a in list(action_names_set)])
html += f"<div>Actions present in CTIs: [{action_name_info}]</div>"
for i,one_cti in enumerate(remaining_ctis_sampled):
html += f"<div class='cti-box'>"
html += (f"<h3>CTI {i} for {node.expr} (Action: {one_cti.getActionName()}, cost={one_cti.cost})</h3>\n")
html += ("<pre>")
for k,s in enumerate(one_cti.getTrace().getStates()):
html += (f"<b>CTI State {k}</b> \n")
html += (s.pretty_str())
html += ("\n")
html += ("</pre>")
html += "</div>"
html += "</div>"
# for c in node.children_list():
# html += self.node_cti_html(c)
return html
def cti_viewer_html(self):
return self.node_cti_html(self.root)
def dump(self):
out_file = open("proof.proof", 'w')
json.dump(self.serialize(), out_file, indent=2)
def gen_html(self, indgen, specname):
""" Generate HTML visualization of structured proof. """
html = ""
html += ("<head>")
html += ('<link rel="stylesheet" type="text/css" href="proof.css">')
html += '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">'
# html += """<script src="{{ url_for('static',filename='proof.js') }}"> </script>"""
# html += ("""<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='proof.css') }}">""")
html += '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>'
html += """
<!-- CytoscapeJS (for graph visualizations) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.20.0/cytoscape.min.js" integrity="sha512-cjmYAonfXK+azDmWqvnqq8xmygHRHqVI7S0zuRxQnvcYVeoakwthRX6pPKoXfG1oIjDvMUtteRV9PhQjJwKWxQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/[email protected]/dist/dagre.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/cytoscape-dagre.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/underscore-umd-min.js"></script>
"""
html += ('<script type="text/javascript" src="proof.js"></script>')
html += ("</head>")
# html += ("<div>")
html += ("<div hidden>")
html += (f"<h1>Proof Structure: {specname}</h1>")
# html += (f"<div>seed={self.indgen.seed}, num_simulate_traces={self.indgen.num_simulate_traces}</div>")
# html += (self.root_to_html())
html += ("</div>")
html += ("<div>")
# html += (f"<h2>Sample of {self.root.num_ctis_remaining()} Remaining CTIs for {self.root.expr}</h2>")
# html += (f"<h2>Sample of remaining CTIs</h2>")
html += (f"<h2 hidden>Sample of remaining CTIs</h2>")
html += self.cti_viewer_html()
# for i,one_cti in enumerate(remaining_ctis):
# html += "<div class='cti-box'>"
# html += (f"<h3>CTI {i}</h3>\n")
# html += ("<pre>")
# for i,s in enumerate(one_cti.getTrace().getStates()):
# html += (f"<b>CTI State {i}</b> \n")
# html += (s.pretty_str())
# html += ("\n")
# html += ("</pre>")
# html += "</div>"
html += ("</div>")
return html
def add_node_to_dot_graph(self, dot, node, seen=set(), omit_labels=False, proof_status_map=None):
""" Add this node and its children, recursively, to DOT graph."""
color = "black"
penwidth="2"
if node.expr == self.safety_goal:
color="black"
penwidth="5"
style="filled"
else:
style="rounded corners"
if node.expr in seen:
return
# print("NODEEXP:", node.expr)
nodes_to_include_var_slice = [
("H_LeaderMatchIndexValid", "ClientRequestAction"),
("H_LeaderMatchIndexBound", "HandleAppendEntriesResponseAction"),
("H_QuorumsSafeAtTerms", "BecomeLeaderAction")
]
lemmas_to_always_show = [
# AsyncRaft key lemmas.
"H_LeaderMatchIndexValid",
"H_LeaderMatchIndexBound",
"H_LeaderMatchIndexValidAppendEntries",
self.safety_goal,
"H_LogMatching",
"H_LogMatchingAppendEntries",
"H_OnePrimaryPerTerm",
"H_PrimaryHasEntriesItCreated",
"H_QuorumsSafeAtTerms",
# "H_CommitIndexInAppendEntriesImpliesCommittedEntryExists",
"H_CommitIndexCoveredOnQuorum",
# Zab lemmas.
"H_PrefixConsistency",
"H_CommittedEntryExistsInLeaderHistory",
"H_CommittedEntryExistsOnQuorum",
"H_UniqueLeadership",
"H_UniqueEstablishedLeader",
"H_TxnZxidsUniqueHistoriesAndMessages",
"H_TwoLeadersCantHaveSameCEPOCH",
# TwoPhase lemmas.