-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregmatch.v
1043 lines (982 loc) · 31.1 KB
/
regmatch.v
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
Require Import List.
Require Import Arith.
Require Import Wellfounded.
Require Import Relation_Definitions.
Require Import Relation_Operators.
Require Import Lia.
From mathcomp Require Import ssreflect.
From RegMatch Require Import regexp.
Import ListNotations.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Section PListDec.
Variables A B : Type.
Variable ABlt : A * B -> A * B -> Prop.
Variable P : A * B -> Prop.
Variable ab : A * B.
Variable P_dec : forall ab', ABlt ab' ab -> { P ab' }+{ ~ P ab' }.
Variable f : A -> A * B.
Hypothesis f_ABlt : forall a' : A, ABlt (f a') ab.
Definition list_t (l : list A) := { a0 | In a0 l /\ P (f a0) }+{ (forall a', In a' l -> ~ P (f a')) }.
Fixpoint P_list_dec (l : list A) : list_t l.
refine
(match l as l0 return _ = l0 -> _ with
| [] => fun H_eq_l => inright _
| a :: l' =>
fun H_eq_l =>
match @P_dec (f a) _ with
| left H_dec => inleft (exist _ a _)
| right H_dec =>
match P_list_dec l' with
| inleft (exist a' H_l') => inleft (exist _ a' _)
| inright H_l' => inright _
end
end
end (refl_equal _)).
- move => a' H_in H_p; subst.
by inversion H_in.
- subst.
exact: f_ABlt.
- subst.
by split; first by left.
- subst.
simpl in *.
move: H_l' => [H_l' H_p].
by split; first by right.
- move => a' H_in H_p.
subst.
case: H_in => H_in; first by subst.
by apply H_l' in H_in.
Defined.
End PListDec.
Section lexprod'.
Variable A : Type.
Variable ltA : A -> A -> Prop.
Inductive lexprod' : A * A -> A * A -> Prop :=
| left_lex' : forall (x1 x2 y1 y2 : A), ltA x1 x2 -> lexprod' (x1, y1) (x2, y2)
| right_lex' : forall (x y1 y2 : A), ltA y1 y2 -> lexprod' (x, y1) (x, y2).
Lemma lexprod'_Acc : well_founded ltA -> forall x, Acc ltA x -> forall y, Acc ltA y -> Acc lexprod' (x, y).
Proof.
intros H x Hx.
induction Hx as [x _ IHacc].
intros y Hy.
induction Hy as [y _ IHacc0].
apply Acc_intro.
intros (x1, y1) HA.
inversion HA; subst.
- apply IHacc; auto.
- apply IHacc0; auto.
Defined.
Theorem wf_lexprod' : well_founded ltA -> well_founded lexprod'.
Proof.
intros H_wf (x, y).
by auto using lexprod'_Acc.
Defined.
End lexprod'.
Section lexprod''.
Variable A : Type.
Variable ltA : A -> A -> Prop.
Inductive lexprod'' : A * A * A -> A * A * A -> Prop :=
| left_lex'' : forall (x1 x2 y1 y2 z1 z2 : A), ltA x1 x2 -> lexprod'' (x1, y1, z1) (x2, y2, z2)
| mid_lex'' : forall (x y1 y2 z1 z2 : A), ltA y1 y2 -> lexprod'' (x, y1, z1) (x, y2, z2)
| right_lex'' : forall (x y z1 z2 : A), ltA z1 z2 -> lexprod'' (x, y, z1) (x, y, z2).
Lemma lexprod''_Acc : well_founded ltA -> forall x, Acc ltA x -> forall y, Acc ltA y -> forall z, Acc ltA z -> Acc lexprod'' (x, y, z).
Proof.
intros H x Hx.
induction Hx as [x _ IHacc].
intros y Hy.
induction Hy as [y _ IHacc0].
intros z Hz.
induction Hz as [z _ IHacc1].
apply Acc_intro.
intros ((x1, y1), z1) HA.
inversion HA; subst; auto.
Defined.
Theorem wf_lexprod'' : well_founded ltA -> well_founded lexprod''.
Proof.
intros H_wf ((x, y), z).
by auto using lexprod''_Acc.
Defined.
End lexprod''.
Section Accept.
Variable char : Type.
Variable char_eq_dec : forall c0 c1 : char, {c0 = c1}+{c0 <> c1}.
Fixpoint regexp_size (r : r char) : nat :=
match r with
| r_zero => 1
| r_unit => 1
| r_char _ => 1
| r_plus r1 r2 => regexp_size r1 + regexp_size r2 + 1
| r_times r1 r2 => regexp_size r1 + regexp_size r2 + 1
| r_star r => regexp_size r + 1
end.
Definition regexp_size_lt (r r' : r char) := regexp_size r < regexp_size r'.
Lemma regexp_size_wf : well_founded regexp_size_lt.
Proof.
exact: (well_founded_lt_compat _ (fun r => regexp_size r)).
Defined.
Fixpoint regexp_subsize (r : r char) : nat :=
match r with
| r_times r1 r2 => regexp_size r1
| _ => 0
end.
Definition regexp_subsize_lt (r r' : r char) := regexp_subsize r < regexp_subsize r'.
Lemma regexp_subsize_wf : well_founded regexp_subsize_lt.
Proof.
exact: (well_founded_lt_compat _ (fun r => regexp_subsize r)).
Defined.
Definition regexp_lt_size_subsize_lexprod' (r r' : r char) :=
lexprod' lt (regexp_size r, regexp_subsize r) (regexp_size r', regexp_subsize r').
Lemma regexp_lt_size_subsize_lexprod'_wf : well_founded regexp_lt_size_subsize_lexprod'.
Proof.
intro.
apply (wf_inverse_image _ _ _ (fun r => (regexp_size r, regexp_subsize r))).
by apply wf_lexprod'; apply lt_wf.
Defined.
Inductive regexp_lt : r char -> r char -> Prop :=
| regexp_lt_lt : forall r r' : r char,
regexp_size r < regexp_size r' ->
regexp_lt r r'
| regexp_lt_times_lt : forall r11 r12 r21 r22 : r char,
regexp_size (r_times r11 r12) = regexp_size (r_times r21 r22) ->
regexp_size r11 < regexp_size r21 ->
regexp_lt (r_times r11 r12) (r_times r21 r22).
Lemma regexp_lt_size_subsize_symprod_incl_impl :
forall r r' : r char,
regexp_lt r r' -> regexp_lt_size_subsize_lexprod' r r'.
Proof.
move => r r'.
elim => {r r'}.
- move => r r' H_lt.
rewrite /regexp_lt_size_subsize_lexprod'.
exact: left_lex'.
- move => r11 r12 r21 r22 H_eq H_lt.
rewrite /regexp_lt_size_subsize_lexprod' H_eq /=.
exact: right_lex'.
Defined.
Lemma regexp_lt_size_subsize_symprod_incl : inclusion _ regexp_lt regexp_lt_size_subsize_lexprod'.
Proof.
move => x y.
exact: regexp_lt_size_subsize_symprod_incl_impl.
Defined.
Lemma regexp_lt_well_founded : well_founded regexp_lt.
Proof.
apply (wf_incl _ _ _ regexp_lt_size_subsize_symprod_incl).
exact: regexp_lt_size_subsize_lexprod'_wf.
Defined.
Definition regexps_no_c_lt (rc rc' : r char * char) := regexp_lt (fst rc) (fst rc').
Lemma regexps_no_c_lt_well_founded : well_founded regexps_no_c_lt.
Proof.
apply (wf_inverse_image _ _ _ (fun rs => fst rs)).
apply regexp_lt_well_founded.
Defined.
Definition regexps_no_c_t (rc : r char * char) :=
{ l : list (r char) | (forall r : r char, In r l -> (forall s, s_in_regexp_lang char s r -> s_in_regexp_c_lang char s (fst rc) (snd rc))) /\ (forall s, s_in_regexp_c_lang char s (fst rc) (snd rc) -> exists r, In r l /\ s_in_regexp_lang char s r) }.
Lemma star_times :
forall (s' : list char) c r',
s_in_regexp_lang _ (c :: s') (r_star r') ->
s_in_regexp_lang _ (c :: s') (r_times r' (r_star r')).
Proof.
case => //=.
- move => c r' H_s.
inversion H_s; subst.
destruct s5.
simpl in *.
have ->: s' = [] ++ s' by [].
by apply s_in_regexp_lang_times.
injection H => H_eq H_eq_c; subst.
destruct s5 => //=.
have ->: c :: s' = [c] ++ s' by [].
by apply s_in_regexp_lang_times.
- move => c s' c' r' H_r'.
inversion H_r'; subst.
destruct s5.
* simpl in *.
have ->: s'0 = [] ++ s'0 by [].
by apply s_in_regexp_lang_times.
* injection H => H_eq H_eq_c.
subst.
by apply s_in_regexp_lang_times.
Qed.
Lemma regexp_star_split : forall r' s' c,
s_in_regexp_lang char (c :: s') (r_star r') ->
exists s1 s2, s' = s1 ++ s2 /\ s_in_regexp_lang char (c :: s1) r' /\ s_in_regexp_lang char s2 (r_star r').
Proof.
intros.
remember (c :: s') as s0.
remember (r_star r') as r0.
revert r' s' c Heqs0 Heqr0.
induction H; intros; try congruence.
inversion Heqr0; subst; clear Heqr0.
destruct s5.
- apply IHs_in_regexp_lang2; auto.
- simpl in *. inversion Heqs0; subst; clear Heqs0.
eauto.
Qed.
Definition regexps_no_c_F : forall (rc : r char * char),
(forall rc' : r char * char, regexps_no_c_lt rc' rc -> regexps_no_c_t rc') -> regexps_no_c_t rc.
refine
(fun rc regexps_no_c_rec =>
match fst rc as r0 return _ = r0 -> _ with
| r_zero => fun H_eq => exist _ [] _
| r_unit => fun H_eq => exist _ [] _
| r_char c =>
fun H_eq =>
match char_eq_dec c (snd rc) with
| left H_a => exist _ [r_unit] _
| right H_a => exist _ [] _
end
| r_plus r1 r2 =>
fun H_eq =>
match regexps_no_c_rec (r1, snd rc) _, regexps_no_c_rec (r2, snd rc) _ with
| exist l1 H_ex1, exist l2 H_ex2 => exist _ (l1 ++ l2) _
end
| r_star r =>
fun H_eq =>
match regexps_no_c_rec (r, snd rc) _ with
| exist l H_ex =>
exist _ (map (fun r' => r_times r' (r_star r)) l) _
end
| r_times r_zero _ => fun H_eq => exist _ [] _
| r_times r_unit r2 =>
fun H_eq =>
match regexps_no_c_rec (r2, snd rc) _ with
| exist l H_ex => exist _ l _
end
| r_times (r_char c) r2 =>
fun H_eq =>
match char_eq_dec c (snd rc) with
| left H_a => exist _ [r2] _
| right H_a => exist _ [] _
end
| r_times (r_plus r11 r12) r2 =>
fun H_eq =>
match regexps_no_c_rec (r_times r11 r2, snd rc) _, regexps_no_c_rec (r_times r12 r2, snd rc) _ with
| exist l11 H_ex11, exist l12 H_ex12 => exist _ (l11 ++ l12) _
end
| r_times (r_times r11 r12) r2 =>
fun H_eq =>
match regexps_no_c_rec (r_times r11 (r_times r12 r2), snd rc) _ with
| exist l H_ex => exist _ l _
end
| r_times (r_star r1) r2 =>
fun H_eq =>
match regexps_no_c_rec (r2, snd rc) _, regexps_no_c_rec (r1, snd rc) _ with
| exist l2 H_ex2, exist l1 H_ex1 =>
exist _ (l2 ++ (map (fun r' => r_times r' (r_times (r_star r1) r2)) l1)) _
end
end (refl_equal _)); destruct rc; simpl in *; subst => //=.
- split => //.
move => s H_s.
inversion H_s; subst.
by inversion H.
- split => //.
move => s H_s.
inversion H_s; subst.
simpl in *.
by inversion H.
- split.
* move => r; case => //.
move => H_eq; subst.
move => s H_in.
inversion H_in; subst.
apply: s_in_regexp_c_lang_cs.
rewrite /=.
exact: s_in_regexp_lang_char.
* move => s' H_s.
inversion H_s; subst.
simpl in *.
inversion H; subst.
exists r_unit.
split; first by left.
exact: s_in_regexp_lang_unit.
- split => //=.
move => s' H_s.
inversion H_s; subst.
simpl in *.
by inversion H; subst.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- move: H_ex1 => [H_ex1 H_ex1'].
move: H_ex2 => [H_ex2 H_ex2'].
split.
* move => r H_in s H_s.
apply: s_in_regexp_c_lang_cs => /=.
apply in_app_or in H_in.
case: H_in => H_in.
+ have H_s' := H_ex1 _ H_in _ H_s.
inversion H_s'; subst.
simpl in *.
exact: s_in_regexp_lang_plus_1.
+ have H_s' := H_ex2 _ H_in _ H_s.
inversion H_s'; subst.
simpl in *.
exact: s_in_regexp_lang_plus_2.
* move => s' H_s'.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
* apply s_in_regexp_c_lang_cs in H2.
have [r [H_in H_ex1'']] := H_ex1' _ H2.
exists r. split => //.
by apply in_or_app; left.
* apply s_in_regexp_c_lang_cs in H2.
have [r [H_in H_ex2'']] := H_ex2' _ H2.
exists r. split => //.
by apply in_or_app; right.
- split => //.
move => s H_s.
inversion H_s; subst.
simpl in *.
inversion H; subst.
by inversion H3.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- move: H_ex => [H_ex H_ex'].
split.
* move => r' H_in s H_s.
apply: s_in_regexp_c_lang_cs => /=.
have H_s' := H_ex _ H_in _ H_s.
inversion H_s'; subst.
simpl in *.
have ->: c :: s = [] ++ c :: s by [].
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_unit.
* move => s' H_s'.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
inversion H3; subst.
simpl in *.
subst.
apply s_in_regexp_c_lang_cs in H4.
apply H_ex' in H4.
move: H4 => [r0 [H_in H_s0]].
by exists r0.
- split.
* move => r'; case => // H_eq; subst.
move => s H_s.
apply: s_in_regexp_c_lang_cs => /=.
have ->: c0 :: s = [c0] ++ s by [].
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_char.
* move => s H_s.
inversion H_s; subst.
simpl in *.
inversion H; subst.
inversion H3; subst.
simpl in *.
injection H0 => H_eq.
subst.
exists r2.
by split; first by left.
- split => //.
move => s H_s.
inversion H_s; subst.
simpl in *.
inversion H; subst.
inversion H3; subst.
simpl in *.
by injection H0.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- move: H_ex11 => [H_ex11 H_ex11'].
move: H_ex12 => [H_ex12 H_ex12'].
split.
* move => r' H_in s H_s.
apply: s_in_regexp_c_lang_cs => /=.
apply in_app_or in H_in.
case: H_in => H_in.
+ have H_s' := H_ex11 _ H_in _ H_s.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_plus_1.
+ have H_s' := H_ex12 _ H_in _ H_s.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_plus_2.
* move => s' H_s'.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
inversion H3; subst.
+ destruct s5.
-- simpl in *.
subst.
have H_sc: s_in_regexp_c_lang _ s' (r_times r11 r2) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: s' = [] ++ c :: s' by [].
by apply s_in_regexp_lang_times.
apply H_ex11' in H_sc.
move: H_sc => [r0 [H_in H_s0]].
exists r0.
split => //.
by apply in_or_app; left.
-- simpl in *.
injection H0 => H_eq H_eq_c.
subst.
have H_sc: s_in_regexp_c_lang _ (s5 ++ s'0) (r_times r11 r2) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: (s5 ++ s'0) = (c :: s5) ++ s'0 by [].
by apply s_in_regexp_lang_times.
apply H_ex11' in H_sc.
move: H_sc => [r0 [H_in H_s0]].
exists r0.
split => //.
by apply in_or_app; left.
+ destruct s5.
-- simpl in *.
subst.
have H_sc: s_in_regexp_c_lang _ s' (r_times r12 r2) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: s' = [] ++ c :: s' by [].
by apply s_in_regexp_lang_times.
apply H_ex12' in H_sc.
move: H_sc => [r0 [H_in H_s0]].
exists r0.
split => //.
by apply in_or_app; right.
-- simpl in *.
injection H0 => H_eq H_eq_c.
subst.
have H_sc: s_in_regexp_c_lang _ (s5 ++ s'0) (r_times r12 r2) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: (s5 ++ s'0) = (c :: s5) ++ s'0 by [].
by apply s_in_regexp_lang_times.
apply H_ex12' in H_sc.
move: H_sc => [r0 [H_in H_s0]].
exists r0.
split => //.
by apply in_or_app; right.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_times_lt => /=; lia.
- move: H_ex => [H_ex H_ex'].
split.
* move => r' H_in s' H_s.
apply: s_in_regexp_c_lang_cs => /=.
have H_s' := H_ex _ H_in _ H_s.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
inversion H4; subst.
rewrite app_assoc.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_times.
* move => s' H_s'.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
inversion H3; subst.
destruct s0.
+ simpl in *.
destruct s'1.
-- simpl in *.
subst.
have H_sc: s_in_regexp_c_lang _ s' (r_times r11 (r_times r12 r2)) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: s' = [] ++ c :: s' by [].
apply s_in_regexp_lang_times => //.
have ->: c :: s' = [] ++ c :: s' by [].
by apply s_in_regexp_lang_times => //.
by apply H_ex' in H_sc.
-- injection H0 => H_eq H_eq_c; subst.
have H_sc: s_in_regexp_c_lang _ (s'1 ++ s'0) (r_times r11 (r_times r12 r2)) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: (s'1 ++ s'0) = [] ++ c :: (s'1 ++ s'0) by [].
apply s_in_regexp_lang_times => //.
have ->: c :: (s'1 ++ s'0) = (c :: s'1) ++ s'0 by [].
by apply s_in_regexp_lang_times.
by apply H_ex' in H_sc.
+ simpl in *.
injection H0 => H_eq H_eq_c.
subst.
have H_sc: s_in_regexp_c_lang _ (s0 ++ (s'1 ++ s'0)) (r_times r11 (r_times r12 r2)) c.
apply s_in_regexp_c_lang_cs.
simpl.
have ->: c :: (s0 ++ s'1 ++ s'0) = (c :: s0) ++ s'1 ++ s'0 by [].
apply s_in_regexp_lang_times => //.
by apply s_in_regexp_lang_times.
apply H_ex' in H_sc.
move: H_sc => [r0 [H_in H_r0]].
exists r0.
split => //.
by rewrite -app_assoc.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- move: H_ex1 => [H_ex1 H_ex1'].
move: H_ex2 => [H_ex2 H_ex2'].
split.
* move => r' H_in s' H_s'.
apply: s_in_regexp_c_lang_cs => /=.
apply in_app_or in H_in.
case: H_in => H_in.
+ have H_s0 := H_ex2 _ H_in _ H_s'.
inversion H_s0; subst.
simpl in *.
have ->: c :: s' = [] ++ c :: s' by [].
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_star_1.
+ apply in_map_iff in H_in.
move: H_in => [r0 [H_eq_r0 H_in']].
subst.
inversion H_s'; subst.
have H_s0 := H_ex1 _ H_in' _ H2.
inversion_clear H3; subst.
inversion_clear H_s0; subst.
simpl in *.
have ->: c :: (s5 ++ s0 ++ s') = (c :: s5 ++ s0) ++ s' by rewrite app_assoc.
apply s_in_regexp_lang_times => //.
have ->: c :: s5 ++ s0 = (c :: s5) ++ s0 by [].
exact: s_in_regexp_lang_star_2.
* move => s' H_s'.
inversion H_s'; subst.
simpl in *.
inversion H; subst.
destruct s5.
+ simpl in *.
subst.
apply s_in_regexp_c_lang_cs in H4.
apply H_ex2' in H4.
move: H4 => [r0 [H_in H_r0]].
exists r0.
split => //.
apply in_or_app.
by left.
+ injection H0 => H_eq H_eq_c.
subst.
apply regexp_star_split in H3.
move: H3 => [s1 [s2 [H_eq [H_s12 H_s12']]]].
subst.
apply s_in_regexp_c_lang_cs in H_s12.
apply H_ex1' in H_s12.
move: H_s12 => [r0 [H_in H_r0]].
exists (r_times r0 (r_times (r_star r1) r2)).
split.
-- apply in_or_app.
right.
apply in_split in H_in.
move: H_in => [l'2 [l'3 H_eq]].
subst.
move {H_ex1 H_ex1'}.
elim: l'2 => //=; first by left.
move => r'0 l'.
rewrite map_app /= => H_in.
right.
apply in_or_app.
by right; left.
-- rewrite -app_assoc.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_times.
- rewrite /regexps_no_c_lt /=.
by apply regexp_lt_lt => /=; lia.
- move: H_ex => [H_ex H_ex'].
split.
* move => r' H_in s' H_s'.
apply: s_in_regexp_c_lang_cs => /=.
apply in_map_iff in H_in.
move: H_in => [r0 [H_eq_r0 H_in']].
subst.
inversion_clear H_s'; subst.
have H_ex0 := H_ex _ H_in' _ H.
inversion_clear H_ex0; subst.
simpl in *.
have ->: c :: (s5 ++ s'0) = (c :: s5) ++ s'0 by [].
exact: s_in_regexp_lang_star_2.
* move => s' H_s'.
inversion H_s'; subst.
simpl in *.
apply regexp_star_split in H.
move: H => [s1 [s2 [H_eq [H_s1 H_s2]]]].
subst.
apply s_in_regexp_c_lang_cs in H_s1.
apply H_ex' in H_s1.
move: H_s1 => [r0 [H_in H_r0]].
exists (r_times r0 (r_star r)).
split.
+ apply in_split in H_in.
move: H_in => [l1 [l2 H_eq]].
subst.
elim: l1 {H_ex' H_ex} => /=; first by left.
move => r1 l.
rewrite map_app /= => H_in.
by right.
+ exact: s_in_regexp_lang_times.
Defined.
Definition regexps_no_c : forall (rs : r char * char), regexps_no_c_t rs :=
@well_founded_induction_type _ _ regexps_no_c_lt_well_founded regexps_no_c_t regexps_no_c_F.
Inductive accept_lt : r char * list char -> r char * list char -> Prop :=
| accept_lt_string : forall rs rs' : r char * list char,
length (snd rs) < length (snd rs') ->
accept_lt rs rs'
| accept_lt_regexp : forall rs rs' : r char * list char,
length (snd rs) = length (snd rs') ->
regexp_lt (fst rs) (fst rs') ->
accept_lt rs rs'.
Definition accept_lt_lexprod'' (rs rs' : r char * list char) :=
lexprod'' lt
(length (snd rs), regexp_size (fst rs), regexp_subsize (fst rs))
(length (snd rs'), regexp_size (fst rs'), regexp_subsize (fst rs')).
Lemma accept_lt_lexprod''_wf : well_founded accept_lt_lexprod''.
Proof.
intro.
apply (wf_inverse_image _ _ _ (fun rs => (length (snd rs), regexp_size (fst rs), regexp_subsize (fst rs)))).
by apply wf_lexprod''; apply lt_wf.
Defined.
Lemma accept_lt_lexprod''_impl : forall rs rs', accept_lt rs rs' -> accept_lt_lexprod'' rs rs'.
Proof.
move => rs rs'.
elim => {rs rs'}.
- move => rs rs' H_lt.
rewrite /accept_lt_lexprod''.
exact: left_lex''.
- move => rs rs' H_eq H_lt.
rewrite /accept_lt_lexprod'' H_eq /=.
inversion H_lt; subst.
* exact: mid_lex''.
* rewrite H1.
exact: right_lex''.
Defined.
Lemma accept_lt_lexprod''_incl : inclusion _ accept_lt accept_lt_lexprod''.
Proof.
move => x y.
exact: accept_lt_lexprod''_impl.
Defined.
Lemma accept_lt_well_founded : well_founded accept_lt.
Proof.
apply (wf_incl _ _ _ accept_lt_lexprod''_incl).
exact: accept_lt_lexprod''_wf.
Defined.
Definition accept_p (rs : r char * list char) :=
s_in_regexp_lang _ (snd rs) (fst rs).
Definition accept_t (rs : r char * list char) :=
{ accept_p rs }+{ ~ accept_p rs }.
Definition accept_list_dec := @P_list_dec (r char) (list char) accept_lt accept_p.
Definition accept_F : forall rs : r char * list char,
(forall rs', accept_lt rs' rs -> accept_t rs') -> accept_t rs.
refine
(fun rs accept_rec =>
match snd rs as s0 return _ = s0 -> _ with
| [] =>
fun H_eq_s =>
match fst rs as r0 return _ = r0 -> _ with
| r_zero => fun H_eq_r => right _
| r_unit => fun H_eq_r => left _
| r_char _ => fun H_eq_r => right _
| r_plus r1 r2 =>
fun H_eq_r =>
match accept_rec (r1, []) _ with
| left H_r1 => left _
| right H_r1 =>
match accept_rec (r2, []) _ with
| left H_r2 => left _
| right H_r2 => right _
end
end
| r_times r1 r2 =>
fun H_eq_r =>
match accept_rec (r1, []) _ with
| left H_r1 =>
match accept_rec (r2, []) _ with
| left H_r2 => left _
| right H_r2 => right _
end
| right H_r1 => right _
end
| r_star r' => fun H_eq_r => left _
end (refl_equal _)
| c :: s' =>
fun H_eq_s =>
match fst rs as r0 return _ = r0 -> _ with
| r_zero => fun H_eq_r => right _
| r_unit => fun H_eq_r => right _
| r_char c' =>
fun H_eq_r =>
match s' as s1 return s' = s1 -> _ with
| [] =>
fun H_eq_s' =>
match char_eq_dec c c' with
| left H_c => left _
| right H_c => right _
end
| _ => fun H_eq_s' => right _
end (refl_equal _)
| r_plus r1 r2 =>
fun H_eq_r =>
match accept_rec (r1, c :: s') _ with
| left H_r1 => left _
| right H_r1 =>
match accept_rec (r2, c :: s') _ with
| left H_r2 => left _
| right H_r2 => right _
end
end
| r_times r_unit r2 =>
fun H_eq_r =>
match accept_rec (r2, c :: s') _ with
| left H_r2 => left _
| right H_r2 => right _
end
| r_times (r_char c') r2 =>
fun H_eq_r =>
match char_eq_dec c c' with
| left H_c =>
match accept_rec (r2, s') _ with
| left H_r2 => left _
| right H_r2 => right _
end
| right H_c => right _
end
| r_times (r_times r11 r12) r2 =>
fun H_eq_r =>
match accept_rec (r_times r11 (r_times r12 r2), c :: s') _ with
| left H_r => left _
| right H_r => right _
end
| r_times (r_plus r11 r12) r2 =>
fun H_eq_r =>
match accept_rec (r_times r11 r2, c :: s') _ with
| left H_r11 => left _
| right H_r11 =>
match accept_rec (r_times r12 r2, c :: s') _ with
| left H_r12 => left _
| right H_r12 => right _
end
end
| r_times (r_star r1) r2 =>
fun H_eq_r =>
match accept_rec (r2, c :: s') _ with
| left H_r2 => left _
| right H_r2 =>
match regexps_no_c (r1, c) with
| exist l H_l =>
match @accept_list_dec rs accept_rec (fun r0 => (r_times r0 (r_times (r_star r1) r2), s')) _ l with
| inleft (exist _ H_ex) => left _
| inright H_l' => right _
end
end
end
| r_star r' =>
fun H_eq_r =>
match regexps_no_c (r', c) with
| exist l H_l =>
match @accept_list_dec rs accept_rec (fun r0 => (r_times r0 (r_star r'), s')) _ l with
| inleft (exist _ H_ex) => left _
| inright H_l' => right _
end
end
| r_times r_zero _ =>
fun H_eq_r => right _
end (refl_equal _)
end (refl_equal _)); destruct rs; simpl in *; subst.
- by move => H_m; inversion H_m.
- exact: s_in_regexp_lang_unit.
- by move => H_m; inversion H_m.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- exact: s_in_regexp_lang_plus_1.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- exact: s_in_regexp_lang_plus_2.
- by move => H_m; inversion H_m; subst.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- exact: s_in_regexp_lang_times _ _ _ _ H_r1 H_r2.
- move => H_m; inversion H_m; subst.
case: H_r2.
by destruct s5, s'.
- move => H_m; inversion H_m; subst.
by destruct s5.
- by apply s_in_regexp_lang_star_1.
- by move => H_m; inversion H_m.
- by move => H_m; inversion H_m.
- exact: s_in_regexp_lang_char.
- by move => H_m; inversion H_m; subst.
- by move => H_m; inversion H_m.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- exact: s_in_regexp_lang_plus_1.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- exact: s_in_regexp_lang_plus_2.
- by move => H_m; inversion H_m.
- move => H_m; inversion H_m; subst.
by inversion H2.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- have H_eq: [] ++ c :: s' = c :: s' by [].
rewrite -H_eq.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_unit.
- move => H_s.
inversion H_s; subst.
inversion H2; subst.
simpl in *.
subst.
by contradict H_r2.
- exact: accept_lt_string.
- have H_eq: [c'] ++ s' = c' :: s' by [].
rewrite -H_eq.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_char.
- move => H_m; inversion H_m; subst.
inversion H2; subst.
injection H => H_eq.
by subst.
- move => H_m; inversion H_m; subst.
inversion H2; subst.
injection H => H_eq H_eq'.
by subst.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- inversion H_r11; subst.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_plus_1.
- apply accept_lt_regexp => //.
apply regexp_lt_lt.
rewrite /=.
by lia.
- inversion H_r12; subst.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_plus_2.
- move => H_m; inversion H_m; subst.
inversion H2; subst.
* contradict H_r11.
rewrite -H.
exact: s_in_regexp_lang_times.
* contradict H_r12.
rewrite -H.
exact: s_in_regexp_lang_times.
- apply accept_lt_regexp => //.
by apply regexp_lt_times_lt => /=; lia.
- inversion H_r; subst.
inversion H3; subst.
rewrite app_assoc.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_times.
- move => H_s; inversion H_s; subst.
inversion H2; subst.
contradict H_r.
rewrite -app_assoc in H.
rewrite -H.
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_times.
- apply accept_lt_regexp => //=.
by apply regexp_lt_lt => /=; lia.
- rewrite /accept_p /= in H_r2.
rewrite /accept_p /=.
have ->: c :: s' = [] ++ c :: s' by [].
apply s_in_regexp_lang_times => //.
exact: s_in_regexp_lang_star_1.
- move => r0.
exact: accept_lt_string.
- move: H_ex => [H_in H_acc].
rewrite /accept_p /= in H_acc,H_r2.
rewrite /accept_p /=.
move {s}.
move: H_l => [H_l H_l'].
have H_l0 := H_l _ H_in.
inversion H_acc; subst.
inversion H3; subst.
clear H_l'.
apply H_l0 in H2.
inversion H2; subst.
simpl in *.
have ->: c :: s5 ++ s0 ++ s' = ((c :: s5) ++ s0) ++ s' by rewrite app_assoc.
apply s_in_regexp_lang_times => //.
by apply s_in_regexp_lang_star_2.
- rewrite /accept_p /=.
rewrite /accept_p /= in H_r2.
move: H_l => [H_l H_l0].
move => H_s.
inversion H_s; subst.
destruct s5.
* simpl in *.
by subst.
* injection H => H_eq H_eq_c.
subst.
clear H.
apply regexp_star_split in H2.
move: H2 => [s1 [s2 [H_s1 [H_s2 H_eq]]]].
subst.
have H_sc: s_in_regexp_c_lang _ s1 r1 c by apply s_in_regexp_c_lang_cs.
apply H_l0 in H_sc.
move: H_sc => [r0 [H_in H_r0]].
have H_l'' := H_l' _ H_in.
case: H_l''.
rewrite /accept_p /=.