-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChain.v
2296 lines (1887 loc) · 60.3 KB
/
Chain.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 HoTT.
(*Require Import Nat.*)
(*Local Unset Elimination Schemes.*)
Generalizable Variables A B P x y z w.
Set Impicit Arguments.
(** This section should really be in some other place. TODO: move it. *)
Section Rel_eq.
(** Heterogeneous equality for 2-parametric type family. *)
Local Definition rel_eq {A: Type} {x y x' y': A} (P: relation A)
(sx: x = x') (sy: y = y') (u: P x y) (u': P x' y'): Type.
path_induction.
exact (u = u').
Defined.
Local Definition rel_eq_ex {A: Type} {x y x' y': A}
(P: relation A) (u: P x y) (u': P x' y'): Type
:= exists (sx: x = x') (sy: y = y'), rel_eq P sx sy u u'.
(** Proof that rel_ex can be defined inductively. *)
Inductive jmeq2 {A: Type} {x y: A} (P: relation A)
(u: P x y): forall {x' y': A}, (P x' y') -> Type
:= ideq: jmeq2 P u u.
(* Scheme jmeq2_ind := Induction for jmeq2 Sort Type.
Scheme jmeq2_rec := Minimality for jmeq2 Sort Type.
Definition jmeq2_rect := jmeq2_ind.*)
Definition jmeq2_to_rel_eq_ex {A: Type} {x y x' y': A} (P: relation A)
(u: P x y) (u': P x' y'): (jmeq2 P u u') -> (rel_eq_ex P u u').
intro jm_s; induction jm_s.
exact (idpath; (idpath; idpath)).
Defined.
Definition rel_eq_ex_to_jmeq2 {A: Type} {x y x' y': A} (P: relation A)
(u: P x y) (u': P x' y'): (rel_eq_ex P u u') -> (jmeq2 P u u').
intros (sx & sy & p).
path_induction.
revert p; simpl.
intro su; induction su; done.
Defined.
Lemma jmeq2_eq_rel_eq_eq {A: Type} {x y x' y': A}
(P: relation A) (u: P x y) (u': P x' y')
: IsEquiv (jmeq2_to_rel_eq_ex P u u').
Proof.
refine (BuildIsEquiv _ _ (jmeq2_to_rel_eq_ex P u u') _ _ _ _ ).
- exact (rel_eq_ex_to_jmeq2 P u u').
- unfold Sect.
intros (? & ? & ? ).
path_induction.
revert proj2_sig.
unfold rel_eq; simpl.
path_induction.
compute; done.
- unfold Sect.
induction x0.
compute; done.
- induction x0; done.
Qed.
End Rel_eq.
Local Notation "u ~= v" := (jmeq2 _ u v) (at level 75).
(*Local Notation "u ~= v" := ((_; (_; u)) = (_; (_; v))) (at level 75).*)
(** Type of chains - parametric analogue of List. Main example: chains of morphisms in a category. Most of functions and lemmas defined for lists are more or less directly applicable for chains. *)
Inductive Chain {A: Type} (P: relation A) : relation A :=
| idchain {x: A} : Chain P x x
| concat {x y z: A} : (P y z) -> (Chain P x y) -> (Chain P x z).
Arguments idchain [A P x].
Arguments concat [A P x y z] _ _.
(*Scheme Chain_ind := Induction for Chain Sort Type.
Scheme Chain_rec := Minimality for Chain Sort Type.
Definition Chain_rect := Chain_ind.*)
Delimit Scope Chain_scope with Chain.
Bind Scope Chain_scope with Chain.
Local Open Scope Chain_scope.
(** Concatenation of two chains *)
Definition app {A: Type} {P: relation A} {x y z: A}
(fs: Chain P y z) (gs: Chain P x y) : Chain P x z.
intros.
induction fs; [ exact gs | exact (concat p (IHfs gs)) ].
Defined.
Module ChainNotations.
Infix "++" := app (right associativity, at level 60) : Chain_scope.
Infix "::" := concat (at level 60, right associativity) : Chain_scope.
Notation "[ ]" := idchain : Chain_scope.
Notation "[ x ]" := (x :: []) : Chain_scope.
Notation "[ x ; .. ; y ]" := (x :: .. (y :: []) .. ) : Chain_scope.
Notation "x ~> y :> P" := (Chain P x y) (at level 99, y at next level).
Notation "x ~> y" := (x ~> y :> _ ) (at level 99, y at next level).
End ChainNotations.
Import ChainNotations.
Section Chains.
Context {A : Type} {P: relation A}.
Implicit Types x y z w: A.
(** Length of chain *)
Fixpoint length {x y: A} (fs: Chain P x y) : nat :=
match fs with
| [] => O
| f :: fs' => S (length fs')
end.
(** Head and tail *)
Definition hd `(default: P x x) `(cs: Chain P x z) : { y: A & P y z }.
induction cs; [exact (x; default) | exact (y; p)].
Defined.
Definition hd_error `(cs: Chain P x z): option { y: A & P y z}.
induction cs; [exact error | exact (value (y; p)) ].
Defined.
Definition tl `(cs: Chain P x z): { y: A & Chain P x y }.
induction cs; [ exact (x; []) | exact (y; cs) ].
Defined.
(** The [In] predicate *)
Fixpoint In `(f: P z w) `(cs: Chain P x y) : Type :=
match cs with
| [] => Empty
| c :: cs' => (f ~= c) + In f cs'
end.
End Chains.
Notation "head" = hd (only parsing).
Notation "tail" = tl (only parsing).
(** Opposite relation $P^{op} x y = P y x$. *)
Definition opp `(P: relation A): relation A :=
fun (x y: A) => P y x.
(** Reverse chain. Its elements lie in dual relation P^op x y = P y x. *)
Fixpoint rev {A: Type} {P: relation A} `(fs: Chain P x y) : Chain (opp P) y x
:= match fs with
| [] => []
| f :: fss => (rev fss) ++ [f]
end.
(** opp opp = id *)
Lemma opp_involutive {A: Type} {P: relation A} : opp (opp P) = P.
unfold opp; auto.
Qed.
Section app_Facts.
Context {A: Type} {P: relation A}.
(** Left and right unit laws for ++. *)
Lemma app_nil_l `(fs: x ~> y :> P): [] ++ fs = fs.
induction fs; auto.
Qed.
Lemma app_nil_r `(fs: x ~> y :> P): fs ++ [] = fs.
induction fs; simpl; [auto | f_ap ].
Qed.
(** ++ is associative. *)
Lemma app_assoc `(fs: z ~> w :> P) `(gs: y ~> z :> P) `(hs: x ~> y :> P):
(fs ++ gs) ++ hs = fs ++ (gs ++ hs).
induction fs; simpl; [ auto | f_ap ].
Qed.
End app_Facts.
Section rev_Facts.
Context {A: Type}.
(** Distributivity of rev over ++. *)
Lemma rev_distr {P: relation A} `(fs: y ~> z :> P) `(gs: x ~> y :> P):
rev (fs ++ gs) = (rev gs) ++ (rev fs).
induction fs; simpl.
exact ((app_nil_r (rev gs))^).
transitivity ((rev gs ++ rev fs) ++ [p]).
f_ap.
exact (app_assoc (rev gs) (rev fs) [p]).
Qed.
(** rev rev = id *)
Lemma rev_involutive {P: relation A} `(fs: x ~> y :> P): rev (rev fs) = fs.
induction fs; [auto | simpl ].
transitivity ([p] ++ (rev (rev fs))).
apply (rev_distr (rev fs) [p]).
simpl; f_ap.
Qed.
End rev_Facts.
(** n-th elemen of chain, error if n>length *)
Fixpoint nth {A: Type} `(cs: x ~> y :> P) (n: nat)
: option (exists (w z: A), P w z) :=
match cs return option (exists (w z: A), P w z) with
| [] => error
| @concat _ _ x' y' z' c cs' => match n with
| O => value (y'; (z'; c))
| S n' => nth cs' n'
end
end.
Section Actions.
Context {A: Type} {P: relation A} {F: A -> Type}.
(** Left action of P on type family F (covariant functors). *)
Definition LAction (P: relation A) (F: A -> Type) : Type
:= `(P x y -> F x -> F y).
(** Right action of P on type family F (contravariant functors). *)
Definition RAction (P: relation A) (F: A -> Type) : Type
:= `(P x y -> F y -> F x).
(** By default any action is right action *)
Notation Action P F := (RAction P F).
(*
(** Left action of P is right action of P^op *)
Lemma left_right : (LAction P F = RAction (opp P) F).
unfold LAction, RAction, opp.
*)
(** (f: Fx; [p_n, .. , p_1]: Chain x y) |-> (..(f * p_n)* ..)* p_1 *)
Fixpoint foldl {x y: A} (m: RAction P F) (f: F y)
(cs: Chain P x y) : F x.
induction cs; [exact f | apply IHcs].
exact (m y z p f).
Defined.
(** (f: Fx; [p_n, .. , p_1]: Chain x y) |-> p_n(.. p_1(f) ..) *)
Fixpoint foldr {x y: A} (m: LAction P F) (f: F x)
(cs: Chain P x y) : F y.
induction cs; [exact f | exact (m y z p (IHcs f)) ].
Defined.
End Actions.
(***************************************************)
(** * Applying functions to the elements of a chain *)
(***************************************************)
Section Chain_map.
Record Chain_map {A B: Type} (PA: relation A) (PB: relation B) := {
base: A -> B;
fam: forall {x y: A}, PA x y -> PB (base x) (base y)
}.
Global Coercion base: Chain_map >-> Funclass.
Global Arguments base [A B PA PB] c _.
Global Arguments fam [A B PA PB] c {x y} _. (* somewhy can't declare another sequence of elements. Reason: 'Multiple sequences of implicit arguments available only for references that cannot be applied to an arbitrarily large number of arguments.' *)
Definition ap_chain {A B: Type} {PA: relation A} {PB: relation B}
(F: Chain_map PA PB) {x y x' y': A} {u: PA x y} {u': PA x' y'}
(su: u ~= u'): ((fam F u) ~= (fam F u')).
induction su; done.
Defined.
(************)
(** ** Map *)
(************)
(** Map between pairs (A,PA) and (B, PB) *)
Definition map {A B: Type} {PA: relation A} {PB: relation B}
{x y: A} (F: Chain_map PA PB) (cs: x ~> y :> PA)
: (F x) ~> (F y) :> PB.
induction cs.
exact [].
exact ((fam F p) :: IHcs).
Defined.
(** Map between type families over a shared base *)
Definition map_base {A: Type} {P Q: relation A} {x y: A}
(f: forall x y: A, P x y -> Q x y)
(cs: x ~> y :> P) : x ~> y :> Q
:= map (@Build_Chain_map A A P Q idmap f) cs.
End Chain_map.
(******************)
(** ** Map facts *)
(******************)
Section Chain_map_facts.
Context {A B: Type} {PA: relation A} {PB: relation B}.
Context (F: Chain_map PA PB).
Implicit Types (x y z w: A).
(** f $ x :: xs = (f x) :: (f $ xs) *)
Lemma map_cons {x y z: A} (p: PA y z) (ps: x ~> y :> PA)
: map F (p :: ps) = (fam F p) :: (map F ps).
Proof.
reflexivity.
Qed.
(** Maps preserve inclusion of elements. *)
Lemma in_map :
forall {x y z w: A} (ps: x ~> y :> PA) (p: PA z w), In p ps
-> In (fam F p) (map F ps).
Proof.
intros; induction ps.
revert X; auto.
rewrite (map_cons p0 ps).
simpl.
assert ((p ~= p0) + In p ps).
revert X; done.
destruct X0.
- exact (inl (ap_chain F j)).
- exact (inr (IHps i)).
Qed.
(** Chain maps preserve length. *)
Lemma map_length {x y: A} (cs: x ~> y :> PA) :
length (map F cs) = length cs.
Proof.
induction cs; [ exact idpath | simpl; f_ap].
Qed.
(*
Section In_map_iff.
Context {x y z w: A} (cs: x ~> y :> PA) (zt wt: B) (t: PB zt wt).
Local Definition in_to_ex: In t (map F cs) -> {z' : A & {w' : A &
{p : PA z' w' & (t ~= fam F p) * In p cs}}}.
induction cs; simpl.
done.
intros [ eq_path | ind_cs ].
- refine (y; (z0; (p; _))).
exact (eq_path, inl (ideq PA p)).
- apply (IHc c) in ind_cs.
revert ind_cs.
intros (z' & w' & p' & fib_eq & p_in_cs).
exact (z'; (w'; (p'; (fib_eq, inr p_in_cs)))).
Defined.
Local Definition ex_to_in: {z' : A & {w' : A & {p : PA z' w' &
(t ~= fam F p) * In p cs}}} -> In t (map F cs).
intros (z' & w' & p' & t_eq & p_in ).
apply (in_map cs p') in p_in.
induction t_eq; auto.
Defined.
(** An element lies in f(chain) only if its preimage lies in chain.*)
Lemma in_map_iff : Equiv (In t (map F cs))
(exists (z' w': A) (p: PA z' w'), (t ~= fam F p) * (In p cs)).
Proof.
refine (BuildEquiv (In t (map F cs))
(exists (z' w': A) (p: PA z' w'), (t ~= fam F p) * (In p cs))
in_to_ex _).
refine (BuildIsEquiv (In t (map F cs))
(exists (z' w': A) (p: PA z' w'), (t ~= fam F p) * (In p cs))
in_to_ex ex_to_in _ _ _).
- admit. (*unfold Sect.
intros (z' & w' & p' & t_eq & p_in ).
induction t_eq.
unfold ex_to_in.
unfold in_map.
simpl.
induction *)
- admit. (*unfold Sect.
intro t_in.
unfold in_to_ex. simpl. *)
- admit.
Qed.
End In_map_iff.
*)
Lemma map_nth `(cs: x ~> y):
nth (map F cs) n = fmap F (nth cs n).
Proof.
induction l; simpl map; destruct n; firstorder.
Qed.
Lemma map_nth_error : forall n l d,
nth_error l n = Some d -> nth_error (map l) n = Some (f d).
Proof.
induction n; intros [ | ] ? Heq; simpl in *; inversion Heq; auto.
Qed.
Lemma map_app : forall l l',
map (l++l') = (map l)++(map l').
Proof.
induction l; simpl; auto.
intros; rewrite IHl; auto.
Qed.
Lemma map_rev : forall l, map (rev l) = rev (map l).
Proof.
induction l; simpl; auto.
rewrite map_app.
rewrite IHl; auto.
Qed.
Lemma map_eq_nil : forall l, map l = [] -> l = [].
Proof.
destruct l; simpl; reflexivity || discriminate.
Qed.
(** [flat_map] *)
Definition flat_map (f:A -> list B) :=
fix flat_map (l:list A) : list B :=
match l with
| nil => nil
| cons x t => (f x)++(flat_map t)
end.
Lemma in_flat_map : forall (f:A->list B)(l:list A)(y:B),
In y (flat_map f l) <-> exists x, In x l /\ In y (f x).
Proof using A B.
induction l; simpl; split; intros.
contradiction.
destruct H as (x,(H,_)); contradiction.
destruct (in_app_or _ _ _ H).
exists a; auto.
destruct (IHl y) as (H1,_); destruct (H1 H0) as (x,(H2,H3)).
exists x; auto.
apply in_or_app.
destruct H as (x,(H0,H1)); destruct H0.
subst; auto.
right; destruct (IHl y) as (_,H2); apply H2.
exists x; auto.
Qed.
End Map.
Lemma flat_map_concat_map : forall A B (f : A -> list B) l,
flat_map f l = concat (map f l).
Proof.
intros A B f l; induction l as [|x l IH]; simpl.
+ reflexivity.
+ rewrite IH; reflexivity.
Qed.
Lemma concat_map : forall A B (f : A -> B) l, map f (concat l) = concat (map (map f) l).
Proof.
intros A B f l; induction l as [|x l IH]; simpl.
+ reflexivity.
+ rewrite map_app, IH; reflexivity.
Qed.
Lemma map_id : forall (A :Type) (l : list A),
map (fun x => x) l = l.
Proof.
induction l; simpl; auto; rewrite IHl; auto.
Qed.
Lemma map_map : forall (A B C:Type)(f:A->B)(g:B->C) l,
map g (map f l) = map (fun x => g (f x)) l.
Proof.
induction l; simpl; auto.
rewrite IHl; auto.
Qed.
Lemma map_ext :
forall (A B : Type)(f g:A->B), (forall a, f a = g a) -> forall l, map f l = map g l.
Proof.
induction l; simpl; auto.
rewrite H; rewrite IHl; auto.
Qed.
End Chain_map.
Section Monadic.
Context {A: Type} {P: relation A}.
(**
Chain is a monad, chain_join is its monadic composition.
[ [x1, .. , xn], .. , [z1, .. zk] ] |-> [ x1, .. , zk ]
LAction = (R y z -> F y -> F z)
++ : (y ~> z) -> (x ~> y) -> (x ~> z)
F y := x ~> y
R y z := y ~> z
*)
Definition chain_join {x y: A} (cs: Chain (Chain P) x y)
: Chain P x y.
assert (act: LAction (Chain P) (Chain P x)).
exact (fun (y' z': A) (p: y' ~> z' :> P) (f: x ~> y' :> P)
=> p ++ f).
exact (@foldr A (Chain P) (Chain P x) x y act [] cs).
Defined.
End Monadic.
(*******************************)
(** ** Last element of a chain *)
(*******************************)
(** [last l d] returns the last element of the list [l],
or the default value [d] if [l] is empty. *)
Fixpoint last (l:list A) (d:A) : A :=
match l with
| [] => d
| [a] => a
| a :: l => last l d
end.
(** [removelast l] remove the last element of [l] *)
Fixpoint removelast (l:list A) : list A :=
match l with
| [] => []
| [a] => []
| a :: l => a :: removelast l
end.
Lemma app_removelast_last :
forall l d, l <> [] -> l = removelast l ++ [last l d].
Proof.
induction l.
destruct 1; auto.
intros d _.
destruct l; auto.
pattern (a0::l) at 1; rewrite IHl with d; auto; discriminate.
Qed.
Lemma exists_last :
forall l, l <> [] -> { l' : (list A) & { a : A | l = l' ++ [a]}}.
Proof.
induction l.
destruct 1; auto.
intros _.
destruct l.
exists [], a; auto.
destruct IHl as [l' (a',H)]; try discriminate.
rewrite H.
exists (a::l'), a'; auto.
Qed.
Lemma removelast_app :
forall l l', l' <> [] -> removelast (l++l') = l ++ removelast l'.
Proof.
induction l.
simpl; auto.
simpl; intros.
assert (l++l' <> []).
destruct l.
simpl; auto.
simpl; discriminate.
specialize (IHl l' H).
destruct (l++l'); [elim H0; auto|f_equal; auto].
Qed.
(* *)
(* *)
(* List ripoff following *)
(* *)
(* *)
Section Facts.
Context {A : Type}.
(** *** Genereric facts *)
(** Discrimination *)
Theorem nil_cons : forall (x:A) (l:list A), [] <> x :: l.
Proof.
intros; discriminate.
Qed.
(** Destruction *)
Theorem destruct_list : forall l : list A, {x:A & {tl:list A | l = x::tl}}+{l = []}.
Proof.
induction l as [|a tail].
right; reflexivity.
left; exists a, tail; reflexivity.
Qed.
Lemma hd_error_tl_repr : forall l (a:A) r,
hd_error l = Some a /\ tl l = r <-> l = a :: r.
Proof. destruct l as [|x xs].
- unfold hd_error, tl; intros a r. split; firstorder discriminate.
- intros. simpl. split.
* intros (H1, H2). inversion H1. rewrite H2. reflexivity.
* inversion 1. subst. auto.
Qed.
Lemma hd_error_some_nil : forall l (a:A), hd_error l = Some a -> l <> nil.
Proof. unfold hd_error. destruct l; now discriminate. Qed.
Theorem length_zero_iff_nil (l : list A):
length l = 0 <-> l=[].
Proof.
split; [now destruct l | now intros ->].
Qed.
(** *** Head and tail *)
Theorem hd_error_nil : hd_error (@nil A) = None.
Proof.
simpl; reflexivity.
Qed.
Theorem hd_error_cons : forall (l : list A) (x : A), hd_error (x::l) = Some x.
Proof.
intros; simpl; reflexivity.
Qed.
(************************)
(** *** Facts about [In] *)
(************************)
(** Characterization of [In] *)
Theorem in_eq : forall (a:A) (l:list A), In a (a :: l).
Proof.
simpl; auto.
Qed.
Theorem in_cons : forall (a b:A) (l:list A), In b l -> In b (a :: l).
Proof.
simpl; auto.
Qed.
Theorem not_in_cons (x a : A) (l : list A):
~ In x (a::l) <-> x<>a /\ ~ In x l.
Proof.
simpl. intuition.
Qed.
Theorem in_nil : forall a:A, ~ In a [].
Proof.
unfold not; intros a H; inversion_clear H.
Qed.
Theorem in_split : forall x (l:list A), In x l -> exists l1 l2, l = l1++x::l2.
Proof.
induction l; simpl; destruct 1.
subst a; auto.
exists [], l; auto.
destruct (IHl H) as (l1,(l2,H0)).
exists (a::l1), l2; simpl. apply f_equal. auto.
Qed.
(** Inversion *)
Lemma in_inv : forall (a b:A) (l:list A), In b (a :: l) -> a = b \/ In b l.
Proof.
intros a b l H; inversion_clear H; auto.
Qed.
(** Decidability of [In] *)
Theorem in_dec :
(forall x y:A, {x = y} + {x <> y}) ->
forall (a:A) (l:list A), {In a l} + {~ In a l}.
Proof.
intro H; induction l as [| a0 l IHl].
right; apply in_nil.
destruct (H a0 a); simpl; auto.
destruct IHl; simpl; auto.
right; unfold not; intros [Hc1| Hc2]; auto.
Defined.
(**************************)
(** *** Facts about [app] *)
(**************************)
(** Discrimination *)
Theorem app_cons_not_nil : forall (x y:list A) (a:A), [] <> x ++ a :: y.
Proof.
unfold not.
destruct x as [| a l]; simpl; intros.
discriminate H.
discriminate H.
Qed.
(** [app] commutes with [cons] *)
Theorem app_comm_cons : forall (x y:list A) (a:A), a :: (x ++ y) = (a :: x) ++ y.
Proof.
auto.
Qed.
(** Facts deduced from the result of a concatenation *)
Theorem app_eq_nil : forall l l':list A, l ++ l' = [] -> l = [] /\ l' = [].
Proof.
destruct l as [| x l]; destruct l' as [| y l']; simpl; auto.
intro; discriminate.
intros H; discriminate H.
Qed.
Theorem app_eq_unit :
forall (x y:list A) (a:A),
x ++ y = [a] -> x = [] /\ y = [a] \/ x = [a] /\ y = [].
Proof.
destruct x as [| a l]; [ destruct y as [| a l] | destruct y as [| a0 l0] ];
simpl.
intros a H; discriminate H.
left; split; auto.
right; split; auto.
generalize H.
generalize (app_nil_r l); intros E.
rewrite -> E; auto.
intros.
injection H.
intro.
assert ([] = l ++ a0 :: l0) by auto.
apply app_cons_not_nil in H1 as [].
Qed.
Lemma app_inj_tail :
forall (x y:list A) (a b:A), x ++ [a] = y ++ [b] -> x = y /\ a = b.
Proof.
induction x as [| x l IHl];
[ destruct y as [| a l] | destruct y as [| a l0] ];
simpl; auto.
- intros a b H.
injection H.
auto.
- intros a0 b H.
injection H as H1 H0.
apply app_cons_not_nil in H0 as [].
- intros a b H.
injection H as H1 H0.
assert ([] = l ++ [a]) by auto.
apply app_cons_not_nil in H as [].
- intros a0 b H.
injection H as <- H0.
destruct (IHl l0 a0 b H0) as (<-,<-).
split; auto.
Qed.
(** Compatibility with other operations *)
Lemma app_length : forall l l' : list A, length (l++l') = length l + length l'.
Proof.
induction l; simpl; auto.
Qed.
Lemma in_app_or : forall (l m:list A) (a:A), In a (l ++ m) -> In a l \/ In a m.
Proof.
intros l m a.
elim l; simpl; auto.
intros a0 y H H0.
now_show ((a0 = a \/ In a y) \/ In a m).
elim H0; auto.
intro H1.
now_show ((a0 = a \/ In a y) \/ In a m).
elim (H H1); auto.
Qed.
Lemma in_or_app : forall (l m:list A) (a:A), In a l \/ In a m -> In a (l ++ m).
Proof.
intros l m a.
elim l; simpl; intro H.
now_show (In a m).
elim H; auto; intro H0.
now_show (In a m).
elim H0. (* subProof completed *)
intros y H0 H1.
now_show (H = a \/ In a (y ++ m)).
elim H1; auto 4.
intro H2.
now_show (H = a \/ In a (y ++ m)).
elim H2; auto.
Qed.
Lemma in_app_iff : forall l l' (a:A), In a (l++l') <-> In a l \/ In a l'.
Proof.
split; auto using in_app_or, in_or_app.
Qed.
Lemma app_inv_head:
forall l l1 l2 : list A, l ++ l1 = l ++ l2 -> l1 = l2.
Proof.
induction l; simpl; auto; injection 1; auto.
Qed.
Lemma app_inv_tail:
forall l l1 l2 : list A, l1 ++ l = l2 ++ l -> l1 = l2.
Proof.
intros l l1 l2; revert l1 l2 l.
induction l1 as [ | x1 l1]; destruct l2 as [ | x2 l2];
simpl; auto; intros l H.
absurd (length (x2 :: l2 ++ l) <= length l).
simpl; rewrite app_length; auto with arith.
rewrite <- H; auto with arith.
absurd (length (x1 :: l1 ++ l) <= length l).
simpl; rewrite app_length; auto with arith.
rewrite H; auto with arith.
injection H; clear H; intros; f_equal; eauto.
Qed.
End Facts.
Hint Resolve app_assoc app_assoc_reverse: datatypes v62.
Hint Resolve app_comm_cons app_cons_not_nil: datatypes v62.
Hint Immediate app_eq_nil: datatypes v62.
Hint Resolve app_eq_unit app_inj_tail: datatypes v62.
Hint Resolve in_eq in_cons in_inv in_nil in_app_or in_or_app: datatypes v62.
(*******************************************)
(** * Operations on the elements of a list *)
(*******************************************)
Section Elts.
Variable A : Type.
(*****************************)
(** ** Nth element of a list *)
(*****************************)
Fixpoint nth (n:nat) (l:list A) (default:A) {struct l} : A :=
match n, l with
| O, x :: l' => x
| O, other => default
| S m, [] => default
| S m, x :: t => nth m t default
end.
Fixpoint nth_ok (n:nat) (l:list A) (default:A) {struct l} : bool :=
match n, l with
| O, x :: l' => true
| O, other => false
| S m, [] => false
| S m, x :: t => nth_ok m t default
end.
Lemma nth_in_or_default :
forall (n:nat) (l:list A) (d:A), {In (nth n l d) l} + {nth n l d = d}.
Proof.
intros n l d; revert n; induction l.
- right; destruct n; trivial.
- intros [|n]; simpl.
* left; auto.
* destruct (IHl n); auto.
Qed.
Lemma nth_S_cons :
forall (n:nat) (l:list A) (d a:A),
In (nth n l d) l -> In (nth (S n) (a :: l) d) (a :: l).
Proof.
simpl; auto.
Qed.
Fixpoint nth_error (l:list A) (n:nat) {struct n} : Exc A :=
match n, l with
| O, x :: _ => value x
| S n, _ :: l => nth_error l n
| _, _ => error
end.
Definition nth_default (default:A) (l:list A) (n:nat) : A :=
match nth_error l n with
| Some x => x
| None => default
end.
Lemma nth_default_eq :
forall n l (d:A), nth_default d l n = nth n l d.
Proof.
unfold nth_default; induction n; intros [ | ] ?; simpl; auto.
Qed.
(** Results about [nth] *)
Lemma nth_In :
forall (n:nat) (l:list A) (d:A), n < length l -> In (nth n l d) l.
Proof.
unfold lt; induction n as [| n hn]; simpl.
- destruct l; simpl; [ inversion 2 | auto ].
- destruct l as [| a l hl]; simpl.
* inversion 2.
* intros d ie; right; apply hn; auto with arith.
Qed.
Lemma In_nth l x d : In x l ->
exists n, n < length l /\ nth n l d = x.
Proof.
induction l as [|a l IH].
- easy.
- intros [H|H].
* subst; exists 0; simpl; auto with arith.
* destruct (IH H) as (n & Hn & Hn').
exists (S n); simpl; auto with arith.
Qed.
Lemma nth_overflow : forall l n d, length l <= n -> nth n l d = d.
Proof.
induction l; destruct n; simpl; intros; auto.
- inversion H.
- apply IHl; auto with arith.
Qed.
Lemma nth_indep :
forall l n d d', n < length l -> nth n l d = nth n l d'.
Proof.
induction l.
- inversion 1.
- intros [|n] d d'; simpl; auto with arith.
Qed.
Lemma app_nth1 :
forall l l' d n, n < length l -> nth n (l++l') d = nth n l d.
Proof.
induction l.
- inversion 1.
- intros l' d [|n]; simpl; auto with arith.
Qed.
Lemma app_nth2 :
forall l l' d n, n >= length l -> nth n (l++l') d = nth (n-length l) l' d.
Proof.
induction l; intros l' d [|n]; auto.
- inversion 1.
- intros; simpl; rewrite IHl; auto with arith.
Qed.
Lemma nth_split n l d : n < length l ->
exists l1, exists l2, l = l1 ++ nth n l d :: l2 /\ length l1 = n.
Proof.
revert l.
induction n as [|n IH]; intros [|a l] H; try easy.
- exists nil; exists l; now simpl.
- destruct (IH l) as (l1 & l2 & Hl & Hl1); auto with arith.
exists (a::l1); exists l2; simpl; split; now f_equal.
Qed.
(** Results about [nth_error] *)
Lemma nth_error_In l n x : nth_error l n = Some x -> In x l.
Proof.
revert n. induction l as [|a l IH]; intros [|n]; simpl; try easy.
- injection 1; auto.
- eauto.
Qed.
Lemma In_nth_error l x : In x l -> exists n, nth_error l n = Some x.
Proof.
induction l as [|a l IH].
- easy.
- intros [H|H].
* subst; exists 0; simpl; auto with arith.
* destruct (IH H) as (n,Hn).
exists (S n); simpl; auto with arith.
Qed.
Lemma nth_error_None l n : nth_error l n = None <-> length l <= n.
Proof.
revert n. induction l; destruct n; simpl.
- split; auto.
- split; auto with arith.
- split; now auto with arith.
- rewrite IHl; split; auto with arith.
Qed.
Lemma nth_error_Some l n : nth_error l n <> None <-> n < length l.
Proof.
revert n. induction l; destruct n; simpl.
- split; [now destruct 1 | inversion 1].
- split; [now destruct 1 | inversion 1].
- split; now auto with arith.
- rewrite IHl; split; auto with arith.
Qed.
Lemma nth_error_split l n a : nth_error l n = Some a ->
exists l1, exists l2, l = l1 ++ a :: l2 /\ length l1 = n.
Proof.
revert l.
induction n as [|n IH]; intros [|x l] H; simpl in *; try easy.
- exists nil; exists l. injection H; clear H; intros; now subst.
- destruct (IH _ H) as (l1 & l2 & H1 & H2).
exists (x::l1); exists l2; simpl; split; now f_equal.
Qed.
Lemma nth_error_app1 l l' n : n < length l ->
nth_error (l++l') n = nth_error l n.
Proof.
revert l.
induction n; intros [|a l] H; auto; try solve [inversion H].
simpl in *. apply IHn. auto with arith.