File: consistent-state-properties.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (1826 lines) | stat: -rw-r--r-- 64,765 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
(in-package "DJVM")
(include-book "../DJVM/consistent-state")
(include-book "../DJVM/djvm-frame-manipulation-primitives")

(acl2::set-match-free-default :all)

(defthm consistent-thread-entry-implies-consistent-opstack
  (implies (consistent-thread-entry th cl hp)
           (consistent-opstack 
            (operand-stack (top (thread-call-stack th))) cl hp))
  :rule-classes :forward-chaining)


(defthm consistent-opstack-consistent-value-x
  (implies (and (consistent-opstack opstack cl hp)
                (canPopCategory1 opstack))
           (consistent-value-x (top opstack) cl hp))
  :rule-classes :forward-chaining)



(defthm topStack-guard-implies-canPopCategory
  (implies (topStack-guard-strong s)
           (canPopCategory1 
            (operand-stack 
             (top (thread-call-stack 
                   (thread-by-id (current-thread s)
                                 (thread-table s)))))))
  :rule-classes :forward-chaining)

;; Doesn't hurt 
;;
(defthm consistent-state-implies-consistent-thread-entry-b
  (implies (consistent-thread-entry th cl hp)
           (consistent-opstack 
            (operand-stack (top (thread-call-stack th))) cl hp)))


(defthm consistent-opstack-consistent-value-x-b
  (implies (and (consistent-opstack opstack cl hp)
                (canPopCategory1 opstack))
           (consistent-value-x (top opstack) cl hp)))
                                      

(defthm topStack-guard-implies-canPopCategory-b
  (implies (topStack-guard-strong s)
           (canPopCategory1 
            (operand-stack 
             (top (thread-call-stack 
                   (thread-by-id (current-thread s)
                                 (thread-table s))))))))


(defthm consistent-state-topstack-consistent-value-x
  (implies (and (topStack-guard-strong s)
                (consistent-state s))
           (consistent-value-x (safe-topStack s) 
                               (instance-class-table s)
                               (heap s)))
  :hints (("Goal" :in-theory (disable consistent-value-x 
                                      consistent-state
                                      topStack-guard-strong
                                      canPopCategory1
                                      operand-stack
                                      thread-call-stack
                                      top
                                      current-thread
                                      instance-class-table
                                      heap
                                      thread-table)))
  :rule-classes :forward-chaining)




(defthm consistent-value-x-implies-wff-tagged-value
  (implies (consistent-value-x v cl hp)
           (wff-tagged-value v))
  :rule-classes :forward-chaining)


(defthm pc-pushStack-unchanged
  (equal (pc (pushStack v s))
         (pc s)))

;;; may have proved it in ADD1-program-correct proof! 
;;; Fri Jan 16 01:34:38 2004. does not matter if .... 


(defthm pc-safe-pushStack-unchanged
  (equal (pc (safe-pushStack v s))
         (pc s)))


(defthm pc-popStack-unchanged
  (equal (pc (popStack s))
         (pc s)))

(defthm pc-safe-popStack-unchanged
  (equal (pc (safe-popStack s))
         (pc s)))

(in-theory (enable wff-state pc))

;; (defthm wff-state-implies-pc-numberp
;;   (implies (wff-state s)
;;            (integerp (pc s)))
;;   :rule-classes :forward-chaining)


;; Mon May 17 16:43:16 2004

;;(i-am-here)

(defthm current-thread-pushStack-unchange
  (equal (current-thread (pushStack v s))
         (current-thread s)))

(defthm current-thread-popStack-unchange
  (equal (current-thread (popStack s))
         (current-thread s)))

;; Mon May 17 16:54:46 2004. If proof breaks. These above is to blame. 
;; Mon May 17 16:55:04 2004


(defthm consistent-state-implies-thread-id-thread-by-id
  (implies (consistent-state s)
           (equal (thread-id (thread-by-id (current-thread s)
                                           (thread-table s)))
                  (current-thread s))))


