Eval.v 51.6 KB
Newer Older
1 2 3 4
(**
This file is part of the Coq.Interval library for proving bounds of
real-valued expressions in Coq: http://coq-interval.gforge.inria.fr/

5
Copyright (C) 2007-2016, Inria
6 7 8 9 10 11 12 13 14 15 16 17 18 19

This library is governed by the CeCILL-C license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/or redistribute the library under the terms of the CeCILL-C
license as circulated by CEA, CNRS and Inria at the following URL:
http://www.cecill.info/

As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided
only with a limited warranty and the library's author, the holder of
the economic rights, and the successive licensors have only limited
liability. See the COPYING file for more details.
*)

20 21 22 23
From Coq Require Import Bool Reals List.
From Coquelicot Require Import Coquelicot.
From mathcomp.ssreflect Require Import ssreflect.

24
Require Import Stdlib.
25
Require Import Coquelicot.
26 27 28 29 30
Require Import Xreal.
Require Import Xreal_derive.
Require Import Basic.
Require Import Interval.
Require Import Taylor_model.
31
Require Import Tree.
32
Require Import Prog.
33

34
Definition no_floor_op op :=
35
  match op with Nearbyint _ => false | _ => true end.
36

37
Definition no_floor_term term :=
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
  match term with Unary op _ => no_floor_op op | _ => true end.

Definition no_floor_prog prog :=
  fold_left (fun r t => r && no_floor_term t) prog true.

Lemma no_floor_prog_cons t prog :
  no_floor_prog (t :: prog) = no_floor_term t && no_floor_prog prog.
Proof.
unfold no_floor_prog.
generalize true.
revert t; elim prog; simpl.
  now intros t b; case b; case no_floor_term.
intros a l IH t b.
now rewrite !IH; case no_floor_term; simpl; case no_floor_term; simpl.
Qed.

Lemma no_floor_prog_rcons t prog :
  no_floor_prog (prog ++ (t :: nil)) = no_floor_term t && no_floor_prog prog.
Proof.
unfold no_floor_prog.
generalize true.
revert t; elim prog; simpl.
  now intros t b; case b; case no_floor_term.
intros a l IH t b.
now rewrite !IH; case no_floor_term; simpl; case no_floor_term; simpl.
Qed.

Lemma no_floor_prog_rev prog :
  no_floor_prog (rev prog) = no_floor_prog prog.
Proof.
elim prog; simpl; try easy.
intros a l IH.
now rewrite no_floor_prog_rcons IH no_floor_prog_cons.
Qed.

73
Definition ext_operations :=
Guillaume Melquiond's avatar
Guillaume Melquiond committed
74
  Build_operations (fun x => Xreal (IZR x))
75 76 77 78 79 80 81 82 83 84 85 86
   (fun o =>
    match o with
    | Neg => Xneg
    | Abs => Xabs
    | Inv => Xinv
    | Sqr => Xsqr
    | Sqrt => Xsqrt
    | Cos => Xcos
    | Sin => Xsin
    | Tan => Xtan
    | Atan => Xatan
    | Exp => Xexp
87
    | Ln => Xln
88
    | PowerInt n => fun x => Xpower_int x n
89
    | Nearbyint m => Xnearbyint m
90 91 92 93 94 95 96 97 98 99 100
    end)
   (fun o =>
    match o with
    | Add => Xadd
    | Sub => Xsub
    | Mul => Xmul
    | Div => Xdiv
    end)
   (fun x => Xcmp x (Xreal 0)).

Definition eval_ext :=
101
  eval_generic Xnan ext_operations.
102 103

Theorem eval_inductive_prop_fun :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
104
  forall {T} A B P defA defB (opsA : operations A) (opsB : operations B),
105
 (forall a1 a2, (forall x, a1 x = a2 x) -> forall b, P a1 b -> P a2 b) ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
106
  P (fun _ : T => defA) defB ->
107 108 109 110 111 112
 (forall o a b, P a b -> P (fun x => unary opsA o (a x)) (unary opsB o b)) ->
 (forall o a1 a2 b1 b2, P a1 b1 -> P a2 b2 -> P (fun x => binary opsA o (a1 x) (a2 x)) (binary opsB o b1 b2)) ->
  forall inpA inpB,
 (forall n, P (fun x => nth n (inpA x) defA) (nth n inpB defB)) ->
  forall prog,
  forall n, P (fun x => nth n (eval_generic defA opsA prog (inpA x)) defA) (nth n (eval_generic defB opsB prog inpB) defB).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
113 114
Proof.
intros T A B P defA defB opsA opsB HP Hdef Hun Hbin inpA inpB Hinp prog n.
115 116 117 118 119 120 121 122
apply HP with (fun x => nth n (fold_right (fun y x => eval_generic_body defA opsA x y) (inpA x) (rev prog)) defA).
intros x.
now rewrite rev_formula.
rewrite rev_formula.
generalize n. clear n.
induction (rev prog).
exact Hinp.
intros [|n].
123 124 125
2: apply IHl.
destruct a as [n|o n|o n1 n2] ;
  [ idtac | apply Hun | apply Hbin ] ; apply IHl.
126 127
Qed.

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
Theorem eval_inductive_prop_floor_fun :
  forall {T} A B P defA defB (opsA : operations A) (opsB : operations B),
 (forall a1 a2, (forall x, a1 x = a2 x) -> forall b, P a1 b -> P a2 b) ->
  P (fun _ : T => defA) defB ->
 (forall o a b, no_floor_op o = true -> P a b -> P (fun x => unary opsA o (a x)) (unary opsB o b)) ->
 (forall o a1 a2 b1 b2, P a1 b1 -> P a2 b2 -> P (fun x => binary opsA o (a1 x) (a2 x)) (binary opsB o b1 b2)) ->
  forall inpA inpB,
 (forall n, P (fun x => nth n (inpA x) defA) (nth n inpB defB)) ->
  forall prog, no_floor_prog prog = true ->
  forall n, P (fun x => nth n (eval_generic defA opsA prog (inpA x)) defA) (nth n (eval_generic defB opsB prog inpB) defB).
Proof.
intros T A B P defA defB opsA opsB HP Hdef Hun Hbin inpA inpB Hinp prog Hprog n.
apply HP with (fun x => nth n (fold_right (fun y x => eval_generic_body defA opsA x y) (inpA x) (rev prog)) defA).
intros x.
now rewrite rev_formula.
rewrite rev_formula.
generalize n. clear n.
revert Hprog; rewrite -no_floor_prog_rev.
induction (rev prog).
intros _.
exact Hinp.
rewrite no_floor_prog_cons.
intros H1.
assert (H2 : no_floor_term a = true).
  now revert H1; case no_floor_term.
assert (H3 : no_floor_prog l = true).
  now revert H1; case no_floor_term; try discriminate.
intros [|n].
2: now apply IHl.
now destruct a as [n|o n|o n1 n2] ;
  [ idtac | apply Hun | apply Hbin ] ; try apply IHl.
Qed.

161 162 163
Lemma rewrite_inv_diff :
  forall u u',
  Xmul u' (Xneg (Xsqr (Xinv u))) = Xneg (Xdiv u' (Xsqr u)).
164
Proof.
165 166 167 168 169
intros.
rewrite Xmul_Xneg_distr_r.
apply f_equal.
rewrite Xdiv_split.
apply f_equal.
170 171
assert (forall x, Xsqr x = Xmul x x) by now intros [|x].
rewrite 2!H.
172 173 174 175 176 177 178
apply sym_eq.
apply Xinv_Xmul_distr.
Qed.

