AffineValidation.v 104 KB
Newer Older
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1
Require Import Coq.QArith.QArith Coq.QArith.Qreals QArith.Qminmax Coq.Lists.List Coq.micromega.Psatz.
2
Require Import Recdef.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
3
Require Import Flover.AffineForm Flover.AffineArithQ Flover.AffineArith.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
4
Require Import Flover.Infra.Abbrevs Flover.Infra.RationalSimps Flover.Infra.RealRationalProps.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
5
Require Import Flover.Infra.Ltacs Flover.Infra.RealSimps Flover.Typing Flover.ssaPrgs.
6
Require Import Flover.IntervalValidation Flover.RealRangeArith.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
7

Nikita Zyuzin's avatar
Nikita Zyuzin committed
8
Definition updateExpMapIncr e new_af noise (emap: expressionsAffine) intv incr :=
Nikita Zyuzin's avatar
Nikita Zyuzin committed
9 10 11 12 13 14 15 16 17 18 19
  let new_iv := toIntv new_af in
  if isSupersetIntv new_iv intv then
    Some (FloverMap.add e new_af emap, (noise + incr)%nat)
  else None.

Definition updateExpMap e af noise emap intv :=
  updateExpMapIncr e af noise emap intv 0.

Definition updateExpMapSucc e af noise emap intv :=
  updateExpMapIncr e af noise emap intv 1.

Nikita Zyuzin's avatar
Nikita Zyuzin committed
20 21 22 23
Definition nozeroiv iv :=
  ((Qleb (ivhi iv) 0) && (negb (Qeq_bool (ivhi iv) 0))) ||
            ((Qleb 0 (ivlo iv)) && (negb (Qeq_bool (ivlo iv) 0))).

24
Fixpoint validAffineBounds (e: expr Q) (A: analysisResult) (P: precond) (validVars: NatSet.t)
Nikita Zyuzin's avatar
Nikita Zyuzin committed
25
           (exprsAf: expressionsAffine) (currentMaxNoise: nat): option (expressionsAffine * nat) :=
26 27 28 29 30 31 32
  match FloverMap.find e exprsAf with
  | Some _ =>
    (* expression has already been checked; we do not want to introduce *)
    (*      a new affine polynomial for the same expression *)
    Some (exprsAf, currentMaxNoise)
  | None =>
    (* We see it for the first time; update the expressions map *)
33 34
    olet ares := FloverMap.find e A in
    let (intv, _) := ares in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
35
    match e with
Nikita Zyuzin's avatar
Nikita Zyuzin committed
36
    | Var _ v =>
37 38 39 40 41 42 43
      if NatSet.mem v validVars then
        Some (exprsAf, currentMaxNoise)
      else
        let af := fromIntv (P v) currentMaxNoise in
        if isSupersetIntv (toIntv af) intv then
          Some (FloverMap.add e af exprsAf, (currentMaxNoise + 1)%nat)
        else None
Nikita Zyuzin's avatar
Nikita Zyuzin committed
44
    | Const _ c => if isSupersetIntv (c, c) intv then
Nikita Zyuzin's avatar
Nikita Zyuzin committed
45
                    let af := fromIntv (c,c) currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
46 47 48
                    Some (FloverMap.add e af exprsAf, currentMaxNoise)
                  else None
    | Unop o e' =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
49
      olet valid := validAffineBounds e' A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
50 51
      let (exprsAf', n') := valid in
      olet af := FloverMap.find e' exprsAf' in
52
      match o with
Nikita Zyuzin's avatar
Nikita Zyuzin committed
53
      | Neg =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
54
        updateExpMap e (AffineArithQ.negate_aff af) n' exprsAf' intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
55
      | Inv =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
