-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloatlib.v
More file actions
436 lines (388 loc) · 12.3 KB
/
floatlib.v
File metadata and controls
436 lines (388 loc) · 12.3 KB
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
Require Import VST.floyd.proofauto.
Require Import vcfloat.VCFloat.
Require Import Coq.Relations.Relations Coq.Classes.Morphisms Coq.Classes.RelationPairs Coq.Classes.RelationClasses.
Require Export vcfloat.FPStdLib.
Set Bullet Behavior "Strict Subproofs".
Definition matrix t := list (list (ftype t)).
Definition vector t := list (ftype t).
Definition dotprod {NAN: Nans} {t: type} (v1 v2: list (ftype t)) : ftype t :=
fold_left (fun s x12 => BFMA (fst x12) (snd x12) s)
(List.combine v1 v2) (Zconst t 0).
Definition norm2 {NAN: Nans} {t} (v: vector t) := dotprod v v.
Definition matrix_vector_mult {NAN: Nans}{t: type} (m: matrix t) (v: vector t) : vector t :=
map (fun row => dotprod row v) m.
Definition matrix_matrix_mult {NAN: Nans}{t: type} (m1 m2: matrix t) : matrix t :=
map (matrix_vector_mult m1) m2.
Definition matrix_cols {t} (m: matrix t) cols :=
Forall (fun r => Zlength r = cols) m.
Definition matrix_rows {t} (m: matrix t) : Z := Zlength m.
Definition map2 {A B C: Type} (f: A -> B -> C) al bl :=
map (uncurry f) (List.combine al bl).
Definition opp_matrix {NAN: Nans}{t:type} (m: matrix t) : matrix t :=
map (map (@BOPP NAN t)) m.
Definition matrix_add {NAN: Nans}{t} : matrix t -> matrix t -> matrix t :=
map2 (map2 (@BPLUS _ t)).
Definition vector_add {NAN: Nans}{t:type} (v1 v2 : vector t) :=
map2 (@BPLUS _ t) v1 v2.
Definition vector_sub {NAN: Nans}{t:type} (v1 v2 : vector t) :=
map2 (@BMINUS _ t) v1 v2.
Definition matrix_index {t} (m: matrix t) (i j: nat) :=
nth j (nth i m nil) (Zconst t 0).
Definition matrix_by_index {t} (rows cols: nat)
(f: nat -> nat -> ftype t) : matrix t :=
map (fun i => map (f i) (seq 0 cols)) (seq 0 rows).
Definition matrix_rows_nat {t} (m: matrix t) := length m.
Definition matrix_cols_nat {t} (m: matrix t) cols :=
Forall (fun r => length r = cols) m.
Lemma matrix_by_index_rows:
forall {t} rows cols (f: nat -> nat -> ftype t),
matrix_rows_nat (matrix_by_index rows cols f) = rows.
Proof.
intros.
unfold matrix_by_index, matrix_rows_nat.
rewrite length_map. rewrite length_seq. auto.
Qed.
Local Open Scope nat.
Lemma matrix_by_index_cols:
forall {t} rows cols (f: nat -> nat -> ftype t),
matrix_cols_nat (matrix_by_index rows cols f) cols.
Proof.
intros.
unfold matrix_by_index, matrix_cols_nat.
pose (k := 0). change (seq 0 rows) with (seq k rows).
clearbody k. revert k; induction rows; intros; constructor; auto.
rewrite length_map, length_seq. auto.
Qed.
Lemma nth_map_seq:
forall {A} (f: nat -> A) d (i n: nat), i < n -> nth i (map f (seq 0 n)) d = f i.
Proof.
intros.
assert (i < n) by lia.
clear H.
transitivity (f (i+0)); [ | f_equal; lia].
set (k := 0 ) in *.
clearbody k.
revert k i H0; induction n; simpl; intros.
destruct i; auto; lia.
destruct i.
destruct k; auto; lia.
rewrite (IHn (S k) i).
f_equal; lia.
lia.
Qed.
Lemma matrix_by_index_index:
forall {t} rows cols (f: nat -> nat -> ftype t) i j,
i < rows -> j < cols ->
matrix_index (matrix_by_index rows cols f) i j = f i j.
Proof.
intros.
unfold matrix_index, matrix_by_index.
rewrite nth_map_seq; auto.
rewrite nth_map_seq; auto.
Qed.
Lemma matrix_extensionality_strong:
forall {t} (m1 m2: matrix t) cols,
matrix_rows_nat m1 = matrix_rows_nat m2 ->
matrix_cols_nat m1 cols -> matrix_cols_nat m2 cols ->
(forall i j, i < matrix_rows_nat m1 -> j < cols ->
matrix_index m1 i j = matrix_index m2 i j) ->
m1 = m2.
Proof.
induction m1; destruct m2; simpl; intros; inv H; auto.
inv H0. inv H1.
f_equal.
clear - H3 H2.
specialize (H2 O).
unfold matrix_index in H2. simpl in H2.
revert l H3 H2; induction a; destruct l; simpl; intros; inv H3; f_equal; auto.
apply (H2 O); try lia.
apply IHa; auto. intros j ? ?. apply (H2 (S j)); try lia.
eapply IHm1; eauto.
intros i j ? ?. apply (H2 (S i) j); lia.
Qed.
Lemma matrix_extensionality:
forall {t} (m1 m2: matrix t) cols,
matrix_rows_nat m1 = matrix_rows_nat m2 ->
matrix_cols_nat m1 cols -> matrix_cols_nat m2 cols ->
(forall i j, i < matrix_rows_nat m1 -> j < cols ->
feq (matrix_index m1 i j) (matrix_index m2 i j)) ->
Forall2 (Forall2 feq) m1 m2.
Proof.
induction m1; destruct m2; simpl; intros; inv H; constructor; auto.
specialize (H2 O).
unfold matrix_index in H2. simpl in H2.
inv H0. inv H1.
clear - l H3 H2.
revert l H3 H2; induction a; destruct l; simpl; intros; inv H3; constructor.
apply (H2 O); try lia.
apply IHa; auto. intros j ? ?. apply (H2 (S j)); try lia.
inv H0. inv H1.
fold (matrix_cols_nat m1 (length a)) in H6.
fold (matrix_cols_nat m2 (length a)) in H5.
eapply IHm1; eauto.
intros i j ? ?. apply (H2 (S i) j); lia.
Qed.
Lemma matrix_index_prop:
forall {t} (P: ftype t -> Prop) (m: matrix t) (cols i j : nat),
matrix_cols_nat m cols ->
Forall (Forall P) m ->
i < matrix_rows_nat m -> j < cols ->
P (matrix_index m i j).
Proof.
induction m; intros.
inv H1.
inv H0.
simpl in H1.
inv H.
unfold matrix_index.
destruct i; simpl.
clear - H2 H5.
revert j H2; induction H5; intros.
inv H2.
destruct j; simpl; auto.
apply IHForall. simpl in H2; lia.
eapply IHm; eauto.
lia.
Qed.
Lemma all_nth_eq:
forall {A} d (al bl: list A),
length al = length bl ->
(forall i, i < length al -> nth i al d = nth i bl d) ->
al=bl.
Proof.
induction al; destruct bl; simpl; intros; try discriminate; f_equal.
apply (H0 0 ltac:(lia)).
apply IHal.
lia.
intros.
apply (H0 (S i)). lia.
Qed.
#[export] Instance zerof {t} : Inhabitant (ftype t) := (Zconst t 0).
Lemma norm2_snoc:
forall {NAN: Nans}{t} (al: vector t) (x: ftype t),
norm2 (al ++ [x]) = BFMA x x (norm2 al).
Proof.
intros. unfold norm2, dotprod.
rewrite combine_app by auto.
rewrite fold_left_app. reflexivity.
Qed.
Lemma dotprod_congr {NAN: Nans}{t} (x x' y y' : vector t):
Forall2 strict_feq x x' ->
Forall2 strict_feq y y' ->
length x = length y ->
feq (dotprod x y) (dotprod x' y').
Proof.
intros.
unfold dotprod.
pose proof (Forall2_length H).
pose proof (Forall2_length H0).
rewrite <- !fold_left_rev_right.
apply Forall2_rev in H, H0.
rewrite !rev_combine by lia.
clear - H H0.
set (a := rev x) in *; clearbody a. set (a' := rev x') in *; clearbody a'.
set (b := rev y) in *; clearbody b. set (b' := rev y') in *; clearbody b'.
revert b b' H0; induction H; simpl; intros; auto.
inv H1. auto. simpl. apply BFMA_mor; auto; apply subrelation_strict_feq; auto.
Qed.
Lemma norm2_congr:
forall {NAN: Nans} {t} (x x': vector t),
Forall2 feq x x' ->
feq (norm2 x) (norm2 x').
Proof.
intros.
unfold norm2.
unfold dotprod.
rewrite <- !fold_left_rev_right.
apply Forall2_rev in H.
rewrite !rev_combine by lia.
set (a := rev x) in *. set (b := rev x') in *. clearbody a. clearbody b.
induction H; simpl; intros; auto.
set (u := fold_right _ _ _) in *. clearbody u.
set (u' := fold_right _ _ _) in *. clearbody u'.
clear - H IHForall2.
destruct x0,y, u,u'; inv H; inv IHForall2;
try constructor;
auto;
try (destruct s,s0,s1,s2; constructor).
destruct H1; subst m0 e1. proof_irr.
unfold BFMA, Binary.Bfma, Binary.BSN2B, BinarySingleNaN.Bfma, Binary.B2BSN;
destruct s0,s1,s2; simpl; try constructor;
destruct (BinarySingleNaN.SF2B _ _); try constructor; auto.
destruct H0,H1; subst m0 m2 e1 e3.
repeat proof_irr.
unfold BFMA, Binary.Bfma, Binary.BSN2B,
BinarySingleNaN.Bfma.
destruct (Binary.B2BSN (fprec t) (femax t)
(Binary.B754_finite (fprec t) (femax t) s0 m e e0));
try constructor; auto.
Qed.
Local Open Scope Z.
Lemma Znth_vector_sub:
forall {NAN: Nans}{t} i (x y: vector t) , Zlength x = Zlength y ->
0 <= i < Zlength x ->
Znth i (vector_sub x y) = BMINUS (Znth i x) (Znth i y).
Proof.
intros.
unfold vector_sub, map2.
rewrite Znth_map by (rewrite Zlength_combine; lia).
rewrite Znth_combine by lia.
reflexivity.
Qed.
Lemma vector_sub_congr: forall {NAN: Nans} {t} (x x' y y': vector t),
Forall2 feq x x' -> Forall2 feq y y' ->
Forall2 feq (vector_sub x y) (vector_sub x' y').
Proof.
induction x; destruct x'; intros; inv H.
constructor.
specialize (IHx x').
inv H0. constructor.
specialize (IHx _ _ H6 H1).
constructor; auto.
apply BMINUS_congr; auto.
Qed.
Lemma norm2_loose_congr:
forall {NAN: Nans}{t} (x x': vector t), Forall2 feq x x' -> feq (norm2 x) (norm2 x').
Proof.
intros.
unfold norm2.
unfold dotprod.
rewrite <- !fold_left_rev_right.
pose proof (Forall2_length H).
rewrite !rev_combine by auto.
clear H0.
apply Forall2_rev in H.
set (a := rev x) in *. clearbody a.
set (b := rev x') in *. clearbody b.
induction H.
constructor.
simpl.
apply BFMA_xx_mor; auto.
Qed.
Lemma nth_map_inrange {A} (d': A) {B: Type}:
forall (f: A -> B) i al d,
(i < length al)%nat ->
nth i (map f al) d = f (nth i al d').
Proof.
intros.
revert i H; induction al; destruct i; simpl; intros; inv H; auto.
apply IHal; auto. lia.
apply IHal; auto. lia.
Qed.
Lemma finite_dotprod_e: forall {NAN: Nans}{t} (x y: vector t),
Zlength x = Zlength y ->
finite (dotprod x y) -> Forall finite x /\ Forall finite y.
Proof.
intros.
rewrite !Zlength_correct in H. apply Nat2Z.inj in H.
unfold dotprod in H0.
rewrite <- fold_left_rev_right in H0.
rewrite rev_combine in H0 by auto.
rewrite <- (length_rev x), <- (length_rev y) in H.
assert (Forall finite (rev x) /\ Forall finite (rev y)).
2:rewrite <- (rev_involutive x), <- (rev_involutive y);
destruct H1; split; apply Forall_rev; auto.
forget (rev x) as a; forget (rev y) as b.
revert b H H0; induction a; destruct b; intros; inv H.
split; constructor.
specialize (IHa _ H2).
simpl in H0.
set (u := fold_right _ _ _) in *. clearbody u.
assert (finite a /\ finite f /\ finite u); [ | split; constructor; tauto].
clear - H0.
apply finite_is_finite in H0.
destruct a,f,u; inv H0; try solve [split3; constructor; auto].
destruct s,s0,s1; inv H1.
destruct s,s0,s1; inv H1.
destruct s,s0,s1; inv H1.
Qed.
Lemma finite_norm2_e: forall {NAN: Nans}{t} (x: vector t),
finite (norm2 x) -> Forall finite x.
Proof.
intros.
apply finite_dotprod_e in H; auto.
destruct H; auto.
Qed.
Lemma matrix_by_index_prop:
forall {t} (f: nat -> nat -> ftype t) (P: ftype t -> Prop) rows cols,
P (Zconst t 0) ->
(forall i j, (i < rows)%nat -> (j < cols)%nat -> P (f i j)) ->
Forall (Forall P) (matrix_by_index rows cols f).
Proof.
intros.
unfold matrix_by_index.
apply Forall_nth; intros.
rewrite length_map, length_seq in H1.
rewrite nth_map_seq by auto.
apply Forall_nth; intros.
rewrite length_map, length_seq in H2.
rewrite nth_map_seq by auto.
apply H0; auto.
Qed.
Lemma Zmatrix_cols_nat:
forall {t} (m: matrix t) cols,
matrix_cols_nat m cols <-> matrix_cols m (Z.of_nat cols).
Proof.
induction m; simpl; intros; split; intro; inv H; constructor; auto.
apply Zlength_correct.
clear - H3.
induction H3; constructor; auto. rewrite <- H; apply Zlength_correct.
rewrite Zlength_correct in H2. lia.
clear - H3.
induction H3; constructor; auto. rewrite Zlength_correct in H; lia.
Qed.
Lemma Zlength_seq: forall lo n, Zlength (seq lo n) = Z.of_nat n.
Proof.
intros. rewrite Zlength_correct. f_equal. apply length_seq.
Qed.
#[export] Hint Rewrite Zlength_seq : sublist rep_lia.
Lemma Zmatrix_rows_nat: forall {t} (m: matrix t), Z.of_nat (matrix_rows_nat m) = matrix_rows m.
Proof.
unfold matrix_rows.
induction m; simpl; auto. list_solve.
Qed.
Add Parametric Morphism {NAN: Nans}{t: type}: (@norm2 _ t)
with signature Forall2 feq ==> feq
as norm2_mor.
Proof.
exact norm2_congr.
Qed.
Add Parametric Morphism {NAN: Nans}{t: type}: (@vector_sub _ t)
with signature Forall2 feq ==> Forall2 feq ==> Forall2 feq
as vector_sub_mor.
Proof.
intros; eapply vector_sub_congr; eauto.
Qed.
Add Parametric Morphism {T: Type} (rel: relation T): (@Zlength T)
with signature Forall2 rel ==> eq
as Zlength_mor.
Proof.
induction 1; auto.
rewrite !Zlength_cons; f_equal; auto.
Qed.
Add Parametric Morphism {NAN: Nans}{t}: (@dotprod _ t)
with signature Forall2 feq ==> Forall2 feq ==> feq
as dotprod_mor.
Proof.
intros.
unfold dotprod.
set (a := Zconst t 0) at 1.
set (a' := Zconst t 0).
assert (feq a a') by reflexivity.
clearbody a. clearbody a'.
revert x0 y0 H0 a a' H1; induction H; intros.
destruct x0, y0; inv H0; simpl; auto.
destruct x0,y0; inv H1; simpl; auto.
eapply IHForall2; eauto.
apply BFMA_mor; auto.
Qed.
Add Parametric Morphism {NAN: Nans} {t}: (@matrix_vector_mult _ t)
with signature Forall2 (Forall2 feq) ==> Forall2 feq ==> Forall2 feq
as matrix_vector_mult_mor.
Proof.
intros.
unfold matrix_vector_mult.
apply Forall2_map.
induction H; simpl; intros; constructor; auto.
apply dotprod_mor; auto.
Qed.