This file is indexed.

/usr/share/acl2-7.2dfsg/books/misc/mult.lisp is in acl2-books-source 7.2dfsg-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
(in-package "ACL2")

#|

Matt Wilding (http://hokiepokie.org) Feb 2002

This ACL2 script solves a challenge posed by Bill Legato, to prove
that a program written for the Mostek 6502 microprocessor correctly
implements multiplication.  The program is described in

A Weakest Precondition Model for Assembly Language Programs

by Bill Legato, dated June 19, 2000.  (A later version of this paper
also containing the program is available, which is currently FOUO.)

Comments from the 2.5x version of this script:

xx In this script we solve this challenge in the following steps:
xx
xx
xx 1. Model the program in ACL2 using a stobj to represent state,
xx adding a definition for each line of the program, and combining the
xx lines into a program.
xx
xx 2. Describe the algorithm abstractly in a function "loop-spec".
xx
xx 3. Prove the loop-spec works.
xx
xx 4. Prove that our Mostek 6502 program model works like loop-spec.
xx
xx 5. Combine the lemmas of 3. and 4. into the final theorem.
xx
xx Solving the challenge problem required 15 hours.
xx
xx   1. Understanding the algorithm                1 hr
xx   2. Modeling the program in ACL2               2 hrs
xx   3. Wasting time working on the proof before
xx      remembering yet again that decomposing
xx      proofs is *always* the right thing to do   4 hrs.
xx   4. Proving that loop-spec works               3 hrs
xx   5. Proving that loop-spec works like the
xx      real program                               3.5 hrs
xx   6. Cleaning and documenting                   1.5 hrs
xx                                                 -------
xx                                                 15 hrs
xx
xx
xx The proof runs in under a minute under ACL2 2.5x, a version of ACL2
xx 2.5 enhanced with nu-rewriter patches.  The proof uses Super-IHS,
xx the Rockwell Collins ACL2 book that extends IHS.  It would take a
xx couple hours of work to port this proof to just-released 2.6, since
xx I'd have to identify the handful of Super-IHS lemmas that are
xx employed here and add their proof to the proof script.  (If anyone
xx wants me to do this, please let me know.)

The proof has now been ported to ACL2 2.6.  The port to 2.6 required
about 3.5 hours of work, plus 1 hour for rearranging the lemmas
sensibly and documenting.  Some Super-IHS rules were ripped out of
Super-IHS and put here so as to make this script depend only on
distibuted books.

While I was at porting the proof, I spent some time reorganizing and
simplifying.  Most importantly, I eliminated the use of a spec
function to decompose the proof in this version.  The spec function
was important to me during the proof development, and is generally the
right thing to do on these kinds of proofs.  But once I knew what the
inductions were, etc., I was able to combine the two proof steps in
this case into one.  The resulting proof is far less pedagogical, but
it's so much shorter that I could not resist.

It's great to see that the techniques Greve and I use for microcode
proofs apply so directly to assembly code reasoning.

Matt Wilding
Christmas 2001
(ported to ACL2 2.6 - 3 Jan 2002)
(updated to avoid nu-rewriter use - 27 Feb 2002)

|#

;; Set up ACL2 how we like.

(set-irrelevant-formals-ok t)

;; We load the IHS book, and some other useful books, that are part of
;; the standard 2.6 distribution.
(include-book "arithmetic/top-with-meta"     :dir :system)
(include-book "ihs/ihs-definitions"          :dir :system)
(include-book "ihs/ihs-lemmas"               :dir :system)
(include-book "data-structures/structures"   :dir :system)
(include-book "data-structures/array1"       :dir :system)
(include-book "ihs/@logops"                  :dir :system)
(include-book "data-structures/list-defuns"  :dir :system)
(include-book "data-structures/list-defthms" :dir :system)
(include-book "data-structures/deflist"      :dir :system)
(include-book "data-structures/defalist"     :dir :system)
(include-book "meta-lemmas")   ;Always include last!

(minimal-ihs-theory)

(in-theory (enable @logops-theory array1-lemmas meta-lemma-theory))

(enable-theory (definition-free-theory (theory 'list-defuns)))
(enable-theory (definition-free-theory (theory 'alist-defuns)))

(in-theory (disable (force)))

(in-theory (enable nth-update-nth))

;;
;; Some induction schemes we'll need, courtesy of the Super-IHS book.
;;

(defun sub1-induction (x)
  (if (zp x)
      x
    (sub1-induction (1- x))))

(defun sub1-logcdr-induction (m x)
  (if (zp m)
      x
    (sub1-logcdr-induction (1- m) (logcdr x))))

(defun logcdr-logcdr-induction (b c)
  (declare (xargs :measure (abs (ifix b))
                  :hints (("goal" :in-theory (enable logcdr)))))
  (if (or (equal b -1) (zip b))
      c
    (logcdr-logcdr-induction (logcdr b) (logcdr c))))

(defun sub1-sub1-logcdr-induction (a b v)
  (if (zp b)
      (or a v)
    (sub1-sub1-logcdr-induction (1- a) (1- b) (logcdr v))))

(defun sub1-logcdr-logcdr-induction (m x y)
  (if (zp m)
      (or x y)
    (sub1-logcdr-logcdr-induction (1- m) (logcdr x) (logcdr y))))

(defun sub1-sub1-induction (m n)
  (if (zp m)
      n
    (sub1-sub1-induction (1- m) (1- n))))

(defun sub1-logcdr-logcdr-carry-induction (m x y c)
  (if (zp m)
      (or x y c)
    (sub1-logcdr-logcdr-carry-induction
     (1- m)
     (logcdr x)
     (logcdr y)
     (if (or (and (equal (logcar x) 1) (equal (logcar y) 1))
             (and (equal (logcar x) 1) (equal c 1))
             (and (equal (logcar y) 1) (equal c 1)))
         1 0))))

;; A couple definitions used in the reader-generated type rules,
;; courtesy of the Super-IHS book
(defthm unsigned-byte-p-<=
  (equal (unsigned-byte-p bits i)
         (and (integerp bits)
              (>= bits 0)
              (integerp i)
              (>= i 0)
              (<= i (1- (expt 2 bits)))))
  :rule-classes :definition
  :hints (("goal" :in-theory (enable unsigned-byte-p))))

(defthm signed-byte-p-<=
  (equal (signed-byte-p bits i)
         (and (integerp bits)
              (> bits 0)
              (integerp i)
              (>= i (- (expt 2 (- bits 1))))
              (<= i (1- (expt 2 (- bits 1))))))
  :rule-classes :definition :hints
  (("goal" :in-theory (enable signed-byte-p))))

(in-theory (disable unsigned-byte-p-<= signed-byte-p-<=))

;; The following ~30 rules are IHS-related.  Some were in the earlier
;; version of this proof.  Many are stolen from Super-IHS.  Some of
;; the more important Super-IHS rules have names starting with *ark*.

(defthm ash-*2-simplify
  (implies
   (and
    (integerp x)
    (integerp n)
    (<= 0 n))
   (equal (ash (* 2 x) n) (* 2 (ash x n))))
  :hints (("goal" :in-theory (enable ash))))

(in-theory (disable ash-*2-simplify))

(defthm *ark*-ifix-ash
  (equal (ifix (ash x y)) (ash x y)))

(defthm *ark*-open-logcons
  (implies (syntaxp (constant-syntaxp b))
           (equal (logcons b i)
                  (let ((b (bfix b)) (i (ifix i)))
                    (+ b (* 2 i)))))
  :hints
  (("goal" :in-theory (enable logcons))))

(defthm *ark*-sum-constants
  (implies (and (syntaxp (constant-syntaxp x))
                (syntaxp (constant-syntaxp y))
                (equal sum (+ x y)))
           (equal (+ x y z) (+ sum z))))

(defthm *ark*-ash-+-pos
  (implies (and (integerp x)
                (integerp y)
                (integerp m)
                (<= 0 m))
           (equal (ash (+ x y) m)
                  (+ (ash x m) (ash y m))))
  :hints
  (("goal" :in-theory
    (enable logops-recursive-definitions-theory)
    :induct (sub1-induction m))))

(in-theory (disable *ARK*-ASH-+-POS))

(defthm logtail-+-ash
  (implies
   (and (integerp n1) (integerp n2) (integerp x) (integerp y)
        (<= 0 n1) (<= n1 n2) )
  (equal
   (logtail n1 (+ x (ash y n2)))
   (+ (logtail n1 x) (ash y (- n2 n1)))))
  :hints (("goal" :in-theory
           (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY logtail*)
           :induct (sub1-sub1-logcdr-induction n2 n1 x))))

(defthm logcdr-logapp
  (implies
   (and
    (integerp n)
    (integerp x)
    (integerp y)
    (< 0 n))
   (equal (logcdr (logapp n x y))
          (logapp (1- n) (logcdr x) y)))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-logcdr-induction n x))))

(defthm logcdr-loghead
  (implies
   (and
    (integerp n)
    (integerp x)
    (< 0 n))
   (equal (logcdr (loghead n x))
          (loghead (1- n) (logcdr x))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-logcdr-induction n x))))


(defthm *ark*-logcar-i+j+2*k
     (implies
      (and (integerp i)
           (integerp j)
           (integerp k))
      (equal (logcar (+ i j (* 2 k)))
             (logcar (+ i j))))
     :hints
     (("Goal"
       :use ((:instance logcar-i+2*j (i (+ i j)) (j k))))))

(defthm *ark*-logcar-+
     (implies
      (and (integerp i)
           (integerp j))
      (equal (logcar (+ i j))
             (b-xor (logcar i) (logcar j))))
     :hints
     (("Goal" :in-theory (enable b-xor))))

(defthm *ark*-loghead-1
  (implies
   (integerp x)
   (equal (loghead 1 x) (logcar x)))
  :hints (("goal" :in-theory (enable loghead logcons))))

(defthm unsigned-byte-p-+
  (implies
   (and
    (unsigned-byte-p n x)
    (unsigned-byte-p n y))
   (unsigned-byte-p (1+ n) (+ x y)))
  :hints (("goal" :in-theory (enable unsigned-byte-p expt))))

(defthm unsigned-byte-p-logcdr-bridge
  (implies
   (unsigned-byte-p 9 x)
   (unsigned-byte-p 8 (logcdr x)))
  :hints (("goal" :in-theory (enable unsigned-byte-p))))

(defthm unsigned-byte-p-logcdr-bridge-2
  (implies
   (unsigned-byte-p 8 x)
   (unsigned-byte-p 7 (logcdr x)))
  :hints (("goal" :in-theory (enable unsigned-byte-p))))

(defthm *ark*-+*
  (implies
   (and (not (zip a)) (not (equal a -1))
        (not (zip b)))
   (equal
    (+ a b)
    (logcons
     (b-xor (logcar a) (logcar b))
     (+ (b-and (logcar a) (logcar b))
        (logcdr a) (logcdr b)))))
   :hints (("goal" :induct (logcdr-logcdr-induction a b)
            :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY b-and b-xor))))

(in-theory (disable *ark*-+*))

(defthm *ark*-logcdr-+1
  (implies
   (integerp x)
  (and
   (implies
    (equal (logcar x) 0)
    (equal (logcdr (+ 1 x)) (logcdr x)))
   (implies
    (equal (logcar x) 1)
    (equal (logcdr (+ 1 x)) (+ 1 (logcdr x))))
   (implies
    (and
     (equal (logcar x) 1)
     (integerp y))
    (equal (logcdr (+ 1 x y)) (+ 1 (logcdr x) (logcdr y))))
   (implies
    (and
     (equal (logcar x) 1)
     (integerp y))
    (equal (logcdr (+ 1 y x)) (+ 1 (logcdr x) (logcdr y))))))
  :hints (("goal" :in-theory (enable *ark*-+*))))

(defthm unsigned-byte-p-+-helper
  (implies
   (and
    (unsigned-byte-p n x)
    (unsigned-byte-p n y)
    (unsigned-byte-p 1 c)
    (integerp n)
    (< 0 n))
   (equal
    (unsigned-byte-p n (+ x y c))
    (not (logbitp n (+ x y c)))))
  :rule-classes nil
  :hints (("goal" :in-theory (enable logops-recursive-definitions-theory *ark*-+* logbitp*)
           :induct (sub1-logcdr-logcdr-carry-induction n x y c))))

(defthm *ark*-unsigned-byte-p-+
  (implies
   (and
    (unsigned-byte-p n x)
    (unsigned-byte-p n y)
    (integerp n)
    (< 0 n))
   (equal
    (unsigned-byte-p n (+ x y))
    (not (logbitp n (+ x y)))))
  :hints (("goal" :use (:instance unsigned-byte-p-+-helper (c 0)))))

(in-theory (disable associativity-of-logapp))

;; Super-IHS associates logapp in a way that now seems backwards to
;; me.  I shut off associativity-of-logapp, and prove a rule that
;; works "forwards", just to keep myself sane.
(defthm logapp-logapp
  (implies
   (and
    (integerp n1) (integerp n2)
    (integerp a) (integerp b) (integerp c)
    (<= 0 n2) (<= n2 n1))
   (equal
    (logapp n1 (logapp n2 a b) c)
    (logapp n2 a (logapp (- n1 n2) b c))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-sub1-logcdr-induction n1 n2 a))))

(defthm equal-logapp-x
  (implies
   (and (integerp n) (integerp x) (integerp y) (integerp z) (<= 0 n)
        (equal (loghead n z) (loghead n x)))
   (equal
    (equal (logapp n x y) z)
    (equal y (logtail n z))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-logcdr-logcdr-induction n x z))))

