File: ADD1-program-correct.improved.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 (1953 lines) | stat: -rw-r--r-- 79,453 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
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
#|
public class First { 
    public static void main(String[] args) {
	int i=1; // modified to i=j 
	int j=i+1;
	i=j;
	return;
    };

};

(defconst *First*
 (make-class-def
      '(class "First"
            "java.lang.Object"
            (constant_pool)
            (fields)
            (methods
                        (method "<init>"
                              (parameters )
                              (returntype void)
                              (accessflags  *class*  *public* )
                              (code
                                   (max_stack 1) (max_locals 1) (code_length 5)
                                   (parsedcode
                                      (0 (aload_0))
                                      (1 (invokespecial
					(methodCP "<init>" "java.lang.Object" () void)))
                                      (4 (return))
                                      (endofcode 5))
                                   (Exceptions )
                                   (StackMap )))
                        (method "main"
                              (parameters (array (class "java.lang.String")))
                              (returntype void)
                              (accessflags  *class*  *public*  *static* )
                              (code
                                   (max_stack 2) (max_locals 3) (code_length 9)
                                   (parsedcode
                                      (0 (iconst_1)) ;; (0 (iload_2)) 
                                      (1 (istore_1))
                                      (2 (iload_1))
                                      (3 (iconst_1))
                                      (4 (iadd))
                                      (5 (istore_2))
                                      (6 (iload_2))
                                      (7 (istore_1))
                                      (8 (return))
                                      (endofcode 9))
                                   (Exceptions )
                                   (StackMap ))))
            (interfaces)
            (accessflags  *class*  *public*  *super*  *synchronized* )
            (attributes
              (attribute "SourceFile")))))
|#

(in-package "M6")


(include-book "../M6/m6-start-jvm")

;;; After proving the following theorem, we disable the definition of m6step.
;;; In some sense to make the only fact ACL2 know about the m6step is the
;;; following. Thus ACL2 will not be side tracked into expanding the definition
;;; of m6step all the time
;;;
;;; The following theorem says: If we know (next-inst s) is a "constant"
;;; already, we know m6step can be expanded in the following ways. 
;;;  

;; (i-am-here) ;; Tue Jul  4 19:14:05 2006