(defthm thread-by-id-replace-entry
  (implies (and (equal (thread-id new-thread) (thread-id old-thread))
                (wff-thread old-thread)
                (wff-thread new-thread)
                (equal (thread-by-id (thread-id old-thread) ths)
                       old-thread)
                (wff-thread-table ths))
           (equal (thread-by-id x 
                    (replace-thread-table-entry old-thread new-thread ths))
                 (if (equal (thread-id old-thread) x)
                     new-thread
                   (thread-by-id x ths))))
  :hints (("Goal" :do-not '(generalize))))




(defthm consistent-thread-entries-consistent-thread-entries
  (implies (and (consistent-thread-entry new-thread cl hp)
                (consistent-thread-entries ths cl hp))
           (consistent-thread-entries (replace-thread-table-entry 
                                        old new-thread ths)
                                      cl hp))
  :hints (("Goal" :in-theory (disable consistent-thread-entry))))



;; (i-am-here) 
;; Sun Aug  8 16:01:24 2004
;; failed after the update to the consistent-state.

;;
;; need some properties about changing thread-table does not affect class-table
;;
;; The following three are added/ 

(defthm  build-a-java-visible-instance-data-guard-independent-of-thread-table
  (implies (wff-state s)
           (equal (build-a-java-visible-instance-data-guard
                   any (state-set-thread-table anytt s))
                  (build-a-java-visible-instance-data-guard any s)))
  :hints (("Goal" :in-theory (enable build-a-java-visible-instance-data-guard
                                     state-set-thread-table))))

(defthm wff-state-state-set-thread-table
  (implies (wff-state s)
           (wff-state (state-set-thread-table anytt s)))
  :hints (("Goal" :in-theory (enable state-set-thread-table))))


(defthm build-a-java-visible-instance-guard-independent-of-thread-table
  (implies (wff-state s)
           (equal (build-a-java-visible-instance-guard
                   any (state-set-thread-table anytt s))
                  (build-a-java-visible-instance-guard any s)))
  :hints (("Goal" :in-theory (e/d (build-a-java-visible-instance-guard)
                                  (wff-state)))))


(defthm class-table-equal-implies-equal-correctly-loaded
  (equal (jvm::array-class-correctly-loaded? l (state-set-thread-table anytt s1))
         (jvm::array-class-correctly-loaded? l s1))
  :hints (("Goal" :in-theory (enable array-class-table class-loaded? 
                                     array-base-type
                                     instance-class-table)
           :do-not '(generalize))))


;; loops easily.
;; Sun Aug  8 16:39:14 2004
;;

;; (defthm class-table-equal-implies-equal
;;    (implies (and (equal (class-table s2) (class-table s1)))
;;             (equal (jvm::array-class-table-inv-helper l s2)
;;                    (jvm::array-class-table-inv-helper l s1)))
;;    :rule-classes nil)


(defthm class-table-equal-implies-equal
  (equal (jvm::array-class-table-inv-helper l (state-set-thread-table anytt s1))
         (jvm::array-class-table-inv-helper l s1)))

(defthm class-loaded-implies-equal
  (equal (class-loaded? any (state-set-thread-table anytt s1))
         (class-loaded? any s1))
  :hints (("Goal" :in-theory (enable instance-class-table 
                                     class-loaded?))))


(defthm loader-inv-independent-of-state-set-thread-table
  (implies (wff-state s1)
           (equal (jvm::loader-inv (state-set-thread-table anytt s1))
                  (jvm::loader-inv s1)))
  :hints (("Goal" :in-theory (enable instance-class-table jvm::loader-inv))))



;; (i-am-here) ;; Thu Feb 10 14:07:45 2005 ;; added the assertion that
;; unique thread-ids of the thread table!! 

(defthm thread-id-no-change-replace-thread-entry
  (implies (equal (thread-id y) (thread-id x))
           (equal (collect-thread-id (replace-thread-table-entry x y tt))
                  (collect-thread-id tt))))


(defthm consistent-state-set-thread-table-consistent-state
  (implies (and (consistent-state s)
                (wff-thread old-thread)
                (wff-thread new-thread)
                (equal (thread-by-id (thread-id old-thread) (thread-table s))
                       old-thread)
                (equal (thread-id new-thread) (thread-id old-thread))
                (consistent-thread-entry new-thread (instance-class-table s) (heap s)))
           (consistent-state 
              (state-set-thread-table 
                (replace-thread-table-entry old-thread new-thread (thread-table s))
                s)))
  :hints (("Goal" :in-theory (disable consistent-thread-entry wff-state thread-id thread-by-id))))


(in-theory (enable thread-set-call-stack thread-call-stack))

;; (i-am-here)

(in-theory (disable consistent-adjacent-frame)) ;; Sun Mar 20 06:29:17 2005
;; new addition 

(in-theory (disable resume-pc))

(defthm consistent-thread-entry-set-call-stack
  (implies (and (consistent-call-stack cs cl hp)
                (consistent-call-stack-linkage cs cl)
                (consistent-thread-entry th cl hp)
                (consp cs))
           (consistent-thread-entry (thread-set-call-stack cs th) cl hp)))

(defthm push-consistent-frame-implies-consistent-call-stack
  (implies (and (consistent-frame frame cl hp)
                (consistent-call-stack call-stack cl hp))
           (consistent-call-stack (push frame call-stack) cl hp)))

;;; Mon Mar 21 10:41:35 2005. 
;;; 


(defthm consistent-adjacent-frame-if-return-pc-method-ptr-equal
  (implies (and (consistent-adjacent-frame caller callee cl)
                (equal (return-pc modified-callee)
                       (return-pc callee))
                (equal (method-ptr modified-callee)
                       (method-ptr callee)))
           (consistent-adjacent-frame caller modified-callee cl))
  :hints (("Goal" :in-theory (enable consistent-adjacent-frame))))



(defthm consistent-linkage-linkage-lemma
  (implies (and (consistent-call-stack-linkage call-stack cl)
                (equal (return-pc frame)
                       (return-pc (top call-stack)))
                (equal (method-ptr frame)
                       (method-ptr (top call-stack)))
                (wff-invocation-frame frame cl))
           (consistent-call-stack-linkage (push frame (pop call-stack)) cl)))


(defthm wff-invocation-frame-frame-set-opstack
  (implies (and (wff-invocation-frame frame cl)
                (<= (LEN OPSTACK)
                    (method-maxstack (deref-method (method-ptr frame) cl)))
                (true-listp opstack))
           (wff-invocation-frame (frame-set-operand-stack opstack frame) cl))
  :hints (("Goal" :in-theory (e/d (wff-call-frame operand-stack) ())
           :do-not-induct t)))

(defthm return-pc-frame-set-operand-stack
  (equal (return-pc (frame-set-operand-stack opstack frame))
         (return-pc frame)))


(defthm return-pc-frame-set-locals
  (equal (return-pc (frame-set-locals locals frame))
         (return-pc frame)))



(defthm method-ptr-frame-set-locals
  (equal (method-ptr (frame-set-locals locals frame))
         (method-ptr frame)))


(defthm method-ptr-frame-set-operand-stack
  (equal (method-ptr (frame-set-operand-stack opstack frame))
         (method-ptr frame)))



(in-theory (enable frame-set-operand-stack))


(local (in-theory (enable wff-call-frame make-frame wff-call-stack operand-stack locals sync-obj-ref return-pc method-ptr)))
;; (local (in-theory (disable frame-set-operand-stack)))
;(i-am-here)

;; (defthm wff-call-frame-make-frame
;;   (implies (valid-sync-obj
;;    (wff-call-frame (make-frame 
;;                     RETURN-PC 
;;                     OPERANT-STACK LOCALS
;;                     METHOD-PTR 
;;                     SYNC-OBJ-REF 
;;                     RESUME-PC)))
;; 
;; ;; (i-am-here)                   

(defthm consistent-opstack-truelistp
  (implies (consistent-opstack opstack cl hp)
           (true-listp opstack))
  :rule-classes :forward-chaining)

(defthm set-opstack-consistent-frame-consistent-frame
  (implies (and (consistent-opstack opstack cl hp)
                (<= (len opstack) (method-maxstack (deref-method (method-ptr frame) cl)))
                (consistent-frame frame cl hp))
           (consistent-frame (frame-set-operand-stack opstack frame) cl hp))
  :hints (("Goal" :do-not-induct t)))

;; Mon May 17 14:20:20 2004. Add extra assertion about OPSTACK size 

(defthm len-pop-opstack-is
  (implies (canPopCategory1 opstack)
           (equal (len (pop opstack))
                  (- (len opstack) 1))))





;; (defthm len-pop-opstack-less-than
;;   (<= (len (pop opstack)) (len opstack)))


(defthm consistent-opstack-pop-consistent-opstack
  (implies (and (canPopCategory1 opstack)
                (consistent-opstack opstack cl hp))
           (consistent-opstack (pop opstack) cl hp)))

(defthm consistent-opstack-push-consistent-opstack
  (implies (and (consistent-value-x v cl hp)
                (consistent-opstack opstack cl hp)
                (Category1 v))
           (consistent-opstack (push v opstack) cl hp)))


(defthm consistent-opstack-push-consistent-opstack
  (implies (and (consistent-value-x v cl hp)
                (consistent-opstack opstack cl hp)
                (Category1 v))
           (consistent-opstack (push v opstack) cl hp)))


(defthm consistent-opstack-pop2-consistent-opstack
  (implies (and (canPopCategory2 opstack)
                (consistent-opstack opstack cl hp))
           (consistent-opstack (pop (pop opstack)) cl hp)))

(defthm consistent-call-stack-consistent-call-stack
  (implies (consistent-call-stack cs cl hp)
           (consistent-call-stack (cdr cs) cl hp)))

(defthm consisntent-frame-implied-by-consistent-call-stack
  (implies (and (consistent-call-stack cs cl hp)
                (consp cs))
           (consistent-frame (top cs) cl hp)))

(defthm consisntent-call-stack-implied-by-consistent-call-stack
  (implies (consistent-call-stack cs cl hp)
           (consistent-call-stack (pop cs) cl hp)))


; (i-am-here) ;;  Sun Oct 17 16:46:27 2004

;; quite some problems. 

(defthm consistent-frame-implies-len-opstack-less-max-stack
  (implies (and (consistent-frame frame cl hp)
                ;;; Sun Oct 17 16:48:08 2004
                (not (mem '*native* (method-accessflags 
                                     (deref-method (method-ptr frame) cl)))))
           (<= (len (operand-stack frame))
               (METHOD-MAXSTACK (DEREF-METHOD (METHOD-PTR FRAME) CL))))
  :rule-classes :linear)


;; (defthm consistent-frame-implies-len-opstack-less-max-stack
;;   (implies (consistent-frame frame cl hp)
;;            (<= (len (operand-stack frame))
;;                (METHOD-MAXSTACK (DEREF-METHOD (METHOD-PTR FRAME) CL))))
;;   :rule-classes :linear)


(defthm consistent-frame-implies-len-opstack-less-max-stack-rewrite
  (implies (and (consistent-frame frame cl hp)
                (not (mem '*native* (method-accessflags 
                                     (deref-method (method-ptr frame) cl)))))
           (<= (len (operand-stack frame))
               (METHOD-MAXSTACK (DEREF-METHOD (METHOD-PTR FRAME) CL)))))


;; free variable problem!! 

(defthm consistent-frame-implies-len-opstack-less-max-stack-forward
  (implies (and (consistent-frame frame cl hp)
                (not (mem '*native* (method-accessflags 
                                     (deref-method (method-ptr frame) cl)))))
           (<= (len (operand-stack frame))
               (METHOD-MAXSTACK (DEREF-METHOD (METHOD-PTR FRAME) CL))))
  :rule-classes :forward-chaining)



(defthm consistent-thread-entry-consistent-frame
  (implies (consistent-thread-entry th cl hp)
           (consistent-frame (top (thread-call-stack th))
                             cl hp))
  :hints (("Goal" :in-theory (e/d () (consistent-frame)))))


;; (defthm consistent-
;;   (implies (consistent-thread-entry th cl hp)
;;            (consistent-frame (top (thread-call-stack th))
;;                              cl hp))
;;   :hints (("Goal" :in-theory (e/d () (consistent-frame)))))

(defthm consistent-state-consistent-thread-entry
  (implies (consistent-state s)
           (consistent-thread-entry (thread-by-id (current-thread s)
                                                  (thread-table s))
                                    (instance-class-table s)
                                    (heap s)))
  :hints (("Goal" :in-theory (e/d (current-frame) (consistent-thread-entry)))))

(local (in-theory (disable top thread-call-stack operand-stack)))


(defthm consistent-state-consistent-frame
  (implies (consistent-state s)
           (consistent-frame (current-frame s)
                             (instance-class-table s)
                             (heap s)))
  :hints (("Goal" :in-theory (e/d (current-frame) (consistent-frame
                                                   consistent-state)))))

(defthm consistent-state-consistent-frame-forward
  (implies (consistent-state s)
           (consistent-frame (current-frame s)
                             (instance-class-table s)
                             (heap s)))
  :hints (("Goal" :in-theory (e/d (current-frame) (consistent-frame
                                                   consistent-state))))
  :rule-classes :forward-chaining)


(local
 (defthm current-frame-normalize
   (equal (TOP (THREAD-CALL-STACK (THREAD-BY-ID (CURRENT-THREAD S)
                                                (THREAD-TABLE S))))
          (current-frame s))))



(defthm consistent-state-implies-len-opstack-less-max-stack-rewrite
  (implies (and (consistent-state s)
                (not (mem '*native* (method-accessflags 
                                     (deref-method (method-ptr (current-frame
                                                                s))
                                                   (instance-class-table s))))))
           (<= (len (operand-stack (current-frame s)))
               (max-stack s)))
  :hints (("Goal" :in-theory (e/d () (current-frame method-ptr method-maxstack 
                                                    consistent-state)))))


;; (skip-proofs 
;;  (defthm consistent-state-implies-len-opstack-less-max-stack
;;    (implies (consistent-state s)
;;             (<= (len (operand-stack (current-frame s)))
;;                 (max-stack s)))))

(defthm consistent-state-implies-len-opstack-less-max-stack-linear
  (implies (and (consistent-state s)
                (not (mem '*native* 
                          (method-accessflags 
                           (deref-method (method-ptr (current-frame s))
                                         (instance-class-table s))))))
           (<= (len (operand-stack (current-frame s)))
               (max-stack s)))
  :hints (("Goal" :in-theory (disable current-frame max-stack operand-stack consistent-state)))
  :rule-classes :linear)

(local (in-theory (disable current-frame-normalize)))

(defthm consistent-state-implies-len-opstack-less-max-stack-specific
   (implies (and (consistent-state s)
                (not (mem '*native* 
                          (method-accessflags 
                           (deref-method (method-ptr (current-frame s))
                                         (instance-class-table s))))))
            (<= (len (operand-stack (top (thread-call-stack (thread-by-id
                                                             (current-thread s)
                                                             (thread-table s))))))

                (METHOD-MAXSTACK
                 (DEREF-METHOD
                  (METHOD-PTR (TOP (THREAD-CALL-STACK (THREAD-BY-ID (CURRENT-THREAD S)
                                                                    (THREAD-TABLE S)))))
                  (INSTANCE-CLASS-TABLE S)))))
   :hints (("Goal" :use
            consistent-state-implies-len-opstack-less-max-stack-rewrite
            :in-theory (disable consistent-state)))
   :rule-classes :linear)

;;;
;;; ??  Sun Oct 17 12:01:08 2004. 
;;; 

;;; (i-am-here)

(in-theory (disable wff-call-frame wff-method-decl 
                    method-accessflags consistent-adjacent-frame
                    wff-method-ptr))

(skip-proofs 
 (defthm consistent-state-implies-consistent-call-linkage
   (implies (consistent-state s)
            (consistent-call-stack-linkage 
             (thread-call-stack (thread-by-id (current-thread s) 
                                              (thread-table s)))
             (instance-class-table s)))))

(skip-proofs 
 (defthm consistent-state-implies-wff-invocation-frame
   (implies (consistent-state s)
            (WFF-INVOCATION-FRAME
             (TOP (THREAD-CALL-STACK (THREAD-BY-ID (CURRENT-THREAD S)
                                                   (THREAD-TABLE S))))
             (INSTANCE-CLASS-TABLE S)))))

(in-theory (disable wff-invocation-frame))

(defthm true-listp-pop
  (implies (true-listp list)
           (TRUE-LISTP (pop list))))

(defthm true-listp-push
  (implies (true-listp list)
           (TRUE-LISTP (push v list))))


(defthm consistent-state-popStack-consistent-state
   (implies (and (topStack-guard-strong s)
                 (consistent-state s))
            (consistent-state (safe-popStack s)))
   :hints (("Goal" :in-theory (disable
                                       state-set-thread-table
                                       consistent-state
                                       consistent-opstack-consistent-value-x-b
                                       consistent-thread-entry
                                       wff-thread
                                       wff-state
                                       thread-call-stack
                                       instance-class-table
                                       heap
                                       top
                                       pop
                                       current-frame
                                       method-ptr
                                       method-maxstack
                                       operand-stack
                                       canPopCategory1
                                       frame-set-operand-stack
                                       thread-set-call-stack
                                       current-thread
                                       thread-table
                                       push
                                       consistent-frame))))



(defthm len-push-v-is
  (equal (len (push v l))
         (+ (len l) 1)))

;(i-am-here)

;; (defthm len-add-minus
;;   (implies (and (< (len l) x)
;;                 (integerp x))
;;            (<= (len (push v l)) x)))

;; (len (operand-stack (top (thread-call-stack
;;                                                     (thread-by-id
;;                                                      (current-thread s) 
;;                                                      (thread-table s)))))))


(defthm consistent-state-pushStack-consistent-state
   (implies (and (consistent-value-x v (instance-class-table s) (heap s))
                 (Category1 v)
                 (<= (+ 1 (len (operand-stack (current-frame s))))
                     (max-stack s))
                 (consistent-state s))
            (consistent-state (safe-pushStack v s)))
   :hints (("Goal" 
            :use consistent-state-implies-len-opstack-less-max-stack-specific
            :in-theory (disable state-set-thread-table
                                       consistent-state
                                       consistent-opstack-consistent-value-x-b
                                       consistent-thread-entry
                                       wff-thread
                                       wff-state
                                       thread-call-stack
                                       instance-class-table
                                       heap
                                       top
                                       pop
                                       method-maxstack
                                       ;; current-frame
                                       method-ptr
                                       operand-stack
                                       canPopCategory1
                                       frame-set-operand-stack
                                       thread-set-call-stack
                                       current-thread
                                       thread-table
                                       push
                                       consistent-frame))))




(defthm array-object-consistent1-and-nth 
  (implies (and (array-obj-consistent1 data-array component-type hp cl)
                (<= 0 index)
                (< index (len data-array)))
           (consistent-value (tag (nth index data-array)
                                  component-type)
                             component-type cl hp))
  :hints (("Goal" :in-theory (disable consistent-value tag)
           :do-not '(generalize))))

(defthm consistent-array-obj-implies-array
  (implies (consistent-array-object array-obj hp cl acl)
           (array-obj-consistent1 (array-data array-obj) (array-component-type
                                                          (obj-type array-obj))
                                  hp cl))
  :rule-classes :forward-chaining)

;; (acl2::set-match-free-error nil)

(defthm consistent-value-element-at-consistent-array
  (implies (and (consistent-array-object array hp cl acl)
                (<= 0 index)
                (<  index (array-bound array)))
           (consistent-value 
                (tag (element-at index array) (array-component-type (obj-type array)))
                (array-component-type (obj-type array))
                cl hp))
  :hints (("Goal" :in-theory (disable consistent-value tag
                                      array-bound array-data
                                      primitive-type? obj-type
                                      array-component-type))))


(defthm consistent-value-implies-consistent-value-x
  (implies (consistent-value v type cl hp)
           (consistent-value-x v cl hp))
  :rule-classes :forward-chaining)


(defthm consistent-value-implies-consistent-value-x-b
  (implies (consistent-value v type cl hp)
           (consistent-value-x v cl hp)))


(in-theory (enable state-set-thread-table make-state))

;; (defthm wff-state-state-set-thread-table
;;   (implies (wff-state s)
;;            (wff-state (state-set-thread-table tt s))))

(in-theory (disable  state-set-thread-table wff-state))

;; (defthm wff-state-state-set-thread-table
;;   (implies (wff-state s)
;;            (wff-state (state-set-thread-table any s)))
;;   :hints (("Goal" :in-theory (enable wff-state state-set-thread-table))))

(defthm pushStack-wffstate
  (implies (wff-state s)
           (wff-state (safe-pushStack v s)))
  :hints (("Goal" :in-theory (disable consistent-state))))

(defthm popStack-wffstate
  (implies (wff-state s)
           (wff-state (safe-popStack s)))
  :hints (("Goal" :in-theory (disable consistent-state))))

(in-theory (enable state-set-thread-table wff-state))


;; (defthm pushStack-wffstate-f
;;   (implies (wff-state s)
;;            (wff-state (safe-pushStack v s)))
;;   :rule-classes :forward-chaining)


;; (defthm popStack-wffstate-f
;;   (implies (wff-state s)
;;            (wff-state (safe-popStack s)))
;;   :rule-classes :forward-chaining)


(defthm consistent-state-implies-wff-state
  (implies (consistent-state s)
           (wff-state s))
  :rule-classes :forward-chaining)

(defthm consistent-state-implies-wff-heap
  (implies (consistent-state s)
           (wff-heap (heap s)))
  :rule-classes :forward-chaining)


(defthm consistent-value-x-and-tag-REF-implies-valid-refp
  (implies (and (not (equal (value-of tagged-value) -1))
                (consistent-value-x tagged-value cl hp)
                (equal (tag-of tagged-value) 'REF))
           (Valid-REFp tagged-value hp))
  :rule-classes :forward-chaining)

;; Be careful about selection of trigger term.

;; the problem here is that binding for cl and hp if we want to use forward
;; chaining. We need to start from (consistent-state s) to get the fact that
;; certain slot are Valid-REFp

(defthm Valid-REFp-Deref2-in-wff-heap-strong-wff-object
  (implies (and (Valid-REFp ref hp)
                (wff-heap-strong hp))
           (wff-obj-strong (deref2 ref hp)))
  :rule-classes :forward-chaining)

;; Forward chaining rule rules!!

;; give stronger guidance


;; (defthm thread-set-call-stack-consistent-thread-entry
;;   (implies (and (consistent-call-stack cs cl hp)
;;                 (consistent-call-stack-linkage cs cl)
;;                 (consp cs)
;;                 (consistent-thread-entry th cl hp))
;;            (consistent-thread-entry (thread-set-call-stack cs th) cl hp))
;;   :hints (("Goal" :in-theory (enable thread-set-call-stack thread-call-stack 
;;                                      wff-thread))))

;;; this seems to be proved before. as: consistent-thread-entry-set-call-stack

(defthm consistent-call-stack-push-new-frame
  (implies (and (consistent-frame frame cl hp)
                (consistent-call-stack cs cl hp))
           (consistent-call-stack (cons frame cs) cl hp)))


(in-theory (disable make-state))

(defthm popStack-heap-unchanged
  (equal (heap (popStack s))
         (heap s)))


(defthm pushStack-heap-unchanged
  (equal (heap (pushStack v s))
         (heap s)))



(defthm safe-popStack-heap-unchanged
  (equal (heap (safe-popStack s))
         (heap s))
  :hints (("Goal" :in-theory (disable consistent-state))))

(defthm safe-pushStack-heap-unchanged
  (equal (heap (safe-pushStack v s))
         (heap s))
  :hints (("Goal" :in-theory (disable consistent-state))))


(in-theory (enable instance-class-table))

(defthm popStack-instance-class-table-unchanged
  (equal (instance-class-table (popStack s))
         (instance-class-table s)))

(defthm pushStack-instance-class-table-unchanged
  (equal (instance-class-table (pushStack v s))
         (instance-class-table s)))


(defthm instance-class-table-no-change-safe-pushCategory2
  (equal (INSTANCE-CLASS-TABLE
          (SAFE-PUSHCATEGORY2 v s))
         (instance-class-table s))
  :hints (("Goal" :in-theory (e/d (safe-pushcategory2)
                                  (pushStack instance-class-table)))))


(defthm safe-popStack-instance-class-table-unchanged
  (equal (instance-class-table (safe-popStack s))
         (instance-class-table s))
  :hints (("Goal" :in-theory (disable consistent-state))))



(defthm safe-pushStack-instance-class-table-unchanged
  (equal (instance-class-table (safe-pushStack v s))
         (instance-class-table s))
  :hints (("Goal" :in-theory (disable consistent-state))))



(in-theory (disable instance-class-table))



(defthm consistent-state-secondStack-consistent-value-x
  (implies (and (secondStack-guard-strong s)
                (consistent-state s))
           (consistent-value-x (safe-secondStack s) 
                               (instance-class-table s)
                               (heap s)))
  :hints (("Goal" :in-theory (disable consistent-value-x safe-popStack safe-topStack consistent-state)
           :use ((:instance consistent-state-topstack-consistent-value-x
                            (s (safe-popStack s))))))
  :rule-classes :forward-chaining)


(defthm consistent-state-implies-wff-heap-strong
  (implies (consistent-state s)
           (wff-heap-strong (heap s)))
  :hints (("Goal" :in-theory (enable consistent-state)))
  :rule-classes :forward-chaining)




;; question, what's the problem with Forward chaining

(defthm consistent-state-implies-wff-class-table
  (implies (consistent-state s)
           (wff-class-table (class-table s)))
  :hints (("Goal" :in-theory (enable consistent-state)))
  :rule-classes :forward-chaining)


(defthm consistent-state-implies-wff-instance-class-table
  (implies (consistent-state s)
           (wff-instance-class-table (instance-class-table s)))
  :hints (("Goal" :in-theory (enable consistent-state)))
  :rule-classes :forward-chaining)


;; (in-theory (disable deref2 consistent-state heap tag-of value-of))


;; >V d          (DEFUN MAKE-STATE
;;                      (JVM::IP JVM::CUR JVM::JAVAHEAP
;;                               THREAD-TABLE JVM::INTERNAL-CLASS-TABLE
;;                               ENV ERROR-FLAG AUX)
;;                      (LIST 'STATE
;;                            JVM::IP
;;                            JVM::CUR (CONS 'HEAP JVM::JAVAHEAP)
;;                            (CONS 'THREAD-TABLE THREAD-TABLE)
;;                            JVM::INTERNAL-CLASS-TABLE
;;                            ENV ERROR-FLAG AUX))

;; (defthm wff-state-state-set-pc
;;   (implies (integerp pc)
;;            (wff-state (make-state pc cid hp tt cl env ef aux)))
;;   :hints (("Goal" :in-theory (e/d (make-state wff-state)))))

;;; moved to jvm-object-manipulation-primitives.lisp Wed Mar 31 13:08:45 2004

;(i-am-here)
(defthm state-set-pc-loader-inv
  (implies (and (wff-state s)
                (integerp pc))
           (equal (jvm::loader-inv (state-set-pc pc s))
                  (jvm::loader-inv s)))
  :hints (("Goal" :in-theory (enable jvm::loader-inv))))


;; (defthm state-set-pc-loader-inv-alternative
;;   (implies (and (wff-state s)
;;                 (integerp pc))
;;            (equal (jvm::loader-inv (state-set-pc pc s))
;;                   (jvm::loader-inv s)))
;;   :hints (("Goal" :in-theory (enable jvm::loader-inv))))


(defthm state-set-pc-wff-state
  (implies (and (wff-state s)
                (integerp pc))
           (wff-state (state-set-pc pc s))))

(defthm state-set-pc-class-loaded?
  (equal (class-loaded? any (state-set-pc pc s))
         (class-loaded? any s))
  :hints (("Goal" :in-theory (enable class-loaded? instance-class-table))))

;(i-am-here)

(defthm state-set-pc-load_class-guard
  (implies (and (wff-state s)
                (integerp pc))
           (equal (jvm::load_class-guard (state-set-pc pc s))
                  (jvm::load_class-guard s)))
  :hints (("Goal" :in-theory (disable state-set-pc))))

(defthm array-class-table-equal-after-state-set-pc
  (equal (array-class-table (state-set-pc pc s))
         (array-class-table s)))

(defthm state-set-pc-build-a-java-instance-data-guard
  (implies (and (wff-state s)
                (integerp pc))
           (equal (build-a-java-visible-instance-data-guard any (state-set-pc pc s))
                  (build-a-java-visible-instance-data-guard any s))))



(defthm state-set-pc-build-a-java-instance-guard
  (implies (and (wff-state s)
                (integerp pc))
           (equal (build-a-java-visible-instance-guard any (state-set-pc pc s))
                  (build-a-java-visible-instance-guard any s)))
  :hints (("Goal" :in-theory (e/d (build-a-java-visible-instance-guard)
                                  (state-set-pc)))))
                                  


(defthm state-set-pc-array-class-correctly-loaded?
  (implies (and (wff-state s)
                (integerp pc))
           (equal (jvm::array-class-correctly-loaded? l (state-set-pc pc s))
                  (jvm::array-class-correctly-loaded? l s)))
  :hints (("Goal" :in-theory (e/d (array-base-type)
                                  (state-set-pc)))))
  


(defthm state-set-pc-array-class-table-inv-helper
  (implies (and (wff-state s)
                (integerp pc))
           (equal (jvm::array-class-table-inv-helper l (state-set-pc pc s))
                  (jvm::array-class-table-inv-helper l s)))
  :hints (("Goal" :in-theory (disable state-set-pc))))


(defthm state-set-pc-load_array_class_guard
  (implies (and (wff-state s)
                (integerp pc))
           (equal (jvm::load_array_class_guard (state-set-pc pc s))
                  (jvm::load_array_class_guard s)))
  :hints (("Goal" :in-theory (disable state-set-pc))))



;; (defthm state-set-pc-build-instance-guard
;;   (implies (and (wff-state s)
;;                 (integerp pc))
;;            (equal (jvm::loader-inv (state-set-pc pc s))
;;                   (jvm::loader-inv s)))
;;   :hints (("Goal" :in-theory (enable jvm::loader-inv))))

  

(local (in-theory (disable state-set-pc wff-state instance-class-table 
                           class-table
                           array-class-table)))


(defthm state-set-pc-consistent-state
  (implies (and (consistent-state s)
                (integerp pc))
           (consistent-state (state-set-pc pc s)))
  :hints (("Goal" :in-theory (e/d (consistent-state)
                                  (CONSISTENT-THREAD-TABLE
                                   jvm::load_array_class_guard
                                   consistent-class-table
                                   jvm::load_class-guard
                                   consistent-heap
                                   )))))

;;state-set-pc 
                                     ;;jvm::loader-inv))))
                                     ;array-class-table instance-class-table
                                     ;heap))))



(defthm consistent-state-implies-pc-numberp
  (implies  (consistent-state s)
            (integerp (pc s)))
  :rule-classes :forward-chaining)

;; (defthm consistent-value-implies-consistent-value-x-b-instance
;;   (let ((v (tag (ELEMENT-AT (VALUE-OF (TOPSTACK S))
;;                             (deref2 (SECONDSTACK S) (heap s)))
;;                  (array-component-type (obj-type (deref2 (secondstack s) (heap s))))))
;;         (type (array-component-type (obj-type (deref2 (secondStack s) (heap s)))))
;;         (cl (instance-class-table s))
;;         (hp (heap s)))
;;     (implies (consistent-value v type cl hp)
;;              (consistent-value-x v cl hp))))
;;
;; moved to djvm-bytecode.lisp
;;


(defthm tag-ref-tag
    (implies (not (primitive-type? type))
           (equal (tag-ref v)
                  (tag v type)))
    :hints (("Goal" :in-theory (enable tag-ref tag))))


;; (defthm consistent-value-implies-consistent-value-x-b-instance-real
;;   (let ((v1 (tag (ELEMENT-AT (VALUE-OF (TOPSTACK S))
;;                             (deref2 (SECONDSTACK S) (heap s)))
;;                  (array-component-type (obj-type (deref2 (secondstack s) (heap
;;                                                                           s))))))
;;         (v2 (tag-ref (ELEMENT-AT (VALUE-OF (TOPSTACK S))
;;                             (deref2 (SECONDSTACK S) (heap s)))))
;;         (type (array-component-type (obj-type (deref2 (secondStack s) (heap s)))))
;;         (cl (instance-class-table s))
;;         (hp (heap s)))
;;     (implies (and (consistent-value v1 type cl hp)
;;                   (not (primitive-type? type)))
;;              (consistent-value-x v2 cl hp))))

;; (in-theory (disable consistent-value-implies-consistent-value-x-b-instance
;;                     tag-ref-tag))




(defthm tag-ref-category1
  (implies (wff-tagged-value (tag-ref v))
           (category1 (tag-ref v)))
  :hints (("Goal" :in-theory (enable category1 tag-ref tag-of 
                                     wff-tagged-value))))


(defthm consistent-value-x-implies-wff-tagged-value-b
  (implies (consistent-value-x v cl hp)
           (wff-tagged-value v)))


(defthm wff-internal-array-implies-array-type
  (implies (wff-internal-array array)
           (isArrayType (obj-type array)))
  :hints (("Goal" :in-theory (enable wff-internal-array
                                     obj-type)))
  :rule-classes :forward-chaining)

;;(i-am-here) ;; Tue Jul 12 20:16:11 2005

(in-theory (disable consistent-array-object))

(defthm consistent-heap2-isArrayType-consistent-array-object
  (implies (and (mem xobj hp1)
                (isArrayType (obj-type (cdr xobj)))
                (consistent-heap2 hp1 hp cl acl id))
           (consistent-array-object (cdr xobj)
                                    hp cl acl)))



(defthm assoc-mem
  (implies (assoc-equal x l)
           (mem (assoc-equal x l) l)))



(defthm array-type-s-implies-isArrayType
  (implies (array-type-s type cl)
           (isArrayType type))
  :rule-classes :forward-chaining)


(in-theory (disable isArrayType))



(in-theory (disable consistent-state safe-popStack topStack-guard-strong pc
                    wff-obj wff-obj-strong safe-pushStack safe-topStack wff-state)) ;; wff-internal-array))