(defthm loghead-+-simple
  (implies
   (and
    (integerp x) (integerp y) (integerp n)
    (<= 0 n)
    (equal (loghead n x) 0))
   (equal
    (loghead n (+ x y))
    (loghead n y)))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-logcdr-logcdr-induction n x y))))

(defthm logtail-+-simple
  (implies
   (and
    (integerp x) (integerp y) (integerp n)
    (<= 0 n)
    (equal (loghead n x) 0))
   (equal
    (logtail n (+ x y))
    (+ (logtail n x) (logtail n y))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-logcdr-logcdr-induction n x y))))

(defthm *ark*-loghead-ash-pos-rewrite
  (implies
   (and
    (integerp n1)
    (integerp n2)
    (integerp x)
    (<= 0 n1)
    (<= 0 n2))
  (equal (loghead n1 (ash x n2))
         (if (<= n1 n2)
             0
           (ash (loghead (- n1 n2) x) n2))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-sub1-induction n1 n2))))

(defthm logtail-ash-pos-rewrite
  (implies
   (and
    (integerp n1)
    (integerp n2)
    (integerp x)
    (<= 0 n1)
    (<= 0 n2))
  (equal (logtail n1 (ash x n2))
         (if (<= n1 n2)
             (ash x (- n2 n1))
           (logtail (- n1 n2) x))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-sub1-induction n1 n2))))

