-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_list.v
1868 lines (1759 loc) · 30.5 KB
/
misc_list.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
(* ---------------------------------------------------------------------
This file is part of a repository containing the definitions and
proof scripts related to the formalization of context-free language
theory in Coq. Specifically, the following results were obtained:
(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 thesis "Formalization of
Context-Free Language Theory", submitted to the Informatics
Center of the Pernambuco Federal University (CIn/UFPE) in
Brazil.
The file README.md descbrides the contents of each file and
provides instructions to compile them.
Marcus Vinícius Midena Ramos
--------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
(* LIST LEMMAS *)
(* --------------------------------------------------------------------- *)
Require Import List.
Require Import Omega.
Require Import misc_arith.
Import ListNotations.
Open Scope list_scope.
Lemma map_expand:
forall (A B: Type) (f: A -> B) (l: list A) (s1 s2: list B),
s1 ++ s2 = map f l ->
exists s1' s2': list A,
l = s1' ++ s2' /\ map f s1' = s1 /\ map f s2' = s2.
Proof.
intros A B f l.
induction l.
- intros s1 s2 H.
simpl in H.
apply app_eq_nil in H.
destruct H as [H1 H2].
subst.
exists [], [].
auto.
- intros s1 s2 H.
simpl in H.
destruct s1.
+ simpl in H.
destruct s2.
* inversion H.
* inversion H.
exists [], (a::l). {
split.
- simpl.
reflexivity.
- split.
+ simpl.
reflexivity.
+ simpl.
reflexivity. }
+ destruct s2.
* inversion H.
exists (a::l), []. {
split.
- simpl.
rewrite app_nil_r.
reflexivity.
- split.
+ simpl.
rewrite <- H2.
rewrite app_nil_r.
reflexivity.
+ simpl.
reflexivity. }
* inversion H.
specialize (IHl s1 (b0::s2)).
specialize (IHl H2).
destruct IHl as [s1' [s2' [H3 [H4 H5]]]].
exists (a::s1').
exists s2'. {
split.
- rewrite H3.
simpl.
reflexivity.
- split.
+ simpl.
rewrite H4.
reflexivity.
+ rewrite H5.
reflexivity. }
Qed.
Lemma equal_app (A: Type) :
forall l1 l2 s1 s2 : list A,
l1 ++ l2 = s1 ++ s2 ->
(exists l, s1 = l1 ++ l /\ l2 = l ++ s2) \/ (exists l, l1 = s1 ++ l /\ s2 = l ++ l2).
Proof.
intros l1.
induction l1 as [|a l1 IH].
- intros l2 s1 s2 H.
simpl in H.
subst.
simpl.
left.
exists s1.
split.
reflexivity.
reflexivity.
- intros l2 s1 s2 H.
simpl in H.
destruct s1 as [|a' s1].
+ simpl in H.
subst.
simpl.
right.
exists (a::l1).
split.
* reflexivity.
* simpl.
reflexivity.
+ simpl in H.
inversion H.
subst a'.
clear H.
apply IH in H2.
destruct H2 as [(l & ? & ?) | (l & ? & ?)].
* subst.
simpl.
left.
exists l. {
split.
- reflexivity.
- reflexivity. }
* right.
exists l. {
split.
- rewrite H.
simpl.
reflexivity.
- rewrite H0.
reflexivity. }
Qed.
Lemma last_last:
forall A: Type,
forall s d: A,
forall l: list A,
last (l++[s]) d = s.
Proof.
intros A s d l.
induction l.
- simpl.
reflexivity.
- simpl.
destruct l.
+ simpl.
reflexivity.
+ rewrite <- app_comm_cons.
exact IHl.
Qed.
Lemma length_ge_0:
forall A: Type,
forall l: list A,
length l >= 0.
Proof.
intros A l.
destruct l.
- simpl.
omega.
- simpl.
omega.
Qed.
Lemma length_ge_2:
forall A: Type,
forall a1 a2: A,
forall l: list A,
length (a1::a2::l) >= 2.
Proof.
intros A a1 a2 l.
simpl.
apply Le.le_n_S.
apply Le.le_n_S.
apply Le.le_0_n.
Qed.
Lemma length_lt_1:
forall A: Type,
forall l: list A,
length l < 1 -> l = [].
Proof.
intros A l H.
destruct l.
- reflexivity.
- replace (a::l) with ([a]++l) in H.
+ rewrite app_length in H.
simpl in H.
apply lt_S_n in H.
apply lt_n_O in H.
contradiction.
+ simpl.
reflexivity.
Qed.
Lemma nth_cons:
forall A: Type,
forall a e: A,
forall l: list A,
forall i: nat,
nth (S i) (a::l) e = nth i l e.
Proof.
intros A a e l i.
simpl.
destruct i.
- simpl.
reflexivity.
- reflexivity.
Qed.
Lemma length_not_zero:
forall A: Type,
forall l: list A,
length l > 0 -> [] <> l.
Proof.
intros A l H.
destruct l.
- simpl in H.
apply gt_irrefl in H.
contradiction.
- apply nil_cons.
Qed.
Lemma length_not_zero_inv:
forall A: Type,
forall l: list A,
l <> [] -> length l > 0.
Proof.
intros A l H.
destruct l.
- destruct H.
reflexivity.
- simpl.
omega.
Qed.
Lemma not_nil:
forall A: Type,
forall l: list A,
l <> [] -> length l <> 0.
Proof.
destruct l.
- intro H.
auto.
- intro H.
replace (a::l) with ([a]++l).
+ rewrite app_length.
simpl.
apply not_eq_sym.
apply O_S.
+ simpl.
reflexivity.
Qed.
Lemma not_nil_ge_1:
forall A: Type,
forall l: list A,
l <> [] -> length l >= 1.
Proof.
intros A l H.
apply not_nil in H.
destruct l.
- simpl in H.
destruct H.
reflexivity.
- simpl.
apply le_n_S.
apply le_0_n.
Qed.
Lemma list_last:
forall A: Type,
forall d: A,
forall l: list A,
removelast l <> [] -> nth (length (removelast l) - 1) (removelast l ++ [last l d]) d = last (removelast l) d.
Proof.
intros A d l H.
rewrite app_nth1 with (l:=removelast l).
- rewrite app_removelast_last with (l:=removelast l)(d:=d).
+ rewrite app_nth2.
* rewrite app_length.
assert (H1: Datatypes.length (removelast (removelast l)) + 1 - 1 -
Datatypes.length (removelast (removelast l)) = 0).
{
omega.
}
simpl.
rewrite H1.
rewrite last_last.
reflexivity.
* rewrite app_length.
simpl.
omega.
+ exact H.
- apply n_minus_1.
apply not_nil.
exact H.
Qed.
Lemma length_zero:
forall A: Type,
forall l: list A,
length l = 0 -> l = [].
Proof.
intros A l H.
destruct l.
- reflexivity.
- simpl in H.
symmetry in H.
apply O_S in H.
contradiction.
Qed.
Lemma list_single:
forall A: Type,
forall l: list A,
forall d e: A,
nth 0 l d = e -> length l = 1 -> l = [e].
Proof.
intros A l d e H1 H2.
destruct l.
- simpl in H2.
apply O_S in H2.
contradiction.
- simpl in H1.
rewrite H1.
simpl in H2.
inversion H2.
apply length_zero in H0.
rewrite H0.
reflexivity.
Qed.
Lemma hd_nth0:
forall A: Type,
forall d: A,
forall l: list A,
hd d l = nth 0 l d.
Proof.
intros A d l.
destruct l.
- simpl.
reflexivity.
- simpl.
reflexivity.
Qed.
Lemma last_nth1:
forall A: Type,
forall d: A,
forall l: list A,
length l = 2 -> last l d = nth 1 l d.
Proof.
intros A d l H.
destruct l.
- simpl.
reflexivity.
- simpl.
destruct l.
+ simpl in H.
inversion H.
+ replace (a :: a0 :: l) with ([a] ++ [a0] ++ l) in H.
* repeat rewrite app_length in H.
simpl in H.
inversion H.
apply length_zero in H1.
subst.
simpl.
reflexivity.
* simpl.
reflexivity.
Qed.
Lemma hd_tl_nth1:
forall A: Type,
forall d: A,
forall l: list A,
length l > 2 -> hd d (tl l) = nth 1 l d.
Proof.
intros A d l H.
destruct l.
- simpl.
reflexivity.
- simpl.
apply hd_nth0.
Qed.
Lemma last_tl_last:
forall A: Type,
forall d: A,
forall l: list A,
length l > 2 -> last (tl l) d = last l d.
Proof.
intros A d l H.
destruct l.
- simpl.
reflexivity.
- simpl.
destruct l.
+ simpl in H.
apply gt_S_n in H.
apply lt_n_0 in H.
contradiction.
+ reflexivity.
Qed.
Lemma nth_S:
forall A: Type,
forall i: nat,
forall s s0 s1: A,
forall l: list A,
nth (S i) (s::s0::s1::l) = nth i (s0::s1::l).
Proof.
intros A i s s0 s1 l.
destruct i.
- simpl.
reflexivity.
- destruct i.
+ simpl.
reflexivity.
+ destruct i.
* simpl.
reflexivity.
* simpl.
reflexivity.
Qed.
Lemma skipn_S:
forall i: nat,
forall A: Type,
forall a: A,
forall l: list A,
skipn (S i) (a :: l) = skipn i l.
Proof.
intros i A a l.
destruct i.
- simpl.
reflexivity.
- simpl.
destruct l.
+ reflexivity.
+ reflexivity.
Qed.
Lemma hd_skip:
forall i: nat,
forall A: Type,
forall d: A,
forall l: list A,
hd d (skipn i l) = nth i l d.
Proof.
intros i A d.
induction i as [| i IH].
- intros l.
destruct l.
+ trivial.
+ trivial.
- intros l.
destruct l.
+ trivial.
+ simpl.
rewrite IH.
reflexivity.
Qed.
Lemma last_skip:
forall i: nat,
forall A: Type,
forall d: A,
forall l: list A,
i < length l -> last (skipn i l) d = last l d.
Proof.
intros i A d.
induction i as [|i IH].
- intros l.
destruct l.
+ trivial.
+ trivial.
- intros l.
destruct l.
+ trivial.
+ simpl.
intros Hl.
assert (Hl' : i < length l) by omega.
rewrite IH.
* {
destruct l.
- simpl in Hl'.
omega.
- trivial.
}
* trivial.
Qed.
Lemma hd_hd:
forall A: Type,
forall d: A,
forall l l': list A,
length l >= 1 -> hd d l = hd d (l ++ l').
Proof.
intros A d l l' H.
destruct l.
- simpl in H.
apply le_Sn_0 in H.
contradiction.
- simpl.
reflexivity.
Qed.
Lemma hd_first:
forall A: Type,
forall d: A,
forall l: list A,
forall i: nat,
i >= 1 -> hd d (firstn i l) = hd d l.
Proof.
intros A d l i H.
destruct l.
- destruct i.
+ simpl.
reflexivity.
+ simpl.
reflexivity.
- destruct i.
+ apply le_Sn_0 in H.
contradiction.
+ simpl.
reflexivity.
Qed.
Lemma firstn_empty:
forall A: Type,
forall a: A,
forall i: nat,
a :: firstn i [] = [a].
Proof.
intros A a i.
destruct i.
- simpl.
reflexivity.
- simpl.
reflexivity.
Qed.
Lemma firstn_single:
forall A: Type,
forall a: A,
forall i: nat,
firstn (S i) [a] = [a].
intros A a i.
destruct i.
- simpl.
reflexivity.
- simpl.
reflexivity.
Qed.
Lemma nth_S':
forall i: nat,
forall A: Type,
forall a d: A,
forall l: list A,
nth (S i) (a :: l) d = nth i l d.
Proof.
intros i A a d l.
destruct i.
- simpl. reflexivity.
- simpl. reflexivity.
Qed.
Lemma firstn_S:
forall A: Type,
forall a: A,
forall l: list A,
forall i: nat,
firstn (S i) (a :: l) = a :: firstn i l.
Proof.
intros A a l.
induction l.
- intros i. simpl. reflexivity.
- intros i. destruct i.
+ simpl. reflexivity.
+ simpl. reflexivity.
Qed.
Lemma last_cons:
forall A: Type,
forall a d: A,
forall l: list A,
l <> [] -> last (a :: l) d = last l d.
Proof.
intros A a d l H.
induction l.
- destruct H. reflexivity.
- trivial.
Qed.
Lemma not_nil_firstn:
forall A: Type,
forall l: list A,
forall i: nat,
l <> [] -> firstn (S i) l <> [].
Proof.
intros A l i H.
destruct l.
- simpl. exact H.
- simpl. apply not_eq_sym. apply nil_cons.
Qed.
Lemma nil_not_nil:
forall A: Type,
forall l: list A,
l = [] \/ l <> [].
Proof.
destruct l.
- left.
reflexivity.
- right.
apply not_eq_sym.
apply nil_cons.
Qed.
Lemma last_first_nth:
forall A: Type,
forall d: A,
forall l: list A,
forall i: nat,
length l >= S i -> last (firstn (S i) l) d = nth i l d.
Proof.
intros A d l.
induction l.
- destruct i.
+ trivial.
+ trivial.
- destruct i.
+ trivial.
+ intros H.
rewrite nth_S'.
rewrite <- IHl.
* rewrite firstn_S.
{
rewrite last_cons.
- reflexivity.
- apply not_nil_firstn.
specialize (IHl i).
replace (a::l) with ([a]++l) in H.
+ rewrite app_length in H.
simpl in H.
assert (H1: length l > 0) by omega.
destruct l.
* simpl in H1.
omega.
* apply not_eq_sym.
apply nil_cons.
+ simpl.
reflexivity.
}
* {
replace (a::l) with ([a]++l) in H.
- rewrite app_length in H.
simpl in H.
omega.
- simpl.
reflexivity.
}
Qed.
Lemma nth_last:
forall A: Type,
forall l: list A,
forall d: A,
nth (length l - 1) l d = last l d.
Proof.
intros A l d.
destruct l.
- simpl.
reflexivity.
- remember (a::l) as l0.
rewrite app_removelast_last with (l:=l0) (d:=d).
+ rewrite app_length.
simpl.
rewrite last_last.
rewrite app_nth2.
* assert (H: (Datatypes.length (removelast l0) + 1 - 1 - Datatypes.length (removelast l0) = 0)) by omega.
rewrite H.
simpl.
reflexivity.
* omega.
+ rewrite Heql0.
apply not_eq_sym.
replace (a::l) with ([]++a::l).
* apply app_cons_not_nil.
* simpl.
reflexivity.
Qed.
Lemma hd_app:
forall A: Type,
forall l l': list A,
forall a d: A,
l <> [] ->
l' <> [] ->
hd d l = hd d l' ->
hd d (l ++ [a]) = hd d (l' ++ [a]).
Proof.
intros A l l' a d H1 H2 H3.
destruct l.
- destruct l'.
+ simpl.
reflexivity.
+ destruct H1.
reflexivity.
- destruct l'.
+ destruct H2.
reflexivity.
+ simpl.
simpl in H3.
exact H3.
Qed.
Lemma last_2_1:
forall A: Type,
forall l: list A,
forall a1 a2 d: A,
last (a1 :: a2 :: l) d = last (a2 :: l) d.
Proof.
intros A l a1 a2 d.
simpl.
reflexivity.
Qed.
Lemma last_app:
forall A: Type,
forall l l': list A,
forall a d: A,
l <> [] ->
l' <> [] ->
last l d = last l' d ->
last ([a] ++ l) d = last ([a] ++ l') d.
Proof.
intros A l l' a d H1 H2 H3.
induction l, l'.
- simpl.
reflexivity.
- destruct H1.
reflexivity.
- destruct H2.
reflexivity.
- replace ([a] ++ a0 :: l) with (a :: a0 :: l).
+ replace ([a] ++ a1 :: l') with (a :: a1 :: l').
* repeat rewrite last_2_1.
exact H3.
* simpl.
reflexivity.
+ simpl.
reflexivity.
Qed.
Lemma hd_not_nil:
forall A: Type,
forall l: list A,
forall a d: A,
d <> a ->
hd d l = a ->
l <> [].
Proof.
intros A l a d H1 H2.
destruct l.
- simpl in H2.
contradiction.
- apply not_eq_sym.
apply nil_cons.
Qed.
Lemma hd_not_nil_v2:
forall A: Type,
forall l: list A,
forall d: A,
hd d l <> d -> l <> [].
Proof.
intros A l d H.
destruct l.
- simpl in H.
destruct H.
reflexivity.
- apply not_eq_sym.
apply nil_cons.
Qed.
Lemma hd_not_zero:
forall A: Type,
forall l: list A,
forall a d: A,
d <> a ->
hd d l = a ->
length l > 0.
Proof.
intros A l a d H1 H2.
destruct l.
- simpl in H2.
contradiction.
- simpl.
apply gt_Sn_O.
Qed.
Lemma hd_removelast:
forall A: Type,
forall s d: A,
forall l l': list A,
l <> [] -> hd d (removelast (s :: l) ++ l') = s.
Proof.
intros A s d l l' H.
destruct l.
- destruct H.
reflexivity.
- simpl.
reflexivity.
Qed.
Lemma length_cons_gt_1:
forall A: Type,
forall s: A,
forall l: list A,
length (s :: l) > 1 -> l <> [].
Proof.
intros A s l H.
replace (s :: l) with ([s] ++ l) in H.
- rewrite app_length in H.
simpl in H.
apply gt_S_n in H.
apply not_eq_sym.
apply length_not_zero.
exact H.
- simpl.
reflexivity.
Qed.
Lemma length_cons_eq_1:
forall A: Type,
forall s: A,
forall l: list A,
length (s :: l) = 1 -> l = [].
Proof.
intros A s l H.
destruct l.
- reflexivity.
- simpl in H.
apply eq_add_S in H.
symmetry in H.
apply O_S in H.
contradiction.
Qed.
Lemma not_exists_forall_not:
forall A B: Type,
forall l: list (A+B),
~ (exists a': A, l = [inl a']) ->
forall a': A, (l <> [inl a']).
Proof.
intros A B l H a'.
unfold not at 1 in H.
unfold not.
destruct l.
- intro H1.
inversion H1.
- intro H1.
apply H.
exists a'.
exact H1.
Qed.
Lemma in_in:
forall A: Type,
forall s: A,
forall s1 s2: list A,
In s (s1 ++ [s] ++ s2).
Proof.
intros A s s1 s2.
induction s1.
- simpl.
left.
reflexivity.
- apply in_app_iff.
right.
simpl.
left.
reflexivity.
Qed.
Lemma in_out:
forall A: Type,
forall s: A,
forall l s1 s2: list A,
l = s1 ++ [s] ++ s2 ->
In s l.
Proof.
intros A s l s1 s2 H.
rewrite H.
apply in_in.
Qed.
Lemma exists_length_eq_2:
forall A: Type,
forall l: list A,
length l = 2 ->
exists s1 s2: A,
l = [s1] ++ [s2].
Proof.
intros A l H.
destruct l.
- simpl in H.
apply O_S in H.
contradiction.
- destruct l.
+ simpl in H.
apply eq_add_S in H.
apply O_S in H.
contradiction.
+ destruct l.
* exists a, a0.
reflexivity.
* simpl in H.
repeat apply eq_add_S in H.
symmetry in H.
apply O_S in H.
contradiction.
Qed.
Lemma exists_length_gt_2:
forall A: Type,
forall d: A,
forall l: list A,
length l > 2 ->
exists s1 s2: A,
exists l': list A,
l = [s1] ++ l' ++ [s2].
Proof.
intros A d l H.
destruct l.
- simpl in H.
apply lt_n_0 in H.
contradiction.
- destruct l.
+ simpl in H.
apply gt_S_n in H.
apply lt_n_0 in H.
contradiction.
+ destruct l.
* simpl in H.
repeat apply gt_S_n in H.
apply lt_n_0 in H.
contradiction.
* assert (H1: l = [] \/ l <> []).
{
apply nil_not_nil.
}
{
destruct H1 as [H1 | H1].
- subst.
exists a, a1, [a0].
reflexivity.
- change (a :: a0 :: a1 :: l) with (([a] ++ [a0] ++ [a1]) ++ l) in H.
exists a.
rewrite app_removelast_last with (A:=A) (l:=l) (d:=d).
+ exists (last l d).
exists (a0 :: a1 :: removelast l).
reflexivity.
+ exact H1.
}
Qed.
Lemma exists_length_ge_2:
forall A: Type,
forall def: A,
forall l: list A,
length l >= 2 ->
exists s1 s2: A,
exists l': list A,
l = [s1] ++ l' ++ [s2].
Proof.
intros A def l H1.
assert (H2: length l > 2 \/ length l = 2) by omega.
destruct H2 as [H2 | H2].