| 1 |
Section Lists. |
| 2 |
Context [A : Type]. |
| 3 |
|
| 4 |
Fixpoint In (a : A) (l : list A) : Prop := |
| 5 |
match l with |
| 6 |
| nil => False |
| 7 |
| cons b m => b = a \/ In a m |
| 8 |
end. |
| 9 |
|
| 10 |
Fixpoint concat (l : list (list A)) : list A := |
| 11 |
match l with |
| 12 |
| nil => nil |
| 13 |
| cons x l => x ++ concat l |
| 14 |
end. |
| 15 |
|
| 16 |
Definition hd (default : A) (l : list A) := |
| 17 |
match l with |
| 18 |
| nil => default |
| 19 |
| cons x _ => x |
| 20 |
end. |
| 21 |
|
| 22 |
Axiom app_nil_r : forall (l : list A), app l nil = l. |
| 23 |
|
| 24 |
Axiom app_inj_tail : |
| 25 |
forall (x y : list A) (a b : A), |
| 26 |
app x (cons a nil) = app y (cons b nil) -> x = y /\ a = b. |
| 27 |
End Lists. |
| 28 |
|
| 29 |
Section Fold_Left_Recursor. |
| 30 |
Context [A B : Type]. |
| 31 |
Variable f : A -> B -> A. |
| 32 |
Fixpoint fold_left (l:list B) (a0:A) : A := |
| 33 |
match l with |
| 34 |
| nil => a0 |
| 35 |
| cons b t => fold_left t (f a0 b) |
| 36 |
end. |
| 37 |
Lemma fold_left_app : forall (l l':list B)(i:A), |
| 38 |
fold_left (l++l') i = fold_left l' (fold_left l i). |
| 39 |
Proof. |
| 40 |
now intro l; induction l; cbn. |
| 41 |
Qed. |
| 42 |
End Fold_Left_Recursor. |
| 43 |
|
| 44 |
Section Fold_Right_Recursor. |
| 45 |
Context [A B : Type]. |
| 46 |
Variable f : B -> A -> A. |
| 47 |
Variable a0 : A. |
| 48 |
Fixpoint fold_right (l : list B) : A := |
| 49 |
match l with |
| 50 |
| nil => a0 |
| 51 |
| cons b t => f b (fold_right t) |
| 52 |
end. |
| 53 |
End Fold_Right_Recursor. |
| 54 |
|
| 55 |
Section Map. |
| 56 |
Context [A B : Type]. |
| 57 |
Variable f : A -> B. |
| 58 |
|
| 59 |
Fixpoint map (l : list A) : list B := |
| 60 |
match l with |
| 61 |
| nil => nil |
| 62 |
| cons a t => (f a) :: (map t) |
| 63 |
end. |
| 64 |
Lemma in_map : |
| 65 |
forall (l:list A) (x:A), In x l -> In (f x) (map l). |
| 66 |
Proof. |
| 67 |
intro l; induction l; firstorder (subst; auto). |
| 68 |
Qed. |
| 69 |
End Map. |
| 70 |
|
| 71 |
Section FlatMap. |
| 72 |
Context [A B : Type]. |
| 73 |
Variable f : A -> list B. |
| 74 |
|
| 75 |
Fixpoint flat_map (l : list A) : list B := |
| 76 |
match l with |
| 77 |
| nil => nil |
| 78 |
| cons x t => app (f x) (flat_map t) |
| 79 |
end. |
| 80 |
End FlatMap. |
| 81 |
|
| 82 |
Section Elts. |
| 83 |
Context [A : Type]. |
| 84 |
|
| 85 |
Fixpoint nth (n : nat) (l : list A) (default : A) {struct l} : A := |
| 86 |
match n, l with |
| 87 |
| O, cons x l' => x |
| 88 |
| O, nil => default |
| 89 |
| S m, nil => default |
| 90 |
| S m, cons x t => nth m t default |
| 91 |
end. |
| 92 |
End Elts. |
| 93 |
|
| 94 |
Section Cutting. |
| 95 |
Context [A : Type]. |
| 96 |
|
| 97 |
Fixpoint skipn (n : nat)(l : list A) : list A := |
| 98 |
match n with |
| 99 |
| 0 => l |
| 100 |
| S n => |
| 101 |
match l with |
| 102 |
| nil => nil |
| 103 |
| cons a l => skipn n l |
| 104 |
end |
| 105 |
end. |
| 106 |
End Cutting. |
| 107 |
|
| 108 |
Module ListNotations. |
| 109 |
Notation "[ ]" := nil (format "[ ]") : list_scope. |
| 110 |
Notation "[ x ]" := (cons x nil) : list_scope. |
| 111 |
Notation "[ x ; y ; .. ; z ]" := (cons x (cons y .. (cons z nil) ..)) |
| 112 |
(format "[ '[' x ; '/' y ; '/' .. ; '/' z ']' ]") : list_scope. |
| 113 |
Open Scope list_scope. |
| 114 |
End ListNotations. |