Lemma rewrite_div_diff :
  forall u v u' v',
  Xdiv (Xsub u' (Xmul v' (Xdiv u v))) v = Xdiv (Xsub (Xmul u' v) (Xmul v' u)) (Xmul v v).
179
Proof.
180 181 182 183 184
intros.
repeat rewrite Xdiv_split.
rewrite Xinv_Xmul_distr.
repeat rewrite <- Xmul_assoc.
apply (f_equal (fun x => Xmul x (Xinv v))).
185 186 187 188 189 190
rewrite 2!Xsub_split.
rewrite Xadd_comm.
set (w := Xmul v' u).
rewrite Xmul_Xadd_distr_r.
rewrite Xmul_assoc Xmul_Xinv.
destruct (Xinv v) as [|z].
191
by rewrite /= 2!Xlift2_nan_r.
192 193
rewrite /= Xmul_1_r Xmul_Xneg_distr_l.
apply Xadd_comm.
194 195 196 197 198 199 200
Qed.

Lemma xreal_to_real :
  forall (P1 : ExtendedR -> Prop) (P2 : R -> Prop),
  (P1 Xnan -> forall r, P2 r) ->
  (forall r, P1 (Xreal r) -> P2 r) ->
  forall prog terms n,
201
  P1 (nth n (eval_ext prog (map Xreal terms)) Xnan) ->
202 203 204 205 206
  P2 (nth n (eval_real prog terms) 0%R).
Proof.
intros P1 P2 HP1 HP2 prog terms n.
unfold eval_ext, eval_real.
refine (_ (eval_inductive_prop _ _ (fun a b => match a with Xreal a => a = b | _ => True end)
207 208
  Xnan R0 ext_operations real_operations _ _ _ (map Xreal terms) terms _ prog n)).
case (nth n (eval_generic Xnan ext_operations prog (map Xreal terms)) Xnan).
209 210 211 212 213
intros _ H.
now apply HP1.
intros y H.
rewrite H.
apply HP2.
214
easy.
215 216 217 218 219
(* unary *)
destruct a as [|a].
now destruct o.
intros b H.
rewrite H.
220
destruct o ; try easy ; simpl ; unfold Xinv'.
221
now case (is_zero b).
222
unfold Xsqrt'.
223
now case (is_negative b).
224
unfold Xtan'.
225
now case (is_zero (cos b)).
226
unfold Xln'.
227
now case (is_positive b).
228
generalize (Xpower_int_correct n0 (Xreal b)).
229 230
simpl.
now case Xpower_int'.
231 232 233 234 235 236
(* binary *)
destruct a1 as [|a1].
now destruct o.
destruct a2 as [|a2].
now destruct o.
intros b1 b2 H1 H2.
237
rewrite H1 H2.
238
destruct o ; try easy ; simpl ; unfold Xdiv'.
239 240
now destruct (is_zero b2).
(* . *)
241 242 243 244
intros k.
destruct (le_or_lt (length (map Xreal terms)) k) as [H|H].
now rewrite nth_overflow.
rewrite (nth_indep _ _ (Xreal 0) H).
245 246 247
now rewrite map_nth.
Qed.

248 249
Lemma continuous_unary :
  forall unop a b x,
250
  no_floor_op unop = true ->
251 252 253 254 255
  (notXnan b -> b = Xreal (a x) /\ continuous a x) ->
  notXnan (unary ext_operations unop b) ->
  unary ext_operations unop b = Xreal (unary real_operations unop (a x)) /\
  continuous (fun x0 : R => unary real_operations unop (a x0)) x.
Proof.
256
move => unop a b x NF Hb HbnotXnan.
257 258 259 260 261 262 263
case Hbnan: b Hb => [|b1] // Hb.
rewrite Hbnan /= in HbnotXnan.
by case unop in HbnotXnan.
case: Hb => // Hb Hcontax.
move: HbnotXnan.
rewrite Hbnan Hb => {Hbnan Hb b1} HnotXnan.
split.
264
(case: unop NF HnotXnan; try discriminate) => //= [_|_|_|_|].
265 266 267 268 269
- rewrite /Xinv'.
  by case is_zero.
- rewrite /Xsqrt'.
  by case is_negative.
- rewrite /Xtan'.
270
  by case is_zero.
271 272 273 274
- rewrite /Xln'.
  by case is_positive.
- case => [||p] //.
  rewrite /Xpower_int'.
275
  by case is_zero.
276
(case: unop NF HnotXnan; try discriminate) => //=
277
    [_|_|_|_|_|_|_|_|_|_|_|].
278 279 280 281 282
- move => _. by apply: continuous_opp.
- move => _. by apply: continuous_Rabs_comp.
- move => HnotXnan.
  apply: continuous_Rinv_comp => // Ha.
  move: HnotXnan.
283
  by rewrite /Xinv' Ha is_zero_0.
284 285
- move => _. by apply: continuous_mult.
- move => HnotXnan.
286
  exact: continuous_sqrt_comp.
287 288 289 290 291 292
- move => _. by apply: continuous_cos_comp.
- move => _. by apply: continuous_sin_comp.
- move => HnotXnan.
  apply: continuous_comp => //.
  apply: continuous_tan => Ha.
    move: HnotXnan.
293
    by rewrite /Xtan' Ha is_zero_0.
294 295 296 297 298
- move => _. by apply: continuous_atan_comp.
- move => _. by apply: continuous_exp_comp.
- move => HnotXnan.
  apply: continuous_comp => //.
  apply: continuous_ln.
299
  rewrite /Xln' in HnotXnan.
300 301 302
  by case: is_positive_spec HnotXnan.
- move => n.
  rewrite /powerRZ.
303
  case: n => [|n|n] _ HnotXnan.
304 305 306 307 308
  + exact: continuous_const.
  + apply: (continuous_comp a (fun x => pow x _)) => //.
    apply: ex_derive_continuous.
    apply: ex_derive_pow.
    exact: ex_derive_id.
309 310
  + rewrite /Xpower_int' in HnotXnan.
    case: is_zero_spec HnotXnan => // Ha _.
311 312 313 314 315 316 317 318 319
    apply: continuous_comp.
    apply: (continuous_comp a (fun x => pow x _)) => //.
    apply: ex_derive_continuous.
    apply: ex_derive_pow.
    exact: ex_derive_id.
    apply: continuous_Rinv.
    exact: pow_nonzero.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
320
Module IntervalAlgos (I : IntervalOps).
321

322 323 324 325 326
Record check := {
  check_f : I.type -> bool;
  check_p : ExtendedR -> Prop;
  _ : forall y yi, contains (I.convert yi) y -> check_f yi = true -> check_p y
}.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
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
Definition subset_check : I.type -> check.
Proof.
intros output.
apply (Build_check (fun i => I.subset i output) (contains (I.convert output))).
abstract (
  intros y yi Hy Hb ;
  assert (H := I.subset_correct yi output Hb) ;
  now apply subset_contains with (1 := H)).
Defined.

Definition positive_check : check.
Proof.
apply (Build_check
  (fun i => match I.sign_strict i with Xgt => true | _ => false end)
  (fun y => match y with Xreal r => (0 < r)%R | _ => False end)).
abstract (
  intros y yi Hy Hb ;
  generalize (I.sign_strict_correct yi) ;
  destruct (I.sign_strict yi) ; try easy ;
  intros H ;
  destruct (H y Hy) as (H1,H2) ;
  now rewrite H1).
Defined.

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
Definition nonzero_check : check.
Proof.
apply (Build_check
  (fun i => match I.sign_strict i with Xgt => true | Xlt => true | _ => false end)
  (fun y => match y with Xreal r => r <> R0 | _ => False end)).
abstract (
  intros y yi Hy Hb ;
  generalize (I.sign_strict_correct yi) ;
  destruct (I.sign_strict yi) ; try easy ;
  intros H ;
  destruct (H y Hy) as [H1 H2] ;
  rewrite H1 ;
  [ apply Rlt_not_eq | apply Rgt_not_eq ] ;
  assumption).
Defined.

368 369
Definition bisect_1d_step l u (check : I.type -> bool) cont :=
  if check (I.bnd l u) then true
Guillaume Melquiond's avatar
Guillaume Melquiond committed
370 371 372 373 374 375 376
  else
    let m := I.midpoint (I.bnd l u) in
    match cont l m with
    | true => cont m u
    | false => false
    end.

377
Fixpoint bisect_1d l u check steps { struct steps } :=
Guillaume Melquiond's avatar
Guillaume Melquiond committed
378 379 380
  match steps with
  | O => false
  | S n =>
381 382
    bisect_1d_step l u check
      (fun l u => bisect_1d l u check n)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
383 384 385
  end.

Theorem bisect_1d_correct :
386 387 388
  forall steps inpl inpu f P,
  (forall y yi, contains (I.convert yi) y -> f yi = true -> P y) ->
  bisect_1d inpl inpu f steps = true ->
Guillaume Melquiond's avatar
Guillaume Melquiond committed
389
  forall x,
390
  contains (I.convert (I.bnd inpl inpu)) x -> P x.
391
Proof.
392
intros steps inpl inpu f P Hf.
393
revert inpl inpu.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
394
induction steps.
395 396
intros inpl inpu Hb.
discriminate Hb.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
397 398 399
intros inpl inpu.
simpl.
unfold bisect_1d_step.
400
case_eq (f (I.bnd inpl inpu)).
401
intros Hb _ x Hx.
402
now apply Hf with (2 := Hb).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
403 404
intros _.
set (inpm := I.midpoint (I.bnd inpl inpu)).
405
case_eq (bisect_1d inpl inpm f steps) ; try easy.
406
intros Hl Hr x Hx.
407
apply (bisect P (I.convert_bound inpl) (I.convert_bound inpm) (I.convert_bound inpu)).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
408 409
unfold domain.
rewrite <- I.bnd_correct.
410
apply IHsteps with (1 := Hl).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
411 412
unfold domain.
rewrite <- I.bnd_correct.
413 414
apply IHsteps with (1 := Hr).
now rewrite <- I.bnd_correct.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
415 416
Qed.

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
Theorem bisect_1d_correct' :
  forall steps inpl inpu f P,
  (forall y yi, contains (I.convert yi) (Xreal y) -> f yi = true -> P y) ->
  bisect_1d inpl inpu f steps = true ->
  forall x,
  contains (I.convert (I.bnd inpl inpu)) (Xreal x) -> P x.
Proof.
intros steps inpl inpu f P Hf.
revert inpl inpu.
induction steps.
intros inpl inpu Hb.
discriminate Hb.
intros inpl inpu.
simpl.
unfold bisect_1d_step.
case_eq (f (I.bnd inpl inpu)).
intros Hb _ x Hx.
now apply Hf with (2 := Hb).
intros _.
set (inpm := I.midpoint (I.bnd inpl inpu)).
case_eq (bisect_1d inpl inpm f steps) ; try easy.
intros Hl Hr x Hx.
change x with (proj_val (Xreal x)).
apply (bisect' P (I.convert_bound inpl) (I.convert_bound inpm) (I.convert_bound inpu)).
unfold domain'.
rewrite <- I.bnd_correct.
apply IHsteps with (1 := Hl).
unfold domain'.
rewrite <- I.bnd_correct.
apply IHsteps with (1 := Hr).
now rewrite <- I.bnd_correct.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
450 451 452 453 454 455
Definition lookup_1d_step fi l u output cont :=
  if I.subset (fi (I.bnd l u)) output then output
  else
    let m := I.midpoint (I.bnd l u) in
    let output := cont l m output in
    if I.lower_bounded output || I.upper_bounded output then cont m u output
456
    else output.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
457 458 459 460 461 462 463 464 465 466

Fixpoint lookup_1d_main fi l u output steps { struct steps } :=
  match steps with
  | O => I.join (fi (I.bnd l u)) output
  | S n =>
    lookup_1d_step fi l u output
      (fun l u output => lookup_1d_main fi l u output n)
  end.

Definition lookup_1d fi l u extend steps :=
Guillaume Melquiond's avatar
Guillaume Melquiond committed
467
  let m := iter_nat (fun u => I.midpoint (I.bnd l u)) steps u in
Guillaume Melquiond's avatar
Guillaume Melquiond committed
468
  let output := extend (fi (I.bnd l m)) in
469 470 471 472 473 474 475
  match steps with
  | O => I.whole
  | S steps =>
    if I.lower_bounded output || I.upper_bounded output then
      lookup_1d_main fi l u output steps
    else output
  end.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
476

477 478
Inductive bound_proof :=
  | Bproof : forall x xi, contains (I.convert xi) (Xreal x) -> bound_proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
479

480 481 482
Definition real_from_bp v := match v with Bproof x _ _ => x end.
Definition xreal_from_bp v := match v with Bproof x _ _ => Xreal x end.
Definition interval_from_bp v := match v with Bproof _ xi _ => xi end.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
483

484 485 486
Lemma iterated_bnd_nth :
  forall bounds n,
  contains (I.convert (nth n (map interval_from_bp bounds) I.nai))
487
    (nth n (map xreal_from_bp bounds) Xnan).
488
Proof.
489 490
intros bounds n.
destruct (le_or_lt (length bounds) n) as [H|H].
491
rewrite -> 2!nth_overflow by now rewrite map_length.
492
now rewrite I.nai_correct.
493
rewrite -> (nth_indep _ Xnan (Xreal 0)) by now rewrite map_length.
494 495
assert (H0: contains (I.convert I.nai) (Xreal 0)) by now rewrite I.nai_correct.
pose (b := Bproof R0 I.nai H0).
496 497
change (Xreal 0) with (xreal_from_bp b).
change I.nai with (interval_from_bp b).
498
rewrite 2!map_nth.
499
now case (nth n bounds b).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
500 501
Qed.

502 503
Lemma continuous_eval_ext :
  forall prog bounds x m,
504
  no_floor_prog prog = true ->
505 506 507 508 509
  notXnan (nth m (eval_ext prog (Xreal x :: map xreal_from_bp bounds)) Xnan) ->
  continuous (fun x => nth m (eval_real prog (x :: map real_from_bp bounds)) R0) x.
Proof.
intros prog bounds x.
rewrite /eval_ext /eval_real.
510
intros m Hf H.
511
eapply proj2.
512 513
revert Hf m H.
apply: (eval_inductive_prop_floor_fun _ _ (fun (f : R -> R) v => notXnan v -> v = Xreal (f x) /\ continuous f x)) => //.
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
intros f1 f2 Heq b H Hb.
case: (H Hb) => {H} H H'.
split.
by rewrite -Heq.
now eapply continuous_ext.
move => unop a b Hb HnotXnan.
exact: continuous_unary.
(* case of binary operator *)
case => a1 a2 b1 b2 Ha1 Ha2 HnotXnan /=.
- move: HnotXnan Ha1 Ha2.
  case: b1 => [|b1] // ;case: b2 => [|b2] // .
  move => _ [] // -> Hconta1 [] // -> Hconta2.
  by split => //; apply: continuous_plus.
- move: HnotXnan Ha1 Ha2.
  case: b1 => [|b1] // ;case: b2 => [|b2] // .
  move => _ [] // -> Hconta1 [] // -> Hconta2.
  by split => //; apply: continuous_minus.
- move: HnotXnan Ha1 Ha2.
  case: b1 => [|b1] // ;case: b2 => [|b2] // .
  move => _ [] // -> Hconta1 [] // -> Hconta2.
  by split => //; apply: continuous_mult.
- move: HnotXnan Ha1 Ha2.
  case: b1 => [|b1] // ;case: b2 => [|b2] // .
  move => HnotXnan [] // Heq1 Hconta1 [] // Heq2 Hconta2.
  split => // .
  + move: HnotXnan.
540
    rewrite /= /Xdiv'.
541 542 543 544
    case: (is_zero b2) => // .
    by inversion Heq1; inversion Heq2.
  + apply: continuous_mult => // .
    apply: continuous_Rinv_comp => // Habs .
545
    by move: Heq2 HnotXnan => ->; rewrite /= /Xdiv' Habs is_zero_0.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
intros [|n].
simpl.
intros _.
apply (conj eq_refl).
apply continuous_id.
simpl.
destruct (le_or_lt (length bounds) n).
rewrite nth_overflow => //.
by rewrite map_length.
rewrite (nth_indep _ _ (Xreal 0)).
replace (map xreal_from_bp bounds) with (map Xreal (map real_from_bp bounds)).
rewrite map_nth.
intros _.
apply (conj eq_refl).
apply continuous_const.
rewrite map_map.
apply map_ext.
now intros [].
by rewrite map_length.
Qed.

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
Module BndValuator.

Definition operations prec :=
  Build_operations I.fromZ
   (fun o =>
    match o with
    | Neg => I.neg
    | Abs => I.abs
    | Inv => I.inv prec
    | Sqr => I.sqr prec
    | Sqrt => I.sqrt prec
    | Cos => I.cos prec
    | Sin => I.sin prec
    | Tan => I.tan prec
    | Atan => I.atan prec
    | Exp => I.exp prec
583
    | Ln => I.ln prec
584
    | PowerInt n => fun x => I.power_int prec x n
585
    | Nearbyint m => I.nearbyint m
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    end)
   (fun o =>
    match o with
    | Add => I.add prec
    | Sub => I.sub prec
    | Mul => I.mul prec
    | Div => I.div prec
    end)
    I.sign_strict.

Definition eval prec :=
  eval_generic I.nai (operations prec).

Lemma eval_correct_aux :
  forall prec prog terms bounds,
601
 (forall n, contains (I.convert (nth n bounds I.nai)) (nth n terms Xnan)) ->
602 603
  forall n,
  contains (I.convert (nth n (eval prec prog bounds) I.nai))
604 605
   (nth n (eval_ext prog terms) Xnan).
Proof.
606 607 608 609 610
intros prec prog terms bounds Hinp.
unfold eval, eval_ext.
apply (eval_inductive_prop _ _ (fun a b => contains (I.convert a) b)).
(* . *)
rewrite I.nai_correct.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
611
exact I.
612 613 614 615 616 617 618 619 620 621 622 623
(* unary *)
destruct o ; simpl ;
  [ apply I.neg_correct
  | apply I.abs_correct
  | apply I.inv_correct
  | apply I.sqr_correct
  | apply I.sqrt_correct
  | apply I.cos_correct
  | apply I.sin_correct
  | apply I.tan_correct
  | apply I.atan_correct
  | apply I.exp_correct
624
  | apply I.ln_correct
625 626
  | apply I.power_int_correct
  | apply I.nearbyint_correct ].
627 628 629 630 631 632 633 634
(* binary *)
destruct o ; simpl ;
  [ apply I.add_correct
  | apply I.sub_correct
  | apply I.mul_correct
  | apply I.div_correct ].
(* . *)
exact Hinp.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
635 636
Qed.

637 638 639 640
Theorem eval_correct :
  forall prec prog bounds n,
  contains
    (I.convert (nth n (eval prec prog (map interval_from_bp bounds)) I.nai))
641
    (nth n (eval_ext prog (map xreal_from_bp bounds)) Xnan).
642 643 644 645
Proof.
intros prec prog bounds.
apply eval_correct_aux.
apply iterated_bnd_nth.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
646 647
Qed.

648
Theorem eval_correct_ext :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
649
  forall prec prog bounds n,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
650
  I.extension
651
    (fun x => nth n (eval_ext prog (x :: map xreal_from_bp bounds)) Xnan)
652 653
    (fun b => nth n (eval prec prog (b :: map interval_from_bp bounds)) I.nai).
Proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
654
intros prec prog bounds n xi x Hx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
655
generalize n. clear n.
656
apply eval_correct_aux.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
657
intros [|n].
Guillaume Melquiond's avatar
Guillaume Melquiond committed
658
exact Hx.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
659
apply iterated_bnd_nth.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
660 661
Qed.

662 663
Lemma continuous_eval :
  forall prec prog bounds m i x,
664
  no_floor_prog prog = true ->
665
  contains (I.convert i) (Xreal x) ->
666
  I.convert (nth m (eval prec prog (i :: map interval_from_bp bounds)) I.nai) <> Inan ->
667 668
  continuous (fun x => nth m (eval_real prog (x :: map real_from_bp bounds)) R0) x.
Proof.
669 670
move => prec prog bounds m i x Hf Hcontains HnotInan.
apply: continuous_eval_ext => //.
671 672 673 674 675 676 677 678
generalize (eval_correct_ext prec prog bounds m i (Xreal x) Hcontains).
revert HnotInan.
case I.convert => //.
by case: (nth _ _ _).
Qed.

Lemma ex_RInt_eval :
  forall prec prog bounds m a b i,
679
  no_floor_prog prog = true ->
680
  (forall x, Rmin a b <= x <= Rmax a b -> contains (I.convert i) (Xreal x)) ->
681
  I.convert (nth m (eval prec prog (i :: map interval_from_bp bounds)) I.nai) <> Inan ->
682 683
  ex_RInt (fun x => nth m (eval_real prog (x :: map real_from_bp bounds)) R0) a b.
Proof.
684
move => prec prog bounds m a b i Hf Hcontains HnotInan.
685 686
apply: ex_RInt_continuous.
intros z Hz.
687
apply: continuous_eval HnotInan => //.
688 689 690
exact: Hcontains.
Qed.

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
End BndValuator.

Module DiffValuator.

Definition diff_operations A (ops : @operations A) :=
  Build_operations
   (fun x => (constant ops x, constant ops 0))
   (fun o x =>
    match x with
    | (v, d) =>
      match o with
      | Neg => let f := unary ops Neg in (f v, f d)
      | Abs => let w := unary ops Abs v in (w,
        match sign ops v with Xlt => unary ops Neg d | Xgt => d | _ => unary ops Inv (constant ops 0) end)
      | Inv => let w := unary ops Inv v in (w,
        binary ops Mul d (unary ops Neg (unary ops Sqr w)))
      | Sqr => let w := binary ops Mul d v in (unary ops Sqr v, binary ops Add w w)
      | Sqrt => let w := unary ops Sqrt v in (w,
        binary ops Div d (binary ops Add w w))
      | Cos => (unary ops Cos v,
        binary ops Mul d (unary ops Neg (unary ops Sin v)))
      | Sin => (unary ops Sin v,
        binary ops Mul d (unary ops Cos v))
      | Tan => let w := unary ops Tan v in (w,
        binary ops Mul d (binary ops Add (constant ops 1) (unary ops Sqr w)))
      | Atan => (unary ops Atan v,
        binary ops Div d (binary ops Add (constant ops 1) (unary ops Sqr v)))
      | Exp => let w := unary ops Exp v in (w,
        binary ops Mul d w)
720 721
      | Ln => (unary ops Ln v,
        match sign ops v with Xgt => binary ops Div d v | _ => unary ops Inv (constant ops 0) end)
722 723
      | PowerInt n =>
        (unary ops o v, binary ops Mul d (binary ops Mul (constant ops n) (unary ops (PowerInt (n-1)) v)))
724
      | Nearbyint m => let w := unary ops (Nearbyint m) v in (w, unary ops Inv (constant ops 0))
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
      end
    end)
   (fun o x y =>
    match x, y with
    | (vx, dx), (vy, dy) =>
      match o with
      | Add => let f := binary ops Add in (f vx vy, f dx dy)
      | Sub => let f := binary ops Sub in (f vx vy, f dx dy)
      | Mul => let f := binary ops Mul in (f vx vy,
        binary ops Add (f dx vy) (f dy vx))
      | Div => let f := binary ops Div in
        let w := f vx vy in
        (w, f (binary ops Sub dx (binary ops Mul dy w)) vy)
      end
    end)
   (fun x => match x with (vx, _) => sign ops vx end).

Guillaume Melquiond's avatar
Guillaume Melquiond committed
742 743 744 745 746 747
Lemma unary_diff_correct :
  forall o f d x,
  Xderive_pt f x d ->
  let v := unary (diff_operations _ ext_operations) o (f x, d) in
  unary ext_operations o (f x) = fst v /\
  Xderive_pt (fun x0 => unary ext_operations o (f x0)) x (snd v).
748
Proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
749 750 751
intros o f d x Hd.
destruct o ; simpl ; repeat split.
now apply Xderive_pt_neg.
752
rewrite /Xinv' is_zero_0.
753
now apply Xderive_pt_abs.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
754 755
rewrite rewrite_inv_diff.
now apply Xderive_pt_inv.
756 757 758 759 760
eapply Xderive_pt_eq_fun.
2: now apply Xderive_pt_mul.
intros y.
simpl.
now case f.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
761 762 763 764
now apply Xderive_pt_sqrt.
now apply Xderive_pt_cos.
now apply Xderive_pt_sin.
now apply Xderive_pt_tan.
765
now apply Xderive_pt_atan.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
766
now apply Xderive_pt_exp.
767
rewrite /Xinv' is_zero_0.
768
now apply Xderive_pt_ln.
769
now apply Xderive_pt_power_int.
770
rewrite /Xinv' is_zero_0.
771
now destruct x.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
772 773 774 775 776 777 778 779 780
Qed.

Lemma binary_diff_correct :
  forall o f1 f2 d1 d2 x,
  Xderive_pt f1 x d1 ->
  Xderive_pt f2 x d2 ->
  let v := binary (diff_operations _ ext_operations) o (f1 x, d1) (f2 x, d2) in
  binary ext_operations o (f1 x) (f2 x) = fst v /\
  Xderive_pt (fun x0 => binary ext_operations o (f1 x0) (f2 x0)) x (snd v).
781
Proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
782 783 784 785 786 787 788 789 790 791 792
intros o f1 f2 d1 d2 x Hd1 Hd2.
destruct o ; simpl ; repeat split.
now apply Xderive_pt_add.
now apply Xderive_pt_sub.
now apply Xderive_pt_mul.
rewrite rewrite_div_diff.
now apply Xderive_pt_div.
Qed.

Lemma eval_diff_correct :
  forall prog terms n x,
793 794 795
  let v := nth n (eval_generic (Xnan, Xnan) (diff_operations _ ext_operations) prog ((x, Xmask (Xreal 1) x) :: map (fun v => (Xreal v, Xmask (Xreal 0) x)) terms)) (Xnan, Xnan) in
  nth n (eval_ext prog (x :: map Xreal terms)) Xnan = fst v /\
  Xderive_pt (fun x => nth n (eval_ext prog (x :: map Xreal terms)) Xnan) x (snd v).
796
Proof.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
intros prog terms n x.
(*set (inpA x := x :: map Xreal terms).
set (inpB := (x, Xmask (Xreal 1) x) :: map (fun v : R => (Xreal v, Xmask (Xreal 0) x)) terms).*)
refine (eval_inductive_prop_fun _ _ (fun a b => a x = fst b /\ Xderive_pt a x (snd b)) _ _ _ _ _ _ _ _ _ _ _ _ _).
(* extensionality *)
intros a1 a2 Heq (bl, br).
simpl.
intros (Hl, Hr).
split.
now rewrite <- Heq.
apply Xderive_pt_eq_fun with (2 := Hr).
intros.
now apply sym_eq.
(* default *)
destruct x ; repeat split.
(* unary *)
intros o a (bl, br) (Hl, Hr).
simpl in Hl.
rewrite <- Hl.
now apply unary_diff_correct.
(* binary *)
intros o a1 a2 (bl1, br1) (bl2, br2) (Hl1, Hr1) (Hl2, Hr2).
simpl in Hl1, Hl2.
rewrite <- Hl1, <- Hl2.
now apply binary_diff_correct.
(* inputs *)
clear n.
intros [|n].
simpl.
repeat split.
apply Xderive_pt_identity.
simpl.
split.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
830
rewrite <- (map_nth (@fst ExtendedR ExtendedR)).
831
rewrite map_map.
832
apply (f_equal (fun v => nth n v _)).
833 834 835 836
now apply map_ext.
rewrite <- map_nth.
rewrite map_map.
simpl.
837
destruct (le_or_lt (length terms) n) as [H|H].
838
rewrite -> 2!nth_overflow by now rewrite map_length.
839
now case x.
840
rewrite -> (nth_indep (map (fun _ => Xmask (Xreal 0) x) terms) _ (Xmask (Xreal 0) x))
841 842 843
  by now rewrite map_length.
change (Xmask (Xreal 0) x) with ((fun _ => Xmask (Xreal 0) x) R0) at 2.
rewrite map_nth.
844
rewrite -> (nth_indep _ _ (Xreal 0)) by now rewrite map_length.
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
rewrite map_nth.
apply Xderive_pt_constant.
Qed.

Lemma unary_diff_bnd_correct :
  forall prec o f f',
  let u x := unary (diff_operations _ ext_operations) o (f x, f' x) in
  forall yi yi' xi,
 (forall x, contains xi x -> contains (I.convert yi) (f x)) ->
 (forall x, contains xi x -> contains (I.convert yi') (f' x)) ->
  let v := unary (diff_operations _ (BndValuator.operations prec)) o (yi, yi') in
 (forall x, contains xi x -> contains (I.convert (snd v)) (snd (u x))).
Proof.
intros prec o f f' u yi yi' xi Hf Hf' v x Hx.
destruct o ; simpl ;
  repeat first
  [ apply I.neg_correct
  | apply I.abs_correct
  | apply I.inv_correct
  | apply I.sqr_correct
  | apply I.sqrt_correct
  | apply I.cos_correct
  | apply I.sin_correct
  | apply I.tan_correct
  | apply I.atan_correct
  | apply I.exp_correct
871
  | apply I.ln_correct
872 873 874 875 876
  | apply I.power_int_correct
  | apply I.add_correct
  | apply I.mul_correct
  | apply I.div_correct
  | apply I.fromZ_correct
Guillaume Melquiond's avatar
Guillaume Melquiond committed
877 878
  | refine (I.add_correct _ _ _ (Xreal 1%R) _ _ _)
  | refine (I.mul_correct _ _ _ (Xreal (IZR _)) _ _ _) ] ;
879 880 881
  try now first [ apply Hf | apply Hf' ].
(* abs *)
generalize (I.inv_correct prec (I.fromZ 0) (Xreal 0) (I.fromZ_correct _)).
882
rewrite /= /Xinv' is_zero_0.
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
specialize (Hf _ Hx).
generalize (I.sign_strict_correct yi).
case I.sign_strict ; case (I.convert (I.inv prec (I.fromZ 0))) ; try easy.
intros H _.
specialize (H _ Hf).
rewrite (proj1 H).
simpl.
rewrite Rcompare_Lt.
apply I.neg_correct.
now apply Hf'.
apply H.
intros H _.
specialize (H _ Hf).
rewrite (proj1 H).
simpl.
rewrite Rcompare_Gt.
now apply Hf'.
apply H.
901 902
(* ln *)
generalize (I.inv_correct prec (I.fromZ 0) (Xreal 0) (I.fromZ_correct _)).
903
rewrite /= /Xinv' is_zero_0.
904 905 906 907 908
specialize (Hf _ Hx).
generalize (I.sign_strict_correct yi).
case I.sign_strict ; case (I.convert (I.inv prec (I.fromZ 0))) ; try easy.
intros H _.
specialize (H _ Hf).
909
rewrite {1}(proj1 H).
910 911 912 913 914 915
simpl.
rewrite Rcompare_Gt.
apply I.div_correct.
now apply Hf'.
exact Hf.
apply H.
916 917 918
(* nearbyint *)
apply (I.inv_correct _ _ (Xreal 0)).
apply I.fromZ_correct.
919 920 921 922 923 924 925 926 927 928 929 930
Qed.

Lemma binary_diff_bnd_correct :
  forall prec o f1 f2 f1' f2',
  let u x := binary (diff_operations _ ext_operations) o (f1 x, f1' x) (f2 x, f2' x) in
  forall yi1 yi2 yi1' yi2' xi,
 (forall x, contains xi x -> contains (I.convert yi1) (f1 x)) ->
 (forall x, contains xi x -> contains (I.convert yi2) (f2 x)) ->
 (forall x, contains xi x -> contains (I.convert yi1') (f1' x)) ->
 (forall x, contains xi x -> contains (I.convert yi2') (f2' x)) ->
  let v := binary (diff_operations _ (BndValuator.operations prec)) o (yi1, yi1') (yi2, yi2') in
 (forall x, contains xi x -> contains (I.convert (snd v)) (snd (u x))).
931
Proof.
932 933 934 935 936 937 938 939 940 941 942 943
intros prec o f1 f2 f1' f2' u yi1 yi2 yi1' yi2' xi Hf1 Hf2 Hf1' Hf2' v x Hx.
destruct o ; simpl ;
  repeat first
  [ apply I.add_correct
  | apply I.sub_correct
  | apply I.mul_correct
  | apply I.div_correct ] ;
  now first [ apply Hf1 | apply Hf2 | apply Hf1' | apply Hf2' ].
Qed.

Lemma eval_diff_bnd_correct :
  forall prec prog bounds n,
944
  let ff' x := nth n (eval_generic (Xnan, Xnan) (diff_operations _ ext_operations) prog ((x, Xmask (Xreal 1) x) :: map (fun v => (Xreal v, Xmask (Xreal 0) x)) (map real_from_bp bounds))) (Xnan, Xnan) in
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
  let ffi' xi := nth n (eval_generic (I.nai, I.nai) (diff_operations _ (BndValuator.operations prec)) prog
    ((xi, I.mask (I.fromZ 1) xi) :: map (fun b => (b, I.mask (I.fromZ 0) xi)) (map interval_from_bp bounds))) (I.nai, I.nai) in
  forall xi,
  nth n (BndValuator.eval prec prog (xi :: map interval_from_bp bounds)) I.nai = fst (ffi' xi) /\
 (forall x, contains (I.convert xi) x -> contains (I.convert (snd (ffi' xi))) (snd (ff' x))).
Proof.
intros prec prog bounds n ff' ffi' xi.
split.
(* . *)
unfold ffi', BndValuator.eval.
apply (eval_inductive_prop _ (I.type * I.type) (fun a b => a = fst b)).
apply refl_equal.
intros o a (bl, br) H.
rewrite H.
now destruct o.
intros o a1 a2 (bl1, br1) (bl2, br2) H1 H2.
961
rewrite H1 H2.
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
now destruct o.
clear.
intros [|n].
apply refl_equal.
simpl.
rewrite <- (map_nth (@fst I.type I.type)).
rewrite map_map.
simpl.
apply sym_eq.
exact (map_nth _ _ _ _).
(* . *)
refine (let toto := _ in fun x Hx => proj2 (toto x Hx : contains (I.convert (fst (ffi' xi))) (fst (ff' x)) /\ _)).
apply (eval_inductive_prop_fun (ExtendedR * _) (I.type * _) (fun a b =>
  forall x, contains (I.convert xi) x ->
  contains (I.convert (fst b)) (fst (a x)) /\
  contains (I.convert (snd b)) (snd (a x)))).
intros f1 f2 Heq (yi, yi') H x Hx.
rewrite <- Heq.
now apply H.
intros _ _.
simpl.
rewrite I.nai_correct.
now split.
intros o f (yi, yi') H x Hx.
rewrite (surjective_pairing (f x)).
split.
assert (Hf := proj1 (H x Hx)).
destruct o ; simpl ;
  [ apply I.neg_correct
  | apply I.abs_correct
  | apply I.inv_correct
  | apply I.sqr_correct
  | apply I.sqrt_correct
  | apply I.cos_correct
  | apply I.sin_correct
  | apply I.tan_correct
  | apply I.atan_correct
  | apply I.exp_correct
1000
  | apply I.ln_correct
1001 1002
  | apply I.power_int_correct
  | apply I.nearbyint_correct ] ;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  exact Hf.
apply (unary_diff_bnd_correct prec o (fun x => fst (f x)) (fun x => snd (f x))) with (3 := Hx).
exact (fun x Hx => proj1 (H x Hx)).
exact (fun x Hx => proj2 (H x Hx)).
intros o f1 f2 (yi1, yi1') (yi2, yi2') H1 H2 x Hx.
rewrite (surjective_pairing (f1 x)).
rewrite (surjective_pairing (f2 x)).
split.
assert (Hf1 := proj1 (H1 x Hx)).
assert (Hf2 := proj1 (H2 x Hx)).
destruct o ; simpl ;
  [ apply I.add_correct
  | apply I.sub_correct
  | apply I.mul_correct
  | apply I.div_correct ] ;
  first [ exact Hf1 | exact Hf2 ].
apply (binary_diff_bnd_correct prec o (fun x => fst (f1 x)) (fun x => fst (f2 x)) (fun x => snd (f1 x)) (fun x => snd (f2 x))) with (5 := Hx).
exact (fun x Hx => proj1 (H1 x Hx)).
exact (fun x Hx => proj1 (H2 x Hx)).
exact (fun x Hx => proj2 (H1 x Hx)).
exact (fun x Hx => proj2 (H2 x Hx)).
clear.
intros [|n] x Hx ; simpl.
split.
exact Hx.
apply I.mask_correct.
apply I.fromZ_correct.
exact Hx.
split.
rewrite <- (map_nth (@fst I.type I.type)).
rewrite <- (map_nth (@fst ExtendedR ExtendedR)).
do 4 rewrite map_map.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1035
simpl.
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
replace (map (fun x => interval_from_bp x) bounds) with (map interval_from_bp bounds).
replace (map (fun x => Xreal (real_from_bp x)) bounds) with (map xreal_from_bp bounds).
apply iterated_bnd_nth.
apply map_ext.
now destruct a.
apply map_ext.
now destruct a.
rewrite <- (map_nth (@snd I.type I.type)).
rewrite <- (map_nth (@snd ExtendedR ExtendedR)).
do 4 rewrite map_map.
simpl.
assert (H1 := map_length (fun _ => I.mask (I.fromZ 0) xi) bounds).
assert (H2 := map_length (fun _ => Xmask (Xreal 0) x) bounds).
destruct (le_or_lt (length bounds) n).
generalize H. intro H0.
rewrite <- H1 in H.
rewrite <- H2 in H0.
1053 1054
rewrite -> nth_overflow with (1 := H).
rewrite -> nth_overflow with (1 := H0).
1055 1056 1057 1058 1059 1060 1061
now rewrite I.nai_correct.
replace (nth n (map (fun _ => I.mask (I.fromZ 0) xi) bounds) I.nai) with (I.mask (I.fromZ 0) xi).
replace (nth n (map (fun _ => Xmask (Xreal 0) x) bounds) Xnan) with (Xmask (Xreal 0) x).
apply I.mask_correct.
apply I.fromZ_correct.
exact Hx.
rewrite <- H2 in H.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1062 1063
rewrite (nth_indep _ Xnan (Xmask (Xreal 0) x) H).
apply sym_eq.
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
refine (map_nth _ bounds (Bproof 0 I.nai _) _).
now rewrite I.nai_correct.
rewrite <- H1 in H.
rewrite (nth_indep _ I.nai (I.mask (I.fromZ 0) xi) H).
apply sym_eq.
refine (map_nth _ bounds (Bproof 0 I.nai _) _).
now rewrite I.nai_correct.
Qed.

Definition diff_refining_points prec xi di yi yi' ym yl yu :=
  match I.sign_large yi' with
  | Xund =>
    if I.bounded yi' then
      I.meet yi (I.add prec ym (I.mul prec di yi'))
    else yi
  | Xeq => ym
  | Xlt =>
    I.meet
     (if I.lower_bounded xi then I.lower_extent yl
      else I.whole)
     (if I.upper_bounded xi then I.upper_extent yu
      else I.whole)
  | Xgt =>
    I.meet
     (if I.lower_bounded xi then I.upper_extent yl
      else I.whole)
     (if I.upper_bounded xi then I.lower_extent yu
      else I.whole)
  end.

Definition diff_refining prec xi yi yi' fi :=
  match I.sign_large yi' with
  | Xund =>
    if I.bounded yi' then
      let m := I.midpoint xi in
      let mi := I.bnd m m in
      I.meet yi
       (I.add prec (fi mi) (I.mul prec (I.sub prec xi mi) yi'))
    else yi
  | Xeq =>
    let m := I.midpoint xi in fi (I.bnd m m)
  | Xlt =>
    I.meet
     (if I.lower_bounded xi then
        let l := I.lower xi in
        I.lower_extent (fi (I.bnd l l))
      else I.whole)
     (if I.upper_bounded xi then
        let u := I.upper xi in
        I.upper_extent (fi (I.bnd u u))
      else I.whole)
  | Xgt =>
    I.meet
     (if I.lower_bounded xi then
        let l := I.lower xi in
        I.upper_extent (fi (I.bnd l l))
      else I.whole)
     (if I.upper_bounded xi then
      let u := I.upper xi in
        I.lower_extent (fi (I.bnd u u))
      else I.whole)
  end.

Lemma diff_refining_aux_0 :
  forall f f' xi yi',
  Xderive f f' ->
  I.sign_large yi' <> Xund ->
 (forall x, contains xi x -> contains (I.convert yi') (f' x)) ->
  forall x, contains xi x ->
  x = Xreal (proj_val x) /\
  forall v,
  f x = Xreal (proj_fun v f (proj_val x)) /\
  f' x = Xreal (proj_fun v f' (proj_val x)).
1137
Proof.
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
intros f f' xi yi' Hd Hs Hy' x Hx.
case_eq (f' x).
(* assuming f'(x) is NaN ... *)
intros Hnf'.
generalize (Hy' _ Hx).
rewrite Hnf'.
intros Hny'.
apply False_ind.
generalize (I.sign_large_correct yi').
destruct (I.sign_large yi') ; intros H.
generalize (H _ Hny').
discriminate.
destruct (H _ Hny') as (H0, _).
discriminate H0.
destruct (H _ Hny') as (H0, _).
discriminate H0.
now elim Hs.
(* ... leads to a contradiction, so f'(x) is real ... *)
intros ry' Hrf'.
generalize (Hd x).
destruct x as [|x].
rewrite Hrf'.
now intro H.
(* ... so x is real too *)
rewrite Hrf'.
unfold Xderive_pt.
case_eq (f (Xreal x)).
now intros _ H.
intros ry Hrf _.
repeat split.
unfold proj_fun, proj_val.
now rewrite Hrf.
unfold proj_fun, proj_val.
now rewrite Hrf'.
Qed.

Lemma diff_refining_aux_1 :
  forall prec f f' xi yi' m mi ymi,
  Xderive f f' ->
  contains (I.convert mi) (Xreal m) ->
  contains (I.convert xi) (Xreal m) ->
  contains (I.convert ymi) (f (Xreal m)) ->
 (forall x, contains (I.convert xi) x -> contains (I.convert yi') (f' x)) ->
  forall x, contains (I.convert xi) x ->
  contains (I.convert (I.add prec ymi (I.mul prec (I.sub prec xi mi) yi'))) (f x).
1183
Proof.
1184 1185 1186 1187
intros prec f f' xi yi' m mi ymi Hd Hxm Hm Hym Hy' x Hx.
case_eq (I.convert yi').
(* - yi' is NaI *)
intro Hyn'.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1188 1189 1190
rewrite I.add_propagate_r.
easy.
now apply I.mul_propagate_r.
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
(* - yi' is real ... *)
intros yl' yu' Hyi'.
destruct x as [|x].
case_eq (I.convert xi).
intros Hxi.
generalize (Hy' _ Hx).
rewrite Hyi'.
generalize (Hd Xnan).
unfold Xderive_pt.
case (f' Xnan).
intros _ H.
elim H.
intros _ H _.
elim H.
intros xl xu Hxi.
rewrite Hxi in Hx.
elim Hx.
(* ... so x is real ... *)
set (Pxi := fun x => contains (I.convert xi) (Xreal x)).
assert (H': forall c, Pxi c -> f' (Xreal c) <> Xnan).
intros c Hc H.
generalize (Hy' (Xreal c) Hc).
1213
rewrite H Hyi'.
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
intro H0.
elim H0.
(* ... and we can apply the MVT *)
destruct (Xderive_MVT _ _ Hd Pxi (contains_connected _) H' _ Hm _ Hx) as (c, (Hc1, Hc2)).
rewrite Hc2.
apply I.add_correct.
exact Hym.
rewrite Xmul_comm.
apply I.mul_correct.
now apply I.sub_correct.
apply Hy'.
exact Hc1.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1226 1227
Qed.

1228 1229 1230 1231 1232 1233 1234 1235
Lemma diff_refining_aux_2 :
  forall f f' xi m ymi,
  Xderive f f' ->
  contains xi (Xreal m) ->
  contains ymi (f (Xreal m)) ->
 (forall x, contains xi x -> contains (Ibnd (Xreal 0) (Xreal 0)) (f' x)) ->
  forall x, contains xi x ->
  contains ymi (f x).
1236
Proof.
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
intros f f' xi m ymi Hd Hm Hym Hy'.
(* the derivative is zero ... *)
destruct xi as [|xl xu].
apply False_ind.
generalize (Hy' Xnan I) (Hd Xnan).
now case (f' (Xnan)).
intros [|x] Hx.
elim Hx.
(* ... so x is real ... *)
set (Pxi := fun x => contains (Ibnd xl xu) (Xreal x)).
assert (H': forall c, Pxi c -> f' (Xreal c) <> Xnan).
intros c Hc H.
generalize (Hy' (Xreal c) Hc).
now rewrite H.
(* ... and we can apply the MVT *)
destruct (Xderive_MVT _ _ Hd Pxi (contains_connected _) H' _ Hm _ Hx) as (c, (Hc1, Hc2)).
rewrite Hc2.
replace (f' (Xreal c)) with (Xreal 0).
1255
simpl.
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
rewrite Rmult_0_l.
rewrite Xadd_0_r.
exact Hym.
generalize (Hy' (Xreal c) Hc1).
destruct (f' (Xreal c)) as [|y].
intro H.
elim H.
intros (H3,H4).
apply f_equal.
now apply Rle_antisym.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1266 1267
Qed.

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
Theorem diff_refining_points_correct :
  forall prec f f' xi yi yi' ym yl yu,
  Xderive f f' ->
 (forall x, contains (I.convert xi) x -> contains (I.convert yi) (f x)) ->
 (forall x, contains (I.convert xi) x -> contains (I.convert yi') (f' x)) ->
  contains (I.convert ym) (f (I.convert_bound (I.midpoint xi))) ->
 (if I.lower_bounded xi then
    contains (I.convert yl) (f (I.convert_bound (I.lower xi)))
  else True) ->
 (if I.upper_bounded xi then
    contains (I.convert yu) (f (I.convert_bound (I.upper xi)))
  else True) ->
  let m := I.midpoint xi in
  forall x, contains (I.convert xi) x ->
  contains (I.convert (diff_refining_points prec xi (I.sub prec xi (I.bnd m m)) yi yi' ym yl yu)) (f x).
1283
Proof.
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
intros prec f f' xi yi yi' ym yl yu Hd Hyi Hyi' Hym Hyl Hyu m x Hx.
unfold m. clear m.
unfold diff_refining_points.
generalize (I.sign_large_correct yi').
case_eq (I.sign_large yi') ; intros Hs1 Hs2.
(* - sign is Xeq *)
destruct (I.midpoint_correct xi (ex_intro _ _ Hx)) as (H1, H2).
eapply diff_refining_aux_2 with (1 := Hd) (5 := Hx).
instantiate (1 := proj_val (I.convert_bound (I.midpoint xi))).
now rewrite <- H1.
now rewrite <- H1.
intros.
rewrite (Hs2 (f' x0)).
split ; apply Rle_refl.
now apply Hyi'.
(* - sign is Xlt *)
assert (I.sign_large yi' <> Xund).
now rewrite Hs1.
clear Hs1. rename H into Hs1.
assert (forall x, contains (I.convert xi) x -> forall v,
  f x = Xreal (proj_fun v f (proj_val x)) /\
  f' x = Xreal (proj_fun v f' (proj_val x)) /\
  (proj_fun v f' (proj_val x) <= 0)%R).
intros a Ha v.
destruct (Hs2 _ (Hyi' _ Ha)) as (Ha1, Ha2).
destruct (diff_refining_aux_0 _ _ _ _ Hd Hs1 Hyi' _ Ha) as (Ha3, Ha4).
destruct (Ha4 v) as (Ha5, Ha6).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1311
split.
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
exact Ha5.
split.
exact Ha6.
rewrite Ha1 in Ha6.
inversion Ha6.
exact Ha2.
clear Hs2. rename H into Hs2.
apply I.meet_correct.
(*   lower part *)
case_eq (I.lower_bounded xi).
intros H.
destruct (I.lower_bounded_correct xi H) as (Hxl, Hxi).
rewrite H in Hyl.
clear Hym Hyu H.
assert (Hl: contains (I.convert xi) (I.convert_bound (I.lower xi))).
1327
rewrite Hxi Hxl.
1328 1329 1330 1331
apply contains_lower with x.
now rewrite <- Hxl, <- Hxi.
rewrite (proj1 (Hs2 _ Hx R0)).
apply I.lower_extent_correct with (proj_fun 0 f (proj_val (I.convert_bound (I.lower xi)))).
1332
now rewrite <- (proj1 (Hs2 _ Hl 0)).
1333 1334 1335 1336 1337 1338 1339 1340 1341
destruct (diff_refining_aux_0 _ _ _ _ Hd Hs1 Hyi' _ Hx) as (Hx1, _).
eapply (derivable_neg_imp_decreasing (proj_fun R0 f) (proj_fun R0 f')).
apply (contains_connected (I.convert xi)).
intros a Ha.
simpl in Ha.
destruct (Hs2 _ Ha R0) as (Ha1, (Ha2, Ha3)).
split.
generalize (Hd (Xreal a)).
unfold Xderive_pt.
1342
rewrite Ha2 Ha1.
1343 1344 1345
intro H.
exact (H R0).
exact Ha3.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1346
simpl.
1347
now rewrite <- Hxl.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1348
simpl.
1349
now rewrite <- Hx1.
1350
rewrite -> Hxi, Hx1, Hxl in Hx.
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
exact (proj1 Hx).
intros _.
rewrite (proj1 (Hs2 x Hx R0)).
apply I.whole_correct.
(*   upper part *)
case_eq (I.upper_bounded xi).
intros H.
destruct (I.upper_bounded_correct xi H) as (Hxu, Hxi).
rewrite H in Hyu.
clear H.
assert (Hu: contains (I.convert xi) (I.convert_bound (I.upper xi))).
1362
rewrite Hxi Hxu.
1363 1364 1365 1366
apply contains_upper with x.
now rewrite <- Hxu, <- Hxi.
rewrite (proj1 (Hs2 _ Hx R0)).
apply I.upper_extent_correct with (proj_fun 0 f (proj_val (I.convert_bound (I.upper xi)))).
1367
now rewrite <- (proj1 (Hs2 _ Hu 0)).
1368 1369 1370 1371 1372 1373 1374 1375 1376
destruct (diff_refining_aux_0 _ _ _ _ Hd Hs1 Hyi' _ Hx) as (Hx1, _).
eapply (derivable_neg_imp_decreasing (proj_fun R0 f) (proj_fun R0 f')).
apply (contains_connected (I.convert xi)).
intros a Ha.
simpl in Ha.
destruct (Hs2 _ Ha R0) as (Ha1, (Ha2, Ha3)).
split.
generalize (Hd (Xreal a)).
unfold Xderive_pt.
1377
rewrite Ha2 Ha1.
1378 1379 1380
intro H.
exact (H R0).
exact Ha3.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1381
simpl.
1382 1383 1384
now rewrite <- Hx1.
simpl.
now rewrite <- Hxu.