;---------------------------------------------------------------------

(defthm consistent-heap1-deref2-f
  (implies (and (consistent-heap1 hp1 hp cl id)
                (deref2 v hp1))
           (consistent-object (deref2 v hp1) hp cl))
  :hints (("Goal" :in-theory (disable consistent-object)))
  :rule-classes :forward-chaining)


(defthm consistent-heap1-deref2
  (implies (and (consistent-heap1 hp1 hp cl id)
                (deref2 v hp1))
           (consistent-object (deref2 v hp1) hp cl))
  :hints (("Goal" :in-theory (disable consistent-object))))


(defthm consistent-heap-deref2-f
  (implies (and (consistent-heap hp cl acl)
                (deref2 v hp))
           (consistent-object (deref2 v hp) hp cl))
  :hints (("Goal" :in-theory (disable consistent-object deref2)))
  :rule-classes :forward-chaining)


(defthm consistent-heap-deref2
  (implies (and (consistent-heap hp cl acl)
                (deref2 v hp))
           (consistent-object (deref2 v hp) hp cl))
  :hints (("Goal" :in-theory (disable consistent-object deref2))))

;---------------------------------------------------------------------

(defthm consistent-thread-entry-implies-consp-call-stack
  (implies (consistent-thread-entry thread cl hp)
           (consp (thread-call-stack thread)))
  :rule-classes :forward-chaining)

