Debian Codegraph @

Formal_ineqs/trig/cos_eval.hl

raw
1 (* ========================================================================== *)
2 (* Formal verification of nonlinear inequalities in HOL Light *)
3 (* *)
4 (* Copyright (c) 2014 Alexey Solovyev *)
5 (* *)
6 (* This file is distributed under the terms of the MIT licence *)
7 (* ========================================================================== *)
8
9 (* -------------------------------------------------------------------------- *)
10 (* Formal interval evaluation of functions: cos *)
11 (* -------------------------------------------------------------------------- *)
12
13 needs "Formal_ineqs/misc/misc_functions.hl";;
14 needs "Formal_ineqs/misc/misc_vars.hl";;
15 needs "Formal_ineqs/arith/interval_arith.hl";;
16 needs "Formal_ineqs/trig/atn_eval.hl";;
17 needs "Formal_ineqs/trig/cos_bounds_eval.hl";;
18
19 module type Cos_eval_sig =
20 sig
21 val float_interval_cos : int -> thm -> thm
22 end;;
23
24 module Cos_eval : Cos_eval_sig = struct
25
26 open Misc_functions;;
27 open Float_theory;;
28 open Interval_arith;;
29 open More_float;;
30 open Arith_float;;
31 open Atn_eval;;
32 open Cos_bounds_eval;;
33 open Misc_vars;;
34
35 prioritize_real();;
36
37
38 (* Interval approximations of 2 * pi: *)
39 (* (|- interval_arith (&2 * pi) (lo, hi), |- hi - lo <= t, (lo, hi)) *)
40 let two_pi_array =
41 let n = Array.length pi_approx_array in
42 let pi = pi_approx_array.(n - 1) in
43 let two_pi = float_interval_mul n two_interval pi in
44 Array.init (n - 1) (fun i ->
45 let th = float_interval_round i two_pi in
46 let _, bounds = dest_interval_arith (concl th) in
47 let lo_tm, hi_tm = dest_pair bounds in
48 th, float_sub_hi i hi_tm lo_tm, (lo_tm, hi_tm));;
49
50 let pi32_array =
51 let n = Array.length pi_approx_array in
52 let pi = pi_approx_array.(n - 1) in
53 let three = mk_float_interval_small_num 3 in
54 let pi32 = float_interval_div n (float_interval_mul n three pi) two_interval in
55 Array.init (n - 1) (fun i ->
56 let th = float_interval_round i pi32 in
57 let _, bounds = dest_interval_arith (concl th) in
58 let lo_tm, hi_tm = dest_pair bounds in
59 th, (lo_tm, hi_tm));;
60
61
62 (* Interval approximations of -pi *)
63 let neg_pi_array =
64 let n = Array.length pi_approx_array in
65 Array.init n (fun i -> float_interval_neg pi_approx_array.(i));;
66
67 let neg_pi_le_pi_lo_array =
68 let th = prove(`(&0 <= lo <=> T) ==> --pi <= lo`, MP_TAC PI_POS_LE THEN REAL_ARITH_TAC) in
69 let n = Array.length pi_approx_array in
70 Array.init n (fun i ->
71 let lo = (rand o rator o rand o concl) pi_approx_array.(i) in
72 let lo_ge0 = float_ge0 lo in
73 MATCH_MP th lo_ge0);;
74
75
76 (* Theorems of the form: |- &2 * pi <= high, |- &0 <= low *)
77 let two_pi_high, two_pi_low =
78 let interval_pos = prove(`interval_arith x (float_num F n e, f)
79 ==> x <= f /\ &0 <= (float_num F n e)`,
80 REWRITE_TAC[interval_arith] THEN
81 MP_TAC (SPECL[`n:num`; `e:num`] FLOAT_F_POS) THEN
82 REAL_ARITH_TAC) in
83 let two_pi_list = Array.to_list two_pi_array in
84 let ths1 = map (fun (th,_,_) -> CONJUNCTS (MATCH_MP interval_pos th)) two_pi_list in
85 let list1, list2 = unzip (map pair_of_list ths1) in
86 Array.of_list list1, Array.of_list list2;;
87
88
89 (**********)
90
91 let f_pi = 3.14159265358979323846 and
92 f_2_pi = 6.28318530717958647693 and
93 f_pi_2 = 1.57079632679489661923;;
94
95
96 let th_rule = UNDISCH_ALL o PURE_REWRITE_RULE[GSYM IMP_IMP] o SPEC_ALL;;
97 let case_rule = PURE_REWRITE_RULE[NOT_CLAUSES; GSYM float1_eq; FLOAT_NEG] o th_rule;;
98
99 (* cos cases *)
100
101 let cos_minus_pi = prove(`!x. cos (x - pi) = -- cos x`,
102 ONCE_REWRITE_TAC[GSYM (SPEC `x - pi` COS_PERIODIC)] THEN
103 REWRITE_TAC[REAL_ARITH `a - b + &2 * b = a + b`] THEN
104 REWRITE_TAC[COS_PERIODIC_PI]);;
105
106 let cos_pi_minus = prove(`!x. cos (pi - x) = -- cos x`,
107 ONCE_REWRITE_TAC[REAL_ARITH `a - b = --(b - a)`] THEN
108 REWRITE_TAC[COS_NEG; cos_minus_pi]);;
109
110 let cos_2pi_minus = prove(`cos (&2 * pi - x) = cos x`,
111 REWRITE_TAC[COS_EQ] THEN EXISTS_TAC `&1` THEN
112 REWRITE_TAC[INTEGER_CLOSED] THEN REAL_ARITH_TAC);;
113
114 (* -1 *)
115 let cos_full_interval = prove(`!x. interval_arith (cos x) (-- &1, &1)`,
116 REWRITE_TAC[interval_arith; COS_BOUNDS]);;
117
118 let cos_case_neg1 = case_rule cos_full_interval;;
119
120
121 (* 0 *)
122 let cos_reduction_0_pi_1 = prove(`interval_arith x (a,b) /\
123 (?k. integer k /\ a1 <= a + (&2 * pi) * k
124 /\ b + (&2 * pi) * k <= b1) /\
125 interval_arith a1 (&0, pi) /\
126 b1 <= pi /\
127 cos a1 <= high /\ low <= cos b1
128 ==> interval_arith (cos x) (low, high)`,
129 REWRITE_TAC[interval_arith] THEN STRIP_TAC THEN REWRITE_TAC[GSYM interval_arith] THEN
130 MATCH_MP_TAC APPROX_INTERVAL THEN
131 EXISTS_TAC `cos b1` THEN EXISTS_TAC `cos a1` THEN ASM_REWRITE_TAC[interval_arith] THEN
132 SUBGOAL_THEN `cos x = cos (x + (&2 * pi) * k)` (fun th -> REWRITE_TAC[th]) THENL [
133 REWRITE_TAC[COS_EQ] THEN EXISTS_TAC `--k:real` THEN ASM_SIMP_TAC[INTEGER_CLOSED] THEN
134 REAL_ARITH_TAC;
135 ALL_TAC
136 ] THEN
137 CONJ_TAC THEN MATCH_MP_TAC COS_MONO_LE THEN REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
138
139 let cos_case0 = case_rule cos_reduction_0_pi_1;;
140
141 (* 1 *)
142 let cos_reduction_0_pi_2 = prove(`interval_arith x (a,b) /\
143 (?k. integer k /\ a1 <= a + (&2 * pi) * k
144 /\ b + (&2 * pi) * k <= b1) /\
145 interval_arith a1 (&0, pi) /\
146 b1 <= &2 * pi /\
147 cos a1 <= high1 /\ cos b1 <= high2 /\
148 max high1 high2 = high
149 ==> interval_arith (cos x) (-- &1, high)`,
150 REWRITE_TAC[interval_arith; COS_BOUNDS] THEN STRIP_TAC THEN
151 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `max (cos a1) (cos b1)` THEN
152 CONJ_TAC THENL [ ALL_TAC; REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC ] THEN
153 REWRITE_TAC[REAL_LE_MAX] THEN
154 SUBGOAL_THEN `cos x = cos (x + (&2 * pi) * k)` (fun th -> REWRITE_TAC[th]) THENL [
155 REWRITE_TAC[COS_EQ] THEN EXISTS_TAC `--k:real` THEN ASM_SIMP_TAC[INTEGER_CLOSED] THEN
156 REAL_ARITH_TAC;
157 ALL_TAC
158 ] THEN
159 ASM_CASES_TAC `x + (&2 * pi) * k <= pi` THENL [
160 DISJ1_TAC THEN
161 MATCH_MP_TAC COS_MONO_LE THEN
162 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC;
163 ALL_TAC
164 ] THEN
165 DISJ2_TAC THEN
166 ONCE_REWRITE_TAC[GSYM REAL_LE_NEG2] THEN
167 REWRITE_TAC[GSYM cos_minus_pi] THEN
168 MATCH_MP_TAC COS_MONO_LE THEN
169 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
170
171 let cos_case1 = case_rule cos_reduction_0_pi_2;;
172
173 (* 2 *)
174 let cos_reduction_0_pi_2a = prove(`interval_arith x (a,b) /\
175 (?k. integer k /\ a1 <= a + (&2 * pi) * k
176 /\ b + (&2 * pi) * k <= b1) /\
177 interval_arith a1 (&0, pi) /\
178 a1 + b1 <= &2 * pi /\
179 cos a1 <= high
180 ==> interval_arith (cos x) (-- &1, high)`,
181 STRIP_TAC THEN MATCH_MP_TAC (GEN_ALL cos_reduction_0_pi_2) THEN
182 MAP_EVERY EXISTS_TAC [`a:real`; `b:real`; `a1:real`; `b1:real`; `high:real`; `high:real`] THEN
183 ASM_REWRITE_TAC[REAL_MAX_ACI] THEN
184 REPEAT CONJ_TAC THENL [
185 EXISTS_TAC `k:real` THEN ASM_REWRITE_TAC[];
186 REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC;
187 ALL_TAC
188 ] THEN
189 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `cos a1` THEN ASM_REWRITE_TAC[] THEN
190 ASM_CASES_TAC `b1 <= pi` THENL [
191 MATCH_MP_TAC COS_MONO_LE THEN
192 REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC;
193 ALL_TAC
194 ] THEN
195 ONCE_REWRITE_TAC[GSYM REAL_LE_NEG2] THEN
196 REWRITE_TAC[GSYM cos_minus_pi] THEN
197 SUBGOAL_THEN `cos (a1 - pi) = cos (pi - a1)` (fun th -> REWRITE_TAC[th]) THENL [
198 ONCE_REWRITE_TAC[REAL_ARITH `a1 - pi = --(pi - a1)`] THEN REWRITE_TAC[COS_NEG];
199 ALL_TAC
200 ] THEN
201 MATCH_MP_TAC COS_MONO_LE THEN
202 REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
203
204 let cos_case2 = case_rule cos_reduction_0_pi_2a;;
205
206 (* 3 *)
207 let cos_reduction_0_pi_2b = prove(`interval_arith x (a,b) /\
208 (?k. integer k /\ a1 <= a + (&2 * pi) * k
209 /\ b + (&2 * pi) * k <= b1) /\
210 interval_arith a1 (&0, pi) /\
211 b1 <= &2 * pi /\ &2 * pi <= a1 + b1 /\
212 cos b1 <= high
213 ==> interval_arith (cos x) (-- &1, high)`,
214 STRIP_TAC THEN MATCH_MP_TAC (GEN_ALL cos_reduction_0_pi_2) THEN
215 MAP_EVERY EXISTS_TAC [`a:real`; `b:real`; `a1:real`; `b1:real`; `high:real`; `high:real`] THEN
216 ASM_REWRITE_TAC[REAL_MAX_ACI] THEN
217 REPEAT CONJ_TAC THENL [
218 EXISTS_TAC `k:real` THEN ASM_REWRITE_TAC[];
219 ALL_TAC
220 ] THEN
221 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `cos b1` THEN ASM_REWRITE_TAC[] THEN
222 ONCE_REWRITE_TAC[GSYM REAL_LE_NEG2] THEN
223 REWRITE_TAC[GSYM cos_minus_pi] THEN
224 SUBGOAL_THEN `cos (a1 - pi) = cos (pi - a1)` (fun th -> REWRITE_TAC[th]) THENL [
225 ONCE_REWRITE_TAC[REAL_ARITH `a1 - pi = --(pi - a1)`] THEN REWRITE_TAC[COS_NEG];
226 ALL_TAC
227 ] THEN
228 MATCH_MP_TAC COS_MONO_LE THEN
229 REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
230
231 let cos_case3 = case_rule cos_reduction_0_pi_2b;;
232
233 (* 4 *)
234 let cos_reduction_neg_pi_0_1 = prove(`interval_arith x (a,b) /\
235 (?k. integer k /\ a1 <= a + (&2 * pi) * k
236 /\ b + (&2 * pi) * k <= b1) /\
237 interval_arith a1 (--pi, &0) /\
238 (b1 <= &0 <=> T) /\
239 low <= cos a1 /\ cos b1 <= high
240 ==> interval_arith (cos x) (low, high)`,
241 STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM INTERVAL_NEG_EQ] THEN REWRITE_TAC[GSYM COS_PERIODIC_PI] THEN
242 MATCH_MP_TAC (GEN_ALL cos_reduction_0_pi_1) THEN
243 MAP_EVERY EXISTS_TAC [`a + pi`; `b + pi`; `a1 + pi`; `b1 + pi`] THEN
244 REWRITE_TAC[COS_PERIODIC_PI] THEN
245 REPEAT CONJ_TAC THEN TRY (REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC) THEN
246 EXISTS_TAC `k:real` THEN
247 ASM_REWRITE_TAC[] THEN CONJ_TAC THENL [
248 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
249 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
250 ]);;
251
252 let cos_case4 = case_rule cos_reduction_neg_pi_0_1;;
253
254 (* 5 (not used in computations) *)
255 let cos_reduction_neg_pi_0_2 = prove(`interval_arith x (a,b) /\
256 (?k. integer k /\ a1 <= a + (&2 * pi) * k
257 /\ b + (&2 * pi) * k <= b1) /\
258 interval_arith a1 (--pi, &0) /\
259 b1 <= pi /\
260 low1 <= cos a1 /\ low2 <= cos b1 /\
261 min low1 low2 = low
262 ==> interval_arith (cos x) (low, &1)`,
263 STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM INTERVAL_NEG_EQ] THEN REWRITE_TAC[GSYM COS_PERIODIC_PI] THEN
264 MATCH_MP_TAC (GEN_ALL cos_reduction_0_pi_2) THEN
265 MAP_EVERY EXISTS_TAC [`a + pi`; `b + pi`; `a1 + pi`; `b1 + pi`; `--low1`; `--low2`] THEN
266 REWRITE_TAC[COS_PERIODIC_PI] THEN
267 REPEAT CONJ_TAC THEN TRY (REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC) THEN
268 EXISTS_TAC `k:real` THEN
269 ASM_REWRITE_TAC[] THEN CONJ_TAC THENL [
270 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
271 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
272 ]);;
273
274 let cos_case5 = case_rule cos_reduction_neg_pi_0_2;;
275
276 (* 6 *)
277 let cos_reduction_neg_pi_0_2a = prove(`interval_arith x (a,b) /\
278 (?k. integer k /\ a1 <= a + (&2 * pi) * k
279 /\ b + (&2 * pi) * k <= b1) /\
280 interval_arith a1 (--pi, &0) /\
281 b2 = --b1 /\ (a1 <= b2 <=> T) /\
282 low <= cos a1
283 ==> interval_arith (cos x) (low, &1)`,
284 STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM INTERVAL_NEG_EQ] THEN REWRITE_TAC[GSYM COS_PERIODIC_PI] THEN
285 MATCH_MP_TAC (GEN_ALL cos_reduction_0_pi_2a) THEN
286 MAP_EVERY EXISTS_TAC [`a + pi`; `b + pi`; `b1 + pi`; `a1 + pi`] THEN
287 REWRITE_TAC[COS_PERIODIC_PI] THEN
288 REPEAT CONJ_TAC THEN TRY (REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC) THEN
289 EXISTS_TAC `k:real` THEN
290 ASM_REWRITE_TAC[] THEN CONJ_TAC THENL [
291 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
292 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
293 ]);;
294
295 let cos_case6 = case_rule cos_reduction_neg_pi_0_2a;;
296
297 (* 7 *)
298 let cos_reduction_neg_pi_0_2b = prove(`interval_arith x (a,b) /\
299 (?k. integer k /\ a1 <= a + (&2 * pi) * k
300 /\ b + (&2 * pi) * k <= b1) /\
301 interval_arith a1 (--pi, &0) /\
302 b1 <= pi /\ b2 = --b1 /\ (a1 <= b2 <=> F) /\
303 low <= cos b1
304 ==> interval_arith (cos x) (low, &1)`,
305 STRIP_TAC THEN ONCE_REWRITE_TAC[GSYM INTERVAL_NEG_EQ] THEN REWRITE_TAC[GSYM COS_PERIODIC_PI] THEN
306 MATCH_MP_TAC (GEN_ALL cos_reduction_0_pi_2b) THEN
307 MAP_EVERY EXISTS_TAC [`a + pi`; `b + pi`; `a1 + pi`; `b1 + pi`] THEN
308 REWRITE_TAC[COS_PERIODIC_PI] THEN
309 REPEAT CONJ_TAC THEN TRY (REPEAT (POP_ASSUM MP_TAC) THEN REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC) THEN
310 EXISTS_TAC `k:real` THEN
311 ASM_REWRITE_TAC[] THEN CONJ_TAC THENL [
312 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
313 ASM_REWRITE_TAC[REAL_ARITH `(a + pi) + b = (a + b) + pi`; REAL_LE_RADD];
314 ]);;
315
316 let cos_case7 = case_rule cos_reduction_neg_pi_0_2b;;
317
318 (* ---------------------------- *)
319 (* Reduction to [0, pi / 2] *)
320 (* ---------------------------- *)
321
322 let cos_high_trivial = (case_rule o prove)
323 (`cos x <= &1`,
324 REWRITE_TAC[COS_BOUNDS]);;
325
326 let cos_low_trivial = (case_rule o prove)
327 (`-- &1 <= cos x`,
328 REWRITE_TAC[COS_BOUNDS]);;
329
330 let interval_neg_pi_0 = (case_rule o prove)
331 (`interval_arith x (--pi, &0) /\ y = --x
332 ==> interval_arith y (&0, pi)`,
333 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
334
335 let reduction_high_neg = (case_rule o prove)
336 (`y = --x /\ cos y <= high ==> cos x <= high`,
337 STRIP_TAC THEN POP_ASSUM MP_TAC THEN ASM_REWRITE_TAC[COS_NEG]);;
338
339 let reduction_low_neg = (case_rule o prove)
340 (`y = --x /\ low <= cos y ==> low <= cos x`,
341 STRIP_TAC THEN POP_ASSUM MP_TAC THEN ASM_REWRITE_TAC[COS_NEG]);;
342
343 let reduction_high_0_pi = (case_rule o prove)
344 (`interval_arith x (&0, pi) /\
345 interval_arith pi (lo, hi) /\
346 hi - x <= t /\ t <= pi /\
347 r <= cos t /\ high = --r
348 ==> cos x <= high`,
349 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
350 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `--cos t` THEN ASM_REWRITE_TAC[REAL_LE_NEG2] THEN
351 REWRITE_TAC[GSYM cos_pi_minus] THEN
352 MATCH_MP_TAC COS_MONO_LE THEN ASM_REWRITE_TAC[] THEN
353 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
354
355 let reduction_low_0_pi = (case_rule o prove)
356 (`interval_arith x (&0, pi) /\
357 interval_arith pi (lo, hi) /\
358 t <= lo - x /\ (&0 <= t <=> T) /\
359 cos t <= r /\ low = --r
360 ==> low <= cos x`,
361 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
362 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `--cos t` THEN ASM_REWRITE_TAC[REAL_LE_NEG2] THEN
363 REWRITE_TAC[GSYM cos_pi_minus] THEN
364 MATCH_MP_TAC COS_MONO_LE THEN ASM_REWRITE_TAC[] THEN
365 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
366
367 let reduction_high_pi_2pi_1 = (case_rule o prove)
368 (`interval_arith x (pi, &2 * pi) /\
369 interval_arith (&2 * pi) (lo, hi) /\
370 t <= lo - x /\ (&0 <= t <=> T) /\
371 cos t <= r
372 ==> cos x <= r`,
373 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
374 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `cos t` THEN ASM_REWRITE_TAC[] THEN
375 SUBGOAL_THEN `cos x = cos (&2 * pi - x)` (fun th -> ONCE_REWRITE_TAC[th]) THENL [
376 REWRITE_TAC[cos_2pi_minus];
377 ALL_TAC
378 ] THEN
379 MATCH_MP_TAC COS_MONO_LE THEN ASM_REWRITE_TAC[] THEN
380 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
381
382 let reduction_low_pi_2pi_1 = (case_rule o prove)
383 (`interval_arith x (pi, &2 * pi) /\
384 interval_arith (&2 * pi) (lo, hi) /\
385 hi - x <= t /\ t <= pi /\
386 r <= cos t
387 ==> r <= cos x`,
388 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
389 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `cos t` THEN ASM_REWRITE_TAC[] THEN
390 SUBGOAL_THEN `cos x = cos (&2 * pi - x)` (fun th -> ONCE_REWRITE_TAC[th]) THENL [
391 REWRITE_TAC[cos_2pi_minus];
392 ALL_TAC
393 ] THEN
394 MATCH_MP_TAC COS_MONO_LE THEN ASM_REWRITE_TAC[] THEN
395 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
396
397 let reduction_high_pi_2pi_2 = (case_rule o prove)
398 (`interval_arith x (pi, &2 * pi) /\
399 interval_arith pi (lo, hi) /\
400 x - lo <= t /\ t <= pi /\
401 r <= cos t /\ high = --r
402 ==> cos x <= high`,
403 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
404 SUBGOAL_THEN `cos x = --cos (x - pi)` (fun th -> ONCE_REWRITE_TAC[th]) THENL [
405 REWRITE_TAC[cos_minus_pi; REAL_NEG_NEG];
406 ALL_TAC
407 ] THEN
408 ASM_REWRITE_TAC[REAL_LE_NEG2] THEN
409 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `cos t` THEN ASM_REWRITE_TAC[] THEN
410 MATCH_MP_TAC COS_MONO_LE THEN ASM_REWRITE_TAC[] THEN
411 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
412
413 let reduction_low_pi_2pi_2 = (case_rule o prove)
414 (`interval_arith x (pi, &2 * pi) /\
415 interval_arith pi (lo, hi) /\
416 t <= x - hi /\ (&0 <= t <=> T) /\
417 cos t <= r /\ low = --r
418 ==> low <= cos x`,
419 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
420 SUBGOAL_THEN `cos x = --cos (x - pi)` (fun th -> ONCE_REWRITE_TAC[th]) THENL [
421 REWRITE_TAC[cos_minus_pi; REAL_NEG_NEG];
422 ALL_TAC
423 ] THEN
424 ASM_REWRITE_TAC[REAL_LE_NEG2] THEN
425 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `cos t` THEN ASM_REWRITE_TAC[] THEN
426 MATCH_MP_TAC COS_MONO_LE THEN ASM_REWRITE_TAC[] THEN
427 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
428
429
430 (* ----------------------------- *)
431 (* Reduction theorems *)
432 (* ----------------------------- *)
433
434 let reduction_zero_th = (th_rule o prove)
435 (`?k. integer k /\ a <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b`,
436 EXISTS_TAC `&0` THEN REWRITE_TAC[INTEGER_CLOSED] THEN REAL_ARITH_TAC);;
437
438 let reduction_neg_th = (th_rule o prove)
439 (`&i = z /\ interval_arith (&2 * pi) (lo, hi) /\ &0 <= lo /\
440 z * hi <= m /\ a1 <= a - m /\
441 n <= z * lo /\ b - n <= b1
442 ==> ?k. integer k /\ a1 <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b1`,
443 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
444 EXISTS_TAC `-- &i` THEN SIMP_TAC[INTEGER_CLOSED] THEN
445 CONJ_TAC THENL [
446 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `a - m:real` THEN
447 ASM_REWRITE_TAC[REAL_LE_LADD; real_sub; REAL_MUL_RNEG; REAL_LE_NEG2] THEN
448 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `z * hi:real` THEN
449 ASM_REWRITE_TAC[REAL_ARITH `(&2 * pi) * z = z * (&2 * pi)`] THEN
450 MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[] THEN
451 REPEAT (POP_ASSUM MP_TAC) THEN MP_TAC PI_POS_LE THEN REAL_ARITH_TAC;
452 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `b - n:real` THEN
453 ASM_REWRITE_TAC[REAL_LE_LADD; real_sub; REAL_MUL_RNEG; REAL_LE_NEG2] THEN
454 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `z * lo:real` THEN
455 ASM_REWRITE_TAC[REAL_ARITH `(&2 * pi) * z = z * (&2 * pi)`] THEN
456 MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[] THEN
457 REPEAT (POP_ASSUM MP_TAC) THEN MP_TAC PI_POS_LE THEN REAL_ARITH_TAC
458 ]);;
459
460 let reduction_pos_th = (th_rule o prove)
461 (`&i = z /\ interval_arith (&2 * pi) (lo, hi) /\ &0 <= lo /\
462 m <= z * lo /\ a1 <= a + m /\
463 z * hi <= n /\ b + n <= b1
464 ==> ?k. integer k /\ a1 <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b1`,
465 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN
466 EXISTS_TAC `&i` THEN SIMP_TAC[INTEGER_CLOSED] THEN
467 CONJ_TAC THENL [
468 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `a + m:real` THEN
469 ASM_REWRITE_TAC[REAL_LE_LADD] THEN
470 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `z * lo:real` THEN
471 ASM_REWRITE_TAC[REAL_ARITH `(&2 * pi) * z = z * (&2 * pi)`] THEN
472 MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[] THEN
473 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC;
474 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `b + n:real` THEN
475 ASM_REWRITE_TAC[REAL_LE_LADD] THEN
476 MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `z * hi:real` THEN
477 ASM_REWRITE_TAC[REAL_ARITH `(&2 * pi) * z = z * (&2 * pi)`] THEN
478 MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[] THEN
479 REPEAT (POP_ASSUM MP_TAC) THEN MP_TAC PI_POS_LE THEN REAL_ARITH_TAC
480 ]);;
481
482 let a_neg_pi_correction = (th_rule o prove)
483 (`interval_arith (&2 * pi) (lo, hi) /\
484 a2 <= a1 + lo /\ b1 + hi <= b2 /\
485 (?k. integer k /\ a1 <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b1)
486 ==> (?k. integer k /\ a2 <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b2)`,
487 REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN EXISTS_TAC `k + &1` THEN
488 ASM_SIMP_TAC[INTEGER_ADD_EQ; INTEGER_CLOSED] THEN
489 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
490
491 let a_pi_correction1, a_pi_correction2 = (pair_of_list o CONJUNCTS o th_rule o prove)
492 (`interval_arith pi (lo, hi) /\
493 (a1 <= lo <=> F)
494 ==> lo <= a1 /\ lo <= pi`,
495 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
496
497 let ab_sum1_correction = (th_rule o prove)
498 (`interval_arith (&2 * pi) (lo, hi) /\
499 m <= a1 + b1 /\ (lo <= m <=> T) /\ hi - lo <= t /\
500 b1 + t <= b2
501 ==> b1 <= b2 /\ &2 * pi <= a1 + b2`,
502 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
503
504 let ab_sum2_correction = (th_rule o prove)
505 (`interval_arith (&2 * pi) (lo, hi) /\
506 a1 + b1 <= n /\ (n <= lo <=> T)
507 ==> a1 + b1 <= &2 * pi`,
508 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
509
510 let ab_sum3_correction = (th_rule o prove)
511 (`interval_arith (&2 * pi) (lo, hi) /\ hi - lo <= t /\
512 m <= a1 + b1 /\ a1 + b1 <= n /\ n - m <= r /\
513 (n <= lo <=> F) /\ r + t <= z /\
514 b1 + z <= b2
515 ==> b1 <= b2 /\ &2 * pi <= a1 + b2`,
516 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
517
518 let ab_sum4_correction = (th_rule o prove)
519 (`interval_arith (&2 * pi) (lo, hi) /\
520 m <= a1 + b1 /\ (hi <= m <=> T)
521 ==> &2 * pi <= a1 + b1`,
522 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
523
524 let a_b_correction = (th_rule o prove)
525 (`a2 <= a1 /\ b1 <= b2 /\
526 (?k. integer k /\ a1 <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b1)
527 ==> (?k. integer k /\ a2 <= a + (&2 * pi) * k /\ b + (&2 * pi) * k <= b2)`,
528 REPEAT STRIP_TAC THEN EXISTS_TAC `k:real` THEN ASM_REWRITE_TAC[] THEN
529 REPEAT (POP_ASSUM MP_TAC) THEN REAL_ARITH_TAC);;
530
531 let a1_case1 = (th_rule o prove)(`(&0 <= a1 <=> T) /\ a1 <= pi ==> interval_arith a1 (&0, pi)`,
532 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
533
534 let a1_case2 = (th_rule o prove)(`(&0 <= a1 <=> F) /\ --pi <= a1 ==> interval_arith a1 (--pi, &0)`,
535 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
536
537 let b1_interval_0_pi_1 = (case_rule o prove)
538 (`(&0 <= b1 <=> T) /\ b1 <= pi ==> interval_arith b1 (&0, pi)`,
539 REWRITE_TAC[interval_arith]);;
540
541 let b1_interval_0_pi_2 = (case_rule o prove)
542 (`b1 <= pi /\ (&0 <= a1 <=> F) /\ b2 = --b1 /\ (a1 <= b2 <=> F)
543 ==> interval_arith b1 (&0, pi)`,
544 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
545
546 let b1_interval_pi_2pi = (case_rule o prove)
547 (`&2 * pi <= a1 + b1 /\ a1 <= pi /\ b1 <= &2 * pi
548 ==> interval_arith b1 (pi, &2 * pi)`,
549 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
550
551 let b1_interval_neg_pi_0 = (case_rule o prove)
552 (`(b1 <= &0 <=> T) /\ (a1 <= b1 <=> T) /\ --pi <= a1
553 ==> interval_arith b1 (--pi, &0)`,
554 REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);;
555
556
557
558 (* ------------------------------ *)
559 (* Reduction procedures *)
560 (* ------------------------------ *)
561
562 (* Upper bound for x in [0, pi] *)
563 let eval_high_0_pi pp x_0_pi x_tm =
564 let pi2_th = pi2_approx_array.(pp) in
565 let flag, ge_pi2 = float_prove_ge_interval x_tm pi2_th in
566 if not flag then
567 float_cos_high_raw pp x_tm
568 else
569 let pi_th = pi_approx_array.(pp) in
570 let bounds = rand (concl pi_th) in
571 let ltm, hi_tm = dest_comb bounds in
572 let lo_tm = rand ltm in
573 let hi_sub_x = float_sub_hi pp hi_tm x_tm in
574 let t_tm = rand (concl hi_sub_x) in
575 let flag, t_le_pi = float_prove_le_interval t_tm pi_th in
576 if not flag then
577 INST[x_tm, x_var_real] cos_high_trivial
578 else
579 let cos_t = float_cos_low_raw pp t_tm in
580 let r_tm = rand (rator (concl cos_t)) in
581 let high_eq = float_neg r_tm in
582 let high_tm = rand (rator (concl high_eq)) in
583 let th0 = INST[x_tm, x_var_real; high_tm, high_var_real;
584 lo_tm, lo_var_real; hi_tm, hi_var_real;
585 r_tm, r_var_real; t_tm, t_var_real] reduction_high_0_pi in
586 itlist MY_PROVE_HYP [high_eq; pi_th; x_0_pi; cos_t; t_le_pi; hi_sub_x] th0;;
587
588 (* Lower bound for x in [0, pi] *)
589 let eval_low_0_pi pp x_0_pi x_tm =
590 let pi2_th = pi2_approx_array.(pp) in
591 let flag, ge_pi2 = float_prove_ge_interval x_tm pi2_th in
592 if not flag then
593 float_cos_low_raw pp x_tm
594 else
595 let pi_th = pi_approx_array.(pp) in
596 let bounds = rand (concl pi_th) in
597 let ltm, hi_tm = dest_comb bounds in
598 let lo_tm = rand ltm in
599 let lo_sub_x = float_sub_lo pp lo_tm x_tm in
600 let t_tm = rand (rator (concl lo_sub_x)) in
601 let t_ge0 = float_ge0 t_tm in
602 if (fst o dest_const o rand o concl) t_ge0 = "F" then
603 INST[x_tm, x_var_real] cos_low_trivial
604 else
605 let cos_t = float_cos_high_raw pp t_tm in
606 let r_tm = rand (concl cos_t) in
607 let low_eq = float_neg r_tm in
608 let low_tm = rand (rator (concl low_eq)) in
609 let th0 = INST[x_tm, x_var_real; low_tm, low_var_real;
610 lo_tm, lo_var_real; hi_tm, hi_var_real;
611 r_tm, r_var_real; t_tm, t_var_real] reduction_low_0_pi in
612 itlist MY_PROVE_HYP [low_eq; pi_th; x_0_pi; cos_t; t_ge0; lo_sub_x] th0;;
613
614 (* Upper bound for x in [-pi, 0] *)
615 let eval_high_neg_pi_0 pp x_neg_pi_0 x_tm =
616 let y_neg_x = float_neg x_tm in
617 let y_tm = rand (rator (concl y_neg_x)) in
618 let y_th0 = INST[x_tm, x_var_real; y_tm, y_var_real] interval_neg_pi_0 in
619 let y_int_th = MY_PROVE_HYP y_neg_x (MY_PROVE_HYP x_neg_pi_0 y_th0) in
620 let cos_y = eval_high_0_pi pp y_int_th y_tm in
621 let high_tm = rand (concl cos_y) in
622 let th0 = INST[x_tm, x_var_real; y_tm, y_var_real; high_tm, high_var_real] reduction_high_neg in
623 MY_PROVE_HYP y_neg_x (MY_PROVE_HYP cos_y th0);;
624
625 (* Lower bound for x in [-pi, 0] *)
626 let eval_low_neg_pi_0 pp x_neg_pi_0 x_tm =
627 let y_neg_x = float_neg x_tm in
628 let y_tm = rand (rator (concl y_neg_x)) in
629 let y_th0 = INST[x_tm, x_var_real; y_tm, y_var_real] interval_neg_pi_0 in
630 let y_int_th = MY_PROVE_HYP y_neg_x (MY_PROVE_HYP x_neg_pi_0 y_th0) in
631 let cos_y = eval_low_0_pi pp y_int_th y_tm in
632 let low_tm = rand (rator (concl cos_y)) in
633 let th0 = INST[x_tm, x_var_real; y_tm, y_var_real; low_tm, low_var_real] reduction_low_neg in
634 MY_PROVE_HYP y_neg_x (MY_PROVE_HYP cos_y th0);;
635
636 (* Upper bound for x in [pi, 2 * pi] *)
637 let eval_high_pi_2pi pp x_pi_2pi x_tm =
638 let _, (pi32_lo, _) = pi32_array.(pp) in
639 let flag, le_pi32 = float_prove_le x_tm pi32_lo in
640 if flag then
641 (* x in [pi, 3/2 * pi] ==> consider (x - pi) *)
642 let pi_th = pi_approx_array.(pp) in
643 let bounds = rand (concl pi_th) in
644 let ltm, hi_tm = dest_comb bounds in
645 let lo_tm = rand ltm in
646 let x_sub_lo = float_sub_hi pp x_tm lo_tm in
647 let t_tm = rand (concl x_sub_lo) in
648 let flag, t_le_pi = float_prove_le_interval t_tm pi_th in
649 if not flag then
650 INST[x_tm, x_var_real] cos_high_trivial
651 else
652 let cos_t = float_cos_low_raw pp t_tm in
653 let r_tm = rand (rator (concl cos_t)) in
654 let high_eq = float_neg r_tm in
655 let high_tm = rand (rator (concl high_eq)) in
656 let th0 = INST[x_tm, x_var_real; high_tm, high_var_real;
657 lo_tm, lo_var_real; hi_tm, hi_var_real;
658 r_tm, r_var_real; t_tm, t_var_real] reduction_high_pi_2pi_2 in
659 itlist MY_PROVE_HYP [high_eq; pi_th; x_pi_2pi; cos_t; t_le_pi; x_sub_lo] th0
660 else
661 (* x in [3/2 * pi, pi] ==> consider (2 * pi - x) *)
662 let two_pi, _, (lo_tm, hi_tm) = two_pi_array.(pp) in
663 let lo_sub_x = float_sub_lo pp lo_tm x_tm in
664 let t_tm = rand (rator (concl lo_sub_x)) in
665 let t_ge0 = float_ge0 t_tm in
666 if (fst o dest_const o rand o concl) t_ge0 = "F" then
667 INST[x_tm, x_var_real] cos_high_trivial
668 else
669 let cos_t = float_cos_high_raw pp t_tm in
670 let r_tm = rand (concl cos_t) in
671 let th0 = INST[x_tm, x_var_real;
672 lo_tm, lo_var_real; hi_tm, hi_var_real;
673 r_tm, r_var_real; t_tm, t_var_real] reduction_high_pi_2pi_1 in
674 itlist MY_PROVE_HYP [t_ge0; x_pi_2pi; two_pi; lo_sub_x; cos_t] th0;;
675
676 (* Lower bound for x in [pi, 2 * pi] *)
677 let eval_low_pi_2pi pp x_pi_2pi x_tm =
678 let _, (pi32_lo, _) = pi32_array.(pp) in
679 let flag, le_pi32 = float_prove_le x_tm pi32_lo in
680 if flag then
681 (* x in [pi, 3/2 * pi] ==> consider (x - pi) *)
682 let pi_th = pi_approx_array.(pp) in
683 let bounds = rand (concl pi_th) in
684 let ltm, hi_tm = dest_comb bounds in
685 let lo_tm = rand ltm in
686 let x_sub_hi = float_sub_lo pp x_tm hi_tm in
687 let t_tm = rand (rator (concl x_sub_hi)) in
688 let t_ge0 = float_ge0 t_tm in
689 if (fst o dest_const o rand o concl) t_ge0 = "F" then
690 INST[x_tm, x_var_real] cos_low_trivial
691 else
692 let cos_t = float_cos_high_raw pp t_tm in
693 let r_tm = rand (concl cos_t) in
694 let low_eq = float_neg r_tm in
695 let low_tm = rand (rator (concl low_eq)) in
696 let th0 = INST[x_tm, x_var_real; low_tm, low_var_real;
697 lo_tm, lo_var_real; hi_tm, hi_var_real;
698 r_tm, r_var_real; t_tm, t_var_real] reduction_low_pi_2pi_2 in
699 itlist MY_PROVE_HYP [low_eq; pi_th; x_pi_2pi; cos_t; t_ge0; x_sub_hi] th0
700 else
701 (* x in [3/2 * pi, pi] ==> consider (2 * pi - x) *)
702 let two_pi, _, (lo_tm, hi_tm) = two_pi_array.(pp) in
703 let pi_th = pi_approx_array.(pp) in
704 let hi_sub_x = float_sub_hi pp hi_tm x_tm in
705 let t_tm = rand (concl hi_sub_x) in
706 let flag, t_le_pi = float_prove_le_interval t_tm pi_th in
707 if not flag then
708 INST[x_tm, x_var_real] cos_low_trivial
709 else
710 let cos_t = float_cos_low_raw pp t_tm in
711 let r_tm = rand (rator (concl cos_t)) in
712 let th0 = INST[x_tm, x_var_real;
713 lo_tm, lo_var_real; hi_tm, hi_var_real;
714 r_tm, r_var_real; t_tm, t_var_real] reduction_low_pi_2pi_1 in
715 itlist MY_PROVE_HYP [t_le_pi; x_pi_2pi; two_pi; hi_sub_x; cos_t] th0;;
716
717
718 (* ------------------------------ *)
719 (* Interval reduction *)
720 (* ------------------------------ *)
721
722 exception Correction_failed;;
723
724 let get_i f_tm =
725 let x = float_of_float_tm f_tm in
726 let k0 = -int_of_float (x /. f_2_pi) in
727 let y = x +. float_of_int k0 *. f_2_pi in
728 if y < -.f_pi then k0 + 1
729 else if y > f_pi then k0 - 1
730 else k0;;
731
732 (* i = 0 *)
733 (* a = f_tm *)
734
735 (* i = 0 *)
736 let reduction_zero a_tm b_tm =
737 let th0 = INST[a_tm, a_var_real; b_tm, b_var_real] reduction_zero_th in
738 a_tm, b_tm, th0;;
739
740 (* i < 0 *)
741 let reduction_neg pp i a_tm b_tm =
742 let i_eq_th, i_num_tm = float_eq_th_of_num (Num.num_of_int (-i)) in
743 let z_tm = rand (concl i_eq_th) in
744 let two_pi_th, _, (lo_tm, hi_tm) = two_pi_array.(pp) in
745 let lo_ge0_th = two_pi_low.(pp) in
746 let z_hi_th = float_mul_hi pp z_tm hi_tm in
747 let z_lo_th = float_mul_lo pp z_tm lo_tm in
748 let m_tm = rand (concl z_hi_th) in
749 let n_tm = rand (rator (concl z_lo_th)) in
750 let a_sub_m_th = float_sub_lo pp a_tm m_tm in
751 let b_sub_n_th = float_sub_hi pp b_tm n_tm in
752 let a1_tm = rand (rator (concl a_sub_m_th)) in
753 let b1_tm = rand (concl b_sub_n_th) in
754 let th0 = INST[z_tm, z_var_real; i_num_tm, i_var_num;
755 lo_tm, lo_var_real; hi_tm, hi_var_real;
756 a_tm, a_var_real; a1_tm, a1_var_real;
757 b_tm, b_var_real; b1_tm, b1_var_real;
758 m_tm, m_var_real; n_tm, n_var_real] reduction_neg_th in
759 let th1 = itlist MY_PROVE_HYP [i_eq_th; two_pi_th; lo_ge0_th;
760 z_hi_th; z_lo_th; a_sub_m_th; b_sub_n_th] th0 in
761 a1_tm, b1_tm, th1;;
762
763 (* i > 0 *)
764 let reduction_pos pp i a_tm b_tm =
765 let i_eq_th, i_num_tm = float_eq_th_of_num (Num.num_of_int i) in
766 let z_tm = rand (concl i_eq_th) in
767 let two_pi_th, _, (lo_tm, hi_tm) = two_pi_array.(pp) in
768 let lo_ge0_th = two_pi_low.(pp) in
769 let z_lo_th = float_mul_lo pp z_tm lo_tm in
770 let z_hi_th = float_mul_hi pp z_tm hi_tm in
771 let m_tm = rand (rator (concl z_lo_th)) in
772 let n_tm = rand (concl z_hi_th) in
773 let a_add_m_th = float_add_lo pp a_tm m_tm in
774 let b_add_n_th = float_add_hi pp b_tm n_tm in
775 let a1_tm = rand (rator (concl a_add_m_th)) in
776 let b1_tm = rand (concl b_add_n_th) in
777 let th0 = INST[z_tm, z_var_real; i_num_tm, i_var_num;
778 lo_tm, lo_var_real; hi_tm, hi_var_real;
779 a_tm, a_var_real; a1_tm, a1_var_real;
780 b_tm, b_var_real; b1_tm, b1_var_real;
781 m_tm, m_var_real; n_tm, n_var_real] reduction_pos_th in
782 let th1 = itlist MY_PROVE_HYP [i_eq_th; two_pi_th; lo_ge0_th;
783 z_hi_th; z_lo_th; a_add_m_th; b_add_n_th] th0 in
784 a1_tm, b1_tm, th1;;
785
786 let correct_neg_pi pp red_th a_tm b_tm a1_tm b1_tm =
787 let two_pi_th, _, (lo_tm, hi_tm) = two_pi_array.(pp) in
788 let a1_add_lo_th = float_add_lo pp a1_tm lo_tm and
789 b1_add_hi_th = float_add_hi pp b1_tm hi_tm in
790 let a2_tm = rand (rator (concl a1_add_lo_th)) and
791 b2_tm = rand (concl b1_add_hi_th) in
792 let th0 = INST[a1_tm, a1_var_real; b1_tm, b1_var_real;
793 a_tm, a_var_real; b_tm, b_var_real;
794 a2_tm, a2_var_real; b2_tm, b2_var_real;
795 lo_tm, lo_var_real; hi_tm, hi_var_real] a_neg_pi_correction in
796 let th1 = itlist MY_PROVE_HYP [red_th; two_pi_th; a1_add_lo_th; b1_add_hi_th] th0 in
797 a2_tm, b2_tm, th1;;
798
799 let correct_pi pp a1_tm neg_pi_le_a1_th =
800 let pi_th = pi_approx_array.(pp) in
801 let flag, le_th = float_prove_le_interval a1_tm pi_th in
802 if flag then
803 [], a1_tm, neg_pi_le_a1_th, le_th
804 else
805 let bounds = rand (concl pi_th) in
806 let ltm, hi_tm = dest_comb bounds in
807 let lo_tm = rand ltm in
808 let inst = INST[lo_tm, lo_var_real; hi_tm, hi_var_real; a1_tm, a1_var_real] in
809 let prove_hyp = MY_PROVE_HYP pi_th o MY_PROVE_HYP le_th in
810 let th1 = prove_hyp (inst a_pi_correction1) and
811 th2 = prove_hyp (inst a_pi_correction2) in
812 [th1], lo_tm, neg_pi_le_pi_lo_array.(pp), th2;;
813
814 (* TODO: use ab_sum1_correction to increase b and another result (similar to ab_sum3_correction)
815 to decrease a *)
816 let correct_ab_sum pp a1_tm b1_tm =
817 let two_pi_th, sub_th, (lo_tm, hi_tm) = two_pi_array.(pp) in
818 let a1_add_b1_hi = float_add_hi pp a1_tm b1_tm in
819 let n_tm = rand (concl a1_add_b1_hi) in
820 let flag, n_le_th = float_prove_le n_tm lo_tm in
821 if flag then
822 (* a1 + b1 <= 2 pi *)
823 let th0 = INST[lo_tm, lo_var_real; hi_tm, hi_var_real; n_tm, n_var_real;
824 a1_tm, a1_var_real; b1_tm, b1_var_real] ab_sum2_correction in
825 let th1 = itlist MY_PROVE_HYP [two_pi_th; a1_add_b1_hi; n_le_th] th0 in
826 -1, [], b1_tm, th1
827 else
828 let a1_add_b1_lo = float_add_lo pp a1_tm b1_tm in
829 let m_tm = rand (rator (concl a1_add_b1_lo)) in
830 let flag, m_ge_th = float_prove_le hi_tm m_tm in
831 if flag then
832 (* a1 + b1 >= 2 pi *)
833 let th0 = INST[lo_tm, lo_var_real; hi_tm, hi_var_real; m_tm, m_var_real;
834 a1_tm, a1_var_real; b1_tm, b1_var_real] ab_sum4_correction in
835 let th1 = itlist MY_PROVE_HYP [two_pi_th; a1_add_b1_lo; m_ge_th] th0 in
836 1, [], b1_tm, th1
837 else
838 (* Find b2 >= b1 such that a1 + b2 >= 2 pi *)
839 let t_tm = rand (concl sub_th) in
840 let n_sub_m = float_sub_hi pp n_tm m_tm in
841 let r_tm = rand (concl n_sub_m) in
842 let r_add_t = float_add_hi pp r_tm t_tm in
843 let z_tm = rand (concl r_add_t) in
844 let b1_add_z = float_add_hi pp b1_tm z_tm in
845 let b2_tm = rand (concl b1_add_z) in
846 let th0 = INST[lo_tm, lo_var_real; hi_tm, hi_var_real;
847 n_tm, n_var_real; m_tm, m_var_real;
848 a1_tm, a1_var_real; b1_tm, b1_var_real;
849 t_tm, t_var_real; r_tm, r_var_real;
850 z_tm, z_var_real; b2_tm, b2_var_real] ab_sum3_correction in
851 let th1 = itlist MY_PROVE_HYP [two_pi_th; a1_add_b1_hi; a1_add_b1_lo;
852 sub_th; n_sub_m; r_add_t; b1_add_z; n_le_th] th0 in
853 let b1_le_b2, ge_2pi = pair_of_list (CONJUNCTS th1) in
854 1, [b1_le_b2], b2_tm, ge_2pi;;
855
856
857 let correct_ab =
858 let le_refl = SPEC_ALL REAL_LE_REFL in
859 fun red_th a_tm b_tm a1_tm b1_tm a1_le0 b1_le0 ->
860 if a1_le0 = [] && b1_le0 = [] then red_th else
861 let a2_le_a1 =
862 match a1_le0 with
863 | le_th :: _ -> le_th
864 | [] -> INST[a1_tm, x_var_real] le_refl in
865 let b1_le_b2 =
866 match b1_le0 with
867 | le_th :: _ -> le_th
868 | [] -> INST[b1_tm, x_var_real] le_refl in
869 let a2_tm, a1_tm = dest_binary "real_le" (concl a2_le_a1) and
870 b1_tm, b2_tm = dest_binary "real_le" (concl b1_le_b2) in
871 let th0 = INST[a1_tm, a1_var_real; b1_tm, b1_var_real;
872 a2_tm, a2_var_real; b2_tm, b2_var_real;
873 a_tm, a_var_real; b_tm, b_var_real] a_b_correction in
874 itlist MY_PROVE_HYP [red_th; a2_le_a1; b1_le_b2] th0;;
875
876 (* ----------------------------------- *)
877 (* The main evaluation function *)
878 (* ----------------------------------- *)
879
880 let float_interval_cos pp x_th =
881 let x_tm, bounds = dest_interval_arith (concl x_th) in
882 let ltm, b_tm = dest_comb bounds in
883 let a_tm = rand ltm in
884 try
885 (* Reduce the interval of x *)
886 let i = get_i a_tm in
887 let a1_tm, b1_tm, red_th =
888 if i = 0 then reduction_zero a_tm b_tm
889 else if i < 0 then reduction_neg pp i a_tm b_tm
890 else reduction_pos pp i a_tm b_tm in
891
892 (* Prove -pi <= a1; otherwise correct the value of a1 *)
893 let neg_pi_th = neg_pi_array.(pp) in
894 let flag, le_th = float_prove_ge_interval a1_tm neg_pi_th in
895 let a1_tm, b1_tm, red_th, neg_pi_le_a1_th =
896 if flag then
897 a1_tm, b1_tm, red_th, le_th
898 else
899 let a1_tm', b1_tm', red_th' = correct_neg_pi pp red_th a_tm b_tm a1_tm b1_tm in
900 let flag, le_th = float_prove_ge_interval a1_tm' neg_pi_th in
901 let _ = if not flag then raise Correction_failed in
902 a1_tm', b1_tm', red_th', le_th in
903
904 (* Make sure that a1 <= pi *)
905 let a1_le0, a1_tm, neg_pi_le_a1_th, a1_le_pi_th = correct_pi pp a1_tm neg_pi_le_a1_th in
906 let a1_0_th = float_ge0 a1_tm in
907 let a1_case, a1_case_th =
908 let inst = MY_PROVE_HYP a1_0_th o INST[a1_tm, a1_var_real] in
909 if (fst o dest_const o rand o concl) a1_0_th = "T" then
910 1, MY_PROVE_HYP a1_le_pi_th (inst a1_case1)
911 else
912 -1, MY_PROVE_HYP neg_pi_le_a1_th (inst a1_case2) in
913
914 (* Consider cases: 1) a1 in [0, pi], 2) a1 in [-pi, 0] *)
915 if a1_case > 0 then
916 (* a1 in [0, pi] *)
917 let ab_sum_case, b1_le0, b1_tm, ab_sum_th = correct_ab_sum pp a1_tm b1_tm in
918 let red_th = correct_ab red_th a_tm b_tm a1_tm b1_tm a1_le0 b1_le0 in
919 let inst_tms = [x_tm, x_var_real; a_tm, a_var_real; b_tm, b_var_real;
920 a1_tm, a1_var_real; b1_tm, b1_var_real] in
921 let flag_b_pi, b_le_pi = float_prove_le_interval b1_tm pi_approx_array.(pp) in
922 let two_pi_th, _, _ = two_pi_array.(pp) in
923 let flag_b_2pi, b_le_2pi = float_prove_le_interval b1_tm two_pi_th in
924 if flag_b_pi then
925 (* b1 in [0, pi] *)
926 let b1_ge0 = float_ge0 b1_tm in
927 let b1_int =
928 let th0 = INST[b1_tm, b1_var_real] b1_interval_0_pi_1 in
929 MY_PROVE_HYP b1_ge0 (MY_PROVE_HYP b_le_pi th0) in
930 let cos_b1 = eval_low_0_pi pp b1_int b1_tm and
931 cos_a1 = eval_high_0_pi pp a1_case_th a1_tm in
932 let low_tm = rand (rator (concl cos_b1)) and
933 high_tm = rand (concl cos_a1) in
934 let th0 = INST ([low_tm, low_var_real; high_tm, high_var_real] @ inst_tms) cos_case0 in
935 itlist MY_PROVE_HYP [red_th; a1_case_th; b_le_pi; x_th; cos_b1; cos_a1] th0
936 else if ab_sum_case < 0 then
937 (* a1 + b1 <= 2 * pi *)
938 let cos_a1 = eval_high_0_pi pp a1_case_th a1_tm in
939 let high_tm = rand (concl cos_a1) in
940 let th0 = INST ([high_tm, high_var_real] @ inst_tms) cos_case2 in
941 itlist MY_PROVE_HYP [red_th; a1_case_th; ab_sum_th; x_th; cos_a1] th0
942 else if ab_sum_case > 0 && flag_b_2pi then
943 (* a1 + b1 >= 2 * pi /\ b1 <= 2 * pi ==> b1 in [pi, 2 * pi] *)
944 let b1_int =
945 let th0 = INST[b1_tm, b1_var_real; a1_tm, a1_var_real] b1_interval_pi_2pi in
946 MY_PROVE_HYP ab_sum_th (MY_PROVE_HYP a1_le_pi_th (MY_PROVE_HYP b_le_2pi th0)) in
947 let cos_b1 = eval_high_pi_2pi pp b1_int b1_tm in
948 let high_tm = rand (concl cos_b1) in
949 let th0 = INST ([high_tm, high_var_real] @ inst_tms) cos_case3 in
950 itlist MY_PROVE_HYP [red_th; a1_case_th; b_le_2pi; ab_sum_th; x_th; cos_b1] th0
951 else
952 INST[x_tm, x_var_real] cos_case_neg1
953 else
954 (* a1 in [-pi, 0] *)
955 let inst_tms = [x_tm, x_var_real; a_tm, a_var_real; b_tm, b_var_real;
956 a1_tm, a1_var_real; b1_tm, b1_var_real] in
957 let b1_0 = float_le0 b1_tm in
958 if (fst o dest_const o rand o concl) b1_0 = "T" then
959 (* b1 <= 0 ==> b1 in [-pi, 0] *)
960 let b1_int =
961 let th0 = INST[b1_tm, b1_var_real; a1_tm, a1_var_real] b1_interval_neg_pi_0 in
962 let a1_le_b1 = float_le a1_tm b1_tm in
963 MY_PROVE_HYP a1_le_b1 (MY_PROVE_HYP b1_0 (MY_PROVE_HYP neg_pi_le_a1_th th0)) in
964 let cos_a1 = eval_low_neg_pi_0 pp a1_case_th a1_tm and
965 cos_b1 = eval_high_neg_pi_0 pp b1_int b1_tm in
966 let low_tm = rand (rator (concl cos_a1)) and
967 high_tm = rand (concl cos_b1) in
968 let th0 = INST ([low_tm, low_var_real; high_tm, high_var_real] @ inst_tms) cos_case4 in
969 itlist MY_PROVE_HYP [red_th; a1_case_th; b1_0; x_th; cos_a1; cos_b1] th0
970 else
971 (* b1 >= 0 *)
972 let neg_b_eq = float_neg b1_tm in
973 let b2_tm = rand (rator (concl neg_b_eq)) in
974 let ab_cmp = float_le a1_tm b2_tm in
975 if (fst o dest_const o rand o concl) ab_cmp = "T" then
976 (* a1 <= --b1 *)
977 let cos_a1 = eval_low_neg_pi_0 pp a1_case_th a1_tm in
978 let low_tm = rand (rator (concl cos_a1)) in
979 let th0 = INST ([b2_tm, b2_var_real; low_tm, low_var_real] @ inst_tms) cos_case6 in
980 itlist MY_PROVE_HYP [red_th; a1_case_th; neg_b_eq; ab_cmp; x_th; cos_a1] th0
981 else
982 (* a1 >= --b1 *)
983 let flag_b_pi, b_le_pi = float_prove_le_interval b1_tm pi_approx_array.(pp) in
984 if flag_b_pi then
985 (* b1 <= pi ==> b1 in [0, pi] *)
986 let b1_int =
987 let th0 = INST[b1_tm, b1_var_real; a1_tm, a1_var_real;
988 b2_tm, b2_var_real] b1_interval_0_pi_2 in
989 itlist MY_PROVE_HYP [b_le_pi; a1_0_th; neg_b_eq; ab_cmp] th0 in
990 let cos_b1 = eval_low_0_pi pp b1_int b1_tm in
991 let low_tm = rand (rator (concl cos_b1)) in
992 let th0 = INST ([b2_tm, b2_var_real; low_tm, low_var_real] @ inst_tms) cos_case7 in
993 itlist MY_PROVE_HYP [red_th; a1_case_th; neg_b_eq; ab_cmp; b_le_pi; x_th; cos_b1] th0
994 else
995 INST[x_tm, x_var_real] cos_case_neg1
996 with Correction_failed ->
997 let _ = warn true (Printf.sprintf "float_interval_cos: reduction failed (%s, %s)"
998 (string_of_term a_tm) (string_of_term b_tm)) in
999 INST[x_tm, x_var_real] cos_case_neg1;;
1000
1001
1002 end;;
1003
1004
1005 (*
1006 (************)
1007
1008 let x_int = float_interval_mul 10 (mk_float_interval_small_num 33) pi_approx_array.(10);;
1009 float_interval_cos 5 x_int;;
1010
1011 let y_int = (ASSUME o mk_interval `x:real` o mk_pair) (mk_float (-100) 0, mk_float (-99) 0);;
1012 float_interval_cos 5 y_int;;
1013
1014 test 100 (float_interval_cos 10) x_int;;
1015
1016 (************)
1017
1018 let a_tm = mk_float (int_of_float (-.(f_pi +. f_2_pi +. 0.0001) *. 10. ** 10.)) (-10);;
1019 let b_tm = mk_float (-9) 0;;
1020
1021 let a_tm = mk_float (int_of_float ((f_pi +. f_2_pi +. 0.0001) *. 10. ** 10.)) (-10);;
1022 let b_tm = mk_float 10 0;;
1023
1024 let a_tm = mk_float (int_of_float ((-.f_pi /. 2.0 +. f_2_pi +. 0.0001) *. 10. ** 10.)) (-10);;
1025 let b_tm = mk_float 10 0;;
1026
1027 let a_tm = mk_float (int_of_float ((-.f_pi) *. 10. ** 10.)) (-10);;
1028 let b_tm = a_tm;;
1029
1030 let a_tm = mk_float 1 0;;
1031 let b_tm = mk_float (int_of_float ((f_2_pi -. 1.0) *. 10. ** 10.)) (-10);;
1032
1033
1034 let pp = 5;;
1035
1036 reduce_cos 10 a_tm b_tm;;
1037 test 1000 (reduce_cos 10 a_tm) b_tm;;
1038
1039 (**************)
1040
1041 let x_tm = mk_float 62 (-1);;
1042 let a_th = (INST[x_tm, x_var_real] o ASSUME) `interval_arith x (&0, pi)`;;
1043 let b_th = (INST[x_tm, x_var_real] o ASSUME) `interval_arith x (--pi, &0)`;;
1044 let c_th = (INST[x_tm, x_var_real] o ASSUME) `interval_arith x (pi, &2 * pi)`;;
1045
1046 eval_low_pi_2pi 10 c_th x_tm;;
1047
1048 eval_high_0_pi 5 a_th x_tm;;
1049 eval_high_pi_2pi 5 c_th x_tm;;
1050
1051
1052
1053
1054
1055 float_cos_high_raw 2 x_tm;;
1056
1057 eval_low_0_pi 6 a_th x_tm;;
1058
1059 eval_high_neg_pi_0 4 b_th x_tm;;
1060 eval_low_neg_pi_0 3 b_th x_tm;;
1061
1062 eval_low_0_pi 3 a_th x_tm;;
1063 eval_high_0_pi 4 a_th x_tm;;
1064
1065 float_neg (mk_float 1 0);;
1066
1067
1068
1069 ;;
1070
1071
1072 (**************)
1073
1074
1075 let i = get_i a_tm;;
1076 let a1_tm, b1_tm, red_th =
1077 if i = 0 then reduction_zero a_tm b_tm
1078 else if i < 0 then reduction_neg pp i a_tm b_tm
1079 else reduction_pos pp i a_tm b_tm;;
1080 (* Prove -pi <= a1; otherwise correct the value of a1 *)
1081 let neg_pi_th = neg_pi_array.(pp);;
1082 let flag, le_th = float_prove_ge_interval a1_tm neg_pi_th;;
1083 let a1_tm, b1_tm, red_th, neg_pi_le_a1_th =
1084 if flag then
1085 a1_tm, b1_tm, red_th, le_th
1086 else
1087 let a1_tm', b1_tm', red_th' = correct_neg_pi pp red_th a_tm b_tm a1_tm b1_tm in
1088 let flag, le_th = float_prove_ge_interval a1_tm' neg_pi_th in
1089 let _ = if not flag then raise Correction_failed in
1090 a1_tm', b1_tm', red_th', le_th;;
1091
1092 let a1_le0, a1_tm, neg_pi_le_a1_th, a1_le_pi_th = correct_pi pp a1_tm neg_pi_le_a1_th;;
1093 let a1_0_th = float_ge0 a1_tm;;
1094 let a1_case, a1_case_th =
1095 let inst = MY_PROVE_HYP a1_0_th o INST[a1_tm, a1_var_real] in
1096 if (fst o dest_const o rand o concl) a1_0_th = "T" then
1097 1, MY_PROVE_HYP a1_le_pi_th (inst a1_case1)
1098 else
1099 -1, MY_PROVE_HYP neg_pi_le_a1_th (inst a1_case2);;
1100
1101 (* a1_case > 0 *)
1102 let ab_sum_case, b1_le0, b1_tm, ab_sum_th = correct_ab_sum pp a1_tm b1_tm;;
1103 let red_th = correct_ab red_th a_tm b_tm a1_tm b1_tm a1_le0 b1_le0 in
1104 let inst_tms = INST[a_tm, a_var_real; b_tm, b_var_real; a1_tm, a1_var_real; b1_tm, b1_var_real] in
1105 let flag_b_pi, b_le_pi = float_prove_le_interval b1_tm pi_approx_array.(pp) in
1106 let flag_b_2pi, b_le_2pi = float_prove_le_interval b1_tm two_pi_th in
1107 if flag_b_pi then
1108 let th0 = inst_tms cos_case0 in
1109 0, itlist MY_PROVE_HYP [red_th; a1_case_th; b_le_pi] th0
1110 else if ab_sum_case < 0 then
1111 let th0 = inst_tms cos_case2 in
1112 2, itlist MY_PROVE_HYP [red_th; a1_case_th; ab_sum_th] th0
1113 else if ab_sum_case > 0 && flag_b_2pi then
1114 let th0 = inst_tms cos_case3 in
1115 3, itlist MY_PROVE_HYP [red_th; a1_case_th; b_le_2pi; ab_sum_th] th0
1116 else
1117 -1, cos_case_neg1;;
1118
1119 (* a1_case < 0 *)
1120 else
1121 let inst_tms = [a_tm, a_var_real; b_tm, b_var_real; a1_tm, a1_var_real; b1_tm, b1_var_real] in
1122 let b1_0 = float_le0 b1_tm in
1123 if (fst o dest_const o rand o concl) b1_0 = "T" then
1124 let th0 = INST inst_tms cos_case4 in
1125 4, itlist MY_PROVE_HYP [red_th; a1_case_th; b1_0] th0
1126 else
1127 let neg_b_eq = float_neg b1_tm in
1128 let neg_b_tm = rand (rator (concl neg_b_eq)) in
1129 let ab_cmp = float_le a1_tm neg_b_tm in
1130 if (fst o dest_const o rand o concl) ab_cmp = "T" then
1131 let th0 = INST ((neg_b_tm, b2_var_real) :: inst_tms) cos_case6 in
1132 6, itlist MY_PROVE_HYP [red_th; a1_case_th; neg_b_eq; ab_cmp] th0
1133 else
1134 let flag_b_pi, b_le_pi = float_prove_le_interval b1_tm pi_approx_array.(pp) in
1135 if flag_b_pi then
1136 let th0 = INST ((neg_b_tm, b2_var_real) :: inst_tms) cos_case7 in
1137 7, itlist MY_PROVE_HYP [red_th; a1_case_th; neg_b_eq; ab_cmp; b_le_pi] th0
1138 else
1139 -1, cos_case_neg1
1140 with Correction_failed ->
1141 let _ = warn true (Printf.sprintf "cos_reduction: reduction failed (%s, %s)"
1142 (string_of_term a_tm) (string_of_term b_tm)) in
1143 -1, cos_case_neg1;;
1144
1145
1146
1147
1148 let two_pi_th, sub_th, (lo_tm, hi_tm) = two_pi_array.(pp);;
1149 let a1_add_b1_hi = float_add_hi pp a1_tm b1_tm;;
1150 let n_tm = rand (concl a1_add_b1_hi);;
1151 let flag, n_le_th = float_prove_le n_tm lo_tm;;
1152 if flag then
1153 let th0 = INST[lo_tm, lo_var_real; hi_tm, hi_var_real; n_tm, n_var_real;
1154 a1_tm, a1_var_real; b1_tm, b1_var_real] ab_sum2_correction in
1155 let th1 = itlist MY_PROVE_HYP [two_pi_th; a1_add_b1_hi; n_le_th] th0 in
1156 -1, [], b1_tm, th1
1157 else
1158 let t_tm = rand (concl sub_th) in
1159 let a1_add_b1_lo = float_add_lo pp a1_tm b1_tm in
1160 let m_tm = rand (rator (concl a1_add_b1_lo)) in
1161 let n_sub_m = float_sub_hi pp n_tm m_tm in
1162 let r_tm = rand (concl n_sub_m) in
1163 let r_add_t = float_add_hi pp r_tm t_tm in
1164 let z_tm = rand (concl r_add_t) in
1165 let b1_add_z = float_add_hi pp b1_tm z_tm in
1166 let b2_tm = rand (concl b1_add_z) in
1167 let th0 = INST[lo_tm, lo_var_real; hi_tm, hi_var_real;
1168 n_tm, n_var_real; m_tm, m_var_real;
1169 a1_tm, a1_var_real; b1_tm, b1_var_real;
1170 t_tm, t_var_real; r_tm, r_var_real;
1171 z_tm, z_var_real; b2_tm, b2_var_real] ab_sum3_correction in
1172 let th1 = itlist MY_PROVE_HYP [two_pi_th; a1_add_b1_hi; a1_add_b1_lo;
1173 sub_th; n_sub_m; r_add_t; b1_add_z; n_le_th] th0 in
1174 let b1_le_b2, ge_2pi = pair_of_list (CONJUNCTS th1) in
1175 1, [b1_le_b2], b2_tm, ge_2pi;;
1176
1177
1178 let reduce_cos pp a_tm b_tm =
1179 try
1180 let i = get_i a_tm in
1181 let a1_tm, b1_tm, red_th =
1182 if i = 0 then reduction_zero a_tm b_tm
1183 else if i < 0 then reduction_neg pp i a_tm b_tm
1184 else reduction_pos pp i a_tm b_tm in
1185 (* Prove -pi <= a1; otherwise correct the value of a1 *)
1186 let neg_pi_th = neg_pi_array.(pp) in
1187 let flag, le_th = float_prove_ge_interval a1_tm neg_pi_th in
1188 let a1_tm, b1_tm, red_th, neg_pi_le_a1_th =
1189 if flag then
1190 a1_tm, b1_tm, red_th, le_th
1191 else
1192 let a1_tm', b1_tm', red_th' = correct_neg_pi pp red_th a_tm b_tm a1_tm b1_tm in
1193 let flag, le_th = float_prove_ge_interval a1_tm' neg_pi_th in
1194 let _ = if not flag then raise Correction_failed in
1195 a1_tm', b1_tm', red_th', le_th in
1196
1197 let a1_le0, a1_tm, neg_pi_le_a1_th, a1_le_pi_th = correct_pi pp a1_tm neg_pi_le_a1_th in
1198 let a1_0_th = float_ge0 a1_tm in
1199 let a1_case, a1_case_th =
1200 let inst = MY_PROVE_HYP a1_0_th o INST[a1_tm, a1_var_real] in
1201 if (fst o dest_const o rand o concl) a1_0_th = "T" then
1202 1, MY_PROVE_HYP a1_le_pi_th (inst a1_case1)
1203 else
1204 -1, MY_PROVE_HYP neg_pi_le_a1_th (inst a1_case2) in
1205
1206 if a1_case > 0 then
1207 let ab_sum_case, b1_le0, b1_tm, ab_sum_th = correct_ab_sum pp a1_tm b1_tm in
1208 let red_th = correct_ab red_th a_tm b_tm a1_tm b1_tm a1_le0 b1_le0 in
1209 let inst_tms = INST[a_tm, a_var_real; b_tm, b_var_real; a1_tm, a1_var_real; b1_tm, b1_var_real] in
1210 let flag_b_pi, b_le_pi = float_prove_le_interval b1_tm pi_approx_array.(pp) in
1211 let two_pi_th, _, _ = two_pi_array.(pp) in
1212 let flag_b_2pi, b_le_2pi = float_prove_le_interval b1_tm two_pi_th in
1213 if flag_b_pi then
1214 let th0 = inst_tms cos_case0 in
1215 0, itlist MY_PROVE_HYP [red_th; a1_case_th; b_le_pi] th0
1216 else if ab_sum_case < 0 then
1217 let th0 = inst_tms cos_case2 in
1218 2, itlist MY_PROVE_HYP [red_th; a1_case_th; ab_sum_th] th0
1219 else if ab_sum_case > 0 && flag_b_2pi then
1220 let th0 = inst_tms cos_case3 in
1221 3, itlist MY_PROVE_HYP [red_th; a1_case_th; b_le_2pi; ab_sum_th] th0
1222 else
1223 -1, cos_case_neg1
1224 else
1225 let inst_tms = [a_tm, a_var_real; b_tm, b_var_real; a1_tm, a1_var_real; b1_tm, b1_var_real] in
1226 let b1_0 = float_le0 b1_tm in
1227 if (fst o dest_const o rand o concl) b1_0 = "T" then
1228 let th0 = INST inst_tms cos_case4 in
1229 4, itlist MY_PROVE_HYP [red_th; a1_case_th; b1_0] th0
1230 else
1231 let neg_b_eq = float_neg b1_tm in
1232 let neg_b_tm = rand (rator (concl neg_b_eq)) in
1233 let ab_cmp = float_le a1_tm neg_b_tm in
1234 if (fst o dest_const o rand o concl) ab_cmp = "T" then
1235 let th0 = INST ((neg_b_tm, b2_var_real) :: inst_tms) cos_case6 in
1236 6, itlist MY_PROVE_HYP [red_th; a1_case_th; neg_b_eq; ab_cmp] th0
1237 else
1238 let flag_b_pi, b_le_pi = float_prove_le_interval b1_tm pi_approx_array.(pp) in
1239 if flag_b_pi then
1240 let th0 = INST ((neg_b_tm, b2_var_real) :: inst_tms) cos_case7 in
1241 7, itlist MY_PROVE_HYP [red_th; a1_case_th; neg_b_eq; ab_cmp; b_le_pi] th0
1242 else
1243 -1, cos_case_neg1
1244 with Correction_failed ->
1245 let _ = warn true (Printf.sprintf "cos_reduction: reduction failed (%s, %s)"
1246 (string_of_term a_tm) (string_of_term b_tm)) in
1247 -1, cos_case_neg1;;
1248
1249
1250 *)