forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitrules.v
471 lines (440 loc) · 11.3 KB
/
unitrules.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
(* ---------------------------------------------------------------------
This file contains definitions and proof scripts related to
(i) closure operations for context-free grammars,
(ii) context-free grammars simplification
(iii) context-free grammar Chomsky normalization and
(iv) pumping lemma for context-free languages.
More information can be found in the paper "Formalization of the
pumping lemma for context-free languages", submitted to
LATA 2016.
Marcus Vinícius Midena Ramos
--------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - UNIT RULES *)
(* --------------------------------------------------------------------- *)
Require Import List.
Require Import Ring.
Require Import Omega.
Require Import misc_arith.
Require Import misc_list.
Require Import cfg.
Require Import useless.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
Open Scope list_scope.
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - UNIT RULES - DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section UnitRules.
Variables terminal non_terminal: Type.
Notation sf := (list (non_terminal + terminal)).
Notation sentence := (list terminal).
Notation term_lift:= ((terminal_lift non_terminal) terminal).
Notation nlist:= (list non_terminal).
Notation tlist:= (list terminal).
Inductive unit (g: cfg non_terminal terminal) (a: non_terminal): non_terminal -> Prop:=
| unit_rule: forall (b: non_terminal),
rules g a [inl b] -> unit g a b
| unit_trans: forall b c: non_terminal,
unit g a b ->
unit g b c ->
unit g a c.
Inductive g_unit_rules (g: cfg _ _): non_terminal -> sf -> Prop :=
| Lift_direct' :
forall left: non_terminal,
forall right: sf,
(forall r: non_terminal,
right <> [inl r]) -> rules g left right ->
g_unit_rules g left right
| Lift_indirect':
forall a b: non_terminal,
unit g a b ->
forall right: sf,
rules g b right ->
(forall c: non_terminal,
right <> [inl c]) ->
g_unit_rules g a right.
Lemma unit_exists_right:
forall g: cfg _ _,
forall a b: non_terminal,
unit g a b ->
exists c : sf, rules g a c.
Proof.
intros g a b H.
induction H.
- exists [inl b].
exact H.
- exact IHunit1.
Qed.
Lemma unit_exists_left:
forall g: cfg _ _,
forall a c: non_terminal,
unit g a c ->
exists b : non_terminal, rules g a [inl b].
Proof.
intros g a c H.
induction H.
- exists b.
exact H.
- exact IHunit1.
Qed.
Lemma g_unit_finite:
forall g: cfg _ _,
exists n: nat,
exists ntl: nlist,
exists tl: tlist,
In (start_symbol g) ntl /\
forall left: non_terminal,
forall right: sf,
g_unit_rules g left right ->
(length right <= n) /\
(In left ntl) /\
(forall s: non_terminal, In (inl s) right -> In s ntl) /\
(forall s: terminal, In (inr s) right -> In s tl).
Proof.
intros g.
destruct (rules_finite g) as [n [ntl [tl H1]]].
exists n, ntl, tl.
split.
- destruct H1 as [H1 _].
exact H1.
- destruct H1 as [_ H1].
intros left right H2.
inversion H2.
+ subst.
specialize (H1 left right H0).
destruct H1 as [H4 [H5 H6]].
split.
* exact H4.
* {
split.
- exact H5.
- exact H6.
}
+ subst.
apply unit_exists_right in H.
destruct H as [c H4].
split.
* specialize (H1 b right H0).
destruct H1 as [H1 _].
exact H1.
* {
split.
- specialize (H1 left c H4).
destruct H1 as [_ [H1 _]].
exact H1.
- specialize (H1 b right H0).
destruct H1 as [_ [_ H1]].
exact H1.
}
Qed.
Definition g_unit (g: cfg _ _): cfg _ _ := {|
start_symbol:= start_symbol g;
rules:= g_unit_rules g;
t_eqdec:= t_eqdec g;
nt_eqdec:= nt_eqdec g;
rules_finite:= g_unit_finite g
|}.
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - UNIT RULES - LEMMAS AND THEOREMS *)
(* --------------------------------------------------------------------- *)
Lemma unit_not_unit:
forall g: cfg _ _,
forall a b: non_terminal,
~ unit (g_unit g) a b.
Proof.
intros g a0 b0 H.
induction H.
- inversion H.
+ specialize (H0 b).
destruct H0.
reflexivity.
+ specialize (H2 b).
destruct H2.
reflexivity.
- exact IHunit1.
Qed.
Lemma unit_derives:
forall g: cfg _ _,
forall a b: non_terminal,
unit g a b ->
derives g [inl a] [inl b].
Proof.
intros g a b H.
induction H.
- apply derives_start.
exact H.
- apply derives_trans with (s2:=[inl b]).
+ exact IHunit1.
+ exact IHunit2.
Qed.
Lemma rules_g_unit_g:
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
rules (g_unit g) left right ->
rules g left right \/ derives g [inl left] right.
Proof.
intros g left right H.
simpl in H.
inversion H.
- left.
exact H1.
- right.
subst.
replace right with ([] ++ right ++ []).
+ apply derives_step with (left:=b).
* apply unit_derives in H0.
exact H0.
* exact H1.
+ rewrite app_nil_l.
rewrite app_nil_r.
reflexivity.
Qed.
Lemma rules_g_unit_g':
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
rules (g_unit g) left right ->
rules g left right \/
exists left': non_terminal, unit g left left' /\ rules g left' right.
Proof.
intros g left right H.
inversion H.
- left.
exact H1.
- right.
exists b.
split.
+ exact H0.
+ exact H1.
Qed.
Lemma rules_g_unit_not_unit:
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
(rules (g_unit g) left right) ->
(~ exists n: non_terminal, right = [inl n]).
Proof.
intros g left right H1 H2.
inversion H1.
- subst.
destruct H2 as [n H2].
specialize (H n).
contradiction.
- subst.
destruct H2 as [n H2].
specialize (H3 n).
contradiction.
Qed.
Lemma generates_g_unit_g:
forall g: cfg _ _,
forall s: sf,
generates (g_unit g) s -> generates g s.
Proof.
unfold generates.
intros g s H.
simpl in H.
remember [inl (start_symbol g)] as w1.
induction H.
- apply derives_refl.
- apply rules_g_unit_g in H0.
destruct H0 as [H0 | H0].
+ apply derives_step with (left:=left).
* apply IHderives.
exact Heqw1.
* exact H0.
+ apply derives_subs with (s3:=[inl left]).
* apply IHderives.
exact Heqw1.
* exact H0.
Qed.
Lemma rules_g_g_unit:
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
rules g left right ->
(forall n: non_terminal, right <> [inl n]) ->
rules (g_unit g) left right.
Proof.
intros g left right H1 H2.
simpl.
apply Lift_direct'.
- exact H2.
- exact H1.
Qed.
Lemma derives3_g_g_unit:
forall g: cfg _ _,
forall n: non_terminal,
forall s: sentence,
derives3 g n s -> derives3 (g_unit g) n s.
Proof.
intros g.
intros n s H.
apply derives3_ind_2 with (g:=g) (P:=derives3 (g_unit g)) (P0:=derives3_aux (g_unit g)).
- intros n0 lt H1.
apply derives3_rule.
apply rules_g_g_unit in H1.
+ exact H1.
+ destruct lt; discriminate.
- intros n0 ltnt lt H1 H2 H3.
destruct ltnt.
+ apply rules_g_g_unit in H1.
* inversion H2.
subst.
apply derives3_rule.
exact H1.
* discriminate.
+ destruct ltnt.
* {
destruct s0.
- apply exists_rule_derives3_aux in H2.
destruct H2 as [H2 | H2].
+ assert (H10: rules (g_unit g) n0 (map term_lift lt)).
{
apply Lift_indirect' with (b:=n1).
* apply unit_rule.
exact H1.
* exact H2.
* destruct lt; discriminate.
}
apply derives3_rule.
exact H10.
+ destruct H2 as [right [H4 H5]].
apply exists_rule_derives3_aux in H3.
destruct H3 as [H3 | H3].
* apply rules_g_unit_g' in H3.
{
destruct H3 as [H3 | H3].
- assert (H10: rules (g_unit g) n0 (map term_lift lt)).
{
apply Lift_indirect' with (b:=n1).
- apply unit_rule.
exact H1.
- exact H3.
- intros c0.
destruct lt.
+ discriminate.
+ discriminate.
}
apply derives3_rule.
exact H10.
- destruct H3 as [left' [H6 H7]].
assert (H10: rules (g_unit g) n0 (map term_lift lt)).
{
apply Lift_indirect' with (b:=left').
- apply unit_trans with (b:=n1).
+ apply unit_rule.
exact H1.
+ exact H6.
- exact H7.
- intros c0.
destruct lt; discriminate.
}
apply derives3_rule.
exact H10.
}
* destruct H3 as [right0 [H6 H7]].
assert (H6':=H6).
apply rules_g_unit_not_unit in H6'.
apply rules_g_unit_g' in H6.
{
destruct H6 as [H6 | H6].
- apply derives3_step with (ltnt:=right0).
+ apply Lift_indirect' with (b:=n1).
* apply unit_rule.
exact H1.
* exact H6.
* apply not_exists_forall_not.
exact H6'.
+ exact H7.
- apply derives3_step with (ltnt:=right0).
+ destruct H6 as [left' [H8 H9]].
apply Lift_indirect' with (b:=left').
* {
apply unit_trans with (b:=n1).
- apply unit_rule.
exact H1.
- exact H8.
}
* exact H9.
* apply not_exists_forall_not.
exact H6'.
+ exact H7.
}
- apply rules_g_g_unit in H1.
+ apply derives3_step with (ltnt:=[inr t]).
* exact H1.
* exact H3.
+ discriminate.
}
* {
apply rules_g_g_unit in H1.
- apply derives3_step with (ltnt:=(s0 :: s1 :: ltnt)).
+ exact H1.
+ exact H3.
- discriminate.
}
- apply derives3_aux_empty.
- intros t ltnt lt H1 H2.
apply derives3_aux_t.
exact H2.
- intros n0 lt lt' ltnt H1 H2 H3 H4.
apply derives3_aux_nt.
+ exact H2.
+ exact H4.
- exact H.
Qed.
Lemma derives_g_g_unit:
forall g: cfg _ _,
forall s: sentence,
forall n: non_terminal,
derives g [inl n] (map term_lift s) -> derives (g_unit g) [inl n] (map term_lift s).
Proof.
intros g s n.
repeat rewrite derives_equiv_derives3.
apply derives3_g_g_unit.
Qed.
Theorem g_equiv_unit:
forall g: cfg _ _,
g_equiv (g_unit g) g.
Proof.
unfold g_equiv.
unfold produces.
intros g s.
split.
- apply generates_g_unit_g.
- apply derives_g_g_unit.
Qed.
Definition has_no_unit_rules (g: cfg _ _): Prop:=
forall left n: non_terminal,
forall right: sf,
rules g left right -> right <> [inl n].
Lemma g_unit_has_no_unit_rules:
forall g: cfg _ _,
has_no_unit_rules (g_unit g).
Proof.
unfold has_no_unit_rules.
intros g left n right H.
destruct right.
- apply nil_cons.
- inversion_clear H.
+ specialize (H0 n).
exact H0.
+ specialize (H2 n).
exact H2.
Qed.
Theorem g_unit_correct:
forall g: cfg _ _,
g_equiv (g_unit g) g /\
has_no_unit_rules (g_unit g).
Proof.
intros g.
split.
- apply g_equiv_unit.
- apply g_unit_has_no_unit_rules.
Qed.
End UnitRules.