;; (acl2::set-match-free-error nil)

(defthm consistent-thread-entry-implies-consp-call-stack-b
  (implies (consistent-thread-entry thread cl hp)
           (consp (thread-call-stack thread))))


(defthm consistent-thread-entry-implies-consp-call-stack-b-specific
  (implies (consistent-thread-entry (thread-by-id id (thread-table s))
                                    (instance-class-table s) (heap s))
           (consp (thread-call-stack (thread-by-id id (thread-table s))))))



(defthm consistent-call-stack-implies-car-consistent-call-frame
  (implies (and (consistent-call-stack cs cl hp)
                (consp cs))
           (consistent-frame (car cs) cl hp))
  :rule-classes :forward-chaining)


(defthm consistent-call-stack-implies-car-consistent-call-frame-b
  (implies (and (consistent-call-stack cs cl hp)
                (consp cs))
           (consistent-frame (car cs) cl hp)))


;----------------------------------------------------------------------

(in-theory (disable thread-table thread-set-call-stack current-thread
                    frame-set-operand-stack thread-table thread-id
                    operand-stack thread-call-stack wff-thread))


(in-theory (enable thread-by-id))

(defthm replace-thread-table-entry-thread-by-id-1
  (implies (and (equal (thread-id new-thread) id2)
                (thread-by-id id2 thread-table)
                (wff-thread-table thread-table)
                (not (equal id1 id2)))
           (equal (thread-by-id id1 (replace-thread-table-entry
                                     (thread-by-id id2 thread-table)
                                     new-thread
                                     thread-table))
                  (thread-by-id id1 thread-table))))

