-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorLib.v
242 lines (199 loc) · 7.1 KB
/
VectorLib.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
(** ** Vectors *)
(** This file defines operations on vectors and proves corresponding facts. *)
Require Import Vector.
Definition vec := t.
Require Import Util.
Require Import Equations.Equations Equations.Prop.DepElim.
Require Import Lia.
Definition Forall {X} (p : X -> Prop) := fix Forall {n} (v : vec X n) :=
match v with
| nil _ => True
| cons _ x _ v => p x /\ Forall v
end.
Definition ForallT {X} (p : X -> Type) := fix ForallT {n} (v : vec X n) :=
match v with
| nil _ => (unit : Type)
| cons _ x _ v => (p x * ForallT v)%type
end.
Definition Forall2 {X Y} (p : X -> Y -> Prop) := fix Forall2 {n} (v1 : vec X n) (v2 : vec Y n) :=
match v1 in Vector.t _ n return vec Y n -> Prop with
| nil _ => fun _ => True
| cons _ x _ v1 => fun v2 => p x (hd v2) /\ Forall2 v1 (tl v2)
end v2.
Fixpoint Forall2T {X Y n} (p : X -> Y -> Type) (v1 : vec X n) (v2 : vec Y n) :=
match v1 in Vector.t _ n return vec Y n -> Type with
| nil _ => fun _ => True
| cons _ x _ v1 => fun v2 => (p x (hd v2) * Forall2T p v1 (tl v2))%type
end v2.
Definition Any {X} (p : X -> Prop) := fix Forall {n} (v : vec X n) :=
match v with
| nil _ => False
| cons _ x _ v => p x \/ Forall v
end.
Fixpoint In {X n} x (v : vec X n) :=
match v with
| nil _ => False
| cons _ y _ v => x = y \/ In x v
end.
Lemma Forall2_Forall {X Y Z n} (p : Y -> Z -> Prop) (f1 : X -> Y) (f2 : X -> Z) v :
@Forall2 Y Z p n (map f1 v) (map f2 v) <-> @Forall X (fun x => p (f1 x) (f2 x)) n v.
Proof.
induction v; firstorder.
Qed.
Lemma Forall2_identical {X n} (v : vec X n) (p : X -> X -> Prop) :
Forall2 p v v <-> Forall (fun x => p x x) v.
Proof.
induction v; firstorder.
Qed.
Lemma Forall2_move_forall {X Y Z n} (v1 : vec X n) (v2 : vec Y n) (p : X -> Y -> Z -> Prop) :
Forall2 (fun x y => forall z, p x y z) v1 v2 <-> forall z, Forall2 (fun x y => p x y z) v1 v2.
Proof.
induction v1; dependent elimination v2; firstorder. apply IHv1, H.
Qed.
Lemma Forall2_eq {X n} (v1 : vec X n) (v2 : vec X n) :
Forall2 eq v1 v2 -> v1 = v2.
Proof.
induction v1; dependent elimination v2; intros H. reflexivity. f_equal; firstorder.
Qed.
Lemma Forall_ext {X n} (p q : X -> Prop) (v : vec X n) :
(forall x, p x -> q x) -> Forall p v -> Forall q v.
Proof.
induction v; firstorder.
Qed.
Lemma Forall_ext_Forall {X n} (p q : X -> Prop) (v : vec X n) :
Forall (fun x => p x -> q x) v -> Forall p v -> Forall q v.
Proof.
induction v; firstorder.
Qed.
Lemma Forall_ext_in {X n} (p q : X -> Prop) (v : vec X n) :
(forall x, In x v -> p x -> q x) -> Forall p v -> Forall q v.
Proof.
induction v; firstorder.
Qed.
Lemma Forall_in {X n} (p : X -> Prop) (v : vec X n) :
Forall p v <-> forall x, In x v -> p x.
Proof.
induction v. easy. split. intros H1 x [->|H2]; firstorder. firstorder.
Qed.
Lemma Forall_map {X Y n} (p : Y -> Prop) (f : X -> Y) (v : vec X n) :
Forall p (map f v) <-> Forall (fun x => p (f x)) v.
Proof.
induction v; firstorder.
Qed.
Require Import Decidable.
Lemma Forall_dec {X n} (p : X -> Prop) (v : vec X n) :
ForallT (fun x => dec (p x)) v -> dec (Forall p v).
Proof.
induction v; firstorder.
Qed.
Lemma ForallT_translate {X Y n} (p : X -> Y -> Prop) (v : vec X n) :
ForallT (fun x => { x' | p x x' }) v -> { v' : vec Y n | Forall2 p v v'}.
Proof.
intros H. induction v.
- now exists (nil _).
- destruct H as [[x' H1] H2]. destruct IHv as [v' IHv]. easy.
now exists (cons _ x' _ v').
Qed.
Lemma ForallT_ext {X n} (p q : X -> Type) (v : vec X n) :
(forall x, p x -> q x) -> ForallT p v -> ForallT q v.
Proof.
induction v; firstorder.
Qed.
Lemma ForallT_general {X n} (p : X -> Type) (v : vec X n) :
(forall x, p x) -> ForallT p v.
Proof.
induction v; firstorder.
Qed.
Lemma map_ext {X Y Z : Type} {n} (v : vec X n) (v' : vec Y n) (f : X -> Z) (g : Y -> Z) :
Forall2 (fun x x' => f x = g x') v v' -> map f v = map g v'.
Proof.
induction v; dependent elimination v'; cbn; intros H.
- reflexivity.
- f_equal. apply H. now apply IHv.
Qed.
Lemma map_ext_in {X Y n} (f g : X -> Y) (v : vec X n):
(forall x, In x v -> f x = g x) -> map f v = map g v.
Proof.
induction v; cbn. reflexivity. intros. f_equal; firstorder.
Qed.
Lemma map_ext_forall {X Y n} (f g : X -> Y) (v : vec X n):
Forall (fun x => f x = g x) v -> map f v = map g v.
Proof.
induction v; cbn. reflexivity. intros. f_equal; firstorder.
Qed.
Lemma In_map {X Y n} (f : X -> Y) (v : vec X n) x :
In x v -> In (f x) (map f v).
Proof.
intros H. induction v; cbn. easy. destruct H as [->|]. now left. right. now apply IHv.
Qed.
Lemma In_map_iff {X Y n} (f : X -> Y) (v : vec X n) y :
In y (map f v) <-> exists x, f x = y /\ In x v.
Proof.
induction v; cbn.
- split. easy. now intros [].
- split.
+ intros [->|]. exists h. split. easy. now left. apply IHv in H as [x H]. exists x.
split. apply H. now right.
+ intros [x [<- [->|]]]. now left. right. apply IHv. now exists x.
Qed.
Lemma In_compat {X n} x (v : vec X n) :
In x v <-> Vector.In x v.
Proof.
induction v; cbn.
- split. easy. intros H. inversion H.
- split. intros [->|]; constructor. now apply IHv. intros H.
inversion H. now left. right. apply inj_pairT2 in H3 as ->. now apply IHv.
decide equality.
Qed.
Fixpoint tabulate {X} n (f : nat -> X) : vec X n :=
match n with
| 0 => Vector.nil X
| S n => Vector.cons _ (f n) _ (tabulate n f)
end.
Fixpoint fold_left {X Y} (f: X -> Y -> Y) y {n} (v : vec X n) : Y :=
match v with
| Vector.nil _ => y
| Vector.cons _ x _ v => fold_left f (f x y) v
end.
Fixpoint fold_two_left {X Y} (f: X -> Y -> Y) y {n} (v1 v2 : vec X n) : Y :=
match v1 in Vector.t _ n return vec X n -> Y with
| Vector.nil _ => fun _ => y
| Vector.cons _ x _ v1 => fun v2 => fold_two_left f (f (hd v2) (f x y)) v1 (tl v2)
end v2.
Fixpoint nth_error {X m} n (v : vec X m) err := match v with
| Vector.nil _ => err
| Vector.cons _ x _ v => match n with 0 => x | S n => nth_error n v err end
end.
Lemma map_tabulate {X Y} n (f : X -> Y) (g : nat -> X) :
map f (tabulate n g) = tabulate n (fun x => f (g x)).
Proof.
induction n; cbn; congruence.
Qed.
Lemma tabulate_ext {X} n (f : nat -> X) (g : nat -> X) :
(forall m, m < n -> f m = g m) -> tabulate n f = tabulate n g.
Proof.
intros H. induction n; cbn. reflexivity. f_equal. apply H. lia.
apply IHn. intros m H'. apply H. lia.
Qed.
Lemma tabulate_nth {X n} (v : vec X n) err :
tabulate n (fun x => nth_error (n-S x) v err) = v.
Proof.
induction v; cbn. reflexivity. f_equal. now replace (n-n) with 0 by lia.
rewrite <- IHv at 1. apply tabulate_ext. intros x H. now replace (n - x) with (S (n - S x)) by lia.
Qed.
Lemma tabulate_Forall {X n} (f : nat -> X) (p : X -> Prop) :
Forall p (tabulate n f) <-> forall x, x < n -> p (f x).
Proof.
induction n; cbn.
- split; intros; lia.
- split.
+ intros [] x ?. assert (x = n \/ x < n) as [->|] by lia. easy. now apply IHn.
+ intros H. split. apply H. lia. apply IHn. intros ? ?. apply H. lia.
Qed.
Lemma nth_tabulate {X} n m (f : nat -> X) err :
m < n -> nth_error m (tabulate n f) err = f (n-S m).
Proof.
revert m. induction n; intros m; cbn -[minus].
- lia.
- intros H. destruct m; cbn. f_equal; lia. apply IHn. lia.
Qed.