File: fat32.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (1463 lines) | stat: -rw-r--r-- 52,067 bytes parent folder | download | duplicates (3)
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
(in-package "ACL2")

;  fat32.lisp                                  Mihir Mehta

; Utilities for FAT32.

;; In commit e1d2e33807c4c529d2c7b1eb7d481f466e7a7d67 there's a (successful)
;; attempt to remove the non-local inclusion of this book. Ultimately, though,
;; this commit was rolled back since the redundant redefinition of logapp,
;; loghead, logtail, imod, ifloor and expt2 is deemed excessively
;; cumbersome. This is part of our theory now.
(include-book "ihs/logops-lemmas" :dir :system)

(include-book "centaur/fty/top" :dir :system)

(include-book "file-system-lemmas")
(include-book "utilities/bounded-nat-listp")

(defconst *expt-2-28* (expt 2 28))

;; from page 18 of the FAT specification
(defconst *ms-end-of-cc* (- *expt-2-28* 1))

;; from page 14 of the FAT specification
(defconst *ms-first-data-cluster* 2)

;; from page 18 of the FAT specification
(defconst *ms-bad-cluster* 268435447)

;; from page 15 of the FAT specification
(defconst *ms-min-count-of-clusters* 65525)

;; from page 9 of the FAT specification
(defconst *ms-min-bytes-per-sector* 512)

;; inferred - there can be as few as one sectors in a cluster
(defconst *ms-min-data-region-size* (* *ms-min-bytes-per-sector*
                                       1
                                       *ms-min-count-of-clusters*))

(defconst *ms-max-bytes-per-sector* 4096)

;; inferred - there can be as many as 128 sectors in a cluster
(defconst *ms-max-data-region-size* (* *ms-max-bytes-per-sector*
                                       128
                                       (- *ms-bad-cluster* 2)))

(defconst *ms-d-e-length* 32)

;; observed
(defconst *current-dir-fat32-name* ".          ")
(defconst *parent-dir-fat32-name* "..         ")

;; This has not really been observed to be any FAT32 entry's name. In fact, it
;; cannot be, since the FAT specification says that the filesystem's root
;; directory does not have "." and ".." entries, and those entries would have
;; different names even if they existed. Still, this is what our
;; name-to-fat32-name function computes when passed the empty string - and that
;; in turn happens with the empty string preceding the first slash in "/home"
;; or "/vmlinuz".
(defconst *empty-fat32-name* "           ")

;; known
(defconst *current-dir-name* ".")
(defconst *parent-dir-name* "..")

;; Page 36 of the FAT specification states that a directory shouldn't have more
;; than 65536 entries. However, *ms-max-d-e-count* below is used for the
;; definition of hifat-bounded-file-alist-p, and since that's applicable to our
;; internal representation of the filesystem, we need to leave room for two
;; entries (dot and dotdot) to be added when we store a directory in the stobj
;; representation. However, *ms-max-dir-size* is applicable to extracting
;; directory contents from the disk, and therefore it needs to be 2097152 as
;; stipulated.
(defconst *ms-max-d-e-count* (- (ash 1 16) 2))
(defconst *ms-max-dir-size* (ash 1 21))

;; from include/uapi/asm-generic/errno-base.h in the linux kernel sources
(defconst *ENOENT* 2) ;; No such file or directory
(defconst *EIO* 5) ;; I/O error
(defconst *EBADF* 9) ;; Bad file number
(defconst *EEXIST* 17) ;; File exists
(defconst *ENOTDIR* 20)	;; Not a directory
(defconst *EISDIR* 21) ;; Is a directory
(defconst *ENOSPC* 28) ;; No space left on device
(defconst *ENAMETOOLONG* 36) ;; File name too long

