-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyList.v
81 lines (73 loc) · 1.89 KB
/
MyList.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
Require Import MyTactics.
Require Export List.
(** * [repeat]: definition and facts. *)
Fixpoint repeat {A} (a: A) (n: nat) : list A :=
match n with
| O => nil
| S n => a :: repeat a n
end.
Lemma map_repeat {A B} (f: A -> B) (a: A) (n: nat) :
map f (repeat a n) = repeat (f a) n.
Proof.
induction n.
* reflexivity.
* simpl. congruence.
Qed.
Lemma repeat_length {A} :
forall (a: A) n, length (repeat a n) = n.
Proof.
intros a n. induction n; simpl in *; auto.
Qed.
Lemma repeat_nth {A} :
forall (a a0 : A) k n,
k < n ->
nth k (repeat a n) a0 = a.
Proof.
intros a a0 k n H.
generalize dependent k. revert a. induction n; intros a k H; auto.
simpl. destruct k; auto.
Qed.
(** * Facts about [last]. *)
Lemma last_nth {A} :
forall (l: list A) (default: A),
last l default = nth (length l - 1) l default.
Proof.
intros l default. induction l.
* reflexivity.
* destruct l as [|b l].
+ reflexivity.
+ transitivity (last (b :: l) default). reflexivity.
rewrite IHl. simpl.
replace (length l - 0) with (length l) by auto with arith.
reflexivity.
Qed.
Lemma last_In {A} :
forall (l: list A) (default: A),
l <> nil -> In (last l default) l.
Proof.
intros l default H. destruct l as [|a l].
* exfalso. congruence.
* rewrite last_nth. apply nth_In. auto with arith.
Qed.
Lemma last_map {A B} (f: A -> B):
forall (l: list A) (default: A),
last (map f l) (f default) = f (last l default).
Proof.
intros l default. induction l.
* reflexivity.
* simpl map. destruct l as [|b l].
+ reflexivity.
+ transitivity (last (map f (b :: l)) (f default)). reflexivity.
rewrite IHl. reflexivity.
Qed.
(** * Facts about [app]. *)
Lemma app_comm_cons_cons {A}:
forall a1 a2 (l1 l2: list A),
(l1 ++ a1 :: nil) ++ a2 :: l2 = l1 ++ a1 :: a2 :: l2.
Proof.
intros a1 a2 l1.
generalize dependent a2. generalize dependent a1.
induction l1; intros a1 a2 l2.
* reflexivity.
* simpl. f_equal. auto.
Qed.