(defthm do-inst-opener
  (implies (consp (next-inst s))
           (equal (m6step s)
                  (let* ((inst (next-inst s))
                         (op   (inst-opcode inst)))
                    (if (no-fatal-error? s)
                        (prog2$ (acl2::cw "thread ~p0 executing inst ~p1~%Current pc ~p2~%"
                                                   (current-thread s) inst (pc s))
                                (if (equal op 'invalid-op-code) ;; shouldn't happen if verified.
                                    (fatalError  "impossible: opcode invalid" s)
                                  (if (equal op 'JVM::INVALID-INST-OFFSET)
                                      (fatalError  "impossible: fall off the method" s)
                                    (case  op 
                                      (NOP        (execute-NOP        inst s))
                                      (ICONST_M1  (execute-ICONST_M1  inst s))
                                      (ICONST_0   (execute-ICONST_0   inst s))
                                      (ICONST_1   (execute-ICONST_1   inst s))
                                      (ICONST_2   (execute-ICONST_2   inst s))
                                      (ICONST_3   (execute-ICONST_3   inst s))
                                      (ICONST_4   (execute-ICONST_4   inst s))
                                      (ICONST_5   (execute-ICONST_5   inst s))
                                      (LCONST_0   (execute-LCONST_0   inst s))
                                      (LCONST_1   (execute-LCONST_1   inst s))
                                    
                                      (ACONST_NULL (execute-ACONST_NULL inst s))
                                      (BIPUSH     (execute-BIPUSH     inst s))
                                      (SIPUSH     (execute-SIPUSH     inst s))
                                      (LDC        (execute-LDC        inst s))             
                                      (ILOAD      (execute-ILOAD      inst s))
                                      (LLOAD      (execute-LLOAD      inst s))
                                      (ALOAD      (execute-ALOAD      inst s))
                                      (ILOAD_0    (execute-ILOAD_0    inst s))
                                      (ILOAD_1    (execute-ILOAD_1    inst s))
                                      (ILOAD_2    (execute-ILOAD_2    inst s))             
                                      (ILOAD_3    (execute-ILOAD_3    inst s))             
                                      (ALOAD_0    (execute-ALOAD_0    inst s))
                                      (ALOAD_1    (execute-ALOAD_1    inst s))
                                      (ALOAD_2    (execute-ALOAD_2    inst s))
                                      (ALOAD_3    (execute-ALOAD_3    inst s))
                                      (IALOAD     (execute-IALOAD     inst s))
                                      (LALOAD     (execute-LALOAD     inst s))
                                      (AALOAD     (execute-AALOAD     inst s))
                                      (BALOAD     (execute-BALOAD     inst s))
                                      (ISTORE     (execute-ISTORE     inst s))
                                      (LSTORE     (execute-LSTORE     inst s))
                                      (ASTORE     (execute-ASTORE     inst s))
                                      (ISTORE_0   (execute-ISTORE_0   inst s))
                                      (ISTORE_1   (execute-ISTORE_1   inst s))
                                      (ISTORE_2   (execute-ISTORE_2   inst s))
                                      (ISTORE_3   (execute-ISTORE_3   inst s))
                                      (LSTORE_0   (execute-LSTORE_0   inst s))
                                      (LSTORE_1   (execute-LSTORE_1   inst s))
                                      (LSTORE_2   (execute-LSTORE_2   inst s))
                                      (LSTORE_3   (execute-LSTORE_3   inst s))
                                      (ASTORE_0   (execute-ASTORE_0   inst s))
                                      (ASTORE_1   (execute-ASTORE_1   inst s))
                                      (ASTORE_2   (execute-ASTORE_2   inst s))
                                      (ASTORE_3   (execute-ASTORE_3   inst s))

                                      (IASTORE    (execute-IASTORE    inst s))
                                    
                                      (LASTORE    (execute-LASTORE    inst s))
                                      (AASTORE    (execute-AASTORE    inst s))
                                      (BASTORE    (execute-BASTORE    inst s))
             
                                      (CALOAD     (execute-CALOAD     inst s))
                                      (CASTORE    (execute-CASTORE    inst s))
                                      (SASTORE    (execute-SASTORE    inst s))
                                      (POP        (execute-POP        inst s))
                                      (POP2       (execute-POP2       inst s))
                                      (DUP        (execute-DUP        inst s))             
                                      (DUP_X1     (execute-DUP_X1     inst s))             
                                      (DUP_X2     (execute-DUP_X2     inst s))
                                      (DUP2       (execute-DUP2       inst s))             
                                      (DUP2_X1    (execute-DUP2_X1    inst s))             
                                      (DUP2_X2    (execute-DUP2_X2    inst s))
                                      (SWAP       (execute-SWAP       inst s))
                                      (IADD       (execute-IADD       inst s))             
                                      (LADD       (execute-LADD       inst s))             
                                      (LCMP       (execute-LCMP       inst s))             
                                      (ISUB       (execute-ISUB       inst s))             
                                      (IMUL       (execute-IMUL       inst s))             
                                      (IDIV       (execute-IDIV       inst s))             
                                      (IREM       (execute-IREM       inst s))
                                      (INEG       (execute-INEG       inst s))             
                                      (IINC       (execute-IINC       inst s))
                                    
                                      (NEWARRAY    (execute-NEWARRAY    inst s))
                                      (ARRAYLENGTH (execute-ARRAYLENGTH inst s))
             
             
                                      (MONITORENTER (execute-MONITORENTER inst s))
                                      (MONITOREXIT  (execute-MONITOREXIT  inst s))
                                    
                                      (CHECKCAST    (execute-CHECKCAST    inst s))
                                    
                                      (CUSTOMCODE (execute-CUSTOMCODE inst s))

                                      (ISTORE_1   (execute-ISTORE_1   inst s))
                                    
                                      (ISTORE_2   (execute-ISTORE_2   inst s))

                                      (IADD       (execute-IADD       inst s))
                                      (NEW        (execute-NEW        inst s))

                                      (LDC        (execute-LDC        inst s))
                                      (INVOKESPECIAL  (execute-INVOKESPECIAL inst s))
                                      (INVOKESTATIC   (execute-INVOKESTATIC inst s))
                                      (INVOKEVIRTUAL   (execute-INVOKEVIRTUAL inst s))
                                      (INVOKEINTERFACE (execute-INVOKEINTERFACE inst s))
                                      (ASTORE_3     (execute-ASTORE_3   inst s))
                                      (ALOAD_0    (execute-ALOAD_0 inst s))

                                      (PUTFIELD   (execute-PUTFIELD inst s))
                                      (GETFIELD   (execute-GETFIELD inst s))

                                      (GETSTATIC  (execute-GETSTATIC inst s))
                                      (PUTSTATIC  (execute-PUTSTATIC inst s))

                                      (CASTORE    (execute-CASTORE   inst s))
                                      (PUTSTATIC  (execute-PUTSTATIC inst s))
                                      (IFNULL     (execute-IFNULL      inst s))
                                      (IFNONNULL  (execute-IFNONNULL   inst s))
                                      (IFEQ       (execute-IFEQ        inst s))
                                      (IFNE       (execute-IFNE        inst s))
                                      (IFLT      (execute-IFLT        inst s))
                                      (IFGE      (execute-IFGE        inst s))
                                      (IFGT      (execute-IFGT        inst s))
                                      (IFLE      (execute-IFLE        inst s))
                                      (IF_ICMPEQ  (execute-IF_ICMPEQ    inst s))
                                      (IF_ICMPNE  (execute-IF_ICMPNE    inst s))
                                      (IF_ICMPLT  (execute-IF_ICMPLT    inst s))
                                      (IF_ICMPGE  (execute-IF_ICMPGE    inst s))
                                      (IF_ICMPGT  (execute-IF_ICMPGT    inst s))
                                      (IF_ICMPLE  (execute-IF_ICMPLE    inst s))
                                      (GOTO       (execute-GOTO         inst s))
                                      (IRETURN    (execute-RETURN       inst s 1))  
                                      (RETURN     (execute-RETURN       inst s 0))
                                      (ARETURN    (execute-RETURN       inst s 1))
                                      (ATHROW     (execute-ATHROW       inst s))
                                      (JSR        (execute-JSR          inst s))
                                      (RET        (execute-RET          inst s))
                                      (t s)))))
                      s))))
                  :hints (("Goal" :in-theory (cons 'm6step (disable   
                                    execute-NOP       
                                    execute-ICONST_M1 
                                    execute-ICONST_0  
                                    execute-ICONST_1  
                                    execute-ICONST_2  
                                    execute-ICONST_3  
                                    execute-ICONST_4  
                                    execute-ICONST_5  
                                    execute-LCONST_0   
                                    execute-LCONST_1   
             			    		       
                                    execute-ACONST_NULL
				    		       
                                    execute-BIPUSH     
                                    execute-SIPUSH     
                                    execute-LDC           
                                    execute-ILOAD      
                                    execute-LLOAD      
                                    execute-ALOAD      
                                    execute-ILOAD_0    
                                    execute-ILOAD_1    
                                    execute-ILOAD_2       
                                    execute-ILOAD_3       
                                    execute-ALOAD_0    
                                    execute-ALOAD_1    
                                    execute-ALOAD_2    
                                    execute-ALOAD_3    
                                    execute-CALOAD
                                    execute-IALOAD     
                                    execute-LALOAD     
                                    execute-AALOAD     
                                    execute-BALOAD     
                                    execute-ISTORE     
                                    execute-LSTORE     
                                    execute-ASTORE     
                                    execute-ISTORE_0   
                                    execute-ISTORE_1   
                                    execute-ISTORE_2   
                                    execute-ISTORE_3   
                                    execute-LSTORE_0   
                                    execute-LSTORE_1   
                                    execute-LSTORE_2   
                                    execute-LSTORE_3   
                                    execute-ASTORE_0   
                                    execute-ASTORE_1   
                                    execute-ASTORE_2   
                                    execute-ASTORE_3   
                                    execute-IASTORE    
                                    execute-LASTORE    
                                    execute-AASTORE    
                                    execute-BASTORE    
                                    execute-CASTORE    
                                    execute-SASTORE    
                                    execute-POP        
                                    execute-POP2       
                                    execute-DUP           
                                    execute-DUP_X1        
                                    execute-DUP_X2     
                                    execute-DUP2          
                                    execute-DUP2_X1       
                                    execute-DUP2_X2    
                                    execute-SWAP       
                                    execute-IADD          
                                    execute-LADD          
                                    execute-LCMP
                                    execute-ISUB          
                                    execute-IMUL          
                                    execute-IDIV          
                                    execute-INEG          
                                    execute-IINC       
             			    		       
                                    execute-NEWARRAY   
                                    execute-ARRAYLENGTH
             			    		       
             			    		       
                                    execute-MONITORENTER
                                    execute-MONITOREXIT
				    		       
                                    execute-CHECKCAST  
				    		       
                                    execute-CUSTOMCODE 
				    		       
                                    execute-ISTORE_1   
				    		       
                                    execute-ISTORE_2   
				    		       
                                    execute-IADD       
                                    execute-NEW        
				    		       
                                    execute-LDC        
                                    execute-INVOKESPECIAL
                                    execute-INVOKESTATIC
                                    execute-INVOKEVIRTUAL
                                    execute-INVOKEINTERFACE
                                    execute-ASTORE_3   
                                    execute-ALOAD_0 
				    		       
                                    execute-PUTFIELD 
                                    execute-GETFIELD
				    		       
                                    execute-GETSTATIC 
                                    execute-PUTSTATIC 
				    		       
                                    execute-CASTORE   
                                    execute-PUTSTATIC 
                                    execute-IFNULL     
                                    execute-IFNONNULL  
                                    execute-IFEQ       
                                    execute-IFNE       
                                    execute-IFLT       
                                    execute-IFGE       
                                    execute-IFGT       
                                    execute-IFLE       
                                    execute-IF_ICMPEQ  
                                    execute-IF_ICMPNE  
                                    execute-IF_ICMPLT  
                                    execute-IF_ICMPGE  
                                    execute-IF_ICMPGT  
                                    execute-IF_ICMPLE  
                                    execute-GOTO       
                                    execute-RETURN     
                                    execute-RETURN     
                                    execute-RETURN     
                                    execute-ATHROW     
                                    execute-JSR    
                                    execute-IREM
                                    execute-RET)))))

(in-theory (disable m6step))

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

; Now we need to effectively reduce the complicated (next-inst s) into our
; intuitive understanding of it. We need to identify under what condition the
; next-inst is simply what we tend to think it is. 


;;; Define a equivalence relation so that we could relatively easily talk about
;;; the next instruction, current-method-ptr, we could succinctly talk about the
;;;  initial condition 

; The definition starts from call frame then going upward until we can define
; an equivalence of state. Because most of our primitives works on state, we
; need such a equivalence to characterize what does NOT change.

(defun equiv-frame (f1 f0)
  (and (equal (return-pc f1) (return-pc f0))
       (equal (method-ptr f1) (method-ptr f0))
       (equal (sync-obj-ref f1) (sync-obj-ref f0))))

;;;  equal except for opstack and locals

(defequiv equiv-frame)   ;; equiv-frame is a equivalence
(defcong equiv-frame equal (return-pc f) 1)
(defcong equiv-frame equal (method-ptr f) 1)
(defcong equiv-frame equal (sync-obj-ref f) 1)

(in-theory (disable equiv-frame))

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

(defun top-frame (thread)
  (top (thread-call-stack thread)))

; We define the concept of top-frame of a thread
; It allows us to take about the concept directly as an primitive.

;;; It would be best if we write our JVM definition in terms of top-frame in
;;; the first place.
;;;
;;; Currently, we add this rewrite rule, so that we don't have to modify the
;;; defintions, else where.
;;; 
;;; ACL2 will handle the conversion "on the fly"

(defthm top-thread-call-stack-rewrite 
  (equal (top (thread-call-stack thread)) (top-frame thread)))

(in-theory (disable top-frame))


; similarly non-top-frame, which we do not care in this proof. 

(defun non-top-frames (thread)
  (pop (thread-call-stack thread)))

(defthm pop-thread-call-stack-rewrite 
  (equal (pop (thread-call-stack thread)) (non-top-frames thread)))

(in-theory (disable non-top-frames))

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

; thread equivalence. 

(defun equiv-thread-except-topframe (t1 t2)
  (and (equal (thread-saved-pc t1) (thread-saved-pc t2))
       (equal (thread-mref t1) (thread-mref t2))
       (equal (thread-mdepth t1) (thread-mdepth t2))
       (equal (thread-ref t1) (thread-ref t2))
       (equal (thread-id t1) (thread-id t2))
       (equal (thread-state t1) (thread-state t2))
       (equiv-frame (top-frame t1)
                    (top-frame t2))))

;; Note: We do not need to assert anything about non-top-frame!  We are only
;; talking about a straightline code that runs in one frame.
;;

(defequiv equiv-thread-except-topframe)

(defcong equiv-thread-except-topframe equiv-frame (top-frame t1) 1)
(defcong equiv-thread-except-topframe equal (thread-id t1) 1)
(defcong equiv-thread-except-topframe equal (thread-state t1) 1)
(defcong equiv-thread-except-topframe equal (thread-saved-pc t1) 1)
(defcong equiv-thread-except-topframe equal (thread-mref t1) 1)
(defcong equiv-thread-except-topframe equal (thread-mdepth t1) 1)
(defcong equiv-thread-except-topframe equal (thread-ref t1) 1)

(in-theory (disable equiv-thread-except-topframe thread-mref thread-state
                    thread-id thread-saved-pc))


(defun equiv-thread-table (tl1 tl2)
  (cond ((endp tl1) (endp tl2))
        ((endp tl2) nil)
        ((equiv-thread-except-topframe (car tl1) (car tl2))
         (equiv-thread-table (cdr tl1) (cdr tl2)))
        (t nil)))

(defequiv equiv-thread-table)


(defcong equiv-thread-table equiv-thread-except-topframe (thread-by-id id tt)
  2)


(defcong equiv-thread-table equal (search-active-thread-in-range s e tt) 3)
;;;  This is used for proving round-robin run return the same result on the
;;;  equiv-state


(defun equiv-state (s1 s0)
  (and (equiv-thread-table (thread-table s1) (thread-table s0))
       (equal (current-thread s1) (current-thread s0))
       (equal (error-flag s1)     (error-flag s0))
       (equal (class-table s1)    (class-table s0))
       (equal (env s1)            (env s0))
       (equal (aux s1)            (aux s0))))

(defequiv equiv-state)
(defcong equiv-state equiv-thread-table (thread-table t1) 1)
(defcong equiv-state equal (current-thread t1) 1)
(defcong equiv-state equal (error-flag t1) 1)
(defcong equiv-state equal (env t1) 1)
(defcong equiv-state equal (aux t1) 1)
(defcong equiv-state equal (instance-class-table t1) 1
  :hints (("Goal" :in-theory (enable instance-class-table))))
(defcong equiv-state equal (array-class-table t1) 1
  :hints (("Goal" :in-theory (enable array-class-table))))


(defcong equiv-state equiv-frame (current-frame s) 1
  :hints (("Goal" :in-theory (enable current-frame))))

(defcong equiv-state equal (no-fatal-error? s) 1
  :hints (("Goal" :in-theory (enable no-fatal-error?))))


(in-theory (disable equiv-state))

(defcong equiv-state equal (current-method-ptr s) 1
  :hints (("Goal" :in-theory (enable current-method-ptr))))


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

(defthm non-top-frame-make-thread
  (equal (non-top-frames (make-thread id pc (cons new-frame frames) status mref
                                      mdep th-ref))
         frames)
  :hints (("Goal" :in-theory (list* 'non-top-frames 
                                    (disable POP-THREAD-CALL-STACK-REWRITE)))))

(defthm  top-frame-make-thread
  (equal (top-frame (make-thread id pc (cons new-frame frames) status mref
                                      mdep th-ref))
         new-frame)
  :hints (("Goal" :in-theory (list* 'top-frame 
                                    (disable TOP-THREAD-CALL-STACK-REWRITE)))))

(in-theory (disable aux))


(in-theory (disable thread-mdepth thread-ref))

(defthm equiv-thread-except-topframe-thread-primitives
  (and (equiv-thread-except-topframe (push-stack-of-thread v thread)
                                     thread)
       (equiv-thread-except-topframe (popStack-of-thread thread)
                                     thread)
       (equiv-thread-except-topframe (set-local-at-of-thread i v thread)
                                     thread))
  :hints (("Goal" :in-theory (enable equiv-thread-except-topframe
                                     push-stack-of-thread
                                     popStack-of-thread
                                     set-local-at-of-thread
                                     equiv-frame))))

; We introduced the push-stack-of-thread, popStack-of-thread as an immediate
; primitives as a bridge between pushStack with push, popStack with pop.



(defthm equiv-thread-except-topframe-thread-primitives-2
  (and (equal (equiv-thread-except-topframe (push-stack-of-thread v thread)
                                            thread) t)
       (equal (equiv-thread-except-topframe (popStack-of-thread thread)
                                            thread) t)
       (equal (equiv-thread-except-topframe (set-local-at-of-thread i v thread)
                                            thread) t)))


;;;  Ideally we may define another equivalence relation to exactly capture the 
;;;  effect of push-stack-of-thread, so property as follows will be expressed
;;;  as congruence rules on that equivalence. 
;;;
;;;  This means that we need a hierachy of equivalence. 

(defthm equal-non-top-frames-frame-op-no-change
  (and (equal (non-top-frames (push-stack-of-thread v thread))
              (non-top-frames thread))
      (equal (non-top-frames (popStack-of-thread thread))
             (non-top-frames thread))
      (equal (non-top-frames (set-local-at-of-thread i v thread))
             (non-top-frames thread)))
  :hints (("Goal" :in-theory (e/d (non-top-frames push-stack-of-thread
                                   popStack-of-thread set-local-at-of-thread)               
                                  (POP-THREAD-CALL-STACK-REWRITE
                                   top-thread-call-stack-rewrite)))))

                         

(in-theory (disable push-stack-of-thread))
(in-theory (disable popStack-of-thread))
(in-theory (disable set-local-at-of-thread))

; Disable those definitions. Now to ACL2, those become primitives (just like
; those to the operator of ACL2 ---  us user)

(defthm equiv-thread-table-replace-thread-table-entry
  (implies (equiv-thread-except-topframe new-thread old-thread)
           (equiv-thread-table (replace-thread-table-entry old-thread
                                                           new-thread tt)
                               tt)))

(defthm opstack-local-primitives-preserve-equiv-state-2
  (and (equiv-state (pushStack v s) s)
       (equiv-state (popStack s) s)
       (equiv-state (state-set-local-at i v s) s)
       (equiv-state (state-set-pc npc s) s))
  :hints (("Goal" :in-theory (enable equiv-state))))



(defthm opstack-local-primitives-preserve-equiv-state-3
   (and (implies (equiv-state s1 s0)
                 (equal (equiv-state (pushStack v s1) s0) t))

        (implies (equiv-state s1 s0)
                 (equal (equiv-state (popStack s1) s0) t))

        (implies (equiv-state s1 s0)
                 (equal (equiv-state (state-set-local-at i v s1) s0) t))
        
        (implies (equiv-state s1 s0)
                 (equal (equiv-state (state-set-pc npc s1) s0) t))))

;;;;; This above is some workaround for the limitation of ACL2.


(defthm pc-state-set-pc
  (equal (pc (state-set-pc ip s))
         ip))

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

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

(defthm pc-state-set-local-at
  (equal (pc (state-set-local-at i v s))
         (pc s)))


(defthm pc-popStackN
  (equal (pc (popStackN n s))
         (pc s)))


(defthm state-set-pc-state-set-pc
  (equal (state-set-pc pc1 (state-set-pc pc2 s))
         (state-set-pc pc1 s))
  :hints (("Goal" :in-theory (enable state-set-pc))))


(in-theory (disable pushStack popStack state-set-local-at state-set-pc))

(in-theory (disable inst-opcode)) 

(defthm no-fatal-error?-state-set
  (and (equal (no-fatal-error? (popStack s))
              (no-fatal-error? s))
       (equal (no-fatal-error? (pushStack v s))
              (no-fatal-error? s))
       (equal (no-fatal-error? (state-set-local-at i v s))
              (no-fatal-error? s))
       (equal (no-fatal-error? (state-set-pc npc s))
              (no-fatal-error? s))))

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

;; (program)
;; (acl2::set-guard-checking nil)

;; (include-book "../M6-DJVM-shared/cldc-classtable")
;; ; (acl2::set-guard-checking t)

;; (defconst *ip* 0)
;; (defconst *th* 0)
;; (defconst *h*  nil)
;; (defconst *tt* nil)
;; (defconst *dcl* nil)
;; (defconst *s* (make-state *ip* *th* *h* *tt* *dcl* (make-env
;;                                                     JVM::|*OUT/CLDC-CLASS-TABLE*|)
;;                           nil
;;                           nil))

;; (acl2::set-guard-checking nil)
;; (defconst *s1* (round-robin-run (setup-initial-state "First" '() *s*) 6))

;; (acl2::set-guard-checking t)
;; (logic)
;; (defun init-state ()
;;   *s1*)


(include-book "../M6-DJVM-shared/cldc-classtable")

(include-book "ADD1-init-state")

; define the init-state
; In fact this is not the state that we want to property on. 
; Our proof is about all states that is in some sense equivalent to this
; initial state. 
;
; Thus the init-state is only a way to specified the states that we want to
; prove properties on. 

;----------------------------------------------------------------------
;
; Start reasoning about the next-inst.  
;
; The goal to reduce the next instruction to the intuitive idea of "next"
; instruction in the code stream.
;

;;; property of initial state.
(defun first-method-ptr ()
  '(METHOD-PTR "First"
               "main" ((ARRAY "java.lang.String"))
               VOID))


(defun theMethod ()
  '(METHOD "First" "main"
           (JVM::PARAMETERS (ARRAY "java.lang.String"))
           (JVM::RETURNTYPE . VOID)
           (JVM::ACCESSFLAGS *CLASS* *PUBLIC* *STATIC*)
           (JVM::CODE (JVM::MAX_STACK . 2)
                      (JVM::MAX_LOCAL . 3)
                      (JVM::CODE_LENGTH . 9)
                      (JVM::PARSEDCODE 
                                      ;;(0 (iconst_1))
                                      (0 (iload_2))
                                      (1 (istore_1))
                                      (2 (iload_1))
                                      (3 (iconst_1))
                                      (4 (iadd))
                                      (5 (istore_2))
                                      (6 (iload_2))
                                      (7 (istore_1))
                                      (8 (return))
                                      (JVM::ENDOFCODE 9))
                      (JVM::EXCEPTIONS)
                      (JVM::STACKMAP))))

(defthm deref-method-first-method-ptr
  (equal (deref-method (first-method-ptr) (instance-class-table (init-state)))
         (theMethod)))

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

(defthm init-state-current-thread
  (equal (current-thread (init-state)) 0))

(defthm init-state-current-method-ptr
  (equal (current-method-ptr (init-state))
         (first-method-ptr))
  :hints (("Goal" :in-theory (enable (init-state)))))

(defthm init-state-no-fatal-error?
  (no-fatal-error? (init-state))
  :hints (("Goal" :in-theory (enable (init-state)))))

; property of the "initial state"

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

(in-theory (disable init-state (init-state) (first-method-ptr)
                    first-method-ptr no-fatal-error?))


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

(in-theory (disable next-inst))

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


(defthm equiv-state-init-state-next-inst
  (implies (equiv-state s (init-state))
           (equal (next-inst s)
                  (inst-by-offset (pc s) (theMethod))))
  :hints (("Goal" :in-theory (enable next-inst))))

; this is the first example theorem refered in the paper 
;        "Java Program Verification via a JVM Deep Embedding in ACL2"

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



(defthm first-is-correct-lemma
  (implies (and (equiv-state  s1 (init-state))
                (equal (pc s1) 2))
           (equiv-state (m6step (m6step (m6step s1)))
                        (init-state))))

(defthm first-is-correct
  (implies (and (equiv-state  s1 (init-state))
                (equal (pc s1) 2))
           (equiv-state (m6step s1)
                        (init-state))))

(defthm simple-run-opener
  (implies (syntaxp (quotep n))
           (equal (simple-run s n)
                  (IF (ZP N)
                      S (SIMPLE-RUN (M6STEP S) (- N 1))))))

(in-theory (disable int-fix))

(defthm first-is-correct-1
  (implies (and (equiv-state  s1 (init-state))
                (equal (pc s1) 0))
           (equiv-state (simple-run s1 7)
                        (init-state))))

;; Some show case theorems: during proof development. Those above are used to
;; guide the search for a proper set of lemma to "train" ACL2.

;; Wed Jan  7 23:33:15 2004
;; 
;; Basically says that straight line code's exeuction perserve the equiv-state
;; From equiv-state we could derive a lot of properties --- What is not changing!!
;; 
;;;;;;;;;;;;;;


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

(defthm round-robin-run-opener
  (implies (or (equal (round-robin-schedule s) 0) 
               (zp n))
           (equal (round-robin-run s n)
                  (if (zp n)
                      s
                    (let ((cid (current-thread s)))
                      (if (equal cid -1)
                          (prog2$ (acl2::cw "NO ACTIVE THREAD!~%") s)
                        (prog2$ (acl2::cw "Executing Thread ~p0 Instruction ~p1~%" cid (next-inst s))
                                (let* ((sn (m6step s))
                                       (nid  (round-robin-schedule sn))
                                       (ccid (current-thread sn)))
                                  (if (equal nid -1)
                                      (prog2$ (acl2::cw "Continue executing ~p0~%" ccid)
                                              (round-robin-run sn (- n 1)))
                                    (if (not (equal nid cid))
                                        (prog2$ (acl2::cw 
                                                 "~%~%***************~%switch from THREAD ~p0 to THREAD~p1 ~%**************~%~%" 
                                                 cid nid )
                                                (round-robin-run 
                                                 (loadExecutionEnvironment nid 
                                                                           (storeExecutionEnvironment
                                                                            (state-set-current-thread cid sn))) (- n 1)))
                                      (round-robin-run sn (- n 1)))))))))))
  :hints (("Goal" :in-theory (disable loadexecutionenvironment
                                      storeexecutionenvironment
                                      inst-opcode do-inst-opener)
           :expand (round-robin-run s n))))

(in-theory (disable round-robin-run))

; Like Do-instr-opener, Decide when should ACL2 should use the definition of
; round-robin-run

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

;; about round robin schedule.
(defthm round-robin-schedule-init-state
  (equal (round-robin-schedule (init-state)) 0)
  :hints (("Goal" :in-theory (enable (init-state)))))

(defcong equiv-thread-table equal (len s1) 1
  :hints (("Goal" :in-theory (enable equiv-thread-table))))


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

;; we need this to prove round-robin-schedule 
(defcong equiv-state equal (round-robin-schedule s) 1
  :hints (("Goal" :in-theory (enable round-robin-schedule))))

(in-theory (disable loadexecutionenvironment storeexecutionenvironment round-robin-schedule))

(in-theory (disable int-fix))

(defthm equal-round-robin-schedule-0
  (implies (equiv-state s (init-state))
           (equal (round-robin-schedule s) 0)))



(defthm first-is-correct-3
  (implies (and (equiv-state  s1 (init-state))
                (equal (pc s1) 2))
           (equiv-state (round-robin-run s1 4)
                        (init-state))))

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

;; Note (round-robin-run s1 4)
;; is expanded into 
;;            (STATE-SET-PC
;;                 6
;;                    (POPSTACK
;;                     (STATE-SET-LOCAL-AT
;;                      2
;;                      (pop (OPERAND-STACK
;;                            (CURRENT-FRAME
;;                             (STATE-SET-PC
;;                              5
;;                              (PUSHSTACK
;;                               (INT-FIX
;;                                (+
;;                                 (pop  (OPERAND-STACK
;;                                        (CURRENT-FRAME
;;                                         (STATE-SET-PC
;;                                          4
;;                                          (PUSHSTACK
;;                                           1
;;                                           (STATE-SET-PC 3
;;                                                         (PUSHSTACK (NTH 1 (LOCALS (CURRENT-FRAME S1)))
;;                                                                    S1)))))))
;;                                 (CAR
;;                                  (OPERAND-STACK
;;                                   (CURRENT-FRAME
;;                                    (POPSTACK
;;                                     (STATE-SET-PC
;;                                      4
;;                                      (PUSHSTACK
;;                                       1
;;                                       (STATE-SET-PC
;;                                        3
;;                                        (PUSHSTACK (NTH 1 (LOCALS (CURRENT-FRAME S1)))
;;                                                   S1))))))))))
;;                               (POPSTACK
;;                                (POPSTACK
;;                                 (STATE-SET-PC
;;                                  4
;;                                  (PUSHSTACK
;;                                   1
;;                                   (STATE-SET-PC 3
;;                                                 (PUSHSTACK (NTH 1 (LOCALS (CURRENT-FRAME S1)))
;;                                                            S1)))))))))))
;;                      (STATE-SET-PC
;;                       5
;;                       (PUSHSTACK
;;                        (INT-FIX
;;                         (+
;;                          (CAR
;;                           (OPERAND-STACK
;;                            (CURRENT-FRAME
;;                             (STATE-SET-PC
;;                              4
;;                              (PUSHSTACK
;;                               1
;;                               (STATE-SET-PC 3
;;                                             (PUSHSTACK (NTH 1 (LOCALS (CURRENT-FRAME S1)))
;;                                                        S1)))))))
;;                          (CAR
;;                           (OPERAND-STACK
;;                            (CURRENT-FRAME
;;                             (POPSTACK
;;                              (STATE-SET-PC
;;                               4
;;               (PUSHSTACK
;;                  1
;;                  (STATE-SET-PC 3
;;                                (PUSHSTACK (NTH 1 (LOCALS (CURRENT-FRAME S1)))
;;                                           S1))))))))))
;;        (POPSTACK
;;         (POPSTACK
;;          (STATE-SET-PC
;;             4
;;             (PUSHSTACK
;;                  1
;;                  (STATE-SET-PC 3
;;                                (PUSHSTACK (NTH 1 (LOCALS (CURRENT-FRAME S1)))
;;                                           S1)))))))))))


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

;; Fri Jan  9 00:12:15 2004
;; Why this? 
;; We need the following to prove 
;; (topStack (state-set-pc ....)) is (topStack s)

;;; Theorems about  what is not changing. The proofs are trival. 
;;; ACL2 learns the "quick" facts.

(defthm current-frame-state-set
  (and (equal (current-frame (state-set-pc npc s))
              (current-frame s))
       (equal (current-frame (state-set-heap heap s))
              (current-frame s))
       (equal (current-frame (state-set-error-flag errflg s))
              (current-frame s))
       (equal (current-frame (state-set-class-table classtable s))
              (current-frame s))
       (equal (current-frame (state-set-aux aux s))
              (current-frame s)))
  :hints (("Goal" :in-theory (enable state-set-pc))))


(defthm current-thread-exists?-state-set
  (and (equal (current-thread-exists? (state-set-pc npc s))
              (current-thread-exists? s))
       (equal (current-thread-exists? (state-set-heap heap s))
              (current-thread-exists? s))
       (equal (current-thread-exists? (state-set-error-flag errflg s))
              (current-thread-exists? s))
       (equal (current-thread-exists?(state-set-class-table classtable s))
              (current-thread-exists? s))
       (equal (current-thread-exists? (state-set-aux aux s))
              (current-thread-exists? s)))
  :hints (("Goal" :in-theory (enable state-set-pc))))


;----------------------------------------------------------------------
;;; A failed attempt in characterizing the proof efforts 

;; 
;;
;;  ...
;; Wed Jan  7 23:35:22 2004 
;; ;;; 
;; ;;; Why wff-state??

;; (defthm wff-state-state-set-pc
;;   (implies (wff-state s)
;;            (wff-state (state-set-pc npc s)))
;;   :hints (("Goal" :in-theory (enable state-set-pc))))


(defthm topStack-of-pushStack-of-thread
  (equal (car (operand-stack (top-frame (push-stack-of-thread v thread))))
         v)
  :hints (("Goal" :in-theory (enable push-stack-of-thread))))


(defthm topStack-of-thread-set-local-at
  (equal (car (operand-stack (top-frame (set-local-at-of-thread i v thread))))
         (car (operand-stack (top-frame thread))))
  :hints (("Goal" :in-theory (enable set-local-at-of-thread))))


;----------------------------------------------------------------------
;
;  Some counter intuitive cases from the our choice of implementation 
;
;
;; the following should be true!
;;; 
;;; Some counter intuitive cases from the our choice of implementation 
;;;
;; (skip-proofs
;;  (defthm topStack-state-set-local
;;    (equal (topStack (state-set-local-at i v s))
;;           (topStack s))
;;    :hints (("Goal" :in-theory (enable state-set-local-at)))))

;; 
;; But we may want to limit ourselves to only talking about 'valid cases'
;; 

;;; this is not true!! because we need unique-id-thread-table!!


;; (defthm replace-thread-table-entry-thread-by-id
;;   (implies (and (wff-thread-table tt)
;;                 (unique-id-thread-table tt)
;;                 (equal (thread-id old-thread) id)
;;                 (equal (thread-id new-thread) id)
;;                 (thread-exists? id tt))
;;            (equal (thread-by-id id (replace-thread-table-entry
;;                                      old-thread
;;                                      new-thread tt))
;;                   new-thread))
;;   :hints (("Goal" :in-theory (enable wff-thread-table wff-thread))))

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

(defcong equiv-thread-except-topframe
         equiv-thread-except-topframe (set-local-at-of-thread i v thread) 3)


(defthm thread-by-id-replace-thread-table-entry
  (let ((old-thread (thread-by-id tid tt)))
  (implies (and (equal (thread-id new-thread)  tid)
                (thread-exists? tid tt))
           (equal (thread-by-id tid
                                (replace-thread-table-entry 
                                 old-thread new-thread tt))
                  new-thread))))

(defthm thread-id-wff-thread-table
  (implies (and (wff-thread-table tt)
                (thread-exists? id tt))
           (equal (thread-id (thread-by-id id tt))
                  id)))

(defthm thread-id-is-set-local-at-of-thread
  (equal (thread-id (set-local-at-of-thread i v thread))
         (thread-id thread)))



;;; Thu Jan  8 16:23:15 2004
;;
;; problemetic that 
;; 
;; Without the explicit rewrite rule, ACL2 will not prove the following. 
;; WHY? congruence rule should rewrite ....

(defthm not-thread-exists?-thread-by-id-nil
  (implies (not (thread-exists? id tt))
           (equal (thread-by-id id tt) nil)))


(defthm wff-thread-table-replace-nil-not-changed
  (implies (wff-thread-table tt)
           (equal (thread-by-id id (replace-thread-table-entry nil any tt))
                  (thread-by-id id tt)))
  :hints (("Goal" :in-theory (enable wff-thread-table))))


(defthm topStack-state-set-local
  (implies (wff-thread-table (thread-table s))
           (equal (topStack (state-set-local-at i v s))
                  (topStack s)))
  :hints (("Goal" :in-theory (e/d (state-set-local-at current-frame current-thread-exists?)
                                  (set-local-at-of-thread thread-exists?))
           :cases ((current-thread-exists? s)))))

(defthm topStack-state-set-pc 
  (equal (topStack (state-set-pc npc s))
         (topStack s)))


;; (defthm topStack-of-pushStack 
;;   (equal (topStack (pushStack v s))
;;          v)
;;   :hints (("Goal" :in-theory (enable pushStack current-frame))))
;;
;;; Thu Jan  8 00:07:33 2004 this is not true!!
;;;
;;; because the way we wrote our replace-thread-table-entry 
;;;


(defthm topStack-of-pushStack 
   (implies (and (current-thread-exists? s)
                 (wff-thread-table (thread-table s)))
            (equal (topStack (pushStack v s))
                   v))
   :hints (("Goal" :in-theory (enable pushStack current-frame current-thread-exists?))))

;; now we need to prove other operations perserve wff-thread-table and
;; current-thread-exists?


(in-theory (disable  POP-THREAD-CALL-STACK-REWRITE
                     TOP-THREAD-CALL-STACK-REWRITE))


;; Why we need the following??

;; ;; (defthm wff-thread-implies-push-top-frame-non-top-frames
;; ;;   (implies (wff-thread thread)
;; ;;            (equal (cons  (top-frame thread)
;; ;;                          (non-top-frames thread))
;; ;;                   (thread-call-stack thread)))
;; ;;   :hints (("Goal" :in-theory (enable top-frame non-top-frames
;; ;;                                      wff-thread))))
                 

;; ;; (defthm wff-thread-implies-top-frame
;; ;;   (implies (wff-thread thread)
;; ;;            (wff-call-frame (top-frame thread)))
;; ;;   :hints (("Goal" :in-theory (list* 'topx-frame 'wff-thread 'topx
;; ;;                                    (disable top-thread-call-stack-rewrite)))))


;; ;; (defthm wff-call-frame-implies-equal
;; ;;   (implies (wff-call-frame frame)
;; ;;            (EQUAL (MAKE-FRAME (RETURN-PC FRAME)
;; ;;                               (OPERAND-STACK FRAME)
;; ;;                               (LOCALS FRAME)
;; ;;                               (METHOD-PTR FRAME)
;; ;;                               (SYNC-OBJ-REF FRAME))
;; ;;                   FRAME)))

;; ;; (in-theory (disable wff-call-frame))



;; ;; (in-theory (enable  POP-THREAD-CALL-STACK-REWRITE
;; ;;                     TOP-THREAD-CALL-STACK-REWRITE))

;; Because we want the following to be exactly equal?


;; (skip-proofs
;;  (defthm pop-stack-of-thread-push-stack-of-thread
;;    (implies (wff-thread thread)
;;             (equal (popStack-of-thread (push-stack-of-thread v thread))
;;                    thread))
;;    :hints (("Goal" :in-theory (enable popStack-of-thread 
;;                                       push-stack-of-thread
;;                                       wff-thread)))))




;; (defun th-mem (th tt)
;;   (mem th tt))

;; (defthm not-thread-id-equal-implies-not-mem
;;   (implies (and (not (mem (thread-id thread) 
;;                           (collect-thread-id tt))))
;;            (not (mem thread tt)))))

;; ;; (in-theory (disable id-mem))

;; (defthm mem-not-mem-thread
;;   (let ((id (thread-id new-thread)))
;;     (implies (and (unique-id-thread-table tt)
;;                   (thread-exists? (thread-id new-thread) tt)
;;                   (not (equal (thread-by-id id tt)
;;                               new-thread)))
;;              (not (mem new-thread tt))))
;;     :hints (("Goal" :in-theory (enable thread-exists?))))

;; (in-theory (disable not-thread-id-equal-implies-not-mem))

;; (defthm mem-not-th-mem-thread
;;   (let ((id (thread-id new-thread)))
;;     (implies (and (unique-id-thread-table tt)
;;                   (thread-exists? (thread-id new-thread) tt)
;;                   (not (equal (thread-by-id id tt)
;;                               new-thread)))
;;              (not (th-mem new-thread tt))))
;;   :hints (("Goal" :in-theory (disable unique-id-thread-table))))

;; (in-theory (disable mem-not-mem-thread th-mem))


;; (defthm thread-exists?-mem-thread-by-id
;;   (implies (thread-exists? id tt)
;;            (th-mem (thread-by-id id tt)
;;                    tt))
;;   :hints (("Goal" :in-theory (enable thread-exists? th-mem))))



;; (defthm replace-thread-table-entry-replace-thread-table-entry
;;   (implies (and (not (th-mem c tt))
;;                 (th-mem a tt)
;;                 (not (equal a c)))
;;            (equal (replace-thread-table-entry c b 
;;                                               (replace-thread-table-entry a c tt))
;;                   (replace-thread-table-entry a b tt)))
;;   :hints (("Goal" :in-theory (enable th-mem))))

;; (defthm replace-thread-table-entry-replace-thread-table-entry-2
;;   (equal (replace-thread-table-entry c c tt) 
;;          tt))

;; (defthm replace-thread-table-entry-replace-thread-table-entry-x
;;   (implies (and (equal c a)
;;                 (th-mem a tt))
;;            (equal (replace-thread-table-entry c b 
;;                                               (replace-thread-table-entry a c tt))
;;                   (replace-thread-table-entry a b tt)))
;;   :hints (("Goal" :in-theory (enable th-mem))))


;; (defthm not-equal-thread-call-stack-implies-not-equal
;;   (implies (not (equal (operand-stack (top (thread-call-stack s2)))
;;                        (operand-stack (top (thread-call-stack s1)))))
;;            (not (equal s2 s1))))


;; (defthm not-equal-pushStack-of-thread
;;   (not (equal (push-stack-of-thread v thread)
;;               thread))
;;   :hints (("Goal" :in-theory (enable push-stack-of-thread)
;;            :use ((:instance  not-equal-thread-call-stack-implies-not-equal
;;                              (s2 (push-stack-of-thread v thread))
;;                              (s1 thread))))))




;; (skip-proofs
;;  (defthm popStack-pushStack-1
;;    (implies (wff-state s)
;;             (equal (popStack (pushStack v s))
;;                    s))
;;    :hints (("Goal" :cases ((current-thread-exists? s))
;;             :in-theory (enable popStack pushStack))
;;            ("Subgoal 1''" :in-theory (enable current-thread-exists?)))))


(defthm thread-primitives-state-set-pc
  (and (equal (popStack (state-set-pc npc s))
              (state-set-pc npc (popStack s)))
       (equal (pushStack v  (state-set-pc npc s))
              (state-set-pc npc (pushStack v s)))
       (equal (state-set-local-at i v (state-set-pc npc s))
              (state-set-pc npc (state-set-local-at i v s))))
  :hints (("Goal" :in-theory (enable state-set-pc popStack pushStack
                                     state-set-local-at
                                     state-set-thread-table))))

;;; move pushStack, popStack inside ....


;; this is not true, because of the norm-state.
;; Maybe I should introduce a normal state predicate.

(defthm locals-unchanged-by
  (and (equal (locals (top-frame (push-stack-of-thread v thread)))
              (locals (top-frame thread)))
       (equal (locals (top-frame (popStack-of-thread  thread)))
              (locals (top-frame thread))))
  :hints (("Goal" :in-theory (enable push-stack-of-thread
                                     top-frame
                                     popStack-of-thread))))


(defthm locals-of-set-local-at
  (equal (locals (top-frame (set-local-at-of-thread i v thread)))
         (update-nth i v (locals (top-frame thread))))
  :hints (("Goal" :in-theory (enable set-local-at-of-thread top-frame))))


(in-theory (enable  POP-THREAD-CALL-STACK-REWRITE
                     TOP-THREAD-CALL-STACK-REWRITE))

(defthm local-at-accessor-2
  (implies (and (wff-thread-table (thread-table s))
                (current-thread-exists? s))
           (equal (local-at i (state-set-local-at j v s))
                  (if (equal (nfix i) (nfix j))
                      v
                    (local-at i s))))
  :hints (("Goal" :in-theory (enable state-set-local-at local-at current-frame 
                                     current-thread-exists?))))
          

;; (defthm local-at-accessor-2
;;   (implies (and (current-thread-exists? s)
;;                 (wff-thread-table (thread-table s)))
;;            (equal (local-at i (state-set-local-at j v s))
;;                   (if (equal (nfix i) (nfix j))
;;                       v
;;                     (local-at i s))))
;;   :hints (("Goal" :in-theory (enable state-set-local-at local-at current-frame 
;;                                      current-thread-exists?))))

(defthm local-at-accessor-1
  (and (implies (wff-thread-table (thread-table s))
                (equal (local-at i (popStack s)) 
                       (local-at i s)))
       (implies (wff-thread-table (thread-table s))
                (equal (local-at i (pushStack v s))
                       (local-at i s)))
       (implies (wff-thread-table (thread-table s))
                (equal (local-at i (state-set-pc npc s))
                       (local-at i s))))
       :hints (("Goal" :in-theory (enable popStack pushStack 
                                          current-frame current-thread-exists?)
                :cases ((current-thread-exists? s)))))


(in-theory (disable local-at topStack int-fix))

(in-theory (disable wff-state init-state m6step round-robin-run (init-state)))


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

(defcong equiv-state equiv-state (pushStack v s) 2)
(defcong equiv-state equiv-state (popStack s) 1)
(defcong equiv-state equiv-state (state-set-local-at i v s) 3)

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


(include-book "../M6-DJVM-shared/wff-data-structure")

;; Identify the domain

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



(defthm wff-thread-push-stack-of-thread
  (wff-thread (push-stack-of-thread v thread))
  :hints (("Goal" :in-theory (enable push-stack-of-thread))))

(defthm wff-call-frame-top-frame-push-stack-of-thread
  (implies (wff-call-frame-regular (top-frame thread))
           (wff-call-frame-regular (top-frame (push-stack-of-thread v thread))))
  :hints (("Goal" :in-theory (enable push-stack-of-thread make-frame
                                     wff-call-frame return-pc operand-stack
                                     locals method-ptr sync-obj-ref))))


(defthm wff-thread-table-prevserved-by-pushStack
  (implies (wff-thread-table (thread-table s))
           (wff-thread-table (thread-table (pushStack v s))))
  :hints (("Goal" :in-theory (enable pushStack))))

(defthm wff-call-frame-current-frame-prevserved-by-pushStack
  (implies (and (wff-call-frame-regular (current-frame s))
                (wff-thread-table (thread-table s))
                (current-thread-exists? s))
           (wff-call-frame-regular (current-frame (pushStack v s))))
  :hints (("Goal" :in-theory (enable pushStack current-thread-exists? current-frame))))

(defthm wff-thread-pop-stack-of-thread
  (wff-thread (popStack-of-thread  thread))
  :hints (("Goal" :in-theory (enable popStack-of-thread))))


(defthm wff-call-frame-top-frame-pop-stack-of-thread
  (implies (wff-call-frame-regular (top-frame thread))
           (wff-call-frame-regular (top-frame (popStack-of-thread thread))))
  :hints (("Goal" :in-theory (enable popStack-of-thread make-frame
                                     wff-call-frame return-pc operand-stack
                                     locals method-ptr sync-obj-ref))))


(defthm wff-call-frame-current-frame-prevserved-by-popStack
  (implies (and (wff-call-frame-regular (current-frame s))
                (wff-thread-table (thread-table s))
                (current-thread-exists? s))
           (wff-call-frame-regular (current-frame (popStack s))))
  :hints (("Goal" :in-theory (enable popStack current-thread-exists? current-frame))))


(defthm wff-thread-table-prevserved-by-popStack
  (implies (wff-thread-table (thread-table s))
           (wff-thread-table (thread-table (popStack s))))
  :hints (("Goal" :in-theory (enable popStack))))


(defthm wff-thread-set-local-at-of-thread
  (wff-thread (set-local-at-of-thread i v  thread))
  :hints (("Goal" :in-theory (enable set-local-at-of-thread))))


(defthm wff-call-frame-top-frame-set-local-at-of-thread
  (implies (wff-call-frame-regular (top-frame thread))
           (wff-call-frame-regular (top-frame (set-local-at-of-thread i v  thread))))
  :hints (("Goal" :in-theory (enable set-local-at-of-thread make-frame
                                     wff-call-frame return-pc operand-stack
                                     locals method-ptr sync-obj-ref))))


(defthm wff-call-frame-current-frame-prevserved-by-state-set-local-at
  (implies (and (wff-call-frame-regular (current-frame s))
                (wff-thread-table (thread-table s))
                (current-thread-exists? s))
           (wff-call-frame-regular (current-frame (state-set-local-at i v  s))))
  :hints (("Goal" :in-theory (enable state-set-local-at current-thread-exists? current-frame))))


(defthm wff-thread-table-prevserved-by-set-local
   (implies (wff-thread-table (thread-table s))
            (wff-thread-table (thread-table (state-set-local-at i v s))))
   :hints (("Goal" :in-theory (enable state-set-local-at))))


(defthm current-thread-exists-prevserved-by-pushStack
  (implies (current-thread-exists? s)
           (current-thread-exists? (pushStack v s)))
  :hints (("Goal" :in-theory (enable current-thread-exists? 
                                     pushStack))))

(defthm current-thread-exists-prevserved-by-popStack
   (implies (current-thread-exists? s)
            (current-thread-exists? (popStack s)))
   :hints (("Goal" :in-theory (enable current-thread-exists? popStack))))

(defthm current-thread-exists-prevserved-by-set-local
   (implies (current-thread-exists? s)
            (current-thread-exists? (state-set-local-at i v s)))
   :hints (("Goal" :in-theory (enable current-thread-exists? state-set-local-at))))

;----------------------------------------------------------------------
   
;; We really want this (popStack (pushStack v s)) == s!
;; However this is not always true. Only if s is wff-state-strong in some sense.!!

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


(defthm consp-call-stack-implies-cons-top-frame-non-top-frame
  (implies (consp (thread-call-stack thread))
           (equal (cons (top-frame thread)
                        (non-top-frames thread))
                  (thread-call-stack thread)))
  :hints (("Goal" :in-theory (e/d (top-frame non-top-frames)
                                  (top-thread-call-stack-rewrite
                                   pop-thread-call-stack-rewrite)))))


(defthm wff-thread-implies-push-top-frame-non-top-frames
  (implies (wff-call-frame (top-frame thread))
           (consp (thread-call-stack thread)))
  :hints (("Goal" :in-theory (e/d (top-frame wff-call-frame)
                                  (top-thread-call-stack-rewrite)))))
                 

(defthm pop-stack-of-thread-push-stack-of-thread
  (implies (and (wff-call-frame-regular (top-frame thread))
                (wff-thread-regular thread))
           (equal (popStack-of-thread (push-stack-of-thread v thread))
                  thread))
  :hints (("Goal" :in-theory (enable popStack-of-thread 
                                     push-stack-of-thread
                                     wff-call-frame
                                     wff-thread))))


;; (defthm wff-state-regular-set-tt
;;   (implies (and (equal tt (thread-table s))
;;                 (wff-state-regular s))
;;            (equal (state-set-thread-table tt s)
;;                   s))
;;   :hints (("Goal" :in-theory (enable state-set-thread-table wff-state 
;;                                      make-state pc current-thread  heap
;;                                      thread-table class-table error-flag aux env))))

;; ;; this proof is hard because the our implementation of
;; ;; replace-thread-table-entry 

;; (defthm state-set-thread-table-set-tt
;;   (equal (state-set-thread-table tt1 (state-set-thread-table tt2 s))
;;          (state-set-thread-table tt1 s))
;;   :hints (("Goal" :in-theory (enable state-set-thread-table))))


(in-theory (disable wff-call-frame-regular thread-exists?))


(defthm thread-by-id-wff-thread-table
  (implies (and (thread-exists? id tt)
               (wff-thread-table tt))
          (wff-thread (thread-by-id id tt)))
  :hints (("Goal" :in-theory (enable wff-thread-table thread-exists?))))


(defthm thread-by-id-wff-thread-table-regular
  (implies (and (thread-exists? id tt)
               (wff-thread-table-regular tt))
           (wff-thread-regular (thread-by-id id tt)))
  :hints (("Goal" :in-theory (enable wff-thread-table-regular thread-exists?))))



;; (defthm current-thread-exists-wff-thread-table
;;   (implies (and (current-thread-exists? s)
;;                 (wff-thread-table (thread-table s)))
;;            (wff-thread (thread-by-id (current-thread s)
;;                                      (thread-table s))))
;;   :hints (("Goal" :in-theory (enable current-thread-exists?))))




(defthm collect-thread-id-replace-is-not-changed
  (implies (equal (thread-id new) (thread-id old))
           (equal (collect-thread-id (replace-thread-table-entry old new tt))
                  (collect-thread-id tt))))

(defthm unique-id-thread-table-replace-entry
  (implies (and (unique-id-thread-table tt)
                (equal (thread-id new) (thread-id old)))
           (unique-id-thread-table (replace-thread-table-entry old new tt))))

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

(defthm replace-replace-is-replace
  (implies (and (unique-id-thread-table tt)
                (equal (thread-id thread2) id)
                (equal (thread-id thread1) id)
                (thread-by-id id tt))
           (equal (replace-thread-table-entry thread1 thread2
                       (replace-thread-table-entry (thread-by-id id tt) thread1 tt))
                  (replace-thread-table-entry (thread-by-id id tt) thread2 tt))))



(defthm replace-equal-is-equal
  (equal (replace-thread-table-entry old old tt)
         tt))

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

(in-theory (disable unique-id-thread-table))

(defthm wff-thread-table-regular-implies-wff-thread-table
  (implies (wff-thread-table-regular tt)
           (wff-thread-table tt))
  :hints (("Goal" :in-theory (enable wff-thread-table))))


(defthm popStack-pushStack-is
   (implies (and (current-thread-exists? s)
                 (wff-state-regular s)
                 (unique-id-thread-table    (thread-table s))
                 (wff-call-frame-regular    (current-frame s))
                 (wff-thread-table-regular  (thread-table s)))
            (equal (popStack (pushStack v s))
                    s))
  :hints (("Goal" :in-theory (enable current-frame
                                     thread-exists?
                                     current-thread-exists? 
                                     state-set-thread-table
                                     pushStack popStack topStack))))

; finally, we have what I wanted. 
;
;----------------------------------------------------------------------

(defthm wff-state-regular-pushStack
  (implies (wff-state-regular s)
           (wff-state-regular (pushStack v s)))
  :hints (("Goal" :in-theory (enable pushStack state-set-thread-table wff-state
                                     make-state pc aux current-thread heap
                                     thread-table class-table env
                                     error-flag))))

(defthm unique-id-thread-table-pushStack
  (implies (unique-id-thread-table (thread-table s))
           (unique-id-thread-table (thread-table (pushStack v s))))
  :hints (("Goal" :in-theory (enable pushStack state-set-thread-table))))



(defthm wff-state-regular-popStack
  (implies (wff-state-regular s)
           (wff-state-regular (popStack s)))
  :hints (("Goal" :in-theory (enable popStack state-set-thread-table wff-state
                                     make-state pc aux current-thread heap
                                     thread-table class-table env error-flag))))

(defthm unique-id-thread-table-popStack
  (implies (unique-id-thread-table (thread-table s))
           (unique-id-thread-table (thread-table (popStack s))))
  :hints (("Goal" :in-theory (enable popStack state-set-thread-table))))



(defthm wff-state-regular-state-set-local
  (implies (wff-state-regular s)
           (wff-state-regular (state-set-local-at i v s)))
  :hints (("Goal" :in-theory (enable state-set-local-at state-set-thread-table wff-state
                                     make-state pc aux current-thread heap
                                     thread-table class-table env error-flag))))

(defthm unique-id-thread-table-state-set-local
  (implies (unique-id-thread-table (thread-table s))
           (unique-id-thread-table (thread-table (state-set-local-at i v s))))
  :hints (("Goal" :in-theory (enable state-set-local-at state-set-thread-table))))


(defthm wff-thread-table-regular-replace-regular
  (implies (and (wff-thread-table-regular tt)
                (wff-thread-regular new))
           (wff-thread-table-regular (replace-thread-table-entry old new tt))))


(defthm wff-thread-regular-push-stack-of-thread
  (implies (wff-thread-regular thread)
           (wff-thread-regular (push-stack-of-thread v thread)))
  :hints (("Goal" :in-theory (enable push-stack-of-thread))))

(defthm wff-thread-regular-pop-stack-of-thread
  (implies (wff-thread-regular thread)
           (wff-thread-regular (popstack-of-thread thread)))
  :hints (("Goal" :in-theory (enable popstack-of-thread))))

(defthm wff-thread-regular-state-set-local-at
  (implies (wff-thread-regular thread)
           (wff-thread-regular (set-local-at-of-thread i v thread)))
  :hints (("Goal" :in-theory (enable set-local-at-of-thread))))

(defthm wff-thread-table-regular-pushStack
  (implies (and (wff-thread-table-regular (thread-table s))
                (current-thread-exists? s))
           (wff-thread-table-regular (thread-table (pushStack v s))))
  :hints (("Goal" :in-theory (enable pushStack current-thread-exists?))))


(defthm wff-thread-table-regular-popStack
  (implies (and (wff-thread-table-regular (thread-table s))
                (current-thread-exists? s))
           (wff-thread-table-regular (thread-table (popStack s))))
  :hints (("Goal" :in-theory (enable popStack current-thread-exists?))))



(defthm wff-thread-table-regular-state-set-local-at
  (implies (and (wff-thread-table-regular (thread-table s))
                (current-thread-exists? s))
           (wff-thread-table-regular (thread-table (state-set-local-at i v s))))
  :hints (("Goal" :in-theory (enable state-set-local-at current-thread-exists?))))

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

(defthm first-is-correct-4
  (let ((old (local-at 2 s1)))
  (implies (and (equiv-state s1 (init-state))
                (current-thread-exists? s1)
                (wff-state-regular s1)
                (wff-thread-table-regular (thread-table s1))
                (wff-call-frame-regular (current-frame s1))
                (unique-id-thread-table (thread-table s1))
                (equal (pc s1) 0)
                (integerp old))
           (equal (local-at 2 (round-robin-run s1 7))
                  (int-fix (+ 1 old)))))
  :hints (("Goal" :in-theory (disable unique-id-thread-table))))


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


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

;; EXTENDED DEMO: STRAIGHT LINE CODE!!

#|
(defconst *FirstX*
 (make-class-def
      '(class "FirstX"
            "java.lang.Object"
            (constant_pool)
            (fields)
            (methods
                        (method "<init>"
                              (parameters )
                              (returntype void)
                              (accessflags  *class*  *public* )
                              (code
                                   (max_stack 1) (max_locals 1) (code_length 5)
                                   (parsedcode
                                      (0 (aload_0))
                                      (1 (invokespecial
					(methodCP "<init>" "java.lang.Object" () void)))
                                      (4 (return))
                                      (endofcode 5))
                                   (Exceptions )
                                   (StackMap )))
                        (method "main"
                              (parameters (array (class "java.lang.String")))
                              (returntype void)
                              (accessflags  *class*  *public*  *static* )
                              (code
                                   (max_stack 2) (max_locals 3) (code_length 14)
                                   (parsedcode
                                      (0 (iload_2)) 
                                      (1 (istore_1))
                                      (2 (iload_1))
                                      (3 (iconst_1))
                                      (4 (iadd))
                                      (5 (istore_2))
                                      (6 (iload_2))
                                      (7 (istore_1))
                                      (8 (iconst_2))
                                      (9 (iload_2))
                                      (10 (iload_1))
                                      (11 (iadd))
                                      (12 (iadd))
                                      (13 (istore_2))
                                      (14 (return))
                                      (endofcode 14))
                                   (Exceptions )
                                   (StackMap ))))
            (interfaces)
            (accessflags  *class*  *public*  *super*  *synchronized* )
            (attributes
              (attribute "SourceFile")))))

