-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDerivParse.v
422 lines (366 loc) · 12.9 KB
/
DerivParse.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
(*
* Parsing regular expressions with derivatives.
* http://matt.might.net/papers/might2011derivatives.pdf
*)
From FunProofs.Lib Require Import
Util.
Import EqDecNotations.
Section RegLang.
Context {A : Type}.
Context `{EqDec A}.
Inductive RLang :=
| Empty : RLang
| Null : RLang
| Single : A -> RLang
| Alt : RLang -> RLang -> RLang
| Concat : RLang -> RLang -> RLang
| Star : RLang -> RLang.
(* Extensions *)
Definition Plus (re : RLang) : RLang :=
Concat re (Star re).
Definition QMark (re : RLang) : RLang :=
Alt re Null.
Definition Class (cs : list A) : RLang :=
fold_right Alt Empty (map Single cs).
Fixpoint nullable (re : RLang) : bool :=
match re with
| Empty => false
| Null => true
| Single _ => false
| Alt re1 re2 => nullable re1 || nullable re2
| Concat re1 re2 => nullable re1 && nullable re2
| Star _ => true
end.
Fixpoint derivative (c : A) (re : RLang) : RLang :=
match re with
| Empty => Empty
| Null => Empty
| Single c' => if c == c' then Null else Empty
| Alt re1 re2 => Alt (derivative c re1) (derivative c re2)
| Concat re1 re2 =>
let con := Concat (derivative c re1) re2 in
if nullable re1
then Alt con (derivative c re2)
else con
| Star re => Concat (derivative c re) (Star re)
end.
Fixpoint matches (re : RLang) (cs : list A) : bool :=
match cs with
| [] => nullable re
| c :: cs' => matches (derivative c re) cs'
end.
Definition re_equiv (re1 re2 : RLang) : Prop :=
forall cs, matches re1 cs = matches re2 cs.
#[export] Instance re_equiv_refl : Reflexive re_equiv.
Proof. now red; unfold re_equiv. Qed.
#[export] Instance re_equiv_sym : Symmetric re_equiv.
Proof. now red; unfold re_equiv. Qed.
#[export] Instance re_equiv_trans : Transitive re_equiv.
Proof. red; unfold re_equiv; intros; etransitivity; auto. Qed.
#[export] Instance re_equiv_equiv : Equivalence re_equiv.
Proof. constructor; typeclasses eauto. Qed.
End RegLang.
Declare Scope re_scope.
Delimit Scope re_scope with re.
Notation "'∅'" := (Empty) : re_scope.
Notation "'ϵ'" := (Null) : re_scope.
Notation "` c `" := (Single c) (at level 10) : re_scope.
Notation "re1 | re2" := (Alt re1 re2) (at level 51, right associativity) : re_scope.
Notation "re1 ;; re2" := (Concat re1 re2) (at level 41, right associativity) : re_scope.
Notation "re *'" := (Star re) (at level 30) : re_scope.
Notation "re +'" := (Plus re) (at level 30) : re_scope.
Notation "re ?" := (QMark re) (at level 30) : re_scope.
Notation "'[r' c1 ; .. ; c2 ]" := (Class (cons c1 .. ([c2]) ..)) : re_scope.
Notation "cs =~ re" := (matches re cs = true) (at level 70) : re_scope.
Notation "cs !~ re" := (matches re cs = false) (at level 70) : re_scope.
Infix "~=~" := (re_equiv) (at level 70) : re_scope.
Open Scope re_scope.
Section Facts.
Context `{alph : EqDec}.
Lemma nullable_match re1 re2 : re1 ~=~ re2 -> nullable re1 = nullable re2.
Proof. intros Hmatch; apply (Hmatch []). Qed.
Lemma derivative_match re1 re2 c :
re1 ~=~ re2 -> derivative c re1 ~=~ derivative c re2.
Proof. red; intros Hmatch *; apply (Hmatch (c :: cs)). Qed.
(* Empty *)
Lemma empty_no_match cs : cs !~ ∅.
Proof. induction cs; auto. Qed.
(* Null *)
Lemma null_one_match cs : cs =~ ϵ <-> cs = [].
Proof.
split; intros H; subst; auto.
destruct cs; auto; cbn in H.
now rewrite empty_no_match in H.
Qed.
(* Single *)
Lemma single_one_match cs c : cs =~ `c` <-> cs = [c].
Proof.
split; intros H; subst; cbn; simplify; auto.
destruct cs as [| c' cs]; cbn in *; try easy.
cases H.
- now rewrite null_one_match in H; subst.
- now rewrite empty_no_match in H.
Qed.
(* Alt *)
Lemma alt_empty_l re : ∅ | re ~=~ re.
Proof.
red; intros; revert re.
induction cs; intros; cbn; auto.
Qed.
Lemma alt_commute re1 re2 : re1 | re2 ~=~ re2 | re1.
Proof.
red; intros; revert re1 re2.
induction cs; intros; cbn; auto with bool.
Qed.
Lemma alt_assoc re1 re2 re3 : re1 | (re2 | re3) ~=~ (re1 | re2) | re3.
Proof.
red; intros; revert re1 re2 re3.
induction cs; intros; cbn; auto with bool.
Qed.
Corollary alt_empty_r re : re | ∅ ~=~ re.
Proof. rewrite alt_commute; apply alt_empty_l. Qed.
Lemma alt_diag re : re | re ~=~ re.
Proof.
red; intros; revert re.
induction cs; intros; cbn; auto using Bool.orb_diag.
Qed.
Lemma alt_match_or cs : forall re1 re2,
matches (re1 | re2) cs = matches re1 cs || matches re2 cs.
Proof. induction cs; intros; cbn; auto. Qed.
Corollary alt_match_true cs re1 re2 : cs =~ (re1 | re2) <-> (cs =~ re1 \/ cs =~ re2).
Proof. now rewrite alt_match_or, Bool.orb_true_iff. Qed.
Corollary alt_match_false cs re1 re2 : cs !~ (re1 | re2) <-> (cs !~ re1 /\ cs !~ re2).
Proof. now rewrite alt_match_or, Bool.orb_false_iff. Qed.
Lemma alt_cancel_l re re1 re2 : re1 ~=~ re2 -> re | re1 ~=~ re | re2.
Proof.
red; intros Hmatch *; revert re re1 re2 Hmatch.
induction cs; intros; cbn.
- erewrite (nullable_match re1); eauto.
- erewrite !alt_match_or, (derivative_match re1); eauto.
Qed.
Corollary alt_cancel_r re re1 re2 : re1 ~=~ re2 -> re1 | re ~=~ re2 | re.
Proof. rewrite (alt_commute re1), (alt_commute re2); apply alt_cancel_l. Qed.
(* Concat *)
Lemma concat_empty_l re : ∅;;re ~=~ ∅.
Proof. red; intros; induction cs; auto. Qed.
Lemma concat_empty_r re : re;;∅ ~=~ ∅.
Proof.
red; intros; revert re.
induction cs; intros; cbn; destruct (nullable _); auto.
rewrite alt_empty_r; auto.
Qed.
Lemma concat_null_l re : ϵ;;re ~=~ re.
Proof.
red; intros; revert re.
induction cs; intros; cbn; auto.
rewrite alt_match_or, concat_empty_l, empty_no_match; auto.
Qed.
Lemma concat_null_r re : re;;ϵ ~=~ re.
Proof.
red; intros; revert re.
induction cs; intros; cbn; auto using Bool.andb_true_r.
destruct (nullable _); auto.
rewrite alt_match_or, IHcs, empty_no_match; auto using Bool.orb_false_r.
Qed.
Lemma concat_match_true cs : forall re1 re2,
cs =~ re1;;re2 <-> exists cs1 cs2, cs = cs1 ++ cs2 /\ cs1 =~ re1 /\ cs2 =~ re2.
Proof.
induction cs; cbn; split; intros H.
- exists [], []; cbn; auto with bool.
- destruct H as ([] & [] & ? & ? & ?); cbn in *; auto with bool; easy.
- cases H.
+ rewrite alt_match_true, IHcs in H.
destruct H; destr *; subst; eauto using app_nil_l, app_comm_cons.
+ rewrite IHcs in H; destr *; subst; eauto using app_nil_l, app_comm_cons.
- destruct H as (cs1 & ? & Heq & ? & ?).
destruct (nullable _) eqn:?.
+ rewrite alt_match_true, IHcs.
destruct cs1; cbn in Heq; simplify; eauto.
left; eauto using app_comm_cons.
+ rewrite IHcs.
destruct cs1; cbn in Heq; simplify; eauto.
Qed.
Corollary concat_match_false' cs re1 re2 :
cs !~ re1;;re2 <-> ~(exists cs1 cs2, cs = cs1 ++ cs2 /\ cs1 =~ re1 /\ cs2 =~ re2).
Proof. apply iff_not_true, concat_match_true. Qed.
Corollary concat_match_false cs re1 re2 :
cs !~ re1;;re2 <-> forall cs1 cs2, cs = cs1 ++ cs2 -> cs1 !~ re1 \/ cs2 !~ re2.
Proof.
rewrite concat_match_false', not_exist.
setoid_rewrite not_exist.
split; intros H; intuition; subst.
- destruct (matches re1 _) eqn:?; auto.
destruct (matches re2 _) eqn:?; auto.
exfalso; eauto.
- specialize (H _ _ eq_refl).
intuition congruence.
Qed.
Lemma concat_cancel_l re re1 re2 : re1 ~=~ re2 -> re;;re1 ~=~ re;;re2.
Proof.
red; intros Hmatch *; revert re re1 re2 Hmatch.
induction cs; intros; cbn.
- erewrite (nullable_match re1); eauto.
- destruct (nullable _); eauto.
erewrite !alt_match_or, IHcs, derivative_match; eauto.
Qed.
Lemma concat_cancel_r re re1 re2 : re1 ~=~ re2 -> re1;;re ~=~ re2;;re.
Proof.
red; intros Hmatch *; revert re re1 re2 Hmatch.
induction cs; intros; cbn.
- erewrite (nullable_match re1); eauto.
- erewrite nullable_match; eauto.
destruct (nullable _); eauto using derivative_match.
erewrite !alt_match_or, IHcs; eauto using derivative_match.
Qed.
Lemma alt_concat_distr_l re1 re2 re3 : re1;;(re2 | re3) ~=~ re1;;re2 | re1;;re3.
Proof.
red; intros; destruct (matches (_ | _) _) eqn:Halt.
- rewrite alt_match_true in Halt.
rewrite !concat_match_true in *.
destruct Halt; destr *; subst; repeat (esplit; eauto).
all: rewrite alt_match_true; auto.
- rewrite alt_match_false in Halt.
rewrite !concat_match_false in *.
intros; subst.
rewrite alt_match_false.
destruct Halt as (Halt1 & Halt2).
specialize (Halt1 _ _ eq_refl); specialize (Halt2 _ _ eq_refl).
intuition.
Qed.
Lemma alt_concat_distr_r re1 re2 re3 : (re1 | re2);;re3 ~=~ re1;;re3 | re2;;re3.
Proof.
red; intros; destruct (matches (_ | _) _) eqn:Halt.
- rewrite alt_match_true in Halt.
rewrite !concat_match_true in *.
destruct Halt; destr *; subst; repeat (esplit; eauto).
all: rewrite alt_match_true; auto.
- rewrite alt_match_false in Halt.
rewrite !concat_match_false in *.
intros; subst.
rewrite alt_match_false.
destruct Halt as (Halt1 & Halt2).
specialize (Halt1 _ _ eq_refl); specialize (Halt2 _ _ eq_refl).
intuition.
Qed.
Lemma concat_assoc re1 re2 re3 : re1;;(re2;;re3) ~=~ (re1;;re2);;re3.
Proof.
red; intros; revert re1 re2 re3.
induction cs; intros; cbn; auto with bool.
destruct (nullable _); cbn; auto.
destruct (nullable _); cbn.
- rewrite !alt_match_or, alt_concat_distr_r, !alt_match_or, !IHcs; auto with bool.
- rewrite alt_concat_distr_r, !alt_match_or, !IHcs; auto.
Qed.
(* Star *)
Lemma star_match_empty re : [] =~ re*'.
Proof. auto. Qed.
Lemma star_unfold re : re*' ~=~ (ϵ | re;;re*').
Proof.
red; intros; revert re.
induction cs; cbn; intros; auto.
rewrite alt_empty_l.
destruct (nullable _); cbn; auto.
rewrite alt_diag; auto.
Qed.
(* Plus *)
Lemma plus_unfold re : re+' ~=~ (re | re;;re+').
Proof.
unfold Plus; red; intros.
rewrite alt_match_or, <- (concat_null_r re), <- alt_match_or, <- alt_concat_distr_l.
erewrite concat_cancel_l; eauto using star_unfold.
Qed.
(* Class *)
Lemma class_match_true cs cs' :
cs' =~ Class cs <-> exists c, cs' = [c] /\ In c cs.
Proof.
induction cs; cbn; split; intros H.
- now rewrite empty_no_match in H.
- now destr *.
- rewrite alt_match_or, Bool.orb_true_iff, IHcs, single_one_match in H.
destruct H; destr *; eauto.
- rewrite alt_match_or, Bool.orb_true_iff, IHcs, single_one_match.
destr *; intuition (subst; eauto).
Qed.
End Facts.
Section KleeneAlgebra.
Context {A : Type}.
Variables (kzero kone : A).
Variables (kplus kmul : A -> A -> A).
Variable (kstar : A -> A).
Variable (keq : A -> A -> Prop).
Notation "0" := kzero.
Notation "1" := kone.
Infix "+" := kplus.
Infix "*" := kmul.
Notation "x *'" := (kstar x).
Infix "~=~" := keq.
Class KleeneAlgebra := {
kadd_0_l x : 0 + x ~=~ x;
kadd_comm x y : x + y ~=~ y + x;
kadd_assoc x y z : x + (y + z) ~=~ (x + y) + z;
kadd_idem x : x + x ~=~ x;
kmul_0_l x : 0 * x ~=~ 0;
kmul_0_r x : x * 0 ~=~ 0;
kmul_1_l x : 1 * x ~=~ x;
kmul_1_r x : x * 1 ~=~ x;
kmul_assoc x y z : x * (y * z) ~=~ (x * y) * z;
kadd_kmul_distr_l x y z : x * (y + z) ~=~ x * y + x * z;
kadd_kmul_distr_r x y z : (y + z) * x ~=~ y * x + z * x;
}.
End KleeneAlgebra.
#[export] Instance RLang_KleeneAlgebra `{EqDec} : KleeneAlgebra ∅ ϵ Alt Concat re_equiv.
Proof.
constructor; intros; [
apply alt_empty_l |
apply alt_commute |
apply alt_assoc |
apply alt_diag |
apply concat_empty_l |
apply concat_empty_r |
apply concat_null_l |
apply concat_null_r |
apply concat_assoc |
apply alt_concat_distr_l |
apply alt_concat_distr_r].
Qed.
Section Tests.
Let a := 0.
Let b := 1.
Let c := 2.
Goal [] =~ (`a`*').
Proof. reflexivity. Qed.
Goal [a] =~ (`a`*').
Proof. reflexivity. Qed.
Goal [a; a] =~ (`a`*').
Proof. reflexivity. Qed.
Goal [a; a; b] !~ (`a`*').
Proof. reflexivity. Qed.
Goal [b] =~ ((`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [c] =~ ((`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [] !~ ((`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [b; b; c; b] =~ ((`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [a; b] =~ (`c`?;;`a`*';;(`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [a; c] =~ (`c`?;;`a`*';;(`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [b; c; b] =~ (`c`?;;`a`*';;(`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [c; a; c] =~ (`c`?;;`a`*';;(`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [c; a] !~ (`c`?;;`a`*';;(`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [c; c] =~ (`c`?;;`a`*';;(`b`|`c`)+').
Proof. reflexivity. Qed.
Goal [] =~ ([r a; b; c]?).
Proof. reflexivity. Qed.
Goal [a] =~ ([r a; b; c]?).
Proof. reflexivity. Qed.
Goal [b] =~ ([r a; b; c]?).
Proof. reflexivity. Qed.
Goal [c] =~ ([r a; b; c]?).
Proof. reflexivity. Qed.
End Tests.