-
Notifications
You must be signed in to change notification settings - Fork 1
/
lecture2.v
267 lines (186 loc) · 12.8 KB
/
lecture2.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
(******************************************************************************)
(* *)
(* LECTURE : Floating-point numbers and formal proof *)
(* [email protected] 12/05/2016 *)
(* *)
(******************************************************************************)
(*
How to define floating point numbers ?
We are going to use the flocq library at http://flocq.gforge.inria.fr/
*)
Require Import Psatz Reals.
From Flocq Require Import Core.
Section Lecture2.
Open Scope R_scope.
Axiom todo : forall P, P.
Ltac todo := apply todo.
(* We start very abstractly *)
Variable F : R -> Prop. (* The predicate "to be a float" *)
Variables vx vy : R. (* Some values for our examples *)
Check F vx. (* vx is a floating-point number *)
Variable P : R -> R -> Prop. (* The relation
"to be a rounded value" *)
Check P vx vy. (* vy is a rounded value of vx *)
(******************************************************************************)
(* ROUNDING RELATION *)
(******************************************************************************)
(* What do we require for our relation "to be a rounded value"? *)
Lemma round_pred_P : round_pred P.
Proof.
split; red.
todo. (* - to be total *)
todo. (* - to be monotone *)
Qed.
Definition rnd := let (f, _) := round_fun_of_pred P round_pred_P in f.
Check rnd. (* the function associated with the
relation *)
Lemma rndP : forall x, P x (rnd x). (* its associated relation *)
Proof. unfold rnd; destruct (round_fun_of_pred P round_pred_P); auto. Qed.
(******************************************************************************)
(* ROUNDING DOWN, UP, TO ZERO *)
(******************************************************************************)
(* DOWN *)
Check Rnd_DN_pt F vx vy.
Eval lazy beta delta [Rnd_DN_pt] in Rnd_DN_pt F vx vy.
Check Rnd_DN F rnd.
Eval lazy beta delta [Rnd_DN] in Rnd_DN F rnd.
(* UP *)
Check Rnd_UP_pt F vx vy.
Eval lazy beta delta [Rnd_UP_pt] in Rnd_UP_pt F vx vy.
Check Rnd_UP F rnd.
Eval lazy beta delta [Rnd_UP] in Rnd_UP F rnd.
(* ZERO *)
Check Rnd_ZR_pt F vx vy.
Eval lazy beta delta [Rnd_ZR_pt] in Rnd_ZR_pt F vx vy.
Check Rnd_ZR F rnd.
Eval lazy beta delta [Rnd_ZR] in Rnd_ZR F rnd.
(******************************************************************************)
(* IDEMPOTENCE AND MONOTONY *)
(******************************************************************************)
Fact ex1 : forall x, F x -> Rnd_DN_pt F x x. (* Rnd_Dn_pt idempotent on F *)
Proof.
intros x Fx; repeat split.
- assumption.
- lra.
- intros g Fg gLx; assumption.
Qed.
Fact ex2 : round_pred_monotone (Rnd_DN_pt F). (* Rnd_Dn_pt monotone *)
Proof.
intros x y f g Rxf Rxg xLy.
destruct Rxf as [Ff [fLx fdown]].
destruct Rxg as [Fg [gLy gdown]].
apply gdown; try lra; assumption.
Qed.
(*
Prove that UP is also idempotent and monotone
Fact ex3 : forall x, F x -> Rnd_UP_pt F x x.
Proof.
...
Qed.
Fact ex4 : round_pred_monotone (Rnd_UP_pt F).
Proof.
...
Qed.
Prove that ZR is idempotent but only monotone if 0 is a float
Fact ex5 : forall x, F x -> Rnd_ZR_pt F x x.
Proof.
...
Qed.
Fact ex6 : F 0 -> round_pred_monotone (Rnd_ZR_pt F).
Proof.
...
Qed.
Hint:
In order to perform a case analysis on the fact that x is smaller to y or not
one can use the tactic "destruct (Rle_lt_dec x y) as [xLy | yLx]"
*)
(******************************************************************************)
(* TOTALITY *)
(******************************************************************************)
Fact ex7 : (forall x, F x -> F (-x)) -> (* the format is symmetric *)
round_pred_total (Rnd_DN_pt F) -> round_pred_total (Rnd_UP_pt F).
Proof.
intros sym tDN.
intros x.
destruct (tDN (-x)) as [y [Fy [yLx yM]]].
exists (-y); repeat split.
- apply sym; auto.
- lra.
- intros g Fg xLg.
assert (HH := yM _ (sym _ Fg)).
lra.
Qed.
Print satisfies_any.
Hypothesis SAF : satisfies_any F.
(*
Prove that DN, UP, ZR are rounding predicates
Fact ex8 : round_pred (Rnd_DN_pt F).
Proof.
...
Qed.
Fact ex9 : round_pred (Rnd_UP_pt F).
Proof.
...
Qed.
Fact ex10 : round_pred (Rnd_ZR_pt F).
Proof.
...
Qed.
*)
(******************************************************************************)
(* ROUNDING TO THE CLOSEST *)
(******************************************************************************)
Check Rnd_N_pt F vx vy.
Eval lazy beta delta [Rnd_N_pt] in Rnd_N_pt F vx vy.
Check Rnd_N.
Eval lazy beta delta [Rnd_N] in Rnd_N F rnd.
(*
Prove that N is idempotent, that is either UP or DOWN and that it is strictly
monotone
Fact ex11 : forall x, F x -> Rnd_N_pt F x x.
Proof.
...
Qed.
Fact ex12 : forall x f, Rnd_N_pt F x f -> Rnd_DN_pt F x f \/ Rnd_UP_pt F x f.
Proof.
...
Qed.
Fact ex13 : forall x y f g,
Rnd_N_pt F x f -> Rnd_N_pt F y g -> x < y -> f <= g.
Proof.
...
Qed.
Hints : some theorems about absolute values
Check Rabs_R0.
Check Rabs_pos.
Check Rabs_right.
Check Rabs_left.
*)
Variable T : R -> R -> Prop. (* Tie-break rule *)
Check Rnd_NG_pt F T vx vy.
Eval lazy beta delta [Rnd_NG_pt] in Rnd_NG_pt F T vx vy.
Check NG_existence_prop F T. (* condition to ensure existance *)
Eval lazy beta delta [NG_existence_prop] in NG_existence_prop F T.
Search NG_existence_prop.
Check Rnd_NG_pt_unique_prop F T.
Eval lazy beta delta [Rnd_NG_pt_unique_prop] in Rnd_NG_pt_unique_prop F T.
Check Rnd_NG_pt_monotone F T.
Search Rnd_NG_pt_unique_prop.
Definition Taway x f := Rabs f >= Rabs x.
(*
Prove that Taway verifies the two properties to build a rounding mode
Fact ex14 : NG_existence_prop F Taway.
Proof.
...
Qed.
Fact ex15 : F 0 -> Rnd_NG_pt_unicity_prop F Taway.
Proof.
...
Qed.
*)
(*
Resume
- "generic" version of format and roundind modes
- 4 rounding functions : up, down, to zero, to the nearest.
*)
End Lecture2.