forked from mvmramos/pumping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useless.v
645 lines (615 loc) · 17.1 KB
/
useless.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
(* ---------------------------------------------------------------------
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 - USELESS SYMBOLS *)
(* --------------------------------------------------------------------- *)
Require Import List.
Require Import Ring.
Require Import Omega.
Require Import misc_arith.
Require Import misc_list.
Require Import cfg.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import ListNotations.
Open Scope list_scope.
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - USELESS SYMBOLS - DEFINITIONS *)
(* --------------------------------------------------------------------- *)
Section Useless.
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).
Definition useful (g: cfg _ _) (s: non_terminal + terminal): Prop:=
match s with
| inr t => True
| inl n => exists s: sentence, derives g [inl n] (map term_lift s)
end.
Definition useful_sf (g: cfg _ _)(l: _): Prop:=
forall s: _,
In s l -> useful g s.
Inductive g_use_rules (g: cfg _ _): non_terminal -> sf -> Prop :=
| Lift_use : forall left: non_terminal,
forall right: sf,
rules g left right ->
useful g (inl left) ->
useful_sf g right ->
g_use_rules g left right.
Lemma g_use_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_use_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.
- intros left right H2.
inversion H2.
subst.
destruct H1 as [_ H1].
specialize (H1 left right H).
destruct H1 as [H4 [H5 H6]].
split.
+ exact H4.
+ split.
* exact H5.
* exact H6.
Qed.
Definition g_use (g: cfg _ _): cfg _ _:= {|
start_symbol:= start_symbol g;
rules:= g_use_rules g;
t_eqdec:= t_eqdec g;
nt_eqdec:= nt_eqdec g;
rules_finite:= g_use_finite g
|}.
Definition has_no_useless_symbols (g: cfg _ _): Prop:=
forall n: _, appears g (inl n) -> useful g (inl n).
Definition non_empty (g: cfg _ _): Prop:=
useful g (inl (start_symbol g)).
(* --------------------------------------------------------------------- *)
(* SIMPLIFICATION - USELESS SYMBOLS - LEMMAS AND THEOREMS *)
(* --------------------------------------------------------------------- *)
Lemma useful_exists:
forall g: cfg _ _,
forall n:non_terminal,
useful g (inl n) ->
exists (left : non_terminal) (right : sf),
rules g left right /\ (n = left \/ In (inl n) right).
Proof.
intros g n H1.
destruct H1 as [s H2].
apply exists_rule in H2.
destruct H2 as [H2 | H2].
- exists n, (map term_lift s).
split.
+ exact H2.
+ left.
reflexivity.
- destruct H2 as [right [H3 H4]].
exists n, right.
split.
+ exact H3.
+ left.
reflexivity.
Qed.
Lemma useful_sf_sentence:
forall l: sf,
forall g: cfg _ _,
useful_sf g l -> exists s': sentence, derives g l (map term_lift s').
Proof.
intros l g H.
induction l.
- exists [].
simpl.
apply derives_refl.
- replace (a :: l) with ([a]++l) in H.
+ assert (H1: forall s: non_terminal + terminal, In s l -> useful g s).
{
intros s H1.
specialize (H s).
apply H.
apply in_or_app.
right.
exact H1.
}
specialize (IHl H1).
assert (H2: useful g a).
{
apply H.
simpl.
left.
reflexivity.
}
unfold useful in H2.
destruct a.
* destruct IHl as [s' H3].
destruct H2 as [s H2].
exists (s ++ s').
simpl.
{
rewrite map_app.
change (inl n :: l) with ([inl n] ++ l).
apply derives_combine.
split.
- exact H2.
- exact H3.
}
* destruct IHl as [s2 H5].
exists ([t] ++ s2).
rewrite map_app.
change (inr t :: l) with ([inr t] ++ l).
apply derives_combine.
{
split.
- simpl.
constructor.
- exact H5.
}
+ simpl.
reflexivity.
Qed.
Lemma use_step:
forall g: cfg _ _,
forall left: non_terminal,
forall right: sf,
rules g left right ->
(forall s: non_terminal + terminal, In s right -> useful g s) ->
useful g (inl left).
Proof.
intros g left right H1 H2.
unfold useful.
apply derives_rule with (s1:=[]) (s2:=[]) in H1.
simpl in H1.
rewrite app_nil_r in H1.
apply useful_sf_sentence in H2.
destruct H2 as [s' H3].
exists s'.
apply derives_trans with (s2:=right).
- exact H1.
- exact H3.
Qed.
Lemma useful_g_g_use:
forall g: cfg _ _,
forall n: non_terminal,
useful g (inl n) -> useful (g_use g) (inl n).
Proof.
intros g n H.
unfold useful in H.
destruct H as [x H].
unfold useful.
exists x.
apply derives_sflist in H.
apply derives_sflist.
destruct H as [l [H2 [H3 H4]]].
exists l.
split.
- assert (H5: length l >= 2 \/ length l < 2).
{
omega.
}
destruct H5 as [H5 | H5].
+ assert (H6:= H5).
apply sflist_rules with (g:=g) in H5.
destruct H5 as [H5 _].
specialize (H5 H2).
apply sflist_rules.
* exact H6.
* intros i H7.
specialize (H5 i H7).
destruct H5 as [left [right [s0 [s' [H10 [H11 H12]]]]]].
exists left, right, s0, s'.
{
split.
- exact H10.
- split.
+ exact H11.
+ simpl.
apply Lift_use.
* exact H12.
* unfold useful.
assert (H20: derives g (s0 ++ inl left :: s') (map term_lift x)).
{
apply derives_sflist.
rewrite <- (firstn_skipn i l) in H2.
apply sflist_app_l in H2.
exists (skipn i l).
split.
- exact H2.
- split.
+ rewrite hd_skip.
exact H10.
+ rewrite last_skip.
* exact H4.
* omega.
}
assert (H21: exists s'': sentence, derives g [inl left] (map term_lift s'')).
{
apply derives_split in H20.
destruct H20 as [s1' [s2' [H21 [_ H22]]]].
replace (inl left :: s') with ([inl left] ++ s') in H22.
- apply derives_split in H22.
destruct H22 as [s1'0 [s2'0 [H31 [H32 _]]]].
symmetry in H21.
apply map_expand in H21.
destruct H21 as [s1'' [s2'' [H40 [H41 H42]]]].
rewrite <- H42 in H31.
symmetry in H31.
apply map_expand in H31.
destruct H31 as [s1''' [s2''' [H50 [H51 H52]]]].
rewrite <- H51 in H32.
exists s1'''.
exact H32.
- simpl.
reflexivity.
}
exact H21.
* intros s1 H8.
{
destruct s1.
- assert (H30: derives g (s0 ++ right ++ s') (map term_lift x)).
{
apply derives_sflist.
rewrite <- (firstn_skipn (S i) l) in H2.
apply sflist_app_l in H2.
exists (skipn (S i) l).
split.
- exact H2.
- split.
+ rewrite <- H11.
apply hd_skip.
+ rewrite <- H4.
apply last_skip.
omega.
}
assert (H31: exists s'': sentence, derives g right (map term_lift s'')).
{
apply derives_split in H30.
destruct H30 as [s1' [s2' [H35 [H36 H37]]]].
apply derives_split in H37.
destruct H37 as [s1'0 [s2'0 [H38 [H39 H40]]]].
symmetry in H35.
apply map_expand in H35.
destruct H35 as [s1'' [s2'' [H41 [H42 H43]]]].
rewrite <- H43 in H38.
symmetry in H38.
apply map_expand in H38.
destruct H38 as [s1''' [s2''' [H50 [H51 H52]]]].
rewrite <- H51 in H39.
exists s1'''.
exact H39.
}
assert (H32: exists s'': sentence, derives g [inl n0] (map term_lift s'')).
{
apply in_split in H8.
destruct H8 as [l1 [l2 H60]].
rewrite H60 in H31.
destruct H31 as [s'' H61].
apply derives_split in H61.
destruct H61 as [s1' [s2' [H35 [H36 H37]]]].
replace (inl n0 :: l2) with ([inl n0] ++ l2) in H37.
- apply derives_split in H37.
destruct H37 as [s1'0 [s2'0 [H38 [H39 H40]]]].
symmetry in H35.
apply map_expand in H35.
destruct H35 as [s1'' [s2'' [H41 [H42 H43]]]].
rewrite <- H43 in H38.
symmetry in H38.
apply map_expand in H38.
destruct H38 as [s1''' [s2''' [H50 [H51 H52]]]].
rewrite <- H51 in H39.
exists s1'''.
exact H39.
- simpl.
reflexivity.
}
exact H32.
- simpl.
auto.
}
}
+ destruct l as [|s0 l].
* apply sflist_empty.
* {
destruct l as [|s1 l].
- apply sflist_start.
- replace (s0 :: s1 :: l) with ([s0] ++ [s1] ++ l) in H5.
+ repeat rewrite app_length in H5.
simpl in H5.
repeat apply lt_S_n in H5.
apply lt_n_0 in H5.
contradiction.
+ simpl.
reflexivity.
}
- split.
+ exact H3.
+ exact H4.
Qed.
Lemma useful_g_use:
forall g: cfg _ _,
forall n: non_terminal,
appears (g_use g) (inl n) -> useful (g_use g) (inl n).
Proof.
intros g n H.
inversion H.
clear H.
destruct H0 as [right [H1 H2]].
destruct H2 as [H2| H2].
- subst.
simpl in H1.
inversion H1.
subst.
apply useful_g_g_use.
exact H0.
- simpl in H1.
inversion H1.
subst.
apply useful_g_g_use.
specialize (H3 (inl n)).
apply H3.
exact H2.
Qed.
Lemma sflist_g_use_g:
forall g: cfg _ _,
forall l: list sf,
sflist (g_use g) l -> sflist g l.
Proof.
intros g l H.
induction H.
- apply sflist_empty.
- apply sflist_start.
- apply sflist_step with (left:=left).
exact IHsflist.
exact H0.
simpl in H1.
inversion H1.
exact H2.
Qed.
Lemma produces_g_use_g:
forall g: cfg _ _,
forall s: sentence,
produces (g_use g) s -> produces g s.
Proof.
unfold produces.
unfold generates.
intros g s H.
apply derives_sflist in H.
destruct H as [l [H1 [H2 H3]]].
simpl in H2.
apply derives_sflist.
exists l.
split.
- apply sflist_g_use_g.
exact H1.
- split.
+ exact H2.
+ exact H3.
Qed.
Lemma sflist_g_g_use:
forall g: cfg _ _,
forall l: list sf,
forall s: sentence,
sflist g l /\ last l [] = map term_lift s -> sflist (g_use g) l.
Proof.
intros g l s [H1 H2].
assert (H3: length l >= 2 \/ length l < 2).
{
omega.
}
destruct H3 as [H3 | H3].
- assert (H3':=H3).
apply sflist_rules with (g:=g) in H3.
destruct H3 as [H3 _].
specialize (H3 H1).
apply sflist_rules.
+ exact H3'.
+ intros i H4.
specialize (H3 i H4).
destruct H3 as [left [right [s0 [s' [H5 [H6 H7]]]]]].
exists left, right, s0, s'.
split.
* exact H5.
* {
split.
- exact H6.
- simpl.
apply Lift_use.
+ exact H7.
+ unfold useful.
assert (H20: derives g (s0 ++ inl left :: s') (map term_lift s)).
{
apply derives_sflist.
rewrite <- (firstn_skipn i l) in H1.
apply sflist_app_l in H1.
exists (skipn i l).
split.
- exact H1.
- split.
+ rewrite hd_skip.
exact H5.
+ rewrite last_skip.
* exact H2.
* omega.
}
assert (H21: exists s'': sentence, derives g [inl left] (map term_lift s'')).
{
apply derives_split in H20.
destruct H20 as [s1' [s2' [H21 [_ H22]]]].
replace (inl left :: s') with ([inl left] ++ s') in H22.
- apply derives_split in H22.
destruct H22 as [s1'0 [s2'0 [H31 [H32 _]]]].
symmetry in H21.
apply map_expand in H21.
destruct H21 as [s1'' [s2'' [H40 [H41 H42]]]].
rewrite <- H42 in H31.
symmetry in H31.
apply map_expand in H31.
destruct H31 as [s1''' [s2''' [H50 [H51 H52]]]].
rewrite <- H51 in H32.
exists s1'''.
exact H32.
- simpl.
reflexivity.
}
exact H21.
+ intros s1 H99.
destruct s1.
* assert (H30: derives g (s0 ++ right ++ s') (map term_lift s)).
{
apply derives_sflist.
rewrite <- (firstn_skipn (S i) l) in H1.
apply sflist_app_l in H1.
exists (skipn (S i) l).
split.
- exact H1.
- split.
+ rewrite <- H6.
apply hd_skip.
+ rewrite <- H2.
apply last_skip.
omega.
}
assert (H31: exists s'': sentence, derives g right (map term_lift s'')).
{
apply derives_split in H30.
destruct H30 as [s1' [s2' [H35 [H36 H37]]]].
apply derives_split in H37.
destruct H37 as [s1'0 [s2'0 [H38 [H39 H40]]]].
symmetry in H35.
apply map_expand in H35.
destruct H35 as [s1'' [s2'' [H41 [H42 H43]]]].
rewrite <- H43 in H38.
symmetry in H38.
apply map_expand in H38.
destruct H38 as [s1''' [s2''' [H50 [H51 H52]]]].
rewrite <- H51 in H39.
exists s1'''.
exact H39.
}
assert (H32: exists s'': sentence, derives g [inl n] (map term_lift s'')).
{
apply in_split in H99.
destruct H99 as [l1 [l2 H60]].
rewrite H60 in H31.
destruct H31 as [s'' H61].
apply derives_split in H61.
destruct H61 as [s1' [s2' [H35 [H36 H37]]]].
replace (inl n :: l2) with ([inl n] ++ l2) in H37.
- apply derives_split in H37.
destruct H37 as [s1'0 [s2'0 [H38 [H39 H40]]]].
symmetry in H35.
apply map_expand in H35.
destruct H35 as [s1'' [s2'' [H41 [H42 H43]]]].
rewrite <- H43 in H38.
symmetry in H38.
apply map_expand in H38.
destruct H38 as [s1''' [s2''' [H50 [H51 H52]]]].
rewrite <- H51 in H39.
exists s1'''.
exact H39.
- simpl.
reflexivity.
}
unfold useful.
exact H32.
* simpl.
auto.
}
- destruct l as [|s0 l].
+ apply sflist_empty.
+ destruct l as [|s1 l].
* apply sflist_start.
* {
replace (s0 :: s1 :: l) with ([s0] ++ [s1] ++ l) in H3.
- repeat rewrite app_length in H3.
simpl in H3.
repeat apply lt_S_n in H3.
apply lt_n_0 in H3.
contradiction.
- simpl.
reflexivity.
}
Qed.
Lemma produces_g_g_use:
forall g: cfg _ _,
forall s: sentence,
produces g s -> produces (g_use g) s.
Proof.
unfold produces.
unfold generates.
intros g s H.
apply derives_sflist in H.
destruct H as [l [H1 [H2 H3]]].
apply derives_sflist.
exists l.
split.
- apply sflist_g_g_use with (s:=s).
split.
+ exact H1.
+ exact H3.
- split.
+ exact H2.
+ exact H3.
Qed.
Theorem g_equiv_use:
forall g: cfg _ _,
non_empty g -> g_equiv (g_use g) g.
Proof.
unfold g_equiv.
intros g H' s.
split.
- apply produces_g_use_g.
- apply produces_g_g_use.
Qed.
Theorem g_use_correct:
forall g: cfg _ _,
non_empty g ->
g_equiv (g_use g) g /\
has_no_useless_symbols (g_use g).
Proof.
intros g H'.
split.
- apply g_equiv_use.
exact H'.
- unfold has_no_useless_symbols.
intros n H.
inversion H.
destruct H0 as [right [H1 H2]].
simpl in H1.
inversion H1.
subst.
destruct H2.
+ subst.
apply useful_g_g_use in H3.
exact H3.
+ specialize (H4 (inl n) H2).
apply useful_g_g_use in H4.
exact H4.
Qed.
End Useless.