;; from the stat code in the coreutils sources
(defconst *S_MAGIC_FUSEBLK* #x65735546)

(encapsulate
  ()

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

  (local
   (defun induction-scheme (bits x)
     (if (zp bits)
         x
       (induction-scheme (- bits 1)
                         (logcdr x)))))

  (defthmd
    unsigned-byte-p-alt
    (implies (natp bits)
             (equal (unsigned-byte-p bits x)
                    (and (unsigned-byte-p (+ bits 1) x)
                         (zp (logand (ash 1 bits) x)))))
    :hints
    (("goal" :in-theory (e/d nil (logand ash logcar logcdr)
                             (logand* ash*))
      :induct (induction-scheme bits x)))))

(encapsulate
  ()

  (local (include-book "arithmetic-5/top" :dir :system))

  (defthm
    logand-ash-lemma-1
    (implies (and (natp c))
             (unsigned-byte-p c (logand i (- (ash 1 c) 1))))))

(defund fat32-entry-p (x)
  (declare (xargs :guard t))
  (unsigned-byte-p 32 x))

(defund fat32-masked-entry-p (x)
  (declare (xargs :guard t))
  (unsigned-byte-p 28 x))

;; 0 is chosen as the default value based on this comment from Microsoft's FAT
;; overview:
;; The only time that the high 4 bits of FAT32 FAT entries should ever be
;; changed is when the volume is formatted, at which time the whole 32-bit FAT
;; entry should be zeroed, including the high 4 bits.
(defund fat32-entry-fix (x)
  (declare (xargs :guard t))
  (if (fat32-entry-p x)
      x 0))

(defund fat32-masked-entry-fix (x)
  (declare (xargs :guard t))
  (if (fat32-masked-entry-p x)
      x 0))

(defthm natp-of-fat32-masked-entry-fix
  (natp (fat32-masked-entry-fix x))
  :hints (("goal" :do-not-induct t
           :in-theory (enable fat32-masked-entry-fix
                              fat32-masked-entry-p)))
  :rule-classes :type-prescription)

(in-theory (enable fat32-entry-p fat32-entry-fix fat32-masked-entry-p fat32-masked-entry-fix))

(defthm fat32-masked-entry-p-correctness-1
  (implies (fat32-masked-entry-p x)
           (natp x))
  :rule-classes :forward-chaining)

(defthm fat32-entry-fix-when-fat32-entry-p
  (implies (fat32-entry-p x)
           (equal (fat32-entry-fix x) x))
  :hints (("Goal" :in-theory (enable fat32-entry-fix))))

(defthm fat32-masked-entry-fix-when-fat32-masked-entry-p
  (implies (fat32-masked-entry-p x)
           (equal (fat32-masked-entry-fix x) x))
  :hints (("Goal" :in-theory (enable fat32-masked-entry-fix))))

(defthm
  fat32-entry-fix-correctness-1
  (and (<= 0 (fat32-entry-fix x))
       (< (fat32-entry-fix x) (ash 1 32))
       (integerp (fat32-entry-fix x)))
  :rule-classes
  ((:linear
    :corollary
    (and (<= 0 (fat32-entry-fix x))
         (< (fat32-entry-fix x) (ash 1 32))))
   (:type-prescription
    :corollary
    (integerp (fat32-entry-fix x))))
  :hints
  (("goal" :in-theory (enable fat32-entry-fix fat32-entry-p))))

;; Use a mask to take the low 28 bits.
(defund fat32-entry-mask (x)
  (declare (xargs :guard (fat32-entry-p x)))
  (loghead 28 x))

(defthm
  fat32-entry-mask-correctness-1
  (fat32-masked-entry-p (fat32-entry-mask x))
  :hints (("goal" :in-theory (e/d (fat32-entry-mask fat32-masked-entry-p)
                                  (unsigned-byte-p logand-ash-lemma-1))
           :use (:instance logand-ash-lemma-1 (c 28)
                           (i x)))))

(fty::deffixtype fat32-entry
                 :pred   fat32-entry-p
                 :fix    fat32-entry-fix
                 :equiv  fat32-entry-equiv
                 :define t
                 :forward t)

(fty::deffixtype fat32-masked-entry
                 :pred   fat32-masked-entry-p
                 :fix    fat32-masked-entry-fix
                 :equiv  fat32-masked-entry-equiv
                 :define t
                 :forward t)

(fty::deflist fat32-entry-list :elt-type fat32-entry-p :true-listp t)

(fty::deflist fat32-masked-entry-list :elt-type fat32-masked-entry-p :true-listp t)

(defthm nat-listp-if-fat32-masked-entry-list-p
  (implies (fat32-masked-entry-list-p x)
           (nat-listp x))
  :rule-classes (:forward-chaining :rewrite))

(in-theory (disable fat32-entry-p fat32-entry-fix fat32-masked-entry-p fat32-masked-entry-fix))

(defthm member-of-fat32-entry-list
  (implies (and (member-equal x lst)
                (fat32-entry-list-p lst))
           (fat32-entry-p x)))

(defthm
  fat32-masked-entry-list-p-of-make-list-ac
  (implies (and (fat32-masked-entry-p val)
                (fat32-masked-entry-list-p ac))
           (fat32-masked-entry-list-p (make-list-ac n val ac))))

(defthm fat32-masked-entry-list-p-of-append
  (equal (fat32-masked-entry-list-p (binary-append x y))
         (and (fat32-masked-entry-list-p (true-list-fix x))
              (fat32-masked-entry-list-p y))))

(defthm fat32-masked-entry-list-p-of-true-list-fix
  (implies
   (fat32-masked-entry-list-p x)
   (fat32-masked-entry-list-p (true-list-fix x))))

(defthm fat32-entry-list-p-of-update-nth
  (implies
   (fat32-entry-list-p l)
   (equal (fat32-entry-list-p (update-nth key val l))
          (and (<= (nfix key) (len l))
               (fat32-entry-p val)))))

(defthm fat32-entry-list-p-of-revappend
  (equal (fat32-entry-list-p (revappend x y))
         (and (fat32-entry-list-p y)
              (fat32-entry-list-p (list-fix x)))))

(defthm fat32-entry-list-of-nthcdr
  (implies (fat32-entry-list-p l)
           (fat32-entry-list-p (nthcdr n l))))

(defthm
  fat32-masked-entry-p-of-nth-when-fat32-masked-entry-list-p
  (implies (fat32-masked-entry-list-p l)
           (iff (fat32-masked-entry-p (nth n l))
                (< (nfix n) (len l))))
  :rule-classes
  ((:rewrite
    :corollary (implies (fat32-masked-entry-list-p l)
                        (equal (fat32-masked-entry-p (nth n l))
                               (< (nfix n) (len l)))))))

(encapsulate
  ()

  (local
   (defthm fat32-entry-list-p-of-first-n-ac-lemma-1
     (implies (not (fat32-entry-list-p (true-list-fix ac)))
              (not (fat32-entry-list-p (first-n-ac i l ac))))))

  (defthm
    fat32-entry-list-p-of-first-n-ac
    (implies (fat32-entry-list-p l)
             (equal (fat32-entry-list-p (first-n-ac n l ac))
                    (and (fat32-entry-list-p (true-list-fix ac))
                         (<= (nfix n) (len l)))))))

(defthm
  fat32-entry-list-p-of-take
  (implies (fat32-entry-list-p l)
           (equal (fat32-entry-list-p (take n l))
                  (<= (nfix n) (len l)))))

(defthm fat32-entry-list-p-of-repeat
  (implies (fat32-entry-p x)
           (fat32-entry-list-p (repeat n x)))
  :hints (("goal" :in-theory (enable repeat))))

(defthm
  fat32-entry-list-p-of-resize-list
  (implies
   (and (fat32-entry-list-p lst)
        (fat32-entry-p default-value))
   (fat32-entry-list-p (resize-list lst n default-value))))

(defthm natp-when-fat32-entry-p
  (implies (fat32-entry-p x) (natp x))
  :hints (("goal" :in-theory (enable fat32-entry-p)))
  :rule-classes :forward-chaining)

(encapsulate
  ()

  (local (include-book "arithmetic-5/top" :dir :system))

  (local
   (defthm fat32-entry-p-of-nth-lemma-1
     (equal (< (+ -1 n) (len (cdr l)))
            (< n (+ 1 (len (cdr l)))))))

  (defthm fat32-entry-p-of-nth
    (implies (fat32-entry-list-p l)
             (equal (fat32-entry-p (nth n l))
                    (< (nfix n) (len l))))
    :hints (("Goal" :in-theory (disable nth-when->=-n-len-l)))))

(defund
  fat32-update-lower-28
  (entry masked-entry)
  (declare
   (xargs
    :guard-hints
    (("goal"
      :in-theory (enable fat32-entry-p fat32-masked-entry-p)))
    :guard (and (fat32-entry-p entry)
                (fat32-masked-entry-p masked-entry))))
  (logapp 28 masked-entry
          (logtail 28
                   (mbe :exec entry
                        :logic (fat32-entry-fix entry)))))

(defthm
  fat32-update-lower-28-correctness-1
  (implies (fat32-masked-entry-p masked-entry)
           (fat32-entry-p (fat32-update-lower-28 entry masked-entry)))
  :hints (("goal" :in-theory (e/d (fat32-update-lower-28 fat32-entry-p)
                                  (logapp logtail))))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies (fat32-masked-entry-p masked-entry)
             (unsigned-byte-p 32
                              (fat32-update-lower-28 entry masked-entry)))
    :hints (("goal" :in-theory (enable fat32-entry-p))))))