(defthm loghead-*2
  (implies
   (and
    (integerp n) (< 0 n) (integerp x))
   (equal
    (equal (loghead n (* 2 x)) 0)
    (equal (loghead (1- n) x) 0)))
  :hints (("goal" :in-theory (enable loghead*))))


(defthm logcar-identity
  (implies (unsigned-byte-p 1 x)
           (equal (logcar x) x))
  :hints
  (("goal" :in-theory
    (enable unsigned-byte-p logcar))))

(defthm unsigned-byte-p-logcar
  (unsigned-byte-p 1 (logcar x))
  :hints
  (("goal" :in-theory
    (enable unsigned-byte-p logcar))))

(defthm logapp-1-logcar-logcdr
  (implies
   (integerp x)
   (equal (logapp 1 (logcar x) (logcdr x))
          x))
  :hints (("goal" :in-theory (enable logapp))))

(defthm unsigned-byte-p-1-
  (implies
   (and
    (integerp n) (<= 0 n)
    (unsigned-byte-p n x))
   (equal
    (unsigned-byte-p n (1- x))
    (< 0 x)))
  :hints (("goal" :in-theory (enable unsigned-byte-p))))

(defthm +-ash-logapp
  (implies
   (and (integerp x) (integerp n) (integerp a) (integerp b)
        (<= 0 n))
   (equal (+ (ash x n) (logapp n a b))
          (logapp n a (+ x b))))
  :hints (("goal" :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY)
           :induct (sub1-logcdr-induction n a))))

