-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.v
4332 lines (3763 loc) · 108 KB
/
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
Require Import String.
Open Scope list_scope.
Require Import bin_rels.
Require Import eq_rel.
Require Import universe.
Require Import LibTactics.
Require Import tactics.
Require Import Coq.Bool.Bool.
Require Import Coq.Program.Tactics.
Require Import Omega.
Require Import Coq.Program.Basics.
Require Import Coq.Lists.List.
Require Import Coq.Init.Notations.
Require Import UsefulTypes.
Require Import Coq.Classes.DecidableClass.
Require Import Coq.Classes.Morphisms.
Notation LIn := (List.In).
(**
uncomment this and remove the line above if [univ]:=Type
Fixpoint LIn {A : Type} (a:A) (l:list A) : [univ] :=
match l with
| nil => False
| b :: m => (b = a) [+] LIn a m
end.
Definition In := 9.
*)
(* Move *)
Definition listPad {T} (def:T) (l: list T) (n: nat) : list T :=
l++(repeat def (n-length l)).
Lemma listPad_length {T} (def:T) (l: list T) (n: nat):
n <= length (listPad def l n).
Proof using.
setoid_rewrite app_length.
rewrite repeat_length.
omega.
Qed.
Fixpoint ball (l : list bool) : bool :=
match l with
| [] => true
| x :: xs => andb x (ball xs)
end.
Lemma ball_true :
forall l,
ball l = true <=> (forall x, LIn x l -> x = true).
Proof.
induction l; simpl; sp.
rw andb_eq_true.
trw IHl; split; sp.
Qed.
Lemma ball_map_true :
forall A,
forall f : A -> bool,
forall l : list A,
ball (map f l) = true <=> forall x, LIn x l -> f x = true.
Proof.
induction l; simpl; sp.
trw andb_eq_true.
trw IHl; split; sp; subst; sp.
Qed.
Fixpoint remove {A:Type } `{Deq A} (x : A) (l : list A) : list A :=
match l with
| [] => []
| y::tl => if (decide (x = y)) then remove x tl else y::(remove x tl)
end.
Theorem remove_In {A:Type } `{Deq A} : forall (l : list A) (x : A), ~ In x (remove x l).
Proof.
induction l as [|x l]; auto. simpl. intro y.
rewrite decide_decideP.
cases_if; simpl; auto.
firstorder.
Qed.
(** l \ lr -- removes the elements of lr from l *)
Fixpoint diff {T} {eqd:Deq T} (lr:list T) (l:list T) : list T :=
match lr with
| [] => l
| h::t => diff t (remove h l)
end.
Lemma remove_trivial :
forall T x eq,
forall l : list T,
(! LIn x l)
-> @remove T eq x l = l.
Proof.
induction l as [| a l]; simpl; intro H; sp.
rewrite decide_decideP.
cases_if as Heq.
destruct H. left. auto.
f_equal.
rewrite IHl. auto.
introv Hlin. apply H. auto.
Qed.
Lemma diff_nil :
forall T eq, forall l : list T, @diff T eq l [] = [].
Proof.
induction l; simpl; auto.
Qed.
Hint Rewrite diff_nil.
Typeclasses eauto := 2.
Lemma in_remove {T:Type} `{Deq T}:
forall x y, forall l : list T,
LIn x (remove y l) <=> (~ x = y) # LIn x l.
Proof.
induction l; simpl.
split; sp. rewrite decide_decideP.
cases_if; subst; allsimpl; allrw IHl; split; sp; subst; sp.
Qed.
Lemma in_diff :
forall T,
forall l1 l2 : list T,
forall x eq,
LIn x (@diff T eq l1 l2)
<=>
(LIn x l2 # (! LIn x l1)).
Proof.
induction l1; simpl; sp.
split; sp. introv. auto.
trw IHl1; trw in_remove; split; sp; auto.
Qed.
Lemma remove_app {T:Type} `{Deq T}:
forall x,
forall l1 l2 : list T,
remove x (l1 ++ l2) = remove x l1 ++ remove x l2.
Proof.
induction l1; simpl; sp. repeat rewrite decide_decideP.
cases_if; subst; rewrite IHl1; auto.
Qed.
(* Move *)
Lemma deq_refl {T:Type} `{Deq T} : forall (x:T), (decide (x=x)) = true.
Proof.
intros.
rewrite Decidable_complete; auto.
Qed.
Lemma deqP_refl {T:Type} `{Deq T} : forall (x:T), (decideP (x=x)) = left eq_refl.
Proof.
intros. destruct (decideP (x = x)); try discriminate.
- f_equal. apply Eqdep_dec.UIP_dec.
intros. apply decideP; auto.
- sp.
Qed.
Hint Rewrite (fun T d => @deq_refl T d) (fun T d => @deqP_refl T d) : SquiggleEq.
Lemma remove_comm {T:Type} `{Deq T} :
forall l : list T,
forall x y,
remove x (remove y l) = remove y (remove x l).
Proof.
induction l; simpl; sp.
repeat rewrite decide_decideP.
repeat (cases_if; subst; simpl in *; auto; autorewrite with SquiggleEq; try congruence;
repeat rewrite decide_decideP).
Qed.
Lemma diff_remove {T:Type} {eq:Deq T} :
forall l1 l2 : list T,
forall x,
@diff T eq l1 (@remove T eq x l2) = @remove _ eq x (@diff _ eq l1 l2).
Proof.
induction l1; simpl; sp.
repeat (rewrite IHl1).
rewrite remove_comm; auto.
Qed.
Lemma diff_comm :
forall T eq,
forall l1 l2 l3 : list T,
@diff _ eq l1 (@diff _ eq l2 l3) = @diff _ eq l2 (@diff _ eq l1 l3).
Proof.
induction l1; simpl; sp.
rewrite <- diff_remove.
rewrite IHl1; auto.
Qed.
Lemma diff_app_r :
forall T eq,
forall l1 l2 l3 : list T,
@diff _ eq l1 (l2 ++ l3) = @diff _ eq l1 l2 ++ @diff _ eq l1 l3.
Proof.
induction l1; simpl; sp.
rewrite remove_app.
rewrite IHl1; auto.
Qed.
Lemma diff_app_l :
forall T eq,
forall l1 l2 l3 : list T,
@diff _ eq l1 (@diff _ eq l2 l3) = @diff _ eq (l1 ++ l2) l3.
Proof.
induction l1; simpl; sp.
repeat (rewrite diff_remove).
rewrite IHl1; auto.
Qed.
Lemma remove_repeat :
forall T eq x,
forall l : list T,
@remove T eq x l = @remove T eq x (@remove T eq x l).
Proof.
induction l; simpl; sp. repeat rewrite decide_decideP.
repeat cases_if; auto. simpl.
repeat rewrite decide_decideP.
repeat cases_if; simpl; auto.
provefalse; apply H; auto.
rewrite <- IHl; auto.
Qed.
Lemma diff_repeat :
forall T eq,
forall l1 l2 : list T,
@diff _ eq l1 l2 = @diff _ eq l1 (@diff _ eq l1 l2).
Proof.
induction l1; simpl; sp.
repeat (rewrite diff_remove).
rewrite <- remove_repeat.
rewrite <- IHl1; auto.
Qed.
Lemma remove_dup :
forall T eq,
forall l1 l2 : list T,
forall x,
LIn x l1
-> @diff _ eq l1 l2 = @remove T eq x (@diff _ eq l1 l2).
Proof.
induction l1; simpl; sp; subst.
rewrite diff_remove.
rewrite <- remove_repeat; auto.
Qed.
Lemma diff_move :
forall T eq,
forall l1 l2 l3 : list T,
forall x,
@diff _ eq (l1 ++ x :: l2) l3 = @diff _ eq (x :: l1 ++ l2) l3.
Proof.
induction l1; simpl; sp.
rewrite IHl1; simpl.
rewrite remove_comm; auto.
Qed.
Lemma diff_dup :
forall T eq,
forall l1 l2 l3 : list T,
forall x,
LIn x (l1 ++ l2)
-> @diff _ eq (l1 ++ l2) l3 = @diff _ eq (l1 ++ x :: l2) l3.
Proof.
induction l1; simpl; sp; subst.
rewrite diff_remove.
apply remove_dup; auto.
rewrite diff_move; simpl.
rewrite <- remove_repeat; auto.
Qed.
Lemma in_app_iff :
forall A l l' (a:A),
LIn a (l++l') <=> (LIn a l) [+] (LIn a l').
Proof.
induction l as [| a l]; introv; simpl; try (rw IHl); split; sp.
Qed.
Lemma in_map_iff :
forall (A B : Type) (f : A -> B) l b,
LIn b (map f l) <=> {a : A $ LIn a l # b = f a}.
Proof.
induction l; simpl; sp; try (complete (split; sp)).
trw IHl; split; sp; subst; sp.
exists a; sp.
exists a0; sp.
right; exists a0; sp.
Qed.
Lemma diff_dup2 :
forall T eq,
forall l1 l2 l3 : list T,
forall x,
LIn x l1
-> @diff _ eq (l1 ++ l2) l3 = @diff _ eq (l1 ++ x :: l2) l3.
Proof.
intros; apply diff_dup.
apply in_app_iff; left; auto.
Qed.
Definition null {T} (l : list T) := forall x, !LIn x l.
Lemma null_nil : forall T, null ([] : list T).
Proof.
unfold null; sp.
Qed.
Hint Immediate null_nil.
Lemma null_nil_iff : forall T, null ([] : list T) <=> True.
Proof.
split; sp; apply null_nil.
Qed.
Hint Rewrite null_nil_iff.
Hint Resolve decideP : SquiggleEq.
Lemma null_diff :
forall T,
forall eq : Deq T,
forall l1 l2 : list T,
null (@diff _ eq l1 l2) <=> forall x, LIn x l2 -> LIn x l1.
Proof.
induction l1; simpl; sp.
trw IHl1; sp; split; sp.
assert ({a = x} + {a <> x}) by auto with SquiggleEq; sp.
right; apply_hyp.
trw in_remove; sp.
alltrewrite in_remove; sp.
apply_in_hyp p; sp; subst; sp.
Qed.
Lemma null_iff_nil : forall T, forall l : list T, null l <=> l = [].
Proof.
induction l; unfold null; simpl; split; sp.
assert ((a = a) [+] LIn a l) by (left; auto).
apply_in_hyp p; sp.
Qed.
Lemma null_cons :
forall T x,
forall l : list T,
!( null (x :: l)).
Proof.
unfold null; sp.
assert (LIn x (x :: l)) by (simpl; left; auto).
apply_in_hyp p; sp.
Qed.
Hint Immediate null_cons.
Lemma null_app :
forall T,
forall l1 l2 : list T,
null (l1 ++ l2) <=> null l1 # null l2.
Proof.
induction l1; simpl; sp; split; sp;
try (apply null_nil);
try(apply null_cons in H); sp;
try(apply null_cons in H0); sp.
Qed.
Lemma null_map :
forall A B,
forall f : A -> B,
forall l : list A,
null (map f l) <=> null l.
Proof.
induction l; simpl; sp; split; sp;
try (apply null_nil);
apply null_cons in H; sp.
Qed.
Definition nullb {T} (l : list T) := if l then true else false.
Lemma assert_nullb :
forall T,
forall l : list T,
assert (nullb l) <=> null l.
Proof.
destruct l; simpl; split; sp.
apply not_assert in H; sp.
apply null_cons in H; sp.
Qed.
Definition subsetb {T} (eq : Deq T) (l1 l2 : list T) : bool :=
nullb (@diff _ eq l2 l1).
Definition eqsetb {T} (eq : Deq T) (l1 l2 : list T) : bool :=
subsetb eq l1 l2 && subsetb eq l2 l1.
Lemma assert_subsetb :
forall T eq,
forall l1 l2 : list T,
assert (subsetb eq l1 l2)
<=>
forall x, LIn x l1 -> LIn x l2.
Proof.
sp; unfold subsetb.
trw assert_nullb; trw null_diff; split; sp.
Qed.
Lemma assert_eqsetb :
forall T eq,
forall l1 l2 : list T,
assert (eqsetb eq l1 l2)
<=>
forall x, LIn x l1 <=> LIn x l2.
Proof.
sp; unfold eqsetb; trw assert_of_andb;
repeat (trw assert_subsetb); repeat (split; sp);
apply_in_hyp p; auto.
Qed.
Fixpoint beq_list {A} (eq : Deq A) (l1 l2 : list A) : bool :=
match l1, l2 with
| [], [] => true
| [], _ => false
| _, [] => false
| x :: xs, y :: ys => if (decide (x=y)) then beq_list eq xs ys else false
end.
Lemma assert_beq_list :
forall A eq,
forall l1 l2 : list A,
assert (beq_list eq l1 l2) <=> l1 = l2.
Proof.
unfold assert.
induction l1; destruct l2; simpl; repeat rewrite decide_decideP;
split; sp; try (complete (inversion H)).
destruct (decideP (a=a0)); subst; sp.
f_equal. apply IHl1; auto.
inversion H. subst.
autorewrite with SquiggleEq.
rewrite IHl1. refl.
Qed.
Global Instance deq_list {A:Type} `{Deq A} : Deq (list A).
Proof.
intros l1 l2. exists (beq_list _ l1 l2).
apply assert_beq_list.
Defined.
Typeclasses eauto :=6.
Lemma beq_list_refl :
forall A eq,
forall l : list A,
beq_list eq l l = true.
Proof.
induction l; simpl; sp.
autorewrite with SquiggleEq.
auto.
Qed.
Lemma eq_lists :
forall A (l1 l2 : list A) x,
l1 = l2
<=>
(
length l1 = length l2
#
forall n, nth n l1 x = nth n l2 x
).
Proof.
induction l1; sp; destruct l2; sp; split; allsimpl; sp;
try(inversion H);try(inversion H0); subst; sp.
gen_some 0; subst.
assert (l1 = l2) as eq; try (rewrite eq; sp).
apply IHl1 with (x := x); sp.
gen_some (S n); sp.
Qed.
(*
Fixpoint memberb' {A} (eq : Deq A) (x : A) (l : list A) : { LIn x l } + { ! LIn x l} :=
match l return { LIn x l } + { ! LIn x l} with
| [] => right (fun x => x)
| y :: ys =>
match eq y x with
| left e => left (or_introl e)
| right _ =>
match memberb' eq x ys with
| left x => left (or_intror x)
| right y => right y
end
end
end.
*)
Fixpoint memberb {A : Type} (eq : Deq A) (x : A) (l : list A) : bool :=
match l with
| [] => false
| y :: ys => if decide (y=x) then true else memberb eq x ys
end.
Theorem assert_memberb :
forall {T:Type} {eq : Deq T} (a:T) (l: list T),
assert (memberb eq a l) <=> LIn a l.
Proof with (repeat rewrite decide_decideP).
intros. induction l. simpl.
try (unfold assert; repeat split; intros Hf; auto ; inversion Hf).
simpl ... cases_if. subst. unfold assert; repeat split; auto.
repeat split; intros Hlr. right. apply IHl; auto.
destruct Hlr as [Heq | Hin]; tauto.
Qed.
Lemma memberb_app :
forall A eq x,
forall l1 l2 : list A,
memberb eq x (l1 ++ l2) = memberb eq x l1 || memberb eq x l2.
Proof with (repeat rewrite decide_decideP).
induction l1; simpl; sp ...
destruct (decideP (a = x)); sp.
Qed.
Lemma in_app_deq :
forall A l1 l2 (a : A) (deq : Deq A),
LIn a (l1 ++ l2) -> (LIn a l1 + LIn a l2).
Proof.
introv deq i.
rw <- (@assert_memberb A deq) in i.
rw memberb_app in i.
apply assert_orb in i; sp; allrw (@assert_memberb A deq); sp.
Qed.
Lemma diff_cons_r :
forall A eq x,
forall l1 l2 : list A,
@diff _ eq l1 (x :: l2)
= if memberb eq x l1
then @diff _ eq l1 l2
else x :: @diff _ eq l1 l2.
Proof with (repeat rewrite decide_decideP).
induction l1; simpl; sp...
destruct (decideP (a=x)); subst; auto.
Qed.
Lemma diff_cons_r_ni :
forall A eq x,
forall l1 l2 : list A,
!LIn x l2 -> @diff _ eq (x :: l1) l2 = @diff _ eq l1 l2.
Proof with (repeat rewrite decide_decideP).
induction l1; simpl; sp.
induction l2; allsimpl; allrw not_over_or; sp...
destruct (decideP (x=a)); try subst; sp ;
allrw; sp.
Locate remove_trivial.
rw (remove_trivial A x); sp.
Qed.
Fixpoint maxl (ts : list nat) : nat :=
match ts with
| nil => 0
| n :: ns => max n (maxl ns)
end.
Lemma maxl_prop :
forall nats n,
LIn n nats -> n <= maxl nats.
Proof.
induction nats; simpl; sp; subst.
apply max_prop1.
allapply IHnats.
assert (maxl nats <= max a (maxl nats)) by apply max_prop2.
omega.
Qed.
Fixpoint addl (ts : list nat) : nat :=
match ts with
| nil => 0
| n :: ns => n + (addl ns)
end.
Theorem lin_flat_map :
forall (A B : Type) (f : A -> list B) (l : list A) (y : B),
LIn y (flat_map f l)
<=>
{x : A $ LIn x l # LIn y (f x)}.
Proof.
induction l; simpl; sp.
split; sp.
trw in_app_iff.
trw IHl.
split; sp; subst; sp.
exists a; sp.
exists x; sp.
right; exists x; sp.
Qed.
Theorem flat_map_empty:
forall A B (ll:list A) (f: A -> list B) , flat_map f ll =[]
<=> forall a, LIn a ll -> f a =[].
Proof. sp_iff Case.
Case "->".
intros Hmap a Hin; remember (f a) as fa; destruct fa.
auto. assert ({a: A $ LIn a ll # LIn b (f a)}) as Hass;
try (apply lin_flat_map in Hass;
rewrite Hmap in Hass; inversion Hass).
exists a. (split; auto). rewrite <- Heqfa.
constructor; auto.
Case "<-".
intros Hin.
remember (flat_map f ll) as flat; destruct flat ;auto.
assert ( LIn b (flat_map f ll)) as Hinbf by
(rewrite <- Heqflat; constructor; auto).
apply lin_flat_map in Hinbf. exrepnd.
apply Hin in Hinbf1.
rewrite Hinbf1 in Hinbf0.
inversion Hinbf0.
Qed.
Lemma flat_map_map :
forall A B C ,
forall f : B -> list C,
forall g : A -> B,
forall l : list A,
flat_map f (map g l) = flat_map (compose f g) l.
Proof.
induction l; simpl; sp.
rewrite IHl.
unfold compose; auto.
Qed.
Lemma map_map :
forall A B C ,
forall f : B -> C,
forall g : A -> B,
forall l : list A,
map f (map g l) = map (compose f g) l.
Proof.
induction l; simpl; sp.
rewrite IHl.
unfold compose; auto.
Qed.
Lemma eq_flat_maps :
forall A B,
forall f g : A -> list B,
forall l : list A,
(forall x, LIn x l -> f x = g x)
-> flat_map f l = flat_map g l.
Proof.
induction l; simpl; sp.
assert (f a = g a).
apply H; left; auto.
rewrite H0.
assert (flat_map f l = flat_map g l).
rewrite IHl; auto.
rewrite H1; auto.
Qed.
Lemma eq_maps :
forall A B,
forall f g : A -> B,
forall l : list A,
(forall x, LIn x l -> f x = g x)
-> map f l = map g l.
Proof.
induction l; simpl; sp.
assert (f a = g a).
apply H; left; auto.
rewrite H0.
rewrite IHl; auto.
Qed.
Lemma in_nth :
forall T a (l:list T),
LIn a l
-> {n : nat $ (n < length l) # a = nth n l a}.
Proof.
intros ? ? ?. induction l; intros Hin.
- simpl in Hin. contradiction.
- simpl in Hin. dorn Hin.
+ intros. subst. exists 0. split; auto. simpl. omega.
+ intros. apply IHl in Hin. exrepnd.
simpl.
exists (S n) ;split ; try (simpl; omega).
fold (app [a0] l);sp.
Qed.
(* stronger one above : no need for decidability
Lemma in_nth :
forall T a (l:list T),
Deq T
-> LIn a l
-> {n : nat $ (n < length l) # a = nth n l a}.
Proof.
intros ? ? ? deq. induction l; intros Hin.
- simpl in Hin. contradiction.
- case (deq a a0).
+ intros. subst. exists 0. split; auto. simpl. omega.
+ intros Hneq. simpl in Hin.
destruct Hin as [Heq | Hin].
* symmetry in Heq. apply Hneq in Heq. contradiction.
* apply IHl in Hin. clear Hneq. destruct Hin as [m Hp]. repnd.
exists (S m) ;split ; try (simpl; omega).
fold (app [a0] l).
rewrite app_nth2. simpl. assert (m-0 =m) as Hrew by omega.
rewrite Hrew. auto. simpl. omega.
Qed.
*)
Lemma nth_in :
forall A n (l : list A) d,
n < length l
-> LIn (nth n l d) l.
Proof.
intros A n l d H; revert n H.
induction l; simpl; sp.
destruct n; sp.
allapply S_lt_inj; sp.
Qed.
Fixpoint snoc {X:Type} (l:list X) (v:X) : (list X) :=
match l with
| nil => cons v nil
| cons h t => cons h (snoc t v)
end.
Lemma length_snoc :
forall T,
forall n : T,
forall l : list T,
length (snoc l n) = S (length l).
Proof.
intros; induction l; simpl; sp.
Qed.
Lemma snoc_as_append :
forall T, forall l : list T, forall n,
snoc l n = l ++ [n].
Proof.
intros; induction l; simpl; sp.
rewrite IHl; sp.
Qed.
Lemma snoc_append_r :
forall T, forall l1 l2 : list T, forall v : T,
(snoc l1 v) ++ l2 = l1 ++ (v :: l2).
Proof.
intros; induction l1; simpl; sp.
rewrite IHl1; sp.
Qed.
Lemma snoc_append_l :
forall T, forall l1 l2 : list T, forall v : T,
l2 ++ (snoc l1 v) = snoc (l2 ++ l1) v.
Proof.
intros; induction l2; simpl; sp.
rewrite IHl2; sp.
Qed.
Lemma in_snoc :
forall T,
forall l : list T,
forall x y : T,
LIn x (snoc l y) <=> (LIn x l [+] x = y).
Proof.
induction l; simpl; sp.
split; sp.
trw IHl.
apply sum_assoc.
Qed.
Hint Rewrite in_snoc.
Lemma snoc_cases :
forall T,
forall l : list T,
l = [] [+] {a : T $ {k : list T $ l = snoc k a}}.
Proof.
induction l.
left; auto.
sp; subst.
right; exists a; exists (@nil T); simpl; auto.
right.
exists a0; exists (a :: k); simpl; auto.
Qed.
Lemma snoc_inj :
forall T,
forall l1 l2 : list T,
forall x1 x2 : T,
snoc l1 x1 = snoc l2 x2 -> l1 = l2 # x1 = x2.
Proof.
induction l1; simpl; intros.
destruct l2; simpl in H; inversion H; subst; auto.
inversion H.
destruct l2; simpl in H1; inversion H1.
destruct l2; simpl in H.
inversion H.
destruct l1; simpl in H2; inversion H2.
inversion H.
apply IHl1 in H2. sp; subst; auto.
Qed.
Lemma map_snoc :
forall A B l x,
forall f : A -> B,
map f (snoc l x) = snoc (map f l) (f x).
Proof.
induction l; simpl; sp.
rewrite IHl; sp.
Qed.
Open Scope nat_scope.
Lemma length_app :
forall T,
forall l1 l2 : list T,
length (l1 ++ l2) = length l1 + length l2.
Proof.
induction l1; simpl; sp.
Qed.
Lemma nil_snoc_false :
forall T, forall a : list T, forall b : T, [] = snoc a b -> False.
Proof.
intros.
assert (length ([] : list T) = length (snoc a b)).
rewrite H; auto.
simpl in H0.
rewrite length_snoc in H0.
inversion H0.
Qed.
Definition subset {T} (l1 l2 : list T) :=
forall x, LIn x l1 -> LIn x l2.
Lemma fold_subset :
forall T l1 l2,
(forall x : T, LIn x l1 -> LIn x l2) = subset l1 l2.
Proof. sp. Qed.
Lemma null_diff_subset :
forall T,
forall eq : Deq T,
forall l1 l2 : list T,
null (@diff _ eq l1 l2) <=> subset l2 l1.
Proof.
sp; apply null_diff; unfold subset; split; sp.
Qed.
Lemma subsetb_subset :
forall T eq,
forall l1 l2 : list T,
assert (subsetb eq l1 l2) <=> subset l1 l2.
Proof.
intros.
apply assert_subsetb; unfold subset; split; sp.
Qed.
Lemma subset_refl :
forall T,
forall l : list T,
subset l l.
Proof.
unfold subset; sp.
Qed.
Hint Immediate subset_refl.
Lemma subset_refl_iff :
forall T,
forall l : list T,
subset l l <=> True.
Proof.
unfold subset; sp; split; sp.
Qed.
Hint Rewrite subset_refl_iff.
Lemma subset_nil_l :
forall T, forall s : list T, subset [] s.
Proof.
unfold subset; simpl; sp.
Qed.
Hint Immediate subset_nil_l.
Lemma subset_nil_l_iff :
forall T, forall s : list T, subset [] s <=> True.
Proof.
unfold subset; simpl; sp; split; sp.
Qed.
Hint Rewrite subset_nil_l_iff.
(* same as subset_nil_l *)
Lemma nil_subset :
forall T, forall l : list T, subset [] l.
Proof.
auto.
Qed.
(* same as subset_nil_l_iff *)
Lemma nil_subset_iff :
forall T,
forall l : list T,
subset [] l <=> True.
Proof.
sp; autorewrite with core; sp.
Qed.
Lemma cons_subset :
forall T,
forall x : T,
forall l1 l2 : list T,
subset (x :: l1) l2
<=> LIn x l2 # subset l1 l2.
Proof.
unfold subset; simpl; sp; split; sp; subst; auto.
Qed.
Tactic Notation "trewritec" constr(H) :=
build_and_rewrite H.
Lemma singleton_subset :
forall T,
forall x : T,
forall l : list T,
subset [x] l <=> LIn x l.
Proof.
intros.
remember (cons_subset T x [] l) as Htr.
trewrite Htr.
split; sp.
Qed.
Lemma app_subset :
forall T,
forall l1 l2 l3 : list T,
subset (l1 ++ l2) l3 <=> subset l1 l3 # subset l2 l3.
Proof.
induction l1; simpl; sp; try(split; sp; fail).
trw cons_subset. trw cons_subset.
split; introv Hlin; repnd;
try(trw IHl1); try(trw_h IHl1 Hlin; repnd);
repeat(auto;split;auto).
Qed.
Lemma subset_trans :
forall T,
forall l1 l2 l3 : list T,
subset l1 l2
-> subset l2 l3
-> subset l1 l3.
Proof.
unfold subset; sp.
Qed.
Lemma subset_cons_nil :
forall T x,
forall l : list T,
! subset (x :: l) [].
Proof.
unfold subset; sp.
assert ( LIn x (x :: l)) by (simpl; left; auto).
apply_in_hyp p; allsimpl; sp.
Qed.
Lemma subset_cons1 :
forall T,
forall x : T,
forall l1 l2 : list T,
subset l1 l2
-> subset l1 (x :: l2).
Proof.
unfold subset; simpl; sp.
Qed.
Lemma subset_cons2 :
forall T,
forall x : T,
forall l1 l2 : list T,
subset l1 l2
-> subset (x :: l1) (x :: l2).
Proof.
unfold subset; simpl; sp.
Qed.
Lemma subset_app_r :
forall T,
forall l1 l2 l3 : list T,
subset l1 l2 -> subset l1 (l2 ++ l3).
Proof.
unfold subset; intros.
apply (@in_app_iff T).