(defthm
  fat32-entry-mask-of-fat32-update-lower-28
  (implies
   (fat32-masked-entry-p masked-entry)
   (equal (fat32-entry-mask (fat32-update-lower-28 entry masked-entry))
          masked-entry))
  :hints
  (("goal" :in-theory
    (e/d (fat32-update-lower-28 fat32-entry-mask fat32-masked-entry-p)
         (unsigned-byte-p loghead logapp)))))

(defthm
  fat32-update-lower-28-of-fat32-update-lower-28
  (implies
   (fat32-masked-entry-p masked-entry1)
   (equal (fat32-update-lower-28 (fat32-update-lower-28 entry masked-entry1)
                                 masked-entry2)
          (fat32-update-lower-28 entry masked-entry2)))
  :hints
  (("goal"
    :in-theory (e/d (fat32-update-lower-28)
                    (logapp logtail
                            (:rewrite fat32-update-lower-28-correctness-1
                                      . 1)))
    :use (:instance (:rewrite fat32-update-lower-28-correctness-1 . 1)
                    (masked-entry masked-entry1)
                    (entry entry)))))

(defthmd
  fat32-update-lower-28-of-fat32-entry-mask
  (implies (and (fat32-entry-p entry)
                (equal masked-entry (fat32-entry-mask entry)))
           (equal (fat32-update-lower-28 entry masked-entry)
                  entry))
  :hints
  (("goal"
    :in-theory (e/d (fat32-update-lower-28 fat32-entry-mask)
                    (logapp loghead logtail)))))

;; This is taken from page 18 of the fat specification.
(defund fat32-is-eof (fat-content)
  (declare (xargs :guard (fat32-masked-entry-p fat-content)
                  :guard-hints (("Goal'" :in-theory (enable fat32-masked-entry-p)))))
  (>= fat-content #xFFFFFF8))

;; The case-split in the hypothesis is because we can't make a linear rule out
;; of the contrapositive.
(defthm fat32-is-eof-correctness-1
  (implies (case-split (< fat-content *ms-bad-cluster*))
           (not (fat32-is-eof fat-content)))
  :hints (("Goal" :in-theory (enable fat32-is-eof)) ))

(defund
  fat32-build-index-list
  (fa-table masked-current-cluster
            length cluster-size)
  (declare
   (xargs
    :measure (nfix length)
    :guard (and (fat32-entry-list-p fa-table)
                (fat32-masked-entry-p masked-current-cluster)
                (natp length)
                (>= masked-current-cluster
                    *ms-first-data-cluster*)
                (< masked-current-cluster (len fa-table))
                (integerp cluster-size)
                (> cluster-size 0))))
  (b*
      (((when (or (zp length) (zp cluster-size)))
        ;; This represents a problem case because masked-current-cluster is a
        ;; valid non-free cluster, but the length is 0. This loosely
        ;; corresponds to the infinite loop protection in the function
        ;; fs/fat/cache.c:fat_get_cluster.
        (mv nil (- *eio*)))
       (masked-next-cluster
        (fat32-entry-mask (nth masked-current-cluster fa-table)))
       ((when (< masked-next-cluster
                 *ms-first-data-cluster*))
        (mv (list masked-current-cluster)
            (- *eio*)))
       ((when (or (fat32-is-eof masked-next-cluster)
                  (>= masked-next-cluster (len fa-table))))
        (mv (list masked-current-cluster) 0))
       ((mv tail-index-list tail-error)
        (fat32-build-index-list fa-table masked-next-cluster
                                (nfix (- length cluster-size))
                                cluster-size)))
    (mv (list* masked-current-cluster tail-index-list)
        tail-error)))

(defthm bounded-nat-listp-of-fat32-build-index-list
  (implies (and (equal b (len fa-table))
                (fat32-masked-entry-p masked-current-cluster)
                (< masked-current-cluster (len fa-table)))
           (b* (((mv index-list &)
                 (fat32-build-index-list fa-table masked-current-cluster
                                         length cluster-size)))
             (bounded-nat-listp index-list b)))
  :hints (("Goal" :in-theory (enable fat32-build-index-list))))

(defthm
  fat32-masked-entry-list-p-of-fat32-build-index-list
  (implies (fat32-masked-entry-p masked-current-cluster)
           (b* (((mv index-list &)
                 (fat32-build-index-list fa-table masked-current-cluster
                                         length cluster-size)))
             (fat32-masked-entry-list-p index-list)))
  :hints (("goal" :in-theory (enable fat32-build-index-list))))

(defthm
  fat32-build-index-list-of-update-nth
  (implies
   (and (<= *ms-first-data-cluster*
            masked-current-cluster)
        (fat32-masked-entry-p masked-current-cluster))
   (mv-let
     (index-list error-code)
     (fat32-build-index-list fa-table masked-current-cluster
                             length cluster-size)
     (declare (ignore error-code))
     (implies (and (not (member-equal key index-list))
                   (< (nfix key) (len fa-table)))
              (equal (fat32-build-index-list (update-nth key val fa-table)
                                             masked-current-cluster
                                             length cluster-size)
                     (fat32-build-index-list fa-table masked-current-cluster
                                             length cluster-size)))))
  :hints
  (("goal" :induct (fat32-build-index-list fa-table masked-current-cluster
                                           length cluster-size)
    :in-theory (e/d (fat32-build-index-list)
                    (nth update-nth))
    :expand (fat32-build-index-list (update-nth key val fa-table)
                                    masked-current-cluster
                                    length cluster-size))))

(defthm
  lower-bounded-integer-listp-of-fat32-build-index-list
  (implies
   (and (fat32-masked-entry-p masked-current-cluster)
        (<= *ms-first-data-cluster*
            masked-current-cluster))
   (lower-bounded-integer-listp
    (mv-nth
     0
     (fat32-build-index-list fa-table masked-current-cluster
                             length cluster-size))
    *ms-first-data-cluster*))
  :hints
  (("goal" :in-theory (enable fat32-build-index-list))))

(defthm
  true-listp-of-fat32-build-index-list
  (true-listp
    (mv-nth
     0
     (fat32-build-index-list fa-table masked-current-cluster
                             length cluster-size)))
  :hints
  (("goal" :in-theory (enable fat32-build-index-list)))
  :rule-classes (:rewrite :type-prescription))

(defthm
  integer-listp-of-fat32-build-index-list
  (implies
   (integerp masked-current-cluster)
   (integer-listp
    (mv-nth 0
            (fat32-build-index-list fa-table masked-current-cluster
                                    length cluster-size))))
  :hints (("goal" :in-theory (enable fat32-build-index-list))))

(defthm
  consp-of-fat32-build-index-list
  (equal
   (consp (mv-nth 0
                  (fat32-build-index-list fa-table masked-current-cluster
                                          length cluster-size)))
   (not (or (zp length) (zp cluster-size))))
  :hints (("goal" :in-theory (enable fat32-build-index-list))))

(encapsulate
  ()

  (local (include-book "arithmetic-3/top" :dir :system))

  (local (in-theory (enable nfix)))

  (local
   (set-default-hints
    '((nonlinearp-default-hint stable-under-simplificationp
                               hist pspv))))

  (defthm
    len-of-fat32-build-index-list-1
    (implies
     (not (zp cluster-size))
     (<= (len (mv-nth 0
                      (fat32-build-index-list fa-table masked-current-cluster
                                              length cluster-size)))
         (ceiling (nfix length) cluster-size)))
    :hints
    (("goal" :induct (fat32-build-index-list fa-table masked-current-cluster
                                             length cluster-size)
      :in-theory (enable fat32-build-index-list)))
    :rule-classes
    (:linear
     (:linear
      :corollary
      (implies
       (not (zp cluster-size))
       (<= (*
            cluster-size
            (len (mv-nth 0
                         (fat32-build-index-list fa-table masked-current-cluster
                                                 length cluster-size))))
           (* cluster-size
              (ceiling (nfix length) cluster-size)))))
     (:linear
      :corollary
      (implies
       (not (zp cluster-size))
       (< (*
           cluster-size
           (len (mv-nth 0
                        (fat32-build-index-list fa-table masked-current-cluster
                                                length cluster-size))))
          (+ (nfix length) cluster-size)))))))