(defthm replace-thread-table-entry-thread-by-id-2
       (implies (and (equal (thread-id new-thread) id2)
                     (thread-by-id id2 thread-table)
                     (wff-thread-table thread-table)                     
                     (equal id1 id2))
                (equal (thread-by-id id1 (replace-thread-table-entry
                                          (thread-by-id id2 thread-table)
                                          new-thread
                                          thread-table))
                       new-thread)))


(defthm consistent-state-implies-thread-by-id-exists
  (implies (consistent-state s)
           (thread-by-id (current-thread s) (thread-table s)))
  :hints (("Goal" :in-theory (enable consistent-state)))
  :rule-classes :forward-chaining)


(defthm thread-call-stack-thread-set-call-stack
  (equal (thread-call-stack (thread-set-call-stack cs thread))
         cs)
  :hints (("Goal" :in-theory (enable thread-call-stack thread-set-call-stack))))





(defthm topFrame-normalization 
  ;; should be named to current-frame
  (equal (top (thread-call-stack (thread-by-id 
                                  (current-thread s)
                                  (thread-table s))))
         (current-frame s)))

           
(defthm consistent-state-implies-consistent-frame
   (implies (consistent-state s)
            (consistent-frame (current-frame s)
                              (instance-class-table s)
                              (heap s)))
   :hints (("Goal" :in-theory (e/d (consistent-state consistent-thread-entry current-frame)
                                   (consistent-frame topFrame-normalization))
            :expand ((current-frame s))))
   :rule-classes :forward-chaining)