|#
;----------------------------------------------------------------------

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

(defun first-method-ptr-x ()
  '(METHOD-PTR "FirstX"
               "main" ((ARRAY "java.lang.String"))
               VOID))


(defun theMethod-x ()
  '(METHOD "FirstX" "main"
        (PARAMETERS (ARRAY "java.lang.String"))
        (RETURNTYPE . VOID)
        (ACCESSFLAGS *CLASS* *PUBLIC* *STATIC*)
        (CODE (MAX_STACK . 2)
              (MAX_LOCAL . 3)
              (CODE_LENGTH . 14)
              (PARSEDCODE (0 (ILOAD_2))
                          (1 (ISTORE_1))
                          (2 (ILOAD_1))
                          (3 (ICONST_1))
                          (4 (IADD))
                          (5 (ISTORE_2))
                          (6 (ILOAD_2))
                          (7 (ISTORE_1))
                          (8 (ICONST_2))
                          (9 (ILOAD_2))
                          (10 (ILOAD_1))
                          (11 (IADD))
                          (12 (IADD))
                          (13 (ISTORE_2))
                          (14 (RETURN))
                          (ENDOFCODE 14))
              (EXCEPTIONS)
              (STACKMAP))))

(defthm deref-method-first-method-ptr-x
  (equal (deref-method (first-method-ptr-x) (instance-class-table (init-state-x)))
         (theMethod-x)))

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