(defun count-free-clusters-helper (fa-table n)
  (declare (xargs :guard (and (fat32-entry-list-p fa-table)
                              (natp n)
                              (>= (len fa-table) n))
                  :guard-hints (("Goal" :in-theory (disable nth)) )))
  (if
      (zp n)
      0
    (if
        (not (equal (fat32-entry-mask (nth (- n 1) fa-table)) 0))
        (count-free-clusters-helper fa-table (- n 1))
      (+ 1 (count-free-clusters-helper fa-table (- n 1))))))

(defthm
  count-free-clusters-helper-of-update-nth-1
  (implies (not (equal (fat32-entry-mask val) 0))
           (equal (count-free-clusters-helper (update-nth key val fa-table)
                                              n)
                  (if (and (< (nfix key) (nfix n))
                           (equal (fat32-entry-mask (nth key fa-table))
                                  0))
                      (- (count-free-clusters-helper fa-table n)
                         1)
                      (count-free-clusters-helper fa-table n))))
  :hints (("goal" :in-theory (disable update-nth))))

(defthm
  count-free-clusters-helper-of-update-nth-2
  (implies (equal (fat32-entry-mask val) 0)
           (equal (count-free-clusters-helper (update-nth key val fa-table)
                                              n)
                  (if (and (< (nfix key) (nfix n))
                           (not (equal (fat32-entry-mask (nth key fa-table))
                                       0)))
                      (+ (count-free-clusters-helper fa-table n)
                         1)
                      (count-free-clusters-helper fa-table n))))
  :hints (("goal" :in-theory (disable update-nth))))

(defthm count-free-clusters-helper-correctness-1
  (<= (count-free-clusters-helper fa-table n)
      (nfix n))
  :rule-classes :linear)

(defund
  count-free-clusters (fa-table)
  (declare
   (xargs :guard (and (fat32-entry-list-p fa-table)
                      (>= (len fa-table)
                          *ms-first-data-cluster*))
          :guard-hints (("goal" :in-theory (disable nth)))))
  (count-free-clusters-helper
   (nthcdr *ms-first-data-cluster* fa-table)
   (- (len fa-table)
      *ms-first-data-cluster*)))

(defthm count-free-clusters-of-update-nth
  (implies (and (<= *ms-first-data-cluster* (nfix key))
                (< (nfix key) (len fa-table)))
           (equal (count-free-clusters (update-nth key val fa-table))
                  (cond ((and (equal (fat32-entry-mask (nth key fa-table))
                                     0)
                              (not (equal (fat32-entry-mask val) 0)))
                         (- (count-free-clusters fa-table) 1))
                        ((and (equal (fat32-entry-mask val) 0)
                              (not (equal (fat32-entry-mask (nth key fa-table))
                                          0)))
                         (+ (count-free-clusters fa-table) 1))
                        (t (count-free-clusters fa-table)))))
  :hints (("goal" :in-theory (enable count-free-clusters))))

(defthm
  count-free-clusters-correctness-1
  (implies (>= (len fa-table)
               *ms-first-data-cluster*)
           (<= (count-free-clusters fa-table)
               (- (len fa-table)
                  *ms-first-data-cluster*)))
  :rule-classes
  ((:linear
    :trigger-terms
    ((count-free-clusters fa-table))))
  :hints (("goal" :in-theory (enable count-free-clusters))))

(defund
  find-n-free-clusters-helper
  (fa-table n start)
  (declare (xargs :guard (and (fat32-entry-list-p fa-table)
                              (natp n)
                              (natp start))))
  (if (or (atom fa-table) (zp n))
      nil
      (if (not (equal (fat32-entry-mask (car fa-table))
                      0))
          (find-n-free-clusters-helper (cdr fa-table)
                                       n (+ start 1))
          (cons start
                (find-n-free-clusters-helper (cdr fa-table)
                                             (- n 1)
                                             (+ start 1))))))

(defthmd
  find-n-free-clusters-helper-correctness-1
  (implies (and (fat32-entry-list-p fa-table)
                (natp n)
                (natp start)
                (equal b (+ start (len fa-table))))
           (bounded-nat-listp
            (find-n-free-clusters-helper fa-table n start)
            b))
  :hints
  (("goal'" :in-theory (enable find-n-free-clusters-helper))))

(defthmd
  find-n-free-clusters-helper-correctness-2
  (implies
   (natp start)
   (nat-listp (find-n-free-clusters-helper fa-table n start)))
  :hints
  (("goal" :in-theory (enable find-n-free-clusters-helper))))

