This file is indexed.

/usr/share/acl2-7.2dfsg/books/ihs/quotient-remainder-lemmas.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
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
; quotient-remainder-lemmas.lisp  --  facts about FLOOR, MOD, TRUNCATE and REM
; Copyright (C) 1997  Computational Logic, Inc.
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.

;;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;;
;;;    quotient-remainder-lemmas.lisp
;;;
;;;    This book includes facts about the functions FLOOR, MOD, TRUNCATE and
;;;    REM, and integer ratios.
;;;
;;;    Bishop Brock
;;;    Computational Logic, Inc.
;;;    1717 West Sixth Street, Suite 290
;;;    Austin, Texas 78703
;;;    (512) 322-9951
;;;    brock@cli.com
;;;
;;;    Modified for ACL2 Version_2.6 by:
;;;    Jun Sawada, IBM Austin Research Lab. sawada@us.ibm.com
;;;    Matt Kaufmann, kaufmann@cs.utexas.edu
;;;
;;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

; Modified by Jared Davis, October 2014, to port documentation to xdoc.

(in-package "ACL2")
(include-book "ihs-init")
(include-book "ihs-theories")
(include-book "std/util/defrule" :dir :system)
(local (include-book "math-lemmas"))
(local (in-theory nil))

(local (in-theory (enable basic-boot-strap ; From ihs-theories
			 ;; From math-lemmas
			  ihs-math
			  rationalp-algebra
			  ifix nfix)))