(defthm init-state-current-thread-x
  (equal (current-thread (init-state-x)) 0))

(defthm init-state-current-method-ptr-x
  (equal (current-method-ptr (init-state-x))
         (first-method-ptr-x))
  :hints (("Goal" :in-theory (enable (init-state-x)))))

(defthm init-state-no-fatal-error?-x
  (no-fatal-error? (init-state-x))
  :hints (("Goal" :in-theory (enable (init-state-x)))))

;----------------------------------------------------------------------
(in-theory (disable first-method-ptr-x (first-method-ptr-x) theMethod-x init-state-x (init-state-x)))
;----------------------------------------------------------------------

(defthm round-robin-schedule-init-state-x
  (equal (round-robin-schedule (init-state-x)) 0)
  :hints (("Goal" :in-theory (enable (init-state-x)))))


(defthm equal-round-robin-schedule-0-x
  (implies (equiv-state s (init-state-x))
           (equal (round-robin-schedule s) 0)))


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


(defthm equiv-state-init-state-next-inst-x
  (implies (equiv-state s (init-state-x))
           (equal (next-inst s)
                  (inst-by-offset (pc s) (theMethod-x))))
  :hints (("Goal" :in-theory (enable next-inst))))

;; ;----------------------------------------------------------------------
;;
;; (defthm first-is-correct-4-x
;;   (let ((old (local-at 2 s1)))
;;   (implies (and (equiv-state s1 (init-state-x))
;;                 (current-thread-exists? s1)
;;                 (wff-state-regular s1)
;;                 (wff-thread-table-regular (thread-table s1))
;;                 (wff-call-frame-regular (current-frame s1))
;;                 (unique-id-thread-table (thread-table s1))
;;                 (equal (pc s1) 0)
;;                 (integerp old))
;;            (equal (local-at 2 (round-robin-run s1 7))
;;                   (int-fix (+ 1 old)))))
;;   :hints (("Goal" :in-theory (disable unique-id-thread-table))))
;;
;; ;----------------------------------------------------------------------