(defthmd
  find-n-free-clusters-helper-correctness-3
  (implies
   (and (natp start)
        (member-equal
         x
         (find-n-free-clusters-helper fa-table n start)))
   (and (integerp x)
        (<= start x)
        (< x (+ start (len fa-table)))))
  :hints
  (("goal" :in-theory (enable find-n-free-clusters-helper))))

(defthm
  find-n-free-clusters-helper-correctness-4
  (implies
   (and
    (fat32-entry-list-p fa-table)
    (natp n)
    (natp start)
    (not (equal (fat32-entry-mask (nth (- x start) fa-table))
                0)))
   (not (member-equal
         x
         (find-n-free-clusters-helper fa-table n start))))
  :hints
  (("goal" :in-theory (enable find-n-free-clusters-helper)
    :use find-n-free-clusters-helper-correctness-3)
   ("subgoal *1/2"
    :use (:instance find-n-free-clusters-helper-correctness-3
                    (fa-table (cdr fa-table))
                    (start (+ 1 start))))))

(defthm
  find-n-free-clusters-helper-correctness-6
  (implies
   (and (fat32-entry-list-p fa-table)
        (natp start))
   (no-duplicatesp-equal (find-n-free-clusters-helper fa-table n start)))
  :hints
  (("goal" :in-theory (enable find-n-free-clusters-helper))
   ("goal'" :induct (find-n-free-clusters-helper fa-table n start))
   ("subgoal *1/9" :use (:instance find-n-free-clusters-helper-correctness-3
                                   (x start)
                                   (fa-table (cdr fa-table))
                                   (n (- n 1))
                                   (start (+ 1 start))))))

(defthm
  find-n-free-clusters-helper-correctness-7
  (implies
   (and
    (natp start)
    (< (nfix m)
       (len (find-n-free-clusters-helper fa-table n start))))
   (and
    (integerp
     (nth m
          (find-n-free-clusters-helper fa-table n start)))
    (<= start
        (nth m
             (find-n-free-clusters-helper fa-table n start)))
    (< (nth m
             (find-n-free-clusters-helper fa-table n start))
        (+ start (len fa-table)))))
  :rule-classes
  ((:linear :corollary
            (implies
             (and
              (natp start)
              (< (nfix m)
                 (len (find-n-free-clusters-helper fa-table n start))))
             (and
              (<= start
                  (nth m
                       (find-n-free-clusters-helper fa-table n start)))
              (< (nth m
                      (find-n-free-clusters-helper fa-table n start))
                 (+ start (len fa-table))))))
   (:rewrite :corollary
            (implies
             (and
              (natp start)
              (< (nfix m)
                 (len (find-n-free-clusters-helper fa-table n start))))
             (integerp
              (nth m
                   (find-n-free-clusters-helper fa-table n start))))))
  :hints
  (("goal"
    :use
    (:instance
     find-n-free-clusters-helper-correctness-3
     (x
      (nth m
           (find-n-free-clusters-helper fa-table n start)))))))


(encapsulate
  ()

  (local
   (defthm
     len-of-find-n-free-clusters-helper-lemma-1
     (implies
      (consp fa-table)
      (equal
       (len (find-n-free-clusters-helper fa-table n start))
       (if
           (and
            (<
             (len (find-n-free-clusters-helper (take (- (len fa-table) 1) fa-table)
                                               n start))
             (nfix n))
            (equal (fat32-entry-mask (nth (- (len fa-table) 1) fa-table))
                   0))
           (+ (len (find-n-free-clusters-helper (take (- (len fa-table) 1) fa-table)
                                                n start))
              1)
         (len (find-n-free-clusters-helper (take (- (len fa-table) 1) fa-table)
                                           n start)))))
     :hints (("goal" :in-theory (enable find-n-free-clusters-helper)
              :induct (find-n-free-clusters-helper fa-table n start)
              :expand (len (cdr fa-table))))))

  (local
   (defthm
     len-of-find-n-free-clusters-helper-lemma-2
     (<= (len (find-n-free-clusters-helper fa-table n start))
         (nfix n))
     :rule-classes :linear
     :hints
     (("goal" :in-theory (enable find-n-free-clusters-helper)))))

  (defthm
    len-of-find-n-free-clusters-helper
    (equal (len (find-n-free-clusters-helper (take n2 fa-table)
                                             n1 start))
           (min (count-free-clusters-helper fa-table n2)
                (nfix n1)))
    :hints
    (("goal" :in-theory (enable find-n-free-clusters-helper
                                len-of-find-n-free-clusters-helper-lemma-1)))
    :rule-classes
    (:rewrite
     (:linear
      :corollary
      (<= (len (find-n-free-clusters-helper fa-table n start))
          (nfix n))))))

(in-theory (disable (:linear len-of-find-n-free-clusters-helper)))

(defund
    find-n-free-clusters (fa-table n)
  (declare (xargs :guard (and (fat32-entry-list-p fa-table)
                              (natp n))))
  ;; the first 2 clusters are excluded
  (find-n-free-clusters-helper
   (nthcdr *ms-first-data-cluster* fa-table)
   n *ms-first-data-cluster*))

(defthm
  find-n-free-clusters-correctness-1
  (implies (and (fat32-entry-list-p fa-table)
                (natp n)
                (equal b (len fa-table))
                (>= (len fa-table)
                    *ms-first-data-cluster*))
           (bounded-nat-listp (find-n-free-clusters fa-table n)
                              b))
  :hints
  (("goal"
    :in-theory (enable find-n-free-clusters)
    :use
    ((:instance
      find-n-free-clusters-helper-correctness-1
      (start *ms-first-data-cluster*)
      (fa-table (nthcdr *ms-first-data-cluster* fa-table))
      (b (len fa-table))))))
  :rule-classes
  (:rewrite
   (:linear
    :corollary
    (implies (and (fat32-entry-list-p fa-table)
                  (natp n)
                  (equal b (len fa-table))
                  (>= (len fa-table)
                      *ms-first-data-cluster*)
                  (consp (find-n-free-clusters fa-table n)))
             (< (car (find-n-free-clusters fa-table n))
                b)))))

(defthm
  find-n-free-clusters-correctness-2
  (nat-listp (find-n-free-clusters fa-table n))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies (consp (find-n-free-clusters fa-table n))
             (and (nat-listp (cdr (find-n-free-clusters fa-table n)))
                  (integerp (car (find-n-free-clusters fa-table n))))))
   (:linear
    :corollary (implies (consp (find-n-free-clusters fa-table n))
                        (<= 0
                            (car (find-n-free-clusters fa-table n))))))
  :hints
  (("goal"
    :in-theory (enable find-n-free-clusters
                       find-n-free-clusters-helper-correctness-2))))