(defthm equal-logcar-1
  (equal (equal (logcar x) 1)
         (not (equal (logcar x) 0)))
  :hints (("goal" :in-theory (enable logcar))))

(defthm logapp-loghead
  (implies
   (and (integerp n1) (integerp n2) (integerp x) (integerp y)
        (<= 0 n1) (<= n1 n2))
   (equal
    (logapp n1 (loghead n2 x) y)
    (logapp n1 x y)))
  :hints (("goal" :induct (sub1-sub1-logcdr-induction n1 n2 x)
           :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY))))

(defthm logtail-almost-all
  (implies
   (and
    (unsigned-byte-p (1+ n) x)
    (integerp n) (<= 0 n))
   (equal (logtail n x) (logbit n x)))
  :hints (("goal" :induct (sub1-logcdr-induction n x)
           :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY logbit))))

(defthm equal-x-logapp-x
  (implies
   (and (integerp x) (integerp y) (integerp n) (<= 0 n))
   (equal
    (equal x (logapp n x y))
    (equal (logtail n x) y)))
  :hints (("goal" :induct (sub1-logcdr-induction n x)
           :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY))))

(defthm equal-x-loghead-x
  (implies
   (and (integerp x) (integerp n) (<= 0 n))
   (equal
    (equal x (loghead n x))
    (unsigned-byte-p n x)))
  :hints (("goal" :induct (sub1-logcdr-induction n x)
           :in-theory (enable LOGOPS-RECURSIVE-DEFINITIONS-THEORY))))

