-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdelmannOriginal.v
1276 lines (1192 loc) · 35.2 KB
/
EdelmannOriginal.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
(* Modified from Romain Edelmann's Coq formalisation
https://github.com/epfl-lara/silex-proofs/blob/master/Zippers.v *)
Require Export List ListSet Ascii Bool.
Require Extraction.
Require Import Regex.
Import ListNotations.
(* Tell Coq to genearate Boolean equality for lists +
proof of decidability for list equality *)
Scheme Equality for list.
(***** CHARACTERS AND WORDS *****)
Global Declare Scope char_class_scope.
Open Scope char_class_scope.
(* Input characters. *)
Definition char := ascii.
Definition char_dec := ascii_dec.
(* Input words. *)
Definition word := list char.
(***** REGULAR EXPRESSIONS *****)
(* Semantics of regular expressions as
* a predicate on words.
*)
Inductive matches: re -> word -> Prop :=
| MEps : matches Epsilon []
| MChar (cl: char) (c : char):
Ascii.eqb cl c = true ->
matches (Atom cl) [c]
| MDisL (l r: re) (w: word):
matches l w ->
matches (Union l r) w
| MDisR (l r: re) (w: word):
matches r w ->
matches (Union l r) w
| MSeq (l r: re) (wl wr: word):
matches l wl ->
matches r wr ->
matches (Concat l r) (wl ++ wr)
| MRepO (e: re):
matches (Star e) []
| MRepS (e: re) (we wr: word):
matches e we ->
matches (Star e) wr ->
matches (Star e) (we ++ wr).
Local Hint Constructors matches : matches.
(* Regular expressions can be compared for equality. *)
Lemma re_eq_dec : forall (e1 e2: re),
{e1 = e2} + {e1 <> e2}.
Proof.
repeat decide equality.
Qed.
(* Boolean equality for regexes *)
Fixpoint regex_eqb (r1 r2 : re) : bool :=
match (r1, r2) with
| (Void, Void) | (Epsilon, Epsilon) =>
true
| (Atom c1, Atom c2) =>
Ascii.eqb c1 c2
| (Union r11 r12, Union r21 r22)
| (Concat r11 r12, Concat r21 r22) =>
regex_eqb r11 r21 && regex_eqb r21 r22
| (Star r1', Star r2') => regex_eqb r1' r2'
| (_, _) => false
end.
(* Unfold one non-empty instance from
* a Star's matches.
*)
Lemma MRep1 : forall e w,
matches (Star e) w ->
w <> [] ->
exists w1 w2,
w = w1 ++ w2 /\
w1 <> [] /\
matches e w1 /\
matches (Star e) w2.
Proof.
intros.
remember (Star e) as re in H.
induction H; inversion Heqre.
{ contradiction. }
subst.
destruct we eqn:W.
- simpl in *.
apply IHmatches2; eauto.
- exists we, wr.
repeat split; subst; eauto.
Qed.
(* Does the expression e accept the empty string? *)
Fixpoint nullable (e: re) : bool :=
match e with
| Void => false
| Epsilon => true
| Atom _ => false
| Union l r => orb (nullable l) (nullable r)
| Concat l r => andb (nullable l) (nullable r)
| Star _ => true
end.
(* Computation of nullable is correct with respect to semantics. *)
Theorem nullable_matches : forall e,
nullable e = true <-> matches e [].
Proof.
induction e; split; intros; simpl in *; eauto with matches.
- inversion H.
- inversion H.
- inversion H.
- inversion H.
- apply Bool.orb_true_iff in H.
destruct H.
+ apply MDisL. apply IHe1. assumption.
+ apply MDisR. apply IHe2. assumption.
- inversion H; subst; apply Bool.orb_true_iff.
+ left. apply IHe1. assumption.
+ right. apply IHe2. assumption.
- apply Bool.andb_true_iff in H as [H1 H2].
rewrite <- app_nil_l.
constructor.
+ apply IHe1. assumption.
+ apply IHe2. assumption.
- apply Bool.andb_true_iff.
inversion H; subst.
apply app_eq_nil in H2 as [LN RN]; subst.
split.
+ apply IHe1. assumption.
+ apply IHe2. assumption.
Qed.
(***** CONTEXTS AND ZIPPERS *****)
(* Contexts are sequences of regular expressions. *)
Definition context := list re.
(* The semantics of contexts. *)
Inductive context_matches : context -> word -> Prop :=
| MCNil : context_matches [] []
| MCCons e ctx we wctx :
matches e we ->
context_matches ctx wctx ->
context_matches (e :: ctx) (we ++ wctx).
Local Hint Constructors context_matches : context_matches.
(* Contexts can be compared for equality. *)
Lemma context_eq_dec : forall (ctx1 ctx2: context),
{ctx1 = ctx2} + {ctx1 <> ctx2}.
Proof.
repeat decide equality.
Qed.
(* Boolean equality for contexts *)
Definition context_eqb (xs : context) (ys : context) : bool :=
list_beq re regex_eqb xs ys.
(* Find the first non-empty instance given a context's match. *)
Lemma context_matches_non_empty : forall ctx w,
context_matches ctx w ->
w <> [] ->
exists ctx1 e ctx2 we wctx2,
ctx = ctx1 ++ (e :: ctx2) /\
w = we ++ wctx2 /\
we <> [] /\
(forall e0, In e0 ctx1 -> matches e0 []) /\
matches e we /\
context_matches ctx2 wctx2.
Proof.
intros ctx w H.
induction H; intros.
- contradiction.
- destruct we.
+ simpl in *.
specialize (IHcontext_matches H1) as [ctx1 [e' [ctx2 [we [wctx2 C]]]]].
destruct C as [Eqctx [Eqw [Ne [N1 [Me' MC]]]]].
exists (e :: ctx1), e', ctx2, we, wctx2.
repeat split.
* subst. simpl. reflexivity.
* assumption.
* assumption.
* intros. inversion H2; subst.
{ assumption. }
apply N1. assumption.
* assumption.
* assumption.
+ exists [], e, ctx, (c :: we), wctx.
repeat split.
* intros A. inversion A.
* intros e0 A. inversion A.
* assumption.
* assumption.
Qed.
(* Zippers are disjunctions of contexts. *)
Definition zipper := set context.
(* The semantics of zippers. *)
Definition zipper_matches z w : Prop :=
exists ctx, set_In ctx z /\ context_matches ctx w.
(* Union of two zippers. *)
Definition zipper_union := set_union context_eq_dec.
(* Addition of a context in a zipper. *)
Definition zipper_add := set_add context_eq_dec.
(* Convert a regular expression into a zipper. *)
Definition focus (e: re): zipper := [[e]].
(* Correctness of focus. *)
Theorem focus_correct : forall e w,
matches e w <-> zipper_matches (focus e) w.
Proof.
unfold focus, zipper_matches; intros.
split; intros.
- exists [e]. split.
+ left. reflexivity.
+ rewrite <- app_nil_r.
apply MCCons; eauto with context_matches.
- destruct H as [ctx [I M]].
inversion M; inversion I; subst.
+ inversion H1.
+ inversion H1.
+ inversion H3; subst.
inversion H0; subst.
rewrite app_nil_r.
assumption.
+ inversion H3.
Qed.
(* Conversion from zipper back to re.
* Unused, but provides some intuition on zippers.
*)
Definition unfocus (z: zipper): re :=
let ds := map (fun ctx => fold_right Concat Epsilon ctx) z in
fold_right Union Void ds.
(* Correctness of unfocus. *)
Theorem unfocus_correct : forall z w,
zipper_matches z w <-> matches (unfocus z) w.
Proof.
split.
- intros. destruct H as [ctx [I M]].
unfold unfocus.
induction z; inversion I; subst; simpl in *.
+ apply MDisL.
clear I IHz z.
generalize dependent w.
induction ctx; intros; inversion M; subst; simpl; constructor.
* assumption.
* apply IHctx. assumption.
+ apply MDisR.
apply IHz.
apply H.
- generalize dependent w.
unfold unfocus.
induction z; intros; simpl in *; inversion H; subst.
+ exists a.
split.
* left. reflexivity.
* clear IHz H z.
generalize dependent w.
induction a; intros; simpl in *; inversion H3; subst.
{ constructor. }
constructor; try assumption.
apply IHa. assumption.
+ specialize (IHz w H3).
destruct IHz as [ctx [I M]].
exists ctx.
split.
* right. assumption.
* assumption.
Qed.
Theorem unfocus_focus :
forall e w,
matches (unfocus (focus e)) w <-> matches e w.
Proof.
intros e w.
rewrite <- unfocus_correct.
rewrite <- focus_correct.
reflexivity.
Qed.
(***** DERIVATION *****)
(* Downwards phase of Brzozowski's derivation on zippers. *)
Fixpoint derive_down (c : char) (e : re) (ctx : context) : zipper :=
match e with
| Atom cl => if Ascii.eqb cl c then [ctx] else []
| Union l r => zipper_union (derive_down c l ctx) (derive_down c r ctx)
| Concat l r => if (nullable l)
then
zipper_union (derive_down c l (r :: ctx)) (derive_down c r ctx)
else
derive_down c l (r :: ctx)
| Star e' => derive_down c e' (e :: ctx)
| _ => []
end.
(* Upwards phase of Brzozowski's derivation on zippers. *)
Fixpoint derive_up (c: char) (ctx: context) : zipper :=
match ctx with
| [] => []
| e :: ctx' => if nullable e
then
zipper_union (derive_down c e ctx') (derive_up c ctx')
else
derive_down c e ctx'
end.
(* Brzozowski's derivation on zippers. *)
Definition derive (c: char) (z: zipper): zipper :=
fold_right zipper_union [] (map (derive_up c) z).
(* Soundness of the downwards phase. *)
Lemma derive_down_sound : forall e ctx c w,
zipper_matches (derive_down c e ctx) w ->
exists we wctx,
w = we ++ wctx /\
matches e (c :: we) /\
context_matches ctx wctx.
Proof.
induction e; intros; simpl in *; destruct H as [ctx' [I' M']].
+ inversion I'.
+ inversion I'.
+ destruct ((c =? c0)%char) eqn:N.
- subst.
exists [], w.
repeat split.
* constructor. assumption.
* inversion I'; subst.
{ assumption. }
inversion H.
- inversion I'.
+ apply set_union_elim in I' as [IL | IR].
- unshelve epose proof IHe1 ctx c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
exists we, wctx.
repeat split.
* assumption.
* apply MDisL. assumption.
* assumption.
- unshelve epose proof IHe2 ctx c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
exists we, wctx.
repeat split.
* assumption.
* apply MDisR. assumption.
* assumption.
+ destruct (nullable e1) eqn:N.
apply set_union_elim in I' as [IL | IR].
- unshelve epose proof IHe1 (e2 :: ctx) c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
inversion MC; subst.
exists (we ++ we0), wctx0.
repeat split.
* rewrite app_assoc. reflexivity.
* rewrite app_comm_cons.
apply MSeq; assumption.
* assumption.
- unshelve epose proof IHe2 ctx c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
exists we, wctx.
repeat split.
* assumption.
* rewrite <- app_nil_l.
apply MSeq.
{ apply nullable_matches. assumption. }
assumption.
* assumption.
- unshelve epose proof IHe1 (e2 :: ctx) c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
inversion MC; subst.
exists (we ++ we0), wctx0.
repeat split.
* rewrite app_assoc. reflexivity.
* rewrite app_comm_cons.
apply MSeq; assumption.
* assumption.
+ unshelve epose proof IHe (Star e :: ctx) c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
inversion MC; subst.
exists (we ++ we0), wctx0.
repeat split.
- rewrite app_assoc. reflexivity.
- rewrite app_comm_cons. apply MRepS; assumption.
- assumption.
Qed.
(* Soundness of the upwards phase. *)
Lemma derive_up_sound : forall ctx c w,
zipper_matches (derive_up c ctx) w ->
context_matches ctx (c :: w).
Proof.
induction ctx; intros; simpl in *.
{ destruct H as [ctx' [A _]]. inversion A. }
destruct H as [ctx' [I' M']].
destruct (nullable a) eqn:N.
+ apply set_union_elim in I' as [IL | IR].
- unshelve epose proof derive_down_sound a ctx c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
subst.
rewrite app_comm_cons.
apply MCCons; assumption.
- unshelve epose proof IHctx c w _.
{ exists ctx'. split; assumption. }
rewrite <- app_nil_l.
apply MCCons.
* apply nullable_matches; assumption.
* assumption.
+ unshelve epose proof derive_down_sound a ctx c w _.
{ exists ctx'. split; assumption. }
destruct H as [we [wctx [Eqw [Me MC]]]].
subst.
rewrite app_comm_cons.
apply MCCons; assumption.
Qed.
(* Soundness of derivation.
*
* When a derivative of a zipper by a character matches a word,
* the original zipper matches that word prefixed
* by that character.
*)
Lemma derive_sound : forall z c w,
zipper_matches (derive c z) w ->
zipper_matches z (c :: w).
Proof.
induction z; intros;
destruct H as [ctx' [I' M']];
unfold derive in *; simpl in *;
try contradiction.
apply set_union_elim in I' as [IL | IR].
+ unshelve epose proof derive_up_sound a c w _.
{ exists ctx'. split; assumption. }
exists a.
split.
- left. reflexivity.
- assumption.
+ unshelve epose proof IHz c w _.
{ exists ctx'. split; assumption. }
destruct H as [ctx [I M]].
exists ctx.
split.
- right. assumption.
- assumption.
Qed.
(* Completeness of the downwards phase. *)
Lemma derive_down_complete : forall e ctx c we wctx,
matches e (c :: we) ->
context_matches ctx wctx ->
zipper_matches (derive_down c e ctx) (we ++ wctx).
Proof.
induction e; intros.
- inversion H.
- inversion H.
- inversion H; subst.
simpl. rewrite H3.
unfold zipper_matches.
exists ctx. split.
+ left. reflexivity.
+ assumption.
- simpl.
inversion H; subst.
+ specialize (IHe1 ctx c we wctx H4 H0).
unfold zipper_matches in IHe1.
destruct IHe1 as [ctx' [I' M']].
exists ctx'.
split.
* apply set_union_intro1. assumption.
* assumption.
+ specialize (IHe2 ctx c we wctx H4 H0).
unfold zipper_matches in IHe2.
destruct IHe2 as [ctx' [I' M']].
exists ctx'.
split.
* apply set_union_intro2. assumption.
* assumption.
- inversion H; subst.
destruct wl.
+ simpl.
assert (N: nullable e1 = true).
{ apply nullable_matches. assumption. }
rewrite N.
simpl in *.
subst.
specialize (IHe2 ctx c we wctx H5 H0).
destruct IHe2 as [ctx' [I' M']].
exists ctx'.
split.
* apply set_union_intro2. assumption.
* assumption.
+ simpl.
unshelve epose proof (IHe1 (e2 :: ctx) c0 wl (wr ++ wctx) H4 _).
{ apply MCCons; assumption. }
destruct H1 as [ctx' [I' M']].
exists ctx'.
simpl in H3.
inversion H3.
subst.
split.
{ destruct (nullable e1).
- apply set_union_intro1. apply I'.
- apply I'.
}
rewrite <- app_assoc.
apply M'.
- simpl.
apply MRep1 in H.
+ destruct H as [w1 [w2 [Eqw [Nn1 [M1 M2]]]]].
destruct w1.
{ contradiction. }
simpl in Eqw.
inversion Eqw; subst.
unshelve epose proof (IHe (Star e :: ctx) c0 w1 (w2 ++ wctx) M1 _).
{ apply MCCons; assumption. }
rewrite <- app_assoc.
apply H.
+ intros A. inversion A.
Qed.
(* Completeness of the upwards phase. *)
Lemma derive_up_complete : forall ctx c w,
context_matches ctx (c :: w) ->
zipper_matches (derive_up c ctx) w.
Proof.
induction ctx; intros; inversion H; subst.
destruct we.
{ simpl in *.
destruct (nullable a) eqn:N.
+ subst.
specialize (IHctx c w H4) as [ctx' [I' M']].
exists ctx'.
split.
- apply set_union_intro2. assumption.
- assumption.
+ rewrite <- nullable_matches in H3.
rewrite H3 in N.
inversion N. }
inversion H2.
subst.
simpl.
pose proof derive_down_complete a ctx c we wctx H3 H4.
destruct H0 as [ctx' [I' M']].
exists ctx'.
destruct (nullable a) eqn:N.
+ split.
- apply set_union_intro1. assumption.
- assumption.
+ split; assumption.
Qed.
(* Completeness of derivation.
*
* When a derivative of a zipper by a character matches a word,
* the original zipper matches that word prefixed
* by that character.
*)
Lemma derive_complete : forall z c w,
zipper_matches z (c :: w) ->
zipper_matches (derive c z) w.
Proof.
induction z; intros;
destruct H as [ctx [I M]].
{ inversion I. }
inversion I; subst.
- pose proof derive_up_complete ctx c w M.
destruct H as [ctx' [I' H']].
exists ctx'.
split.
+ unfold derive.
simpl.
apply set_union_intro1.
assumption.
+ assumption.
- unshelve epose proof IHz c w _.
{ exists ctx. split; assumption. }
destruct H0 as [ctx' [I' M']].
exists ctx'.
split.
+ unfold derive. simpl.
apply set_union_intro2.
apply I'.
+ assumption.
Qed.
(* Correctness of derivation. *)
Theorem derive_correct : forall z c w,
zipper_matches z (c :: w) <->
zipper_matches (derive c z) w.
Proof.
intuition auto;
eauto using derive_sound, derive_complete.
Qed.
Theorem derive_correct_unfocus:
forall z c w,
matches (unfocus (derive c z)) w <->
matches (unfocus z) (c :: w).
Proof.
intros.
repeat rewrite <- unfocus_correct.
split; apply derive_correct.
Qed.
(* Generalisation of derivatives to words. *)
Fixpoint derive_word w z :=
match w with
| [] => z
| c :: w' => derive_word w' (derive c z)
end.
(* Correctness of derivation generalised to words. *)
Theorem derive_word_correct : forall z w1 w2,
zipper_matches z (w1 ++ w2) <->
zipper_matches (derive_word w1 z) w2.
Proof.
intros z w1.
generalize dependent z.
induction w1; intros; simpl in *.
{ reflexivity. }
split; intros.
+ apply IHw1. apply derive_correct. assumption.
+ apply derive_correct. apply IHw1. assumption.
Qed.
Theorem derive_word_correct_unfocus:
forall z w1 w2,
matches (unfocus (derive_word w1 z)) w2 <->
matches (unfocus z) (w1 ++ w2).
Proof.
intros.
repeat rewrite <- unfocus_correct.
split; apply derive_word_correct.
Qed.
(* Does the zipper z accept the empty word? *)
Definition zipper_nullable (z : zipper) : bool :=
existsb (fun ctx => forallb nullable ctx) z.
(* Correctness of nullability checks on zippers. *)
Theorem zipper_nullable_correct : forall z,
zipper_nullable z = true <->
zipper_matches z [].
Proof.
intros z.
split.
- intros.
unfold zipper_nullable in H.
rewrite existsb_exists in H.
destruct H as [ctx [I F]].
rewrite forallb_forall in F.
exists ctx.
split.
{ assumption. }
clear I. clear z.
induction ctx; intros.
+ apply MCNil.
+ rewrite <- app_nil_l.
apply MCCons.
* apply nullable_matches.
apply F. left. reflexivity.
* apply IHctx.
intros e H. apply F. right. assumption.
- intros. destruct H as [ctx [I M]].
unfold zipper_nullable.
rewrite existsb_exists.
exists ctx.
split.
{ assumption. }
clear I. clear z.
remember [] as w in M.
induction M.
+ simpl. reflexivity.
+ simpl. rewrite Bool.andb_true_iff.
apply app_eq_nil in Heqw as [W WC].
subst.
split.
* apply nullable_matches. assumption.
* apply IHM; eauto.
Qed.
Theorem nullable_correct_unfocus:
forall z,
zipper_nullable z = true <->
matches (unfocus z) [].
Proof.
intros.
rewrite <- unfocus_correct.
split; apply zipper_nullable_correct.
Qed.
(* Does the zipper z accept the word w? *)
Definition zipper_accepts (z : zipper) (w : word) : bool :=
zipper_nullable (derive_word w z).
(* Correctness of the zipper recogniser. *)
Theorem zipper_accepts_correct : forall z w,
zipper_accepts z w = true <->
zipper_matches z w.
Proof.
intros.
unfold zipper_accepts.
rewrite zipper_nullable_correct.
pose proof derive_word_correct z w [].
rewrite app_nil_r in H.
symmetry.
apply H.
Qed.
(* Does the regular expression e accept the word w?
* Finds out using zippers!
*)
Definition accepts (e : re) (w : list char) : bool :=
zipper_accepts (focus e) w.
(* Unit test: does the character literal ['c'] match [Atom 'c']? *)
(* Compute (accepts (Atom "c"%char) ["c"%char]). *)
(* Correctness of the zipper-based recogniser. *)
Theorem accepts_correct : forall e w,
accepts e w = true <->
matches e w.
Proof.
intros.
rewrite focus_correct.
unfold accepts.
apply zipper_accepts_correct.
Qed.
(***** FINITENESS *****)
(* Downwards phase of the maximal zipper. *)
Fixpoint max_zipper_down (e: re) (ctx: context): set context :=
match e with
| Atom _ => [ctx]
| Union l r => zipper_union (max_zipper_down l ctx) (max_zipper_down r ctx)
| Concat l r => zipper_union (max_zipper_down l (r :: ctx)) (max_zipper_down r ctx)
| Star e' => max_zipper_down e' (e :: ctx)
| _ => []
end.
(* Upwards phase of the maximal zipper. *)
Fixpoint max_zipper_up (ctx: context): set context :=
match ctx with
| [] => []
| e :: ctx' => zipper_union (max_zipper_down e ctx') (max_zipper_up ctx')
end.
(* Maximal zipper. We shall prove that all derivatives are subsets of this. *)
Definition max_zipper (z: zipper): set context :=
fold_right zipper_union [] (map max_zipper_up z).
(* Derivation's downwards phase's result is included as part of the
* maximal zipper computation's downwards phase's result.
*)
Lemma derive_max_zipper_down_incl : forall e ectx ctx c,
set_In ctx (derive_down c e ectx) -> set_In ctx (max_zipper_down e ectx).
Proof.
induction e; intros; simpl in *; try contradiction.
- destruct ((c =? c0)%char); inversion H; subst.
+ left. reflexivity.
+ inversion H0.
- apply set_union_elim in H as [H | H].
+ apply set_union_intro1. apply IHe1 with c. assumption.
+ apply set_union_intro2. apply IHe2 with c. assumption.
- destruct (nullable e1) eqn:N.
apply set_union_elim in H as [H | H].
+ apply set_union_intro1. apply IHe1 with c. assumption.
+ apply set_union_intro2. apply IHe2 with c. assumption.
+ apply set_union_intro1. apply IHe1 with c. assumption.
- apply IHe with c. assumption.
Qed.
(* Derivation's upwards phase's result is included as part of the
* maximal zipper computation's upwards phase's result.
*)
Lemma derive_max_zipper_up_incl : forall ectx ctx c,
set_In ctx (derive_up c ectx) -> set_In ctx (max_zipper_up ectx).
Proof.
induction ectx; intros; simpl in *; try contradiction.
destruct (nullable a) eqn:N.
apply set_union_elim in H as [H | H].
- apply set_union_intro1.
apply derive_max_zipper_down_incl with c.
assumption.
- apply set_union_intro2.
apply IHectx with c.
assumption.
- apply set_union_intro1.
apply derive_max_zipper_down_incl with c.
assumption.
Qed.
(* A zipper's (single-step) derivatives are included in its maximal zipper. *)
Theorem derive_max_zipper_incl : forall z c ctx,
set_In ctx (derive c z) -> set_In ctx (max_zipper z).
Proof.
induction z; intros; simpl in *; try contradiction.
unfold derive in *.
unfold max_zipper.
simpl in *.
apply set_union_elim in H as [H | H].
+ apply set_union_intro1.
apply derive_max_zipper_up_incl with c.
assumption.
+ apply set_union_intro2.
apply IHz with c.
assumption.
Qed.
(* Lemma on membership in a mapped zipper addition. *)
Lemma map_zipper_add {A} :
forall z f (x : A) ctx,
In x (map f (zipper_add ctx z)) ->
(f ctx = x \/ In x (map f z)).
Proof.
induction z; intros; simpl in *.
- assumption.
- destruct (context_eq_dec ctx a); subst.
+ inversion H.
* left. assumption.
* right. right. assumption.
+ simpl in *.
inversion H.
* right. left. assumption.
* specialize (IHz f x ctx H0) as [I | I];
eauto.
Qed.
(* Lemma on membership in a mapped zipper union. *)
Lemma map_zipper_union {A} :
forall z1 z2 f (x : A),
In x (map f (zipper_union z1 z2)) ->
(In x (map f z1) \/ In x (map f z2)).
Proof.
intros z1 z2.
generalize dependent z1.
induction z2; intros; simpl in *.
+ left. assumption.
+ apply map_zipper_add in H as [H | H].
- right. left. assumption.
- specialize (IHz2 z1 f x H) as [I | I]; eauto.
Qed.
(* Elimination of unions of zippers. *)
Lemma zippers_union_elim :
forall zs ctx,
set_In ctx (fold_right zipper_union [] zs) ->
exists z,
In z zs /\
set_In ctx z.
Proof.
induction zs; intros; simpl in *; try contradiction.
apply set_union_elim in H as [H | H].
- exists a. split.
+ left. reflexivity.
+ assumption.
- specialize (IHzs ctx H) as [z [I1 I2]].
exists z. split.
+ right. assumption.
+ assumption.
Qed.
(* Introduction of unions of zippers. *)
Lemma zippers_union_intro :
forall zs z ctx,
In z zs ->
set_In ctx z ->
set_In ctx (fold_right zipper_union [] zs).
Proof.
induction zs; intros; simpl in *; try contradiction.
destruct H as [H | H]; subst.
- apply set_union_intro1. assumption.
- specialize (IHzs z ctx H H0).
apply set_union_intro2. assumption.
Qed.
(* Monotonicity of the maximal zipper's upwards phase
* with respect to (single-step) derivation.
*)
Theorem derive_max_zipper_down_mono : forall e ectx ctx c,
set_In ctx (max_zipper (derive_down c e ectx)) ->
set_In ctx (max_zipper [e :: ectx]).
Proof.
induction e; intros; simpl in *; try contradiction.
- destruct ((c =? c0)%char); subst; simpl in *.
+ unfold max_zipper in *.
simpl in *.
apply set_union_intro2. apply H.
+ contradiction.
- unfold max_zipper in H.
apply zippers_union_elim in H as [z [I1 I2]].
apply map_zipper_union in I1 as [I1 | I1].
+ unshelve epose proof IHe1 ectx ctx c _.
{ apply zippers_union_intro with z; assumption. }
unfold max_zipper in H.
apply zippers_union_elim in H as [z' [I1' I2']].
destruct I1' as [I1' | A]; try contradiction.
subst.
simpl in I2'.
unfold max_zipper. simpl.
apply set_union_elim in I2' as [I2' | I2'].
* apply set_union_intro1. apply set_union_intro1.
assumption.
* apply set_union_intro2. assumption.
+ unshelve epose proof IHe2 ectx ctx c _.
{ apply zippers_union_intro with z; assumption. }
unfold max_zipper in H.
apply zippers_union_elim in H as [z' [I1' I2']].
destruct I1' as [I1' | A]; try contradiction.
subst.
simpl in I2'.
unfold max_zipper. simpl.
apply set_union_elim in I2' as [I2' | I2'].
* apply set_union_intro1. apply set_union_intro2.
assumption.
* apply set_union_intro2. assumption.
- unfold max_zipper in H.
destruct (nullable e1) eqn:N.
+ apply zippers_union_elim in H as [z [I1 I2]].
apply map_zipper_union in I1 as [I1 | I1].
* unshelve epose proof IHe1 (e2 :: ectx) ctx c _.
{ unfold max_zipper.
apply zippers_union_intro with z; assumption. }
unfold max_zipper in H.
unfold max_zipper.
simpl in *.
apply set_union_elim in H as [H | H].
{ apply set_union_intro1.
apply set_union_intro1.
assumption. }
apply set_union_elim in H as [H | H].
{ apply set_union_intro1.
apply set_union_intro2.
assumption. }
apply set_union_intro2.
assumption.
* unshelve epose proof IHe2 ectx ctx c _.
{ unfold max_zipper.
apply zippers_union_intro with z; assumption. }
unfold max_zipper in H.
unfold max_zipper.
simpl in *.
apply set_union_elim in H as [H | H].
{ apply set_union_intro1.
apply set_union_intro2.
assumption. }
apply set_union_intro2.
assumption.
+ apply zippers_union_elim in H as [z [I1 I2]].
unshelve epose proof IHe1 (e2 :: ectx) ctx c _.
{ unfold max_zipper.
apply zippers_union_intro with z; assumption. }
unfold max_zipper in H.
unfold max_zipper.
simpl in *.
apply set_union_elim in H as [H | H].
{ apply set_union_intro1.
apply set_union_intro1.
assumption. }
apply set_union_elim in H as [H | H].
{ apply set_union_intro1.
apply set_union_intro2.
assumption. }
apply set_union_intro2.
assumption.
- unfold max_zipper in H.
apply zippers_union_elim in H as [z [I1 I2]].
unfold max_zipper.
unshelve epose proof IHe (Star e :: ectx) ctx c _.
{ unfold max_zipper.
apply zippers_union_intro with z; assumption. }
unfold max_zipper in H. simpl in H.
apply set_union_elim in H as [H | H].
{ simpl. apply set_union_intro1. assumption. }
apply set_union_elim in H as [H | H].
{ simpl. apply set_union_intro1. assumption. }
simpl.
apply set_union_intro2.