(defthmd
  find-n-free-clusters-correctness-3
  (implies (member-equal x (find-n-free-clusters fa-table n))
           (and (integerp x)
                (<= *ms-first-data-cluster* x)
                (< x (len fa-table))))
  :hints
  (("goal" :in-theory (enable find-n-free-clusters))
   ("goal'"
    :use
    (:instance
     find-n-free-clusters-helper-correctness-3
     (start *ms-first-data-cluster*)
     (fa-table (nthcdr *ms-first-data-cluster* fa-table))))))

(defthm
  find-n-free-clusters-correctness-4
  (implies
   (and (fat32-entry-list-p fa-table)
        (natp n)
        (not (equal (fat32-entry-mask (nth x fa-table))
                    0)))
   (not (member-equal x (find-n-free-clusters fa-table n))))
  :hints
  (("goal"
    :in-theory (e/d (find-n-free-clusters)
                    (member-of-a-nat-list))
    :use
    ((:instance
      member-of-a-nat-list
      (lst (find-n-free-clusters-helper
            (nthcdr *ms-first-data-cluster* fa-table)
            n *ms-first-data-cluster*)))
     (:instance
      find-n-free-clusters-helper-correctness-3
      (fa-table (nthcdr *ms-first-data-cluster* fa-table))
      (start *ms-first-data-cluster*))))))

;; Holding off for now on determining what
;; find-n-free-clusters-helper-correctness-5 should look like.
(defthm
  find-n-free-clusters-correctness-5
  (implies
   (and (fat32-entry-list-p fa-table)
        (natp n1)
        (< (nfix n2)
           (len (find-n-free-clusters fa-table n1))))
   (equal (fat32-entry-mask
           (nth (nth n2 (find-n-free-clusters fa-table n1))
                fa-table))
          0))
  :hints
  (("goal"
    :do-not-induct t
    :in-theory (e/d nil
                    (find-n-free-clusters-correctness-4))
    :use ((:instance
           find-n-free-clusters-correctness-4
           (n n1)
           (x (nth n2
                   (find-n-free-clusters fa-table n1))))))))

(defthm find-n-free-clusters-correctness-6
  (implies (fat32-entry-list-p fa-table)
           (no-duplicatesp-equal (find-n-free-clusters fa-table n)))
  :hints (("goal" :in-theory (enable find-n-free-clusters))))

(defthm
  find-n-free-clusters-correctness-8
  (integer-listp (find-n-free-clusters fa-table n)))

(defthm find-n-free-clusters-correctness-7
  (implies (case-split (< (nfix m)
                          (len (find-n-free-clusters fa-table n))))
           (and (<= *ms-first-data-cluster*
                    (nth m (find-n-free-clusters fa-table n)))
                (< (nth m (find-n-free-clusters fa-table n))
                   (len fa-table))))
  :rule-classes :linear
  :hints (("goal" :in-theory (e/d (find-n-free-clusters) (nth)))))

(defthm
  find-n-free-clusters-helper-of-true-list-fix
  (equal (find-n-free-clusters-helper (true-list-fix fa-table)
                                      n start)
         (find-n-free-clusters-helper fa-table n start))
  :hints
  (("goal" :in-theory (enable find-n-free-clusters-helper))))

(defthm
  len-of-find-n-free-clusters
  (equal (len (find-n-free-clusters fa-table n))
         (min (count-free-clusters fa-table)
              (nfix n)))
  :hints (("goal" :in-theory (e/d (count-free-clusters find-n-free-clusters)
                                  (nthcdr len-of-find-n-free-clusters-helper))
           :use (:instance len-of-find-n-free-clusters-helper
                           (n2 (len (nthcdr 2 fa-table)))
                           (fa-table (nthcdr 2 fa-table))
                           (n1 n)
                           (start 2))))
  :rule-classes
  (:rewrite
   (:linear
    :corollary
    (<= (len (find-n-free-clusters fa-table n))
        (nfix n)))))

(in-theory (disable (:linear len-of-find-n-free-clusters)))

(defthm consp-of-find-n-free-clusters
  (equal (consp (find-n-free-clusters fa-table n))
         (and (not (zp (count-free-clusters fa-table)))
              (not (zp n))))
  :hints (("goal" :do-not-induct t
           :in-theory (e/d (len-when-consp)
                           (len-of-find-n-free-clusters))
           :use len-of-find-n-free-clusters)))

(defthm find-n-free-clusters-when-zp
  (implies (zp n)
           (equal (find-n-free-clusters fa-table n)
                  nil))
  :hints (("goal" :in-theory (enable find-n-free-clusters
                                     find-n-free-clusters-helper))))

(defcong nat-equiv equal (find-n-free-clusters fa-table n) 2)

(defthmd
  fat32-masked-entry-list-p-alt
  (equal (fat32-masked-entry-list-p x)
         (bounded-nat-listp x *expt-2-28*))
  :hints (("goal" :in-theory (enable fat32-masked-entry-p))))

(defthm
  fat32-masked-entry-list-p-of-find-n-free-clusters
  (implies (and (fat32-entry-list-p fa-table)
                (natp n)
                (>= (len fa-table) *ms-first-data-cluster*)
                (<= (len fa-table) *ms-bad-cluster*))
           (fat32-masked-entry-list-p (find-n-free-clusters fa-table n)))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies
     (and (fat32-entry-list-p fa-table)
          (natp n)
          (>= (len fa-table) *ms-first-data-cluster*)
          (<= (len fa-table) *ms-bad-cluster*))
     (let ((l (find-n-free-clusters fa-table n)))
       (implies (consp l)
                (and (fat32-masked-entry-list-p (cdr l))
                     (fat32-masked-entry-p (car l))))))))
  :hints (("goal" :in-theory (disable find-n-free-clusters-correctness-1)
           :use ((:instance find-n-free-clusters-correctness-1
                            (b (len fa-table)))
                 (:instance fat32-masked-entry-list-p-alt
                            (x (find-n-free-clusters fa-table n)))
                 (:instance bounded-nat-listp-correctness-5
                            (l (find-n-free-clusters fa-table n))
                            (x (len fa-table))
                            (y *expt-2-28*))))))