(defthm equal-logbit-logbit-logcdr-bridge
  (implies
   (and (integerp x) (integerp n) (<= 0 n))
   (equal
    (equal (logbit n (logcdr x)) (logbit (1+ n) x))
    t))
  :hints (("goal" :in-theory (enable logbit logbitp*))))

(defthm unsigned-byte-p-logcdr
  (implies
   (unsigned-byte-p n x)
   (unsigned-byte-p n (logcdr x)))
  :hints (("goal" :in-theory (enable unsigned-byte-p))))

(defthm unsigned-byte-p-1-logbit
  (unsigned-byte-p 1 (logbit n x))
  :hints (("goal" :in-theory (enable logbit))))

(defthm unsigned-byte-p-1-b-xor
  (unsigned-byte-p 1 (b-xor x y))
  :hints (("goal" :in-theory (enable b-xor))))

(defthm ash-0-arg2
  (implies
   (integerp x)
   (equal (ash x 0) x))
  :hints (("goal" :in-theory (enable ash))))

;;;;;;;;;;;;;;;;;;;;
;; Finally, we're ready to model the problem and do the proof...

;; We model the relevant parts of the Mostek 6502 state
(defstobj st
  (f1 :type (unsigned-byte 8) :initially 0)
  (f2 :type (unsigned-byte 8) :initially 0)
  (f1save :type (unsigned-byte 8) :initially 0)
  (a :type (unsigned-byte 8) :initially 0)
  (low :type (unsigned-byte 8) :initially 0)
  (cflg :type (unsigned-byte 1) :initially 0)
  (zflg :type (unsigned-byte 1) :initially 0)
  (x :type (unsigned-byte 8) :initially 0))

;; Now, we prove some rules about the types of the Mostek 6502 state.
;; Normally the following "type" lemmas would be generated
;; automatically by our READER, but we want to share this example
;; without distributing the READER so we take a shortcut: we steal
;; some automatically generated update/accessor lemmas from another
;; example that does use the READER and edit them a little.

;; Most of these are not necessary, but the simplest thing for me to
;; do is put them all in.  Fortunately, the state is small!

