forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterProgScript.sml
1488 lines (1374 loc) · 56.3 KB
/
filterProgScript.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
(*
Filter case study from CASE.
*)
open preamble basis MapProgTheory ml_translatorLib ml_progLib basisFunctionsLib ml_translatorTheory
charsetTheory regexpTheory regexp_parserTheory regexp_compilerTheory cfTacticsBaseLib
cfDivTheory cfDivLib;
val _ = temp_delsimps ["NORMEQ_CONV"]
val _ = new_theory "filterProg";
(*---------------------------------------------------------------------------*)
(* The regexp wrt. which we're filtering *)
(* In the real implementation it's read from the cmakeConstants SML module *)
(* which is generated by the build system; here we hardcode a regex. *)
(*---------------------------------------------------------------------------*)
val the_regexp = "through\^@";
val _ = translation_extends"MapProg";
val regexp_compilation_results as {certificate, aux, ...}
= regexpLib.gen_dfa regexpLib.HOL (Regexp_Type.fromString the_regexp);
val matcher_certificate = save_thm
("matcher_certificate",
certificate
|> valOf
|> CONV_RULE(QUANT_CONV(LHS_CONV (REWRITE_CONV [MAP])))
);
(*---------------------------------------------------------------------------*)
(* Define a named matcher function *)
(*---------------------------------------------------------------------------*)
val matcher_def =
Define `matcher ^(matcher_certificate |> concl |> dest_forall |> fst) =
^(matcher_certificate |> concl |> dest_forall |> snd |> lhs)`
val match_string_def = Define `match_string s = matcher(explode s)`
val language_def =
Define `language =
^(matcher_certificate |> concl |> dest_forall |> snd |> rhs |> rator)`
val match_string_eq = Q.prove(`match_string = language o explode`,
`!s. match_string s = (language o explode) s` suffices_by metis_tac[]
>> rw[match_string_def,language_def,matcher_def,matcher_certificate]);
(*---------------------------------------------------------------------------*)
(* Translator setup boilerplate *)
(*---------------------------------------------------------------------------*)
fun def_of_const tm = let
val res = dest_thy_const tm handle HOL_ERR _ =>
failwith ("Unable to translate: " ^ term_to_string tm)
val name = (#Name res)
fun def_from_thy thy name =
DB.fetch thy (name ^ "_def") handle HOL_ERR _ =>
DB.fetch thy (name ^ "_DEF") handle HOL_ERR _ =>
DB.fetch thy (name ^ "_thm") handle HOL_ERR _ =>
DB.fetch thy name
val def = def_from_thy (#Thy res) name handle HOL_ERR _ =>
failwith ("Unable to find definition of " ^ name)
in def end
val _ = find_def_for_const := def_of_const;
(* TODO: translate balanced_map module separately? *)
val _ = ml_translatorLib.pick_name :=
let val default = !ml_translatorLib.pick_name in
fn c =>
if same_const c ``balanced_map$member`` then "balanced_map_member" else
if same_const c ``balanced_map$empty`` then "balanced_map_empty" else
default c
end
val spec64 = INST_TYPE[alpha|->``:64``]
val _ = translate matcher_def
val mem_tolist = Q.prove(`MEM (toList l) (MAP toList ll) = MEM l ll`,
Induct_on `ll` >> fs[]);
val length_tolist_cancel = Q.prove(
`!n. n < LENGTH l ==> LENGTH (EL n (MAP mlvector$toList l)) = length (EL n l)`,
Induct_on `l`
>> fs[]
>> rpt strip_tac
>> Cases_on `n`
>> fs[mlvectorTheory.length_toList]);
val EL_map_toList = Q.prove(`!n. n < LENGTH l ==> EL n' (EL n (MAP toList l)) = sub (EL n l) n'`,
Induct_on `l`
>> fs[]
>> rpt strip_tac
>> Cases_on `n`
>> fs[mlvectorTheory.EL_toList]);
val exec_dfa_side_imp = Q.prove(
`!finals table n s.
good_vec (MAP toList (toList table)) (toList finals)
/\ EVERY (λc. MEM (ORD c) ALPHABET) (EXPLODE s)
/\ n < length finals
==> exec_dfa_side finals table n s`,
Induct_on `s`
>- fs[fetch "-" "exec_dfa_side_def"]
>> PURE_ONCE_REWRITE_TAC [fetch "-" "exec_dfa_side_def"]
>> fs[good_vec_def,mlvectorTheory.length_toList]
>> rpt GEN_TAC
>> Induct_on `table`
>> rpt strip_tac
>> fs[sub_def,length_def,mlvectorTheory.toList_thm]
>> `MEM (toList (EL n l)) (MAP toList l)`
by fs[EL_MEM,mem_tolist,mlvectorTheory.toList_thm]
>- metis_tac[mlvectorTheory.length_toList]
>> first_x_assum(MATCH_MP_TAC o Q.SPECL [`finals`,`Vector l`, `x1`])
>> rpt strip_tac
>> fs[mlvectorTheory.toList_thm, length_def, mem_tolist]
>- metis_tac[]
>> first_x_assum(ASSUME_TAC o Q.SPECL [`toList (EL n l)`,`ORD h`])
>> first_x_assum(MATCH_MP_TAC o Q.SPECL [`n`,`ORD h`,`x1`])
>> rfs[mlvectorTheory.length_toList,mem_tolist,EL_map_toList,length_tolist_cancel]);
val all_ord_string = Q.prove
(`EVERY (\c. MEM (ORD c) ALPHABET) s
<=>
EVERY (\c. ORD c < alphabet_size) s`,
simp_tac list_ss [mem_alphabet_iff]);
val good_vec_thm =
SIMP_RULE std_ss [dom_Brz_alt_equal]
regexp_compilerTheory.compile_regexp_good_vec;
val matcher_side_lem = Q.prove(
`!s. matcher_side s <=> T`,
rw[fetch "-" "matcher_side_def"]
>> match_mp_tac exec_dfa_side_imp
>> rw_tac list_ss [mlvectorTheory.toList_thm]
>- metis_tac [aux |> valOf,good_vec_thm]
>- rw_tac list_ss [all_ord_string,ORD_BOUND,alphabet_size_def]
>- EVAL_TAC)
|>
update_precondition;
val _ = translate match_string_def
(* val _ = translate(word_bit_test |> spec64); *)
(* TODO: this is largely copied from the bootstrap translation
can it be properly abstracted out? *)
local
val ths = ml_translatorLib.eq_lemmas();
in
fun find_equality_type_thm tm =
first (can (C match_term tm) o rand o snd o strip_imp o concl) ths
end
val EqualityType_WORD = find_equality_type_thm``WORD``
val no_closures_def = ml_translatorTheory.no_closures_def;
val LIST_TYPE_def = ml_translatorTheory.LIST_TYPE_def;
val EqualityType_def = ml_translatorTheory.EqualityType_def;
val types_match_def = ml_translatorTheory.types_match_def;
val ctor_same_type_def = semanticPrimitivesTheory.ctor_same_type_def;
Theorem tolist_fromlist_map_cancel:
MAP mlvector$toList (MAP fromList ll) = ll
Proof
Induct_on `ll` >> fs[]
QED
(*---------------------------------------------------------------------------*)
(* Auxiliary functions to deal with null termination. *)
(*---------------------------------------------------------------------------*)
val null_index_def = tDefine "null_index"
`null_index s n =
if n >= strlen s then NONE
else if strsub s n = CHR 0 then SOME n
else null_index s (SUC n)`
(wf_rel_tac `inv_image (measure (λ(a,b). SUC a - b)) (strlen ## I)`);
val null_index_ind = fetch "-" "null_index_ind";
Theorem null_index_le_len:
!s n m. null_index s n = SOME m ==> m < strlen s
Proof
ho_match_mp_tac null_index_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_def])
>> rw[]
QED
Theorem null_index_in_bound:
!s n m. null_index s n = SOME m ==> n <= m
Proof
ho_match_mp_tac null_index_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_def])
>> rw[] >> fs[]
QED
Theorem null_index_null:
!s n m. null_index s n = SOME m ==> strsub s m = CHR 0
Proof
ho_match_mp_tac null_index_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_def])
>> rw[] >> fs[]
QED
Theorem null_index_no_null:
!s n m. null_index s n = SOME m ==> EVERY ($~ o $= (CHR 0)) (TAKE (m-n) (DROP n (explode s)))
Proof
ho_match_mp_tac null_index_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_def])
>> rw[]
>> first_x_assum drule >> rpt(disch_then drule)
>> strip_tac
>> imp_res_tac null_index_le_len
>> imp_res_tac null_index_in_bound
>> qmatch_goalsub_abbrev_tac `EVERY _ (TAKE a1 a2)`
>> Q.ISPECL_THEN [`a1`,`1`,`a2`] mp_tac take_drop_partition
>> unabbrev_all_tac
>> impl_tac >- intLib.COOPER_TAC
>> disch_then(fn x => rw[GSYM x])
>> fs[ADD1,DROP_DROP]
>> `n < LENGTH(explode s)`
by(fs[])
>> drule TAKE1_DROP
>> Cases_on `s` >> fs[mlstringTheory.strsub_def]
QED
Theorem null_index_no_null2:
!s n. null_index s n = NONE ==> EVERY ($~ o $= (CHR 0)) (DROP n (explode s))
Proof
ho_match_mp_tac null_index_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_def])
>> rw[] >> Cases_on `n ≥ strlen s`
>> Cases_on `s` >> fs[GREATER_EQ]
>> imp_res_tac DROP_LENGTH_TOO_LONG >> asm_rewrite_tac[] >> fs[]
>> `n < STRLEN s'` by fs[]
>> imp_res_tac DROP_CONS_EL >> fs[]
QED
val cut_at_null_def = Define `cut_at_null s =
case null_index s 0 of
NONE => strcat s (str(CHR 0))
| SOME n => substring s 0 (SUC n)`
Theorem cut_at_null_SPLITP:
!s. cut_at_null s = implode(FST(SPLITP ($= (CHR 0)) (explode s)) ++ [CHR 0])
Proof
Cases >> rw[cut_at_null_def] >> reverse(PURE_TOP_CASE_TAC >> rw[])
>- (imp_res_tac null_index_le_len >> rw[mlstringTheory.substring_def]
>> fs[mlstringTheory.strlen_def,mlstringTheory.implode_def]
>> imp_res_tac null_index_no_null >> fs[]
>> imp_res_tac null_index_null >> fs[]
>> imp_res_tac SPLITP_TAKE_DROP >> rfs[]
>> imp_res_tac (GSYM TAKE_SEG) >> fs[]
>> fs[ADD1]
>> Q.ISPECL_THEN [`s'`,`x`] assume_tac TAKE_EL_SNOC
>> rfs[])
>- (imp_res_tac null_index_no_null2
>> fs[o_DEF] >> imp_res_tac SPLITP_EVERY
>> fs[mlstringTheory.implode_def,mlstringTheory.strcat_thm])
QED
val _ = translate cut_at_null_def;
val null_index_side_lem = Q.prove(
`!s n. null_index_side s n <=> T`,
ho_match_mp_tac null_index_ind
>> rw[]
>> PURE_ONCE_REWRITE_TAC[fetch "-" "null_index_side_def"]
>> fs[ADD1])
|> update_precondition;
val cut_at_null_side_lem = Q.prove(`!s. cut_at_null_side s <=> T`,
rw[fetch "-" "cut_at_null_side_def",null_index_side_lem]
>> imp_res_tac null_index_le_len >> fs[])
|> update_precondition;
val null_index_w_def = tDefine "null_index_w"
`null_index_w s n =
if n >= LENGTH s then NONE
else if EL n s = 0w then SOME n
else null_index_w s (SUC n)`
(wf_rel_tac `inv_image (measure (λ(a,b). SUC a - b)) (LENGTH ## I)`);
val null_index_w_ind = fetch "-" "null_index_w_ind";
Theorem null_index_w_le_len:
!s n m. null_index_w s n = SOME m ==> m < LENGTH s
Proof
ho_match_mp_tac null_index_w_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index_w` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_w_def])
>> rw[]
QED
Theorem null_index_w_in_bound:
!s n m. null_index_w s n = SOME m ==> n <= m
Proof
ho_match_mp_tac null_index_w_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index_w` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_w_def])
>> rw[] >> fs[]
QED
Theorem null_index_w_null:
!s n m. null_index_w s n = SOME m ==> EL m s = 0w
Proof
ho_match_mp_tac null_index_w_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index_w` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_w_def])
>> rw[] >> fs[]
QED
Theorem null_index_w_no_null:
!s n m. null_index_w s n = SOME m ==> EVERY ($~ o $= 0w) (TAKE (m-n) (DROP n s))
Proof
ho_match_mp_tac null_index_w_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index_w` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_w_def])
>> rw[]
>> first_x_assum drule >> rpt(disch_then drule)
>> strip_tac
>> imp_res_tac null_index_w_le_len
>> imp_res_tac null_index_w_in_bound
>> qmatch_goalsub_abbrev_tac `EVERY _ (TAKE a1 a2)`
>> Q.ISPECL_THEN [`a1`,`1`,`a2`] mp_tac take_drop_partition
>> unabbrev_all_tac
>> impl_tac >- intLib.COOPER_TAC
>> disch_then(fn x => rw[GSYM x])
>> fs[ADD1,DROP_DROP]
>> `n < LENGTH s`
by(fs[])
>> drule TAKE1_DROP >> fs[]
QED
Theorem null_index_w_no_null2:
!s n. null_index_w s n = NONE ==> EVERY ($~ o $= 0w) (DROP n s)
Proof
ho_match_mp_tac null_index_w_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index_w` (mp_tac o PURE_ONCE_REWRITE_RULE [null_index_w_def])
>> rw[] >> Cases_on `n ≥ LENGTH s`
>> fs[GREATER_EQ]
>> imp_res_tac DROP_LENGTH_TOO_LONG >> asm_rewrite_tac[] >> fs[]
>> `n < LENGTH s` by fs[]
>> imp_res_tac DROP_CONS_EL >> fs[]
QED
val cut_at_null_w_def = Define `cut_at_null_w s =
case null_index_w s 0 of
NONE => s ++ [0w]
| SOME n => SEG (SUC n) 0 s`
Theorem cut_at_null_w_SPLITP:
!s. cut_at_null_w s = FST(SPLITP ($= 0w) s) ++ [0w]
Proof
rw[cut_at_null_w_def] >> reverse(PURE_TOP_CASE_TAC >> rw[])
>- (imp_res_tac null_index_w_le_len >> rw[mlstringTheory.substring_def]
>> fs[mlstringTheory.strlen_def,mlstringTheory.implode_def]
>> imp_res_tac null_index_w_no_null >> fs[]
>> imp_res_tac null_index_w_null >> fs[]
>> imp_res_tac SPLITP_TAKE_DROP >> rfs[]
>> fs[GSYM TAKE_SEG,ADD1]
>> Q.ISPECL_THEN [`s`,`x`] assume_tac TAKE_EL_SNOC
>> rfs[])
>- (imp_res_tac null_index_w_no_null2
>> fs[o_DEF] >> imp_res_tac SPLITP_EVERY >> fs[])
QED
Theorem null_index_w_thm:
∀s n. null_index_w (s:word8 list) n = null_index (implode (MAP (CHR ∘ w2n) s)) n
Proof
ho_match_mp_tac null_index_w_ind
>> rpt strip_tac
>> MAP_EVERY PURE_ONCE_REWRITE_TAC [[null_index_def],[null_index_w_def]] >> rw[]
>> fs[mlstringTheory.implode_def]
>> `n < LENGTH s` by fs[]
>> rfs[EL_MAP]
>> qspecl_then [`[EL n s]`,`[0w]`] assume_tac MAP_CHR_w2n_11
>> fs[]
QED
Theorem cut_at_null_w_thm:
∀s. cut_at_null_w (s:word8 list) = MAP (n2w o ORD) (explode (cut_at_null (implode (MAP (CHR ∘ w2n) s))))
Proof
rw[cut_at_null_w_def,cut_at_null_def,null_index_w_thm]
>> PURE_TOP_CASE_TAC >> rw[MAP_MAP_o]
>> fs[n2w_ORD_CHR_w2n]
>> imp_res_tac null_index_le_len
>> fs[mlstringTheory.implode_def,mlstringTheory.substring_def]
>> MAP_EVERY PURE_ONCE_REWRITE_TAC [[null_index_def],[null_index_w_def]] >> rw[]
>> fs[GSYM TAKE_SEG,MAP_TAKE,MAP_MAP_o,n2w_ORD_CHR_w2n]
QED
Theorem cut_at_null_thm:
cut_at_null(strlit (MAP (CHR o w2n) l)) = strlit(MAP (CHR o w2n) (cut_at_null_w(l:word8 list)))
Proof
rw[cut_at_null_w_thm,MAP_MAP_o,implode_def,CHR_w2n_n2w_ORD,REWRITE_RULE[implode_def] implode_explode]
QED
val null_terminated_def = Define `
null_terminated s = ?m. null_index s 0 = SOME m`
val null_terminated_w_def = Define `
null_terminated_w s = ?m. null_index_w s 0 = SOME m`
Theorem null_terminated_w_thm:
!s. null_terminated_w (s:word8 list) = null_terminated(implode(MAP (CHR o w2n) s))
Proof
rw[null_terminated_def,null_terminated_w_def,null_index_w_thm]
QED
Theorem null_index_strcat1:
!s1 n s2 m. null_index s1 n = SOME m ==> null_index (strcat s1 s2) n = SOME m
Proof
ho_match_mp_tac null_index_ind
>> rpt strip_tac
>> qhdtm_x_assum `null_index` mp_tac
>> PURE_ONCE_REWRITE_TAC [null_index_def]
>> rw[] >> fs[]
>> MAP_EVERY Cases_on [`s1`,`s2`]
>> fs[mlstringTheory.strsub_def,mlstringTheory.strcat_def,mlstringTheory.concat_def,EL_APPEND_EQN]
QED
Theorem null_terminated_cut_APPEND:
!s1 s2. null_terminated s1 ==> cut_at_null(strcat s1 s2) = cut_at_null s1
Proof
rw[null_terminated_def,cut_at_null_def] >> imp_res_tac null_index_strcat1
>> fs[] >> imp_res_tac null_index_le_len
>> MAP_EVERY Cases_on [`s1`,`s2`]
>> fs[mlstringTheory.strsub_def,mlstringTheory.strcat_def,mlstringTheory.concat_def,
mlstringTheory.substring_def]
>> match_mp_tac SEG_APPEND1 >> fs[]
QED
Theorem null_terminated_cut_w_APPEND:
!s1 s2. null_terminated_w(s1:word8 list) ==> cut_at_null_w(s1 ++ s2) = cut_at_null_w s1
Proof
rw[null_terminated_w_thm,cut_at_null_w_thm]
>> drule null_terminated_cut_APPEND
>> disch_then(qspec_then `implode(MAP (CHR ∘ w2n) s2)` assume_tac)
>> simp[mlstringTheory.implode_STRCAT]
QED
(*---------------------------------------------------------------------------*)
(* Loop processing input stream. Currently limited to strings <= 256 chars. *)
(* This is not a HOL function so is not being pushed through translate. *)
(*---------------------------------------------------------------------------*)
val dummyarr_e = ``(App Aw8alloc [Lit (IntLit 0); Lit (Word8 0w)])``
val eval_thm = let
val env = get_ml_prog_state () |> ml_progLib.get_env
val st = get_ml_prog_state () |> ml_progLib.get_state
val new_st = ``^st with refs := ^st.refs ++ [W8array (REPLICATE 0 0w)]``
val goal = list_mk_icomb (prim_mk_const {Thy="ml_prog", Name="eval_rel"},
[st, env, dummyarr_e, new_st, mk_var ("x", semanticPrimitivesSyntax.v_ty)])
val lemma = goal |> (EVAL THENC SIMP_CONV(srw_ss())[semanticPrimitivesTheory.state_component_equality])
val v_thm = prove(mk_imp(lemma |> concl |> rand, goal),
rpt strip_tac \\ rveq \\ match_mp_tac(#2(EQ_IMP_RULE lemma))
\\ asm_simp_tac bool_ss [])
|> GEN_ALL |> SIMP_RULE std_ss [] |> SPEC_ALL;
val v_tm = v_thm |> concl |> strip_comb |> #2 |> last
val v_def = define_abbrev false "dummyarr_loc" v_tm
in v_thm |> REWRITE_RULE [GSYM v_def] end
val dummyarr_loc_def = fetch "-" "dummyarr_loc_def";
val _ = ml_prog_update (add_Dlet eval_thm "dummyarr");
val forward_matching_lines = process_topdecs`
fun forward_loop inputarr =
(#(accept_call) "" inputarr;
let val ln = Word8Array.substring inputarr 0 256;
val ln' = cut_at_null ln;
in
if match_string ln' then
#(emit_string) ln' dummyarr
else ()
end;
forward_loop inputarr);
fun forward_matching_lines u =
let val inputarr = Word8Array.array 256 (Word8.fromInt 0);
in
forward_loop inputarr
end`
val _ = append_prog forward_matching_lines;
val st = get_ml_prog_state();
val maincall =
``Dlet unknown_loc (Pcon NONE []) (App Opapp [Var (Short "forward_matching_lines"); Con NONE []])``
val filter_ffi = Datatype `
filter_ffi =
<| input : word8 list llist
|>`
val filter_oracle = Define `
(filter_oracle:filter_ffi oracle) port st conf bytes =
if port = ExtCall "accept_call" then
(if st.input = LNIL then Oracle_final FFI_diverged
else if LENGTH bytes = 256 then
Oracle_return (st with input := THE(LTL(st.input)))
(TAKE 256 (THE(LHD st.input)) ++ DROP (LENGTH(THE(LHD st.input))) bytes)
else Oracle_final FFI_failed)
else if port = ExtCall "emit_string" then
Oracle_return st bytes
else Oracle_final FFI_failed
`
val encode_oracle_state_def = Define `
encode_oracle_state st =
(Fun
(λffi.
case ffi of
iNum n =>
(case LNTH n st.input of
SOME l => Str(MAP (CHR o w2n) l)
| NONE => Num 0)
| _ => ARB
)
)`
val filter_ffi_component_equality = fetch "-" "filter_ffi_component_equality"
val decode_oracle_state_def = Define `
decode_oracle_state ffi =
case destFun ffi of
SOME instream =>
<|input:= LUNFOLD (λn. OPTION_MAP ($, (SUC n) o MAP (n2w o ORD))
(destStr(instream (iNum n)))) 0|>`
val filter_cf_oracle = Define `
filter_cf_oracle port conf bytes ffi =
case filter_oracle (ExtCall port) (decode_oracle_state ffi) conf bytes of
Oracle_final FFI_failed => NONE
| Oracle_final FFI_diverged => SOME FFIdiverge
| Oracle_return st' bytes => SOME(FFIreturn bytes (encode_oracle_state st'))`
val seL4_IO_def = Define `
seL4_IO input events =
one(
FFI_part
(encode_oracle_state (<|input:=input|>))
filter_cf_oracle
["accept_call"; "emit_string"]
events)`
Theorem decode_encode_oracle_state_11:
!ffi_st. decode_oracle_state(encode_oracle_state ffi_st) = ffi_st
Proof
rw[encode_oracle_state_def,decode_oracle_state_def,
filter_ffi_component_equality] >>
rename1 `_ = ll` >>
qho_match_abbrev_tac `a1 ll = _` >>
PURE_ONCE_REWRITE_TAC[LLIST_BISIMULATION] >>
qexists_tac `\x y. ?ll. x = a1 ll /\ y = ll` >>
qunabbrev_tac `a1` >>
rw[] >>
simp[SimpL ``$=``, Once LUNFOLD] >>
simp[LNTH] >>
rename1 `LHD ll` >> Cases_on `ll` >> fs[MAP_MAP_o,n2w_ORD_CHR_w2n] >>
simp[GSYM OPTION_MAP_COMPOSE] >>
PURE_ONCE_REWRITE_TAC[LUNFOLD_BISIMULATION] >>
qexists_tac `\x y. x = SUC y` >>
rw[] >> metis_tac[option_CASES]
QED
Theorem every_LDROP:
!i l ll1.
every f ll1 /\
LDROP i ll1 = SOME ll2
==> every f ll2
Proof
Induct >> Cases_on `ll1` >> rw[] >> rw[] >> metis_tac[]
QED
val LLENGTH_NONE_LTAKE = Q.prove(
`!n ll. LLENGTH ll = NONE ==> ?l. LTAKE n ll = SOME l`,
Induct >> Cases_on `ll` >> rw[]);
val is_emit_def = Define
`is_emit (IO_event s _ _) = (s = ExtCall "emit_string")`
val output_event_of_def = Define
`output_event_of s = IO_event (ExtCall "emit_string") s []`
val nth_arr_def = Define `nth_arr n ll =
FST(FUNPOW (λ(l,ll).
if ll = [||] then (l,[||])
else (
TAKE 256 (THE(LHD ll)) ++ DROP (LENGTH(THE(LHD ll))) l
,THE(LTL ll))) n
(REPLICATE 256 (0w:word8),ll))`
Theorem nth_arr_infinite:
!n ll.
LLENGTH ll = NONE ==>
nth_arr n ll =
FST(FUNPOW (λ(l,ll).
(TAKE 256 (THE(LHD ll)) ++ DROP (LENGTH(THE(LHD ll))) l,THE(LTL ll))) n
(REPLICATE 256 (0w:word8),ll))
Proof
simp[nth_arr_def] >>
qabbrev_tac `a1 = REPLICATE 256 (0w:word8)` >>
`LENGTH a1 = 256` by(unabbrev_all_tac >> simp[]) >>
MAP_EVERY pop_assum [mp_tac,kall_tac] >>
simp[PULL_FORALL] >> qid_spec_tac `a1` >>
Induct_on `n` >> rw[FUNPOW] >>
Cases_on `ll` >> fs[LENGTH_TAKE_EQ]
QED
Theorem nth_arr_SUC: !n ll.
LLENGTH ll = NONE ==>
nth_arr (SUC n) ll
=
TAKE 256 (THE(LNTH n ll)) ++ DROP (LENGTH(THE(LNTH n ll))) (nth_arr n ll)
Proof
`∀n ll (l:word8 list).
LLENGTH ll = NONE /\ LENGTH l = 256 ⇒
(FUNPOW
(λ(l,ll).
(TAKE 256 (THE (LHD ll)) ++ DROP (LENGTH (THE (LHD ll))) l,
THE (LTL ll))) (SUC n) (l,ll)) =
(TAKE 256 (THE (LNTH n ll)) ++
DROP (LENGTH (THE (LNTH n ll)))
(FST
(FUNPOW
(λ(l,ll).
(TAKE 256 (THE (LHD ll)) ++
DROP (LENGTH (THE (LHD ll))) l,THE (LTL ll))) n
(l,ll))),THE(LDROP (SUC n) ll))`
suffices_by rw[nth_arr_infinite] >>
Induct_on `n` >> rpt strip_tac >> fs[FUNPOW_SUC,LDROP1_THM,CONJUNCT1 LNTH] >>
`~LFINITE ll` by simp[LFINITE_LLENGTH] >>
imp_res_tac infinite_lnth_some >>
first_x_assum(qspec_then `SUC n` strip_assume_tac) >>
drule LNTH_LDROP >> simp[] >> disch_then kall_tac >>
Cases_on `ll` >> fs[LDROP_SUC] >>
rename1 `LDROP n ll` >>
Cases_on `LDROP n ll` >> fs[] >>
metis_tac[LDROP_NONE_LFINITE]
QED
Theorem nth_arr_LENGTH:
every ($>= 256 ∘ LENGTH) ll
==> LENGTH(nth_arr n ll) = 256
Proof
simp[nth_arr_def] >>
qabbrev_tac `a1 = REPLICATE 256 (0w:word8)` >>
`LENGTH a1 = 256` by(unabbrev_all_tac >> simp[]) >>
MAP_EVERY pop_assum [mp_tac,kall_tac] >>
qid_spec_tac `a1` >> qid_spec_tac `ll` >>
Induct_on `n` >> rw[FUNPOW] >>
Cases_on `ll` >> fs[LENGTH_TAKE_EQ]
QED
val next_filter_events = Define
`next_filter_events filter_fun last_input input =
let new_input = TAKE 256 input ++ DROP (LENGTH input) last_input
in
[IO_event (ExtCall "accept_call") [] (ZIP (last_input,new_input))] ++
if filter_fun(cut_at_null_w input) then
[IO_event (ExtCall "emit_string") (cut_at_null_w new_input) []]
else
[]`
val nth_arr_init_def = Define `nth_arr_init n ll buff =
FST(FUNPOW
(λ(l,ll).
if ll = [||] then (l,[||])
else
(TAKE 256 (THE (LHD ll)) ++
DROP (LENGTH (THE (LHD ll))) l,THE (LTL ll))) n
(buff:word8 list,ll))`
Theorem nth_arr_init_SUC: !h n ll init.
nth_arr_init (SUC n) (h:::ll) init = nth_arr_init n ll (TAKE 256 h ++ DROP(LENGTH h) init)
Proof
rw[nth_arr_init_def,FUNPOW]
QED
Theorem nth_arr_init_add: !n m ll init.
LLENGTH ll = NONE ==>
nth_arr_init (n + m) ll init = nth_arr_init n (THE(LDROP m ll)) (nth_arr_init m ll init)
Proof
Induct_on `m` >- rw[nth_arr_init_def] >>
rw[] >>
PURE_ONCE_REWRITE_TAC[GSYM ADD_SUC] >>
Cases_on `ll` >> fs[] >>
fs[nth_arr_init_SUC]
QED
Theorem LLENGTH_NONE_LDROP:
!n ll ll'. LLENGTH ll = NONE /\ LDROP n ll = SOME ll' ==> LLENGTH ll' = NONE
Proof
Induct >> Cases_on `ll` >> rw[] >> rw[LLENGTH_THM] >> metis_tac[]
QED
Theorem nth_arr_init_LENGTH:
!n ll init. every ($>= 256 ∘ LENGTH) ll /\
LENGTH init = 256 /\ LLENGTH ll = NONE
==> LENGTH(nth_arr_init n ll init) = 256
Proof
Induct_on `n` >> Cases_on `ll` >> rw[nth_arr_init_SUC,TAKE_LENGTH_TOO_LONG] >>
TRY(simp[nth_arr_init_def] >> NO_TAC)
QED
val filter_bisim_rel_infinite_def = Define `
filter_bisim_rel_infinite =
λl1 l2.
(?inl buff.
l1 = LFILTER is_emit (LFLATTEN
(LGENLIST
(λx.
fromList
(next_filter_events (language ∘ MAP (CHR ∘ w2n))
(nth_arr_init x inl buff) (THE (LNTH x inl)))) NONE)) /\
l2 = LMAP (output_event_of o cut_at_null_w) (LFILTER (language o MAP (CHR o w2n) o cut_at_null_w) inl) /\
LLENGTH inl = NONE /\
LENGTH buff = 256 /\
every ($>= 256 ∘ LENGTH) inl /\
every null_terminated_w inl)`
Theorem exists_LFLATTEN:
!P ll. exists P (LFLATTEN ll) /\
every LFINITE ll /\
every ($<> [||]) ll==>
exists (exists P) ll
Proof
rw[exists_LNTH] >>
rpt(pop_assum mp_tac) >>
MAP_EVERY qid_spec_tac [`e`,`ll`,`n`] >>
ho_match_mp_tac COMPLETE_INDUCTION >>
Cases >> rw[LNTH] >-
(Cases_on `ll` >> fs[] >>
rename1 `[||] <> hd` >> Cases_on `hd` >> fs[] >>
rveq >> qexists_tac `0` >> simp[] >> qexists_tac `0` >> simp[]) >-
(qpat_x_assum `_ = OPTION_JOIN _` (assume_tac o GSYM) >>
fs[OPTION_JOIN_EQ_SOME,OPTION_MAP_EQ_SOME] >>
Cases_on `ll` >> fs[] >>
rename1 `h:::ll` >>
Cases_on `h` >> fs[] >>
rename1 `(h:::l):::ll` >>
imp_res_tac LFINITE_HAS_LENGTH >>
rveq >> fs[LNTH_LAPPEND] >>
PURE_FULL_CASE_TAC >-
(fs[] >> qexists_tac `0` >> simp[] >>
Q.REFINE_EXISTS_TAC `SUC _` >> simp[] >> metis_tac[]) >-
(fs[] >> Q.REFINE_EXISTS_TAC `SUC _` >> simp[] >>
rename1 `n0 < n` >>
first_x_assum (match_mp_tac o MP_CANON) >>
qexists_tac `n0 - n` >> simp[] >> metis_tac[]))
QED
val cut_at_null_simplify = Q.prove(`(?n' e'.
SOME e' =
LNTH n'
(fromList
(next_filter_events f
iarr input)) ∧ is_emit e')
= f(cut_at_null_w input)`,
rw[EQ_IMP_THM,next_filter_events] >-
(qexists_tac `SUC 0` >> fs[is_emit_def]) >-
(rename1 `LNTH n` >> Cases_on `n` >> fs[] >> rveq >> fs[is_emit_def] >>
CCONTR_TAC >> fs[] >> rveq >> fs[is_emit_def]));
val LFLATTEN_fromList_GENLIST =
LFLATTEN_fromList |> Q.SPEC `GENLIST f n` |> SIMP_RULE std_ss [MAP_GENLIST,o_DEF]
val LFILTER_fromList = Q.prove(`
!l. LFILTER f (fromList l) = fromList(FILTER f l)`,
Induct >> rw[]);
Theorem forward_matching_lines_div_spec:
!input output rv.
limited_parts ["accept_call";"emit_string"] p /\
LLENGTH input = NONE /\
every (null_terminated_w) input /\
every ($>= 256 o LENGTH) input
==>
app (p:'ffi ffi_proj) ^(fetch_v "forward_matching_lines" st) [rv]
(seL4_IO input [] * W8ARRAY dummyarr_loc [])
(POSTd io. LFILTER is_emit io = LMAP (output_event_of o cut_at_null_w) (LFILTER (language o MAP (CHR o w2n) o cut_at_null_w) input))
Proof
rpt strip_tac >>
xcf "forward_matching_lines" st >>
xlet_auto >- xsimpl >>
xlet_auto >- xsimpl >>
unfold_cf_app >>
xcf_div_rule IMP_app_POSTd_one_FFI_part_FLATTEN "forward_loop" st >>
rename1 `_ 0 inputarr` >>
MAP_EVERY qexists_tac
[`λn. W8ARRAY dummyarr_loc [] *
W8ARRAY inputarr
(nth_arr n input)`,
`\n. if n = 0 then []
else next_filter_events (language o MAP (CHR o w2n)) (nth_arr (n-1) input) (THE(LNTH (n-1) input))
`,
`K($= inputarr)`,
`λn. encode_oracle_state
<|input:=THE(LDROP n input)|>`,
`filter_cf_oracle`
] >>
conj_tac >- simp[] >>
conj_tac >-
(xsimpl >>
simp[seL4_IO_def,next_filter_events,nth_arr_def] >>
simp[Once LUNFOLD] >>
simp[toList,LAPPEND_NIL_2ND] >>
xsimpl) >>
simp[] >>
conj_tac >-
(strip_tac >>
qmatch_goalsub_abbrev_tac `W8ARRAY inputarr inputbuff` >>
`LENGTH inputbuff = 256`
by(unabbrev_all_tac >> metis_tac[nth_arr_LENGTH]) >>
xlet `(POSTv v. &UNIT_TYPE () v * W8ARRAY dummyarr_loc [] *
W8ARRAY inputarr ((TAKE 256 (THE (LHD (THE (LDROP i input)))) ++
DROP (LENGTH (THE (LHD (THE (LDROP i input))))) inputbuff)) *
one
(FFI_part
(encode_oracle_state
<|input := THE (LDROP (SUC i) input)|>) filter_cf_oracle
["accept_call"; "emit_string"]
[IO_event (ExtCall "accept_call") [] (ZIP(inputbuff,
((TAKE 256 (THE (LHD (THE (LDROP i input)))) ++
DROP (LENGTH (THE (LHD (THE (LDROP i input))))) inputbuff))))]
))` >-
(xffi >> xsimpl >>
qmatch_goalsub_abbrev_tac `one(FFI_part s u ns events)` >>
MAP_EVERY qexists_tac [`W8ARRAY dummyarr_loc []`,`s`,`u`,`ns`,`events`] >>
unabbrev_all_tac >> xsimpl >>
simp[filter_cf_oracle,decode_encode_oracle_state_11,filter_oracle] >>
`?i1 input'. LDROP i input = SOME(i1:::input')`
by(qpat_x_assum `LLENGTH input = NONE` mp_tac >>
qid_spec_tac `input` >> rpt(pop_assum kall_tac) >>
Induct_on `i` >> fs[] >>
Cases >> rw[]) >>
simp[LDROP_SUC] >>
xsimpl) >>
xlet `(POSTv v. &UNIT_TYPE () v * W8ARRAY dummyarr_loc [] *
W8ARRAY inputarr ((TAKE 256 (THE (LHD (THE (LDROP i input)))) ++
DROP (LENGTH (THE (LHD (THE (LDROP i input))))) inputbuff)) *
one
(FFI_part
(encode_oracle_state
<|input := THE (LDROP (SUC i) input)|>) filter_cf_oracle
["accept_call"; "emit_string"]
(next_filter_events (language o MAP (CHR o w2n)) inputbuff (THE(LNTH i input)))))` >-
(xlet_auto >-
(xsimpl >>
`?i1 input'. LDROP i input = SOME(i1:::input')`
by(qpat_x_assum `LLENGTH input = NONE` mp_tac >>
qid_spec_tac `input` >> rpt(pop_assum kall_tac) >>
Induct_on `i` >> fs[] >>
Cases >> rw[]) >>
fs[] >>
imp_res_tac every_LDROP >>
fs[every_thm,LENGTH_TAKE_EQ]) >>
xlet_auto >- xsimpl >>
xlet_auto >- xsimpl >>
reverse xif >-
(xcon >> xsimpl >>
`~LFINITE input` by rw[LFINITE_LLENGTH] >>
`?i1. LNTH i input = SOME(i1)`
by(CCONTR_TAC >> fs[LFINITE_LNTH_NONE] >> metis_tac[NOT_SOME_NONE,option_nchotomy]) >>
fs[] >>
simp[Abbr `inputbuff`,next_filter_events,nth_arr_SUC] >>
drule_then(qspec_then `i` strip_assume_tac) (GEN_ALL nth_arr_LENGTH) >>
drule_then strip_assume_tac LNTH_LDROP >> fs[] >>
fs[match_string_eq] >> fs[cut_at_null_w_thm,MAP_MAP_o,CHR_w2n_n2w_ORD,implode_def] >>
imp_res_tac every_LNTH >> fs[every_thm] >>
fs[null_terminated_w_thm,implode_def] >>
fs[null_terminated_cut_APPEND,TAKE_APPEND,TAKE_LENGTH_TOO_LONG] >>
fs[GSYM strlit_STRCAT,null_terminated_cut_APPEND] >>
fs[DROP_LENGTH_TOO_LONG] >>
xsimpl) >-
(xffi >>
fs[cut_at_null_thm,STRING_TYPE_def] >>
qmatch_goalsub_abbrev_tac `MAP _ a1 = _` >>
qexists_tac `a1` >> qunabbrev_tac `a1` >>
simp[] >>
qmatch_goalsub_abbrev_tac `W8ARRAY inputarr ibuff` >>
MAP_EVERY qexists_tac [`[]`,`W8ARRAY inputarr ibuff`] >>
xsimpl >>
qmatch_goalsub_abbrev_tac `FFI_part s u ns events` >>
MAP_EVERY qexists_tac [`s`,`u`,`ns`,`events`] >>
unabbrev_all_tac >> simp[filter_cf_oracle,filter_oracle] >>
xsimpl >>
`~LFINITE input` by rw[LFINITE_LLENGTH] >>
`?i1. LNTH i input = SOME(i1)`
by(CCONTR_TAC >> fs[LFINITE_LNTH_NONE] >> metis_tac[NOT_SOME_NONE,option_nchotomy]) >>
fs[] >>
simp[next_filter_events,nth_arr_SUC] >>
drule_then(qspec_then `i` strip_assume_tac) (GEN_ALL nth_arr_LENGTH) >>
drule_then strip_assume_tac LNTH_LDROP >> fs[] >>
fs[match_string_eq] >> fs[cut_at_null_w_thm,MAP_MAP_o,CHR_w2n_n2w_ORD,implode_def] >>
imp_res_tac every_LNTH >> fs[every_thm] >>
fs[null_terminated_w_thm,implode_def] >>
fs[null_terminated_cut_APPEND,TAKE_APPEND,TAKE_LENGTH_TOO_LONG] >>
fs[DROP_LENGTH_TOO_LONG] >>
fs[GSYM strlit_STRCAT,null_terminated_cut_APPEND] >>
simp[decode_encode_oracle_state_11] >>
xsimpl)
) >>
xvar >> xsimpl >>
simp[nth_arr_SUC] >>
`~LFINITE input` by rw[LFINITE_LLENGTH] >>
`?i1. LNTH i input = SOME(i1)`
by(CCONTR_TAC >> fs[LFINITE_LNTH_NONE] >> metis_tac[NOT_SOME_NONE,option_nchotomy]) >>
fs[] >>
drule_then strip_assume_tac LNTH_LDROP >> fs[]
) >>
simp [Once(REWRITE_RULE [o_DEF] LGENLIST_NONE_UNFOLD)] >>
match_mp_tac LLIST_BISIM_UPTO >>
qexists_tac `filter_bisim_rel_infinite` >>
conj_tac >-
(simp[filter_bisim_rel_infinite_def,nth_arr_def,nth_arr_init_def] >>
metis_tac[LENGTH_REPLICATE]) >>
rpt(pop_assum kall_tac) >>
rw[filter_bisim_rel_infinite_def] >>
qmatch_goalsub_abbrev_tac `a1 /\ _ \/ _` >>
Cases_on `a1` (* Are there incoming messages that will pass the filter? *) >>
fs[markerTheory.Abbrev_def]
>-
(`LFILTER (language ∘ MAP (CHR ∘ w2n) ∘ cut_at_null_w) inl = [||]` suffices_by fs[] >>
fs[LFILTER_EQ_NIL] >>
rpt(pop_assum mp_tac) >>
MAP_EVERY qid_spec_tac [`buff`,`inl`] >>
SIMP_TAC pure_ss [AND_IMP_INTRO,LEFT_FORALL_IMP_THM] >>
ho_match_mp_tac every_coind >>
rpt gen_tac >> rpt(disch_then strip_assume_tac) >>
fs[] >>
pop_assum(assume_tac o PURE_ONCE_REWRITE_RULE[LGENLIST_NONE_UNFOLD]) >>
pop_assum(mp_tac o PURE_REWRITE_RULE[Once next_filter_events]) >>
rw[is_emit_def] >> fs[o_DEF] >>
fs(map (REWRITE_RULE[ADD1]) [nth_arr_init_SUC,LNTH]) >>
PURE_ONCE_REWRITE_TAC[CONJ_SYM] >> asm_exists_tac >>
fs[LENGTH_TAKE_EQ]) >>
fs[LFILTER_EQ_NIL,every_def] >>
conj_tac >-
(drule exists_LFLATTEN >>
impl_tac >- rw[every_LNTH,LNTH_LGENLIST,next_filter_events,LFINITE_fromList] >>
simp[SimpL ``$==>``,exists_LNTH,LNTH_LGENLIST] >>
Ho_Rewrite.PURE_ONCE_REWRITE_TAC [cut_at_null_simplify] >>
disch_then(strip_assume_tac o Ho_Rewrite.REWRITE_RULE[whileTheory.LEAST_EXISTS]) >>
rename1 `LNTH n0` >>
qmatch_goalsub_abbrev_tac `LGENLIST f` >>
Q.ISPECL_THEN [`n0`,`f`] assume_tac (GEN_ALL LGENLIST_CHUNK_GENLIST) >>
simp[] >>
pop_assum kall_tac >>
simp[LFLATTEN_LAPPEND_fromList] >>
unabbrev_all_tac >>
Ho_Rewrite.REWRITE_TAC[LFLATTEN_fromList_GENLIST] >>
simp[LFILTER_APPEND,LFINITE_fromList,LFILTER_fromList] >>
qmatch_goalsub_abbrev_tac `FILTER is_emit a1` >>
`FILTER is_emit a1 = []`
by(unabbrev_all_tac >>
rw[FILTER_EQ_NIL,EVERY_FLAT,EVERY_GENLIST,next_filter_events,
is_emit_def]) >>
simp[] >> ntac 2 (pop_assum kall_tac) >>
simp[Once LGENLIST_NONE_UNFOLD] >>
simp[LFILTER_APPEND,LFINITE_fromList,LFILTER_fromList,
next_filter_events,is_emit_def] >>
`~LFINITE inl` by fs[LFINITE_LLENGTH] >>
drule(CONJUNCT1 LTAKE_DROP) >>
disch_then(qspec_then `n0` (fn thm => CONV_TAC(RHS_CONV(PURE_ONCE_REWRITE_CONV [GSYM thm])))) >>
simp[LFILTER_APPEND,LFINITE_fromList,LFILTER_fromList] >>
qmatch_goalsub_abbrev_tac `FILTER af al` >>
`FILTER af al = []`
by(unabbrev_all_tac >>
drule_then(qspec_then `n0` strip_assume_tac) LLENGTH_NONE_LTAKE >>
rw[FILTER_EQ_NIL,EVERY_FLAT,EVERY_GENLIST,next_filter_events,
is_emit_def] >>
rw[EVERY_EL] >>
first_x_assum(qspec_then `n` mp_tac) >>
impl_keep_tac >- metis_tac[LTAKE_LENGTH] >>
drule_then drule LTAKE_LNTH_EL >> simp[]) >>
unabbrev_all_tac >>
`?i1 inl'. LDROP n0 inl = SOME(i1:::inl')`
by(qpat_x_assum `LLENGTH inl = NONE` mp_tac >>
qid_spec_tac `inl` >> rpt(pop_assum kall_tac) >>
Induct_on `n0` >> fs[] >>
Cases >> rw[]) >>
fs[LNTH_HD_LDROP,output_event_of_def] >>
fs[GSYM every_def] >>
imp_res_tac every_LDROP >>