(defxdoc ihs/quotient-remainder-lemmas
  :parents (ihs)
  :short "A book of facts about FLOOR, MOD, TRUNCATE and REM, and integer
  ratios.  Also enough of a theory of the Acl2 function @(see
  nonnegative-integer-quotient) to prove the rules."

  :long "<p>Since @(see nonnegative-integer-quotient) is the only one of these
functions that is recursive, the others must be DISABLEd for this library to be
of any use.  This can easily be done by DISABLEing the
QUOTIENT-REMAINDER-FUNCTIONS theory (defined by this book):</p>

@({
  (IN-THEORY (DISABLE QUOTIENT-REMAINDER-FUNCTIONS))
})

<h3>Introduction</h3>

<p>Common Lisp defines the quotient/remainder functions FLOOR/MOD and
TRUNCATE/REM, which operate on any rational numbers (as long as the divisor is
non-zero).  Both (TRUNCATE x y) and (FLOOR x y) are integers, and specify the
`integer part' of the rational number x/y; they differ in the direction of
rounding.</p>

<p>TRUNCATE is the `FORTRAN-style' quotient operation, rounding towards 0,
i.e., (TRUNCATE x y) = (TRUNCATE (ABS x) (ABS y)).  This book provides a
selected theory of TRUNCATE and REM.</p>

<p>(FLOOR x y) is identical to TRUNCATE if x/y &gt; 0 or x/y is an integer,
otherwise for negative non-integer ratios x/y, (FLOOR x y) = (TRUNCATE x y) -
1.  (FLOOR i (EXPT 2 j)) is the specification of an `arithmetic shift' of the
integer i by -j bits.  Since FLOOR and MOD are the foundations for integer
descriptions of hardware, this book contains a very extensive theory of FLOOR
and MOD.</p>

<p>The formal definitions of the Common Lisp functions are made in terms of the
Acl2 function NONNEGATIVE-INTEGER-QUOTIENT, which is simple recursive
specification of division of nonnegative integers by repeated subtraction.  We
provide only enough of a theory of NONNEGATIVE-INTEGER-QUOTIENT to prove the
desired properties of the Common Lisp functions.</p>

<h3>Documentation</h3>

<p>The documentation for this library is divided into a number of sections.
There is a section for the rules that apply to each function.  Some of the
rules will appear in more than 1 section.  If a rule is exported DISABLEd, then
you will see `(D)' after the rule class in the `one-liner' for the rule.  Note
that we often abbreviate NONNEGATIVE-INTEGER-QUOTIENT as NIQ.</p>

<h3>Approach</h3>

<p>We have tried to capture the properties of the quotient/remainder functions
with the smallest number of the most general rules possible.  This approach
takes advantage of Acl2 type reasoning, and the assumed existence of a basic
mathematics simplification library.  Several lemmas contain the
hypothesis (INTEGERP (/ x y)), which we consider to be the simplest statement
of the fact that @('(<quotient> x y)') = x/y, e.g.</p>

<ul>
<li>(INTEGERP (/ x y)) &rarr; (FLOOR x y) = (/ x y),</li>
<li>(INTEGERP (/ x y)) &rarr; (MOD x y) = 0.</li>
</ul>

<p>Thus, the first fact above obviates the need for a specials lemmas
like (FLOOR i 1) = i for integers i, since (/ i 1) = i by simplification.</p>

<p>In general, at most 2 of the many possible commutative forms of the rules
are exported from this library.  If they aren't the ones you need, simply prove
the appropriate corollary, or :USE an :INSTANCE of the library rule.  Also,
lemmas are generally exported DISABLEd if they seemed to interfere with the
proofs of other lemmas, or could easily lead to infinite looping.  Be careful
when ENABLEing these lemmas.</p>

<p>Questions, comments, and sugestions are welcome.  Contact
brock@cli.com.</p>")

(defsection niq-lemmas
  :parents (quotient-remainder-lemmas nonnegative-integer-quotient)
  :short "Lemmas about @(see nonnegative-integer-quotient) (abbreviated NIQ).")

(defsection floor-lemmas
  :parents (quotient-remainder-lemmas floor)
  :short "Lemmas about @(see FLOOR).")

(defsection truncate-lemmas
  :parents (quotient-remainder-lemmas truncate)
  :short "Lemmas about @(see TRUNCATE).")

(defsection mod-lemmas
  :parents (quotient-remainder-lemmas mod)
  :short "Lemmas about @(see MOD).")

(defsection rem-lemmas
  :parents (quotient-remainder-lemmas rem)
  :short "Lemmas about @(see REM).")

(defsection integer-ratio-lemmas
  :parents (quotient-remainder-lemmas /)
  :short "Lemmas about ratios @('x/y') that are known to be @(see integerp).")


;;;****************************************************************************
;;;
;;;    DEFINITIONS and GUARD MACROS
;;;
;;;****************************************************************************

(defsection qr-guard-macros
  :parents (quotient-remainder-lemmas)
  :short "Macro forms of the guards for the quotient/remainder functions."
  :long "<p>Without these macros, fully 25% of the text of the
\"quotient-remainder-lemmas\" book is given over simply to expressing the
guards!</p>")

(defsection niq-guard
  :parents (qr-guard-macros)
  :short "Macro form of the guard for @(see nonnegative-integer-quotient) (forced)."

  (defmacro niq-guard (i j)
    (mlambda (i j)
             (and (force (integerp i))
                  (force (>= i 0))
                  (force (integerp j))
                  (force (> j 0))))))

(defsection qr-guard
  :parents (qr-guard-macros)
  :short "Quotient/Remainder @(see GUARD): Macro form of the guards for @(see
FLOOR), @(see MOD), @(see TRUNCATE), and @(see REM), or any ratio @('x/y') of
rationals (forced)."
  (defmacro qr-guard (x y)
    (mlambda (x y)
             (and (force (real/rationalp x))
                  (force (real/rationalp y))
                  (force (not (equal 0 y)))))))


;;;****************************************************************************
;;;
;;;    LOCAL LEMMAS --  A few special rules derived from the more general
;;;    rules included above.
;;;
;;;****************************************************************************

(local
 (defthm cancel-<-+-3
   (equal (< (+ x y z) y)
	  (< (+ x z) 0))
   :hints (("Goal" :in-theory (enable rewrite-linear-equalities-to-iff)))))

(local
 (defthm cancel-equal-+-3
   (implies (acl2-numberp y)
	    (equal (equal (+ x y z) y)
		   (equal (fix x) (- z))))))

(local
 (defthm cancel-equal-+-right
   (equal (equal (+ y x) (+ z x))
	  (equal (fix y) (fix z)))))

;  This theory is useful for proving certain types of bounds properties, but
;  will cause thrashing in linear arithmetic unless the hypotheses e.g.
;  x <= y can be relieved.

(local
 (defthm ratio-theory-of-1
   (and (implies (and (qr-guard x y) (<= 0 x) (< 0 y) (< x y))
                 (< (/ x y) 1))
        (implies (and (qr-guard x y) (<= 0 x) (< 0 y) (<= y x))
                 (<= 1 (/ x y)))
        (implies (and (qr-guard x y) (<= 0 x) (< y 0) (< x (- y)))
                 (< -1 (/ x y)))
        (implies (and (qr-guard x y) (<= 0 x) (< y 0) (<= (- y) x))
                 (<= (/ x y) -1))
        (implies (and (qr-guard x y) (<= 0 x) (< y 0) (<= x (- y)))
                 (<= -1 (/ x y)))
        (implies (and (qr-guard x y) (<= x 0) (< 0 y) (< (- x) y))
                 (< -1 (/ x y)))
        (implies (and (qr-guard x y) (<= x 0) (< 0 y) (<= y (- x)))
                 (<= (/ x y) -1))
        (implies (and (qr-guard x y) (<= x 0) (< 0 y) (<= (- x) y))
                 (<= -1 (/ x y)))
        (implies (and (qr-guard x y) (<= x 0) (< y 0) (< (- x) (- y)))
                 (< (/ x y) 1))
        (implies (and (qr-guard x y) (<= x 0) (< y 0) (<= (- y) (- x)))
                 (<= 1 (/ x y))))
   :rule-classes :linear
   :hints (("Goal" :in-theory (enable prefer-*-to-/
                                      rewrite-linear-equalities-to-iff)))))


;;;****************************************************************************
;;;
;;;    LEMMAS -- Begin proving lemmas.
;;;
;;;****************************************************************************

(deflabel begin-quotient-remainder-lemmas)

;;;****************************************************************************
;;;
;;;    NONNEGATIVE-INTEGER-QUOTIENT
;;;
;;;****************************************************************************

(local (defthm niq-bounds-help-1
	 (implies (and (real/rationalp i)
		       (< 0 j)
		       (real/rationalp j)
		       (real/rationalp x))
		  (equal (< (+ -1 (* i (/ j))) x)
			 (< i (+ j (* j x)))))
	 :hints (("Goal" :in-theory (set-difference-theories
                                     (enable rewrite-linear-equalities-to-iff)
                                     '(<-*-left-cancel))
		  :use (:instance <-*-left-cancel
                        (z j) (y x) (x (/ (+ i (- j)) j)))))
	 :rule-classes nil))

(defrule niq-bounds
  :parents (niq-lemmas)
  :short "Linear (D): @('i/j - 1 < (NIQ i j) <= i/j')"
  :long "<p>This lemma serves as a :LINEAR definition of @(see
NONNEGATIVE-INTEGER-QUOTIENT), and allows us to derive interesting properties
of @(see FLOOR) and @(see TRUNCATE) by linear arithmetic.</p>

<p>This lemma is stored as a :LINEAR rule under NIQ since we think of this as a
property of NIQ, and not as a general property of (/ I J).</p>"

  (implies (niq-guard i j)
           (and (<= (nonnegative-integer-quotient i j) (/ i j))
                (< (- (/ i j) 1) (nonnegative-integer-quotient i j))))
  :rule-classes ((:linear :trigger-terms ((nonnegative-integer-quotient i j))))
  :hints
  (("Goal" :in-theory (enable ifix nfix nonnegative-integer-quotient
                              ratio-theory-of-1))
   ("Subgoal *1/2.2" :use (:instance niq-bounds-help-1
                           (i i) (j j)
                           (x (nonnegative-integer-quotient (+ i (- j)) j))))))

;< Although the following follows naturally from NIQ-BOUNDS, it can't be
;proved by linear alone, probably because (/ i j) is `too heavy'.

(defrule niq-type
  :parents (niq-lemmas)
  :short "Decide @('(NIQ i j) = 0'), @('(NIQ i j) > 0'), and @('(NIQ i j) =
i/j') based on the inequalities of i and j, and the INTEGERP-ness of i/j."

  (implies (niq-guard i j)
           (and (equal (equal (nonnegative-integer-quotient i j) 0)
                       (< i j))
                (equal (< 0 (nonnegative-integer-quotient i j))
                       (>= i j))
                (equal (equal (nonnegative-integer-quotient i j) (/ i j))
                       (integerp (/ i j)))))
  :rule-classes
  ((:rewrite)
   (:linear :corollary (implies (and (>= i j)
                                     (niq-guard i j))
                                (< 0 (nonnegative-integer-quotient i j))))
   (:rewrite :corollary (implies (and (< i j)
                                      (niq-guard i j))
                                 (equal (nonnegative-integer-quotient i j)
                                        0)))
   (:rewrite :corollary (implies (and (equal r (/ i j))
                                      (integerp r)
                                      (niq-guard i j))
                                 (equal (nonnegative-integer-quotient i j) r))))
  :disable (niq-bounds <-*-/-left)
  :use (niq-bounds))


;;;****************************************************************************
;;;
;;;    TRUNCATE and REM
;;;
;;;  We begin with TRUNCATE and REM since we will sometimes prove properties of
;;;  FLOOR from a definition of FLOOR in terms of TRUNCATE.  Since TRUNCATE
;;;  doesn't figure into our hardware specification, however, it's theory is
;;;  somewhat TRUNCATEd!
;;;
;;;****************************************************************************

(defrule truncate-rem-elim
  :parents (truncate-lemmas)
  :short "Rewrite: @('(+ (REM x y) (* y (TRUNCATE x y))) = x')."
  (implies
   ;; (qr-guard x y) ; changed for v2-9-2 at Jared Davis's suggestion
   (and (force (real/rationalp x))
        (force (real/rationalp y)))
   (equal (+ (rem x y) (* y (truncate x y)))
          x))
  :rule-classes (:rewrite :elim)
  :enable rem)

(defrule truncate-=-x/y
  :parents (truncate-lemmas)
  :short "Rewrite: @('(TRUNCATE x y) = x/y'), when x/y is an integer."
  :long "<p>This rule is a corollary of a more general equality, which is also
stored as a :REWRITE and :GENERALIZE rule.</p>"
  (implies (qr-guard x y)
           (equal (equal (truncate x y) (/ x y))
                  (integerp (/ x y))))
  :rule-classes ((:rewrite)
                 (:generalize)
                 (:rewrite :corollary (implies (and (equal r (/ x y))
                                                    (integerp r)
                                                    (qr-guard x y))
                                               (equal (truncate x y) r))))
  :in-theory (set-difference-theories (enable truncate equal-*-x-y-x)
                                      '(commutativity-of-*)))

;<  This is a fast and beautiful proof, using the :LINEAR rule NIQ-BOUNDS.

(defrule truncate-bounds
  :parents (truncate-lemmas)
  :short "Linear (D) : @('x/y - 1 < (TRUNCATE x y) <= x/y'), when @('x/y >= 0');
 @('x/y <= (TRUNCATE x y) < x/y + 1'), when @('x/y =< 0')."

  :long "<p>This lemma `defines' TRUNCATE as a set of inequalties.  Many of the
properties of TRUNCATE will be derived from this theorem.  Unfortunately, this
lemma is implicated in thrashing in the linear arithmetic procedure unless the
inequalities of X and Y can be decided, so it may need to be DISABLEd at times.
This lemma is stored as a :LINEAR rule for TRUNCATE exclusively since we
consider it to be a property of TRUNCATE, and not a general property of (/ x
y).</p>

<p>The statement of the hypotheses of this lemma is critical for its proper
application.  It is necessary for each inequality of x and y to stand alone in
order to be relieveable by linear arithmetic.</p>"

  (and (implies (and (>= x 0) (> y 0) (qr-guard x y))
                (and (< (- (/ x y) 1) (truncate x y))
                     (<= (truncate x y) (/ x y))))
       (implies (and (<= x 0) (< y 0) (qr-guard x y))
                (and (< (- (/ x y) 1) (truncate x y))
                     (<= (truncate x y) (/ x y))))
       (implies (and (>= x 0) (< y 0) (qr-guard x y))
                (and (<= (/ x y) (truncate x y))
                     (< (truncate x y) (+ (/ x y) 1))))
       (implies (and (<= x 0) (> y 0) (qr-guard x y))
                (and (<= (/ x y) (truncate x y))
                     (< (truncate x y) (+ (/ x y) 1)))))
  :rule-classes ((:linear :trigger-terms ((truncate x y))))
  :in-theory (set-difference-theories (enable truncate
                                              rational-implies2)
                                      '(<-*-/-left <-*-/-right)))

;<  Without the :CASES hint, the inequality conditions simplify to a form
;that doesn't allow us to decide the sign of X, and the proof fails.  With
;the :CASES hint, we can decide the sign of X and the proof is obvious from
;TRUNCATE-BOUNDS.

(defrule truncate-type
  :parents (truncate-lemmas)
  :short "Decide @('(TRUNCATE x y) < 0'), @('(TRUNCATE x y) > 0'), and
@('(TRUNCATE x y) = 0') based on inequalities of x and y."

  :long "<p>This rule is available in various forms: :REWRITE, :LINEAR,
:TYPE-PRESCRIPTION, and :GENERALIZE as appropriate.  Note that unless we can
decide the inequalities of X and Y the :LINEAR forms may thrash.</p>"

  (implies (qr-guard x y)
           (and (equal (< (truncate x y) 0)
                       (or (and (<= x 0) (> y 0) (<= y (- x)))
                           (and (>= x 0) (< y 0) (<= (- y) x))))
                (equal (> (truncate x y) 0)
                       (or (and (>= x 0) (> y 0) (<= y x))
                           (and (<= x 0) (< y 0) (>= y x))))
                (equal (equal (truncate x y) 0)
                       (< (abs x) (abs y)))))
  :rule-classes
  ((:rewrite)
   (:generalize)
   (:linear :corollary (implies
                        (and (<= x 0) (> y 0) (<= y (- x)) (qr-guard x y))
                        (< (truncate x y) 0)))
   (:linear :corollary (implies
                        (and (>= x 0) (< y 0) (<= (- y) x) (qr-guard x y))
                        (< (truncate x y) 0)))
   (:linear :corollary (implies
                        (and (>= x 0) (> y 0) (<= y x) (qr-guard x y))
                        (> (truncate x y) 0)))
   (:linear :corollary (implies
                        (and (<= x 0) (< y 0) (>= y x) (qr-guard x y))
                        (> (truncate x y) 0)))
   (:rewrite :corollary (implies
                         (and (< (abs x) (abs y)) (qr-guard x y))
                         (equal (truncate x y) 0)))
   (:type-prescription :corollary (implies
                                   (and (<= x 0) (> y 0) (qr-guard x y))
                                   (<= (truncate x y) 0)))
   (:type-prescription :corollary (implies
                                   (and (>= x 0) (< y 0) (qr-guard x y))
                                   (<= (truncate x y) 0)))
   (:type-prescription :corollary (implies
                                   (and (>= x 0) (> y 0) (qr-guard x y))
                                   (>= (truncate x y) 0)))
   (:type-prescription :corollary (implies
                                   (and (<= x 0) (< y 0) (qr-guard x y))
                                   (>= (truncate x y) 0))))
  :cases ((< x 0) (> x 0)))

;< These follow immediately from the definition of TRUNCATE.  If we enter
;these lemmas in a theory that includes the :LINEAR rules for TRUNCATE we will
;observe severe thrashing in linear arithmetic, since these rules are
;independent of the signs of x and y.  So, we'll just prove them in the theory
;that prevails at the beginning of this book.

(encapsulate ()

  (local (in-theory (current-theory 'begin-quotient-remainder-lemmas)))
  (local (in-theory (enable truncate)))

  (local (defthm foo (equal (real/rationalp (- x))
                            (or (real/rationalp x)
                                (not (acl2-numberp x))))))

  (defrule truncate-minus
    :parents (truncate-lemmas)
    :short "Rewrite: @('(TRUNCATE (- x) y) = (- (TRUNCATE x y))') and
@('(TRUNCATE x (- y)) = (- (TRUNCATE x y))')."
    (and (equal (truncate (- x) y) (- (truncate x y)))
         (equal (truncate x (- y)) (- (truncate x y))))
    :in-theory (enable denominator-unary-minus)
    :expand (nonnegative-integer-quotient 0 (denominator (- (* x (/ y))))))

  (defruled rewrite-truncate-x*y-z-left
    :parents (truncate-lemmas)
    :short "Rewrite (D): @('(TRUNCATE (* x y) z) = (TRUNCATE y (/ z x))'), when x /= 0."
    :long "<p>Since we don't presume any rewriting strategy for @(see /)
  vis-a-vis @(see *), this often useful rule is exported DISABLEd.</p>"
    (equal (truncate (* x y) z)
           (truncate y (/ z x))))

  (defruled rewrite-truncate-x*y-z-right
    :parents (truncate-lemmas)
    :short "Rewrite (D): @('(TRUNCATE (* x y) z) = (TRUNCATE x (/ z y))'), when y /= 0."
    :long "<p>Since we don't presume any rewriting strategy for / vis-a-vis *, this
  often useful rule is exported DISABLEd.</p>"
    (equal (truncate (* x y) z)
           (truncate x (/ z y))))

  (defrule truncate-cancel-*
    :parents (truncate-lemmas)
    :short "Rewrite: @('(TRUNCATE (* x y) y) = (TRUNCATE x 1)')."
    (implies (qr-guard x y)
             (and (equal (truncate (* x y) y) (truncate x 1))
                  (equal (truncate (* y x) y) (truncate x 1))))))

;<  The linear rules refuse to fire on their own.  From TRUNCATE-BOUNDS and
;NIQ-BOUNDS it is obvious that these are the same integers.

(defruled integer-truncate-as-niq
  :parents (truncate-lemmas)
  :short "Rewrite (D) : @('(TRUNCATE i j) = (SIGNUM i) * (SIGNUM j) * (NIQ i
    j)'), for integers i,j."
  :long "<p>This rule shows that TRUNCATE is the \"usual\" (i.e., FORTRAN-style)
  integer quotient for both positive and negative integers.</p>"
  (implies (and (integerp i)
                (integerp j)
                (force (not (equal j 0))))
           (equal (truncate i j)
                  (* (signum i) (signum j)
                     (nonnegative-integer-quotient (abs i) (abs j)))))
  :in-theory (disable truncate-bounds niq-bounds <-*-/-right <-*-/-left
                      truncate-type default-<-1 default-<-2
                      integerp-+-minus-*)
  :use ((:instance truncate-bounds (x i) (y j))
        (:instance niq-bounds (i (abs i)) (j (abs j)))))

;; (defthm truncate-truncate-integer
;;   (implies
;;    (and (integerp i)
;; 	(integerp j)
;; 	(integerp k)
;; 	(force (not (equal j 0)))
;; 	(force (not (equal k 0))))
;;    (equal (truncate (truncate i j) k)
;; 	  (truncate i (* j k))))
;;   :hints
;;   (("Goal"
;;     :in-theory (enable truncate))))
;;   :hints
;;   (("Goal"
;;     :in-theory (e/d (integer-truncate-as-niq niq-type niq-i/j-<-k
;; 					     prefer-*-to-/)
;; 		    (x-<-y*z))
;;     :use ((:instance x-<-y*z (x (abs i)) (y (abs j)) (z (abs k))))))
;;     Rewrite: (TRUNCATE (TRUNCATE i j) k) = (TRUNCATE i (* j k))
;;     for integers i,j,k.~/~/~/")


;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;;
;;;    REM
;;;
;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

(defruled linearize-rem
  :parents (rem-lemmas)
  :short "Rewrite (D): Transform @('(REM x y) < z'), @('(REM x y) > z'), and
@('(REM x y) = z') into an equivalent @(see TRUNCATE) expression suitable for
reasoning about with @(see TRUNCATE-BOUNDS) and other @(see truncate-lemmas)."
  :long "<p>Since this lemma can be considered a \"definition\" of REM, it is
exported DISABLED.</p>"
  (implies (and (qr-guard x y)
                (force (real/rationalp z)))
           (and (equal (< (rem x y) z)
                       (if (> y 0)
                           (< (- (/ x y) (truncate x y)) (/ z y))
                         (> (- (/ x y) (truncate x y)) (/ z y))))
                (equal (> (rem x y) z)
                       (if (> y 0)
                           (> (- (/ x y) (truncate x y)) (/ z y))
                         (< (- (/ x y) (truncate x y)) (/ z y))))
                (equal (equal (rem x y) z)
                       (equal (- (/ x y) (truncate x y)) (/ z y)))))
  :enable (rem prefer-*-to-/))

(defrule rem-=-0
  :parents (rem-lemmas)
  :short "Rewrite: @('(REM x y) = 0'), when @('x/y') is an integer."
  :long "<p>This rule is a corollary of a more general equality.  The equality
  is also stored as a :REWRITE and :GENERALIZE rule.</p>"
  (implies (qr-guard x y)
           (equal (equal (rem x y) 0)
                  (integerp (/ x y))))
  :rule-classes ((:rewrite)
                 (:generalize)
                 (:rewrite :corollary (implies (and (integerp (/ x y))
                                                    (qr-guard x y))
                                               (equal (rem x y) 0))))
  :enable (linearize-rem))

(defrule rem-x-y-=-x
  :parents (rem-lemmas)
  :short "@('(REM x y) = x'), when @('|x| < |y|')."
  :long "<p>This rule is a corollary of a more general equality which is also
  stored as a :REWRITE and :GENERALIZE rule.</p>"
  (implies (qr-guard x y)
           (equal (equal (rem x y) x)
                  (< (abs x) (abs y))))
  :rule-classes
  ((:rewrite)
   (:generalize)
   (:rewrite :corollary (implies (and (< (abs x) (abs y))
                                      (qr-guard x y))
                                 (equal (rem x y) x))))
  :enable (linearize-rem))

(defrule integerp-rem
  :parents (rem-lemmas)
  :short "Type-Prescription: (REM i j) is an integer, when i and j are integers."
  (implies (and (integerp i)
                (integerp j)
                (force (not (equal j 0))))
           (integerp (rem i j)))
  :rule-classes :type-prescription
  :enable rem)

;<  Again, this rule is an easy consequence of TRUNCATE-BOUNDS, but (/ x y)
;is too `heavy' to let it fire naturally, so we have to :USE it.

(defrule rem-bounds
  :parents (rem-lemmas)
  :short "Linear: Useful forms of the fact that @('(REM x y) < |y|')."
  :long "<p>This lemma is also stored as a :GENERALIZE rule.</p>"
  (and (implies (and (>= x 0)
                     (qr-guard x y))
                (< (rem x y) (abs y)))
       (implies (and (<= x 0)
                     (qr-guard x y))
                (> (rem x y) (- (abs y)))))
  :rule-classes ((:linear :trigger-terms ((rem x y)))
                 (:generalize))
  :enable linearize-rem
  :disable truncate-bounds
  :use truncate-bounds)

(defrule rem-type
  :parents (rem-lemmas)
  :short "Decide @('(REM x y) < 0') and @('(REM x y) > 0') based on the sign of
    x and the INTEGERP-ness of x/y."
  :long "<p>This rule is stored as appropriate :REWRITE, :LINEAR, :GENERALIZE,
  and :TYPE-PRESCRIPTION rules.</p>"
  (implies (qr-guard x y)
           (and (equal (< (rem x y) 0)
                       (and (< x 0)
                            (not (integerp (/ x y)))))
                (equal (> (rem x y) 0)
                       (and (> x 0)
                            (not (integerp (/ x y)))))))
  :rule-classes
  ((:rewrite)
   (:generalize)
   (:linear :corollary (implies (and (< x 0)
                                     (not (integerp (/ x y)))
                                     (qr-guard x y))
                                (< (rem x y) 0)))
   (:linear :corollary (implies (and (> x 0)
                                     (not (integerp (/ x y)))
                                     (qr-guard x y))
                                (> (rem x y) 0)))
   (:linear :corollary (implies (and (<= x 0) (qr-guard x y))
                                (<= (rem x y) 0)))
   (:linear :corollary (implies (and (>= x 0)
                                     (qr-guard x y))
                                (>= (rem x y) 0)))
   (:type-prescription :corollary (implies (and (< x 0)
                                                (not (integerp (/ x y)))
                                                (qr-guard x y))
                                           (< (rem x y) 0)))
   (:type-prescription :corollary (implies (and (> x 0)
                                                (not (integerp (/ x y)))
                                                (qr-guard x y))
                                           (> (rem x y) 0)))
   (:type-prescription :corollary (implies (and (<= x 0)
                                                (qr-guard x y))
                                           (<= (rem x y) 0)))
   (:type-prescription :corollary (implies (and (>= x 0)
                                                (qr-guard x y))
                                           (>= (rem x y) 0))))
  :in-theory (set-difference-theories (enable linearize-rem)
                                      '(<-*-/-right <-*-/-left)))

(defrule rem-minus
  :parents (rem-lemmas)
  :short "Rewrite: @('(REM (- x) y) = (- (REM x y))');
   @('(REM x (- y)) = (SIGNUM x) * (SIGNUM y) * (REM x y)')."
  (implies (qr-guard x y)
           (and (equal (rem (- x) y)
                       (- (rem x y)))
                (equal (rem x (- y))
                       (* (signum y) (signum y) (rem x y)))))
  :enable linearize-rem
  :expand (rem x y))


;;;****************************************************************************
;;;
;;;    FLOOR and MOD
;;;
;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;  We'll sometimes use this lemma which allows us to prove properties of
;  FLOOR from properties of TRUNCATE.

(defruled floor-as-truncate
  :parents (floor-lemmas)
  :short "Rewrite (FLOOR x y) to a function of (TRUNCATE x y)."
  (implies (qr-guard x y)
           (equal (floor x y)
                  (if (or (integerp (/ x y))
                          (> (/ x y) 0))
                      (truncate x y)
                    (- (truncate x y) 1))))
  :enable (floor truncate))

(defrule floor-mod-elim
  :parents (floor-lemmas)
  :short "Rewrite: @('(+ (MOD x y) (* y (FLOOR x y))) = x')."
  :long "<p>This rule is also stored as an :ELIM rule.</p>"
  ;; [Jared] modified on 2014-07-29 to not forcibly assume acl2-numberp, to
  ;; avoid name clash with arithmetic-5.
  (implies (acl2-numberp x)
	   (equal (+ (mod x y) (* y (floor x y))) x))
  :rule-classes (:rewrite :elim)
  :enable mod)

(defrule floor-=-x/y
  :parents (floor-lemmas)
  :short "Rewrite @('(FLOOR x y) = x/y'), when x/y is an integer."
  :long "<p>This rule is a corollary of a more general equality which is also
  stored as a :REWRITE and :GENERALIZE rule.</p>"
  ;; [Jared]: modified on 2014-07-29 to remove unnecessary (qr-guard x y)
  ;; hypothesis and for compatibility with arithmetic-5.
  (equal (equal (floor x y) (/ x y))
         (integerp (/ x y)))
  ;; The original IHS rule had the following rule classes:
  ;;
  ;; ((:rewrite)
  ;;  (:generalize)
  ;;  (:rewrite :corollary (implies (and (equal r (/ x y))
  ;;                                     (integerp r))
  ;;                                (equal (floor x y) r))))
  ;;
  ;; The original arithmetic-5 rule has the following rule-classes:
  ;;
  ;; (:rewrite :corollary (implies (integerp (/ x y))
  ;;                               (equal (floor x y)
  ;;                                      (/ x y))))
  ;; (:rewrite :corollary (implies (equal (* x (/ y)) z)
  ;;                               (equal (equal (floor x y) z)
  ;;                                      (integerp z))))
  ;;
  ;; Solution: DO ALL THE THINGS.
  :rule-classes ((:rewrite)
                 (:generalize)
                 (:rewrite :corollary (implies (and (equal r (/ x y))
                                                    (integerp r))
                                               (equal (floor x y) r)))
                 (:rewrite :corollary (implies (integerp (/ x y))
                                               (equal (floor x y)
                                                      (/ x y))))
		 (:rewrite :corollary (implies (equal (* x (/ y)) z)
                                               (equal (equal (floor x y) z)
                                                      (integerp z)))))
  :in-theory (set-difference-theories (enable floor equal-*-x-y-x)
                                      '(commutativity-of-*)))

;< Another beautiful proof from NIQ-BOUNDS.

(defrule floor-bounded-by-/
  :parents (floor-lemmas)
  :short "Linear (D) : @('x/y - 1 < (FLOOR x y) <= x/y')."
  :long "<p>This lemma \"defines\" FLOOR as a set of inequalties.  Many of the
properties of FLOOR will be derived from this theorem.  Unfortunately, this
lemma is implicated in thrashing in the linear arithmetic procedure and must be
DISABLEd at times.  This lemmas is stored as a :LINEAR rule for FLOOR
exclusively since we consider it to be a property of FLOOR, and not a general
property of @('(/ x y)').</p>"
  (implies (qr-guard x y)
           (and (< (- (/ x y) 1) (floor x y))
                (<= (floor x y) (/ x y))))
  :rule-classes ((:linear :trigger-terms ((floor x y)))
                 (:generalize))
  :in-theory (set-difference-theories (enable floor rational-implies2)
                                      '(<-*-/-left <-*-/-right)))

;< We need to consider the :CASES to get FLOOR-BOUNDED-BY-/ to do its job.  This
;proof does 2 eliminations (considering (FLOOR x y) = -1) but it goes
;through.  If we simply :USE FLOOR-BOUNDED-BY-/ with the same :CASES it also works
;and takes about the same amount of time.  I'll bet that it could get the
;(FLOOR x y) = -1 cases with FLOOR-BOUNDED-BY-/ if we let FLOOR-BOUNDED-BY-/ trigger on
;(/ x y).

;; The lemma FLOOR-TYPE had too many cases, so I split it in to 4 lemmas:
;; FLOOR-TYPE-1, FLOOR-TYPE-2, FLOOR-TYPE-3 and FLOOR-TYPE-4.
;; A. Flatau 17-Nov-1994

(defrule floor-type-1
  :parents (floor-lemmas)
  :short "Decide @('(FLOOR x y) < 0') based on inequalities of x and y."
  :long "<p>This rule is available in various forms: :REWRITE, :LINEAR,
  :TYPE-PRESCRIPTION, and :GENERALIZE as appropriate.  Note that unless we
  can decide the inequalities of x and y the :LINEAR forms may thrash.</p>"
  (implies (qr-guard x y)
	   (iff (< (floor x y) 0)
                (or (and (< x 0) (> y 0))
                    (and (> x 0) (< y 0)))))
  :cases ((< (/ x y) 0) (> (/ x y) 0))
  :in-theory (enable normalize-<-/-to-*-3)
  :rule-classes
  ((:rewrite :corollary (implies (qr-guard x y)
                                 (equal (< (floor x y) 0)
                                        (or (and (< x 0) (> y 0))
                                            (and (> x 0) (< y 0))))))
   (:generalize :corollary (implies (qr-guard x y)
                                    (equal (< (floor x y) 0)
                                           (or (and (< x 0) (> y 0))
                                               (and (> x 0) (< y 0))))))
   (:linear :corollary (implies (and (< x 0) (> y 0) (qr-guard x y))
                                (< (floor x y) 0)))
   (:linear :corollary (implies (and (> x 0) (< y 0) (qr-guard x y))
                                (< (floor x y) 0)))
   (:type-prescription :corollary (implies (and (< x 0)
                                                (> y 0)
                                                (qr-guard x y))
                                           (< (floor x y) 0)))
   (:type-prescription :corollary (implies (and (> x 0)
                                                (< y 0)
                                                (qr-guard x y))
                                           (< (floor x y) 0)))))

(defrule floor-type-2
  :parents (floor-lemmas)
  :short "Decide @('(FLOOR x y) > 0') based on inequalities of x and y."
  :long "<p>This rule is available in various forms: :REWRITE, :LINEAR,
  :TYPE-PRESCRIPTION, and :GENERALIZE as appropriate.  Note that unless we
  can decide the inequalities of x and y the :LINEAR forms may thrash.</p>"
  (implies (qr-guard x y)
	   (equal (> (floor x y) 0)
		  (or (and (>= x 0) (> y 0) (<= y x))
		      (and (<= x 0) (< y 0) (>= y x)))))
  :rule-classes ((:rewrite)
		 (:generalize)
		 (:linear :corollary
			  (implies (and (>= x 0) (> y 0) (<= y x)
					(qr-guard x y))
				   (> (floor x y) 0)))
		 (:linear :corollary
			  (implies (and (<= x 0) (< y 0) (>= y x)
					(qr-guard x y))
				   (> (floor x y) 0))))
  :hints (("Subgoal 6" :cases ((<= x 0) (<= 0 x)))
          ("Subgoal 2" :cases ((<= x 0) (<= 0 x)))))

(defrule floor-type-3
  :parents (floor-lemmas)
  :short "Decide @('(FLOOR x y) > 0') based on inequalities of x and y."
  :long "<p>This rule is available in various forms: :REWRITE, :LINEAR,
  :TYPE-PRESCRIPTION, and :GENERALIZE as appropriate.  Note that unless we
  can decide the inequalities of x and y the :LINEAR forms may thrash.</p>"
  (implies (qr-guard x y)
	   (equal (equal (floor x y) 0)
		  (or (and (>= x 0) (> y 0) (< x y))
		      (and (<= x 0) (< y 0) (> x y)))))
  :rule-classes ((:rewrite)
		 (:generalize)
		 (:rewrite :corollary
			   (implies (and (>= x 0) (> y 0) (< x y)
					 (qr-guard x y))
				    (equal (floor x y) 0)))
		 (:rewrite :corollary
			   (implies (and (<= x 0) (< y 0) (> x y)
					 (qr-guard x y))
				    (equal (floor x y) 0)))
		 (:type-prescription :corollary
				     (implies (and (>= x 0) (> y 0)
						   (qr-guard x y))
					      (>= (floor x y) 0)))
		 (:type-prescription :corollary
				     (implies (and (<= x 0) (< y 0)
						   (qr-guard x y))
					      (>= (floor x y) 0))))
  :cases ((< (/ x y) 0) (> (/ x y) 0)))

(defrule floor-type-4
  :parents (floor-lemmas)
  :short "Decide @('(FLOOR x y) = -1') based on inequalities of x and y."
  :long "<p>This rule is available in various forms: :REWRITE, :LINEAR,
  :TYPE-PRESCRIPTION, and :GENERALIZE as appropriate.  Note that unless we
  can decide the inequalities of x and y the :LINEAR forms may thrash.</p>"
  (implies (qr-guard x y)
	   (equal (equal (floor x y) -1)
		  (or (and (< x 0) (> y 0) (<= (- x) y))
		      (and (> x 0) (< y 0) (<= x (- y))))))
  :rule-classes ((:rewrite)
		 (:generalize)
		 (:rewrite :corollary
			   (implies (and (> x 0) (< y 0) (<= x (- y))
					 (qr-guard x y))
				    (equal (floor x y) -1)))
		 (:rewrite :corollary
			   (implies (and (< x 0) (> y 0) (<= (- x) y)
					 (qr-guard x y))
				    (equal (floor x y) -1))))
  :hints (("Goal" :cases ((< (/ x y) 0) (> (/ x y) 0)))
	  ("Subgoal 2"
	   :in-theory (set-difference-theories (enable <-+-negative-0-1
						       <-+-negative-0-2
						       normalize-<-/-to-*-3)
					       '(floor-bounded-by-/))
	   :use (:instance floor-bounded-by-/ (x x) (y y)))))

(defsection floor-type-linear
  :parents (floor-lemmas)
  :short "A theory of the :LINEAR rules created by the lemmas FLOOR-TYPE-1 and
  FLOOR-TYPE-2."
  :long "<p>These rules are implicated in thrashing linear arithmetic, so we
  provide this theory which can be DISABLED if it becomes a problem.</p>"

  (deftheory floor-type-linear
    '((:linear floor-type-1 . 1)
      (:linear floor-type-1 . 2)
      (:linear floor-type-2 . 1)
      (:linear floor-type-2 . 2))))

;< These follow immediately from the definition of FLOOR.  If we enter
;these lemmas in a theory that includes the :LINEAR rules for FLOOR we will
;observe severe thrashing in linear arithmetic, since these rules are
;independent of the signs of x and y.  So, we'll just prove them in the theory
;that prevails at the beginning of this book.

(encapsulate ()

  (local (in-theory (current-theory 'begin-quotient-remainder-lemmas)))
  (local (in-theory (enable floor)))

  (defrule floor-minus
    :parents (floor-lemmas)
    :short "Rewrite @('(FLOOR (- x) y)')."
    (and (implies (qr-guard x y)
                  (equal (floor (- x) y)
                         (if (integerp (* x (/ y)))
                             (- (floor x y))
                           (- (- (floor x y)) 1))))
         (implies (qr-guard x y)
                  (equal (floor x (- y))
                         (if (integerp (* x (/ y)))
                             (- (floor x y))
                           (- (- (floor x y)) 1)))))
    :enable denominator-unary-minus)

  (defruled rewrite-floor-x*y-z-left
    :parents (floor-lemmas)
    :short "Rewrite (D): @('(FLOOR (* x y) z) = (FLOOR y (/ z x))'), when x /= 0."
    :long "<p>Since we don't presume any rewriting strategy for / vis-a-vis *,
  this often useful rule is exported DISABLEd.</p>"
    (implies (and (real/rationalp x)
                  (not (equal x 0))
                  (real/rationalp y)
                  (force (real/rationalp z))
                  (force (not (equal z 0))))
             (equal (floor (* x y) z)
                    (floor y (/ z x)))))

  (defruled rewrite-floor-x*y-z-right
    :parents (floor-lemmas)
    :short "Rewrite: @('(FLOOR (* x y) z) = (FLOOR x (/ z y))'), when y /= 0."
    :long "<p>Since we don't presume any rewriting strategy for / vis-a-vis *,
  this often useful rule is exported DISABLEd.</p>"
    (implies (and (real/rationalp x)
                  (real/rationalp y)
                  (not (equal y 0))
                  (force (real/rationalp z))
                  (force (not (equal z 0))))
             (equal (floor (* x y) z)
                    (floor x (/ z y)))))

  (defrule floor-cancel-*
    :parents (floor-lemmas)
    :short "Rewrite: @('(FLOOR (* x y) y) = (FLOOR x 1)')."
    (implies (qr-guard x y)
             (and (equal (floor (* x y) y) (floor x 1))
                  (equal (floor (* y x) y) (floor x 1)))))

  (defrule floor-cancel-*-2
    :parents (floor-lemmas)
    :short "Rewrite: @('(FLOOR (* x y) (* x z)) = (FLOOR y z)')."
    (implies (and (real/rationalp x)
                  (not (equal x 0))
                  (real/rationalp y)
                  (real/rationalp z)
                  (not (equal z 0)))
             (equal (floor (* x y) (* x z))
                    (floor y z)))
    :enable rewrite-floor-x*y-z-left))

;  This proof is only this complicated because I wanted to prove the most
;  general thing possible.

(encapsulate ()

  (local
   (defthm crock0
     (implies (and (< 1 y)
                   (< 0 x)
                   (qr-guard x y))
              (< (/ x y) x))
     :rule-classes :linear))

  (local
   (defthm crock1
     (implies (and (<= (+ 1 1) y)
                   (< x 0)
                   (qr-guard x y))
              (<= (* x y) (+ x x)))
     :rule-classes :linear
     :hints (("Goal" :in-theory (disable <-*-left-cancel (binary-+))
	      :use (:instance <-*-left-cancel (z x) (x 2) (y y))))))

  (local
   (defthm crock2
     (implies (and (<= 2 y)
                   (< x 0)
                   (< y (- x))
                   (qr-guard x y))
              (< x (- (/ x y) 1)))
     :rule-classes :linear
     :hints
     (("Goal"
       :in-theory (e/d (prefer-*-to-/) (<-*-left-cancel))
       :use (:instance <-*-left-cancel (z y) (x x) (y (- (/ x y) 1)))))))

  (defrule floor-recursion
    :parents (floor-lemmas)
    :short "Rewrite: @('(FLOOR x y) < x'), when @('x > 0') and @('y > 1');
           @('x < (FLOOR x y)'), when @('x < -1') and @('y >= 2')."
    :long "<p>This theorem justifies recursion by FLOOR using the measure
  ACL2-COUNT, which for integers i is simply (ABS i).  Thus, this theorem won't
  justify a simple recursion by a negative y, since (FLOOR 1 y) = -1 for
  negative y, and (ABS -1) = (ABS 1).  For the most general case that includes
  negative y one would need to define a different measure that could handle
  this condition.</p>"
    ;; [Jared]: renamed from justify-floor-recursion to simply floor-recursion on
    ;; 2014-07-29, to avoid name conflict with arithmetic-5
    (implies (qr-guard x y)
             (and (implies (and (< 0 x)
                                (< 1 y))
                           (< (floor x y) x))
                  (implies (and (< x -1)
                                (<= 2 y))
                           (< x (floor x y)))))
    :hints (("Goal" :use ((:instance floor-bounded-by-/ (x x) (y y))))
            ("Goal'" :cases ((< 0 x) (< y (- x)))))))


;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;;
;;;    MOD
;;;
;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

(defruled linearize-mod
  :parents (mod-lemmas)
  :short "Rewrite (D): Transform @('(MOD x y) < z'), @('(MOD x y) > z'), and
  @('(MOD x y) = z') into an equivalent FLOOR expression suitable for reasoning
  about with FLOOR-BOUNDED-BY-/ and other theorems about FLOOR."
  :long "<p>Since this lemma can be considered a `definition' of MOD, it is
  exported DISABLED.</p>"
  (implies (and (qr-guard x y)
                (force (real/rationalp z)))
           (and (equal (< (mod x y) z)
                       (if (> y 0)
                           (< (- (/ x y) (floor x y)) (/ z y))
                         (> (- (/ x y) (floor x y)) (/ z y))))
                (equal (> (mod x y) z)
                       (if (> y 0)
                           (> (- (/ x y) (floor x y)) (/ z y))
                         (< (- (/ x y) (floor x y)) (/ z y))))
                (equal (equal (mod x y) z)
                       (equal (- (/ x y) (floor x y)) (/ z y)))))
  :enable (mod prefer-*-to-/))

(defrule mod-=-0
  :parents (mod-lemmas)
  :short "Rewrite: @('(MOD x y) = 0'), when x/y is an integer."
  :long "<p>This rule is a corollary of a more general equality.  The equality
  is also stored as a :REWRITE and :GENERALIZE rule.</p>"
  (implies (qr-guard x y)
           (equal (equal (mod x y) 0)
                  (integerp (/ x y))))
  :rule-classes ((:rewrite)
                 (:generalize)
                 (:rewrite :corollary (implies (and (integerp (/ x y))
                                                    (qr-guard x y))
                                               (equal (mod x y) 0))))
  :enable linearize-mod
  :disable commutativity-of-*)

(defrule mod-x-y-=-x-for-rationals
  :parents (mod-lemmas)
  :short "Rewrite: @('(MOD x y) = x'), when @('|x| <= |y|') and x and y have
  the same sign."
  :long "<p>This rule is a corollary of a more general equality which is also
  stored as :REWRITE and :GENERALIZE rules.</p>"
  (implies (qr-guard x y)
           (equal (equal (mod x y) x)
                  (or (and (>= x 0) (> y 0) (< x y))
                      (and (<= x 0) (< y 0) (> x y)))))
  :rule-classes
  ((:rewrite)
   (:generalize)
   (:rewrite :corollary (implies (and (>= x 0) (> y 0) (< x y) (qr-guard x y))
                                 (equal (mod x y) x)))
   (:rewrite :corollary (implies (and (<= x 0) (< y 0) (> x y) (qr-guard x y))
                                 (equal (mod x y) x))))
  :enable linearize-mod)

;<  Again, we need to :USE FLOOR-BOUNDED-BY-/ to make this proof quick.

(encapsulate nil

   (local (defthm another-crock
	    (equal (equal (- x) 1) (equal x -1))))

   (defrule mod-x-y-=-x+y-for-rationals
     :parents (mod-lemmas)
     :short "Rewrite: @('(MOD x y) = x + y'), when @('|x| <= |y|') and x and y
  have different signs and x /= 0."
     :long "<p>This rule is a corollary of a more general equality which is
  also stored as :REWRITE and :GENERALIZE rules.</p>"
     (implies (qr-guard x y)
              (equal (equal (mod x y) (+ x y))
                     (or (and (> x 0) (< y 0) (<= x (- y)))
                         (and (< x 0) (> y 0) (<= (- x) y)))))
     :rule-classes
     ((:rewrite)
      (:generalize)
      (:rewrite :corollary (implies (and (> x 0) (< y 0) (<= x y) (qr-guard x y))
                                    (equal (mod x y) (+ x y))))
      (:rewrite :corollary (implies (and (< x 0) (> y 0) (<= (- x) y) (qr-guard x y))
                                    (equal (mod x y) (+ x y)))))
     :in-theory (e/d (linearize-mod) (floor-bounded-by-/))
     :use floor-bounded-by-/))

;; Added the :rule-classes :rewrite as this seems necessary at times.
;; A. Flatau  1-Dec-1994
;; Changed variable names from i, j to m, n to match RTL and arithmetic-5 --
;; Sol Swords 1/2011
(defrule integerp-mod
  :parents (mod-lemmas)
  :short "Type-Prescription: @('(MOD m n)') is an integer, when m and n are
  integers."
  (implies (and (integerp m)
                (integerp n))
           (integerp (mod m n)))
  :enable mod
  :rule-classes (:rewrite :type-prescription))

(defrule mod-bounded-by-modulus
  :parents (mod-lemmas)
  :short "Linear: Useful forms of the fact that @('|(MOD x y)| < |y|')."
  :long "<p>This lemma is also stored as a :GENERALIZE rule.</p>"
  (and (implies (and (> y 0)
                     (qr-guard x y))
                (< (mod x y) y))
       (implies (and (< y 0)
                     (qr-guard x y))
                (> (mod x y) y)))
  :rule-classes ((:linear :trigger-terms ((mod x y)))
                 (:generalize))
  :enable linearize-mod
  :disable floor-bounded-by-/
  :use floor-bounded-by-/)

(defrule mod-type
  :parents (mod-lemmas)
  :short "Decide @('(MOD x y) < 0') and @('(MOD x y) > 0') based on the sign of
  y and the INTEGERP-ness of x/y."
  (implies (qr-guard x y)
           (and (equal (< (mod x y) 0)
                       (and (< y 0)
                            (not (integerp (/ x y)))))
                (equal (> (mod x y) 0)
                       (and (> y 0)
                            (not (integerp (/ x y)))))))
  :rule-classes
  ((:rewrite)
   (:generalize)
   (:linear :corollary (implies (and (< y 0)
                                     (not (integerp (/ x y)))
                                     (qr-guard x y))
                                (< (mod x y) 0)))
   (:linear :corollary (implies (and (> y 0)
                                     (not (integerp (/ x y)))
                                     (qr-guard x y))
                                (> (mod x y) 0)))
   (:linear :corollary (implies (and (<= y 0)
                                     (qr-guard x y))
                                (<= (mod x y) 0)))
   (:linear :corollary (implies (and (>= y 0)
                                     (qr-guard x y))
                                (>= (mod x y) 0)))
   (:type-prescription :corollary (implies (and (< y 0)
                                                (not (integerp (/ x y)))
                                                (qr-guard x y))
                                           (< (mod x y) 0)))
   (:type-prescription :corollary (implies (and (> y 0)
                                                (not (integerp (/ x y)))
                                                (qr-guard x y))
                                           (> (mod x y) 0)))
   (:type-prescription :corollary (implies (and (<= y 0)
                                                (qr-guard x y))
                                           (<= (mod x y) 0)))
   (:type-prescription :corollary (implies (and (>= y 0)
                                                (qr-guard x y))
                                           (>= (mod x y) 0))))
  :enable linearize-mod
  :disable (floor-type-3 floor-type-1 (:type-prescription floor))
  :use floor-bounded-by-/)

(defsection mod-type-linear
  :parents (mod-lemmas)
  :short "A theory of the :LINEAR rules created by the lemma MOD-TYPE."
  :long "<p>These rules are implicated in thrashing linear arithmetic, so we
  provide this theory which can be DISABLED if it becomes a problem.</p>"

  (deftheory mod-type-linear
    '((:linear mod-type . 1)
      (:linear mod-type . 2)
      (:linear mod-type . 3)
      (:linear mod-type . 4))))

(defrule mod-minus
  :parents (mod-lemmas)
  :short "Rewrite: @('(MOD (- x) y)') and @('(MOD x (- y))')."
  (implies (qr-guard x y)
           (and (equal (mod (- x) y)
                       (if (integerp (/ x y))
                           0
                         (- y (mod x y))))
                (equal (mod x (- y))
                       (if (integerp (/ x y))
                           0
                         (- (mod x y) y)))))
  :enable linearize-mod
  :expand (mod x y))

(encapsulate ()

  (local (in-theory (current-theory 'begin-quotient-remainder-lemmas)))

  (defthm simplify-mod-*
    (implies (and (integerp x)
                  (not (equal x 0))
                  (integerp y)
                  (integerp z)
                  (not (equal z 0)))
             (equal (mod (* x y) (* x z))
                    (* x (mod y z))))
    :hints (("Goal" :in-theory (enable mod floor-cancel-*-2)))))


;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;;
;;;    Addition Cancellation theory for FLOOR and MOD
;;;
;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;< This next section of lemmas has nothing to do with the :LINEAR theory of
;FLOOR and MOD, so we DISABLE the key :LINEAR lemmas to avoid thrashing.

(local (in-theory (disable floor-bounded-by-/ floor-type-1 floor-type-2
			   floor-type-3 floor-type-4 mod-bounded-by-modulus mod-type)))

;  These LOCAL theorems will be superceded by CANCEL-FLOOR-+-BASIC,
;  CANCEL-FLOOR-+-3, CANCEL-MOD-+-BASIC, and CANCEL-MOD-+-3.

(local
 (defthm floor-x+i*y-y
   (implies
    (and (integerp i)
	 (qr-guard x y))
    (and
     (equal (floor (+ x (* i y)) y)
	    (+ i (floor x y)))
     (equal (floor (+ x (* y i)) y)
	    (+ i (floor x y)))
     (equal (floor (- x (* i y)) y)
	    (- (floor x y) i))
     (equal (floor (- x (* y i)) y)
	    (- (floor x y) i))))
   :hints
   (("Goal"
     :use ((:instance floor-bounded-by-/ (x (+ x (* i y))) (y y))
	   (:instance floor-bounded-by-/ (x (- x (* i y))) (y y))
	   (:instance floor-bounded-by-/ (x x) (y y)))))))

(local
 (defthm floor-x+y+i*z-z
   (implies
    (and (integerp i)
	 (force (real/rationalp x))
	 (qr-guard y z))
    (and
     (equal (floor (+ x y (* i z)) z)
	    (+ i (floor (+ x y) z)))
     (equal (floor (+ x y (* z i)) z)
	    (+ i (floor (+ x y) z)))
     (equal (floor (+ x y (- (* z i))) z)
	    (- (floor (+ x y) z) i))
     (equal (floor (+ x y (- (* i z))) z)
	    (- (floor (+ x y) z) i))))
   :hints
   (("Goal"
     :in-theory (disable floor-x+i*y-y)
     :use ((:instance floor-x+i*y-y (x (+ x y)) (y z)))))))

(local
 (defthm mod-x+i*y-y
   (implies
    (and (integerp i)
	 (qr-guard x y))
    (and
     (equal (mod (+ x (* i y)) y)
	    (mod x y))
     (equal (mod (+ x (* y i)) y)
	    (mod x y))
     (equal (mod (+ x (- (* i y))) y)
	    (mod x y))
     (equal (mod (+ x (- (* y i))) y)
	    (mod x y))))
   :hints
   (("Goal"
     :in-theory (enable mod)))))

(local
 (defthm mod-x+y+i*z-z
   (implies
    (and (integerp i)
	 (force (real/rationalp x))
	 (qr-guard y z))
    (and
     (equal (mod (+ x y (* i z)) z)
	    (mod (+ x y) z))
     (equal (mod (+ x y (* z i)) z)
	    (mod (+ x y) z))
     (equal (mod (+ x y (- (* i z))) z)
	    (mod (+ x y) z))
     (equal (mod (+ x y (- (* z i))) z)
	    (mod (+ x y) z))))
   :hints
   (("Goal"
     :in-theory (disable mod-x+i*y-y)
     :use ((:instance mod-x+i*y-y (x (+ x y)) (y z)))))))

(encapsulate ()

  (local
   (defthm floor-+-crock
     (implies
      (and (real/rationalp x)
	   (real/rationalp y)
	   (real/rationalp z)
	   (syntaxp (and (eq x 'x) (eq y 'y) (eq z 'z))))
      (equal (floor (+ x y) z)
	     (floor (+ (+ (mod x z) (mod y z))
		       (* (+ (floor x z) (floor y z)) z)) z)))
     :hints(("Goal" :in-theory (disable mod-x-y-=-x+y-for-rationals
                                        mod-x-y-=-x-for-rationals)))))

  (defruled floor-+
    :parents (floor-lemmas)
    :short "Rewrite (D): @('(FLOOR (+ x y) z)')."
    :long "<p>As this rule could easily loop it is exported DISABLEd.  Don't
   ENABLE this lemma unless you are sure that the FLOOR/MOD term will simplify,
   or else put SYNTAXP guards on the variables x, y, and/or z.</p>"
    (implies (and (force (real/rationalp x))
                  (force (real/rationalp y))
                  (force (real/rationalp z))
                  (force (not (equal z 0))))
             (equal (floor (+ x y) z)
                    (+ (floor (+ (mod x z) (mod y z)) z)
                       (+ (floor x z) (floor y z)))))
    :in-theory (union-theories (disable associativity-of-+
                                        commutativity-2-of-+
                                        associativity-of-*
                                        commutativity-2-of-*
                                        distributivity)
                               '(rationalp-+
                                 #+non-standard-analysis
                                 realp-+
                                 mod))))

(encapsulate ()

  (local
   (defthm mod-+-crock
     (implies
      (and (real/rationalp x)
	   (real/rationalp y)
	   (real/rationalp z)
	   (not (equal z 0))
	   (syntaxp (and (eq x 'x) (eq y 'y) (eq z 'z))))
      (equal (mod (+ x y) z)
	     (mod (+ (+ (mod x z) (mod y z))
		     (* (+ (floor x z) (floor y z)) z)) z)))
     :hints(("Goal" :in-theory (disable mod-x-y-=-x+y-for-rationals
                                        mod-x-y-=-x-for-rationals)))))

  (defruled mod-+
    :parents (mod-lemmas)
    :short "Rewrite (D): @('(MOD (+ x y) z)')."
    :long "<p>As this rule could easily loop it is exported DISABLEd.  Don't
   ENABLE this lemma unless you are sure that the MOD/MOD term will simplify,
   or else put SYNTAXP guards on the variables x, y, and/or z.</p>"
    (implies (and (force (real/rationalp x))
                  (force (real/rationalp y))
                  (force (real/rationalp z))
                  (force (not (equal z 0))))
             (equal (mod (+ x y) z)
                    (mod (+ (mod x z) (mod y z)) z)))
    :in-theory (union-theories (disable associativity-of-+
                                        commutativity-2-of-+
                                        associativity-of-*
                                        commutativity-2-of-*
                                        distributivity)
                               '(rationalp-+
                                 #+non-standard-analysis
                                 realp-+
                                 mod))))

(encapsulate ()

  (local
   (defthm crock0
     (implies
      (and (integerp i)
	   (integerp (* x y)))
      (integerp (* x y i)))))

  (defrule rewrite-floor-mod
    :parents (floor-lemmas)
    :short "Rewrite: @('(FLOOR (MOD x y) z) = (FLOOR x z) - i*(FLOOR x y)'),
    when i = y/z is an integer."
    (implies (and (equal i (/ y z))
                  (integerp i)
                  (qr-guard x y)
                  (qr-guard x z))
             (equal (floor (mod x y) z)
                    (- (floor x z) (* i (floor x y)))))
    :enable mod
    :use ((:instance floor-+ (x x) (y (- (* y (floor x y)))) (z z))))

  (defrule rewrite-mod-mod
    :parents (mod-lemmas)
    :short "Rewrite: (MOD (MOD x y) z) = (MOD x z), when y/z is an integer."
    (implies (and (equal i (/ y z))
                  (integerp i)
                  (qr-guard x y)
                  (qr-guard y z))
             (equal (mod (mod x y) z)
                    (mod x z)))
    :expand ((mod x y) (mod x z))
    :use ((:instance mod-+ (x x) (y (- (* y (floor x y)))) (z z)))))

(defrule cancel-floor-+-basic
  :parents (floor-lemmas)
  :short "Rewrite: @('(FLOOR (+ x y) z) = x/z + (FLOOR y z)'), when x/z is an
  integer; also the commutative form."
  ;; [Jared] modified on 2014-07-29: this was originally called CANCEL-FLOOR-+
  ;; but that name clashes with an arithmetic-5 rule that appears to be more
  ;; sophisticated (it uses bind-free to find cancelling addends, etc.), so I
  ;; am renaming this to cancel-floor-+-basic.
  (implies (and (equal i (/ x z))
                (integerp i)
                (force (real/rationalp x))
                (force (real/rationalp y))
                (force (real/rationalp z))
                (force (not (equal z 0))))
           (and (equal (floor (+ x y) z) (+ i (floor y z)))
                (equal (floor (+ y x) z) (+ i (floor y z)))))
  :enable floor-+)

(defrule cancel-floor-+-3
  :parents (floor-lemmas)
  :short "Rewrite: @('(FLOOR (+ w x y) z) = y/z + (FLOOR (+ w x) z)'), when y/z
  is an integer."
  (implies (and (equal i (/ y z))
                (integerp i)
                (force (real/rationalp w))
                (force (real/rationalp x))
                (force (real/rationalp y))
                (force (real/rationalp z))
                (force (not (equal z 0))))
           (equal (floor (+ w x y) z)
                  (+ i (floor (+ w x) z))))
  :disable cancel-floor-+-basic
  :use ((:instance cancel-floor-+-basic (x y) (y (+ w x)) (z z))))

(defrule cancel-mod-+-basic
  :parents (mod-lemmas)
  :short "Rewrite: @('(MOD (+ x y) z) = (MOD y z)'), when x/z is an integer;
  also the commutative form."
  ;; [Jared] modified on 2014-07-29: this was originally called CANCEL-MOD-+
  ;; but that name clashes with an arithmetic-5 rule that appears to be more
  ;; sophisticated (it uses bind-free to find cancelling addends, etc.), so I
  ;; am renaming this to cancel-floor-+-basic.
  (implies (and (equal i (/ x z))
                (integerp i)
                (force (real/rationalp x))
                (force (real/rationalp y))
                (force (real/rationalp z))
                (force (not (equal z 0))))
           (and (equal (mod (+ x y) z) (mod y z))
                (equal (mod (+ y x) z) (mod y z))))
  :enable mod-+)

(defrule cancel-mod-+-3
  :parents (mod-lemmas)
  :short "Rewrite: @('(MOD (+ w x y) z) = (MOD (+ w x) z)'), when y/z is an
  integer."
  (implies (and (equal i (/ y z))
                (integerp i)
                (force (real/rationalp w))
                (force (real/rationalp x))
                (force (real/rationalp y))
                (force (real/rationalp z))
                (force (not (equal z 0))))
           (equal (mod (+ w x y) z)
                  (mod (+ w x) z)))
  :disable cancel-mod-+-basic
  :use ((:instance cancel-mod-+-basic (x y) (y (+ w x)) (z z))))

; [Jared] bozo this looks kind of expensive...

(defthm not-rationalp-rationalp-plus
 (implies (and (acl2-numberp x)
               (rationalp y)
               (not (rationalp x)))
          (not (rationalp (+ x y))))
 :hints (("Goal" :use ((:instance rationalp-+ (x (+ x y)) (y (- y)))))))

(defthm not-rationalp-rationalp-unary---plus
 (implies (and (acl2-numberp x)
               (rationalp y)
               (not (rationalp (- x))))
          (not (rationalp (+ x y))))
 :hints (("Goal" :use ((:instance rationalp-+ (x (+ x y)) (y (- y))))
	  :in-theory (enable rationalp-unary--))))

#+non-standard-analysis
(defthm not-realp-realp-plus
 (implies (and (acl2-numberp x)
               (realp y)
               (not (realp x)))
          (not (realp (+ x y))))
 :hints (("Goal" :use ((:instance realp-+ (x (+ x y)) (y (- y)))))))

#+non-standard-analysis
(defthm not-realp-realp-unary---plus
 (implies (and (acl2-numberp x)
               (realp y)
               (not (realp (- x))))
          (not (realp (+ x y))))
 :hints (("Goal" :use ((:instance realp-+ (x (+ x y)) (y (- y))))
	  :in-theory (enable realp-unary--))))

(encapsulate nil

  (local (defthm simplify-mod-+-mod-crock
           (equal (equal (* a b) (+ y z))
                  (equal (fix z) (- (* a b) y)))))

  (defrule simplify-mod-+-mod
    :parents (mod-lemmas)
    :short "Rewrite @('(MOD (+ w (MOD x y)) z)') and similar, where y/z is an integer."
    (implies (and (equal i (/ y z))
                  (integerp i)
                  (qr-guard x y)
                  (qr-guard w z))
             (and (equal (mod (+ w (mod x y)) z) (mod (+ w x) z))
                  (equal (mod (+ (mod x y) w) z) (mod (+ w x) z))
                  (equal (mod (- w (mod x y)) z) (mod (- w x) z))
                  (equal (mod (- (mod x y) w) z) (mod (- x w) z))))
    :hints(("Goal" :in-theory (disable mod-x-y-=-x+y-for-rationals
                                       mod-x-y-=-x-for-rationals
                                       integerp-mod
                                       integerp-+-minus-*
                                       mod-=-0)))))

(defrule mod-+-cancel-0
  :parents (mod-lemmas)
  :short "Rewrite @('(equal (mod (+ x y) z) x)')."
  (implies (and (qr-guard x z)
                (qr-guard y z))
           (equal (equal (mod (+ x y) z) x)
                  (and (equal (mod y z) 0)
                       (equal (mod x z) x))))
  :disable (left-cancellation-for-* equal-*-/-2)
  :use ((:instance left-cancellation-for-*
         (z (/ z)) (x y) (y (* z (floor (+ x y) z)))))
  :expand ((mod (+ x y) z)))

(local (in-theory (enable floor-type-1 floor-type-2 floor-type-3 floor-type-4
			  floor-bounded-by-/ mod-type mod-bounded-by-modulus)))


;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;;
;;;    Positive integer theory for FLOOR and MOD
;;;
;;;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;;;
;;;    The following is a proof of the theorem
;;;
;;;      (implies
;;;       (and (integerp i)
;;;            (integerp j)
;;;            (< 0 i)
;;;            (< 0 j)
;;;            (real/rationalp x))
;;;       (equal (floor (floor x i) j)
;;;      	 (floor x (* i j))))).
;;;
;;;    I believe that this is the most general, or at least the most
;;;    generally useful form of this result. E.g., it's not true for negative
;;;    J and K.  This theorem is used to justify a recursive definition of
;;;    "shifting" integers, i.e.,
;;;
;;;    (equal (floor i (expt 2 j)) (floor (floor i 2) (expt 2 (- j 1))))
;;;
;;;    for J > 0.

#-non-standard-analysis
(defthm rationalp-mod
  (implies (rationalp x)
           (rationalp (mod x y)))
  :rule-classes (:rewrite :type-prescription)
  :hints(("Goal"
          :cases ((rationalp y))
          :in-theory (enable mod floor))))

#+non-standard-analysis
(defthm rationalp-mod
  (implies (and (rationalp x)
		(rationalp y))
	   (rationalp (mod x y)))
  :hints (("Goal" :in-theory (enable mod rationalp-+)))
  :rule-classes (:rewrite :type-prescription))

#+non-standard-analysis
(defrule realp-mod
  (implies (real/rationalp x)
	   (real/rationalp (mod x y)))
  :cases ((not (acl2-numberp y))
          (real/rationalp y))
  :enable (mod floor realp-+)
  :rule-classes (:rewrite :type-prescription))

(encapsulate ()

  ;;  This proof of FLOOR-FLOOR-INTEGER is an elaborate rewriting trick,
  ;;  which is spoiled by these 2 lemmas!

  (local (in-theory (disable rewrite-floor-mod rewrite-mod-mod)))

  ;;< These first 2 lemmas have nothing to do with the :LINEAR theory of
  ;;FLOOR and MOD, so we DISABLE the key :LINEAR lemmas to avoid thrashing.

  (local (in-theory (disable floor-type-1 floor-type-2 floor-type-3
			     floor-type-4 floor-bounded-by-/ mod-type mod-bounded-by-modulus)))

  ;; First, write x as a quotient and remainder of i*j.

  (local
   (defthm floor-floor-integer-crock0
     (implies
      (and (real/rationalp x)
	   (integerp i)
	   (not (equal i 0))
	   (integerp j)
	   (not (equal j 0))
	   (syntaxp (and (eq x 'x) (eq i 'i) (eq j 'j))))
      (equal (floor (floor x i) j)
	     (floor (floor (+ (mod x (* i j))
			      (* (* i j) (floor x (* i j)))) i)
		    j)))
     :hints (("Goal" :in-theory (disable commutativity-2-of-+
					 commutativity-2-of-*
					 associativity-of-*)))))

  ;;  Next, divide out i and j through the sums.

  (local
   (defthm floor-floor-integer-crock1
     (implies
      (and (real/rationalp x)
	   (integerp i)
	   (not (equal i 0))
	   (integerp j)
	   (not (equal j 0))
	   (syntaxp (and (eq x 'x) (eq i 'i) (eq j 'j))))
      (equal (floor (floor x i) j)
	     (+ (floor x (* i j)) (floor (floor (mod x (* i j)) i) j))))
     :hints
     (("Goal"
       :in-theory (disable floor-mod-elim)))))

  ;;< This proof takes 20 sec. with no splitting. We need to re-ENABLE the
  ;;type lemmas to make it work. It could probably be speeded up by
  ;;DISABLEing selected parts of the :LINEAR theory of FLOOR and MOD.

  (local
   (defthm floor-floor-integer-crock2
     (implies
      (and (real/rationalp x)
	   (integerp i)
	   (< 0 i)
	   (integerp j)
	   (< 0 j))
      (equal (floor (floor (mod x (* i j)) i) j)
	     0))
     :hints (("Goal" :in-theory
	      (set-difference-theories (enable floor-type-1
					       floor-type-2
					       floor-type-3
					       mod-type)
				       '(floor-bounded-by-/ mod-bounded-by-modulus
						      <-*-left-cancel
						      <-*-/-left-commuted))
       :use ((:instance floor-bounded-by-/ (x (mod x (* i j))) (y i))
	     (:instance mod-bounded-by-modulus (x x) (y (* i j)))
	     (:instance <-*-left-cancel
			(z (/ i)) (x (mod x (* i j))) (y (* i j))))))))

  ;; Voila!

  (defrule floor-floor-integer
    :parents (floor-lemmas)
    :short "Rewrite: @('(FLOOR (FLOOR x i) j) = (FLOOR x (* i j))') for integers i,j &gt; 0."
    (implies (and (integerp i)
                  (integerp j)
                  (< 0 i)
                  (< 0 j)
                  (real/rationalp x))
             (equal (floor (floor x i) j)
                    (floor x (* i j))))))

(defrule floor-x+i*k-i*j
  :parents (floor-lemmas)
  :short "Rewrite: @('(FLOOR (+ x (* i k)) (* i j)) = (FLOOR k j)'), when
  i,j &gt; 0 and 0 &lt;= x &lt; i."
  :long "<p>This is a crucial lemma for certain kinds of reasoning about hardware
  specifications, and is used to prove @(see MOD-x+i*j-i*k).</p>"
  (implies (and (force (real/rationalp x))
                (force (integerp i))
                (force (integerp j))
                (force (integerp k))
                (< 0 i)
                (< 0 j)
                (<= 0 x)
                (< x i))
           (equal (floor (+ x (* i k)) (* i j))
                  (floor k j)))
  :disable (floor-floor-integer floor-+)
  :use ((:instance floor-floor-integer (x (+ x (* i k))) (i i) (j j))
        (:instance floor-+ (x x) (y (* i k)) (z i))))

(defrule mod-x+i*k-i*j
  :parents (mod-lemmas)
  :short "Rewrite: @('(MOD (+ x (* i k)) (* i j)) = (+ x (* i (MOD k j)))'),
  when i,j &gt; 0 and 0 &lt;= x &lt; i."
  :long "<p>This is a crucial lemma for certain kinds of
  reasoning about hardware specifications, for example, we can use this to
  prove that</p>
  @({
       (MOD i (EXPT 2 n)) = (MOD i 2) + (MOD (FLOOR i 2) (EXPT 2 (1- n)))
  })
  <p>for @('n > 0'), which justifies a recursive specification of hardware
  operations.</p>"
  (implies (and (force (real/rationalp x))
                (force (integerp i))
                (force (integerp j))
                (force (integerp k))
                (< 0 i)
                (< 0 j)
                (<= 0 x)
                (< x i))
           (equal (mod (+ x (* i k)) (* i j))
                  (+ x (* i (mod k j)))))
  :enable mod)

(encapsulate ()

  (local (in-theory (disable floor-type-1 floor-type-2 floor-type-3
			     floor-type-4 floor-bounded-by-/)))

  (local
   (defthm mod-x-i*j-crock
     (implies
      (and (> i 0)
	   (> j 0)
	   (force (integerp i))
	   (force (integerp j))
	   (force (real/rationalp x)))
      (equal (mod (+ (mod x i) (* i (floor x i))) (* i j))
	     (+ (mod x i) (* i (mod (floor x i) j)))))
     :rule-classes nil
     :hints (("Goal" :in-theory (disable floor-mod-elim)))))

  (defthm mod-x-i*j-of-positives
    (implies (and (> i 0)
                  (> j 0)
                  (force (integerp i))
                  (force (integerp j))
                  (force (real/rationalp x)))
             (equal (mod x (* i j))
                    (+ (mod x i) (* i (mod (floor x i) j)))))
    :hints (("Goal" :use mod-x-i*j-crock))))


;;;****************************************************************************
;;;
;;;  Misc.
;;;
;;;****************************************************************************

;;  This is a nice "quotient" theorem -- If J is an integer and I/J is an
;;  integer, then I is also an integer, namely J*(FLOOR I J).  It was proved
;;  as part of en earlier pass at this book, and although it's not used
;;  anymore, I like it so I'm leaving it in.

(encapsulate ()

  (local
   (defthm crock0
     (implies
      (and (integerp (/ i j))
	   (real/rationalp i)
	   (integerp j)
	   (not (equal 0 j)))
      (integerp (+ (* j (floor i j)) (mod i j))))
     :rule-classes nil
     :hints
     (("Goal"
       :in-theory (disable floor-=-x/y)))))

  (defrule integerp-i/j-integerp-forward
    :parents (integer-ratio-lemmas)
    :short "Forward: If i/j is an integer and j is an integer, then i is an
    integer."
    (implies (and (integerp (/ i j))
                  (real/rationalp i)
                  (integerp j)
                  (not (zerop j)))
             (integerp i))
    :use ((:instance crock0))
    :disable (mod-=-0 floor-=-x/y)
    :rule-classes
    ((:forward-chaining :corollary (implies (and (integerp (/ i j))
                                                 (force (real/rationalp i))
                                                 (integerp j)
                                                 (force (not (equal 0 j))))
                                            (integerp i)))
     (:forward-chaining :corollary (implies (and (integerp (* (/ j) i))
                                                 (force (real/rationalp i))
                                                 (integerp j)
                                                 (force (not (equal 0 j))))
                                            (integerp i))))))


;;;****************************************************************************
;;;
;;;    THEORIES -- A couple of exported theories.
;;;
;;;****************************************************************************

(defxdoc quotient-remainder-theories
  :parents (quotient-remainder-lemmas)
  :short "Logical theories supplied by the QUOTIENT-REMAINDER book."

  :long "<p>The QUOTIENT-REMAINDER book exports 2 theories:
  QUOTIENT-REMAINDER-FUNCTIONS and QUOTIENT-REMAINDER-RULES.  The former is
  simply a theory of the functions characterized by the book.  Since these
  functions are all ENABLEd by default, and most are non-recursive, one should
  immediately:</p>

  @({
     (in-theory (disable quotient-remainder-functions))
  })

  <p>upon loading this book, or the lemmas may never be applied.</p>

  <p>QUOTIENT-REMAINDER-RULES is a theory of all of the lemmas exported by this
  book which are ENABLEd by default.  You can \"turn off\" this book after it
  is loaded by</p>

  @({
      (in-theory (disable quotient-remainder-rules))
  })")

(defsection quotient-remainder-functions
  :parents (quotient-remainder-theories)
  :short "A theory of the function symbols characterized by
  \"quotient-remainder-lemmas\"."
  :long "<p>You should DISABLE this theory immediately after loading this
  book.</p>"

  (deftheory quotient-remainder-functions
    '(nonnegative-integer-quotient floor mod truncate rem)))

(defsection quotient-remainder-rules
  :parents (quotient-remainder-theories)
  :short "A theory of all rules exported ENABLEd by the
  \"quotient-remainder-lemmas\" book."

  (deftheory quotient-remainder-rules
    (union-theories
     (defun-type/exec-theory
       '(NONNEGATIVE-INTEGER-QUOTIENT FLOOR MOD TRUNCATE REM))
     (set-difference-theories (current-theory :here)
                              (current-theory 'begin-quotient-remainder-lemmas)))))