-
Notifications
You must be signed in to change notification settings - Fork 1
/
lecture1_solution.v
160 lines (129 loc) · 4.49 KB
/
lecture1_solution.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
(******************************************************************************)
(* *)
(* LECTURE : Floating point numbers and formal proof *)
(* Laurent.Thery@inria.fr 10/26/2013 *)
(* *)
(******************************************************************************)
(* Solutions of lecture1 *)
Require Import Reals Psatz.
Section Solution1.
Open Scope R_scope.
(* Easy exercise *)
Fact ex3 : forall x y, (x = y) \/ (x = -y) -> x * x = y * y.
Proof.
intros x y [Exy | Exy]; rewrite Exy; ring.
Qed.
(*
Easy exercise: Prove that INR 42 = 42.
*)
Goal INR 42 = 42.
simpl.
ring.
Qed.
(*
Easy exercise: Prove that IZR(-42) = -42.
*)
Goal IZR(-42) = -42.
trivial.
Qed.
(* Easy exercise *)
Fact ex6 : forall x y, x <> y -> (x * x - y * y) / (x - y) = y + x.
Proof.
intros x y Dxy.
field.
contradict Dxy.
assert (Hx : x = (x - y) + y) by ring.
rewrite Hx; clear Hx.
rewrite Dxy; ring.
Qed.
(*
Easy exercise : define Rmax3 that takes 3 numbers and returns the largest
of the three
*)
Definition Rmax3 : R -> R -> R -> R.
intros x y z.
destruct (total_order_T x y) as [[xLy | xEy] | yLx].
destruct (total_order_T y z) as [[yLz | yEz] | zLy].
exact z.
exact z.
exact y.
destruct (total_order_T y z) as [[yLz | yEz] | zLy].
exact z.
exact z.
exact x.
destruct (total_order_T x z) as [[xLz | xEz] | zLx].
exact z.
exact z.
exact x.
Defined.
Definition Rmax2 : R -> R -> R.
intros x y.
destruct (total_order_T x y) as [[xLy |xEy] | xGy].
exact y.
exact x.
exact x.
Defined.
Fact Rmax3_def : forall x y z,
Rmax3 x y z = Rmax2 (Rmax2 x y) z.
Proof.
intros x y z; unfold Rmax2; unfold Rmax3.
destruct (total_order_T x y) as [[xLy | xEy] |xGy];
destruct (total_order_T y z) as [[yLz | yEz] |yGz];
destruct (total_order_T x z) as [[xLz | xEz] |xGz]; lra.
Qed.
(*
Exercise : Build a computational version of Req_dec
*)
Definition Reqc_dec (x y : R) : {x = y} + {x <> y}.
destruct (total_order_T x y) as [[xLy | xEy] | yLx].
abstract (right; lra).
left; assumption.
abstract (right; lra).
Defined.
(*
Easy exercise : Reprove the integral propertie without using Rmult_integral
*)
Lemma Rmult_integral1 r1 r2 : r1 * r2 = 0 -> r1 = 0 \/ r2 = 0.
Proof.
intros Hm.
destruct (Req_dec r1 0) as [ZR1|ZNR1]; [left; assumption | right].
replace r2 with (r1 * r2 / r1); [idtac | field; assumption].
rewrite Hm; field; assumption.
Qed.
(*
Exercise : Prove that (x * x = y * y) is equivalent to (Rabs x = Rabs y)
*)
Fact ex11 x y : x * x = y * y <-> Rabs x = Rabs y.
Proof.
split; intros H.
assert (H1: (x - y) * (x + y) = 0).
transitivity (x * x - y * y); [ring|lra].
split_Rabs; destruct (Rmult_integral _ _ H1) as [H2 | H2]; lra.
replace x with (--x); [idtac|ring].
replace y with (--y); [idtac|ring].
split_Rabs; rewrite H; ring.
Qed.
(*
Exercise,
given two real numbers x and y such that 0 < x < y, show that
if A is the arithmetic mean and G the geometric one then we have
x < G < A < y
*)
Fact geom x y : 0 < x < y ->
x < sqrt (x * y) < (x + y) /2 /\ (x + y) /2 < y.
Proof.
intros xPos.
assert (Lem := Rmult_le_pos); assert (Ltm := Rmult_lt_0_compat).
repeat split; try lra.
- assert (x = sqrt (x * x)) by (rewrite sqrt_square; lra).
assert (sqrt (x * x) < sqrt (x * y)); try lra.
apply sqrt_lt_1; try (apply Lem; lra).
apply Rmult_lt_compat_l; lra.
- replace ((x + y) / 2) with (sqrt (((x + y) / 2) * ((x + y) / 2)));
[idtac | rewrite sqrt_square; lra].
apply sqrt_lt_1; try (apply Lem; lra).
replace ((x + y) / 2 * ((x + y) / 2)) with (((y - x) * (y - x)) /4 + x * y);
[idtac|field].
assert (0 < (y - x) * (y - x) / 4); repeat apply Ltm; lra.
Qed.
End Solution1.