(in-theory (disable thread-by-id))

;; (in-theory (enable thread-table thread-set-call-stack current-thread
;;                     frame-set-operand-stack thread-table thread-id
;;                     operand-stack thread-call-stack wff-thread))



(defthm consistent-state-implies-consistent-class-decls
  (implies (consistent-state s)
           (consistent-class-decls (instance-class-table s)
                                   (instance-class-table s)
                                   (heap s)))
  :hints (("Goal" :in-theory (e/d (consistent-state consistent-class-table)
                                  (consistent-class-decls))))
  :rule-classes :forward-chaining)



;;----------------------------------------------------------------------
;;
;; (defthm consistent-state-implies-consistent-frame
;;    (implies (consistent-state s)
;;             (consistent-frame (top-frame s)
;;                               (instance-class-table s)
;;                               (heap s)))
;;    :hints (("Goal" :in-theory (e/d (consistent-state consistent-thread-entry top-frame)
;;                                    (consistent-frame topframe-normalization))
;;             :expand ((top-frame s))))
;;    :rule-classes :forward-chaining)
;;
;; this should be easy too.
;;

;; ;;; moved to consistent-state-properties.lisp Mon Feb 23 22:23:17 2004


;; (defthm consistent-frame-implies-consistent-opstack
;;    (implies (consistent-frame frame cl hp)
;;             (consistent-opstack (operand-stack frame) cl hp))
;;    :rule-classes :forward-chaining)


;; (defthm consistent-opstack-implies-canPop-implies-consistent-value
;;   (implies (and (consistent-opstack opstack cl hp)
;;                 (canPopCategory1 opstack))
;;            (consistent-value-x (car opstack) cl hp))
;;   :rule-classes :forward-chaining)


;----------------------------------------------------------------------


(defthm consistent-frame-implies-wff-call-frame-f
  (implies (consistent-frame frame cl hp)
           (wff-call-frame frame))
  :rule-classes :forward-chaining)


(defthm consistent-state-consistent-frame-f
  (implies (consistent-state s)
           (consistent-frame (current-frame s)
                             (instance-class-table s)
                             (heap s)))
  :rule-classes :forward-chaining)


;; (defthm consistent-state-consistent-thread-entry
;;   (implies (consistent-state s)
;;            (consistent-thread-entry 
;;             (thread-by-id (current-thread s)
;;                           (thread-table s))
;;             (instance-class-table s)
;;             (heap s)))
;;   :rule-classes :forward-chaining)


;; (defthm consistent-thread-entry-implies-consistent-call-stack
;;   (implies (consistent-thread-entry thread cl hp)
;;            (consistent-call-stack (thread-call-stack thread) cl hp))
;;   :rule-classes :forward-chaining)
;;;
;;; proved in djvm-frame-manipulation-primitives.lisp
;;;

(defthm consp-len-no-less-than-0
  (implies (consp l)
           (<= 1 (len l)))
  :rule-classes :linear)


(defthm consistent-thread-entry-implies-wff-call-stack
  (implies (consistent-thread-entry th cl hp)
           (wff-call-stack (thread-call-stack th)))
  :rule-classes :forward-chaining)

