-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregular.v
276 lines (232 loc) · 7.31 KB
/
regular.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
From stars Require Import definitions.
Inductive re (X : Type) :=
| RE_None
| RE_Empty
| RE_Literal (x : X)
| RE_Or (a b : re X)
| RE_Seq (a b : re X)
| RE_Star (a : re X).
Arguments RE_None {_}.
Arguments RE_Empty {_}.
Arguments RE_Literal {_}.
Arguments RE_Or {_}.
Arguments RE_Seq {_}.
Arguments RE_Star {_}.
Global Instance : FMap re := λ X Y f, fix go (a : re X) :=
match a with
| RE_None => RE_None
| RE_Empty => RE_Empty
| RE_Literal x => RE_Literal (f x)
| RE_Or b c => RE_Or (go b) (go c)
| RE_Seq b c => RE_Seq (go b) (go c)
| RE_Star b => RE_Star (go b)
end.
Notation "a ∣ b" := (RE_Or a b)
(left associativity, at level 53, format "a ∣ b").
Notation "a ⋅ b" := (RE_Seq a b)
(left associativity, at level 52, format "a ⋅ b").
Notation "a ∗" := (RE_Star a)
(left associativity, at level 51, format "a ∗").
Section Regular_Expressions.
Variable X : Type.
Notation re := (re X).
Fixpoint re_in (w : list X) (a : re) :=
match a with
| RE_None => False
| RE_Empty => w = []
| RE_Literal x => w = [x]
| RE_Or b c => re_in w b \/ re_in w c
| RE_Seq b c => ∃ u v, w = u ++ v /\ re_in u b /\ re_in v c
| RE_Star b => w = [] \/ ∃ vs, w = concat vs /\ Forall (λ v, re_in v b) vs
end.
Global Instance : Zero re := RE_None.
Global Instance : One re := RE_Empty.
Global Instance : Empty re := RE_None.
Global Instance : ElemOf (list X) re := re_in.
Global Instance : Equiv re := λ a b, ∀ w, w ∈ a <-> w ∈ b.
Global Instance : Add re :=
λ a b, match a, b with
| RE_None, _ => b
| _, RE_None => a
| RE_Empty, RE_Empty => RE_Empty
| RE_Empty, RE_Star _ => b
| RE_Star _, RE_Empty => a
| _, _ => RE_Or a b
end.
Global Instance : Mul re :=
λ a b, match a with
| RE_None => RE_None
| RE_Empty => b
| _ => match b with
| RE_None => RE_None
| RE_Empty => a
| _ => RE_Seq a b
end
end.
Global Instance : Star re :=
λ a, match a with
| RE_None => RE_Empty
| RE_Empty => RE_Empty
| RE_Star _ => a
| _ => RE_Star a
end.
Global Instance : @Equivalence re (≡).
Proof. firstorder. Qed.
Section Simplifications.
Lemma re_seq_1_l a :
1⋅a ≡ a.
Proof.
intros w; cbn; split.
intros (u&v&->&->&Hv); done.
intros Hw; exists [], w; done.
Qed.
Lemma re_seq_1_r a :
a⋅1 ≡ a.
Proof.
intros w; cbn; split.
intros (u&v&->&Hu&->); simplify_list_eq; done.
intros Hw; exists w, []; simplify_list_eq; done.
Qed.
Lemma re_star_0 :
0∗ ≡ 1.
Proof.
intros w; cbn; firstorder; subst.
destruct x; decompose_Forall; done.
Qed.
Lemma re_star_1 :
1∗ ≡ 1.
Proof.
intros w; cbn; firstorder; subst.
apply concat_nil_Forall; done.
Qed.
Lemma re_star_star a :
a∗∗ ≡ a∗.
Proof.
intros w; cbn; split; intros [H|(vs&->&H)]; auto.
- induction vs as [|v vs]. left; done. decompose_Forall.
apply IHvs in H0 as [->|(vs'&->&Hvs')]; clear IHvs.
+ simplify_list_eq; firstorder.
+ right; destruct H as [->|(us&->&Hus)]; cbn.
exists vs'; done. exists (us ++ vs'); split.
symmetry; apply concat_app. decompose_Forall; done.
- right; exists vs; split; [done|]. eapply Forall_impl.
apply H. cbn; intros v Hv; right; exists [v]; split.
cbn; simplify_list_eq; done. decompose_Forall; done.
Qed.
Theorem equiv_re_add a b :
a + b ≡ a∣b.
Proof.
destruct a, b; cbn; try firstorder.
Qed.
Theorem equiv_re_mul a b :
a * b ≡ a⋅b.
Proof.
destruct a, b; cbn; try reflexivity; symmetry.
all: try apply re_seq_1_l; try apply re_seq_1_r.
all: firstorder.
Qed.
Theorem equiv_re_star a :
a{*} ≡ a∗.
Proof.
destruct a; cbn; try reflexivity; symmetry.
apply re_star_0. apply re_star_1. apply re_star_star.
Qed.
End Simplifications.
Section Properties.
Lemma assoc_re_seq a b c :
a⋅(b⋅c) ≡ a⋅b⋅c.
Proof.
intros w; cbn; split; intros (u&v&->&H).
+ destruct H as (Ha&v1&v2&->&Hv1&Hv2).
exists (u ++ v1), v2; repeat split; try done.
apply app_assoc. exists u, v1; done.
+ destruct H as ((u1&u2&->&Hu1&Hu2)&Hv).
exists u1, (u2 ++ v); repeat split; try done.
symmetry; apply app_assoc. exists u2, v; done.
Qed.
Lemma left_expand_re_star a :
a∗ ≡ 1∣a⋅a∗.
Proof.
intros w; cbn; split; intros H.
+ destruct H as [->|(vs&->&H)]; [left; done|].
destruct vs as [|v vs]; [left; done|right]; exists v, (concat vs).
decompose_Forall; repeat split; try done. right; exists vs; done.
+ destruct H as [->|(u&v&->&Hu&H)]; [left; done|right].
destruct H as [->|(vs&->&Hvs)]; [exists [u]|exists (u :: vs)];
cbn; repeat split; decompose_Forall; done.
Qed.
Lemma right_expand_re_star a :
a∗ ≡ 1∣a∗⋅a.
Proof.
intros w; cbn; split; intros H.
+ destruct H as [->|(vs&->&H)]; [left; done|].
destruct vs as [|v vs _] using rev_ind; [left; done|right].
decompose_Forall; exists (concat vs), v; repeat split.
rewrite concat_app; simplify_list_eq; done.
right; exists vs; done. done.
+ destruct H as [->|(u&v&->&H&Hv)]; [left; done|right].
destruct H as [->|(us&->&Hus)]; [exists [v]|exists (us ++ [v])];
cbn; repeat split; simplify_list_eq; decompose_Forall; try done.
rewrite concat_app; simplify_list_eq; done.
Qed.
Lemma left_intro_re_star a b :
a⋅b∣b ≡ b -> a∗⋅b∣b ≡ b.
Proof.
intros H w; cbn; split; [|auto].
intros [(u&v&->&[->|(us&->&Hus)]&Hv)|Hw]; simplify_list_eq; try done.
induction us as [|u us]; simplify_list_eq. done. decompose_Forall.
apply H; cbn; left; eexists; eexists; auto.
Qed.
Lemma right_intro_re_star a b :
b⋅a∣b ≡ b -> b⋅a∗∣b ≡ b.
Proof.
intros H w; cbn; split; [|auto].
intros [(u&v&->&Hu&[->|(vs&->&Hvs)])|Hw]; simplify_list_eq; try done.
induction vs as [|v vs] using rev_ind; simplify_list_eq. done.
rewrite concat_app; simplify_list_eq; rewrite app_assoc.
decompose_Forall; apply H; cbn; left; eexists; eexists; auto.
Qed.
End Properties.
Section Rewriting.
Global Instance : Proper ((≡) ==> (≡) ==> (≡)) RE_Or.
Proof. firstorder. Qed.
Global Instance : Proper ((≡) ==> (≡) ==> (≡)) RE_Seq.
Proof. firstorder. Qed.
Global Instance : Proper ((≡) ==> (≡)) RE_Star.
Proof.
intros a b Hab w; cbn; split; intros [->|(vs&->&H)]; try (left; done); right;
exists vs; split; try done; eapply Forall_impl; try apply H; apply Hab.
Qed.
Global Instance : Proper ((≡) ==> (≡) ==> (≡)) (@add re _).
Proof. intros a b Hab c d Hcd; rewrite ?equiv_re_add, Hab, Hcd; done. Qed.
Global Instance : Proper ((≡) ==> (≡) ==> (≡)) (@mul re _).
Proof. intros a b Hab c d Hcd; rewrite ?equiv_re_mul, Hab, Hcd; done. Qed.
Global Instance : Proper ((≡) ==> (≡)) (@star re _).
Proof. intros a b Hab; rewrite ?equiv_re_star, Hab; done. Qed.
End Rewriting.
Local Ltac expand := rewrite ?equiv_re_add, ?equiv_re_mul, ?equiv_re_star.
Global Instance : Kleene_Algebra re.
Proof.
split. split. split. c. split. 1,3: split. 1,4: split. 1,3: c.
1,3-4,7-11,14: intro; intros; expand; firstorder.
- intros a b c; expand; apply assoc_re_seq.
- intros a; expand; apply re_seq_1_l.
- intros a; expand; apply re_seq_1_r.
- intros a; expand; apply left_expand_re_star.
- intros a; expand; apply right_expand_re_star.
- intros a b; expand; apply left_intro_re_star.
- intros a b; expand; apply right_intro_re_star.
Qed.
Section Evaluation.
Context `{Kleene_Algebra X}.
Fixpoint re_eval (a : re) : X :=
match a with
| RE_None => 0
| RE_Empty => 1
| RE_Literal x => x
| RE_Or b c => re_eval b + re_eval c
| RE_Seq b c => re_eval b * re_eval c
| RE_Star b => (re_eval b){*}
end.
End Evaluation.
End Regular_Expressions.