56 57 58 59 60
        let iv := toIntv af in
        if nozeroiv iv
        then
          updateExpMapSucc e (AffineArithQ.inverse_aff af n') n' exprsAf' intv
        else None
Nikita Zyuzin's avatar
Nikita Zyuzin committed
61 62
      end
    | Binop o e1 e2 =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
63
      olet valid1 := validAffineBounds e1 A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
64 65
      let (exprsAf1, n1) := valid1 in
      olet af1 := FloverMap.find e1 exprsAf1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
66
      olet valid2 := validAffineBounds e2 A P validVars exprsAf1 n1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
67 68 69 70
      let (exprsAf2, n2) := valid2 in
      olet af2 := FloverMap.find e2 exprsAf2 in
      match o with
      | Plus =>
71
        updateExpMap e (AffineArithQ.plus_aff af1 af2) n2 exprsAf2 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
72
      | Sub =>
73
        updateExpMap e (AffineArithQ.subtract_aff af1 af2) n2 exprsAf2 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
74
      | Mult =>
75
        updateExpMapSucc e (AffineArithQ.mult_aff af1 af2 n2) n2 exprsAf2 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
76
      | Div =>
77 78
          olet ares2 := FloverMap.find e2 A in
          let (aiv2, _) := ares2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
79
          let iv2 := toIntv af2 in
80
          if nozeroiv iv2 && nozeroiv aiv2
Nikita Zyuzin's avatar
Nikita Zyuzin committed
81
          then
82
            updateExpMapIncr e (AffineArithQ.divide_aff af1 af2 n2) n2 exprsAf2 intv 2
Nikita Zyuzin's avatar
Nikita Zyuzin committed
83 84 85
          else None
      end
    | Fma e1 e2 e3 =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
86
      olet valid1 := validAffineBounds e1 A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
87 88
      let (exprsAf1, n1) := valid1 in
      olet af1 := FloverMap.find e1 exprsAf1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
89
      olet valid2 := validAffineBounds e2 A P validVars exprsAf1 n1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
90 91
      let (exprsAf2, n2) := valid2 in
      olet af2 := FloverMap.find e2 exprsAf2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
92
      olet valid3 := validAffineBounds e3 A P validVars exprsAf2 n2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
93 94
      let (exprsAf3, n3) := valid3 in
      olet af3 := FloverMap.find e3 exprsAf3 in
95
        updateExpMapSucc e (AffineArithQ.plus_aff af1 (AffineArithQ.mult_aff af2 af3 n3)) n3 exprsAf3 intv
96
    | Downcast _ e' =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
97
      olet valid' := validAffineBounds e' A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
98 99 100 101 102
      let (exprsAf', n') := valid' in
      olet asubres := FloverMap.find e' A in
      let (iv, _) := asubres in
      olet af' := FloverMap.find e' exprsAf' in
      if (isSupersetIntv intv iv) && (isSupersetIntv iv intv) then
103
        Some (FloverMap.add e af' exprsAf', n')
Nikita Zyuzin's avatar
Nikita Zyuzin committed
104
      else None
105
    end
Nikita Zyuzin's avatar
Nikita Zyuzin committed
106
  end.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
107

Nikita Zyuzin's avatar
Nikita Zyuzin committed
108 109 110 111 112
Fixpoint afQ2R (af: affine_form Q): affine_form R := match af with
| AffineForm.Const c => AffineForm.Const (Q2R c)
| Noise n v af' => Noise n (Q2R v) (afQ2R af')
end.

113 114 115 116 117 118
Lemma afQ2R_const v:
  afQ2R (AffineForm.Const v) = AffineForm.Const (Q2R v).
Proof.
  trivial.
Qed.

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
Lemma afQ2R_get_const a:
  get_const (afQ2R a) = Q2R (get_const a).
Proof.
  induction a; auto.
Qed.

Lemma afQ2R_radius a:
  radius (afQ2R a) = Q2R (AffineArithQ.radius a).
Proof.
  induction a; try (simpl; lra).
  simpl.
  rewrite Q2R_plus.
  rewrite Rabs_eq_Qabs.
  now f_equal.
Qed.

135 136 137 138 139 140 141
Lemma afQ2R_get_max_index a:
  get_max_index (afQ2R a) = get_max_index a.
Proof.
  unfold get_max_index.
  functional induction (get_max_index_aux 0 a); try auto;simpl; rewrite e0; auto.
Qed.

142 143 144 145 146 147 148 149 150 151
Lemma to_interval_to_intv a:
  (Q2R (fst (toIntv a)), Q2R (snd (toIntv a))) = toInterval (afQ2R a).
Proof.
  unfold toIntv, toInterval.
  unfold mkInterval, mkIntv.
  simpl fst; simpl snd.
  rewrite afQ2R_get_const.
  rewrite afQ2R_radius.
  f_equal; try apply Q2R_minus; try apply Q2R_plus.
Qed.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

Lemma afQ2R_plus_aff af1 af2:
  afQ2R (AffineArithQ.plus_aff af1 af2) = plus_aff (afQ2R af1) (afQ2R af2).
Proof.
  unfold AffineArithQ.plus_aff, plus_aff.
  remember (af1, af2) as a12.
  assert (fst a12 = af1 /\ snd a12 = af2) as Havals by now rewrite Heqa12.
  destruct Havals as [Heqa1 Heqa2].
  rewrite <- Heqa1, <- Heqa2.
  clear Heqa1 Heqa2 Heqa12 af1 af2.
  functional induction (AffineArithQ.plus_aff_tuple a12); simpl; rewrite plus_aff_tuple_equation.
  - f_equal; apply Q2R_plus.
  - f_equal.
    assumption.
  - f_equal.
    assumption.
  - rewrite e2.
    f_equal; try apply Q2R_plus.
    assumption.
  - rewrite e2.
    rewrite e3.
    f_equal; try apply Q2R_plus.
    assumption.
  - rewrite e2.
    rewrite e3.
    f_equal; try apply Q2R_plus.
    assumption.
Qed.

Nikita Zyuzin's avatar
Nikita Zyuzin committed
181 182
Lemma afQ2R_mult_aff_aux af1 af2:
  afQ2R (AffineArithQ.mult_aff_aux (af1, af2)) = mult_aff_aux (afQ2R af1, afQ2R af2).
183 184 185 186 187 188 189
Proof.
  unfold AffineArithQ.mult_aff, mult_aff.
  remember (af1, af2) as a12.
  assert (fst a12 = af1 /\ snd a12 = af2) as Havals by now rewrite Heqa12.
  destruct Havals as [Heqa1 Heqa2].
  rewrite <- Heqa1, <- Heqa2.
  clear Heqa1 Heqa2 Heqa12 af1 af2.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
190 191 192 193 194 195
  functional induction (AffineArithQ.mult_aff_aux a12); simpl in *;
    rewrite mult_aff_aux_equation;
    try (f_equal; try apply Q2R_mult; assumption).
  {
    rewrite e2.
    f_equal; try assumption.
196
    simpl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    rewrite Q2R_plus; do 2 rewrite Q2R_mult.
    do 2 rewrite afQ2R_get_const.
    reflexivity.
  }
  all: rewrite e2; rewrite e3.
  all: f_equal; try assumption.
  all: simpl.
  all: rewrite Q2R_mult.
  all: rewrite afQ2R_get_const.
  all: reflexivity.
Qed.

Lemma afQ2R_mult_aff af1 af2 n:
  afQ2R (AffineArithQ.mult_aff af1 af2 n) = mult_aff (afQ2R af1) (afQ2R af2) n.
Proof.
  unfold AffineArithQ.mult_aff, mult_aff.
  destruct (Qeq_bool (AffineArithQ.radius af1) 0) eqn: Heq.
  - rewrite orb_true_l.
    rewrite Qeq_bool_iff in Heq.
    apply Qeq_eqR in Heq.
    rewrite <- afQ2R_radius in Heq.
    rewrite Q2R0_is_0 in Heq.
    destruct Req_dec_sum as [Heq' | Heq']; rewrite Heq in Heq'; try lra.
    apply afQ2R_mult_aff_aux.
  - rename Heq into Heq1.
    destruct (Qeq_bool (AffineArithQ.radius af2) 0) eqn: Heq2.
    + rewrite orb_true_r.
      rewrite Qeq_bool_iff in Heq2.
      apply Qeq_eqR in Heq2.
      rewrite <- afQ2R_radius in Heq2.
      rewrite Q2R0_is_0 in Heq2.
      destruct Req_dec_sum as [Heq' | Heq']; rewrite Heq2 in Heq'; try lra.
      apply afQ2R_mult_aff_aux.
    + apply RMicromega.Qeq_false in Heq1.
      apply RMicromega.Qeq_false in Heq2.
      rewrite Q2R0_is_0 in Heq1.
      rewrite Q2R0_is_0 in Heq2.
      rewrite <- afQ2R_radius in Heq1.
      rewrite <- afQ2R_radius in Heq2.
236
      simpl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
237 238
      destruct Req_dec_sum as [Heq' | Heq'];
        try (apply Rmult_integral in Heq'; destruct Heq'; try lra).
239
      f_equal.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
240 241
      * rewrite Q2R_mult.
        do 2 rewrite afQ2R_radius.
242
        reflexivity.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
243 244
      * apply afQ2R_mult_aff_aux.
Qed.
245

Nikita Zyuzin's avatar
Nikita Zyuzin committed
246 247 248
Lemma afQ2R_negate_aff af:
  afQ2R (AffineArithQ.negate_aff af) = negate_aff (afQ2R af).
Proof.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  unfold AffineArithQ.negate_aff.
  unfold AffineArithQ.mult_aff_const.
  rewrite afQ2R_mult_aff.
  simpl.
  unfold negate_aff.
  unfold mult_aff_const.
  f_equal.
  - f_equal.
    rewrite Q2R_opp.
    lra.
  - f_equal.
    unfold get_max_index.
    functional induction (get_max_index_aux 0 af); try auto; simpl; now rewrite e0.
Qed.

Lemma afQ2R_subtract_aff af1 af2:
  afQ2R (AffineArithQ.subtract_aff af1 af2) = subtract_aff (afQ2R af1) (afQ2R af2).
Proof.
  unfold AffineArithQ.subtract_aff.
  rewrite afQ2R_plus_aff.
  rewrite afQ2R_negate_aff.
  reflexivity.
Qed.

Nikita Zyuzin's avatar
Nikita Zyuzin committed
273
Lemma afQ2R_inverse_aff af n:
274
  above_zero (afQ2R af) \/ below_zero (afQ2R af) ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
275 276
  afQ2R (AffineArithQ.inverse_aff af n) = inverse_aff (afQ2R af) n.
Proof.
277
  intros above_below.
278 279
  unfold AffineArithQ.inverse_aff.
  unfold inverse_aff.
280 281 282
  unfold above_zero, below_zero in above_below.
  unfold ivhi, IVhi, ivlo, IVlo in *.
  rewrite <- to_interval_to_intv in above_below.
283
  rewrite <- to_interval_to_intv.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  simpl fst in above_below |-*.
  simpl snd in above_below |-*.
  assert (AffineArithQ.radius af >= 0) by eauto using AffineArithQ.radius_nonneg.
  assert (get_const af - AffineArithQ.radius af <= get_const af + AffineArithQ.radius af) as Hrel by lra.
  replace 0%R with (Q2R 0) in above_below by lra.
  destruct above_below as [Heq | Heq]; apply Rlt_Qlt in Heq.
  - assert (get_const af + AffineArithQ.radius af > 0) as Hposhi by lra.
    destruct (Qlt_bool (get_const af + AffineArithQ.radius af) 0) eqn: H'';
      try rewrite Qlt_bool_iff in H''; try lra.
    apply Qlt_Rlt in Hposhi.
    destruct (Rlt_dec (Q2R (get_const af + AffineArithQ.radius af)) 0); try lra.
    apply Rlt_Qlt in Hposhi.
    assert (~ get_const af - AffineArithQ.radius af == 0) as minus_nonzero by lra.
    assert (~ get_const af + AffineArithQ.radius af == 0) as plus_nonzero by lra.
    assert (~ -(get_const af - AffineArithQ.radius af) == 0) as minus_nonzero' by lra.
    assert (~ -(get_const af + AffineArithQ.radius af) == 0) as plus_nonzero' by lra.
    assert (~ maxAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) *
            maxAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) == 0) as ?.
    {
      unfold maxAbs.
      unfold minAbs.
      simpl fst.
      simpl snd.
      try rewrite Qabs.Qabs_pos; try lra.
      try rewrite Qabs.Qabs_pos; try lra.
      try rewrite Q.max_r; try lra.
      try rewrite Q.min_l; try lra.
      unfold not.
      intros H'.
      apply Qmult_integral in H'; lra.
    }
    assert (~ maxAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) == 0) as ?.
    {
      unfold maxAbs.
      unfold minAbs.
      simpl fst.
      simpl snd.
      try rewrite Qabs.Qabs_pos; try lra.
      try rewrite Qabs.Qabs_pos; try lra.
      try rewrite Q.max_r; try lra.
    }
    assert (~ minAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) == 0) as ?.
    {
      unfold maxAbs.
      unfold minAbs.
      simpl fst.
      simpl snd.
      try rewrite Qabs.Qabs_pos; try lra.
      try rewrite Qabs.Qabs_pos; try lra.
      try rewrite Q.max_r; try lra.
      try rewrite Q.min_l; try lra.
    }
    simpl.
    f_equal; unfold toIntv; simpl;
      repeat rewrite minAbs_impl_RminAbs; repeat rewrite maxAbs_impl_RmaxAbs;
      replace 1%R with (Q2R (1%Q)) by lra;
      replace 2%R with (Q2R (2#1)) by lra;
      repeat (repeat rewrite <- Q2R_plus; repeat rewrite <- Q2R_mult; repeat rewrite <- Q2R_div;
              repeat rewrite <- Q2R_minus; repeat rewrite <- Q2R_opp); try lra.
      unfold mult_aff_const, plus_aff_const.
      unfold AffineArithQ.mult_aff_const, AffineArithQ.plus_aff_const.
      repeat rewrite <- afQ2R_const.
      rewrite <- afQ2R_mult_aff.
      rewrite <- afQ2R_plus_aff.
      repeat f_equal.
      now rewrite afQ2R_get_max_index.
  - rewrite <- Qlt_bool_iff in Heq.
    rewrite Heq.
    rewrite Qlt_bool_iff in Heq.
353 354
    apply Qlt_Rlt in Heq.
    destruct (Rlt_dec (Q2R (get_const af + AffineArithQ.radius af)) 0); try lra.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    apply Rlt_Qlt in Heq.
    assert (get_const af - AffineArithQ.radius af < 0) as minus_neg by lra.
    assert (~ get_const af - AffineArithQ.radius af == 0) as minus_nonzero by lra.
    assert (~ get_const af + AffineArithQ.radius af == 0) as plus_nonzero by lra.
    assert (~ -(get_const af - AffineArithQ.radius af) == 0) as minus_nonzero' by lra.
    assert (~ -(get_const af + AffineArithQ.radius af) == 0) as plus_nonzero' by lra.
    assert (~ maxAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) *
            maxAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) == 0) as ?.
    {
      unfold maxAbs.
      unfold minAbs.
      simpl fst.
      simpl snd.
      try rewrite Qabs.Qabs_neg; try lra.
      try rewrite Qabs.Qabs_neg; try lra.
      try rewrite Q.max_l; try lra.
      try rewrite Q.min_r; try lra.
      unfold not.
      intros H'.
      apply Qmult_integral in H'; lra.
    }
    assert (~ maxAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) == 0) as ?.
    {
      unfold maxAbs.
      unfold minAbs.
      simpl fst.
      simpl snd.
      try rewrite Qabs.Qabs_neg; try lra.
      try rewrite Qabs.Qabs_neg; try lra.
      rewrite Q.max_l; try lra.
    }
    assert (~ minAbs (get_const af - AffineArithQ.radius af, get_const af + AffineArithQ.radius af) == 0) as ?.
    {
      unfold maxAbs.
      unfold minAbs.
      simpl fst.
      simpl snd.
      try rewrite Qabs.Qabs_neg; try lra.
      try rewrite Qabs.Qabs_neg; try lra.
      try rewrite Q.max_l; try lra.
      try rewrite Q.min_r; try lra.
    }