;(i-am-here)

(local
 (defthm consistent-method-decl-search-method
   (implies (and (searchMethod method-ptr methods)
                 (consistent-method-decls methods))
            (consistent-method-decl (searchMethod method-ptr methods)))))

(local 
 (defthm consistent-class-decl-consistent-class-decls
   (implies (and (class-by-name name cls)
                 (consistent-class-decls cls cl hp))
            (consistent-class-decl (class-by-name name cls) cl hp))
   :hints (("Goal" :in-theory (disable consistent-class-decl)))))


(local 
 (defthm consistent-class-decl-consistent-class-decls-f
   (implies (and (class-by-name name cls)
                 (consistent-class-decls cls cl hp))
            (consistent-class-decl (class-by-name name cls) cl hp))
   :hints (("Goal" :in-theory (disable consistent-class-decl)))
   :rule-classes :forward-chaining))



(local
 (defthm consistent-method-decls-if-consistent-class-decl
   (implies (consistent-class-decl class cl hp)
            (consistent-method-decls (methods class)))))


(local
 (defthm consistent-method-decls-if-consistent-class-decl-f
   (implies (consistent-class-decl class cl hp)
            (consistent-method-decls (methods class)))
   :rule-classes :forward-chaining))


(defthm valid-method-ptr-consistent-class-decls-consistent-method
  (implies (and (valid-method-ptr method-ptr cl)
                (consistent-class-decls cl anyCl hp))
           (consistent-method-decl (deref-method method-ptr cl)))
  :hints (("Goal" :in-theory (e/d (deref-method) 
                                  (consistent-method-decl 
                                   consistent-class-decl
                                   methods))))
  :rule-classes :forward-chaining)


;; (skip-proofs
;;  (defthm valid-method-ptr-consistent-class-decls-consistent-method
;;    (implies (and (valid-method-ptr method-ptr cl)
;;                  (consistent-class-decls cl anyCl hp))
;;             (consistent-method-decl (deref-method method-ptr cl))))
;;  :rule-classes :forward-chaining)

                                  
;; Mon Mar 29 23:46:34 2004 TODO.

(defthm valid-method-ptr-implied-by-consistent-frame
  (implies (consistent-frame frame cl hp)
           (valid-method-ptr (method-ptr frame) cl))
  :rule-classes :forward-chaining)


;; (defthm consistent-state-implies-consistent-method-decl
;;   (implies (consistent-state s)
;;            (consistent-method-decl 
;;             (deref-method (method-ptr (current-frame s)) (instance-class-table s))))
;;   :hints (("Goal" :in-theory (e/d () (consistent-method-decl method-ptr
;;                                      consistent-frame valid-method-ptr)))))



; (i-am-here)
;
;; Sun Oct 17 12:33:02 2004

;(i-am-here)

(defthm consp-implies-len
  (implies (consp x)
           (<= 1 (len x)))
  :rule-classes :linear)

;; (defthm consistent-thread-entry-implies-call-stack-len
;;   (implies (consistent-thread-entry th cl hp)
;;            (<= 1 (LEN (THREAD-CALL-STACK th)))))

(defthm consistent-state-current-frame-guard
  (implies (consistent-state s)
           (current-frame-guard s))
  :hints (("Goal" :in-theory (e/d () ())))
  :rule-classes :forward-chaining)



;; (skip-proofs 
;;  (defthm consistent-state-current-frame-guard
;;    (implies (consistent-state s)
;;             (current-frame-guard s))
;;    :rule-classes :forward-chaining))


(defthm wff-call-frame-implies-wff-method-ptr
  (implies (wff-call-frame frame)
           (wff-method-ptr (method-ptr frame)))
  :hints (("Goal" :in-theory (enable wff-call-frame method-ptr)))
  :rule-classes :forward-chaining)

;;; Mon Apr 18 15:45:53 2005
;;; Why we want this current-method definiton here? 
;;; We can only define it after consistent-state is defined! 
;;;

(defun current-method (s)
  (declare (xargs :guard (consistent-state s)))
  (deref-method (method-ptr (current-frame s)) (instance-class-table s)))

(defthm consistent-state-implies-consistent-method-decl
  (implies (consistent-state s)
           (consistent-method-decl 
            (current-method s)))
  :hints (("Goal" :in-theory (e/d (current-method) (consistent-method-decl method-ptr
                                     consistent-frame valid-method-ptr))))
  :rule-classes :forward-chaining)

(defthm current-method-normalization
  (equal (deref-method (method-ptr (current-frame s))
                       (instance-class-table s))
         (current-method s))
  :hints (("Goal" :in-theory (enable current-method))))


(defthm consistent-method-decl-implies-wff-method-decl
  (implies (consistent-method-decl method)
           (wff-method-decl method))
  :rule-classes :forward-chaining)
  
(defthm wff-method-decl-implies-method-accessflags
  (implies (wff-method-decl method)
           (true-listp (method-accessflags method)))
  :hints (("Goal" :in-theory (enable wff-method-decl method-accessflags)))
  :rule-classes :forward-chaining)

