summaryrefslogtreecommitdiff
path: root/backends/hol4/primitivesScript.sml
blob: 82da4de9408708cd583a9fc8c24a0b66c92c368e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
open stringTheory
open primitivesArithTheory primitivesBaseTacLib ilistTheory

val _ = new_theory "primitives"

(*** Result *)
Datatype:
  error = Failure
End

Datatype:
  result = Return 'a | Fail error | Diverge
End

Type M = ``: 'a result``

Definition bind_def:
  (bind : 'a M -> ('a -> 'b M) -> 'b M) x f =
    case x of
      Return y => f y
    | Fail e => Fail e
    | Diverge => Diverge
End

val bind_name = fst (dest_const “bind”)

Theorem bind_return_fail_div_eq:
  (bind (Return x) f = f x)  (bind (Fail e) f = Fail e)  (bind Diverge f = Diverge)
Proof
  fs [bind_def]
QED
val _ = BasicProvers.export_rewrites ["bind_return_fail_div_eq"]

Definition return_def:
  (return : 'a -> 'a M) x =
    Return x
End

Definition massert_def:
  massert b = if b then Return () else Fail Failure
End

Overload monad_bind = ``bind``
Overload monad_unitbind = ``\x y. bind x (\z. y)``
Overload monad_ignore_bind = ``\x y. bind x (\z. y)``