(include-book "arithmetic-2/meta/top" :dir :system)

(include-book "arithmetic-2/floor-mod/floor-mod" :dir :system)

(defthm int-fix-int-fix-plus
  (implies (and (integerp x)
                (integerp y))
           (equal (int-fix (+ x (int-fix y)))
                  (int-fix (+ x y))))
   :hints (("Goal" :in-theory (enable int-fix))))


(defthm mod-n-a-multpliy-n
  (implies (and (integerp n)
                (integerp y))
           (equal (mod (* n y) n)
                  0))
  :hints (("Goal" :in-theory (enable mod))))


(defthm mod-plus
  (implies (and (integerp x)
                (integerp n)
                (integerp y))
           (equal (mod (+ x (mod y n)) n)
                  (mod (+ x y) n))))


(defthm mod-plus-collorary
  (implies (and (integerp x)
                (integerp n)
                (integerp y))
           (equal (mod (+ (mod x n) (mod y n)) n)
                  (mod (+ x y) n))))


(defthm mod-n-multiply-y-plus
  (implies (and (integerp x)
                (integerp n)
                (integerp y))
           (equal (mod (+ x (* n y)) n)
                  (mod x n))))

;; (defthm mod-n-multiply-specific
;;   (implies (and (integerp x)
;;                 (integerp i)
;;                 (integerp i0))
;;            (equal (mod (* x (+ I (* 4294967296 i0))) 4294967296)
;;                   (mod (* x i) 4294967296))))