(defthm
  lower-bounded-integer-listp-of-find-n-free-clusters-helper
  (implies (integerp start)
           (lower-bounded-integer-listp
            (find-n-free-clusters-helper fa-table n start)
            start))
  :hints
  (("goal" :in-theory (enable lower-bounded-integer-listp
                              find-n-free-clusters-helper))
   ("subgoal *1/5.1'"
    :use
    (:instance lower-bounded-integer-listp-correctness-5
               (l (find-n-free-clusters-helper (cdr fa-table)
                                               (+ -1 n)
                                               (+ 1 start)))
               (x (+ 1 start))
               (y start)))
   ("subgoal *1/3''"
    :use
    (:instance lower-bounded-integer-listp-correctness-5
               (l (find-n-free-clusters-helper (cdr fa-table)
                                               n (+ 1 start)))
               (x (+ 1 start))
               (y start)))))

(defthm
  lower-bounded-integer-listp-of-find-n-free-clusters
  (lower-bounded-integer-listp (find-n-free-clusters fa-table n)
                               *ms-first-data-cluster*)
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies
     (consp (find-n-free-clusters fa-table n))
     (lower-bounded-integer-listp (cdr (find-n-free-clusters fa-table n))
                                  *ms-first-data-cluster*))
    :hints (("goal" :in-theory (enable lower-bounded-integer-listp))))
   (:linear
    :corollary (implies (consp (find-n-free-clusters fa-table n))
                        (<= *ms-first-data-cluster*
                            (car (find-n-free-clusters fa-table n))))
    :hints (("goal" :in-theory (enable lower-bounded-integer-listp)))))
  :hints (("goal" :in-theory (enable find-n-free-clusters))))

(defund
  set-indices-in-fa-table
  (fa-table index-list value-list)
  (declare
   (xargs :measure (acl2-count index-list)
          :guard (and (fat32-entry-list-p fa-table)
                      (nat-listp index-list)
                      (fat32-masked-entry-list-p value-list)
                      (equal (len index-list)
                             (len value-list)))))
  (if
   (atom index-list)
   fa-table
   (let
    ((current-index (car index-list)))
    (if
     (or (not (natp current-index))
         (>= current-index (len fa-table)))
     fa-table
     (set-indices-in-fa-table
      (update-nth current-index
                  (fat32-update-lower-28 (nth current-index fa-table)
                                         (car value-list))
                  fa-table)
      (cdr index-list)
      (cdr value-list))))))

(defthm
  set-indices-in-fa-table-correctness-1
  (implies
   (and (fat32-entry-list-p fa-table)
        (bounded-nat-listp index-list (len fa-table))
        (fat32-masked-entry-list-p value-list)
        (equal (len index-list)
               (len value-list)))
   (fat32-entry-list-p
    (set-indices-in-fa-table fa-table index-list value-list)))
  :hints (("Goal" :in-theory (enable set-indices-in-fa-table))))

(defthm
  set-indices-in-fa-table-correctness-2
  (equal (len (set-indices-in-fa-table fa-table index-list value-list))
         (len fa-table))
  :hints (("goal" :in-theory (enable set-indices-in-fa-table))))

;; Well, it might not be a great idea to borrow a numbering scheme from
;; set-indices.lisp
(defthm
  set-indices-in-fa-table-correctness-3
  (implies
   (not (member-equal (nfix n) index-list))
   (equal (nth n
               (set-indices-in-fa-table fa-table index-list value-list))
          (nth n fa-table)))
  :hints (("goal" :in-theory (enable set-indices-in-fa-table))))

(defthm set-indices-in-fa-table-correctness-4
  (implies (and (natp key)
                (< key (len l))
                (nat-listp index-list)
                (not (member-equal key index-list)))
           (equal (set-indices-in-fa-table (update-nth key val l) index-list value-list)
                  (update-nth key val (set-indices-in-fa-table l index-list value-list))))
  :hints (("Goal" :in-theory (enable set-indices-in-fa-table))))

(defthm
  fat32-masked-entry-list-p-when-bounded-nat-listp
  (implies
   (and (bounded-nat-listp file-index-list (len fa-table))
        (<= (len fa-table) *ms-bad-cluster*))
   (fat32-masked-entry-list-p file-index-list))
  :hints (("goal" :in-theory (enable fat32-masked-entry-p))))

(defthm
  fat32-build-index-list-of-set-indices-disjoint
  (implies
   (and
    (natp masked-current-cluster)
    (nat-listp index-list)
    (not (intersectp-equal
          (mv-nth 0
                  (fat32-build-index-list fa-table masked-current-cluster
                                          length cluster-size))
          index-list)))
   (equal (fat32-build-index-list
           (set-indices-in-fa-table fa-table index-list value-list)
           masked-current-cluster
           length cluster-size)
          (fat32-build-index-list fa-table masked-current-cluster
                                  length cluster-size)))
  :hints
  (("goal" :induct (fat32-build-index-list fa-table masked-current-cluster
                                           length cluster-size)
    :in-theory (e/d (intersectp-equal fat32-build-index-list)
                    (intersectp-is-commutative)))))

(defthmd
  set-indices-in-fa-table-of-true-list-fix-1
  (equal (set-indices-in-fa-table fa-table
                                  index-list (true-list-fix value-list))
         (set-indices-in-fa-table fa-table index-list value-list))
  :hints (("goal" :in-theory (enable set-indices-in-fa-table))))

(defcong
  list-equiv equal
  (set-indices-in-fa-table fa-table index-list value-list)
  3
  :hints
  (("goal"
    :do-not-induct t
    :in-theory (enable list-equiv)
    :use ((:instance set-indices-in-fa-table-of-true-list-fix-1
                     (value-list value-list-equiv))
          set-indices-in-fa-table-of-true-list-fix-1))))