397
    simpl.
398 399 400 401 402 403 404 405 406 407 408 409 410 411
    f_equal; unfold toIntv; simpl;
      repeat rewrite minAbs_impl_RminAbs; repeat rewrite maxAbs_impl_RmaxAbs;
      replace 1%R with (Q2R (1%Q)) by lra;
      replace 2%R with (Q2R (2#1)) by lra;
      repeat (repeat rewrite <- Q2R_plus; repeat rewrite <- Q2R_mult; repeat rewrite <- Q2R_div;
              repeat rewrite <- Q2R_minus; repeat rewrite <- Q2R_opp); try lra.
      unfold mult_aff_const, plus_aff_const.
      unfold AffineArithQ.mult_aff_const, AffineArithQ.plus_aff_const.
      repeat rewrite <- afQ2R_const.
      rewrite <- afQ2R_mult_aff.
      rewrite <- afQ2R_plus_aff.
      repeat f_equal.
      now rewrite afQ2R_get_max_index.
Qed.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
412

413
Lemma afQ2R_divide_aff af1 af2 n:
414
  above_zero (afQ2R af2) \/ below_zero (afQ2R af2) ->
415
  afQ2R (AffineArithQ.divide_aff af1 af2 n) = divide_aff (afQ2R af1) (afQ2R af2) n.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
416
Proof.
417
  intros.
418 419
  unfold AffineArithQ.divide_aff.
  rewrite afQ2R_mult_aff.
420
  rewrite afQ2R_inverse_aff; auto.
421
Qed.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
422

Nikita Zyuzin's avatar
Nikita Zyuzin committed
423
Lemma afQ2R_fresh n a:
Nikita Zyuzin's avatar
Nikita Zyuzin committed
424
  fresh n a <-> fresh n (afQ2R a).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
425
Proof.
426
  split; induction a; intros *.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
427 428 429 430 431 432 433
  all: try (unfold fresh, get_max_index; rewrite get_max_index_aux_equation; now simpl).
  all: intros A.
  all: remember A as A' eqn:tmp; clear tmp.
  all: apply fresh_noise_gt in A.
  all: apply fresh_noise_compat in A'.
  all: specialize (IHa A').
  all: apply fresh_noise; assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
434 435
Qed.

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
Lemma nozero_above_below af:
  nozeroiv (toIntv af) = true ->
  above_zero (afQ2R af) \/ below_zero (afQ2R af).
Proof.
  intros noz % orb_prop.
  destruct noz as [below | above].
  - right.
    unfold below_zero.
    apply andb_prop in below as [below notz].
    rewrite negb_true_iff in notz.
    apply Qeq_bool_neq in notz.
    rewrite Qle_bool_iff in below.
    assert (IVhi (toInterval (afQ2R af)) = Q2R (ivhi (toIntv af))) as Heq
        by (rewrite <- to_interval_to_intv; trivial).
    rewrite Heq.
    assert (ivhi (toIntv af) < 0) as Hlt by lra.
    apply Qlt_Rlt in Hlt.
    lra.
  - left.
    unfold above_zero.
    apply andb_prop in above as [above notz].
    rewrite negb_true_iff in notz.
    apply Qeq_bool_neq in notz.
    rewrite Qle_bool_iff in above.
    assert (IVlo (toInterval (afQ2R af)) = Q2R (ivlo (toIntv af))) as Heq
        by (rewrite <- to_interval_to_intv; trivial).
    rewrite Heq.
    assert (ivlo (toIntv af) > 0) as Hlt by lra.
    apply Qlt_Rlt in Hlt.
    lra.
Qed.

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
Lemma subset_union s1 s2 s3:
  NatSet.Subset (s1 -- s3) s2 ->
  NatSet.Subset s1 (NatSet.union s2 s3).
Proof.
  intros diff.
  hnf in diff |-*.
  intros a Hin1.
  specialize (diff a).
  destruct (NatSet.mem a s3) eqn: Hmem.
  - rewrite NatSet.mem_spec in Hmem.
    rewrite NatSet.union_spec.
    now right.
  - apply not_in_not_mem in Hmem.
    rewrite NatSet.union_spec.
    left.
    apply diff.
    set_tac.
Qed.

Nikita Zyuzin's avatar
Nikita Zyuzin committed
487 488 489
Definition affine_dVars_range_valid (dVars: NatSet.t) (E: env) (A: analysisResult) noise exprAfs map1: Prop :=
  forall v, NatSet.In v dVars ->
       exists af vR iv err,
490 491 492
         isSupersetIntv (toIntv af) iv = true /\
         FloverMap.find (elt:=affine_form Q) (Var Q v) exprAfs = Some af /\
         fresh noise af /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
493
         (forall n, (n >= noise)%nat -> map1 n = None) /\
494 495
         FloverMap.find (Var Q v) A = Some (iv, err) /\
         E v = Some vR /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
496
         af_evals (afQ2R af) vR map1.
497

498
Lemma validAffineBounds_validRanges e (A: analysisResult) E Gamma fBits:
499 500 501
  (exists map af vR aiv aerr,
      FloverMap.find e A = Some (aiv, aerr) /\
      isSupersetIntv (toIntv af) aiv = true /\
502
      eval_expr E (toRMap Gamma) fBits (toREval (toRExp e)) vR REAL /\
503 504 505
      af_evals (afQ2R af) vR map) ->
  exists iv err vR,
    FloverMap.find e A = Some (iv, err) /\
506
    eval_expr E (toRMap Gamma) fBits (toREval (toRExp e)) vR REAL /\
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    (Q2R (fst iv) <= vR <= Q2R (snd iv))%R.
Proof.
  intros sound_affine.
  destruct sound_affine as [map [af [vR [aiv [aerr [Haiv [Hsup [Hee Heval]]]]]]]].
  exists aiv, aerr, vR.
  split; try split; try auto.
  apply AffineArith.to_interval_containment in Heval.
  unfold isSupersetIntv in Hsup.
  apply andb_prop in Hsup as [Hsupl Hsupr].
  apply Qle_bool_iff in Hsupl.
  apply Qle_bool_iff in Hsupr.
  apply Qle_Rle in Hsupl.
  apply Qle_Rle in Hsupr.
  rewrite <- to_interval_to_intv in Heval.
  simpl in Heval.
  destruct Heval as [Heval1 Heval2].
  split; eauto using Rle_trans.
Qed.

526
Definition checked_expressions (A: analysisResult) E Gamma fBits fVars dVars e iexpmap inoise map1 :=
527
  exists af vR aiv aerr,
528
    NatSet.Subset (usedVars e) (NatSet.union fVars dVars) /\
529 530 531 532 533
    FloverMap.find e A = Some (aiv, aerr) /\
    isSupersetIntv (toIntv af) aiv = true /\
    FloverMap.find e iexpmap = Some af /\
    fresh inoise af /\
    (forall n, (n >= inoise)%nat -> map1 n = None) /\
534 535
    eval_expr E (toRMap Gamma) fBits (toREval (toRExp e)) vR REAL /\
    validRanges e A E Gamma fBits /\
536 537
    af_evals (afQ2R af) vR map1.

538
Lemma checked_expressions_contained A E Gamma fBits fVars dVars e expmap1 expmap2 map1 map2 noise1 noise2:
539 540 541 542
  contained_map map1 map2 ->
  contained_flover_map expmap1 expmap2 ->
  (noise2 >= noise1)%nat ->
  (forall n : nat, (n >= noise2)%nat -> map2 n = None) ->
543 544
  checked_expressions A E Gamma fBits fVars dVars e expmap1 noise1 map1 ->
  checked_expressions A E Gamma fBits fVars dVars e expmap2 noise2 map2.
545 546 547 548 549 550 551 552
Proof.
  intros cont contf Hnoise Hvalidmap checked1.
  unfold checked_expressions in checked1 |-*.
  destruct checked1 as [af [vR [aiv [aerr checked1]]]].
  exists af, vR, aiv, aerr.
  intuition; eauto using fresh_monotonic, af_evals_map_extension.
Qed.

553
Lemma checked_expressions_flover_map_add_compat A E Gamma fBits fVars dVars e e' af expmap noise map:
554
  Q_orderedExps.exprCompare e e' <> Eq ->
555 556
  checked_expressions A E Gamma fBits fVars dVars e' expmap noise map ->
  checked_expressions A E Gamma fBits fVars dVars e' (FloverMap.add e af expmap) noise map.
557 558 559 560 561 562 563 564
Proof.
  intros Hneq checked1.
  unfold checked_expressions in checked1 |-*.
  destruct checked1 as [af' [vR [aiv [aerr checked1]]]].
  exists af', vR, aiv, aerr.
  intuition.
  rewrite FloverMapFacts.P.F.add_neq_o; auto.
Qed.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
565

566
Lemma validAffineBounds_sound (e: expr Q) (A: analysisResult) (P: precond)
567
      fVars dVars (E: env) Gamma fBits exprAfs noise iexpmap inoise map1:
568
  (forall e, (exists af, FloverMap.find e iexpmap = Some af) ->
569
        checked_expressions A E Gamma fBits fVars dVars e iexpmap inoise map1) ->
570 571 572 573 574
  (inoise > 0)%nat ->
  (forall n, (n >= inoise)%nat -> map1 n = None) ->
  validAffineBounds e A P dVars iexpmap inoise = Some (exprAfs, noise) ->
  affine_dVars_range_valid dVars E A inoise iexpmap map1 ->
  NatSet.Subset (NatSet.diff (Expressions.usedVars e) dVars) fVars ->
575 576
  fVars_P_sound fVars E P ->
  vars_typed (NatSet.union fVars dVars) Gamma ->
577 578 579 580 581 582 583 584 585
  exists map2 af vR aiv aerr,
    contained_map map1 map2 /\
    contained_flover_map iexpmap exprAfs /\
    FloverMap.find e A = Some (aiv, aerr) /\
    isSupersetIntv (toIntv af) aiv = true /\
    FloverMap.find e exprAfs = Some af /\
    fresh noise af /\
    (forall n, (n >= noise)%nat -> map2 n = None) /\
    (noise >= inoise)%nat /\
586 587
    eval_expr E (toRMap Gamma) fBits (toREval (toRExp e)) vR REAL /\
    validRanges e A E Gamma fBits /\
588 589 590
    af_evals (afQ2R af) vR map2 /\
    (forall e, FloverMap.find e iexpmap = None ->
          (exists af, FloverMap.find e exprAfs = Some af) ->
591
          checked_expressions A E Gamma fBits fVars dVars e exprAfs noise map2).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
592
Proof.
593
  revert noise exprAfs inoise iexpmap map1.
594
  induction e;
595
    intros * visitedExpr inoisegtz validmap1 validBounds dVarsValid varsDisjoint fVarsSound varsTyped;
596
    simpl in validBounds.
597
  - specialize (dVarsValid n).
598
    specialize (fVarsSound n).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
599
    specialize (varsTyped n).
600
    pose proof visitedExpr as visitedExpr'.
601 602 603
    destruct (FloverMap.find (elt:=affine_form Q) (Var Q n) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
604
      specialize (visitedExpr (Var Q n)).
605 606 607 608 609 610 611 612 613 614 615 616
      assert (NatSet.Subset (usedVars (Var Q n)) (fVars  dVars)).
      {
        set_tac. set_tac. subst.
        hnf in varsDisjoint.
        specialize (varsDisjoint a0).
        destruct (NatSet.mem a0 dVars) eqn:?.
        + right. now apply NatSet.mem_spec.
        + left. apply varsDisjoint. set_tac.
      }
      destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]]; eauto.
      exists map1, af, vR, aiv, aerr.
      intuition.
617
    }
618
    destruct (FloverMap.find (elt:=intv * error) (Var Q n) A) as [p |] eqn: Hares; simpl in validBounds; try congruence.
619
    destruct p as [aiv aerr].
Nikita Zyuzin's avatar
Nikita Zyuzin committed
620
    destruct (n mem dVars) eqn: Hmem.
621
    + rewrite NatSet.mem_spec in Hmem.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
622
      specialize (dVarsValid Hmem).
623 624
      assert (n  fVars  dVars) as H by intuition.
      specialize (varsTyped H) as [m varsTyped].
Nikita Zyuzin's avatar
Nikita Zyuzin committed
625
      destruct dVarsValid as [af [vR [iv [err dVarsValid]]]]; try reflexivity.
626
      inversion validBounds; subst; clear validBounds.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
627
      exists map1, af, vR, iv, err.
628
      intuition; try congruence.
629
    + destruct (isSupersetIntv (toIntv (fromIntv (P n) inoise)) aiv) eqn: Hsup; try congruence.
630
      inversion validBounds; subst; clear validBounds.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
631 632 633 634
      apply not_in_not_mem in Hmem.
      assert (n  fVars  dVars) as H by intuition.
      specialize (varsTyped H) as [m varsTyped].
      assert (n  fVars) as H' by intuition.
635
      specialize (fVarsSound H') as [vR [eMap interval_containment]].
636
      assert (FloverMap.find (Var Q n) (FloverMap.add (Var Q n) (fromIntv (P n) inoise) iexpmap) = Some (fromIntv (P n) inoise)) as Hfind
637
        by (rewrite FloverMapFacts.P.F.add_eq_o; try auto; apply Q_orderedExps.exprCompare_refl).
638
      assert (eval_expr E (toRMap Gamma) fBits (toREval (toRExp (Var Q n))) vR REAL) as Heeval
639
          by (constructor; auto; simpl; rewrite varsTyped; reflexivity).
640
      destruct (Qeq_bool (ivlo (P n)) (ivhi (P n))) eqn: Heq.
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
      * assert (af_evals (afQ2R (fromIntv (P n) inoise)) vR map1) as Hevals.
        {
          assert (fromIntv (P n) inoise = (AffineForm.Const (ivhi (P n) / (2 # 1) + ivlo (P n) / (2 # 1))%Q)) as HfromIntv
              by (unfold fromIntv; now rewrite Heq).
          pose proof Heq as Heq'.
          apply Qeq_bool_iff in Heq'.
          simpl in Heq'.
          apply Qeq_eqR in Heq'.
          rewrite Heq' in interval_containment.
          assert (vR = Q2R (snd (P n))) as HvR by lra.
          rewrite HfromIntv.
          unfold af_evals.
          simpl.
          rewrite Q2R_plus.
          repeat rewrite Q2R_div by lra.
          rewrite Heq'.
          rewrite HvR.
          lra.
        }
        assert (fresh (inoise + 1) (fromIntv (P n) inoise)) as Hfresh
            by (unfold fresh, fromIntv, get_max_index; rewrite Heq; simpl; lia).
        exists map1, (fromIntv (P n) inoise), vR, aiv, aerr.
663 664
        repeat split; auto.
        -- reflexivity.
665
        -- apply contained_flover_map_extension; assumption.
666 667 668 669
        -- intros n' Hn'.
           apply validmap1.
           lia.
        -- lia.
670 671 672
        -- apply validAffineBounds_validRanges.
           exists map1, (fromIntv (P n) inoise), vR, aiv, aerr.
           repeat split; auto.
673 674 675 676
        -- intros e Hnone Hsome.
           destruct Hsome as [afS Hsome].
           {
             destruct (FloverMapFacts.O.MO.eq_dec (Var Q n) e).
677 678
             - assert (Q_orderedExps.exprCompare e (Var Q n) = Eq)
                 by (now rewrite Q_orderedExps.exprCompare_eq_sym).
679
               rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
680 681 682 683
               inversion Hsome; subst; clear Hsome.
               unfold checked_expressions.
               exists (fromIntv (P n) inoise), vR, aiv, aerr.
               intuition.
684 685 686
               + rewrite usedVars_eq_compat; eauto.
                 set_tac.
                 left; set_tac; split; auto; subst; set_tac.
687 688
               + erewrite FloverMapFacts.P.F.find_o; eauto.
               + rewrite FloverMapFacts.P.F.add_eq_o; auto.
689
               + erewrite expr_compare_eq_eval_compat; eauto.
690 691 692 693 694
               + eapply validRanges_eq_compat; eauto.
                 simpl; split; auto.
                 apply validAffineBounds_validRanges.
                 exists map1, (fromIntv (P n) inoise), vR, aiv, aerr.
                 repeat split; auto.
695 696 697
             - rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
               congruence.
           }
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
      * assert (exists q, af_evals (afQ2R (fromIntv (P n) inoise)) vR (updMap map1 inoise q))
          as [q Hevals].
        {
          unfold af_evals, fromIntv.
          rewrite Heq.
          apply Qeq_bool_neq in Heq.
          simpl in Heq.
          simpl.
          setoid_rewrite upd_sound.
          simpl.
          apply Q.max_case_strong.
          - intros x y Hxy.
            apply Qeq_eqR in Hxy.
            rewrite Hxy.
            auto.
          - intros Hmax.
            apply Qle_Rle in Hmax.
            repeat rewrite Q2R_minus.
            repeat rewrite Q2R_plus.
            repeat rewrite Q2R_div; try lra.
            replace (Q2R (2#1)) with 2%R by lra.
            repeat rewrite Q2R_minus in Hmax.
            repeat rewrite Q2R_plus in Hmax.
            rewrite Q2R_div in Hmax; try lra.
            rewrite Q2R_div in Hmax; try lra.
            replace (Q2R (2#1)) with 2%R in Hmax by lra.
            pose (l := (Q2R (fst (P n)))).
            pose (h := (Q2R (snd (P n)))).
            fold l h in Hmax, interval_containment |-*.
            pose (noise_expression := ((vR - h / 2 - l / 2) / (h / 2 + l / 2 - l))%R).
            assert (-(1) <= noise_expression <= 1)%R as Hnoise.
            {
              unfold noise_expression.
              apply Rabs_Rle_condition.
              destruct (Rle_lt_dec (h / 2 + l / 2 - l) 0)%R as [Hle0 | Hle0].
              - apply Rle_lt_or_eq_dec in Hle0; destruct Hle0 as [Hlt | Hlt];
                  try (field_simplify in Hlt; assert (h = l) as Hz by lra; apply eqR_Qeq in Hz; lra).
              - rewrite Rdiv_abs_le_bounds; try lra.
                assert (0 < h - l)%R as H1 by lra.
                Rrewrite (vR - h / 2 - l /2 = vR - (h + l) / 2)%R.
                Rrewrite (1 * (h / 2 + l / 2 - l) = (h - l) / 2)%R.
                apply Rabs_Rle_condition; lra.
            }
            pose (noise := exist (fun x => -(1) <= x <= 1)%R noise_expression Hnoise).
            exists noise.
            unfold noise, noise_expression.
            simpl.
            field.
            intros Hnotz.
            field_simplify in Hnotz.
            assert (h = l) as Hz by lra.
            apply eqR_Qeq in Hz.
            lra.
          - intros Hmax.
            apply Qle_Rle in Hmax.
            repeat rewrite Q2R_minus.
            repeat rewrite Q2R_plus.
            repeat rewrite Q2R_div; try lra.
            replace (Q2R (2#1)) with 2%R by lra.
            repeat rewrite Q2R_minus in Hmax.
            repeat rewrite Q2R_plus in Hmax.
            rewrite Q2R_div in Hmax; try lra.
            rewrite Q2R_div in Hmax; try lra.
            replace (Q2R (2#1)) with 2%R in Hmax by lra.
            pose (l := (Q2R (fst (P n)))).
            pose (h := (Q2R (snd (P n)))).
            fold l h in Hmax, interval_containment |-*.
            pose (noise_expression := ((vR - h / 2 - l / 2) / (h / 2 + l / 2 - l))%R).
            assert (-(1) <= noise_expression <= 1)%R as Hnoise.
            {
              unfold noise_expression.
              apply Rabs_Rle_condition.
              destruct (Rle_lt_dec (h / 2 + l / 2 - l) 0)%R as [Hle0 | Hle0].
              - apply Rle_lt_or_eq_dec in Hle0; destruct Hle0 as [Hlt | Hlt];
                  try (field_simplify in Hlt; assert (h = l) as Hz by lra; apply eqR_Qeq in Hz; lra).
              - rewrite Rdiv_abs_le_bounds; try lra.
                assert (0 < h - l)%R as H1 by lra.
                Rrewrite (vR - h / 2 - l /2 = vR - (h + l) / 2)%R.
                Rrewrite (1 * (h / 2 + l / 2 - l) = (h - l) / 2)%R.
                apply Rabs_Rle_condition; lra.
            }
            pose (noise := exist (fun x => -(1) <= x <= 1)%R noise_expression Hnoise).
            exists noise.
            unfold noise, noise_expression.
            simpl.
            field.
            intros Hnotz.
            field_simplify in Hnotz.
            assert (h = l) as Hz by lra.
            apply eqR_Qeq in Hz.
            lra.
        }
790 791 792 793 794 795 796 797 798 799 800 801
        assert (forall n0 : nat, (n0 >= inoise + 1)%nat -> updMap map1 inoise q n0 = None).
        {
          intros n' Hn'.
          unfold updMap.
          destruct (n' =? inoise) eqn: Hneq.
          - apply beq_nat_true in Hneq.
            lia.
          - apply validmap1.
            lia.
        }
        assert (fresh (inoise + 1) (fromIntv (P n) inoise)) as Hfresh
            by (unfold fresh, fromIntv, get_max_index; rewrite Heq; simpl; lia).
802 803 804 805
        exists (updMap map1 inoise q), (fromIntv (P n) inoise), vR, aiv, aerr.
        repeat split; auto.
        -- apply contained_map_extension.
           apply validmap1; lia.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
806
        -- apply contained_flover_map_extension.
807
           assumption.
808
        -- lia.
809 810 811
        -- apply validAffineBounds_validRanges.
           exists (updMap map1 inoise q), (fromIntv (P n) inoise), vR, aiv, aerr.
           repeat split; auto.
812 813 814 815
        -- intros e Hnone Hsome.
           destruct Hsome as [afS Hsome].
           {
             destruct (FloverMapFacts.O.MO.eq_dec (Var Q n) e).
816 817
             - assert (Q_orderedExps.exprCompare e (Var Q n) = Eq)
                 by (now rewrite Q_orderedExps.exprCompare_eq_sym).
818
               rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
819 820 821 822
               inversion Hsome; subst; clear Hsome.
               unfold checked_expressions.
               exists (fromIntv (P n) inoise), vR, aiv, aerr.
               intuition.
823 824 825
               + rewrite usedVars_eq_compat; eauto.
                 set_tac.
                 left; set_tac; split; auto; subst; set_tac.
826 827
               + erewrite FloverMapFacts.P.F.find_o; eauto.
               + rewrite FloverMapFacts.P.F.add_eq_o; auto.
828
               + erewrite expr_compare_eq_eval_compat; eauto.
829 830 831 832 833
               + eapply validRanges_eq_compat; eauto.
                 simpl; split; auto.
                 apply validAffineBounds_validRanges.
                 exists (updMap map1 inoise q), (fromIntv (P n) inoise), vR, aiv, aerr.
                 repeat split; auto.
834 835 836
             - rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
               congruence.
           }
837
  - pose proof visitedExpr as visitedExpr'.
838
    unfold checked_expressions in visitedExpr.
839 840 841
    destruct (FloverMap.find (elt:=affine_form Q) (Const m v) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
842
      specialize (visitedExpr (Const m v)).
843
      destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
844
      - eexists; eauto.
845
      - exists map1, af, vR, aiv, aerr.
846
        intuition.
847
    }
848 849 850
    destruct (FloverMap.find (elt:=intv * error) (Const m v) A) eqn: Hares;
      simpl in validBounds; try congruence.
    destruct p as [i e].
851 852 853
    destruct (isSupersetIntv (v, v) i) eqn: Hsup; try congruence.
    assert (isSupersetIntv (v, v) i = true) as Hsup' by assumption.
    apply andb_prop in Hsup' as [L R].
Nikita Zyuzin's avatar
Nikita Zyuzin committed
854 855 856
    rewrite Qle_bool_iff in L, R.
    simpl ivlo in L, R.
    simpl ivhi in L, R.
857 858
    assert (fst i <= v) as L' by assumption.
    assert (v <= snd i) as R' by assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
859 860 861
    apply Qle_Rle in L.
    apply Qle_Rle in R.
    inversion validBounds; subst; clear validBounds.
862 863 864
    assert (isSupersetIntv (toIntv (fromIntv (v, v) noise)) i = true).
    {
      unfold fromIntv, toIntv.
865 866 867 868 869 870
      simpl.
      rewrite Qeq_bool_refl.
      apply andb_true_intro.
      split; rewrite Qle_bool_iff; simpl; field_simplify; Qrewrite ((2#1) * v * / (2#1) == v).
      * now Qrewrite (fst i / 1 == fst i).
      * now Qrewrite (snd i / 1 == snd i).
871 872 873 874
    }
    assert (fresh noise (fromIntv (v, v) noise))
      by (unfold fromIntv; simpl ivlo; simpl ivhi; rewrite Qeq_bool_refl;
          unfold fresh, get_max_index; rewrite get_max_index_aux_equation; lia).
875
    assert (af_evals (afQ2R (fromIntv (v, v) noise)) (perturb (Q2R v) REAL 0) map1).
876 877
    {
      unfold perturb.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
878 879 880 881 882
      unfold fromIntv.
      simpl.
      rewrite Qeq_bool_refl.
      simpl.
      rewrite Q2R_plus.
883 884 885
      rewrite Q2R_div; try lra.
      unfold af_evals, Ropt_eq; simpl.
      lra.
886 887 888
    }
    assert (FloverMap.find (elt:=affine_form Q) (Const m v) (FloverMap.add (Const m v) (fromIntv (v, v) noise) iexpmap) = Some (fromIntv (v, v) noise))
      by (rewrite FloverMapFacts.P.F.add_eq_o; try auto;
889
          apply Q_orderedExps.exprCompare_refl).
890
    assert (eval_expr E (toRMap Gamma) fBits (toREval (toRExp (Const m v))) (perturb (Q2R v) REAL 0) REAL)
891 892
      by (constructor; simpl; rewrite Rabs_R0; lra).
    exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
893 894 895 896
    repeat split; auto.
    + reflexivity.
    + apply contained_flover_map_extension.
      assumption.
897 898 899
    + apply validAffineBounds_validRanges.
      exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
      repeat split; auto.
900 901 902 903
    + intros e' Hnone Hsome.
      destruct Hsome as [afS Hsome].
      {
        destruct (FloverMapFacts.O.MO.eq_dec (Const m v) e').
904 905
        - assert (Q_orderedExps.exprCompare e' (Const m v) = Eq)
            by (now rewrite Q_orderedExps.exprCompare_eq_sym).
906
          rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
907 908
          inversion Hsome; subst; clear Hsome.
          unfold checked_expressions.
909
          exists (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
910
          intuition.
911 912 913
          + rewrite usedVars_eq_compat; eauto.
            simpl.
            set_tac.
914 915
          + erewrite FloverMapFacts.P.F.find_o; eauto.
          + rewrite FloverMapFacts.P.F.add_eq_o; auto.
916
          + erewrite expr_compare_eq_eval_compat; eauto.
917 918 919 920 921
          + eapply validRanges_eq_compat; eauto.
            simpl; split; auto.
            apply validAffineBounds_validRanges.
            exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
            repeat split; auto.
922 923 924 925
        - rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
          congruence.
      }
  - destruct (FloverMap.find (elt:=affine_form Q) (Unop u e) iexpmap) eqn: Hvisited.
926
    {
927
      pose proof visitedExpr as visitedExpr'.
928
      inversion validBounds; subst; clear validBounds.
929
      specialize (visitedExpr (Unop u e)).
930
      destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
931
      - eexists; eauto.
932
      - exists map1, af, vR, aiv, aerr.
933
        intuition.
934
    }
935
    unfold updateExpMap, updateExpMapSucc, updateExpMapIncr in validBounds.
936
    destruct (FloverMap.find (elt:=intv * error) (Unop u e) A) as [p |] eqn: Hares; simpl in validBounds; try congruence.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
937
    destruct p as [aiv aerr].
938
    destruct (validAffineBounds e A P dVars iexpmap inoise) eqn: Hsubvalid; simpl in validBounds; try congruence.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
939
    destruct p as [subexprAff subnoise].
940
    destruct (FloverMap.find (elt:=affine_form Q) e subexprAff) as [af |] eqn: He; simpl in validBounds; try congruence.
941
    destruct (IHe subnoise subexprAff inoise iexpmap map1)
942
      as [ihmap [af' [vR [subaiv [subaerr [Hcont [Hcontf [Hsubares [Hsubsup [Haf [subfresh [Hsubvalidmap [Hsubnoise [subeval [subranges [subaff HvisitedExpr]]]]]]]]]]]]]]]];
943
      auto; clear IHe.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
944 945 946
    assert (af' = af) by congruence; subst.
    destruct u.
    + destruct (isSupersetIntv (toIntv (AffineArithQ.negate_aff af)) aiv) eqn: Hsup; try congruence.
947
      exists ihmap, (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
948 949
      assert (af_evals (afQ2R (AffineArithQ.negate_aff af)) (- vR) ihmap) as ?
          by (rewrite afQ2R_negate_aff; now apply negate_aff_sound).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
950
      inversion validBounds; subst; clear validBounds.
951 952 953
      rewrite plus_0_r.
      assert (fresh subnoise (AffineArithQ.negate_aff af)) by
        (unfold AffineArithQ.negate_aff; now apply AffineArithQ.fresh_mult_aff_const).
954
      repeat split; auto.
955 956
      * pose proof contained_flover_map_extension as H'.
        specialize (H' _ iexpmap _ (AffineArithQ.negate_aff af) Hvisited).
957 958 959
        etransitivity; try eassumption.
        apply contained_flover_map_add_compat.
        assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
960
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
961
        apply Q_orderedExps.exprCompare_refl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
962
      * now constructor.
963 964 965 966
      * apply validAffineBounds_validRanges.
        exists ihmap, (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
        repeat split; auto.
        now constructor.
967 968 969 970
      * intros e' Hnone Hsome.
        destruct Hsome as [afS Hsome].
        {
          destruct (FloverMapFacts.O.MO.eq_dec (Unop Neg e) e').
971 972
          - assert (Q_orderedExps.exprCompare e' (Unop Neg e) = Eq)
              by (now rewrite Q_orderedExps.exprCompare_eq_sym).
973
            rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
974 975 976 977
            inversion Hsome; subst; clear Hsome.
            unfold checked_expressions.
            exists (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
            intuition.
978 979 980
            + rewrite usedVars_eq_compat; eauto.
              simpl in varsDisjoint |-*; auto.
              now apply subset_union.
981 982
            + erewrite FloverMapFacts.P.F.find_o; eauto.
            + rewrite FloverMapFacts.P.F.add_eq_o; auto.
983
            + erewrite expr_compare_eq_eval_compat; eauto.
984
              now constructor.
985 986 987 988 989 990
            + eapply validRanges_eq_compat; eauto.
              simpl; split; auto.
              apply validAffineBounds_validRanges.
              exists ihmap, (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
              repeat split; auto.
              now constructor.
991 992 993 994
          - rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
            apply checked_expressions_flover_map_add_compat; auto.
            apply HvisitedExpr; eauto.
        }
Nikita Zyuzin's avatar
Nikita Zyuzin committed
995 996