-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.lean
4138 lines (4053 loc) · 117 KB
/
star.lean
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
import classes.unrestricted.basics.lifting
import classes.unrestricted.closure_properties.concatenation
import utilities.written_by_others.trim_assoc
-- new nonterminal type
private def nn (N : Type) : Type :=
N ⊕ fin 3
-- new symbol type
private def ns (T N : Type) : Type :=
symbol T (nn N)
variables {T : Type}
section specific_symbols
private def Z {N : Type} : ns T N := symbol.nonterminal (sum.inr 0)
private def H {N : Type} : ns T N := symbol.nonterminal (sum.inr 1) -- denoted by `#` in the pdf
private def R {N : Type} : ns T N := symbol.nonterminal (sum.inr 2)
private def S {g : grammar T} : ns T g.nt := symbol.nonterminal (sum.inl g.initial)
private lemma Z_neq_H {N : Type} : Z ≠ @H T N :=
begin
intro ass,
have imposs := sum.inr.inj (symbol.nonterminal.inj ass),
exact fin.zero_ne_one imposs,
end
private lemma Z_neq_R {N : Type} : Z ≠ @R T N :=
begin
intro ass,
have imposs := sum.inr.inj (symbol.nonterminal.inj ass),
have zero_ne_two : (0 : fin 3) ≠ (2 : fin 3), dec_trivial,
exact zero_ne_two imposs,
end
private lemma H_neq_R {N : Type} : H ≠ @R T N :=
begin
intro ass,
have imposs := sum.inr.inj (symbol.nonterminal.inj ass),
have one_ne_two : (1 : fin 3) ≠ (2 : fin 3), dec_trivial,
exact one_ne_two imposs,
end
end specific_symbols
section construction
private def wrap_sym {N : Type} : symbol T N → ns T N
| (symbol.terminal t) := symbol.terminal t
| (symbol.nonterminal n) := symbol.nonterminal (sum.inl n)
private def wrap_gr {N : Type} (r : grule T N) : grule T (nn N) :=
grule.mk
(list.map wrap_sym r.input_L)
(sum.inl r.input_N)
(list.map wrap_sym r.input_R)
(list.map wrap_sym r.output_string)
private def rules_that_scan_terminals (g : grammar T) : list (grule T (nn g.nt)) :=
list.map (λ t, grule.mk
[] (sum.inr 2) [symbol.terminal t] [symbol.terminal t, R]
) (all_used_terminals g)
-- based on `/informal/KleeneStar.pdf`
private def star_grammar (g : grammar T) : grammar T :=
grammar.mk (nn g.nt) (sum.inr 0) (
grule.mk [] (sum.inr 0) [] [Z, S, H] ::
grule.mk [] (sum.inr 0) [] [R, H] ::
grule.mk [] (sum.inr 2) [H] [R] ::
grule.mk [] (sum.inr 2) [H] [] ::
list.map wrap_gr g.rules ++
rules_that_scan_terminals g
)
end construction
section easy_direction
private lemma short_induction {g : grammar T} {w : list (list T)}
(ass : ∀ wᵢ ∈ w.reverse, grammar_generates g wᵢ) :
grammar_derives (star_grammar g) [Z] (Z ::
list.join (list.map (++ [H]) (list.map (list.map symbol.terminal) w.reverse))
) ∧
∀ p ∈ w, ∀ t ∈ p, symbol.terminal t ∈ list.join (list.map grule.output_string g.rules) :=
begin
induction w with v x ih,
{
split,
{
apply grammar_deri_self,
},
{
intros p pin,
exfalso,
exact list.not_mem_nil p pin,
},
},
have vx_reverse : (v :: x).reverse = x.reverse ++ [v],
{
apply list.reverse_cons,
},
rw vx_reverse at *,
specialize ih (by {
intros wᵢ in_reversed,
apply ass,
apply list.mem_append_left,
exact in_reversed,
}),
specialize ass v (by {
apply list.mem_append_right,
apply list.mem_singleton_self,
}),
unfold grammar_generates at ass,
split,
{
apply grammar_deri_of_tran_deri,
{
use (star_grammar g).rules.nth_le 0 (by dec_trivial),
split,
{
apply list.nth_le_mem,
},
use [[], []],
split;
refl,
},
rw [list.nil_append, list.append_nil, list.map_append, list.map_append],
change grammar_derives (star_grammar g) [Z, S, H] _,
have ih_plus := grammar_deri_with_postfix ([S, H] : list (symbol T (star_grammar g).nt)) ih.left,
apply grammar_deri_of_deri_deri ih_plus,
have ass_lifted : grammar_derives (star_grammar g) [S] (list.map symbol.terminal v),
{
clear_except ass,
have wrap_eq_lift : @wrap_sym T g.nt = lift_symbol_ sum.inl,
{
ext,
cases x;
refl,
},
let lifted_g : lifted_grammar_ T :=
lifted_grammar_.mk g (star_grammar g) sum.inl sum.get_left (by {
intros x y hyp,
exact sum.inl.inj hyp,
}) (by {
intros x y hyp,
cases x,
{
cases y,
{
simp only [sum.get_left] at hyp,
left,
congr,
exact hyp,
},
{
simp only [sum.get_left] at hyp,
exfalso,
exact hyp,
},
},
{
cases y,
{
simp only [sum.get_left] at hyp,
exfalso,
exact hyp,
},
{
right,
refl,
},
},
}) (by {
intro x,
refl,
}) (by {
intros r rin,
apply list.mem_cons_of_mem,
apply list.mem_cons_of_mem,
apply list.mem_cons_of_mem,
apply list.mem_cons_of_mem,
apply list.mem_append_left,
rw list.mem_map,
use r,
split,
{
exact rin,
},
unfold wrap_gr,
unfold lift_rule_,
unfold lift_string_,
rw wrap_eq_lift,
}) (by {
rintros r ⟨rin, n, nrn⟩,
iterate 4 {
cases rin,
{
exfalso,
rw rin at nrn,
exact sum.no_confusion nrn,
},
},
change r ∈ list.map wrap_gr g.rules ++ rules_that_scan_terminals g at rin,
rw list.mem_append at rin,
cases rin,
{
clear_except rin wrap_eq_lift,
rw list.mem_map at rin,
rcases rin with ⟨r₀, rin₀, r_of_r₀⟩,
use r₀,
split,
{
exact rin₀,
},
convert r_of_r₀,
unfold lift_rule_,
unfold wrap_gr,
unfold lift_string_,
rw wrap_eq_lift,
},
{
exfalso,
unfold rules_that_scan_terminals at rin,
rw list.mem_map at rin,
rcases rin with ⟨t, tin, r_of_tg⟩,
rw ←r_of_tg at nrn,
exact sum.no_confusion nrn,
},
}),
convert_to
grammar_derives lifted_g.g
[symbol.nonterminal (sum.inl g.initial)]
(lift_string_ lifted_g.lift_nt (list.map symbol.terminal v)),
{
unfold lift_string_,
rw list.map_map,
congr,
},
exact lift_deri_ lifted_g ass,
},
have ass_postf := grammar_deri_with_postfix ([H] : list (symbol T (star_grammar g).nt)) ass_lifted,
rw list.join_append,
rw ←list.cons_append,
apply grammar_deri_with_prefix,
rw list.map_map,
rw list.map_singleton,
rw list.join_singleton,
change grammar_derives (star_grammar g) [S, H] (list.map symbol.terminal v ++ [H]),
convert ass_postf,
},
{
intros p pin t tin,
cases pin,
{
rw pin at tin,
clear pin,
have stin : symbol.terminal t ∈ list.map symbol.terminal v,
{
rw list.mem_map,
use t,
split,
{
exact tin,
},
{
refl,
},
},
cases grammar_generates_only_legit_terminals ass stin with rule_exists imposs,
{
rcases rule_exists with ⟨r, rin, stirn⟩,
rw list.mem_join,
use r.output_string,
split,
{
rw list.mem_map,
use r,
split,
{
exact rin,
},
{
refl,
},
},
{
exact stirn,
},
},
{
exfalso,
exact symbol.no_confusion imposs,
}
},
{
exact ih.right p pin t tin,
}
},
end
private lemma terminal_scan_ind {g : grammar T} {w : list (list T)} (n : ℕ) (n_lt_wl : n ≤ w.length)
(terminals : ∀ v ∈ w, ∀ t ∈ v, symbol.terminal t ∈ list.join (list.map grule.output_string g.rules)) :
grammar_derives (star_grammar g)
((list.map (λ u, list.map symbol.terminal u) (list.take (w.length - n) w)).join ++ [R] ++
(list.map (λ v, [H] ++ list.map symbol.terminal v) (list.drop (w.length - n) w)).join ++ [H])
(list.map symbol.terminal w.join ++ [R, H]) :=
begin
induction n with k ih,
{
rw nat.sub_zero,
rw list.drop_length,
rw list.map_nil,
rw list.join,
rw list.append_nil,
rw list.take_length,
rw list.map_join,
rw list.append_assoc,
apply grammar_deri_self,
},
specialize ih (nat.le_of_succ_le n_lt_wl),
apply grammar_deri_of_deri_deri _ ih,
clear ih,
have wlk_succ : w.length - k = (w.length - k.succ).succ,
{
omega,
},
have lt_wl : w.length - k.succ < w.length,
{
omega,
},
have split_ldw :
list.drop (w.length - k.succ) w =
(w.nth (w.length - k.succ)).to_list ++ list.drop (w.length - k) w,
{
rw wlk_succ,
generalize substit : w.length - k.succ = q,
rw substit at lt_wl,
rw ←list.take_append_drop q w,
rw list.nth_append_right,
swap, {
apply list.length_take_le,
},
have eq_q : (list.take q w).length = q,
{
rw list.length_take,
exact min_eq_left_of_lt lt_wl,
},
rw eq_q,
rw nat.sub_self,
have drop_q_succ :
list.drop q.succ (list.take q w ++ list.drop q w) = list.drop 1 (list.drop q w),
{
rw list.drop_drop,
rw list.take_append_drop,
rw add_comm,
},
rw [drop_q_succ, list.drop_left' eq_q, list.drop_drop],
rw ←list.take_append_drop (1 + q) w,
have q_lt : q < (list.take (1 + q) w).length,
{
rw list.length_take,
exact lt_min (lt_one_add q) lt_wl,
},
rw list.drop_append_of_le_length (le_of_lt q_lt),
apply congr_arg2,
{
rw list.nth_append,
swap, {
rw list.length_drop,
exact nat.sub_pos_of_lt q_lt,
},
rw list.nth_drop,
rw add_zero,
rw list.nth_take (lt_one_add q),
rw add_comm,
rw list_drop_take_succ lt_wl,
rw list.nth_le_nth lt_wl,
refl,
},
{
rw list.take_append_drop,
},
},
apply grammar_deri_with_postfix,
rw [split_ldw, list.map_append, list.join_append, ←list.append_assoc],
apply grammar_deri_with_postfix,
rw [wlk_succ, list.take_succ, list.map_append, list.join_append, list.append_assoc, list.append_assoc],
apply grammar_deri_with_prefix,
clear_except terminals lt_wl,
specialize terminals (w.nth_le (w.length - k.succ) lt_wl) (list.nth_le_mem w (w.length - k.succ) lt_wl),
rw list.nth_le_nth lt_wl,
unfold option.to_list,
rw [list.map_singleton, list.join_singleton, ←list.map_join, list.join_singleton],
apply grammar_deri_of_tran_deri,
{
use (star_grammar g).rules.nth_le 2 (by dec_trivial),
split_ile,
use [[], list.map symbol.terminal (w.nth_le (w.length - k.succ) lt_wl)],
split;
refl,
},
rw list.nil_append,
have scan_segment : ∀ m : ℕ, m ≤ (w.nth_le (w.length - k.succ) lt_wl).length →
grammar_derives (star_grammar g)
([R] ++ list.map symbol.terminal (w.nth_le (w.length - k.succ) lt_wl))
(list.map symbol.terminal (list.take m (w.nth_le (w.length - k.succ) lt_wl)) ++
([R] ++ list.map symbol.terminal (list.drop m (w.nth_le (w.length - k.succ) lt_wl)))),
{
intros m small,
induction m with n ih,
{
rw ←list.append_assoc,
convert grammar_deri_self,
},
apply grammar_deri_of_deri_tran (ih (nat.le_of_succ_le small)),
rw nat.succ_le_iff at small,
use ⟨[], (sum.inr 2), [symbol.terminal (list.nth_le (w.nth_le (w.length - k.succ) lt_wl) n small)],
[symbol.terminal (list.nth_le (w.nth_le (w.length - k.succ) lt_wl) n small), R]⟩,
split,
{
iterate 4 {
apply list.mem_cons_of_mem,
},
apply list.mem_append_right,
unfold rules_that_scan_terminals,
rw list.mem_map,
use list.nth_le (w.nth_le (w.length - k.succ) lt_wl) n small,
split,
{
unfold all_used_terminals,
rw list.mem_filter_map,
use (w.nth_le (w.length - k.succ) lt_wl).nth_le n small,
split,
{
apply terminals,
apply list.nth_le_mem,
},
{
refl,
},
},
{
refl,
},
},
use list.map symbol.terminal (list.take n (w.nth_le (w.length - k.succ) lt_wl)),
use list.map symbol.terminal (list.drop n.succ (w.nth_le (w.length - k.succ) lt_wl)),
dsimp only,
split,
{
trim,
rw list.nil_append,
rw list.append_assoc,
apply congr_arg2,
{
refl,
},
rw ←list.take_append_drop 1 (list.map symbol.terminal (list.drop n (w.nth_le (w.length - k.succ) lt_wl))),
apply congr_arg2,
{
rw ←list.map_take,
rw list_take_one_drop,
rw list.map_singleton,
},
{
rw ←list.map_drop,
rw list.drop_drop,
rw add_comm,
},
},
{
rw list.take_succ,
rw list.map_append,
trim,
rw list.nth_le_nth small,
refl,
},
},
convert scan_segment (w.nth_le (w.length - k.succ) lt_wl).length (by refl),
{
rw list.take_length,
},
{
rw list.drop_length,
rw list.map_nil,
refl,
},
end
private lemma terminal_scan_aux {g : grammar T} {w : list (list T)}
(terminals : ∀ v ∈ w, ∀ t ∈ v, symbol.terminal t ∈ list.join (list.map grule.output_string g.rules)) :
grammar_derives (star_grammar g)
([R] ++ (list.map (λ v, [H] ++ v) (list.map (list.map symbol.terminal) w)).join ++ [H])
(list.map symbol.terminal w.join ++ [R, H]) :=
begin
rw list.map_map,
convert terminal_scan_ind w.length (by refl) terminals,
{
rw nat.sub_self,
rw list.take_zero,
refl,
},
{
rw nat.sub_self,
refl,
},
end
end easy_direction
section hard_direction
lemma zero_of_not_ge_one {n : ℕ} (not_pos : ¬ (n ≥ 1)) : n = 0 :=
begin
push_neg at not_pos,
rwa nat.lt_one_iff at not_pos,
end
lemma length_ge_one_of_not_nil {α : Type*} {l : list α} (lnn : l ≠ []) : l.length ≥ 1 :=
begin
by_contradiction contra,
have llz := zero_of_not_ge_one contra,
rw list.length_eq_zero at llz,
exact lnn llz,
end
private lemma nat_eq_tech {a b c : ℕ} (b_lt_c : b < c) (ass : c = a.succ + c - b.succ) :
a = b :=
begin
omega,
end
private lemma wrap_never_outputs_nt_inr {N : Type} {a : symbol T N} (i : fin 3) :
wrap_sym a ≠ symbol.nonterminal (sum.inr i) :=
begin
cases a;
unfold wrap_sym,
{
apply symbol.no_confusion,
},
intro contr,
have inl_eq_inr := symbol.nonterminal.inj contr,
exact sum.no_confusion inl_eq_inr,
end
private lemma wrap_never_outputs_Z {N : Type} {a : symbol T N} :
wrap_sym a ≠ Z :=
begin
exact wrap_never_outputs_nt_inr 0,
end
private lemma wrap_never_outputs_H {N : Type} {a : symbol T N} :
wrap_sym a ≠ H :=
begin
exact wrap_never_outputs_nt_inr 1,
end
private lemma wrap_never_outputs_R {N : Type} {a : symbol T N} :
wrap_sym a ≠ R :=
begin
exact wrap_never_outputs_nt_inr 2,
end
private lemma map_wrap_never_contains_nt_inr {N : Type} {l : list (symbol T N)} (i : fin 3) :
symbol.nonterminal (sum.inr i) ∉ list.map wrap_sym l :=
begin
intro contra,
rw list.mem_map at contra,
rcases contra with ⟨s, -, imposs⟩,
exact wrap_never_outputs_nt_inr i imposs,
end
private lemma map_wrap_never_contains_Z {N : Type} {l : list (symbol T N)} :
Z ∉ list.map wrap_sym l :=
begin
exact map_wrap_never_contains_nt_inr 0,
end
private lemma map_wrap_never_contains_H {N : Type} {l : list (symbol T N)} :
H ∉ list.map wrap_sym l :=
begin
exact map_wrap_never_contains_nt_inr 1,
end
private lemma map_wrap_never_contains_R {N : Type} {l : list (symbol T N)} :
R ∉ list.map wrap_sym l :=
begin
exact map_wrap_never_contains_nt_inr 2,
end
private lemma wrap_sym_inj {N : Type} {a b : symbol T N} (wrap_eq : wrap_sym a = wrap_sym b) :
a = b :=
begin
cases a,
{
cases b,
{
congr,
exact symbol.terminal.inj wrap_eq,
},
{
exfalso,
exact symbol.no_confusion wrap_eq,
},
},
{
cases b,
{
exfalso,
exact symbol.no_confusion wrap_eq,
},
{
congr,
unfold wrap_sym at wrap_eq,
exact sum.inl.inj (symbol.nonterminal.inj wrap_eq),
},
},
end
private lemma wrap_str_inj {N : Type} {x y : list (symbol T N)}
(wrap_eqs : list.map wrap_sym x = list.map wrap_sym y) :
x = y :=
begin
ext1,
have eqnth := congr_arg (λ l, list.nth l n) wrap_eqs,
dsimp only at eqnth,
rw list.nth_map at eqnth,
rw list.nth_map at eqnth,
cases x.nth n with xₙ,
{
cases y.nth n with yₙ,
{
refl,
},
{
exfalso,
exact option.no_confusion eqnth,
},
},
{
cases y.nth n with yₙ,
{
exfalso,
exact option.no_confusion eqnth,
},
{
congr,
apply wrap_sym_inj,
rw option.map_some' at eqnth,
rw option.map_some' at eqnth,
exact option.some.inj eqnth,
},
},
end
private lemma H_not_in_rule_input {g : grammar T} {r : grule T g.nt} :
H ∉ list.map wrap_sym r.input_L ++ [symbol.nonterminal (sum.inl r.input_N)] ++
list.map wrap_sym r.input_R :=
begin
intro contra,
rw list.mem_append at contra,
cases contra,
swap, {
exact map_wrap_never_contains_H contra,
},
rw list.mem_append at contra,
cases contra,
{
exact map_wrap_never_contains_H contra,
},
{
rw list.mem_singleton at contra,
have imposs := symbol.nonterminal.inj contra,
exact sum.no_confusion imposs,
},
end
private lemma snsri_not_in_join_mpHmmw {g : grammar T} {x : list (list (symbol T g.nt))} {i : fin 3}
(snsri_neq_H : symbol.nonterminal (sum.inr i) ≠ @H T g.nt) :
symbol.nonterminal (sum.inr i) ∉ list.join (list.map (++ [H]) (list.map (list.map wrap_sym) x)) :=
begin
intro contra,
rw list.mem_join at contra,
rw list.map_map at contra,
rcases contra with ⟨l, l_in, in_l⟩,
rw list.mem_map at l_in,
rcases l_in with ⟨y, -, eq_l⟩,
rw ←eq_l at in_l,
rw function.comp_app at in_l,
rw list.mem_append at in_l,
cases in_l,
{
exact map_wrap_never_contains_nt_inr i in_l,
},
{
rw list.mem_singleton at in_l,
exact snsri_neq_H in_l,
},
end
private lemma Z_not_in_join_mpHmmw {g : grammar T} {x : list (list (symbol T g.nt))} :
Z ∉ list.join (list.map (++ [H]) (list.map (list.map wrap_sym) x)) :=
begin
exact snsri_not_in_join_mpHmmw Z_neq_H,
end
private lemma R_not_in_join_mpHmmw {g : grammar T} {x : list (list (symbol T g.nt))} :
R ∉ list.join (list.map (++ [H]) (list.map (list.map wrap_sym) x)) :=
begin
exact snsri_not_in_join_mpHmmw H_neq_R.symm,
end
private lemma zero_Rs_in_the_long_part {g : grammar T} {x : list (list (symbol T g.nt))} [decidable_eq (ns T g.nt)] :
list.count_in (list.map (++ [H]) (list.map (list.map wrap_sym) x)).join R = 0 :=
begin
exact list.count_in_zero_of_notin R_not_in_join_mpHmmw,
end
private lemma cases_1_and_2_and_3a_match_aux {g : grammar T} {r₀ : grule T g.nt}
{x : list (list (symbol T g.nt))} {u v : list (ns T g.nt)} (xnn : x ≠ [])
(hyp : (list.join (list.map (++ [H]) (list.map (list.map wrap_sym) x))) =
u ++ list.map wrap_sym r₀.input_L ++ [symbol.nonterminal (sum.inl r₀.input_N)] ++
list.map wrap_sym r₀.input_R ++ v) :
∃ m : ℕ, ∃ u₁ v₁ : list (symbol T g.nt),
u = list.join (list.map (++ [H]) (list.take m (list.map (list.map wrap_sym) x))) ++ list.map wrap_sym u₁
∧ list.nth x m = some (u₁ ++ r₀.input_L ++ [symbol.nonterminal r₀.input_N] ++ r₀.input_R ++ v₁) ∧
v = list.map wrap_sym v₁ ++ [H] ++
list.join (list.map (++ [H]) (list.drop m.succ (list.map (list.map wrap_sym) x))) :=
begin
have hypp :
(list.map (++ [H]) (list.map (list.map wrap_sym) x)).join =
u ++ (
list.map wrap_sym r₀.input_L ++ [symbol.nonterminal (sum.inl r₀.input_N)] ++ list.map wrap_sym r₀.input_R
) ++ v,
{
simpa [list.append_assoc] using hyp,
},
have mid_brack : ∀ u', ∀ v',
u' ++ r₀.input_L ++ [symbol.nonterminal r₀.input_N] ++ r₀.input_R ++ v' =
u' ++ (r₀.input_L ++ [symbol.nonterminal r₀.input_N] ++ r₀.input_R) ++ v',
{
intros,
simp only [list.append_assoc],
},
simp_rw mid_brack,
clear hyp mid_brack,
classical,
have count_Hs := congr_arg (λ l, list.count_in l H) hypp,
dsimp only at count_Hs,
rw list.count_in_append at count_Hs,
rw list.count_in_append at count_Hs,
rw list.count_in_zero_of_notin H_not_in_rule_input at count_Hs,
rw add_zero at count_Hs,
rw [list.count_in_join, list.map_map, list.map_map] at count_Hs,
have lens := congr_arg list.length hypp,
rw list.length_append_append at lens,
rw list.length_append_append at lens,
rw list.length_singleton at lens,
have ul_lt : u.length < list.length (list.join (list.map (++ [H]) (list.map (list.map wrap_sym) x))),
{
clear_except lens,
linarith,
},
rcases list.take_join_of_lt ul_lt with ⟨m, k, mlt, klt, init_ul⟩,
have vnn : v ≠ [],
{
by_contradiction v_nil,
rw [v_nil, list.append_nil] at hypp,
clear_except hypp xnn,
have hlast := congr_arg (λ l : list (ns T g.nt), l.reverse.nth 0) hypp,
dsimp only at hlast,
rw [list.reverse_join, list.reverse_append, list.reverse_append_append, list.reverse_singleton] at hlast,
have hhh : some H = ((list.map wrap_sym r₀.input_R).reverse ++ [symbol.nonterminal (sum.inl r₀.input_N)] ++ (list.map wrap_sym r₀.input_L).reverse ++ u.reverse).nth 0,
{
convert hlast,
rw list.map_map,
change some H = (list.map (λ l, list.reverse (l ++ [H])) (list.map (list.map wrap_sym) x)).reverse.join.nth 0,
simp_rw list.reverse_append,
rw list.map_map,
change some H = (list.map (λ l, [H].reverse ++ (list.map wrap_sym l).reverse) x).reverse.join.nth 0,
rw ←list.map_reverse,
have xrnn : x.reverse ≠ [],
{
intro xr_nil,
rw list.reverse_eq_iff at xr_nil,
exact xnn xr_nil,
},
cases x.reverse with d l,
{
exfalso,
exact xrnn rfl,
},
rw [list.map_cons, list.join, list.append_assoc],
rw list.nth_append,
swap, {
rw list.length_reverse,
rw list.length_singleton,
exact one_pos,
},
rw list.reverse_singleton,
refl,
},
rw ←list.map_reverse at hhh,
cases r₀.input_R.reverse,
{
rw [list.map_nil, list.nil_append] at hhh,
simp only [list.nth, list.cons_append] at hhh,
exact sum.no_confusion (symbol.nonterminal.inj hhh),
},
{
simp only [list.nth, list.map_cons, list.cons_append] at hhh,
exact wrap_never_outputs_H hhh.symm,
},
},
have urrrl_lt :
list.length (u ++ (
list.map wrap_sym r₀.input_L ++ [symbol.nonterminal (sum.inl r₀.input_N)] ++ list.map wrap_sym r₀.input_R
)) <
list.length (list.join (list.map (++ [H]) (list.map (list.map wrap_sym) x))),
{
have vl_pos : v.length > 0,
{
exact list.length_pos_of_ne_nil vnn,
},
clear_except lens vl_pos,
rw list.length_append,
rw list.length_append_append,
rw list.length_singleton,
linarith,
},
rcases list.drop_join_of_lt urrrl_lt with ⟨m', k', mlt', klt', last_vl⟩,
have mxl : m < x.length,
{
rw list.length_map at mlt,
rw list.length_map at mlt,
exact mlt,
},
have mxl' : m' < x.length,
{
rw list.length_map at mlt',
rw list.length_map at mlt',
exact mlt',
},
have mxlmm : m < (list.map (list.map wrap_sym) x).length,
{
rwa list.length_map,
},
have mxlmm' : m' < (list.map (list.map wrap_sym) x).length,
{
rwa list.length_map,
},
use [m, list.take k (x.nth_le m mxl), list.drop k' (x.nth_le m' mxl')],
have hyp_u := congr_arg (list.take u.length) hypp,
rw list.append_assoc at hyp_u,
rw list.take_left at hyp_u,
rw init_ul at hyp_u,
rw list.nth_le_map at hyp_u,
swap, {
exact mxlmm,
},
rw list.take_append_of_le_length at hyp_u,
swap, {
rw list.nth_le_map at klt,
swap, {
exact mxlmm,
},
rw list.length_append at klt,
rw list.length_singleton at klt,
rw list.nth_le_map at klt ⊢,
iterate 2 {
swap, {
exact mxl,
},
},
rw list.length_map at klt ⊢,
rw nat.lt_succ_iff at klt,
exact klt,
},
rw ←hyp_u at count_Hs,
have hyp_v :=
congr_arg (list.drop (list.length (u ++ (
list.map wrap_sym r₀.input_L ++ [symbol.nonterminal (sum.inl r₀.input_N)] ++ list.map wrap_sym r₀.input_R
)))) hypp,
rw list.drop_left at hyp_v,
rw last_vl at hyp_v,
rw list.nth_le_map at hyp_v,
swap, {
exact mxlmm',
},
rw list.drop_append_of_le_length at hyp_v,
swap, {
rw list.nth_le_map at klt',
swap, {
exact mxlmm',
},
rw list.length_append at klt',
rw list.length_singleton at klt',
rw list.nth_le_map at klt' ⊢,
iterate 2 {
swap, {
exact mxl',
},
},
rw list.length_map at klt' ⊢,
rw nat.lt_succ_iff at klt',
exact klt',
},
rw ←hyp_v at count_Hs,
have mm : m = m',
{
clear_except count_Hs mxl mxl' klt klt',
rw [
list.count_in_append, list.count_in_append, list.map_map,
list.count_in_join, ←list.map_take, list.map_map,
list.count_in_join, ←list.map_drop, list.map_map
] at count_Hs,
change
(list.map (λ w, list.count_in (list.map wrap_sym w ++ [H]) H) x).sum =
(list.map (λ w, list.count_in (list.map wrap_sym w ++ [H]) H) (list.take m x)).sum + _ +
(_ + (list.map (λ w, list.count_in (list.map wrap_sym w ++ [H]) H) (list.drop m'.succ x)).sum)
at count_Hs,
simp_rw list.count_in_append at count_Hs,
have inside_wrap : ∀ y : list (symbol T g.nt), (list.map wrap_sym y).count_in H = 0,
{
intro,
rw list.count_in_zero_of_notin,
apply map_wrap_never_contains_H,
},
have inside_one : ∀ z : list (symbol T g.nt),
(list.map wrap_sym z).count_in (@H T g.nt) + [@H T g.nt].count_in (@H T g.nt) = 1,
{
intro,
rw list.count_in_singleton_eq H,
rw inside_wrap,
},
simp_rw inside_one at count_Hs,
repeat {
rw [list.map_const, list.sum_const_nat, one_mul] at count_Hs,
},
rw [list.length_take, list.length_drop, list.nth_le_map', list.nth_le_map'] at count_Hs,
rw min_eq_left (le_of_lt mxl) at count_Hs,
have inside_take : (list.take k (list.map wrap_sym (x.nth_le m mxl))).count_in H = 0,
{
rw ←list.map_take,
rw inside_wrap,
},
have inside_drop : (list.drop k' (list.map wrap_sym (x.nth_le m' mxl'))).count_in H + [H].count_in H = 1,
{
rw ←list.map_drop,
rw inside_wrap,
rw list.count_in_singleton_eq (@H T g.nt),
},
rw [inside_take, inside_drop] at count_Hs,
rw [add_zero, ←add_assoc, ←nat.add_sub_assoc] at count_Hs,
swap, {
rwa nat.succ_le_iff,
},
exact nat_eq_tech mxl' count_Hs,
},
rw ←mm at *,
split,
{
symmetry,
convert hyp_u,
{
rw list.map_take,
},
{
rw list.map_take,
rw list.nth_le_map,
},
},
split,
swap, {
symmetry,
convert hyp_v,
{
rw list.map_drop,
rw list.nth_le_map,
},