(DEFTHM F1-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 0 ST)) (<= (NTH 0 ST) 255)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP F1P)))))
(DEFTHM F1-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 0 ST))
                      (UNSIGNED-BYTE-P 8 (NTH 0 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= F1P))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM F1-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 0 V ST)) (F1P V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM F1-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 0 V ST))
                      (UNSIGNED-BYTE-P 8 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P F1P)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE F1-UPDATE-TYPE-HELPER)))

(DEFTHM F2-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 1 ST)) (<= (NTH 1 ST) 255)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP F2P)))))
(DEFTHM F2-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 1 ST))
                      (UNSIGNED-BYTE-P 8 (NTH 1 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= F2P))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM F2-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 1 V ST)) (F2P V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM F2-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 1 V ST))
                      (UNSIGNED-BYTE-P 8 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P F2P)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE F2-UPDATE-TYPE-HELPER)))
(DEFTHM F1SAVE-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 2 ST)) (<= (NTH 2 ST) 255)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP F1SAVEP)))))
(DEFTHM F1SAVE-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 2 ST))
                      (UNSIGNED-BYTE-P 8 (NTH 2 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= F1SAVEP))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM F1SAVE-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 2 V ST)) (F1SAVEP V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM F1SAVE-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 2 V ST))
                      (UNSIGNED-BYTE-P 8 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P F1SAVEP)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE F1SAVE-UPDATE-TYPE-HELPER)))
(DEFTHM A-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 3 ST)) (<= (NTH 3 ST) 255)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP AP)))))
(DEFTHM A-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 3 ST))
                      (UNSIGNED-BYTE-P 8 (NTH 3 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= AP))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM A-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 3 V ST)) (AP V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM A-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 3 V ST))
                      (UNSIGNED-BYTE-P 8 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P AP)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE A-UPDATE-TYPE-HELPER)))
(DEFTHM LOW-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 4 ST)) (<= (NTH 4 ST) 255)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP LOWP)))))
(DEFTHM LOW-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 4 ST))
                      (UNSIGNED-BYTE-P 8 (NTH 4 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= LOWP))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM LOW-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 4 V ST)) (LOWP V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM LOW-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 4 V ST))
                      (UNSIGNED-BYTE-P 8 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P LOWP)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE LOW-UPDATE-TYPE-HELPER)))
(DEFTHM CFLG-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 5 ST)) (<= (NTH 5 ST) 1)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP CFLGP)))))
(DEFTHM CFLG-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 5 ST))
                      (UNSIGNED-BYTE-P 1 (NTH 5 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= CFLGP))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM CFLG-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 5 V ST)) (CFLGP V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM CFLG-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 5 V ST))
                      (UNSIGNED-BYTE-P 1 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P CFLGP)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE CFLG-UPDATE-TYPE-HELPER)))
(DEFTHM ZFLG-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 6 ST)) (<= (NTH 6 ST) 1)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP ZFLGP)))))
(DEFTHM ZFLG-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 6 ST))
                      (UNSIGNED-BYTE-P 1 (NTH 6 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= ZFLGP))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM ZFLG-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 6 V ST)) (ZFLGP V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM ZFLG-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 6 V ST))
                      (UNSIGNED-BYTE-P 1 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P ZFLGP)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE ZFLG-UPDATE-TYPE-HELPER)))
(DEFTHM X-LINEAR
        (IMPLIES (STP ST)
                 (AND (<= 0 (NTH 7 ST)) (<= (NTH 7 ST) 255)))
        :RULE-CLASSES (:LINEAR :REWRITE) :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(STP XP)))))
(DEFTHM X-TYPE
        (IMPLIES (STP ST)
                 (AND (INTEGERP (NTH 7 ST))
                      (UNSIGNED-BYTE-P 8 (NTH 7 ST))))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(SIGNED-BYTE-P-<= UNSIGNED-BYTE-P-<= XP))
          :EXPAND (STP ST)))
        :rule-classes (:rewrite :type-prescription))