Definition is_diverge_def:
  is_diverge (r: 'a result) : bool = case r of Diverge => T | _ => F
End

(* Allow the use of monadic syntax *)
val _ = monadsyntax.enable_monadsyntax ()

(*** For the globals *)
Definition get_return_value_def:
  get_return_value (Return x) = x
End

(*** Misc *)
Type char = “:char”
Type string = “:string”

Definition mem_replace_fwd_def:
  mem_replace_fwd (x : 'a) (y :'a) : 'a = x
End

Definition mem_replace_back_def:
  mem_replace_back (x : 'a) (y :'a) : 'a = y
End

(*** Scalars *)
(* Remark: most of the following code was partially generated *)

(* The bounds for the isize/usize types are opaque, because they vary with
   the architecture *)
val _ = new_constant ("isize_min", “:int”)
val _ = new_constant ("isize_max", “:int”)
val _ = new_constant ("usize_max", “:int”)

val _ = new_type ("usize", 0)
val _ = new_type ("u8", 0)
val _ = new_type ("u16", 0)
val _ = new_type ("u32", 0)
val _ = new_type ("u64", 0)
val _ = new_type ("u128", 0)
val _ = new_type ("isize", 0)
val _ = new_type ("i8", 0)
val _ = new_type ("i16", 0)
val _ = new_type ("i32", 0)
val _ = new_type ("i64", 0)
val _ = new_type ("i128", 0)

val _ = new_constant ("isize_to_int", “:isize -> int”)
val _ = new_constant ("i8_to_int",    “:i8 -> int”)
val _ = new_constant ("i16_to_int",   “:i16 -> int”)
val _ = new_constant ("i32_to_int",   “:i32 -> int”)
val _ = new_constant ("i64_to_int",   “:i64 -> int”)
val _ = new_constant ("i128_to_int",  “:i128 -> int”)
val _ = new_constant ("usize_to_int", “:usize -> int”)
val _ = new_constant ("u8_to_int",    “:u8 -> int”)
val _ = new_constant ("u16_to_int",   “:u16 -> int”)
val _ = new_constant ("u32_to_int",   “:u32 -> int”)
val _ = new_constant ("u64_to_int",   “:u64 -> int”)
val _ = new_constant ("u128_to_int",  “:u128 -> int”)

(* The functions which convert integers to machine scalars don't fail.

   If we were to write a model of those functions, we would return an arbitrary
   element (or saturate) if the input integer is not in bounds.

   This design choice makes it a lot easier to manipulate those functions.
   Moreover, it allows to define and prove rewriting theorems and custom
   unfolding theorems which are necessary to evaluate terms (when doing
   unit tests). For instance, we can prove: “int_to_isize (isize_to_int i) = i”.
 *)
val _ = new_constant ("int_to_isize", “:int -> isize”)
val _ = new_constant ("int_to_i8", “:int -> i8”)
val _ = new_constant ("int_to_i16", “:int -> i16”)
val _ = new_constant ("int_to_i32", “:int -> i32”)
val _ = new_constant ("int_to_i64", “:int -> i64”)
val _ = new_constant ("int_to_i128", “:int -> i128”)
val _ = new_constant ("int_to_usize", “:int -> usize”)
val _ = new_constant ("int_to_u8", “:int -> u8”)
val _ = new_constant ("int_to_u16", “:int -> u16”)
val _ = new_constant ("int_to_u32", “:int -> u32”)
val _ = new_constant ("int_to_u64", “:int -> u64”)
val _ = new_constant ("int_to_u128", “:int -> u128”)

(* The bounds *)
val i8_min_def   = Define ‘i8_min   = (-128:int)
val i8_max_def   = Define ‘i8_max   = (127:int)
val i16_min_def  = Define ‘i16_min  = (-32768:int)
val i16_max_def  = Define ‘i16_max  = (32767:int)
val i32_min_def  = Define ‘i32_min  = (-2147483648:int)
val i32_max_def  = Define ‘i32_max  = (2147483647:int)
val i64_min_def  = Define ‘i64_min  = (-9223372036854775808:int)
val i64_max_def  = Define ‘i64_max  = (9223372036854775807:int)
val i128_min_def = Define ‘i128_min = (-170141183460469231731687303715884105728:int)
val i128_max_def = Define ‘i128_max = (170141183460469231731687303715884105727:int)
val u8_max_def   = Define ‘u8_max   = (255:int)
val u16_max_def  = Define ‘u16_max  = (65535:int)
val u32_max_def  = Define ‘u32_max  = (4294967295:int)
val u64_max_def  = Define ‘u64_max  = (18446744073709551615:int)
val u128_max_def = Define ‘u128_max = (340282366920938463463374607431768211455:int)

val all_bound_defs = [
  i8_min_def,   i8_max_def,
  i16_min_def,  i16_max_def,
  i32_min_def,  i32_max_def,
  i64_min_def,  i64_max_def,
  i128_min_def, i128_max_def,
  u8_max_def,
  u16_max_def,
  u32_max_def,
  u64_max_def,
  u128_max_def
]

(* The following bounds are valid for all architectures *)
val isize_bounds = new_axiom ("isize_bounds", “isize_min <= i16_min /\ i16_max <= isize_max”)
val usize_bounds = new_axiom ("usize_bounds", “u16_max <= usize_max”)

(* Conversion bounds *)
val isize_to_int_bounds = new_axiom ("isize_to_int_bounds",
  “!n. isize_min <= isize_to_int n /\ isize_to_int n <= isize_max”)

val i8_to_int_bounds = new_axiom ("i8_to_int_bounds",
  “!n. i8_min <= i8_to_int n /\ i8_to_int n <= i8_max”)

val i16_to_int_bounds = new_axiom ("i16_to_int_bounds",
  “!n. i16_min <= i16_to_int n /\ i16_to_int n <= i16_max”)

val i32_to_int_bounds = new_axiom ("i32_to_int_bounds",
  “!n. i32_min <= i32_to_int n /\ i32_to_int n <= i32_max”)

val i64_to_int_bounds = new_axiom ("i64_to_int_bounds",
  “!n. i64_min <= i64_to_int n /\ i64_to_int n <= i64_max”)

val i128_to_int_bounds = new_axiom ("i128_to_int_bounds",
  “!n. i128_min <= i128_to_int n /\ i128_to_int n <= i128_max”)

val usize_to_int_bounds = new_axiom ("usize_to_int_bounds",
  “!n. 0 <= usize_to_int n /\ usize_to_int n <= usize_max”)

val u8_to_int_bounds = new_axiom ("u8_to_int_bounds",
  “!n. 0 <= u8_to_int n /\ u8_to_int n <= u8_max”)

val u16_to_int_bounds = new_axiom ("u16_to_int_bounds",
  “!n. 0 <= u16_to_int n /\ u16_to_int n <= u16_max”)

val u32_to_int_bounds = new_axiom ("u32_to_int_bounds",
  “!n. 0 <= u32_to_int n /\ u32_to_int n <= u32_max”)

val u64_to_int_bounds = new_axiom ("u64_to_int_bounds",
  “!n. 0 <= u64_to_int n /\ u64_to_int n <= u64_max”)

val u128_to_int_bounds = new_axiom ("u128_to_int_bounds",
  “!n. 0 <= u128_to_int n /\ u128_to_int n <= u128_max”)

val all_to_int_bounds_lemmas = [
  isize_to_int_bounds,
  i8_to_int_bounds,
  i16_to_int_bounds,
  i32_to_int_bounds,
  i64_to_int_bounds,
  i128_to_int_bounds,
  usize_to_int_bounds,
  u8_to_int_bounds,
  u16_to_int_bounds,
  u32_to_int_bounds,
  u64_to_int_bounds,
  u128_to_int_bounds
]

(* Conversion to and from int.

   Note that for isize and usize, we write the lemmas in such a way that the
   proofs are easily automatable for constants.
 *)
val isize_to_int_int_to_isize =
  new_axiom ("isize_to_int_int_to_isize",
    “!n. isize_min <= n /\ n <= isize_max ==> isize_to_int (int_to_isize n) = n”)

val i8_to_int_int_to_i8 =
  new_axiom ("i8_to_int_int_to_i8",
    “!n. i8_min <= n /\ n <= i8_max ==> i8_to_int (int_to_i8 n) = n”)

val i16_to_int_int_to_i16 =
  new_axiom ("i16_to_int_int_to_i16",
    “!n. i16_min <= n /\ n <= i16_max ==> i16_to_int (int_to_i16 n) = n”)

val i32_to_int_int_to_i32 =
  new_axiom ("i32_to_int_int_to_i32",
    “!n. i32_min <= n /\ n <= i32_max ==> i32_to_int (int_to_i32 n) = n”)

val i64_to_int_int_to_i64 =
  new_axiom ("i64_to_int_int_to_i64",
    “!n. i64_min <= n /\ n <= i64_max ==> i64_to_int (int_to_i64 n) = n”)

val i128_to_int_int_to_i128 =
  new_axiom ("i128_to_int_int_to_i128",
    “!n. i128_min <= n /\ n <= i128_max ==> i128_to_int (int_to_i128 n) = n”)

val usize_to_int_int_to_usize =
  new_axiom ("usize_to_int_int_to_usize",
    “!n. 0 <= n /\ n <= usize_max ==> usize_to_int (int_to_usize n) = n”)

val u8_to_int_int_to_u8 =
  new_axiom ("u8_to_int_int_to_u8",
    “!n. 0 <= n /\ n <= u8_max ==> u8_to_int (int_to_u8 n) = n”)

val u16_to_int_int_to_u16 =
  new_axiom ("u16_to_int_int_to_u16",
    “!n. 0 <= n /\ n <= u16_max ==> u16_to_int (int_to_u16 n) = n”)

val u32_to_int_int_to_u32 =
  new_axiom ("u32_to_int_int_to_u32",
    “!n. 0 <= n /\ n <= u32_max ==> u32_to_int (int_to_u32 n) = n”)

val u64_to_int_int_to_u64 =
  new_axiom ("u64_to_int_int_to_u64",
    “!n. 0 <= n /\ n <= u64_max ==> u64_to_int (int_to_u64 n) = n”)

val u128_to_int_int_to_u128 =
  new_axiom ("u128_to_int_int_to_u128",
    “!n. 0 <= n /\ n <= u128_max ==> u128_to_int (int_to_u128 n) = n”)

val all_int_to_scalar_to_int_lemmas = [
  isize_to_int_int_to_isize,
  i8_to_int_int_to_i8,
  i16_to_int_int_to_i16,
  i32_to_int_int_to_i32,
  i64_to_int_int_to_i64,
  i128_to_int_int_to_i128,
  usize_to_int_int_to_usize,
  u8_to_int_int_to_u8,
  u16_to_int_int_to_u16,
  u32_to_int_int_to_u32,
  u64_to_int_int_to_u64,
  u128_to_int_int_to_u128
]

(* Additional conversion lemmas  for isize/usize *)
Theorem isize_to_int_int_to_isize_i16_bounds:
  !n. i16_min <= n /\ n <= i16_max ==> isize_to_int (int_to_isize n) = n
Proof
  rw [] >> irule isize_to_int_int_to_isize >>
  assume_tac isize_bounds >>
  int_tac
QED

Theorem usize_to_int_int_to_usize_u16_bounds:
  !n. 0 <= n /\ n <= u16_max ==> usize_to_int (int_to_usize n) = n
Proof
  rw [] >> irule usize_to_int_int_to_usize >>
  assume_tac usize_bounds >>
  int_tac
QED

val prove_int_to_scalar_to_int_unfold_tac =
  assume_tac isize_bounds >> (* Only useful for isize of course *)
  assume_tac usize_bounds >> (* Only useful for usize of course *)
  rw [] >> MAP_FIRST irule all_int_to_scalar_to_int_lemmas >> int_tac

(* Custom unfolding lemmas for the purpose of evaluation *)

(* For isize, we don't use isize_{min,max} (which are opaque) but i16_{min,max} *)
Theorem isize_to_int_int_to_isize_unfold:
  ∀n. isize_to_int (int_to_isize n) = if i16_min <= n /\ n <= i16_max then n else isize_to_int (int_to_isize n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem i8_to_int_int_to_i8_unfold:
  ∀n. i8_to_int (int_to_i8 n) = if i8_min <= n /\ n <= i8_max then n else i8_to_int (int_to_i8 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem i16_to_int_int_to_i16_unfold:
  ∀n. i16_to_int (int_to_i16 n) = if i16_min <= n /\ n <= i16_max then n else i16_to_int (int_to_i16 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem i32_to_int_int_to_i32_unfold:
  ∀n. i32_to_int (int_to_i32 n) = if i32_min <= n /\ n <= i32_max then n else i32_to_int (int_to_i32 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem i64_to_int_int_to_i64_unfold:
  ∀n. i64_to_int (int_to_i64 n) = if i64_min <= n /\ n <= i64_max then n else i64_to_int (int_to_i64 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem i128_to_int_int_to_i128_unfold:
  ∀n. i128_to_int (int_to_i128 n) = if i128_min <= n /\ n <= i128_max then n else i128_to_int (int_to_i128 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

(* For usize, we don't use isize_{min,max} (which are opaque) but u16_{min,max} *)
Theorem usize_to_int_int_to_usize_unfold:
  ∀n. usize_to_int (int_to_usize n) = if 0  n  n <= u16_max then n else usize_to_int (int_to_usize n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem u8_to_int_int_to_u8_unfold:
  ∀n. u8_to_int (int_to_u8 n) = if 0 <= n /\ n <= u8_max then n else u8_to_int (int_to_u8 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem u16_to_int_int_to_u16_unfold:
  ∀n. u16_to_int (int_to_u16 n) = if 0 <= n /\ n <= u16_max then n else u16_to_int (int_to_u16 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem u32_to_int_int_to_u32_unfold:
  ∀n. u32_to_int (int_to_u32 n) = if 0 <= n /\ n <= u32_max then n else u32_to_int (int_to_u32 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem u64_to_int_int_to_u64_unfold:
  ∀n. u64_to_int (int_to_u64 n) = if 0 <= n /\ n <= u64_max then n else u64_to_int (int_to_u64 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

Theorem u128_to_int_int_to_u128_unfold:
  ∀n. u128_to_int (int_to_u128 n) = if 0 <= n /\ n <= u128_max then n else u128_to_int (int_to_u128 n)
Proof
  prove_int_to_scalar_to_int_unfold_tac
QED

val all_int_to_scalar_to_int_unfold_lemmas = [
  isize_to_int_int_to_isize_unfold,
  i8_to_int_int_to_i8_unfold,
  i16_to_int_int_to_i16_unfold,
  i32_to_int_int_to_i32_unfold,
  i64_to_int_int_to_i64_unfold,
  i128_to_int_int_to_i128_unfold,
  usize_to_int_int_to_usize_unfold,
  u8_to_int_int_to_u8_unfold,
  u16_to_int_int_to_u16_unfold,
  u32_to_int_int_to_u32_unfold,
  u64_to_int_int_to_u64_unfold,
  u128_to_int_int_to_u128_unfold
]

val _ = evalLib.add_unfold_thms [
  "isize_to_int_int_to_isize_unfold",
  "i8_to_int_int_to_i8_unfold",
  "i16_to_int_int_to_i16_unfold",
  "i32_to_int_int_to_i32_unfold",
  "i64_to_int_int_to_i64_unfold",
  "i128_to_int_int_to_i128_unfold",
  "usize_to_int_int_to_usize_unfold",
  "u8_to_int_int_to_u8_unfold",
  "u16_to_int_int_to_u16_unfold",
  "u32_to_int_int_to_u32_unfold",
  "u64_to_int_int_to_u64_unfold",
  "u128_to_int_int_to_u128_unfold"
]

val int_to_i8_i8_to_int       = new_axiom ("int_to_i8_i8_to_int",       “∀i. int_to_i8 (i8_to_int i) = i”)
val int_to_i16_i16_to_int     = new_axiom ("int_to_i16_i16_to_int",     “∀i. int_to_i16 (i16_to_int i) = i”)
val int_to_i32_i32_to_int     = new_axiom ("int_to_i32_i32_to_int",     “∀i. int_to_i32 (i32_to_int i) = i”)
val int_to_i64_i64_to_int     = new_axiom ("int_to_i64_i64_to_int",     “∀i. int_to_i64 (i64_to_int i) = i”)
val int_to_i128_i128_to_int   = new_axiom ("int_to_i128_i128_to_int",   “∀i. int_to_i128 (i128_to_int i) = i”)
val int_to_isize_isize_to_int = new_axiom ("int_to_isize_isize_to_int", “∀i. int_to_isize (isize_to_int i) = i”)

val int_to_u8_u8_to_int       = new_axiom ("int_to_u8_u8_to_int",       “∀i. int_to_u8 (u8_to_int i) = i”)
val int_to_u16_u16_to_int     = new_axiom ("int_to_u16_u16_to_int",     “∀i. int_to_u16 (u16_to_int i) = i”)
val int_to_u32_u32_to_int     = new_axiom ("int_to_u32_u32_to_int",     “∀i. int_to_u32 (u32_to_int i) = i”)
val int_to_u64_u64_to_int     = new_axiom ("int_to_u64_u64_to_int",     “∀i. int_to_u64 (u64_to_int i) = i”)
val int_to_u128_u128_to_int   = new_axiom ("int_to_u128_u128_to_int",   “∀i. int_to_u128 (u128_to_int i) = i”)
val int_to_usize_usize_to_int = new_axiom ("int_to_usize_usize_to_int", “∀i. int_to_usize (usize_to_int i) = i”)

val all_scalar_to_int_to_scalar_lemmas = [
  int_to_i8_i8_to_int,
  int_to_i16_i16_to_int,
  int_to_i32_i32_to_int,
  int_to_i64_i64_to_int,
  int_to_i128_i128_to_int,
  int_to_isize_isize_to_int,
  int_to_u8_u8_to_int,
  int_to_u16_u16_to_int,
  int_to_u32_u32_to_int,
  int_to_u64_u64_to_int,
  int_to_u128_u128_to_int,
  int_to_usize_usize_to_int
]

val _ = BasicProvers.export_rewrites [
  "int_to_i8_i8_to_int",
  "int_to_i16_i16_to_int",
  "int_to_i32_i32_to_int",
  "int_to_i64_i64_to_int",
  "int_to_i128_i128_to_int",
  "int_to_isize_isize_to_int",
  "int_to_u8_u8_to_int",
  "int_to_u16_u16_to_int",
  "int_to_u32_u32_to_int",
  "int_to_u64_u64_to_int",
  "int_to_u128_u128_to_int",
  "int_to_usize_usize_to_int"
]

(** Utilities to define the arithmetic operations *)
val mk_isize_def = Define
  ‘mk_isize n =
    if isize_min <= n /\ n <= isize_max then Return (int_to_isize n)
    else Fail Failure

val mk_i8_def = Define
  ‘mk_i8 n =
    if i8_min <= n /\ n <= i8_max then Return (int_to_i8 n)
    else Fail Failure

val mk_i16_def = Define
  ‘mk_i16 n =
    if i16_min <= n /\ n <= i16_max then Return (int_to_i16 n)
    else Fail Failure

val mk_i32_def = Define
  ‘mk_i32 n =
    if i32_min <= n /\ n <= i32_max then Return (int_to_i32 n)
    else Fail Failure

val mk_i64_def = Define
  ‘mk_i64 n =
    if i64_min <= n /\ n <= i64_max then Return (int_to_i64 n)
    else Fail Failure

val mk_i128_def = Define
  ‘mk_i128 n =
    if i128_min <= n /\ n <= i128_max then Return (int_to_i128 n)
    else Fail Failure

val mk_usize_def = Define
  ‘mk_usize n =
    if 0 <= n /\ n <= usize_max then Return (int_to_usize n)
    else Fail Failure

val mk_u8_def = Define
  ‘mk_u8 n =
    if 0 <= n /\ n <= u8_max then Return (int_to_u8 n)
    else Fail Failure

val mk_u16_def = Define
  ‘mk_u16 n =
    if 0 <= n /\ n <= u16_max then Return (int_to_u16 n)
    else Fail Failure

val mk_u32_def = Define
  ‘mk_u32 n =
    if 0 <= n /\ n <= u32_max then Return (int_to_u32 n)
    else Fail Failure

val mk_u64_def = Define
  ‘mk_u64 n =
    if 0 <= n /\ n <= u64_max then Return (int_to_u64 n)
    else Fail Failure

val mk_u128_def = Define
  ‘mk_u128 n =
    if 0 <= n /\ n <= u128_max then Return (int_to_u128 n)
    else Fail Failure

val all_mk_int_defs = [
  mk_isize_def,
  mk_i8_def,
  mk_i16_def,
  mk_i32_def,
  mk_i64_def,
  mk_i128_def,
  mk_usize_def,
  mk_u8_def,
  mk_u16_def,
  mk_u32_def,
  mk_u64_def,
  mk_u128_def
]

(* Unfolding theorems for “mk_usize” and “mk_isize”: we need specific unfolding
   theorems because the isize/usize bounds are opaque, and may make the evaluation
   get stuck in the unit tests *)
Theorem mk_usize_unfold:
   n. mk_usize n =
    if 0  n  (n  u16_max  n  usize_max) then Return (int_to_usize n)
    else Fail Failure
Proof
  rw [mk_usize_def] >> fs [] >>
  assume_tac usize_bounds >>
  int_tac
QED
val _ = evalLib.add_unfold_thm "mk_usize_unfold"

Theorem mk_isize_unfold:
   n. mk_isize n =
    if (i16_min  n  isize_min  n) 
       (n  i16_max  n  isize_max)
    then Return (int_to_isize n)
    else Fail Failure
Proof
  rw [mk_isize_def] >> fs [] >>
  assume_tac isize_bounds >>
  int_tac
QED
val _ = evalLib.add_unfold_thm "mk_isize_unfold"

val isize_neg_def = Define ‘isize_neg x = mk_isize (- (isize_to_int x))
val i8_neg_def    = Define ‘i8_neg x    = mk_i8 (- (i8_to_int x))
val i16_neg_def   = Define ‘i16_neg x   = mk_i16 (- (i16_to_int x))
val i32_neg_def   = Define ‘i32_neg x   = mk_i32 (- (i32_to_int x))
val i64_neg_def   = Define ‘i64_neg x   = mk_i64 (- (i64_to_int x))
val i128_neg_def  = Define ‘i128_neg x  = mk_i128 (- (i128_to_int x))

val all_neg_defs = [
  isize_neg_def,
  i8_neg_def,
  i16_neg_def,
  i32_neg_def,
  i64_neg_def,
  i128_neg_def
]

val isize_add_def = Define ‘isize_add x y = mk_isize ((isize_to_int x) + (isize_to_int y))
val i8_add_def    = Define ‘i8_add x y    = mk_i8 ((i8_to_int x) + (i8_to_int y))
val i16_add_def   = Define ‘i16_add x y   = mk_i16 ((i16_to_int x) + (i16_to_int y))
val i32_add_def   = Define ‘i32_add x y   = mk_i32 ((i32_to_int x) + (i32_to_int y))
val i64_add_def   = Define ‘i64_add x y   = mk_i64 ((i64_to_int x) + (i64_to_int y))
val i128_add_def  = Define ‘i128_add x y  = mk_i128 ((i128_to_int x) + (i128_to_int y))
val usize_add_def = Define ‘usize_add x y = mk_usize ((usize_to_int x) + (usize_to_int y))
val u8_add_def    = Define ‘u8_add x y    = mk_u8 ((u8_to_int x) + (u8_to_int y))
val u16_add_def   = Define ‘u16_add x y   = mk_u16 ((u16_to_int x) + (u16_to_int y))
val u32_add_def   = Define ‘u32_add x y   = mk_u32 ((u32_to_int x) + (u32_to_int y))
val u64_add_def   = Define ‘u64_add x y   = mk_u64 ((u64_to_int x) + (u64_to_int y))
val u128_add_def  = Define ‘u128_add x y  = mk_u128 ((u128_to_int x) + (u128_to_int y))

val all_add_defs = [
  isize_add_def,
  i8_add_def,
  i16_add_def,
  i32_add_def,
  i64_add_def,
  i128_add_def,
  usize_add_def,
  u8_add_def,
  u16_add_def,
  u32_add_def,
  u64_add_def,
  u128_add_def
]

val isize_sub_def = Define ‘isize_sub x y = mk_isize ((isize_to_int x) - (isize_to_int y))
val i8_sub_def    = Define ‘i8_sub x y    = mk_i8 ((i8_to_int x) - (i8_to_int y))
val i16_sub_def   = Define ‘i16_sub x y   = mk_i16 ((i16_to_int x) - (i16_to_int y))
val i32_sub_def   = Define ‘i32_sub x y   = mk_i32 ((i32_to_int x) - (i32_to_int y))
val i64_sub_def   = Define ‘i64_sub x y   = mk_i64 ((i64_to_int x) - (i64_to_int y))
val i128_sub_def  = Define ‘i128_sub x y  = mk_i128 ((i128_to_int x) - (i128_to_int y))
val usize_sub_def = Define ‘usize_sub x y = mk_usize ((usize_to_int x) - (usize_to_int y))
val u8_sub_def    = Define ‘u8_sub x y    = mk_u8 ((u8_to_int x) - (u8_to_int y))
val u16_sub_def   = Define ‘u16_sub x y   = mk_u16 ((u16_to_int x) - (u16_to_int y))
val u32_sub_def   = Define ‘u32_sub x y   = mk_u32 ((u32_to_int x) - (u32_to_int y))
val u64_sub_def   = Define ‘u64_sub x y   = mk_u64 ((u64_to_int x) - (u64_to_int y))
val u128_sub_def  = Define ‘u128_sub x y  = mk_u128 ((u128_to_int x) - (u128_to_int y))

val all_sub_defs = [
  isize_sub_def,
  i8_sub_def,
  i16_sub_def,
  i32_sub_def,
  i64_sub_def,
  i128_sub_def,
  usize_sub_def,
  u8_sub_def,
  u16_sub_def,
  u32_sub_def,
  u64_sub_def,
  u128_sub_def
]

val isize_mul_def = Define ‘isize_mul x y = mk_isize ((isize_to_int x) * (isize_to_int y))
val i8_mul_def    = Define ‘i8_mul x y    = mk_i8 ((i8_to_int x) * (i8_to_int y))
val i16_mul_def   = Define ‘i16_mul x y   = mk_i16 ((i16_to_int x) * (i16_to_int y))
val i32_mul_def   = Define ‘i32_mul x y   = mk_i32 ((i32_to_int x) * (i32_to_int y))
val i64_mul_def   = Define ‘i64_mul x y   = mk_i64 ((i64_to_int x) * (i64_to_int y))
val i128_mul_def  = Define ‘i128_mul x y  = mk_i128 ((i128_to_int x) * (i128_to_int y))
val usize_mul_def = Define ‘usize_mul x y = mk_usize ((usize_to_int x) * (usize_to_int y))
val u8_mul_def    = Define ‘u8_mul x y    = mk_u8 ((u8_to_int x) * (u8_to_int y))
val u16_mul_def   = Define ‘u16_mul x y   = mk_u16 ((u16_to_int x) * (u16_to_int y))
val u32_mul_def   = Define ‘u32_mul x y   = mk_u32 ((u32_to_int x) * (u32_to_int y))
val u64_mul_def   = Define ‘u64_mul x y   = mk_u64 ((u64_to_int x) * (u64_to_int y))
val u128_mul_def  = Define ‘u128_mul x y  = mk_u128 ((u128_to_int x) * (u128_to_int y))

val all_mul_defs = [
  isize_mul_def,
  i8_mul_def,
  i16_mul_def,
  i32_mul_def,
  i64_mul_def,
  i128_mul_def,
  usize_mul_def,
  u8_mul_def,
  u16_mul_def,
  u32_mul_def,
  u64_mul_def,
  u128_mul_def
]

val isize_div_def = Define ‘isize_div x y =
  if isize_to_int y = 0 then Fail Failure else mk_isize ((isize_to_int x) / (isize_to_int y))
val i8_div_def = Define ‘i8_div x y =
  if i8_to_int y = 0 then Fail Failure else mk_i8 ((i8_to_int x) / (i8_to_int y))
val i16_div_def = Define ‘i16_div x y =
  if i16_to_int y = 0 then Fail Failure else mk_i16 ((i16_to_int x) / (i16_to_int y))
val i32_div_def = Define ‘i32_div x y =
  if i32_to_int y = 0 then Fail Failure else mk_i32 ((i32_to_int x) / (i32_to_int y))
val i64_div_def = Define ‘i64_div x y =
  if i64_to_int y = 0 then Fail Failure else mk_i64 ((i64_to_int x) / (i64_to_int y))
val i128_div_def = Define ‘i128_div x y =
  if i128_to_int y = 0 then Fail Failure else mk_i128 ((i128_to_int x) / (i128_to_int y))
val usize_div_def = Define ‘usize_div x y =
  if usize_to_int y = 0 then Fail Failure else mk_usize ((usize_to_int x) / (usize_to_int y))
val u8_div_def = Define ‘u8_div x y =
  if u8_to_int y = 0 then Fail Failure else mk_u8 ((u8_to_int x) / (u8_to_int y))
val u16_div_def = Define ‘u16_div x y =
  if u16_to_int y = 0 then Fail Failure else mk_u16 ((u16_to_int x) / (u16_to_int y))
val u32_div_def = Define ‘u32_div x y =
  if u32_to_int y = 0 then Fail Failure else mk_u32 ((u32_to_int x) / (u32_to_int y))
val u64_div_def = Define ‘u64_div x y =
  if u64_to_int y = 0 then Fail Failure else mk_u64 ((u64_to_int x) / (u64_to_int y))
val u128_div_def = Define ‘u128_div x y =
  if u128_to_int y = 0 then Fail Failure else mk_u128 ((u128_to_int x) / (u128_to_int y))

val all_div_defs = [
  isize_div_def,
  i8_div_def,
  i16_div_def,
  i32_div_def,
  i64_div_def,
  i128_div_def,
  usize_div_def,
  u8_div_def,
  u16_div_def,
  u32_div_def,
  u64_div_def,
  u128_div_def
]

(* The remainder operation is not a modulo.

   In Rust, the remainder has the sign of the dividend.
   In HOL4, it has the sign of the divisor.
 *)
val int_rem_def = Define ‘int_rem (x : int) (y : int) : int =
  if (0  x /\ 0  y) \/ (x < 0 /\ y < 0) then x % y else -(x % y)

(* Checking consistency with Rust *)
val _ = prove(“int_rem 1 2 = 1”, EVAL_TAC)
val _ = prove(“int_rem (-1) 2 = -1”, EVAL_TAC)
val _ = prove(“int_rem 1 (-2) = 1”, EVAL_TAC)
val _ = prove(“int_rem (-1) (-2) = -1”, EVAL_TAC)

Theorem pos_rem_pos_ineqs:
  ∀x y. 0 ≤ x  0 < y  0  int_rem x y  int_rem x y < y
Proof
  rw [int_rem_def] >> metis_tac [pos_mod_pos_ineqs]
QED

val isize_rem_def = Define ‘isize_rem x y =
  if isize_to_int y = 0 then Fail Failure else mk_isize (int_rem (isize_to_int x) (isize_to_int y))
val i8_rem_def = Define ‘i8_rem x y =
  if i8_to_int y = 0 then Fail Failure else mk_i8 (int_rem (i8_to_int x) (i8_to_int y))
val i16_rem_def = Define ‘i16_rem x y =
  if i16_to_int y = 0 then Fail Failure else mk_i16 (int_rem (i16_to_int x) (i16_to_int y))
val i32_rem_def = Define ‘i32_rem x y =
  if i32_to_int y = 0 then Fail Failure else mk_i32 (int_rem (i32_to_int x) (i32_to_int y))
val i64_rem_def = Define ‘i64_rem x y =
  if i64_to_int y = 0 then Fail Failure else mk_i64 (int_rem (i64_to_int x) (i64_to_int y))
val i128_rem_def = Define ‘i128_rem x y =
  if i128_to_int y = 0 then Fail Failure else mk_i128 (int_rem (i128_to_int x) (i128_to_int y))
val usize_rem_def = Define ‘usize_rem x y =
  if usize_to_int y = 0 then Fail Failure else mk_usize (int_rem (usize_to_int x) (usize_to_int y))
val u8_rem_def = Define ‘u8_rem x y =
  if u8_to_int y = 0 then Fail Failure else mk_u8 (int_rem (u8_to_int x) (u8_to_int y))
val u16_rem_def = Define ‘u16_rem x y =
  if u16_to_int y = 0 then Fail Failure else mk_u16 (int_rem (u16_to_int x) (u16_to_int y))
val u32_rem_def = Define ‘u32_rem x y =
  if u32_to_int y = 0 then Fail Failure else mk_u32 (int_rem (u32_to_int x) (u32_to_int y))
val u64_rem_def = Define ‘u64_rem x y =
  if u64_to_int y = 0 then Fail Failure else mk_u64 (int_rem (u64_to_int x) (u64_to_int y))
val u128_rem_def = Define ‘u128_rem x y =
  if u128_to_int y = 0 then Fail Failure else mk_u128 (int_rem (u128_to_int x) (u128_to_int y))

val all_rem_defs = [
  isize_rem_def,
  i8_rem_def,
  i16_rem_def,
  i32_rem_def,
  i64_rem_def,
  i128_rem_def,
  usize_rem_def,
  u8_rem_def,
  u16_rem_def,
  u32_rem_def,
  u64_rem_def,
  u128_rem_def
]

(*
val (asms,g) = top_goal ()
*)

fun prove_arith_unop_eq (asms, g) =
  let
    val rw_thms = List.concat [all_neg_defs, all_mk_int_defs, all_int_to_scalar_to_int_lemmas]
  in
    (rpt gen_tac >>
     rpt disch_tac >>
     assume_tac isize_bounds >> (* Only useful for isize of course *)
     rw rw_thms) (asms, g)
  end

Theorem isize_neg_eq:
  !x y.
    isize_min  - isize_to_int x 
    - isize_to_int x  isize_max 
    ?y. isize_neg x = Return y /\ isize_to_int y = - isize_to_int x
Proof
  prove_arith_unop_eq
QED

Theorem i8_neg_eq:
  !x y.
    i8_min  - i8_to_int x 
    - i8_to_int x  i8_max 
    ?y. i8_neg x = Return y /\ i8_to_int y = - i8_to_int x
Proof
  prove_arith_unop_eq
QED

Theorem i16_neg_eq:
  !x y.
    i16_min  - i16_to_int x 
    - i16_to_int x  i16_max 
    ?y. i16_neg x = Return y /\ i16_to_int y = - i16_to_int x
Proof
  prove_arith_unop_eq
QED

Theorem i32_neg_eq:
  !x y.
    i32_min  - i32_to_int x 
    - i32_to_int x  i32_max 
    ?y. i32_neg x = Return y /\ i32_to_int y = - i32_to_int x
Proof
  prove_arith_unop_eq
QED

Theorem i64_neg_eq:
  !x y.
    i64_min  - i64_to_int x 
    - i64_to_int x  i64_max 
    ?y. i64_neg x = Return y /\ i64_to_int y = - i64_to_int x
Proof
  prove_arith_unop_eq
QED

Theorem i128_neg_eq:
  !x y.
    i128_min  - i128_to_int x 
    - i128_to_int x  i128_max 
    ?y. i128_neg x = Return y /\ i128_to_int y = - i128_to_int x
Proof
  prove_arith_unop_eq
QED


fun prove_arith_op_eq (asms, g) =
  let
    val (_, t) = (dest_exists o snd o strip_imp o snd o strip_forall) g;
    val (x_to_int, y_to_int) =
      case (snd o strip_comb o rhs o snd o dest_conj) t of
        [x, y] => (x,y)
      | _ => failwith "Unexpected"
    val x = (snd o dest_comb) x_to_int;
    val y = (snd o dest_comb) y_to_int;
    val rw_thms = int_rem_def :: List.concat [all_rem_defs, all_add_defs, all_sub_defs, all_mul_defs, all_div_defs, all_mk_int_defs, all_to_int_bounds_lemmas, all_int_to_scalar_to_int_lemmas];
    fun inst_first_lem arg lems =
      map_first_tac (fn th => (assume_tac (SPEC arg th) handle HOL_ERR _ => fail_tac "")) lems;
  in
    (rpt gen_tac >>
     rpt disch_tac >>
     assume_tac usize_bounds >> (* Only useful for usize of course *)
     assume_tac isize_bounds >> (* Only useful for isize of course *)
     rw rw_thms >>
     fs rw_thms >>
     inst_first_lem x all_to_int_bounds_lemmas >>
     inst_first_lem y all_to_int_bounds_lemmas >>
     gs [not_le_eq_gt, not_lt_eq_ge, not_ge_eq_lt, not_gt_eq_le, ge_eq_le, gt_eq_lt] >>
     try_tac cooper_tac >>
     first_tac [
       (* For division *)
       qspecl_assume [‘^x_to_int’, ‘^y_to_int’] pos_div_pos_is_pos >>
       qspecl_assume [‘^x_to_int’, ‘^y_to_int’] pos_div_pos_le_init >>
       cooper_tac,
       (* For remainder *)
       dep_rewrite.DEP_PURE_ONCE_REWRITE_TAC all_int_to_scalar_to_int_lemmas >> fs [] >>
       qspecl_assume [‘^x_to_int’, ‘^y_to_int’] pos_mod_pos_is_pos >>
       qspecl_assume [‘^x_to_int’, ‘^y_to_int’] pos_mod_pos_le_init >>
       cooper_tac,
       dep_rewrite.DEP_PURE_ONCE_REWRITE_TAC all_int_to_scalar_to_int_lemmas >> fs []
     ]) (asms, g)
  end

Theorem u8_add_eq:
  !x y.
    u8_to_int x + u8_to_int y <= u8_max 
    ?z. u8_add x y = Return z /\ u8_to_int z = u8_to_int x + u8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u16_add_eq:
  !(x y).
    u16_to_int x + u16_to_int y <= u16_max 
    ?(z). u16_add x y = Return z /\ u16_to_int z = u16_to_int x + u16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u32_add_eq:
  !x y.
    u32_to_int x + u32_to_int y <= u32_max 
    ?z. u32_add x y = Return z /\ u32_to_int z = u32_to_int x + u32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u64_add_eq:
  !x y.
    u64_to_int x + u64_to_int y <= u64_max 
    ?z. u64_add x y = Return z /\ u64_to_int z = u64_to_int x + u64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u128_add_eq:
  !x y.
    u128_to_int x + u128_to_int y <= u128_max 
    ?z. u128_add x y = Return z /\ u128_to_int z = u128_to_int x + u128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem usize_add_eq:
  !x y.
    (usize_to_int x + usize_to_int y <= u16_max) \/ (usize_to_int x + usize_to_int y <= usize_max) 
    ?z. usize_add x y = Return z /\ usize_to_int z = usize_to_int x + usize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i8_add_eq:
  !x y.
    i8_min <= i8_to_int x + i8_to_int y 
    i8_to_int x + i8_to_int y <= i8_max 
    ?z. i8_add x y = Return z /\ i8_to_int z = i8_to_int x + i8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i16_add_eq:
  !x y.
    i16_min <= i16_to_int x + i16_to_int y 
    i16_to_int x + i16_to_int y <= i16_max 
    ?z. i16_add x y = Return z /\ i16_to_int z = i16_to_int x + i16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i32_add_eq:
  !x y.
    i32_min <= i32_to_int x + i32_to_int y 
    i32_to_int x + i32_to_int y <= i32_max 
    ?z. i32_add x y = Return z /\ i32_to_int z = i32_to_int x + i32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i64_add_eq:
  !x y.
    i64_min <= i64_to_int x + i64_to_int y 
    i64_to_int x + i64_to_int y <= i64_max 
    ?z. i64_add x y = Return z /\ i64_to_int z = i64_to_int x + i64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i128_add_eq:
  !x y.
    i128_min <= i128_to_int x + i128_to_int y 
    i128_to_int x + i128_to_int y <= i128_max 
    ?z. i128_add x y = Return z /\ i128_to_int z = i128_to_int x + i128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem isize_add_eq:
  !x y.
    (i16_min <= isize_to_int x + isize_to_int y \/ isize_min <= isize_to_int x + isize_to_int y) 
    (isize_to_int x + isize_to_int y <= i16_max \/ isize_to_int x + isize_to_int y <= isize_max) 
    ?z. isize_add x y = Return z /\ isize_to_int z = isize_to_int x + isize_to_int y
Proof
  prove_arith_op_eq
QED
      
Theorem u8_sub_eq:
  !x y.
    0 <= u8_to_int x - u8_to_int y 
    ?z. u8_sub x y = Return z /\ u8_to_int z = u8_to_int x - u8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u16_sub_eq:
  !x y.
    0 <= u16_to_int x - u16_to_int y 
    ?z. u16_sub x y = Return z /\ u16_to_int z = u16_to_int x - u16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u32_sub_eq:
  !x y.
    0 <= u32_to_int x - u32_to_int y 
    ?z. u32_sub x y = Return z /\ u32_to_int z = u32_to_int x - u32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u64_sub_eq:
  !x y.
    0 <= u64_to_int x - u64_to_int y 
    ?z. u64_sub x y = Return z /\ u64_to_int z = u64_to_int x - u64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u128_sub_eq:
  !x y.
    0 <= u128_to_int x - u128_to_int y 
    ?z. u128_sub x y = Return z /\ u128_to_int z = u128_to_int x - u128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem usize_sub_eq:
  !x y.
    0 <= usize_to_int x - usize_to_int y 
    ?z. usize_sub x y = Return z /\ usize_to_int z = usize_to_int x - usize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i8_sub_eq:
  !x y.
    i8_min <= i8_to_int x - i8_to_int y 
    i8_to_int x - i8_to_int y <= i8_max 
    ?z. i8_sub x y = Return z /\ i8_to_int z = i8_to_int x - i8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i16_sub_eq:
  !x y.
    i16_min <= i16_to_int x - i16_to_int y 
    i16_to_int x - i16_to_int y <= i16_max 
    ?z. i16_sub x y = Return z /\ i16_to_int z = i16_to_int x - i16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i32_sub_eq:
  !x y.
    i32_min <= i32_to_int x - i32_to_int y 
    i32_to_int x - i32_to_int y <= i32_max 
    ?z. i32_sub x y = Return z /\ i32_to_int z = i32_to_int x - i32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i64_sub_eq:
  !x y.
    i64_min <= i64_to_int x - i64_to_int y 
    i64_to_int x - i64_to_int y <= i64_max 
    ?z. i64_sub x y = Return z /\ i64_to_int z = i64_to_int x - i64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i128_sub_eq:
  !x y.
    i128_min <= i128_to_int x - i128_to_int y 
    i128_to_int x - i128_to_int y <= i128_max 
    ?z. i128_sub x y = Return z /\ i128_to_int z = i128_to_int x - i128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem isize_sub_eq:
  !x y.
    (i16_min <= isize_to_int x - isize_to_int y \/ isize_min <= isize_to_int x - isize_to_int y) 
    (isize_to_int x - isize_to_int y <= i16_max \/ isize_to_int x - isize_to_int y <= isize_max) 
    ?z. isize_sub x y = Return z /\ isize_to_int z = isize_to_int x - isize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u8_mul_eq:
  !x y.
    u8_to_int x * u8_to_int y <= u8_max 
    ?z. u8_mul x y = Return z /\ u8_to_int z = u8_to_int x * u8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u16_mul_eq:
  !x y.
    u16_to_int x * u16_to_int y <= u16_max 
    ?z. u16_mul x y = Return z /\ u16_to_int z = u16_to_int x * u16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u32_mul_eq:
  !x y.
    u32_to_int x * u32_to_int y <= u32_max 
    ?z. u32_mul x y = Return z /\ u32_to_int z = u32_to_int x * u32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u64_mul_eq:
  !x y.
    u64_to_int x * u64_to_int y <= u64_max 
    ?z. u64_mul x y = Return z /\ u64_to_int z = u64_to_int x * u64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u128_mul_eq:
  !x y.
    u128_to_int x * u128_to_int y <= u128_max 
    ?z. u128_mul x y = Return z /\ u128_to_int z = u128_to_int x * u128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem usize_mul_eq:
  !x y.
    (usize_to_int x * usize_to_int y <= u16_max) \/ (usize_to_int x * usize_to_int y <= usize_max) 
    ?z. usize_mul x y = Return z /\ usize_to_int z = usize_to_int x * usize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i8_mul_eq:
  !x y.
    i8_min <= i8_to_int x * i8_to_int y 
    i8_to_int x * i8_to_int y <= i8_max 
    ?z. i8_mul x y = Return z /\ i8_to_int z = i8_to_int x * i8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i16_mul_eq:
  !x y.
    i16_min <= i16_to_int x * i16_to_int y 
    i16_to_int x * i16_to_int y <= i16_max 
    ?z. i16_mul x y = Return z /\ i16_to_int z = i16_to_int x * i16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i32_mul_eq:
  !x y.
    i32_min <= i32_to_int x * i32_to_int y 
    i32_to_int x * i32_to_int y <= i32_max 
    ?z. i32_mul x y = Return z /\ i32_to_int z = i32_to_int x * i32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i64_mul_eq:
  !x y.
    i64_min <= i64_to_int x * i64_to_int y 
    i64_to_int x * i64_to_int y <= i64_max 
    ?z. i64_mul x y = Return z /\ i64_to_int z = i64_to_int x * i64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i128_mul_eq:
  !x y.
    i128_min <= i128_to_int x * i128_to_int y 
    i128_to_int x * i128_to_int y <= i128_max 
    ?z. i128_mul x y = Return z /\ i128_to_int z = i128_to_int x * i128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem isize_mul_eq:
  !x y.
    (i16_min <= isize_to_int x * isize_to_int y \/ isize_min <= isize_to_int x * isize_to_int y) 
    (isize_to_int x * isize_to_int y <= i16_max \/ isize_to_int x * isize_to_int y <= isize_max) 
    ?z. isize_mul x y = Return z /\ isize_to_int z = isize_to_int x * isize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u8_div_eq:
  !x y.
    u8_to_int y <> 0 
    ?z. u8_div x y = Return z /\ u8_to_int z = u8_to_int x / u8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u16_div_eq:
  !x y.
    u16_to_int y <> 0 
    ?z. u16_div x y = Return z /\ u16_to_int z = u16_to_int x / u16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u32_div_eq:
  !x y.
    u32_to_int y <> 0 
    ?z. u32_div x y = Return z /\ u32_to_int z = u32_to_int x / u32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u64_div_eq:
  !x y.
    u64_to_int y <> 0 
    ?z. u64_div x y = Return z /\ u64_to_int z = u64_to_int x / u64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u128_div_eq:
  !x y.
    u128_to_int y <> 0 
    ?z. u128_div x y = Return z /\ u128_to_int z = u128_to_int x / u128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem usize_div_eq:
  !x y.
    usize_to_int y <> 0 
    ?z. usize_div x y = Return z /\ usize_to_int z = usize_to_int x / usize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i8_div_eq:
  !x y.
    i8_to_int y <> 0 
    i8_min <= i8_to_int x / i8_to_int y 
    i8_to_int x / i8_to_int y <= i8_max 
    ?z. i8_div x y = Return z /\ i8_to_int z = i8_to_int x / i8_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i16_div_eq:
  !x y.
    i16_to_int y <> 0 
    i16_min <= i16_to_int x / i16_to_int y 
    i16_to_int x / i16_to_int y <= i16_max 
    ?z. i16_div x y = Return z /\ i16_to_int z = i16_to_int x / i16_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i32_div_eq:
  !x y.
    i32_to_int y <> 0 
    i32_min <= i32_to_int x / i32_to_int y 
    i32_to_int x / i32_to_int y <= i32_max 
    ?z. i32_div x y = Return z /\ i32_to_int z = i32_to_int x / i32_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i64_div_eq:
  !x y.
    i64_to_int y <> 0 
    i64_min <= i64_to_int x / i64_to_int y 
    i64_to_int x / i64_to_int y <= i64_max 
    ?z. i64_div x y = Return z /\ i64_to_int z = i64_to_int x / i64_to_int y
Proof
  prove_arith_op_eq
QED

Theorem i128_div_eq:
  !x y.
    i128_to_int y <> 0 
    i128_min <= i128_to_int x / i128_to_int y 
    i128_to_int x / i128_to_int y <= i128_max 
    ?z. i128_div x y = Return z /\ i128_to_int z = i128_to_int x / i128_to_int y
Proof
  prove_arith_op_eq
QED

Theorem isize_div_eq:
  !x y.
    isize_to_int y <> 0 
    (i16_min <= isize_to_int x / isize_to_int y \/ isize_min <= isize_to_int x / isize_to_int y) 
    (isize_to_int x / isize_to_int y <= i16_max \/ isize_to_int x / isize_to_int y <= isize_max) 
    ?z. isize_div x y = Return z /\ isize_to_int z = isize_to_int x / isize_to_int y
Proof
  prove_arith_op_eq
QED

Theorem u8_rem_eq:
  !x y.
    u8_to_int y <> 0 
    ?z. u8_rem x y = Return z /\ u8_to_int z = int_rem (u8_to_int x) (u8_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem u16_rem_eq:
  !x y.
    u16_to_int y <> 0 
    ?z. u16_rem x y = Return z /\ u16_to_int z = int_rem (u16_to_int x) (u16_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem u32_rem_eq:
  !x y.
    u32_to_int y <> 0 
    ?z. u32_rem x y = Return z /\ u32_to_int z = int_rem (u32_to_int x) (u32_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem u64_rem_eq:
  !x y.
    u64_to_int y <> 0 
    ?z. u64_rem x y = Return z /\ u64_to_int z = int_rem (u64_to_int x) (u64_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem u128_rem_eq:
  !x y.
    u128_to_int y <> 0 
    ?z. u128_rem x y = Return z /\ u128_to_int z = int_rem (u128_to_int x) (u128_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem usize_rem_eq:
  !x y.
    usize_to_int y <> 0 
    ?z. usize_rem x y = Return z /\ usize_to_int z = int_rem (usize_to_int x) (usize_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem i8_rem_eq:
  !x y.
    i8_to_int y <> 0 
    i8_min <= int_rem (i8_to_int x) (i8_to_int y) 
    int_rem (i8_to_int x) (i8_to_int y) <= i8_max 
    ?z. i8_rem x y = Return z /\ i8_to_int z = int_rem (i8_to_int x) (i8_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem i16_rem_eq:
  !x y.
    i16_to_int y <> 0 
    i16_min <= int_rem (i16_to_int x) (i16_to_int y) 
    int_rem (i16_to_int x) (i16_to_int y) <= i16_max 
    ?z. i16_rem x y = Return z /\ i16_to_int z = int_rem (i16_to_int x) (i16_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem i32_rem_eq:
  !x y.
    i32_to_int y <> 0 
    i32_min <= int_rem (i32_to_int x) (i32_to_int y) 
    int_rem (i32_to_int x) (i32_to_int y) <= i32_max 
    ?z. i32_rem x y = Return z /\ i32_to_int z = int_rem (i32_to_int x) (i32_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem i64_rem_eq:
  !x y.
    i64_to_int y <> 0 
    i64_min <= int_rem (i64_to_int x) (i64_to_int y) 
    int_rem (i64_to_int x) (i64_to_int y) <= i64_max 
    ?z. i64_rem x y = Return z /\ i64_to_int z = int_rem (i64_to_int x) (i64_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem i128_rem_eq:
  !x y.
    i128_to_int y <> 0 
    i128_min <= int_rem (i128_to_int x) (i128_to_int y) 
    int_rem (i128_to_int x) (i128_to_int y) <= i128_max 
    ?z. i128_rem x y = Return z /\ i128_to_int z = int_rem (i128_to_int x) (i128_to_int y)
Proof
  prove_arith_op_eq
QED

Theorem isize_rem_eq:
  !x y.
    isize_to_int y <> 0 
    (i16_min <= int_rem (isize_to_int x) (isize_to_int y) \/
     isize_min <= int_rem (isize_to_int x) (isize_to_int y)) 
    (int_rem (isize_to_int x) (isize_to_int y) <= i16_max \/
     int_rem (isize_to_int x) (isize_to_int y) <= isize_max) 
    ?z. isize_rem x y = Return z /\ isize_to_int z = int_rem (isize_to_int x) (isize_to_int y)
Proof
  prove_arith_op_eq
QED

Definition u8_lt_def:
  u8_lt x y = (u8_to_int x < u8_to_int y)
End

Definition u16_lt_def:
  u16_lt x y = (u16_to_int x < u16_to_int y)
End

Definition u32_lt_def:
  u32_lt x y = (u32_to_int x < u32_to_int y)
End

Definition u64_lt_def:
  u64_lt x y = (u64_to_int x < u64_to_int y)
End

Definition u128_lt_def:
  u128_lt x y = (u128_to_int x < u128_to_int y)
End

Definition usize_lt_def:
  usize_lt x y = (usize_to_int x < usize_to_int y)
End

Definition i8_lt_def:
  i8_lt x y = (i8_to_int x < i8_to_int y)
End

Definition i16_lt_def:
  i16_lt x y = (i16_to_int x < i16_to_int y)
End

Definition i32_lt_def:
  i32_lt x y = (i32_to_int x < i32_to_int y)
End

Definition i64_lt_def:
  i64_lt x y = (i64_to_int x < i64_to_int y)
End

Definition i128_lt_def:
  i128_lt x y = (i128_to_int x < i128_to_int y)
End

Definition isize_lt_def:
  isize_lt x y = (isize_to_int x < isize_to_int y)
End

Definition u8_le_def:
  u8_le x y = (u8_to_int x  u8_to_int y)
End

Definition u16_le_def:
  u16_le x y = (u16_to_int x  u16_to_int y)
End

Definition u32_le_def:
  u32_le x y = (u32_to_int x  u32_to_int y)
End

Definition u64_le_def:
  u64_le x y = (u64_to_int x  u64_to_int y)
End

Definition u128_le_def:
  u128_le x y = (u128_to_int x  u128_to_int y)
End

Definition usize_le_def:
  usize_le x y = (usize_to_int x  usize_to_int y)
End

Definition i8_le_def:
  i8_le x y = (i8_to_int x  i8_to_int y)
End

Definition i16_le_def:
  i16_le x y = (i16_to_int x  i16_to_int y)
End

Definition i32_le_def:
  i32_le x y = (i32_to_int x  i32_to_int y)
End

Definition i64_le_def:
  i64_le x y = (i64_to_int x  i64_to_int y)
End

Definition i128_le_def:
  i128_le x y = (i128_to_int x  i128_to_int y)
End

Definition isize_le_def:
  isize_le x y = (isize_to_int x  isize_to_int y)
End

Definition u8_gt_def:
  u8_gt x y = (u8_to_int x > u8_to_int y)
End

Definition u16_gt_def:
  u16_gt x y = (u16_to_int x > u16_to_int y)
End

Definition u32_gt_def:
  u32_gt x y = (u32_to_int x > u32_to_int y)
End

Definition u64_gt_def:
  u64_gt x y = (u64_to_int x > u64_to_int y)
End

Definition u128_gt_def:
  u128_gt x y = (u128_to_int x > u128_to_int y)
End

Definition usize_gt_def:
  usize_gt x y = (usize_to_int x > usize_to_int y)
End

Definition i8_gt_def:
  i8_gt x y = (i8_to_int x > i8_to_int y)
End

Definition i16_gt_def:
  i16_gt x y = (i16_to_int x > i16_to_int y)
End

Definition i32_gt_def:
  i32_gt x y = (i32_to_int x > i32_to_int y)
End

Definition i64_gt_def:
  i64_gt x y = (i64_to_int x > i64_to_int y)
End

Definition i128_gt_def:
  i128_gt x y = (i128_to_int x > i128_to_int y)
End

Definition isize_gt_def:
  isize_gt x y = (isize_to_int x > isize_to_int y)
End

Definition u8_ge_def:
  u8_ge x y = (u8_to_int x >= u8_to_int y)
End

Definition u16_ge_def:
  u16_ge x y = (u16_to_int x >= u16_to_int y)
End

Definition u32_ge_def:
  u32_ge x y = (u32_to_int x >= u32_to_int y)
End

Definition u64_ge_def:
  u64_ge x y = (u64_to_int x >= u64_to_int y)
End

Definition u128_ge_def:
  u128_ge x y = (u128_to_int x >= u128_to_int y)
End

Definition usize_ge_def:
  usize_ge x y = (usize_to_int x >= usize_to_int y)
End

Definition i8_ge_def:
  i8_ge x y = (i8_to_int x >= i8_to_int y)
End

Definition i16_ge_def:
  i16_ge x y = (i16_to_int x >= i16_to_int y)
End

Definition i32_ge_def:
  i32_ge x y = (i32_to_int x >= i32_to_int y)
End

Definition i64_ge_def:
  i64_ge x y = (i64_to_int x >= i64_to_int y)
End

Definition i128_ge_def:
  i128_ge x y = (i128_to_int x >= i128_to_int y)
End

Definition isize_ge_def:
  isize_ge x y = (isize_to_int x >= isize_to_int y)
End


(* Equality theorems for the comparisons between integers - used by evalLib *)

val prove_scalar_eq_equiv_tac = metis_tac all_scalar_to_int_to_scalar_lemmas

Theorem isize_eq_equiv:
  ∀x y. (x = y) = (isize_to_int x = isize_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem i8_eq_equiv:
  ∀x y. (x = y) = (i8_to_int x = i8_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem i16_eq_equiv:
  ∀x y. (x = y) = (i16_to_int x = i16_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem i32_eq_equiv:
  ∀x y. (x = y) = (i32_to_int x = i32_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem i64_eq_equiv:
  ∀x y. (x = y) = (i64_to_int x = i64_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem i128_eq_equiv:
  ∀x y. (x = y) = (i128_to_int x = i128_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem usize_eq_equiv:
  ∀x y. (x = y) = (usize_to_int x = usize_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem u8_eq_equiv:
  ∀x y. (x = y) = (u8_to_int x = u8_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem u16_eq_equiv:
  ∀x y. (x = y) = (u16_to_int x = u16_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem u32_eq_equiv:
  ∀x y. (x = y) = (u32_to_int x = u32_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem u64_eq_equiv:
  ∀x y. (x = y) = (u64_to_int x = u64_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED

Theorem u128_eq_equiv:
  ∀x y. (x = y) = (u128_to_int x = u128_to_int y)
Proof
  prove_scalar_eq_equiv_tac
QED


val _ = evalLib.add_rewrite_thms [
  "isize_eq_equiv",
  "i8_eq_equiv",
  "i16_eq_equiv",
  "i32_eq_equiv",
  "i64_eq_equiv",
  "i128_eq_equiv",
  "usize_eq_equiv",
  "u8_eq_equiv",
  "u16_eq_equiv",
  "u32_eq_equiv",
  "u64_eq_equiv",
  "u128_eq_equiv"
]

(***
 * Vectors
 *)

val _ = new_type ("vec", 1)
val _ = new_constant ("vec_to_list", “:'a vec -> 'a list”)

(* Similarly to the "int_to_scalar" functions, the “mk_vec” function always
   succeeds (it must however make sure vectors have a length which is at most
   usize_max). In case the input list is too long, a model could return
   an arbitrary vector (a truncated vector for instance).

   Once again, this design choice makes it a lot easier to manipulate vectors,
   and allows to define and prove simpler rewriting and unfolding theorems.
 *)
val _ = new_constant ("mk_vec", “:'a list -> 'a vec”)

val vec_to_list_num_bounds =
  new_axiom ("vec_to_list_num_bounds",
    “!v. (0:num) <= LENGTH (vec_to_list v) /\ LENGTH (vec_to_list v) <= Num usize_max”)

val mk_vec_axiom = new_axiom ("mk_vec_axiom",
  “∀l. len l  usize_max  vec_to_list (mk_vec l) = l”)

Theorem update_len:
  ∀ls i y. len (update ls i y) = len ls
Proof
  Induct_on ‘ls’ >> Cases_on ‘i’ >> rw [update_def, len_def]
QED

Theorem update_spec:
  ∀ls i y.
    len (update ls i y) = len ls 
    (0 <= i 
     i < len ls 
     index i (update ls i y) = y 
     ∀j. j < len ls  j  i  index j (update ls i y) = index j ls)
Proof
  Induct_on ‘ls’ >> Cases_on ‘i = 0 >> rw [update_def, len_def, index_def] >>
  try_tac (exfalso >> cooper_tac) >>
  try_tac (
    pop_last_assum (qspecl_assume [‘i' - 1’, ‘y’]) >> fs [] >>
    pop_assum sg_premise_tac >- cooper_tac >>
    pop_assum sg_premise_tac >- cooper_tac >>
    rw [])
  >> (
    pop_assum (qspec_assume ‘j - 1’) >>
    pop_assum sg_premise_tac >- cooper_tac >>
    pop_assum sg_premise_tac >- cooper_tac >>
    rw [])
QED

Theorem len_update:
   ls i y. len (update ls i y) = len ls
Proof
 fs [update_spec]
QED
val _ = BasicProvers.export_rewrites ["len_update"]

Theorem index_update_same:
  ∀ls i j y.
    0 <= i 
    i < len ls 
    j < len ls  j  i  index j (update ls i y) = index j ls
Proof
  rpt strip_tac >>
  qspecl_assume [‘ls’, ‘i’, ‘y’] update_spec >>
  rw []
QED

Theorem index_update_diff:
  ∀ls i j y.
    0 <= i 
    i < len ls 
    index i (update ls i y) = y
Proof
  rpt strip_tac >>
  qspecl_assume [‘ls’, ‘i’, ‘y’] update_spec >>
  rw []
QED

Theorem vec_to_list_int_bounds:
  !v. 0 <= len (vec_to_list v) /\ len (vec_to_list v) <= usize_max
Proof
  gen_tac >>
  qspec_assume ‘v’ vec_to_list_num_bounds >>
  fs [len_eq_LENGTH] >>
  assume_tac usize_bounds >> fs [u16_max_def] >>
  cooper_tac
QED

val vec_len_def = Define ‘vec_len v = int_to_usize (len (vec_to_list v))
Theorem vec_len_spec:
  ∀v.
    vec_len v = int_to_usize (len (vec_to_list v)) 
    0  len (vec_to_list v)  len (vec_to_list v)  usize_max
Proof
  gen_tac >> rw [vec_len_def] >>
  qspec_assume ‘v’ vec_to_list_int_bounds >>
  fs []
QED

Definition vec_new_def:
  vec_new = mk_vec []
End

Theorem vec_to_list_vec_new:
  vec_to_list vec_new = []
Proof
  fs [vec_new_def] >>
  sg_dep_rewrite_all_tac mk_vec_axiom >> fs [len_def] >>
  assume_tac usize_bounds >> fs [u16_max_def] >> int_tac
QED
val _ = BasicProvers.export_rewrites ["vec_to_list_vec_new"]

Theorem vec_len_vec_new:
  vec_len vec_new = int_to_usize 0
Proof
  fs [vec_len_def, vec_new_def] >>
  sg_dep_rewrite_all_tac usize_to_int_int_to_usize >> fs [len_def, u16_max_def] >>
  assume_tac usize_bounds >> fs [u16_max_def] >> int_tac
QED
val _ = BasicProvers.export_rewrites ["vec_len_vec_new"]

(* A custom unfolding theorem for evaluation - we compare to “u16_max” rather
   than “usize_max” on purpose. *)
Theorem mk_vec_unfold:
  ∀l. vec_to_list (mk_vec l) = if len l  u16_max then l else vec_to_list (mk_vec l)
Proof
  rw [] >> Cases_on ‘len l  u16_max >> fs [] >>
  assume_tac usize_bounds >>
  sg ‘len l  usize_max >- int_tac >>
  metis_tac [mk_vec_axiom]
QED
val _ = evalLib.add_unfold_thm "mk_vec_unfold"

(* Helper *)
Definition vec_index_def:
  vec_index v i = index (usize_to_int i) (vec_to_list v)
End

(* Helper *)
Definition vec_update_def:
  vec_update v i x = mk_vec (update (vec_to_list v) (usize_to_int i) x)
End

Theorem vec_update_eq:
  v i x.
   let nv = vec_update v i x in
   len (vec_to_list nv) = len (vec_to_list v) 
   len (update (vec_to_list v) (usize_to_int i) x) = len (vec_to_list v) 
   (usize_to_int i < len (vec_to_list v) 
      vec_index nv i = x 
      (∀j. usize_to_int j < usize_to_int (vec_len nv) 
            usize_to_int j  usize_to_int i 
            vec_index nv j = vec_index v j))
Proof
  rpt strip_tac >> fs [vec_update_def] >>
  qspec_assume ‘update (vec_to_list v) (usize_to_int i) x mk_vec_axiom >>
  sg_dep_rewrite_all_tac update_len >> fs [] >>
  qspec_assume ‘v’ vec_len_spec >> gvs [] >>
  fs [vec_len_def, vec_index_def] >>
  qspec_assume ‘i’ usize_to_int_bounds >>
  sg_dep_rewrite_all_tac usize_to_int_int_to_usize >- cooper_tac >> fs [] >>
  rw []
  >- (irule index_update_diff >> cooper_tac) >>
  sg_dep_rewrite_all_tac index_update_same >- cooper_tac >> fs []
QED

Theorem vec_to_list_vec_update:
   v i x. vec_to_list (vec_update v i x) = update (vec_to_list v) (usize_to_int i) x
Proof
  rw [vec_update_def] >>
  qspec_assume ‘v’ vec_len_spec >>
  qspecl_assume [‘v’, ‘i’, ‘x’] vec_update_eq >> fs [] >>
  qspecl_assume [‘vec_to_list v’, ‘usize_to_int i’, ‘x’] update_len >>
  sg_dep_rewrite_all_tac mk_vec_axiom >- fs [] >>
  fs []
QED
val _ = export_rewrites ["vec_to_list_vec_update"]

Definition vec_index_fwd_def:
  vec_index_fwd v i =
    if usize_to_int i  usize_to_int (vec_len v)
    then Return (vec_index v i)
    else Fail Failure
End

Definition vec_index_back_def:
  vec_index_back v i =
    if usize_to_int i < usize_to_int (vec_len v) then Return () else Fail Failure
End

Definition vec_index_mut_fwd_def:
  vec_index_mut_fwd v i =
    if usize_to_int i  usize_to_int (vec_len v)
    then Return (vec_index v i)
    else Fail Failure
End

Definition vec_index_mut_back_def:
  vec_index_mut_back v i x =
    if usize_to_int i  usize_to_int (vec_len v)
    then Return (vec_update v i x)
    else Fail Failure
End

Theorem vec_index_fwd_spec:
  ∀v i.
    usize_to_int i < usize_to_int (vec_len v) 
    vec_index_fwd v i = Return (vec_index v i)
Proof
  rw [vec_index_fwd_def]
QED

Theorem vec_index_back_spec:
  ∀v i.
    usize_to_int i < usize_to_int (vec_len v) 
    vec_index_back v i = Return ()
Proof
  rw [vec_index_back_def]
QED

Theorem vec_index_mut_fwd_spec:
  ∀v i.
    usize_to_int i < usize_to_int (vec_len v) 
    vec_index_mut_fwd v i = Return (vec_index v i)
Proof
  rw [vec_index_mut_fwd_def]
QED

Theorem vec_index_mut_back_spec:
  ∀v i x.
    usize_to_int i < usize_to_int (vec_len v) 
    vec_index_mut_back v i x = Return (vec_update v i x)
Proof
  rw [vec_index_mut_back_def]
QED

Definition vec_insert_back_def:
  vec_insert_back (v : 'a vec) (i : usize) (x : 'a) =
    if usize_to_int i < usize_to_int (vec_len v) then
      Return (vec_update v i x)
    else Fail Failure
End

Theorem vec_insert_back_spec:
  ∀v i x.
    usize_to_int i < usize_to_int (vec_len v) 
    vec_insert_back v i x = Return (vec_update v i x)
Proof
  rw [vec_insert_back_def]
QED

Definition vec_push_back_def:
  vec_push_back (v : 'a vec) (x : 'a) : ('a vec) result =
    if usize_to_int (vec_len v) < usize_max then
      Return (mk_vec ((vec_to_list v) ++ [x]))
    else Fail Failure
End

Theorem vec_push_back_unfold:
   v x. vec_push_back (v : 'a vec) (x : 'a) : ('a vec) result =
    if usize_to_int (vec_len v) < u16_max  usize_to_int (vec_len v) < usize_max then
      Return (mk_vec ((vec_to_list v) ++ [x]))
    else Fail Failure
Proof
  assume_tac usize_bounds >>
  rw [vec_push_back_def] >> fs [] >>
  int_tac
QED
val _ = evalLib.add_unfold_thm "vec_push_back_unfold"

Theorem vec_push_back_spec:
   v x.
    usize_to_int (vec_len v) < usize_max 
     nv. vec_push_back v x = Return nv 
    vec_to_list nv = vec_to_list v ++ [x]
Proof
  rw [vec_push_back_def, vec_len_def] >>
  qspec_assume ‘v’ vec_len_spec >>
  sg_dep_rewrite_all_tac usize_to_int_int_to_usize >- int_tac >> fs [] >>
  sg_dep_rewrite_all_tac mk_vec_axiom
  >- (fs [len_append, len_def, vec_len_def] >> int_tac) >>
  fs []
QED

val _ = export_theory ()