(encapsulate
  ()

  (local
   (defun
       induction-scheme
       (file-index-list file-length cluster-size)
     (if (or (zp file-length) (zp cluster-size))
         file-index-list
       (induction-scheme (cdr file-index-list)
                         (nfix (- file-length cluster-size))
                         cluster-size))))

  (defthm
    fat32-build-index-list-of-set-indices-in-fa-table-coincident
    (implies
     (and (natp file-length)
          (no-duplicatesp-equal file-index-list)
          (< (* cluster-size
                (+ -1 (len file-index-list)))
             file-length)
          (lower-bounded-integer-listp file-index-list *ms-first-data-cluster*)
          (bounded-nat-listp file-index-list (len fa-table))
          (consp file-index-list)
          (<= (len fa-table) *ms-bad-cluster*)
          (not (zp cluster-size))
          (fat32-masked-entry-p fat-content)
          (or
           (fat32-is-eof fat-content)
           (<= (len fa-table) fat-content)))
     (equal (fat32-build-index-list
             (set-indices-in-fa-table fa-table file-index-list
                                      (append (cdr file-index-list)
                                              (list fat-content)))
             (car file-index-list)
             file-length cluster-size)
            (mv file-index-list 0)))
    :hints
    (("goal"
      :in-theory (e/d (set-indices-in-fa-table fat32-build-index-list)
                      (fat32-masked-entry-list-p-when-bounded-nat-listp))
      :induct (induction-scheme file-index-list
                                file-length cluster-size)
      :expand
      ((:free (fa-table value-list)
              (set-indices-in-fa-table fa-table file-index-list value-list))
       (fat32-build-index-list
        (update-nth (car file-index-list)
                    (fat32-update-lower-28 (nth (car file-index-list) fa-table)
                                           (cadr file-index-list))
                    (set-indices-in-fa-table fa-table (cdr file-index-list)
                                             (append (cddr file-index-list)
                                                     '(268435455))))
        (car file-index-list)
        file-length cluster-size)
       (fat32-build-index-list
        (update-nth (car file-index-list)
                    (fat32-update-lower-28 (nth (car file-index-list) fa-table)
                                           fat-content)
                    fa-table)
        (car file-index-list)
        file-length cluster-size)))
     ("subgoal *1/2" :use fat32-masked-entry-list-p-when-bounded-nat-listp))))

(defthm
  set-indices-in-fa-table-when-atom
  (implies (atom index-list)
           (equal (set-indices-in-fa-table fa-table index-list value-list)
                  fa-table))
  :hints (("goal" :in-theory (enable set-indices-in-fa-table))))

(defthm
  member-of-fat32-build-index-list
  (implies
   (and (equal (mv-nth 1
                       (fat32-build-index-list fa-table masked-current-cluster
                                               length cluster-size))
               0)
        (equal (fat32-entry-mask (nth x fa-table))
               0))
   (not (member-equal
         x
         (mv-nth 0
                 (fat32-build-index-list fa-table masked-current-cluster
                                         length cluster-size)))))
  :hints (("goal" :in-theory (enable fat32-build-index-list))))

(defthm
  integerp-of-fat32-build-index-list
  (implies
   (not (equal (mv-nth 1
                       (fat32-build-index-list fa-table masked-current-cluster
                                               length cluster-size))
               0))
   (equal (mv-nth 1
                  (fat32-build-index-list fa-table masked-current-cluster
                                          length cluster-size))
          (- *eio*)))
  :hints (("goal" :in-theory (enable fat32-build-index-list)))
  :rule-classes
  (:rewrite
   (:type-prescription
    :corollary
    (and
     (integerp (mv-nth 1
                       (fat32-build-index-list fa-table masked-current-cluster
                                               length cluster-size)))
     (<= (mv-nth 1
                 (fat32-build-index-list fa-table masked-current-cluster
                                         length cluster-size))
         0)))))

(defthm
  fat32-build-index-list-correctness-1
  (implies
   (and (fat32-entry-list-p fa-table)
        (natp n)
        (equal (mv-nth 1
                       (fat32-build-index-list fa-table masked-current-cluster
                                               length cluster-size))
               0))
   (not (intersectp-equal
         (mv-nth 0
                 (fat32-build-index-list fa-table masked-current-cluster
                                         length cluster-size))
         (find-n-free-clusters fa-table n))))
  :hints (("goal" :in-theory (e/d (intersectp-equal fat32-build-index-list)
                                  (intersectp-is-commutative))))
  :rule-classes
  (:rewrite
   (:rewrite
    :corollary
    (implies
     (and
      (fat32-entry-list-p fa-table)
      (natp n)
      (equal (mv-nth 1
                     (fat32-build-index-list fa-table masked-current-cluster
                                             length cluster-size))
             0))
     (not (intersectp-equal
           (find-n-free-clusters fa-table n)
           (mv-nth 0
                   (fat32-build-index-list fa-table masked-current-cluster
                                           length cluster-size))))))))

(encapsulate ()

  (local
   (defthmd
     lemma
     (implies (and (bounded-nat-listp index-list (len fa-table))
                   (fat32-masked-entry-p val)
                   (member-equal n index-list))
              (equal (nth n
                          (set-indices-in-fa-table fa-table index-list
                                                   (make-list-ac (len index-list)
                                                                 val nil)))
                     (fat32-update-lower-28 (nth n fa-table)
                                            val)))
     :hints (("goal" :in-theory (enable set-indices-in-fa-table)))))

  (defthm
    nth-of-set-indices-in-fa-table-when-member
    (implies (and (bounded-nat-listp index-list (len fa-table))
                  (fat32-masked-entry-p val))
             (equal (nth n
                         (set-indices-in-fa-table fa-table index-list
                                                  (make-list-ac (len index-list)
                                                                val nil)))
                    (if (member-equal (nfix n) index-list)
                        (fat32-update-lower-28 (nth n fa-table)
                                               val)
                        (nth n fa-table))))
    :hints
    (("goal"
      :in-theory (e/d (set-indices-in-fa-table)
                      ((:rewrite set-indices-in-fa-table-correctness-3)))
      :use ((:instance (:rewrite set-indices-in-fa-table-correctness-3)
                       (value-list (make-list-ac (len index-list) val nil))
                       (index-list index-list)
                       (fa-table fa-table)
                       (n n))
            (:instance lemma (n (nfix n))))))))

(defthm
  fat32-build-index-list-correctness-2
  (implies
   (equal (mv-nth 1
                  (fat32-build-index-list fa-table masked-current-cluster
                                          length cluster-size))
          0)
   (member-equal
    masked-current-cluster
    (mv-nth 0
            (fat32-build-index-list fa-table masked-current-cluster
                                    length cluster-size))))
  :hints
  (("goal" :in-theory (enable fat32-build-index-list))))

(defthm
  car-of-last-of-fat32-build-index-list
  (implies
   (and (fat32-masked-entry-p masked-current-cluster)
        (< masked-current-cluster (len fa-table)))
   (<
    (car (last (mv-nth 0
                       (fat32-build-index-list fa-table masked-current-cluster
                                               length cluster-size))))
    (len fa-table)))
  :hints
  (("goal"
    :in-theory (disable car-of-last-when-bounded-nat-listp)
    :use
    (:instance
     car-of-last-when-bounded-nat-listp
     (l (mv-nth 0
                (fat32-build-index-list fa-table masked-current-cluster
                                        length cluster-size)))
     (b (len fa-table)))))
  :rule-classes :linear)