(DEFTHM X-UPDATE-TYPE-HELPER
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 7 V ST)) (XP V)))
        :HINTS
        (("goal" :EXPAND (:FREE (X) (STP X)) :IN-THEORY
          (SET-DIFFERENCE-THEORIES (CURRENT-THEORY 'GROUND-ZERO)
              '(NTH UPDATE-NTH))))
        :RULE-CLASSES NIL)
(DEFTHM X-UPDATE-TYPE
        (IMPLIES (STP ST)
                 (IFF (STP (UPDATE-NTH 7 V ST))
                      (UNSIGNED-BYTE-P 8 V)))
        :HINTS
        (("goal" :IN-THEORY
          (UNION-THEORIES '(UNSIGNED-BYTE-P SIGNED-BYTE-P XP)
              (CURRENT-THEORY 'GROUND-ZERO))
          :USE X-UPDATE-TYPE-HELPER)))

;; We now model the algorithm contained in Legato's paper.  We use the
;; line numbers from page 7 of his note.

;; We use an awkward modeling style to avoid use of the reader.  This
;; means that

;; 1.  We employ IHS functions directly.  logapp, loghead, logtail,
;;     ash, logcdr, unsigned-byte-p etc. are all described in the IHS
;;     library that comes with the ACL2 distribution.
;; 2.  We employ an awkward, multiple-let-binding style.
;;
;; Note also that the Mostek 6502 microprocessor commands of this
;; program assume a big-endian byte, while we prefer a small-endian
;; style for proofs.  We model the operations how we like, but this
;; sometimes makes the model confusing.  Note for example that a
;; "right shift" command is implemented using the IHS function logcdr.

;; ldx #8
(defun line1 (st)
  (declare (xargs :stobjs st))
  (let ((st (update-x 8 st)))
    st))

;; lda #0
(defun line2 (st)
  (declare (xargs :stobjs st))
  (let ((st (update-a 0 st)))
    st))

; Added by Matt K. for v2-7; needed for several guard proofs below.
(local (in-theory (enable unsigned-byte-p logbitp)))

;; loop ror f1
(defun line3 (st)
  (declare (xargs :stobjs st
                  :guard-hints (("goal" :in-theory
                                 (enable logapp loghead logcar stp)))))
  (let ((temp (cflg st)))
    (let ((st (update-cflg (logcar (f1 st)) st)))
      (let ((st (update-f1 (logapp 7 (logcdr (f1 st)) temp) st)))
        st))))

;; bcc zcoef

;; clc
(defun line5 (st)
  (declare (xargs :stobjs st))
  (let ((st (update-cflg 0 st)))
    st))

;; adc f2 - set a to a + f2 + c
(defun line6 (st)
  (declare (xargs :stobjs st
                  :guard-hints (("goal" :in-theory
                                 (enable logapp loghead
                                         logcar logbit)))))
  (let ((temp (+ (a st) (f2 st) (cflg st))))
    (let ((st (update-a (loghead 8 temp) st)))
      (let ((st (update-cflg (logbit 8 temp) st)))
        st))))

;; zcoef ror a
(defun line7 (st)
  (declare (xargs :stobjs st
                  :guard-hints (("goal" :in-theory
                                 (enable logapp loghead logcar)))))
  (let ((temp (cflg st)))
    (let ((st (update-cflg (logcar (a st)) st)))
      (let ((st (update-a (logapp 7 (logcdr (a st)) temp) st)))
        st))))

;; ror low
(defun line8 (st)
  (declare (xargs :stobjs st
                  :guard-hints (("goal" :in-theory
                                 (enable logapp loghead logcar)))))
  (let ((temp (cflg st)))
    (let ((st (update-cflg (logcar (low st)) st)))
      (let ((st (update-low (logapp 7 (logcdr (low st)) temp) st)))
        st))))

;; dex
(defun line9 (st)
  (declare (xargs :stobjs st
                  :guard-hints (("goal" :in-theory
                                 (enable logapp loghead logcar)))))
  (let ((temp (loghead 8 (1- (x st)))))
    (let ((st (update-zflg (if (equal temp 0) 1 0) st)))
      (let ((st (update-x temp st)))
        st))))

;; bne loop

;;; Now, compose line models into a program
(defun multloop (st)
  (declare (xargs :stobjs st
                  :measure (nfix (x st))
                  :hints (("goal" :in-theory
                           (enable unsigned-byte-p nfix)))))
  (if (and (integerp (x st)) (< 0 (x st)))
      (let ((st (line3 st)))
        (let ((st (if (equal (cflg st) 1)
                      (let ((st (line5 st)))
                        (line6 st))
                    st)))
          (let ((st (line7 st)))
            (let ((st (line8 st)))
              (let ((st (line9 st)))
                (if (equal (zflg st) 0)
                    (multloop st)
                  st))))))
    st))

(defun multprog (st)
  (declare (xargs :stobjs st))
  (let ((st (line1 st)))
    (let ((st (line2 st)))
      (multloop st))))

;; Here's the result of the computation - a 16 bit product that winds
;; up in two 8-bit registers.
(defun result (st)
  (declare (xargs :stobjs st))
  (logapp 8 (low st) (a st)))

#|

Let's test our program!

ACL2 !>:q

Exiting the ACL2 read-eval-print loop.  To re-enter, execute (LP).
ACL2>(update-f2 79 st)
(#(0) #(79) #(0) #(0) #(0) #*0 #*0 #(0))

ACL2>(update-f1 84 st)
(#(84) #(79) #(0) #(0) #(0) #*0 #*0 #(0))

ACL2>(multprog st)
(#(0) #(79) #(0) #(25) #(236) #*0 #*1 #(0))

ACL2>(result st)
6636

ACL2>(* 79 84)
6636

ACL2>

|#

(in-theory (disable stp))

;; We're finally ready to do the proof.

;; The following rules instruct the prover to open up the
;; multiplication in a way consistent with how the 6502 program
;; calculates the product.

(defthm ash-*-casesplit-v1
  (implies
   (and
    (equal (logcar x) 0)
    (integerp x) (integerp y) (integerp n) (<= 0 n))
   (equal (ash (* y x) n) (ash (* y (logcdr x)) (1+ n))))
  :hints (("goal" :in-theory (enable ash-*2-simplify ash*))))

(defthm ash-*-casesplit-v2
  (implies
   (and
    (equal (logcar x) 1)
    (integerp x) (integerp y) (integerp n) (<= 0 n))
   (equal (ash (* y x) n) (+ (ash y n) (ash (* y (logcdr x)) (1+ n)))))
  :hints (("goal" :in-theory
           (enable *ARK*-ASH-+-POS ash-*2-simplify ash*))))

(defthm *-logcar-casesplit
  (implies
   (and (integerp x) (integerp y))
   (equal
    (* (logcar x) y)
    (if (equal (logcar x) 0) 0 y)))
  :hints (("goal" :in-theory (enable logcar))))

;; The symbolic result of the loop leaves our result a mess.  We use
;; this rule to put the result "back together" again so that we can
;; reason about the effect of one iteration of the loop.
(defthm +-reconstruct-bridge
  (implies
   (and
    (equal (logcdr (+ x y)) z)
    (integerp x) (integerp y))
   (equal
    (logapp
     1
     (b-xor (logcar x) (logcar y))
     z)
    (+ x y)))
  :hints (("goal" :in-theory (enable logapp*))))

;; simplify result function if the upper byte is set to 0
(defthm result-update-nth
  (implies
   (stp st)
   (equal
    (result (update-nth 3 0 st))
    (low st))))

; Added by Matt K. for v2-7:
(local (in-theory (disable unsigned-byte-p logbitp)))

;; Prove that the loop works.  Note that the theorem's conclusion is a
;; generalization of the needed behavior that allows for induction.
(defthm loop-works
  (implies
   (and
    (stp st)
    (<= (x st) 8))
   (equal (result (multloop st))
          (+
           (logtail (x st) (result st))
           (ash (* (loghead (x st) (f1 st)) (f2 st)) (- 8 (x st))))))
  :hints (("goal" :in-theory (enable logtail* logbitp* ash*))))

;; With the loop proved, the program's proof is straightforward.
(defthm prog-works
  (implies
   (stp st)
   (equal (result (multprog st))
          (* (f1 st) (f2 st))))
  :hints (("goal" :in-theory (disable multloop result))))