-
Notifications
You must be signed in to change notification settings - Fork 86
/
TextIOProofScript.sml
8687 lines (8282 loc) · 313 KB
/
TextIOProofScript.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
(*
Proofs about the code in the TextIO module.
*)
open preamble
ml_translatorTheory ml_translatorLib ml_progLib cfLib basisFunctionsLib
mlstringTheory fsFFITheory fsFFIPropsTheory Word8ProgTheory cfMonadLib
Word8ArrayProofTheory TextIOProgTheory MarshallingProgTheory MarshallingTheory
integerTheory int_arithTheory;
val _ = temp_delsimps ["NORMEQ_CONV", "TAKE_LENGTH_ID_rwt2", "TAKE_LENGTH_ID_rwt2"];
val _ = new_theory"TextIOProof";
val _ = translation_extends "TextIOProg";
val _ = preamble.option_monadsyntax.temp_add_option_monadsyntax();
(* heap predicate for the file-system state *)
Definition IOFS_iobuff_def:
IOFS_iobuff =
SEP_EXISTS v. W8ARRAY iobuff_loc v * cond (LENGTH v >= 2052)
End
Definition IOFS_def:
IOFS fs = IOx (fs_ffi_part) fs * IOFS_iobuff * &wfFS fs
End
(*Used for read_into where the target buffer is specified*)
Definition IOFS_WO_iobuff_def:
IOFS_WO_iobuff fs = IOx (fs_ffi_part) fs * &wfFS fs
End
Theorem UNIQUE_IOFS:
!s. VALID_HEAP s ==> !fs1 fs2 H1 H2. (IOFS fs1 * H1) s /\
(IOFS fs2 * H2) s ==> fs1 = fs2
Proof
rw[IOFS_def,cfHeapsBaseTheory.IOx_def, fs_ffi_part_def,
GSYM STAR_ASSOC,encode_def] >>
IMP_RES_TAC FRAME_UNIQUE_IO >>
fs[IO_fs_component_equality]
QED;
Theorem IOFS_FFI_part_hprop:
FFI_part_hprop (IOFS fs)
Proof
rw [IOFS_def,
cfHeapsBaseTheory.IO_def, cfHeapsBaseTheory.IOx_def,
fs_ffi_part_def, cfMainTheory.FFI_part_hprop_def,
set_sepTheory.SEP_CLAUSES,set_sepTheory.SEP_EXISTS_THM,
cfHeapsBaseTheory.W8ARRAY_def,IOFS_iobuff_def,
cfHeapsBaseTheory.cell_def]
\\ fs[set_sepTheory.one_STAR,STAR_def]
\\ imp_res_tac SPLIT_SUBSET >> fs[SUBSET_DEF]
\\ metis_tac[]
QED;
Theorem IOFS_iobuff_HPROP_INJ[hprop_inj]:
!fs1 fs2. HPROP_INJ (IOFS fs1) (IOFS fs2) (fs2 = fs1)
Proof
rw[HPROP_INJ_def, IOFS_def, GSYM STAR_ASSOC, SEP_CLAUSES, SEP_EXISTS_THM,
HCOND_EXTRACT] >>
fs[IOFS_def,cfHeapsBaseTheory.IOx_def, fs_ffi_part_def] >>
EQ_TAC >> rpt DISCH_TAC >> IMP_RES_TAC FRAME_UNIQUE_IO >> fs[]
QED;
(* "end-user" property *)
(* abstracts away the lazy list and ensure that standard streams are opened on
* their respective standard fds at the right position *)
Definition STDIO_def:
STDIO fs = (SEP_EXISTS ll. IOFS (fs with numchars := ll)) *
&STD_streams fs
End
(* Used by the monadic translator *)
Definition MONAD_IO_def:
MONAD_IO fs = STDIO fs * &hasFreeFD fs
End
Theorem STDIO_numchars:
STDIO (fs with numchars := x) = STDIO fs
Proof
rw[STDIO_def,GSYM STD_streams_numchars]
QED;
Theorem STDIO_bumpFD[simp]:
STDIO (bumpFD fd fs n) = STDIO (forwardFD fs fd n)
Proof
rw[bumpFD_forwardFD,STDIO_numchars]
QED;
Theorem UNIQUE_STDIO:
!s. VALID_HEAP s ==> !fs1 fs2 H1 H2. (STDIO fs1 * H1) s /\
(STDIO fs2 * H2) s ==>
(fs1.infds = fs2.infds /\ fs1.files = fs2.files /\ fs1.maxFD = fs2.maxFD /\ fs1.inode_tbl = fs2.inode_tbl)
Proof
rw[STDIO_def,STD_streams_def,SEP_CLAUSES,SEP_EXISTS_THM,STAR_COMM,STAR_ASSOC,cond_STAR] >>
fs[Once STAR_COMM] >>
imp_res_tac UNIQUE_IOFS >>
cases_on`fs1` >> cases_on`fs2` >> fs[recordtype_IO_fs_seldef_numchars_fupd_def]
QED;
(* weak injection theorem *)
Theorem STDIO_HPROP_INJ[hprop_inj]:
HPROP_INJ (STDIO fs1) (STDIO fs2)
(fs1.infds = fs2.infds /\ fs1.files = fs2.files /\ fs1.maxFD = fs2.maxFD /\ fs1.inode_tbl = fs2.inode_tbl)
Proof
rw[HPROP_INJ_def, GSYM STAR_ASSOC, SEP_CLAUSES, SEP_EXISTS_THM,
HCOND_EXTRACT] >>
EQ_TAC >> rpt DISCH_TAC
>-(mp_tac UNIQUE_STDIO >> disch_then drule >>
strip_tac >>
first_x_assum (qspecl_then [`fs1`, `fs2`] mp_tac) >>
rpt (disch_then drule) >> fs[cond_def] >> rw[] >>
fs[STDIO_def,STD_streams_def,STAR_def,SEP_EXISTS,cond_def] >>
qmatch_assum_rename_tac`IOFS (fs2 with numchars := ll) u1` >>
qmatch_assum_rename_tac`SPLIT u0 (u1, _)` >>
qmatch_assum_rename_tac`SPLIT s (u0, v0)` >>
qexists_tac`u0` >> qexists_tac`v0` >> fs[] >>
qexists_tac`u1` >> fs[PULL_EXISTS] >> qexists_tac`ll` >> fs[] >>
cases_on`fs1` >> cases_on`fs2` >> fs[recordtype_IO_fs_seldef_numchars_fupd_def] >>
metis_tac[]
) >>
fs[STDIO_def,STD_streams_def,STAR_def,SEP_EXISTS,cond_def] >>
qmatch_assum_rename_tac`IOFS (fs1 with numchars := ll) u1` >>
qmatch_assum_rename_tac`SPLIT u0 (u1, _)` >>
qmatch_assum_rename_tac`SPLIT s (u0, v0)` >>
qexists_tac`u0` >> qexists_tac`v0` >> fs[] >>
qexists_tac`u1` >> fs[PULL_EXISTS] >> qexists_tac`ll` >> fs[] >>
cases_on`fs1` >> cases_on`fs2` >> fs[recordtype_IO_fs_seldef_numchars_fupd_def] >>
metis_tac[]
QED;
(* refinement invariant for filenames *)
Definition FILENAME_def:
FILENAME s sv =
(STRING_TYPE s sv ∧
¬MEM (CHR 0) (explode s) ∧
strlen s < 256 * 256)
End
val filename_tac = metis_tac[FILENAME_def,EqualityType_NUM_BOOL,EqualityType_def];
Theorem FILENAME_UNICITY_R[xlet_auto_match]:
!f fv fv'. FILENAME f fv ==> (FILENAME f fv' <=> fv' = fv)
Proof
filename_tac
QED;
Theorem FILENAME_UNICITY_L[xlet_auto_match]:
!f f' fv. FILENAME f fv ==> (FILENAME f' fv <=> f' = f)
Proof
filename_tac
QED;
Theorem FILENAME_STRING_UNICITY_R[xlet_auto_match]:
!f fv fv'. FILENAME f fv ==> (STRING_TYPE f fv' <=> fv' = fv)
Proof
filename_tac
QED;
Theorem FILENAME_STRING_UNICITY_L[xlet_auto_match]:
!f f' fv. FILENAME f fv ==> (STRING_TYPE f' fv <=> f' = f)
Proof
filename_tac
QED;
Theorem STRING_FILENAME_UNICITY_R[xlet_auto_match]:
!f fv fv'. STRING_TYPE f fv ==>
(FILENAME f fv' <=> fv' = fv /\ ¬MEM #"\^@" (explode f) /\ strlen f < 256 * 256)
Proof
filename_tac
QED;
Theorem STRING_FILENAME_UNICITY_L[xlet_auto_match]:
!f f' fv. STRING_TYPE f fv ==>
(FILENAME f' fv <=> f' = f /\ ¬MEM #"\^@" (explode f) /\ strlen f < 256 * 256)
Proof
filename_tac
QED;
(* exception refinement invariant lemmas *)
Theorem BadFileName_UNICITY[xlet_auto_match]:
!v1 v2. BadFileName_exn v1 ==> (BadFileName_exn v2 <=> v2 = v1)
Proof
fs[BadFileName_exn_def]
QED;
Theorem InvalidFD_UNICITY[xlet_auto_match]:
!v1 v2. InvalidFD_exn v1 ==> (InvalidFD_exn v2 <=> v2 = v1)
Proof
fs[InvalidFD_exn_def]
QED;
Theorem EndOfFile_UNICITY[xlet_auto_match]:
!v1 v2. EndOfFile_exn v1 ==> (EndOfFile_exn v2 <=> v2 = v1)
Proof
fs[EndOfFile_exn_def]
QED;
Theorem IllegalArgument_UNICITY[xlet_auto_match]:
!v1 v2. IllegalArgument_exn v1 ==> (IllegalArgument_exn v2 <=> v2 = v1)
Proof
fs[IllegalArgument_exn_def]
QED;
(* convenient functions for standard output/error
* n.b. numchars is ignored *)
Definition stdo_def:
stdo fd name fs out =
(ALOOKUP fs.infds fd = SOME(UStream(strlit name),WriteMode,strlen out) /\
ALOOKUP fs.inode_tbl (UStream(strlit name)) = SOME (explode out))
End
Overload stdout = ``stdo 1 "stdout"``;
Overload stderr = ``stdo 2 "stderr"``;
Theorem stdo_UNICITY_R[xlet_auto_match]:
!fd name fs out out'. stdo fd name fs out ==> (stdo fd name fs out' <=> out = out')
Proof
rw[stdo_def] >> EQ_TAC >> rw[explode_11]
QED
Definition up_stdo_def:
up_stdo fd fs out = fsupdate fs fd 0 (strlen out) (explode out)
End
Overload up_stdout = ``up_stdo 1``;
Overload up_stderr = ``up_stdo 2``;
Theorem stdo_numchars:
stdo fd name (fs with numchars := l) out ⇔ stdo fd name fs out
Proof
rw[stdo_def]
QED
Theorem up_stdo_numchars[simp]:
(up_stdo fd fs x).numchars = fs.numchars
Proof
rw[up_stdo_def,fsupdate_def]
\\ CASE_TAC \\ CASE_TAC \\ rw[]
QED
Theorem fsupdate_files[simp]:
(fsupdate fs fd k pos c).files = fs.files
Proof
fs[fsupdate_def] >> NTAC 2 CASE_TAC >>fs[]
QED
Theorem up_stdo_files[simp]:
(up_stdo fd fs x).files = fs.files
Proof
fs[up_stdo_def,fsupdate_def] >> NTAC 2 CASE_TAC >>fs[]
QED
Theorem up_stdo_maxFD[simp]:
(up_stdo fd fs x).maxFD = fs.maxFD
Proof
rw[up_stdo_def,fsupdate_def]
\\ CASE_TAC \\ CASE_TAC \\ rw[]
QED
Theorem up_stdo_with_numchars:
up_stdo fd (fs with numchars := ns) x =
up_stdo fd fs x with numchars := ns
Proof
rw[up_stdo_def,fsupdate_numchars]
QED
Definition add_stdo_def:
add_stdo fd nm fs out = up_stdo fd fs ((@init. stdo fd nm fs init) ^ out)
End
Overload add_stdout = ``add_stdo 1 "stdout"``;
Overload add_stderr = ``add_stdo 2 "stderr"``;
Theorem stdo_add_stdo:
stdo fd nm fs init ⇒ stdo fd nm (add_stdo fd nm fs out) (strcat init out)
Proof
rw[add_stdo_def]
\\ SELECT_ELIM_TAC \\ rw[] >- metis_tac[]
\\ imp_res_tac stdo_UNICITY_R \\ rveq
\\ fs[up_stdo_def,stdo_def,fsupdate_def,AFUPDKEY_ALOOKUP]
QED
Theorem up_stdo_unchanged:
!fs out. stdo fd nm fs out ==> up_stdo fd fs out = fs
Proof
fs[up_stdo_def,stdo_def,fsupdate_unchanged,get_file_content_def,PULL_EXISTS]
QED
Theorem stdo_up_stdo:
!fs out out'. stdo fd nm fs out ==> stdo fd nm (up_stdo fd fs out') out'
Proof
rw[up_stdo_def,stdo_def,fsupdate_def,AFUPDKEY_ALOOKUP,PULL_EXISTS]
\\ rw[]
QED
Theorem add_stdo_nil:
stdo fd nm fs out ⇒ add_stdo fd nm fs (strlit "") = fs
Proof
rw[add_stdo_def]
\\ SELECT_ELIM_TAC
\\ metis_tac[up_stdo_unchanged]
QED
Theorem add_stdo_o:
stdo fd nm fs out ⇒
add_stdo fd nm (add_stdo fd nm fs x1) x2 = add_stdo fd nm fs (x1 ^ x2)
Proof
rw[add_stdo_def]
\\ SELECT_ELIM_TAC \\ rw[] >- metis_tac[]
\\ SELECT_ELIM_TAC \\ rw[] >- metis_tac[stdo_up_stdo]
\\ imp_res_tac stdo_UNICITY_R \\ rveq
\\ rename1`stdo _ _ (up_stdo _ _ _) l`
\\ `l = out ^ x1` by metis_tac[stdo_UNICITY_R,stdo_up_stdo]
\\ rveq \\ fs[up_stdo_def]
QED
Theorem add_stdo_numchars[simp]:
(add_stdo fd nm fs x).numchars = fs.numchars
Proof
rw[add_stdo_def]
QED
Theorem add_stdo_maxFD[simp]:
(add_stdo fd nm fs x).maxFD = fs.maxFD
Proof
rw[add_stdo_def]
QED
Theorem add_stdo_with_numchars:
add_stdo fd nm (fs with numchars := ns) x =
add_stdo fd nm fs x with numchars := ns
Proof
rw[add_stdo_def,stdo_numchars,up_stdo_with_numchars]
QED
Theorem up_stdo_MAP_FST_infds[simp]:
MAP FST (up_stdo fd fs out).infds = MAP FST fs.infds
Proof
rw[up_stdo_def]
QED
Theorem add_stdo_MAP_FST_infds[simp]:
MAP FST (add_stdo fd nm fs out).infds = MAP FST fs.infds
Proof
rw[add_stdo_def]
QED
Theorem up_stdo_MAP_FST_files[simp]:
MAP FST (up_stdo fd fs out).files = MAP FST fs.files
Proof
rw[up_stdo_def]
QED
Theorem up_stdo_MAP_FST_inode_tbl[simp]:
MAP FST (up_stdo fd fs out).inode_tbl = MAP FST fs.inode_tbl
Proof
rw[up_stdo_def]
QED
Theorem add_stdo_MAP_FST_inode_tbl[simp]:
MAP FST (add_stdo fd nm fs out).inode_tbl = MAP FST fs.inode_tbl
Proof
rw[add_stdo_def]
QED
Theorem inFS_fname_add_stdo[simp]:
inFS_fname (add_stdo fd nm fs out) = inFS_fname fs
Proof
rw[inFS_fname_def,FUN_EQ_THM] >> EVAL_TAC >> EVERY_CASE_TAC >> rw[]
QED
Theorem STD_streams_stdout:
STD_streams fs ⇒ ∃out. stdout fs out
Proof
rw[STD_streams_def,stdo_def] \\ rw[] \\ metis_tac[explode_implode,strlen_implode]
QED
Theorem STD_streams_stderr:
STD_streams fs ⇒ ∃out. stderr fs out
Proof
rw[STD_streams_def,stdo_def] \\ rw[] \\ metis_tac[explode_implode,strlen_implode]
QED
Theorem STD_streams_add_stdout:
STD_streams fs ⇒ STD_streams (add_stdout fs out)
Proof
rw[]
\\ imp_res_tac STD_streams_stdout
\\ rw[add_stdo_def]
\\ SELECT_ELIM_TAC
\\ rw[] >- metis_tac[]
\\ rw[up_stdo_def]
\\ match_mp_tac STD_streams_fsupdate \\ rw[]
QED
Theorem STD_streams_add_stderr:
STD_streams fs ⇒ STD_streams (add_stderr fs out)
Proof
rw[]
\\ imp_res_tac STD_streams_stderr
\\ rw[add_stdo_def]
\\ SELECT_ELIM_TAC
\\ rw[] >- metis_tac[]
\\ rw[up_stdo_def]
\\ match_mp_tac STD_streams_fsupdate \\ rw[]
QED
Theorem validFD_up_stdo[simp]:
validFD fd (up_stdo fd' fs out) ⇔ validFD fd fs
Proof
rw[up_stdo_def]
QED
Theorem validFD_add_stdo[simp]:
validFD fd (add_stdo fd' nm fs out) ⇔ validFD fd fs
Proof
rw[add_stdo_def]
QED
Theorem validFileFD_up_stdo[simp]:
validFileFD fd (up_stdo fd' fs out).infds ⇔ validFileFD fd fs.infds
Proof
rw[up_stdo_def]
QED
Theorem validFileFD_add_stdo[simp]:
validFileFD fd (add_stdo fd' nm fs out).infds ⇔ validFileFD fd fs.infds
Proof
rw[add_stdo_def]
QED
Theorem up_stdo_ADELKEY:
fd ≠ fd' ⇒
up_stdo fd (fs with infds updated_by ADELKEY fd') out =
up_stdo fd fs out with infds updated_by ADELKEY fd'
Proof
rw[up_stdo_def,fsupdate_ADELKEY]
QED
Theorem stdo_ADELKEY:
fd ≠ fd' ⇒
stdo fd nm (fs with infds updated_by ADELKEY fd') = stdo fd nm fs
Proof
rw[stdo_def,FUN_EQ_THM,ALOOKUP_ADELKEY]
QED
Theorem add_stdo_ADELKEY:
fd ≠ fd' ⇒
add_stdo fd nm (fs with infds updated_by ADELKEY fd') out =
add_stdo fd nm fs out with infds updated_by ADELKEY fd'
Proof
rw[add_stdo_def,up_stdo_ADELKEY,stdo_ADELKEY]
QED
Theorem get_file_content_add_stdout:
STD_streams fs ∧ fd ≠ 1 ⇒
get_file_content (add_stdout fs out) fd = get_file_content fs fd
Proof
rw[get_file_content_def,add_stdo_def,up_stdo_def,fsupdate_def]
\\ CASE_TAC \\ CASE_TAC \\ simp[AFUPDKEY_ALOOKUP]
\\ TOP_CASE_TAC \\ fs[]
\\ pairarg_tac \\ fs[]
\\ CASE_TAC
>- metis_tac[STD_streams_def,SOME_11,PAIR,FST,SND]
\\ CASE_TAC
QED
Theorem get_mode_add_stdo[simp]:
get_mode (add_stdo fd nm fs x) fd' = get_mode fs fd'
Proof
rw[get_mode_def,add_stdo_def, up_stdo_def, fsupdate_def]
\\ TOP_CASE_TAC \\ rw[]
\\ TOP_CASE_TAC \\ rw[]
\\ simp[AFUPDKEY_ALOOKUP]
\\ TOP_CASE_TAC \\ rw[]
QED
Theorem get_mode_bumpFD[simp]:
get_mode (bumpFD fd fs n) fd' = get_mode fs fd'
Proof
rw[get_mode_def,bumpFD_def,AFUPDKEY_ALOOKUP]
\\ TOP_CASE_TAC \\ rw[]
QED
Theorem linesFD_add_stdout:
STD_streams fs ∧ fd ≠ 1 ⇒
linesFD (add_stdout fs out) fd = linesFD fs fd
Proof
rw[linesFD_def,get_file_content_add_stdout]
QED
Theorem get_file_content_add_stderr:
STD_streams fs ∧ fd ≠ 2 ⇒
get_file_content (add_stderr fs err) fd = get_file_content fs fd
Proof
rw[get_file_content_def,add_stdo_def,up_stdo_def,fsupdate_def]
\\ CASE_TAC \\ CASE_TAC \\ simp[AFUPDKEY_ALOOKUP]
\\ TOP_CASE_TAC \\ fs[]
\\ pairarg_tac \\ fs[]
\\ CASE_TAC
>- metis_tac[STD_streams_def,SOME_11,PAIR,FST,SND]
\\ CASE_TAC
QED
Theorem linesFD_add_stderr:
STD_streams fs ∧ fd ≠ 2 ⇒
linesFD (add_stderr fs err) fd = linesFD fs fd
Proof
rw[linesFD_def,get_file_content_add_stderr]
QED
Theorem up_stdo_forwardFD:
fd ≠ fd' ⇒ up_stdo fd' (forwardFD fs fd n) out = forwardFD (up_stdo fd' fs out) fd n
Proof
rw[forwardFD_def,up_stdo_def,fsupdate_def,AFUPDKEY_ALOOKUP]
\\ CASE_TAC \\ CASE_TAC \\ rw[]
\\ rpt(AP_TERM_TAC ORELSE AP_THM_TAC)
\\ match_mp_tac AFUPDKEY_comm \\ rw[]
QED
Theorem up_stdout_fastForwardFD:
STD_streams fs ⇒
up_stdout (fastForwardFD fs fd) out = fastForwardFD (up_stdout fs out) fd
Proof
rw[fastForwardFD_def,up_stdo_def]
\\ Cases_on`ALOOKUP fs.infds fd` >- (
fs[miscTheory.the_def,fsupdate_def]
\\ CASE_TAC \\ fs[miscTheory.the_def]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP] )
\\ fs[] \\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` >- (
fs[miscTheory.the_def,fsupdate_def]
\\ CASE_TAC \\ fs[miscTheory.the_def]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP]
\\ rw[miscTheory.the_def] )
\\ fs[miscTheory.the_def]
\\ fs[fsupdate_def,miscTheory.the_def,AFUPDKEY_ALOOKUP]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP]
>- ( rw[AFUPDKEY_o,o_DEF,PAIR_MAP] )
\\ CASE_TAC \\ fs[miscTheory.the_def]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP]
\\ rw[miscTheory.the_def,AFUPDKEY_comm]
\\ metis_tac[STD_streams_def,SOME_11,PAIR,FST,SND]
QED
Theorem up_stderr_fastForwardFD:
STD_streams fs ⇒
up_stderr (fastForwardFD fs fd) out = fastForwardFD (up_stderr fs out) fd
Proof
rw[fastForwardFD_def,up_stdo_def]
\\ Cases_on`ALOOKUP fs.infds fd` >- (
fs[miscTheory.the_def,fsupdate_def]
\\ CASE_TAC \\ fs[miscTheory.the_def]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP] )
\\ fs[] \\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` >- (
fs[miscTheory.the_def,fsupdate_def]
\\ CASE_TAC \\ fs[miscTheory.the_def]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP]
\\ rw[miscTheory.the_def] )
\\ fs[miscTheory.the_def]
\\ fs[fsupdate_def,miscTheory.the_def,AFUPDKEY_ALOOKUP]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP]
>- ( rw[AFUPDKEY_o,o_DEF,PAIR_MAP] )
\\ CASE_TAC \\ fs[miscTheory.the_def]
\\ CASE_TAC \\ fs[miscTheory.the_def,AFUPDKEY_ALOOKUP]
\\ rw[miscTheory.the_def,AFUPDKEY_comm]
\\ metis_tac[STD_streams_def,SOME_11,PAIR,FST,SND]
QED
Theorem stdo_forwardFD:
fd ≠ fd' ⇒ (stdo fd' nm (forwardFD fs fd n) out ⇔ stdo fd' nm fs out)
Proof
rw[stdo_def,forwardFD_def,AFUPDKEY_ALOOKUP]
\\ CASE_TAC
QED
Theorem stdo_fastForwardFD:
fd ≠ fd' ⇒ (stdo fd' nm (fastForwardFD fs fd) out ⇔ stdo fd' nm fs out)
Proof
rw[stdo_def,fastForwardFD_def,AFUPDKEY_ALOOKUP]
\\ Cases_on`ALOOKUP fs.infds fd` \\ fs[miscTheory.the_def]
\\ pairarg_tac \\ fs[]
\\ Cases_on`ALOOKUP fs.inode_tbl ino` \\ fs[miscTheory.the_def]
\\ fs[AFUPDKEY_ALOOKUP] \\ rw[]
\\ CASE_TAC
QED
Theorem add_stdo_forwardFD:
fd ≠ fd' ⇒ add_stdo fd' nm (forwardFD fs fd n) out = forwardFD (add_stdo fd' nm fs out) fd n
Proof
rw[add_stdo_def,stdo_forwardFD,up_stdo_forwardFD]
QED
Theorem add_stdout_lineForwardFD:
STD_streams fs ∧ fd ≠ 1 ⇒
add_stdout (lineForwardFD fs fd) out = lineForwardFD (add_stdout fs out) fd
Proof
rw[lineForwardFD_def,get_file_content_add_stdout]
\\ CASE_TAC \\ CASE_TAC
\\ rw[] \\ pairarg_tac \\ fs[add_stdo_forwardFD]
QED
Theorem add_stdout_fastForwardFD:
STD_streams fs ∧ fd ≠ 1 ⇒
add_stdout (fastForwardFD fs fd) out = fastForwardFD (add_stdout fs out) fd
Proof
rw[add_stdo_def,up_stdout_fastForwardFD,stdo_fastForwardFD]
QED
Theorem add_stderr_lineForwardFD:
STD_streams fs ∧ fd ≠ 2 ⇒
add_stderr (lineForwardFD fs fd) out = lineForwardFD (add_stderr fs out) fd
Proof
rw[lineForwardFD_def,get_file_content_add_stderr]
\\ CASE_TAC \\ CASE_TAC
\\ rw[] \\ pairarg_tac \\ fs[add_stdo_forwardFD]
QED
Theorem add_stderr_fastForwardFD:
STD_streams fs ∧ fd ≠ 2 ⇒
add_stderr (fastForwardFD fs fd) out = fastForwardFD (add_stderr fs out) fd
Proof
rw[add_stdo_def,up_stderr_fastForwardFD,stdo_fastForwardFD]
QED
Theorem FILTER_File_add_stdo:
stdo fd nm fs init ⇒
FILTER (isFile o FST) (add_stdo fd nm fs out).inode_tbl = FILTER (isFile o FST) fs.inode_tbl
Proof
rw[add_stdo_def,up_stdo_def,fsupdate_def]
\\ CASE_TAC \\ CASE_TAC \\ fs[]
\\ match_mp_tac FILTER_EL_EQ \\ simp[]
\\ qmatch_assum_rename_tac`_ = SOME (k,_)`
\\ qx_gen_tac`n`
\\ simp[GSYM AND_IMP_INTRO] \\ strip_tac
\\ reverse(Cases_on`FST (EL n fs.inode_tbl) = k`)
>- simp[EL_AFUPDKEY_unchanged]
\\ simp[FST_EL_AFUPDKEY,GSYM AND_IMP_INTRO]
\\ fs[stdo_def]
QED
Theorem FILTER_File_add_stdout:
STD_streams fs ⇒
FILTER (isFile o FST) (add_stdout fs out).inode_tbl = FILTER (isFile o FST) fs.inode_tbl
Proof
metis_tac[STD_streams_stdout,FILTER_File_add_stdo]
QED
Theorem FILTER_File_add_stderr:
STD_streams fs ⇒
FILTER (isFile o FST) (add_stderr fs out).inode_tbl = FILTER (isFile o FST) fs.inode_tbl
Proof
metis_tac[STD_streams_stderr,FILTER_File_add_stdo]
QED
(* Note: more general versions of the following 4 theorems
can be proved for stdo, but requires
assumption to ensure that the file descriptors do not overlap *)
Theorem stdout_add_stderr:
STD_streams fs ∧ stdout fs out ⇒
stdout (add_stderr fs err) out
Proof
rw[stdo_def]>>
simp[add_stdo_def,up_stdo_def,fsupdate_def]>>
every_case_tac>>simp[AFUPDKEY_ALOOKUP]>>
fs[STD_streams_def]>>
rw[]>>simp[]>>
Cases_on`r`>>
rpt(first_x_assum(qspecl_then [`2`,`q`,`r'`] assume_tac))>>fs[]
QED
Theorem stderr_add_stdout:
STD_streams fs ∧ stderr fs err ⇒
stderr (add_stdout fs out) err
Proof
rw[stdo_def]>>
simp[add_stdo_def,up_stdo_def,fsupdate_def]>>
every_case_tac>>simp[AFUPDKEY_ALOOKUP]>>
fs[STD_streams_def]>>
rw[]>>simp[]>>
Cases_on`r`>>
rpt(first_x_assum(qspecl_then [`1`,`q`,`r'`] assume_tac))>>fs[]
QED
Theorem add_stdout_inj:
add_stdout fs1 out1 = add_stdout fs2 out2 ∧
stdout fs1 out ∧ stdout fs2 out ⇒
out1 = out2
Proof
rw[]>>
`stdout (add_stdout fs1 out1) (out ^ out1)` by
metis_tac[stdo_add_stdo]>>
`stdout (add_stdout fs2 out2) (out ^ out2)` by
metis_tac[stdo_add_stdo]>>
fs[fsFFITheory.IO_fs_component_equality]>>
rw[]>>fs[stdo_def]>>
gs[]
QED
Theorem add_stderr_inj:
add_stderr fs1 err1 = add_stderr fs2 err2 ∧
stderr fs1 err ∧ stderr fs2 err ⇒
err1 = err2
Proof
rw[]>>
`stderr (add_stderr fs1 err1) (err ^ err1)` by
metis_tac[stdo_add_stdo]>>
`stderr (add_stderr fs2 err2) (err ^ err2)` by
metis_tac[stdo_add_stdo]>>
fs[fsFFITheory.IO_fs_component_equality]>>
rw[]>>fs[stdo_def]>>
gs[]
QED
Definition stdin_def:
stdin fs inp pos = (ALOOKUP fs.infds 0 = SOME(UStream(strlit"stdin"),ReadMode,pos) /\
ALOOKUP fs.inode_tbl (UStream(strlit"stdin"))= SOME inp)
End
Definition up_stdin_def:
up_stdin inp pos fs = fsupdate fs 0 0 pos inp
End
Definition get_stdin_def:
get_stdin fs = let (inp,pos) = @(inp,pos). stdin fs inp pos in DROP pos inp
End
Theorem stdin_11:
stdin fs i1 p1 ∧ stdin fs i2 p2 ⇒ i1 = i2 ∧ p1 = p2
Proof
rw[stdin_def] \\ fs[]
QED
Theorem stdin_get_file_content:
stdin fs inp pos ⇒ get_file_content fs 0 = SOME (inp,pos)
Proof
rw[stdin_def,fsFFITheory.get_file_content_def]
QED
Theorem stdin_forwardFD:
stdin fs inp pos ⇒
stdin (forwardFD fs fd n) inp (if fd = 0 then pos+n else pos)
Proof
rw[stdin_def,forwardFD_def]
\\ simp[AFUPDKEY_ALOOKUP]
QED
Theorem stdin_get_stdin:
stdin fs inp pos ⇒ get_stdin fs = DROP pos inp
Proof
rw[get_stdin_def]
\\ SELECT_ELIM_TAC
\\ rw[EXISTS_PROD,FORALL_PROD]
>- metis_tac[]
\\ pairarg_tac \\ fs[]
\\ imp_res_tac stdin_11 \\ fs[]
QED
Theorem get_stdin_forwardFD:
stdin fs inp pos ⇒
get_stdin (forwardFD fs fd n) =
if fd = 0 then
DROP n (get_stdin fs)
else get_stdin fs
Proof
strip_tac
\\ imp_res_tac stdin_get_stdin
\\ imp_res_tac stdin_forwardFD
\\ first_x_assum(qspecl_then[`n`,`fd`]mp_tac)
\\ strip_tac
\\ simp[DROP_DROP_T]
\\ imp_res_tac stdin_get_stdin
\\ rw[]
QED
Theorem linesFD_splitlines_get_stdin:
stdin fs inp pos ⇒
MAP (λl. l ++ "\n") (splitlines (get_stdin fs)) = linesFD fs 0
Proof
rw[linesFD_def]
\\ imp_res_tac stdin_get_stdin
\\ fs[stdin_def,get_file_content_def]
QED
(* file descriptors are encoded on 8 bytes *)
Definition FD_def:
FD fd fdv = (STRING_TYPE (strlit(MAP (CHR ∘ w2n) (n2w8 fd))) fdv ∧ fd < 256**8)
End
Definition INSTREAM_def:
INSTREAM fd fdv <=>
INSTREAM_TYPE (Instream (strlit(MAP (CHR ∘ w2n) (n2w8 fd)))) fdv ∧
fd < 256**8
End
Definition OUTSTREAM_def:
OUTSTREAM fd fdv <=>
OUTSTREAM_TYPE (Outstream (strlit(MAP (CHR ∘ w2n) (n2w8 fd)))) fdv ∧
fd < 256**8
End
Theorem INSTREAM_stdin:
INSTREAM 0 stdin_v
Proof
fs[INSTREAM_def,MarshallingTheory.n2w8_def,stdin_v_thm,GSYM stdIn_def]
QED
Theorem OUTSTREAM_stdout:
OUTSTREAM 1 stdout_v
Proof
fs[OUTSTREAM_def,MarshallingTheory.n2w8_def,stdout_v_thm,GSYM stdOut_def]
QED
Theorem OUTSTREAM_stderr:
OUTSTREAM 2 stderr_v
Proof
fs[OUTSTREAM_def,MarshallingTheory.n2w8_def,stderr_v_thm,GSYM stdErr_def]
QED
Definition REF_NUM_def:
REF_NUM loc n =
SEP_EXISTS v. REF loc v * & (NUM n v)
End
Definition instream_buffered_inv_def:
instream_buffered_inv r w bcontent bactive =
(1028 <= LENGTH bcontent /\
LENGTH bcontent < 256**2 /\
4 <= w /\
4 <= r /\
r <= LENGTH bcontent /\
w <= LENGTH bcontent /\
r <= w /\
LENGTH bactive = w - r /\
LENGTH bactive < LENGTH bcontent /\
bactive = TAKE (w-r) (DROP r bcontent))
End
(*(bactive = [] <=> r = w))*)
Theorem instream_buffered_inv_alt:
instream_buffered_inv r w bcontent bactive =
∃old unused.
bcontent = old ++ bactive ++ unused ∧
1028 <= LENGTH bcontent /\
LENGTH bcontent < 256**2 /\
4 <= LENGTH old /\
LENGTH (old ++ bactive) = w /\
LENGTH old = r
Proof
reverse eq_tac \\ gvs [instream_buffered_inv_def]
>-
(rw [] \\ gvs []
\\ once_rewrite_tac [GSYM APPEND_ASSOC]
\\ rewrite_tac [DROP_LENGTH_APPEND,TAKE_LENGTH_APPEND])
\\ strip_tac
\\ qpat_x_assum ‘r ≤ LENGTH bcontent’ assume_tac
\\ drule LESS_EQ_LENGTH \\ strip_tac \\ gvs []
\\ qexists_tac ‘ys1’ \\ gvs [DROP_LENGTH_APPEND]
\\ pop_assum mp_tac
\\ rewrite_tac [LESS_EQ_EXISTS] \\ strip_tac \\ gvs []
\\ ‘p ≤ LENGTH ys2’ by gvs []
\\ drule LESS_EQ_LENGTH \\ strip_tac \\ gvs [TAKE_LENGTH_APPEND]
QED
Overload TypeStamp_InstreamBuffered = “TypeStamp "InstreamBuffered" 35”;
Definition INSTREAM_BUFFERED_def:
INSTREAM_BUFFERED bactive is =
SEP_EXISTS rr r wr w buff bcontent fd fdv.
& (is = (Conv instreambuffered_con_stamp [fdv; rr; wr; buff]) /\
INSTREAM fd fdv /\
instream_buffered_inv r w bcontent bactive) *
REF_NUM rr r *
REF_NUM wr w *
W8ARRAY buff bcontent
End
Definition INSTREAM_BUFFERED_FD_def:
INSTREAM_BUFFERED_FD bactive fd is =
SEP_EXISTS rr r wr w buff bcontent fdv.
& (is = (Conv instreambuffered_con_stamp [fdv; rr; wr; buff]) /\
INSTREAM fd fdv /\
instream_buffered_inv r w bcontent bactive) *
REF_NUM rr r *
REF_NUM wr w *
W8ARRAY buff bcontent
End
Definition INSTREAM_BUFFERED_BL_FD_def:
INSTREAM_BUFFERED_BL_FD bcontent bactive fd is =
SEP_EXISTS rr r wr w buff fdv.
& (is = (Conv instreambuffered_con_stamp [fdv; rr; wr; buff]) /\
INSTREAM fd fdv /\
instream_buffered_inv r w bcontent bactive) *
REF_NUM rr r *
REF_NUM wr w *
W8ARRAY buff bcontent
End
Definition INSTREAM_BUFFERED_BL_FD_RW_def:
INSTREAM_BUFFERED_BL_FD_RW bcontent bactive fd r w is =
SEP_EXISTS rr wr buff fdv.
& (is = (Conv instreambuffered_con_stamp [fdv; rr; wr; buff]) /\
INSTREAM fd fdv /\
instream_buffered_inv r w bcontent bactive) *
REF_NUM rr r *
REF_NUM wr w *
W8ARRAY buff bcontent
End
(* -- *)
Theorem openIn_spec:
∀s sv fs.
FILENAME s sv ∧
hasFreeFD fs ⇒
app (p:'ffi ffi_proj) TextIO_openIn_v [sv]
(IOFS fs)
(POSTve
(\fdv. &(INSTREAM (nextFD fs) fdv ∧
validFD (nextFD fs) (openFileFS s fs ReadMode 0) ∧
inFS_fname fs s) *
IOFS (openFileFS s fs ReadMode 0))
(\e. &(BadFileName_exn e ∧ ~inFS_fname fs s) * IOFS fs))
Proof
rw [] >> qpat_abbrev_tac `Q = POSTve _ _` >>
simp [IOFS_def, fs_ffi_part_def, IOx_def, IO_def] >>
xpull >> qunabbrev_tac `Q` >>
rpt strip_tac >>
xcf_with_def TextIO_openIn_v_def >>
fs[FILENAME_def, strlen_def, IOFS_def, IOFS_iobuff_def] >>
REVERSE (Cases_on`consistentFS fs`) >-(xpull >> fs[wfFS_def]) >>
xpull >> rename [`W8ARRAY _ fnm0`] >>
qmatch_goalsub_abbrev_tac`catfs fs * _` >>
rpt(xlet_auto >- xsimpl) >>
qmatch_goalsub_abbrev_tac`W8ARRAY _ fd0` >>
qmatch_goalsub_rename_tac`W8ARRAY loc fd0` >>
qmatch_goalsub_abbrev_tac`catfs fs' * _` >>
Cases_on `inFS_fname fs s`
>- (xlet `POSTv u2.
&(UNIT_TYPE () u2 /\ nextFD fs < fs.maxFD /\
validFD (nextFD fs) (openFileFS s fs ReadMode 0)) *
W8ARRAY loc (0w :: n2w8(nextFD fs)) *
W8ARRAY iobuff_loc fnm0 *
catfs fs'`
>- (simp[Abbr`catfs`,Abbr`fs'`] >>
xffi >> xsimpl >>
qexists_tac`(MAP (n2w o ORD) (explode s) ++ [0w])` >>
fs[strcat_thm,implode_def] >>
simp[fsFFITheory.fs_ffi_part_def,IOx_def,IO_def] >>
qmatch_goalsub_abbrev_tac `FFI_part st f ns` >>
CONV_TAC(RESORT_EXISTS_CONV List.rev) >>
map_every qexists_tac[`events`,`ns`,`f`,`st`]
>> xsimpl >>
simp[Abbr`f`,Abbr`st`,Abbr`ns`, mk_ffi_next_def,
ffi_open_in_def, (* decode_encode_FS, *) Abbr`fd0`,
getNullTermStr_add_null, MEM_MAP, ORD_BOUND, ORD_eq_0,
dimword_8, MAP_MAP_o, o_DEF, char_BIJ,str_def,strcat_thm,
LENGTH_explode,REPLICATE_compute,LUPDATE_compute,explode_implode] >>
imp_res_tac inFS_fname_ALOOKUP_EXISTS >>
imp_res_tac nextFD_ltX >>
csimp[openFileFS_def, openFile_def, validFD_def] >>
fs[STRING_TYPE_def] \\ xsimpl >>
qpat_abbrev_tac `new_events = events ++ _` >>
qexists_tac `new_events` >> xsimpl) >>
xlet_auto >- xsimpl >>
xlet_auto >- xsimpl >>
xlet_auto >- (xsimpl >> imp_res_tac WORD_UNICITY_R >> fs[])
>> xif >>
instantiate >>
xlet_auto >- (xsimpl \\ fs [LENGTH_n2w8]) >>
reverse xcon >- xsimpl >>
simp[INSTREAM_def] >> xsimpl >> fs [INSTREAM_TYPE_def] >>
fs[EL_LUPDATE,Abbr`fd0`,LENGTH_explode,LENGTH_n2w8,TAKE_LENGTH_ID_rwt] >>
imp_res_tac nextFD_ltX >>
fs[wfFS_openFile,Abbr`fs'`,liveFS_openFileFS]) >>
xlet `POSTv u2.
&UNIT_TYPE () u2 * catfs fs * W8ARRAY iobuff_loc fnm0 *
W8ARRAY loc (LUPDATE 1w 0 fd0)`
>- (simp[Abbr`catfs`,Abbr`fs'`] >> xffi >> xsimpl >>
simp[fsFFITheory.fs_ffi_part_def,IOx_def,IO_def] >>
qmatch_goalsub_abbrev_tac `FFI_part st f ns` >>
CONV_TAC(RESORT_EXISTS_CONV List.rev) >>
map_every qexists_tac[`events`,`ns`,`f`,`st`] >> xsimpl >>
qexists_tac`(MAP (n2w o ORD) (explode s) ++ [0w])` >>
fs[strcat_thm,implode_def] >>
simp[Abbr`f`,Abbr`st`,Abbr`ns`, mk_ffi_next_def,
ffi_open_in_def, (* decode_encode_FS, *) Abbr`fd0`,
getNullTermStr_add_null, MEM_MAP, ORD_BOUND, ORD_eq_0,
dimword_8, MAP_MAP_o, o_DEF, char_BIJ,str_def,strcat_thm,
implode_explode, LENGTH_explode] >>
fs[not_inFS_fname_openFile,STRING_TYPE_def] \\ xsimpl >>
qpat_abbrev_tac `new_events = events ++ _` >>
qexists_tac `new_events` >> xsimpl) >>
xlet_auto >-(xsimpl) >> fs[] >>
xlet_auto >- xsimpl >>