(defthm consistent-method-decl-implies-consistent-code-if-not-native-abstract
  (implies (and (consistent-method-decl method)
                (not (mem '*abstract* (method-accessflags method)))
                (not (mem '*native* (method-accessflags method))))
           (consistent-code (method-code method)))
  :hints (("Goal" :in-theory (disable consistent-code method-accessflags)))
  :rule-classes :forward-chaining)

(defthm consistent-code-wff-code
  (implies (consistent-code code)
           (wff-code code))
  :rule-classes :forward-chaining)

(defthm consistent-code-integerp-max-stack
  (implies (consistent-code code)
           (integerp (code-max-Stack code)))
  :rule-classes :forward-chaining)

(defthm consistent-code-integerp-max-local
   (implies (consistent-code code)
            (integerp (code-max-local code)))
   :rule-classes :forward-chaining)

;----------------------------------------------------------------------

(defthm consistent-frame-implies-consistent-opstack
   (implies (consistent-frame frame cl hp)
            (consistent-opstack (operand-stack frame) cl hp))
   :rule-classes :forward-chaining)

(defthm consistent-frame-implies-consistent-locals
   (implies (consistent-frame frame cl hp)
            (consistent-locals (locals frame) cl hp))
   :rule-classes :forward-chaining)

;(i-am-here)  ;; Mon Oct 18 07:25:57 2004

(defthm consistent-opstack-implies-canPop-implies-consistent-value
  (implies (and (consistent-opstack opstack cl hp)
                (canPopCategory1 opstack))
           (consistent-value-x (car opstack) cl hp))
  :hints (("Goal" :in-theory (e/d (top) (consistent-value-x))))
  :rule-classes :forward-chaining)


;----------------------------------------------------------------------


(defthm consistent-state-implies-consistent-heap-init-state-forward
   (implies (consistent-state s)
            (consistent-heap-array-init-state
                              (heap s) (instance-class-table s)
                              (array-class-table s)
                              (heap-init-map (aux s))))
   :hints (("Goal" :in-theory (disable consistent-state CONSISTENT-HEAP-ARRAY-INIT-STATE)
            :expand ((consistent-state s))))
   :rule-classes :forward-chaining)



(defthm consistent-state-implies-consistent-heap-init-state-backward
   (implies (consistent-state s)
            (consistent-heap-array-init-state
                              (heap s) (instance-class-table s)
                              (array-class-table s)
                              (heap-init-map (aux s)))))


;----------------------------------------------------------------------

;; (acl2::i-am-here)

;(i-am-here) ; Sun Oct 17 14:41:19 2004

(local (in-theory (disable method-ptr wff-call-stack wff-method-decl
                           wff-method-ptr wff-code method-ptr
                           method-accessflags
                           wff-call-frame method-code method-maxstack)))

;; Sun Oct 17 14:51:34 2004
;; Note: this is caught by the guard verification

(defthm consistent-method-decl-implies-consistent-code
  (implies (and (consistent-method-decl method)
                (not (mem '*abstract* (method-accessflags method)))
                (not (mem '*native* (method-accessflags method))))
           (consistent-code (method-code method)))
  :hints (("Goal" :in-theory (enable method-code))))

;; (defthm consistent-method-decl-implies-consistent-code
;;   (implies (consistent-method-decl method)
;;            (consistent-code (method-code method)))
;;   :hints (("Goal" :in-theory (enable method-code)))
;;   :rule-classes :forward-chaining)
;;
;; This is not true. using consistent-state definition of today.  I need to
;; assert that the current method is not an abstract or *native*
;; 
;;

(defthm consistent-code-wff-code-b
  (implies (consistent-code code)
           (wff-code code))
  :hints (("Goal" :in-theory (enable consistent-code))))


(defthm consistent-code-integerp-max-stack-b
  (implies (consistent-code code)
           (integerp (code-max-stack code)))
  :hints (("Goal" :in-theory (enable consistent-code))))


(defthm consistent-frame-implies-not-mem-abstract
  (implies (consistent-frame frame cl hp)
           (not (mem '*abstract* 
                     (method-accessflags 
                      (deref-method (method-ptr frame) cl))))))

(defthm consistent-frame-implies-not-mem-abstract-f
  (implies (consistent-frame frame cl hp)
           (not (mem '*abstract* 
                     (method-accessflags 
                      (deref-method (method-ptr frame) cl)))))
  :rule-classes :forward-chaining)



;; (defthm consistent-state-consistent-frame-f
;;   (implies (consistent-state s)
;;            (consistent-frame (current-frame s)
;;                              (instance-class-table s)
;;                              (heap s)))
;;   :rule-classes :forward-chaining)
;;; this should use 


(defthm consistent-state-implies-not-mem-native
  (implies (consistent-state s)
           (not (mem '*abstract* (method-accessflags (current-method s)))))
  :hints (("Goal" :in-theory (disable consistent-state consistent-frame)
           :use ((:instance consistent-frame-implies-not-mem-abstract-f
                            (frame (current-frame s))
                            (cl (instance-class-table s))
                            (hp (heap s)))))))


(defthm consistent-state-implies-max-stack-guard-true
  (implies (and (consistent-state s)
                (not (mem '*native* (method-accessflags 
                                     (current-method s)))))
           (max-stack-guard s))
  :hints (("Goal" :in-theory (e/d () (consistent-state 
                                      consistent-code))))
  :rule-classes :forward-chaining)

;; (skip-proofs 
;;  (defthm consistent-state-implies-max-stack-guard-true
;;    (implies (consistent-state s)
;;             (max-stack-guard s))
;;    :rule-classes :forward-chaining))



(defthm consistent-state-implies-max-stack-integerp
  (implies (and (consistent-state s)
                (not (mem '*native* (method-accessflags (current-method s)))))
           (integerp (max-stack s)))
  :hints (("Goal" :in-theory (e/d (method-maxstack) 
                                  (consistent-state 
                                   code-max-stack
                                   consistent-code))))
   :rule-classes :forward-chaining)

;;; these above is not true. Sun May 16 20:48:11 2004 we need to fix the
;;; consistent-state to assert stronger well-formed-ness on the class-table. 
;;;
;;; TO DO!! Sun May 16 20:48:41 2004
;;;
;;; Mon May 17 11:43:36 2004. The above should be true now.  

(verify-guards safe-pushStack
               :hints (("Goal" :in-theory (disable max-stack-guard
                                                   consistent-state))))

;; ======================================================================
;; 
;; Fri Oct 29 16:55:46 2004


;;; Tue Jun  7 13:28:04 2005
;;(i-am-here)

;;(i-am-here)


  ;;; Tue Jun  7 14:42:28 2005 Hack to get safe-pushCategory2 guard verified!! 
(local 
 (encapsulate ()
    (defthm current-frame-pushStack-is
      (implies (consistent-state s)
               (equal (current-frame (pushStack v s))
                      (frame-set-operand-stack (push v (operand-stack (current-frame s)))
                                               (current-frame s))))
      :hints (("Goal" :in-theory (e/d (current-frame pushStack top
                                                     push-Stack-of-thread
                                                     frame-set-operand-stack)
                                      (topframe-normalization)))))
              


    (defthm wff-call-frame-frame-set-operand-stack-x
      (implies (wff-call-frame frame)
               (wff-call-frame (frame-set-operand-stack (push v (operand-stack
                                                                 frame)) 
                                                        frame)))
      :hints (("Goal" :in-theory (enable wff-call-frame frame-set-operand-stack
                                         operand-stack locals method-ptr
                                         make-frame return-pc wff-method-ptr))))



    (defthm top-push-is
      (equal (top (push frame cs)) frame)
      :hints (("Goal" :in-theory (enable top))))

    (in-theory (disable push top))

    (defthm thread-table-pushStack-is
      (equal (thread-table (pushStack v s))
             (replace-thread-table-entry (thread-by-id (current-thread s)
                                                       (thread-table s))
                                         (push-stack-of-thread 
                                             v (thread-by-id (current-thread s)
                                                             (thread-table s)))
                                         (thread-table s)))
      :hints (("Goal" :in-theory (e/d (pushStack) ())))) 
                                         

    (defthm thread-call-stack-push-stack-of-thread-is
      (equal (THREAD-CALL-STACK  (PUSH-STACK-OF-THREAD v thread))
             (push (frame-set-operand-stack (push v (operand-stack (top
                                                                    (thread-call-stack thread))))
                                            (top (thread-call-stack thread)))
                   (pop (thread-call-stack thread))))
      :hints (("Goal" :in-theory (e/d (push-stack-of-thread) ()))))


    (defthm wff-call-stack-push-frame
      (wff-call-stack (push frame cs))
      :hints (("Goal" :in-theory (enable wff-call-stack push))))))



(verify-guards safe-pushCategory2
               :hints (("Goal" :in-theory (disable consistent-state method-ptr
                                                   method-code))))

;;Tue Jun  7 13:28:06 2005


;----------------------------------------------------------------------



(defthm consistent-state-good-scl
  (IMPLIES (CONSISTENT-STATE S)
           (bcv::good-scl (bcv::scl-jvm2bcv (env-class-table (env s)))))
  :hints (("Goal" :in-theory (e/d (consistent-state) (bcv::good-scl))))
  :rule-classes :forward-chaining)

(defthm consistent-scl-implies-not-super-java-lang-Object-bounded
  (implies (bcv::good-scl scl)
           (NOT
             (BCV::ISCLASSTERM
              (BCV::CLASS-BY-NAME
               (BCV::CLASSSUPERCLASSNAME
                (BCV::CLASS-BY-NAME "java.lang.Object" scl))
               scl))))
  :rule-classes :forward-chaining)

(defthm consistent-scl-implies-not-nil-bounded
  (implies (bcv::good-scl scl)
           (NOT
            (BCV::ISCLASSTERM
             (BCV::CLASS-BY-NAME  nil  scl))))
  :rule-classes :forward-chaining)

;;----------------------------------------------------------------------
;;
;; Thu Jul 21 22:33:17 2005

(skip-proofs 
 (defthm consistent-state-implies-not-bound-hp-init-fact-1
   (implies (consistent-state s)
            (not (bound? nil (heap-init-map (aux s)))))))


(skip-proofs 
 (defthm consistent-state-implies-not-bound-hp-init-fact-2
   (implies (consistent-state s)
            (not (bound? -1 (heap-init-map (aux s)))))))


;; need some efforts. 
;;
;;----------------------------------------------------------------------