forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsFFIPropsScript.sml
1449 lines (1276 loc) · 42.9 KB
/
fsFFIPropsScript.sml
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
(*
Lemmas about the file system model used by the proof about TextIO.
*)
open preamble mlstringTheory cfHeapsBaseTheory fsFFITheory MarshallingTheory
val _ = temp_delsimps ["NORMEQ_CONV"]
val _ = new_theory"fsFFIProps"
val _ = option_monadsyntax.temp_add_option_monadsyntax();
val option_case_eq =
prove_case_eq_thm { nchotomy = option_nchotomy, case_def = option_case_def}
Theorem numchars_self:
!fs. fs = fs with numchars := fs.numchars
Proof
cases_on`fs` >>
fs[fsFFITheory.recordtype_IO_fs_seldef_numchars_fupd_def]
QED
(* we can actually open a file if the OS limit has not been reached and we can
* still encode the file descriptor on 8 bits *)
Overload hasFreeFD = ``λfs. CARD (set (MAP FST fs.infds)) < MIN fs.maxFD (256**8)``
(* nextFD lemmas *)
Theorem nextFD_ltX:
CARD (set (MAP FST fs.infds)) < x ⇒ nextFD fs < x
Proof
simp[nextFD_def] >> strip_tac >> numLib.LEAST_ELIM_TAC >> simp[] >>
qabbrev_tac `ns = MAP FST fs.infds` >> RM_ALL_ABBREVS_TAC >> conj_tac
>- (qexists_tac `MAX_SET (set ns) + 1` >>
pop_assum kall_tac >> DEEP_INTRO_TAC MAX_SET_ELIM >> simp[] >>
rpt strip_tac >> res_tac >> fs[]) >>
rpt strip_tac >> spose_not_then assume_tac >>
`count x ⊆ set ns` by simp[SUBSET_DEF] >>
`x ≤ CARD (set ns)`
by metis_tac[CARD_COUNT, CARD_SUBSET, FINITE_LIST_TO_SET] >>
fs[]
QED
Theorem nextFD_leX:
CARD (set (MAP FST fs.infds)) ≤ x ⇒ nextFD fs ≤ x
Proof
simp[nextFD_def] >> strip_tac >> numLib.LEAST_ELIM_TAC >> simp[] >>
qabbrev_tac `ns = MAP FST fs.infds` >> RM_ALL_ABBREVS_TAC >> conj_tac
>- (qexists_tac `MAX_SET (set ns) + 1` >>
pop_assum kall_tac >> DEEP_INTRO_TAC MAX_SET_ELIM >> simp[] >>
rpt strip_tac >> res_tac >> fs[]) >>
rpt strip_tac >> spose_not_then assume_tac >>
`count x PSUBSET set ns` by (
simp[PSUBSET_DEF,SUBSET_DEF] >>
simp[EXTENSION] \\
qexists_tac`x` \\ simp[] ) \\
`x < CARD (set ns)`
by metis_tac[CARD_COUNT, CARD_PSUBSET, FINITE_LIST_TO_SET] >>
fs[]
QED
Theorem nextFD_NOT_MEM:
∀f m n fs. ¬MEM (nextFD fs,f,m,n) fs.infds
Proof
rpt gen_tac >> simp[nextFD_def] >> numLib.LEAST_ELIM_TAC >> conj_tac
>- (qexists_tac `MAX_SET (set (MAP FST fs.infds)) + 1` >>
DEEP_INTRO_TAC MAX_SET_ELIM >>
simp[MEM_MAP, EXISTS_PROD, FORALL_PROD] >> rw[] >> strip_tac >>
res_tac >> fs[]) >>
simp[EXISTS_PROD, FORALL_PROD, MEM_MAP]
QED
Theorem nextFD_numchars:
!fs ll. nextFD (fs with numchars := ll) = nextFD fs
Proof
rw[nextFD_def]
QED
(* bumpFD lemmas *)
Theorem bumpFD_numchars:
!fs fd n ll. bumpFD fd (fs with numchars := ll) n =
(bumpFD fd fs n) with numchars := THE (LTL ll)
Proof
fs[bumpFD_def]
QED
Theorem bumpFD_inode_tbl[simp]:
(bumpFD fd fs n).inode_tbl = fs.inode_tbl
Proof
EVAL_TAC
QED
Theorem bumpFD_files[simp]:
(bumpFD fd fs n).files = fs.files
Proof
EVAL_TAC
QED
Theorem bumpFD_o:
!fs fd n1 n2.
bumpFD fd (bumpFD fd fs n1) n2 =
bumpFD fd fs (n1 + n2) with numchars := THE (LTL (THE (LTL fs.numchars)))
Proof
rw[bumpFD_def] >> cases_on`fs` >> fs[IO_fs_component_equality] >>
fs[AFUPDKEY_o] >> irule AFUPDKEY_eq >> rw[] >> PairCases_on `v` >> fs[]
QED
Theorem bumpFD_0:
bumpFD fd fs 0 = fs with numchars := THE (LTL fs.numchars)
Proof
rw[bumpFD_def,IO_fs_component_equality] \\
match_mp_tac AFUPDKEY_unchanged \\
simp[FORALL_PROD]
QED
(* validFD lemmas *)
Theorem validFD_numchars:
!fd fs ll. validFD fd fs <=> validFD fd (fs with numchars := ll)
Proof
rw[validFD_def]
QED
Theorem validFD_bumpFD:
validFD fd' fs ⇒ validFD fd' (bumpFD fd fs n)
Proof
rw[bumpFD_def,validFD_def]
QED
Theorem validFD_ALOOKUP:
validFD fd fs ==> ?v. ALOOKUP fs.infds fd = SOME v
Proof
rw[validFD_def] >> cases_on`ALOOKUP fs.infds fd` >> fs[ALOOKUP_NONE]
QED
Theorem ALOOKUP_validFD:
ALOOKUP fs.infds fd = SOME (fname, md, pos) ⇒ validFD fd fs
Proof
rw[validFD_def] >> imp_res_tac ALOOKUP_MEM >>
fs[MEM_MAP,EXISTS_PROD] >> metis_tac[]
QED
val validFileFD_def = Define`
validFileFD fd infds ⇔
∃fnm md off. ALOOKUP infds fd = SOME (File fnm, md, off)`;
Theorem validFD_nextFD:
~validFD (nextFD fs) fs
Proof
fs [validFD_def,nextFD_def]
\\ qabbrev_tac `xs = MAP FST fs.infds`
\\ match_mp_tac (SIMP_RULE std_ss []
(Q.ISPEC `\n:num. ~MEM n xs` whileTheory.LEAST_INTRO))
\\ qexists_tac `SUM xs + 1`
\\ strip_tac
\\ qsuff_tac `!xs m:num. MEM m xs ==> m <= SUM xs`
THEN1 (strip_tac \\ res_tac \\ fs [])
\\ Induct \\ fs [] \\ rw [] \\ fs [] \\ res_tac \\ fs []
QED
(* getNullTermStr lemmas *)
Theorem getNullTermStr_add_null:
∀cs. ¬MEM 0w cs ⇒ getNullTermStr (cs++(0w::ls)) = SOME (MAP (CHR o w2n) cs)
Proof
simp[getNullTermStr_def, findi_APPEND, NOT_MEM_findi, findi_def, TAKE_APPEND]
QED
Theorem getNullTermStr_insert_atI:
∀cs l. LENGTH cs < LENGTH l ∧ ¬MEM 0w cs ⇒
getNullTermStr (insert_atI (cs++[0w]) 0 l) = SOME (MAP (CHR o w2n) cs)
Proof
simp[getNullTermStr_def, insert_atI_def, findi_APPEND, NOT_MEM_findi,
findi_def, TAKE_APPEND]
QED
(* the filesystem will always eventually allow to write something *)
val live_numchars_def = Define`
live_numchars ns ⇔
¬LFINITE ns ∧
always (eventually (λll. ∃k. LHD ll = SOME k ∧ k ≠ 0n)) ns`;
val liveFS_def = Define`
liveFS fs ⇔ live_numchars fs.numchars`;
(* each inode refered to by a filename has a content *)
val consistentFS_def = Define`
consistentFS fs = (∀fname ino. ALOOKUP fs.files fname = SOME ino ⇒
(File ino) ∈ FDOM (alist_to_fmap fs.inode_tbl))`
(* well formed file descriptor: all descriptors are <= maxFD
* and correspond to file names in files *)
val wfFS_def = Define`
wfFS fs =
((∀fd. fd ∈ FDOM (alist_to_fmap fs.infds) ⇒
fd <= fs.maxFD ∧
∃ino off. ALOOKUP fs.infds fd = SOME (ino,off) ∧
ino ∈ FDOM (alist_to_fmap fs.inode_tbl))∧
consistentFS fs ∧ liveFS fs)
`;
Theorem consistentFS_with_numchars[simp]:
!fs ll. consistentFS fs ⇒ consistentFS (fs with numchars := ll)
Proof
fs[consistentFS_def]
QED
Theorem wfFS_numchars:
!fs ll. wfFS fs ==> ¬LFINITE ll ==>
always (eventually (λll. ∃k. LHD ll = SOME k ∧ k ≠ 0)) ll ==>
wfFS (fs with numchars := ll)
Proof
rw[wfFS_def,liveFS_def,live_numchars_def]
QED
Theorem wfFS_LTL:
!fs ll. wfFS (fs with numchars := ll) ==>
wfFS (fs with numchars := THE (LTL ll))
Proof
rw[wfFS_def,liveFS_def,live_numchars_def,consistentFS_def] >>
cases_on `ll` >> fs[LDROP_1] >> imp_res_tac always_thm >> metis_tac[]
QED
Theorem wfFS_openFile:
wfFS fs ⇒ wfFS (openFileFS fnm fs md off)
Proof
simp[openFileFS_def, openFile_def] >>
Cases_on `nextFD fs <= fs.maxFD` >> simp[] >>
Cases_on`ALOOKUP fs.files fnm` >> simp[] >>
Cases_on `ALOOKUP fs.inode_tbl (File x)` >> simp[] >>
dsimp[wfFS_def,consistentFS_def, MEM_MAP, EXISTS_PROD, FORALL_PROD] >> rw[] >>
fs[liveFS_def] >> imp_res_tac ALOOKUP_EXISTS_IFF >>
metis_tac[]
QED
Theorem wfFS_ADELKEY[simp]:
wfFS fs ⇒ wfFS (fs with infds updated_by ADELKEY k)
Proof
simp[wfFS_def, MEM_MAP, PULL_EXISTS, FORALL_PROD, EXISTS_PROD,
ALOOKUP_ADELKEY,liveFS_def,consistentFS_def] >>
metis_tac[]
QED
Theorem wfFS_LDROP:
wfFS fs ==> wfFS (fs with numchars := (THE (LDROP k fs.numchars)))
Proof
rw[wfFS_def,liveFS_def,live_numchars_def,always_DROP,consistentFS_def] >>
imp_res_tac NOT_LFINITE_DROP >>
first_x_assum (assume_tac o Q.SPEC `k`) >> fs[] >>
metis_tac[NOT_LFINITE_DROP_LFINITE]
QED
Theorem wfFS_bumpFD[simp]:
wfFS fs ⇒ wfFS (bumpFD fd fs n)
Proof
simp[bumpFD_def] >>
dsimp[wfFS_def, AFUPDKEY_ALOOKUP, option_case_eq, bool_case_eq,
EXISTS_PROD,consistentFS_def] >>
rw[] >- metis_tac[] >>
cases_on`fs.numchars` >> fs[liveFS_def,live_numchars_def] >>
imp_res_tac always_thm >> metis_tac[]
QED
(* end of file is reached when the position index is the length of the file *)
val eof_def = Define`
eof fd fsys =
do
(ino,md,pos) <- ALOOKUP fsys.infds fd ;
contents <- ALOOKUP fsys.inode_tbl ino ;
return (LENGTH contents <= pos)
od
`;
Theorem eof_numchars[simp]:
eof fd (fs with numchars := ll) = eof fd fs
Proof
rw[eof_def]
QED
Theorem wfFS_eof_EQ_SOME:
wfFS fs ∧ validFD fd fs ⇒
∃b. eof fd fs = SOME b
Proof
simp[eof_def, EXISTS_PROD, PULL_EXISTS, MEM_MAP, wfFS_def, validFD_def] >>
rpt strip_tac >> res_tac >> metis_tac[ALOOKUP_EXISTS_IFF]
QED
(*
Theorem eof_read:
!fd fs n. wfFS fs ⇒
0 < n ⇒ (eof fd fs = SOME T) ⇒
read fd fs n = SOME ([], fs with numchars := THE(LTL fs.numchars))
Proof
rw[eof_def,read_def,MIN_DEF] >>
qexists_tac `x` >> rw[] >>
pairarg_tac >> fs[bumpFD_def,wfFS_def] >>
cases_on`fs.numchars` >> fs[IO_fs_component_equality,liveFS_def,live_numchars_def] >>
irule AFUPDKEY_unchanged >> cases_on`v` >> rw[]
QED
*)
Theorem eof_read:
!fd fs n fs'. 0 < n ⇒ read fd fs n = SOME ([], fs') ⇒ eof fd fs = SOME T
Proof
rw[eof_def,read_def] >>
qexists_tac `x` >> rw[] >>
PairCases_on `x` >>
fs[DROP_NIL]
QED
(*
Theorem neof_read:
eof fd fs = SOME F ⇒ 0 < n ⇒
wfFS fs ⇒
∃l fs'. l <> "" /\ read fd fs n = SOME (l,fs')
Proof
mp_tac (Q.SPECL [`fd`, `fs`, `n`] read_def) >>
rw[wfFS_def,liveFS_def,live_numchars_def] >>
cases_on `ALOOKUP fs.infds fd` >> fs[eof_def] >>
cases_on `x` >> fs[] >>
cases_on `ALOOKUP fs.inode_tbl q` >> fs[eof_def] >>
cases_on `fs.numchars` >> fs[] >>
cases_on `DROP x2 contents` >> fs[] >>
`x2 ≥ LENGTH contents` by fs[DROP_EMPTY] >>
fs[]
QED
*)
Theorem get_file_content_eof:
get_file_content fs fd = SOME (content,pos) ⇒ eof fd fs = SOME (¬(pos < LENGTH content))
Proof
rw[get_file_content_def,eof_def]
\\ pairarg_tac \\ fs[]
QED
(* inFS_fname *)
val inFS_fname_def = Define `
inFS_fname fs s = (?ino. ALOOKUP fs.files s = SOME ino)`
Theorem not_inFS_fname_openFile:
~inFS_fname fs iname ⇒ openFile iname fs md off = NONE
Proof
rw[inFS_fname_def, openFile_def] >>
Cases_on`ALOOKUP fs.files iname` >> fs[]
QED
Theorem inFS_fname_ALOOKUP_EXISTS:
! fs fname. consistentFS fs /\ inFS_fname fs fname ⇒
∃ino content.
ALOOKUP fs.files fname = SOME ino /\
ALOOKUP fs.inode_tbl (File ino) = SOME content
Proof
fs [inFS_fname_def] >> rpt strip_tac >> fs[] >>
Cases_on`ALOOKUP fs.files fname` >> fs[ALOOKUP_NONE,wfFS_def] >>
rename1 `File ino` >>
Cases_on`ALOOKUP fs.inode_tbl (File ino)` >> fs[ALOOKUP_NONE,consistentFS_def] >>
rw[] >> res_tac >> fs[MEM_MAP]
QED
Theorem ALOOKUP_SOME_inFS_fname:
ALOOKUP fs.files fnm = SOME ino ==> inFS_fname fs fnm
Proof
rw[openFileFS_def,openFile_def] \\ imp_res_tac inFS_fname_def
QED
Theorem ALOOKUP_inFS_fname_openFileFS_nextFD:
!fs f ino off md. consistentFS fs /\ inFS_fname fs f ∧ nextFD fs <= fs.maxFD ∧
ALOOKUP fs.files f = SOME ino
⇒
ALOOKUP (openFileFS f fs md off).infds (nextFD fs) = SOME (File ino,md,off)
Proof
rw[openFileFS_def,openFile_def]
\\ imp_res_tac inFS_fname_ALOOKUP_EXISTS
\\ rfs[]
QED
Theorem inFS_fname_numchars:
!s fs ll. inFS_fname (fs with numchars := ll) s = inFS_fname fs s
Proof
EVAL_TAC >> rw[]
QED
(* ffi lengths *)
Theorem ffi_open_in_length:
ffi_open_in conf bytes fs = SOME (FFIreturn bytes' fs') ==> LENGTH bytes' = LENGTH bytes
Proof
rw[ffi_open_in_def] \\ fs[option_eq_some]
\\ TRY(pairarg_tac) \\ rw[] \\ fs[] \\ rw[] \\ fs[n2w8_def]
QED
Theorem ffi_open_out_length:
ffi_open_out conf bytes fs = SOME (FFIreturn bytes' fs') ==> LENGTH bytes' = LENGTH bytes
Proof
rw[ffi_open_out_def] \\ fs[option_eq_some]
\\ TRY(pairarg_tac) \\ rw[] \\ fs[] \\ rw[] \\ fs[n2w8_def]
QED
Theorem read_length:
read fd fs k = SOME (l, fs') ==> LENGTH l <= k
Proof
rw[read_def] >> pairarg_tac >> fs[option_eq_some,LENGTH_TAKE] >>
` l = TAKE (MIN k (MIN (STRLEN content − off) (SUC strm)))
(DROP off content)` by (fs[]) >>
fs[MIN_DEF,LENGTH_DROP]
QED
Theorem ffi_read_length:
ffi_read conf bytes fs = SOME (FFIreturn bytes' fs') ==> LENGTH bytes' = LENGTH bytes
Proof
rw[ffi_read_def]
\\ fs[option_case_eq,prove_case_eq_thm{nchotomy=list_nchotomy,case_def=list_case_def}]
\\ fs[option_eq_some]
\\ TRY(pairarg_tac) \\ rveq \\ fs[] \\ rveq \\ fs[n2w2_def]
\\ imp_res_tac read_length \\ fs[]
QED
Theorem ffi_write_length:
ffi_write conf bytes fs = SOME (FFIreturn bytes' fs') ==> LENGTH bytes' = LENGTH bytes
Proof
EVAL_TAC \\ rw[]
\\ fs[option_eq_some] \\ every_case_tac \\ fs[] \\ rw[]
\\ pairarg_tac \\ fs[] \\ pairarg_tac \\ fs[n2w2_def]
\\ rw[] \\ Cases_on`bytes` \\ fs[]
\\ rpt(Cases_on`t` \\ fs[] \\ Cases_on`t'` \\ fs[])
QED
Theorem ffi_close_length:
ffi_close conf bytes fs = SOME (FFIreturn bytes' fs') ==> LENGTH bytes' = LENGTH bytes
Proof
rw[ffi_close_def] \\ fs[option_eq_some] \\ TRY pairarg_tac \\ fs[] \\ rw[]
QED
(* fastForwardFD *)
val fastForwardFD_def = Define`
fastForwardFD fs fd =
the fs (do
(ino,md,off) <- ALOOKUP fs.infds fd;
content <- ALOOKUP fs.inode_tbl ino;
SOME (fs with infds updated_by AFUPDKEY fd (I ## I ## MAX (LENGTH content)))
od)`;
Theorem validFD_fastForwardFD[simp]:
validFD fd (fastForwardFD fs fd) = validFD fd fs
Proof
rw[validFD_def,fastForwardFD_def,bumpFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ rw[OPTION_GUARD_COND,libTheory.the_def]
QED
Theorem validFileFD_fastForwardFD[simp]:
validFileFD fd (fastForwardFD fs x).infds ⇔ validFileFD fd fs.infds
Proof
rw[validFileFD_def, fastForwardFD_def]
\\ Cases_on`ALOOKUP fs.infds x` \\ rw[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ simp[AFUPDKEY_ALOOKUP]
\\ TOP_CASE_TAC \\ simp[]
\\ rw[PAIR_MAP, FST_EQ_EQUIV, PULL_EXISTS, SND_EQ_EQUIV]
\\ rw[EQ_IMP_THM]
QED
Theorem fastForwardFD_inode_tbl[simp]:
(fastForwardFD fs fd).inode_tbl = fs.inode_tbl
Proof
EVAL_TAC
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ rw[OPTION_GUARD_COND,libTheory.the_def]
QED
Theorem fastForwardFD_files[simp]:
!fs fd. (fastForwardFD fs fd).files = fs.files
Proof
rw[fastForwardFD_def] >>
Cases_on`ALOOKUP fs.infds fd` >> fs[libTheory.the_def] >>
pairarg_tac >> fs[] >>
Cases_on`ALOOKUP fs.inode_tbl ino` >> fs[libTheory.the_def]
QED
Theorem ADELKEY_fastForwardFD_elim[simp]:
ADELKEY fd (fastForwardFD fs fd).infds = ADELKEY fd fs.infds
Proof
rw[fastForwardFD_def,bumpFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ rw[OPTION_GUARD_COND,libTheory.the_def]
QED
Theorem fastForwardFD_ADELKEY_same[simp]:
fastForwardFD fs fd with infds updated_by ADELKEY fd =
fs with infds updated_by ADELKEY fd
Proof
rw[fastForwardFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[libTheory.the_def]
\\ pairarg_tac \\ fs[libTheory.the_def]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ fs[IO_fs_component_equality,ADELKEY_unchanged]
QED
Theorem fastForwardFD_0:
(∀content pos. get_file_content fs fd = SOME (content,pos) ⇒ LENGTH content ≤ pos) ⇒
fastForwardFD fs fd = fs
Proof
rw[fastForwardFD_def,get_file_content_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ fs[IO_fs_component_equality]
\\ match_mp_tac AFUPDKEY_unchanged
\\ rw[] \\ rw[PAIR_MAP_THM]
\\ rw[MAX_DEF]
QED
Theorem fastForwardFD_with_numchars:
fastForwardFD (fs with numchars := ns) fd = fastForwardFD fs fd with numchars := ns
Proof
rw[fastForwardFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ simp[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ simp[libTheory.the_def]
QED
Theorem fastForwardFD_numchars[simp]:
(fastForwardFD fs fd).numchars = fs.numchars
Proof
rw[fastForwardFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ simp[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ simp[libTheory.the_def]
QED
Theorem fastForwardFD_maxFD[simp]:
(fastForwardFD fs fd).maxFD = fs.maxFD
Proof
rw[fastForwardFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ simp[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ simp[libTheory.the_def]
QED
Theorem wfFS_fastForwardFD[simp]:
!fs fd. validFD fd fs /\ wfFS fs ==> wfFS (fastForwardFD fs fd)
Proof
rw[wfFS_def,fastForwardFD_def,validFD_def]
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[libTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[libTheory.the_def]
\\ rw[]
>-(res_tac \\ simp[AFUPDKEY_ALOOKUP] \\ CASE_TAC \\ fs[])
>-(fs[consistentFS_def] \\ rw[] \\ res_tac)
>-(fs[liveFS_def])
QED
Theorem fsupdate_fastForwardFD_comm:
∀fs fd1 fd2 k p c .
ALOOKUP fs.infds fd1 = SOME(ino1,r,pos1) /\
ALOOKUP fs.infds fd2 = SOME(ino2,r',pos2) /\
ALOOKUP fs.inode_tbl ino1 = SOME content1 /\
ALOOKUP fs.inode_tbl ino2 = SOME content2 /\
ino1 ≠ ino2 ⇒
fsupdate (fastForwardFD fs fd1) fd2 k p c =
fastForwardFD (fsupdate fs fd2 k p c) fd1
Proof
rw[fsupdate_def,fastForwardFD_def,AFUPDKEY_ALOOKUP] >> EVAL_TAC >>
fs[AFUPDKEY_ALOOKUP,IO_fs_component_equality,AFUPDKEY_comm]
QED
(* fsupdate *)
Theorem wfFS_fsupdate:
! fs fd content pos k. wfFS fs ==> MEM fd (MAP FST fs.infds) ==>
wfFS (fsupdate fs fd k pos content)
Proof
rw[wfFS_def,fsupdate_def,consistentFS_def]
>- (res_tac \\ fs[])
>- (CASE_TAC \\ fs[] \\
CASE_TAC \\ fs[AFUPDKEY_ALOOKUP] \\
res_tac \\ fs[] \\ rw[])
>-(res_tac >> fs[] >> res_tac)
>-( CASE_TAC \\ fs[] \\ CASE_TAC \\ fs[] \\
fs[liveFS_def,live_numchars_def,always_DROP] >>
`∃y. LDROP k fs.numchars = SOME y` by(fs[NOT_LFINITE_DROP]) >>
fs[] >> metis_tac[NOT_LFINITE_DROP_LFINITE])
QED
Theorem fsupdate_unchanged:
get_file_content fs fd = SOME(content, pos) ==>
fsupdate fs fd 0 pos content = fs
Proof
fs[fsupdate_def,get_file_content_def,validFD_def,IO_fs_component_equality]>>
rw[] >> pairarg_tac >> fs[AFUPDKEY_unchanged] >> rw[]
QED
Theorem fsupdate_o:
liveFS fs ==>
fsupdate (fsupdate fs fd k1 pos1 c1) fd k2 pos2 c2 =
fsupdate fs fd (k1+k2) pos2 c2
Proof
rw[fsupdate_def]
\\ CASE_TAC \\ fs[]
\\ CASE_TAC \\ fs[]
\\ fs[IO_fs_component_equality]
\\ fs[AFUPDKEY_ALOOKUP,AFUPDKEY_o,AFUPDKEY_eq] \\
fs[LDROP_ADD,liveFS_def,live_numchars_def] >> imp_res_tac NOT_LFINITE_DROP >>
FIRST_X_ASSUM(ASSUME_TAC o Q.SPEC`k1`) >> fs[]
\\ rpt (AP_TERM_TAC ORELSE AP_THM_TAC)
\\ simp[FUN_EQ_THM, FORALL_PROD]
QED
Theorem fsupdate_o_0[simp]:
fsupdate (fsupdate fs fd 0 pos1 c1) fd 0 pos2 c2 =
fsupdate fs fd 0 pos2 c2
Proof
rw[fsupdate_def] \\ CASE_TAC \\ fs[] \\ CASE_TAC \\ fs[] \\
rw[IO_fs_component_equality,AFUPDKEY_ALOOKUP,AFUPDKEY_o]
\\ rpt(AP_TERM_TAC ORELSE AP_THM_TAC)
\\ simp[FUN_EQ_THM, FORALL_PROD]
QED
Theorem fsupdate_comm:
!fs fd1 fd2 k1 p1 c1 fnm1 pos1 k2 p2 c2 fnm2 pos2.
ALOOKUP fs.infds fd1 = SOME(fnm1, pos1) /\
ALOOKUP fs.infds fd2 = SOME(fnm2, pos2) /\
fnm1 <> fnm2 /\ fd1 <> fd2 /\ ¬ LFINITE fs.numchars ==>
fsupdate (fsupdate fs fd1 k1 p1 c1) fd2 k2 p2 c2 =
fsupdate (fsupdate fs fd2 k2 p2 c2) fd1 k1 p1 c1
Proof
fs[fsupdate_def] >> rw[] >> fs[AFUPDKEY_ALOOKUP] >>
rpt CASE_TAC >> fs[AFUPDKEY_comm,LDROP_LDROP]
QED
Theorem fsupdate_MAP_FST_infds[simp]:
MAP FST (fsupdate fs fd k pos c).infds = MAP FST fs.infds
Proof
rw[fsupdate_def] \\ every_case_tac \\ rw[]
QED
Theorem fsupdate_MAP_FST_inode_tbl[simp]:
MAP FST (fsupdate fs fd k pos c).inode_tbl = MAP FST fs.inode_tbl
Proof
rw[fsupdate_def] \\ every_case_tac \\ rw[]
QED
Theorem fsupdate_MAP_FST_files[simp]:
MAP FST (fsupdate fs fd k pos c).files = MAP FST fs.files
Proof
rw[fsupdate_def] \\ every_case_tac \\ rw[]
QED
Theorem validFD_fsupdate[simp]:
validFD fd (fsupdate fs fd' x y z) ⇔ validFD fd fs
Proof
rw[fsupdate_def,validFD_def]
QED
Theorem validFileFD_fsupdate[simp]:
validFileFD fd (fsupdate fs fd' x y z).infds ⇔ validFileFD fd fs.infds
Proof
rw[fsupdate_def,validFileFD_def]
\\ TOP_CASE_TAC \\ simp[]
\\ TOP_CASE_TAC \\ simp[]
\\ simp[AFUPDKEY_ALOOKUP]
\\ TOP_CASE_TAC \\ simp[]
\\ rw[]
\\ PairCases_on`x`
\\ simp[]
QED
Theorem fsupdate_numchars:
!fs fd k p c ll. fsupdate fs fd k p c with numchars := ll =
fsupdate (fs with numchars := ll) fd 0 p c
Proof
rw[fsupdate_def] \\ CASE_TAC \\ CASE_TAC \\ rw[]
QED
Theorem fsupdate_ADELKEY:
fd ≠ fd' ⇒
fsupdate (fs with infds updated_by ADELKEY fd') fd k pos content =
fsupdate fs fd k pos content with infds updated_by ADELKEY fd'
Proof
rw[fsupdate_def,ALOOKUP_ADELKEY]
\\ CASE_TAC \\ CASE_TAC
\\ rw[ADELKEY_AFUPDKEY]
QED
Theorem fsupdate_0_numchars:
IS_SOME (ALOOKUP fs.infds fd) ⇒
fsupdate fs fd n pos content =
fsupdate (fs with numchars := THE (LDROP n fs.numchars)) fd 0 pos content
Proof
rw[fsupdate_def] \\ TOP_CASE_TAC \\ fs[]
QED
Theorem fsupdate_maxFD[simp]:
!fs fd k pos content.
(fsupdate fs fd k pos content).maxFD = fs.maxFD
Proof
rw [fsupdate_def] \\ every_case_tac \\ simp []
QED
(* get_file_content *)
Theorem get_file_content_numchars:
!fs fd. get_file_content fs fd =
get_file_content (fs with numchars := ll) fd
Proof
fs[get_file_content_def]
QED
Theorem get_file_content_validFD:
get_file_content fs fd = SOME(c,p) ⇒ validFD fd fs
Proof
fs[get_file_content_def,validFD_def] >> rw[] >> pairarg_tac >>
imp_res_tac ALOOKUP_MEM >> fs[ALOOKUP_MEM,MEM_MAP] >>
qexists_tac`(fd,x)` >> fs[]
QED
Theorem get_file_content_fsupdate:
!fs fd x i c u. get_file_content fs fd = SOME u ⇒
get_file_content (fsupdate fs fd x i c) fd = SOME(c,i)
Proof
rw[get_file_content_def, fsupdate_def] >>
pairarg_tac >> fs[AFUPDKEY_ALOOKUP]
QED
Theorem get_file_content_fsupdate_unchanged:
!fs fd u fnm pos fd' fnm' pos' x i c.
get_file_content fs fd = SOME u ⇒
ALOOKUP fs.infds fd = SOME (fnm,pos) ⇒
ALOOKUP fs.infds fd' = SOME (fnm',pos') ⇒ fnm ≠ fnm' ⇒
get_file_content (fsupdate fs fd' x i c) fd = SOME u
Proof
rw[get_file_content_def, fsupdate_def] >>
pairarg_tac >> fs[AFUPDKEY_ALOOKUP] >>
rpt(CASE_TAC >> fs[])
QED
Theorem get_file_content_bumpFD[simp]:
!fs fd c pos n.
get_file_content (bumpFD fd fs n) fd =
OPTION_MAP (I ## (+) n) (get_file_content fs fd)
Proof
rw[get_file_content_def,bumpFD_def,AFUPDKEY_ALOOKUP]
\\ CASE_TAC \\ fs[]
\\ pairarg_tac \\ fs[]
\\ pairarg_tac \\ fs[] \\ rw[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[]
QED
(* liveFS *)
Theorem liveFS_openFileFS:
liveFS fs ⇒ liveFS (openFileFS s fs md n)
Proof
rw[liveFS_def,openFileFS_def, openFile_def] >>
CASE_TAC >> fs[] >> CASE_TAC >> fs[] >>
`r.numchars = fs.numchars` by
(cases_on`fs` >> cases_on`r` >> fs[fsFFITheory.recordtype_IO_fs_seldef_infds_fupd_def]) >>
fs[]
QED
Theorem liveFS_fsupdate:
liveFS fs ⇒ liveFS (fsupdate fs fd n k c)
Proof
rw[liveFS_def,live_numchars_def,fsupdate_def] >>
every_case_tac \\ fs[always_DROP] \\
metis_tac[NOT_LFINITE_DROP,NOT_LFINITE_DROP_LFINITE,THE_DEF]
QED
Theorem liveFS_bumpFD:
liveFS fs ⇒ liveFS (bumpFD fd fs k)
Proof
rw[liveFS_def,live_numchars_def,bumpFD_def] >> cases_on`fs.numchars` >> fs[] >>
imp_res_tac always_thm
QED
(* openFile, openFileFS *)
Theorem openFile_fupd_numchars:
!s fs k ll fd fs'. openFile s (fs with numchars := ll) md k =
case openFile s fs md k of
SOME (fd, fs') => SOME (fd, fs' with numchars := ll)
| NONE => NONE
Proof
rw[openFile_def,nextFD_def] >> rpt(CASE_TAC >> fs[]) >>
rfs[IO_fs_component_equality]
QED
Theorem openFileFS_numchars[simp]:
!s fs k. (openFileFS s fs md k).numchars = fs.numchars
Proof
rw[openFileFS_def] \\ CASE_TAC \\ CASE_TAC
\\ fs[openFile_def] \\ rw[]
QED
Theorem openFileFS_inode_tbl[simp]:
!s fs k md. (openFileFS s fs md k).inode_tbl = fs.inode_tbl
Proof
rw[openFileFS_def] \\ CASE_TAC \\ CASE_TAC \\ fs[openFile_def] \\ rw[]
QED
Theorem openFileFS_files[simp]:
!s fs k md. (openFileFS s fs md k).files = fs.files
Proof
rw[openFileFS_def] \\ CASE_TAC \\ CASE_TAC \\ fs[openFile_def] \\ rw[]
QED
Theorem wfFS_openFileFS:
!f fs k md. CARD (FDOM (alist_to_fmap fs.infds)) <= fs.maxFD /\ wfFS fs ==>
wfFS (openFileFS f fs md k)
Proof
rw[wfFS_def,openFileFS_def,liveFS_def] >> full_case_tac >> fs[openFile_def] >>
cases_on`x` >> rw[] >> fs[MEM_MAP] >> res_tac >> fs[]
>-(imp_res_tac ALOOKUP_MEM >-(qexists_tac`(File iname,x')` >> fs[]))
>-(CASE_TAC
>-(cases_on`y` >> fs[] >> PairCases_on`r` >> fs[] >> metis_tac[nextFD_NOT_MEM])
>-(metis_tac[]))
>-(fs[consistentFS_def,MEM_MAP] >> rw[] >> res_tac >> metis_tac[])
QED
Theorem openFileFS_maxFD[simp]:
(openFileFS f fs md pos).maxFD = fs.maxFD
Proof
rw[openFileFS_def]
\\ CASE_TAC
\\ CASE_TAC
\\ fs[openFile_def]
\\ rw[]
QED
Theorem openFileFS_fupd_numchars:
!s fs k ll. openFileFS s (fs with numchars := ll) md k =
(openFileFS s fs md k with numchars := ll)
Proof
rw[openFileFS_def,openFile_fupd_numchars] >> rpt CASE_TAC
QED
Theorem IS_SOME_get_file_content_openFileFS_nextFD:
!fs f off md. consistentFS fs ∧ inFS_fname fs f ∧ nextFD fs ≤ fs.maxFD
⇒ IS_SOME (get_file_content (openFileFS f fs md off) (nextFD fs))
Proof
rw[get_file_content_def]
\\ imp_res_tac inFS_fname_ALOOKUP_EXISTS \\ fs[]
\\ imp_res_tac ALOOKUP_inFS_fname_openFileFS_nextFD \\ simp[]
QED
Theorem ADELKEY_nextFD_openFileFS[simp]:
nextFD fs <= fs.maxFD ⇒
ADELKEY (nextFD fs) (openFileFS f fs md off).infds = fs.infds
Proof
rw[openFileFS_def]
\\ CASE_TAC
\\ TRY CASE_TAC
\\ simp[ADELKEY_unchanged,nextFD_NOT_MEM,MEM_MAP,EXISTS_PROD]
\\ fs[openFile_def] \\ rw[]
\\ rw[ADELKEY_def,FILTER_EQ_ID,EVERY_MEM,FORALL_PROD,nextFD_NOT_MEM]
QED
Theorem openFileFS_ADELKEY_nextFD:
nextFD fs ≤ fs.maxFD ⇒
openFileFS f fs md off with infds updated_by ADELKEY (nextFD fs) = fs
Proof
rw[IO_fs_component_equality,ADELKEY_nextFD_openFileFS]
QED
(* forwardFD: like bumpFD but leave numchars *)
val forwardFD_def = Define`
forwardFD fs fd n =
fs with infds updated_by AFUPDKEY fd (I ## I ## (+) n)`;
Theorem forwardFD_const[simp]:
(forwardFD fs fd n).files = fs.files ∧
(forwardFD fs fd n).inode_tbl = fs.inode_tbl ∧
(forwardFD fs fd n).numchars = fs.numchars ∧
(forwardFD fs fd n).maxFD = fs.maxFD
Proof
rw[forwardFD_def]
QED
Theorem forwardFD_o:
forwardFD (forwardFD fs fd n) fd m = forwardFD fs fd (n+m)
Proof
rw[forwardFD_def,IO_fs_component_equality,AFUPDKEY_o]
\\ AP_THM_TAC \\ AP_TERM_TAC
\\ simp[FUN_EQ_THM,FORALL_PROD]
QED
Theorem forwardFD_0[simp]:
forwardFD fs fd 0 = fs
Proof
rw[forwardFD_def,IO_fs_component_equality]
\\ match_mp_tac AFUPDKEY_unchanged
\\ simp[FORALL_PROD]
QED
Theorem forwardFD_numchars:
forwardFD (fs with numchars := ll) fd n = forwardFD fs fd n with numchars := ll
Proof
rw[forwardFD_def]
QED
Theorem liveFS_forwardFD[simp]:
liveFS (forwardFD fs fd n) = liveFS fs
Proof
rw[liveFS_def]
QED
Theorem MAP_FST_forwardFD_infds[simp]:
MAP FST (forwardFD fs fd n).infds = MAP FST fs.infds
Proof
rw[forwardFD_def]
QED
Theorem validFD_forwardFD[simp]:
validFD fd (forwardFD fs fd n)= validFD fd fs
Proof
rw[validFD_def]
QED
Theorem wfFS_forwardFD[simp]:
wfFS (forwardFD fs fd n) = wfFS fs
Proof
rw[wfFS_def,consistentFS_def]
\\ rw[forwardFD_def,AFUPDKEY_ALOOKUP]
\\ rw[EQ_IMP_THM]
\\ res_tac \\ fs[]
\\ FULL_CASE_TAC \\ fs[]
\\ FULL_CASE_TAC \\ fs[]
\\ Cases_on`x` \\ fs[]
QED
Theorem get_file_content_forwardFD[simp]:
!fs fd c pos n.
get_file_content (forwardFD fs fd n) fd =
OPTION_MAP (I ## (+) n) (get_file_content fs fd)
Proof
rw[get_file_content_def,forwardFD_def,AFUPDKEY_ALOOKUP]
\\ CASE_TAC \\ fs[]
\\ pairarg_tac \\ fs[]
\\ pairarg_tac \\ fs[] \\ rw[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[]
QED
Theorem bumpFD_forwardFD:
bumpFD fd fs n = forwardFD fs fd n with numchars := THE (LTL fs.numchars)
Proof
rw[bumpFD_def,forwardFD_def]
QED
Theorem fastForwardFD_forwardFD:
get_file_content fs fd = SOME (content,pos) ∧ pos + n ≤ LENGTH content ⇒
fastForwardFD (forwardFD fs fd n) fd = fastForwardFD fs fd
Proof
rw[fastForwardFD_def,get_file_content_def,forwardFD_def,AFUPDKEY_ALOOKUP]
\\ rw[]
\\ pairarg_tac \\ fs[]
\\ pairarg_tac \\ fs[libTheory.the_def]
\\ fs[IO_fs_component_equality,AFUPDKEY_o]
\\ match_mp_tac AFUPDKEY_eq
\\ simp[MAX_DEF]
QED
Theorem forwardFD_ADELKEY_same:
forwardFD fs fd n with infds updated_by ADELKEY fd = fs with infds updated_by ADELKEY fd
Proof
rw[forwardFD_def,IO_fs_component_equality]
QED
(* lineFD: the next line *)
val lineFD_def = Define`
lineFD fs fd = do
(content, pos) <- get_file_content fs fd;
assert (pos < LENGTH content);
let (l,r) = SPLITP ((=)#"\n") (DROP pos content) in
SOME(l++"\n") od`;
(* linesFD: get all the lines *)
val linesFD_def = Define`
linesFD fs fd =
case get_file_content fs fd of
| NONE => []
| SOME (content,pos) =>
MAP (λx. x ++ "\n")
(splitlines (DROP pos content))`;