(defthm integerp-multiply
  (implies (and (integerp i)
                (integerp x))
           (integerp (* i x))))

(defthm int-fix-multiply
  (implies (and (integerp x)
                (integerp y))
           (equal (int-fix (* x (int-fix y)))
                  (int-fix (* x y))))
  :hints (("Goal" :in-theory (e/d (int-fix)))))



;; (skip-proofs
;;  (defthm int-fix-int-fix-plus
;;    (equal (int-fix (+ x (int-fix y)))
;;           (int-fix (+ x y)))
;;    :hints (("Goal" :in-theory (enable int-fix)))))

;; (skip-proofs
;;  (defthm int-fix-multiply
;;    (equal (int-fix (* x (int-fix y)))
;;           (int-fix (* x y)))
;;    :hints (("Goal" :in-theory (enable int-fix)))))



(defthm first-is-correct-5-x
  (let ((old (local-at 2 s1)))
  (implies (and (equiv-state s1 (init-state-x))
                (current-thread-exists? s1)
                (wff-state-regular s1)
                (wff-thread-table-regular (thread-table s1))
                (wff-call-frame-regular (current-frame s1))
                (unique-id-thread-table (thread-table s1))
                (equal (pc s1) 0)
                (integerp old))
           (equal (local-at 2 (round-robin-run s1 14))
                  (INT-FIX (+ 4 (* 2 old))))))
  :hints (("Goal" :in-theory (disable unique-id-thread-table))))