AffineValidation.v 115 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
From Coq
     Require Import QArith.QArith QArith.Qreals QArith.Qminmax Lists.List
     micromega.Psatz Recdef.

From Flover.Infra
     Require Import Abbrevs RationalSimps RealRationalProps Ltacs RealSimps.

From Flover
     Require Import TypeValidator ssaPrgs AffineForm AffineArithQ AffineArith
     IntervalValidation RealRangeArith.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
11

Nikita Zyuzin's avatar
Nikita Zyuzin committed
12
Definition updateExpMapIncr e new_af noise (emap: expressionsAffine) intv incr :=
Nikita Zyuzin's avatar
Nikita Zyuzin committed
13 14 15 16 17 18 19 20 21 22 23
  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
24 25 26 27
Definition nozeroiv iv :=
  ((Qleb (ivhi iv) 0) && (negb (Qeq_bool (ivhi iv) 0))) ||
            ((Qleb 0 (ivlo iv)) && (negb (Qeq_bool (ivlo iv) 0))).

28
Fixpoint validAffineBounds (e: expr Q) (A: analysisResult) (P: precond) (validVars: NatSet.t)
Nikita Zyuzin's avatar
Nikita Zyuzin committed
29
           (exprsAf: expressionsAffine) (currentMaxNoise: nat): option (expressionsAffine * nat) :=
30 31 32 33 34 35 36
  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 *)
37 38
    olet ares := FloverMap.find e A in
    let (intv, _) := ares in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
39
    match e with
Nikita Zyuzin's avatar
Nikita Zyuzin committed
40
    | Var _ v =>
41 42 43 44 45 46 47
      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
48
    | Const _ c => if isSupersetIntv (c, c) intv then
Nikita Zyuzin's avatar
Nikita Zyuzin committed
49
                    let af := fromIntv (c,c) currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
50 51 52
                    Some (FloverMap.add e af exprsAf, currentMaxNoise)
                  else None
    | Unop o e' =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
53
      olet valid := validAffineBounds e' A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
54 55
      let (exprsAf', n') := valid in
      olet af := FloverMap.find e' exprsAf' in
56
      match o with
Nikita Zyuzin's avatar
Nikita Zyuzin committed
57
      | Neg =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
58
        updateExpMap e (AffineArithQ.negate_aff af) n' exprsAf' intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
59
      | Inv =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
60 61 62 63 64
        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
65 66
      end
    | Binop o e1 e2 =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
67
      olet valid1 := validAffineBounds e1 A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
68 69
      let (exprsAf1, n1) := valid1 in
      olet af1 := FloverMap.find e1 exprsAf1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
70
      olet valid2 := validAffineBounds e2 A P validVars exprsAf1 n1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
71 72 73 74
      let (exprsAf2, n2) := valid2 in
      olet af2 := FloverMap.find e2 exprsAf2 in
      match o with
      | Plus =>
75
        updateExpMap e (AffineArithQ.plus_aff af1 af2) n2 exprsAf2 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
76
      | Sub =>
77
        updateExpMap e (AffineArithQ.subtract_aff af1 af2) n2 exprsAf2 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
78
      | Mult =>
79
        updateExpMapSucc e (AffineArithQ.mult_aff af1 af2 n2) n2 exprsAf2 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
80
      | Div =>
81 82
          olet ares2 := FloverMap.find e2 A in
          let (aiv2, _) := ares2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
83
          let iv2 := toIntv af2 in
84
          if nozeroiv iv2 && nozeroiv aiv2
Nikita Zyuzin's avatar
Nikita Zyuzin committed
85
          then
86
            updateExpMapIncr e (AffineArithQ.divide_aff af1 af2 n2) n2 exprsAf2 intv 2
Nikita Zyuzin's avatar
Nikita Zyuzin committed
87 88 89
          else None
      end
    | Fma e1 e2 e3 =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
90
      olet valid1 := validAffineBounds e1 A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
91 92
      let (exprsAf1, n1) := valid1 in
      olet af1 := FloverMap.find e1 exprsAf1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
93
      olet valid2 := validAffineBounds e2 A P validVars exprsAf1 n1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
94 95
      let (exprsAf2, n2) := valid2 in
      olet af2 := FloverMap.find e2 exprsAf2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
96
      olet valid3 := validAffineBounds e3 A P validVars exprsAf2 n2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
97 98
      let (exprsAf3, n3) := valid3 in
      olet af3 := FloverMap.find e3 exprsAf3 in
99
        updateExpMapSucc e (AffineArithQ.plus_aff af1 (AffineArithQ.mult_aff af2 af3 n3)) n3 exprsAf3 intv
100
    | Downcast _ e' =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
101
      olet valid' := validAffineBounds e' A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
102 103 104 105 106
      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
107
        Some (FloverMap.add e af' exprsAf', n')
Nikita Zyuzin's avatar
Nikita Zyuzin committed
108
      else None
109
    end
Nikita Zyuzin's avatar
Nikita Zyuzin committed
110
  end.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
111

Nikita Zyuzin's avatar
Nikita Zyuzin committed
112 113 114 115 116
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.

117 118 119 120 121 122
Lemma afQ2R_const v:
  afQ2R (AffineForm.Const v) = AffineForm.Const (Q2R v).
Proof.
  trivial.
Qed.

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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.

139 140 141 142 143 144 145
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.

146 147 148 149 150 151 152 153 154 155
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.
156

Nikita Zyuzin's avatar
Nikita Zyuzin committed
157 158 159 160 161 162 163
Lemma to_interval_to_intv_iv a:
  (Q2RP (toIntv a)) = toInterval (afQ2R a).
Proof.
  rewrite <- to_interval_to_intv.
  reflexivity.
Qed.

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
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
192 193
Lemma afQ2R_mult_aff_aux af1 af2:
  afQ2R (AffineArithQ.mult_aff_aux (af1, af2)) = mult_aff_aux (afQ2R af1, afQ2R af2).
194 195 196 197 198 199 200
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
201 202 203 204 205 206
  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.
207
    simpl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    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.
247
      simpl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
248 249
      destruct Req_dec_sum as [Heq' | Heq'];
        try (apply Rmult_integral in Heq'; destruct Heq'; try lra).
250
      f_equal.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
251 252
      * rewrite Q2R_mult.
        do 2 rewrite afQ2R_radius.
253
        reflexivity.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
254 255
      * apply afQ2R_mult_aff_aux.
Qed.
256

Nikita Zyuzin's avatar
Nikita Zyuzin committed
257 258 259
Lemma afQ2R_negate_aff af:
  afQ2R (AffineArithQ.negate_aff af) = negate_aff (afQ2R af).
Proof.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  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
284
Lemma afQ2R_inverse_aff af n:
285
  above_zero (afQ2R af) \/ below_zero (afQ2R af) ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
286 287
  afQ2R (AffineArithQ.inverse_aff af n) = inverse_aff (afQ2R af) n.
Proof.
288
  intros above_below.
289 290
  unfold AffineArithQ.inverse_aff.
  unfold inverse_aff.
291 292 293
  unfold above_zero, below_zero in above_below.
  unfold ivhi, IVhi, ivlo, IVlo in *.
  rewrite <- to_interval_to_intv in above_below.
294
  rewrite <- to_interval_to_intv.
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 353 354 355 356 357 358 359 360 361 362 363
  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.
364 365
    apply Qlt_Rlt in Heq.
    destruct (Rlt_dec (Q2R (get_const af + AffineArithQ.radius af)) 0); try lra.
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 397 398 399 400 401 402 403 404 405 406 407
    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.
    }
408
    simpl.
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    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
423

424
Lemma afQ2R_divide_aff af1 af2 n:
425
  above_zero (afQ2R af2) \/ below_zero (afQ2R af2) ->
426
  afQ2R (AffineArithQ.divide_aff af1 af2 n) = divide_aff (afQ2R af1) (afQ2R af2) n.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
427
Proof.
428
  intros.
429 430
  unfold AffineArithQ.divide_aff.
  rewrite afQ2R_mult_aff.
431
  rewrite afQ2R_inverse_aff; auto.
432
Qed.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
433

Nikita Zyuzin's avatar
Nikita Zyuzin committed
434
Lemma afQ2R_fresh n a:
Nikita Zyuzin's avatar
Nikita Zyuzin committed
435
  fresh n a <-> fresh n (afQ2R a).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
436
Proof.
437
  split; induction a; intros *.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
438 439 440 441 442 443 444
  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
445 446
Qed.

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
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.

Nikita Zyuzin's avatar
Nikita Zyuzin committed
479 480 481
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,
482 483 484
         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
485
         (forall n, (n >= noise)%nat -> map1 n = None) /\
486 487
         FloverMap.find (Var Q v) A = Some (iv, err) /\
         E v = Some vR /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
488
         af_evals (afQ2R af) vR map1.
489

490
Lemma validAffineBounds_validRanges e (A: analysisResult) E Gamma:
491 492 493
  (exists map af vR aiv aerr,
      FloverMap.find e A = Some (aiv, aerr) /\
      isSupersetIntv (toIntv af) aiv = true /\
494
      eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp e)) vR REAL /\
495 496 497
      af_evals (afQ2R af) vR map) ->
  exists iv err vR,
    FloverMap.find e A = Some (iv, err) /\
498
    eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp e)) vR REAL /\
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    (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.

518 519
Definition checked_expressions (A: analysisResult) E Gamma fVars dVars e iexpmap
           inoise map1 :=
520
  exists af vR aiv aerr,
521
    NatSet.Subset (usedVars e) (NatSet.union fVars dVars) /\
522 523 524 525 526
    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) /\
527
    eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp e)) vR REAL /\
528
    validRanges e A E (toRTMap (toRExpMap Gamma)) /\
529 530
    af_evals (afQ2R af) vR map1.

531 532
Lemma checked_expressions_contained A E Gamma fVars dVars e expmap1 expmap2 map1
      map2 noise1 noise2:
533 534 535 536
  contained_map map1 map2 ->
  contained_flover_map expmap1 expmap2 ->
  (noise2 >= noise1)%nat ->
  (forall n : nat, (n >= noise2)%nat -> map2 n = None) ->
537 538
  checked_expressions A E Gamma fVars dVars e expmap1 noise1 map1 ->
  checked_expressions A E Gamma fVars dVars e expmap2 noise2 map2.
539 540 541 542 543 544 545 546
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.

547 548
Lemma checked_expressions_flover_map_add_compat A E Gamma fVars dVars e e' af
      expmap noise map:
549
  Q_orderedExps.exprCompare e e' <> Eq ->
550 551
  checked_expressions A E Gamma fVars dVars e' expmap noise map ->
  checked_expressions A E Gamma fVars dVars e' (FloverMap.add e af expmap) noise map.
552 553 554 555 556 557 558 559
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
560

Nikita Zyuzin's avatar
Nikita Zyuzin committed
561 562
Lemma af_evals_afQ2R_from_intv_updMap iv noise map vR:
  (Q2R (fst iv) <= vR <= Q2R (snd iv))%R ->
563
  exists q : noise_type,
Nikita Zyuzin's avatar
Nikita Zyuzin committed
564
    af_evals (afQ2R (fromIntv iv noise)) vR (updMap map noise q).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
565
Proof.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  intros interval_containment.
  destruct (Qeq_bool (ivlo iv) (ivhi iv)) eqn: Heq.
  {
    exists noise_zero.
    unfold af_evals, fromIntv.
    rewrite Heq.
    cbn.
    rewrite Qeq_bool_iff in Heq.
    apply Qeq_eqR in Heq.
    rewrite Q2R_plus.
    rewrite Q2R_div; [|lra].
    rewrite Q2R_div; [|lra].
    field_rewrite ((Q2R (2 # 1)) = 2%R).
    cbn in Heq.
    rewrite Heq.
    rewrite Heq in interval_containment.
    lra.
  }
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  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.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
607 608
    pose (l := (Q2R (fst iv))).
    pose (h := (Q2R (snd iv))).
609 610 611
    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.
612
    {
613 614 615 616 617 618 619
      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.
620 621
        field_rewrite (vR - h / 2 - l /2 = vR - (h + l) / 2)%R.
        field_rewrite (1 * (h / 2 + l / 2 - l) = (h - l) / 2)%R.
622
        apply Rabs_Rle_condition; lra.
623
    }
Nikita Zyuzin's avatar
Nikita Zyuzin committed
624
    rename noise into inoise.
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    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.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
646 647
    pose (l := (Q2R (fst iv))).
    pose (h := (Q2R (snd iv))).
648 649 650
    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.
651
    {
652 653 654 655 656 657 658
      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.
659 660
        field_rewrite (vR - h / 2 - l /2 = vR - (h + l) / 2)%R.
        field_rewrite (1 * (h / 2 + l / 2 - l) = (h - l) / 2)%R.
661
        apply Rabs_Rle_condition; lra.
662
    }
Nikita Zyuzin's avatar
Nikita Zyuzin committed
663
    rename noise into inoise.
664 665 666 667 668 669 670 671 672 673 674 675
    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.
Qed.

676
Lemma validAffineBounds_sound_var A P E Gamma fVars dVars n:
677 678 679 680
  forall (noise : nat) (exprAfs : expressionsAffine) (inoise : nat)
    (iexpmap : FloverMap.t (affine_form Q)) (map1 : nat -> option noise_type),
    (forall e : FloverMap.key,
        (exists af : affine_form Q, FloverMap.find (elt:=affine_form Q) e iexpmap = Some af) ->
681
        checked_expressions A E Gamma fVars dVars e iexpmap inoise map1) ->
682 683 684 685 686 687
    (inoise > 0)%nat ->
    (forall n0 : nat, (n0 >= inoise)%nat -> map1 n0 = None) ->
    validAffineBounds (Var Q n) A P dVars iexpmap inoise = Some (exprAfs, noise) ->
    affine_dVars_range_valid dVars E A inoise iexpmap map1 ->
    NatSet.Subset (usedVars (Var Q n) -- dVars) fVars ->
    fVars_P_sound fVars E P ->
688
    validTypes (Var Q n) Gamma DeltaMapR ->
689
    exists (map2 : noise_mapping) (af : affine_form Q) (vR : R) (aiv : intv)
690 691 692 693 694 695 696 697 698
      (aerr : error),
      contained_map map1 map2 /\
      contained_flover_map iexpmap exprAfs /\
      FloverMap.find (elt:=intv * error) (Var Q n) A = Some (aiv, aerr) /\
      isSupersetIntv (toIntv af) aiv = true /\
      FloverMap.find (elt:=affine_form Q) (Var Q n) exprAfs = Some af /\
      fresh noise af /\
      (forall n0 : nat, (n0 >= noise)%nat -> map2 n0 = None) /\
      (noise >= inoise)%nat /\
699
      eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp (Var Q n))) vR REAL /\
700
      validRanges (Var Q n) A E (toRTMap (toRExpMap Gamma)) /\
701 702 703 704
      af_evals (afQ2R af) vR map2 /\
      (forall e : FloverMap.key,
          FloverMap.find (elt:=affine_form Q) e iexpmap = None ->
          (exists af0 : affine_form Q, FloverMap.find (elt:=affine_form Q) e exprAfs = Some af0) ->
705
          checked_expressions A E Gamma fVars dVars e exprAfs noise map2).
706 707 708 709 710
Proof.
  intros * visitedExpr inoisegtz validmap1 validBounds dVarsValid varsDisjoint fVarsSound varsTyped;
    simpl in validBounds.
  specialize (dVarsValid n).
  specialize (fVarsSound n).
711
  destruct (varsTyped) as [mV [find_mv [_ valid_exec]]].
712 713 714
  pose proof visitedExpr as visitedExpr'.
  destruct (FloverMap.find (elt:=affine_form Q) (Var Q n) iexpmap) eqn: Hvisited.
  {
Nikita Zyuzin's avatar
Nikita Zyuzin committed
715
    inversion validBounds; subst; clear validBounds.
716 717
    specialize (visitedExpr (Var Q n)).
    assert (NatSet.Subset (usedVars (Var Q n)) (fVars  dVars)).
718
    {
719 720 721 722 723 724
      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.
725
    }
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]]; eauto.
    exists map1, af, vR, aiv, aerr.
    intuition.
  }
  destruct (FloverMap.find (elt:=intv * error) (Var Q n) A) as [p |] eqn: Hares; simpl in validBounds; try congruence.
  destruct p as [aiv aerr].
  destruct (n mem dVars) eqn: Hmem.
  - rewrite NatSet.mem_spec in Hmem.
    specialize (dVarsValid Hmem).
    assert (n  fVars  dVars) as H by intuition.
    destruct dVarsValid as [af [vR [iv [err dVarsValid]]]]; try reflexivity.
    inversion validBounds; subst; clear validBounds.
    exists map1, af, vR, iv, err.
    intuition; try congruence.
  - destruct (isSupersetIntv (toIntv (fromIntv (P n) inoise)) aiv) eqn: Hsup; try congruence.
    inversion validBounds; subst; clear validBounds.
    apply not_in_not_mem in Hmem.
    assert (n  fVars  dVars) as H by intuition.
    assert (n  fVars) as H' by intuition.
    specialize (fVarsSound H') as [vR [eMap interval_containment]].
    assert (FloverMap.find (Var Q n) (FloverMap.add (Var Q n) (fromIntv (P n) inoise) iexpmap) = Some (fromIntv (P n) inoise)) as Hfind
        by (rewrite FloverMapFacts.P.F.add_eq_o; try auto; apply Q_orderedExps.exprCompare_refl).
748 749
    assert (eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR
                      (toREval (toRExp (Var Q n))) vR REAL) as Heeval.
750 751 752 753 754 755
    { eapply Var_load; try eauto.
      unfold toRTMap.
      assert (exists m, toRExpMap Gamma (Var R n) = Some m) as t_var.
      { eexists; eapply toRExpMap_some with (e:= Var Q n); eauto. }
      destruct t_var as(? & t_var).
      rewrite t_var; auto. }
756 757
    destruct (Qeq_bool (ivlo (P n)) (ivhi (P n))) eqn: Heq.
    + assert (af_evals (afQ2R (fromIntv (P n) inoise)) vR map1) as Hevals.
758
      {
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
        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.
775
      }
776 777 778
      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.
779
      repeat split; try auto.
780 781 782 783 784 785
      * reflexivity.
      * apply contained_flover_map_extension; assumption.
      * intros n' Hn'.
        apply validmap1.
        lia.
      * lia.
786
      * apply validAffineBounds_validRanges.
787
        exists map1, (fromIntv (P n) inoise), vR, aiv, aerr.
788
        repeat split; auto.
789
      * intros e Hnone Hsome.
790 791
        destruct Hsome as [afS Hsome].
        {
792 793
          destruct (FloverMapFacts.O.MO.eq_dec (Var Q n) e).
          - assert (Q_orderedExps.exprCompare e (Var Q n) = Eq)
794
              by (now rewrite Q_orderedExps.exprCompare_eq_sym).
795
            rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
796 797
            inversion Hsome; subst; clear Hsome.
            unfold checked_expressions.
798
            exists (fromIntv (P n) inoise), vR, aiv, aerr.
799
            intuition.
800
            + rewrite usedVars_eq_compat; eauto.
801 802
              set_tac.
              left; set_tac; split; auto; subst; set_tac.
803 804
            + erewrite FloverMapFacts.P.F.find_o; eauto.
            + rewrite FloverMapFacts.P.F.add_eq_o; auto.
805
            + erewrite expr_compare_eq_eval_compat; eauto.
806 807 808
            + eapply validRanges_eq_compat; eauto.
              simpl; split; auto.
              apply validAffineBounds_validRanges.
809
              exists map1, (fromIntv (P n) inoise), vR, aiv, aerr.
810
              repeat split; auto.
811
          - rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
812
            congruence.
813
        }
814 815 816
    + assert (exists q, af_evals (afQ2R (fromIntv (P n) inoise)) vR (updMap map1 inoise q))
        as [q Hevals] by (apply af_evals_afQ2R_from_intv_updMap; auto).
      assert (forall n0 : nat, (n0 >= inoise + 1)%nat -> updMap map1 inoise q n0 = None).
817
      {
818 819 820 821 822 823 824
        intros n' Hn'.
        unfold updMap.
        destruct (n' =? inoise) eqn: Hneq.
        - apply beq_nat_true in Hneq.
          lia.
        - apply validmap1.
          lia.
825
      }
826 827 828 829 830 831 832 833
      assert (fresh (inoise + 1) (fromIntv (P n) inoise)) as Hfresh
          by (unfold fresh, fromIntv, get_max_index; rewrite Heq; simpl; lia).
      exists (updMap map1 inoise q), (fromIntv (P n) inoise), vR, aiv, aerr.
      repeat split; auto.
      * apply contained_map_extension.
        apply validmap1; lia.
      * apply contained_flover_map_extension.
        assumption.
834
      * lia.
835
      * apply validAffineBounds_validRanges.
836
        exists (updMap map1 inoise q), (fromIntv (P n) inoise), vR, aiv, aerr.
837
        repeat split; auto.
838
      * intros e Hnone Hsome.
839 840
        destruct Hsome as [afS Hsome].
        {
841 842
          destruct (FloverMapFacts.O.MO.eq_dec (Var Q n) e).
          - assert (Q_orderedExps.exprCompare e (Var Q n) = Eq)
843
              by (now rewrite Q_orderedExps.exprCompare_eq_sym).
844 845 846
            rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
            inversion Hsome; subst; clear Hsome.
            unfold checked_expressions.
847 848
            exists (fromIntv (P n) inoise), vR, aiv, aerr.
            intuition.
849
            + rewrite usedVars_eq_compat; eauto.
850 851 852
              set_tac.
              left; set_tac; split; auto; subst; set_tac.
            + erewrite FloverMapFacts.P.F.find_o; eauto.
853
            + rewrite FloverMapFacts.P.F.add_eq_o; auto.
854
            + erewrite expr_compare_eq_eval_compat; eauto.
855
            + eapply validRanges_eq_compat; eauto.
856
              simpl; split; auto.
857
              apply validAffineBounds_validRanges.
858
              exists (updMap map1 inoise q), (fromIntv (P n) inoise), vR, aiv, aerr.
859
              repeat split; auto.
860
          - rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
861
            congruence.
862
        }
863 864
Qed.

865
Lemma validAffineBounds_sound_const A P E Gamma fVars dVars m v:
866 867 868 869
  forall (noise : nat) (exprAfs : expressionsAffine) (inoise : nat)
    (iexpmap : FloverMap.t (affine_form Q)) (map1 : nat -> option noise_type),
    (forall e : FloverMap.key,
        (exists af : affine_form Q, FloverMap.find (elt:=affine_form Q) e iexpmap = Some af) ->
870
        checked_expressions A E Gamma fVars dVars e iexpmap inoise map1) ->
871 872 873 874 875 876
    (inoise > 0)%nat ->
    (forall n : nat, (n >= inoise)%nat -> map1 n = None) ->
    validAffineBounds (Const m v) A P dVars iexpmap inoise = Some (exprAfs, noise) ->
    affine_dVars_range_valid dVars E A inoise iexpmap map1 ->
    NatSet.Subset (usedVars (Const m v) -- dVars) fVars ->
    fVars_P_sound fVars E P ->
877
    validTypes (Const m v) Gamma DeltaMapR ->
878
    exists (map2 : noise_mapping) (af : affine_form Q) (vR : R) (aiv : intv)
879 880 881 882 883 884 885 886 887
      (aerr : error),
      contained_map map1 map2 /\
      contained_flover_map iexpmap exprAfs /\
      FloverMap.find (elt:=intv * error) (Const m v) A = Some (aiv, aerr) /\
      isSupersetIntv (toIntv af) aiv = true /\
      FloverMap.find (elt:=affine_form Q) (Const m v) exprAfs = Some af /\
      fresh noise af /\
      (forall n : nat, (n >= noise)%nat -> map2 n = None) /\
      (noise >= inoise)%nat /\
888
      eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp (Const m v))) vR REAL /\
889
      validRanges (Const m v) A E (toRTMap (toRExpMap Gamma)) /\
890 891 892 893
      af_evals (afQ2R af) vR map2 /\
      (forall e : FloverMap.key,
          FloverMap.find (elt:=affine_form Q) e iexpmap = None ->
          (exists af0 : affine_form Q, FloverMap.find (elt:=affine_form Q) e exprAfs = Some af0) ->
894
          checked_expressions A E Gamma fVars dVars e exprAfs noise map2).
895 896 897 898 899 900 901 902 903 904 905 906 907 908
Proof.
  intros * visitedExpr inoisegtz validmap1 validBounds dVarsValid varsDisjoint fVarsSound varsTyped;
    simpl in validBounds.
  pose proof visitedExpr as visitedExpr'.
  unfold checked_expressions in visitedExpr.
  destruct (FloverMap.find (elt:=affine_form Q) (Const m v) iexpmap) eqn: Hvisited.
  {
    inversion validBounds; subst; clear validBounds.
    specialize (visitedExpr (Const m v)).
    destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
    - eexists; eauto.
    - exists map1, af, vR, aiv, aerr.
      intuition.
  }
909 910
  destruct varsTyped as (mt & find_t & t_eq & valid_exec); subst.
  rename mt into m.
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
  destruct (FloverMap.find (elt:=intv * error) (Const m v) A) eqn: Hares;
    simpl in validBounds; try congruence.
  destruct p as [i e].
  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].
  rewrite Qle_bool_iff in L, R.
  simpl ivlo in L, R.
  simpl ivhi in L, R.
  assert (fst i <= v) as L' by assumption.
  assert (v <= snd i) as R' by assumption.
  apply Qle_Rle in L.
  apply Qle_Rle in R.
  inversion validBounds; subst; clear validBounds.
  assert (isSupersetIntv (toIntv (fromIntv (v, v) noise)) i = true).
  {
    unfold fromIntv, toIntv.
    simpl.
    rewrite Qeq_bool_refl.
    apply andb_true_intro.
931 932 933
    split; rewrite Qle_bool_iff; simpl; field_simplify; field_rewrite ((2#1) * v * / (2#1) == v).
    * now field_rewrite (fst i / 1 == fst i).
    * now field_rewrite (snd i / 1 == snd i).
934 935 936 937 938 939 940 941 942 943 944 945 946
  }
  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).
  assert (af_evals (afQ2R (fromIntv (v, v) noise)) (perturb (Q2R v) REAL 0) map1).
  {
    unfold perturb.
    unfold fromIntv.
    simpl.
    rewrite Qeq_bool_refl.
    simpl.
    rewrite Q2R_plus.
    rewrite Q2R_div; try lra.
947
    unfold af_evals; simpl.
948 949 950 951 952
    lra.
  }
  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;
        apply Q_orderedExps.exprCompare_refl).
953
  assert (eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp (Const m v)))
954
                    (perturb (Q2R v) REAL 0) REAL)
955
    by (unfold DeltaMapR; constructor; simpl; auto; rewrite Rabs_R0; lra).
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
  exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
  repeat split; auto.
  - reflexivity.
  - apply contained_flover_map_extension.
    assumption.
  - apply validAffineBounds_validRanges.
    exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
    repeat split; auto.
  - intros e' Hnone Hsome.
    destruct Hsome as [afS Hsome].
    destruct (FloverMapFacts.O.MO.eq_dec (Const m v) e').
    + assert (Q_orderedExps.exprCompare e' (Const m v) = Eq)
        by (now rewrite Q_orderedExps.exprCompare_eq_sym).
      rewrite FloverMapFacts.P.F.add_eq_o in Hsome; auto.
      inversion Hsome; subst; clear Hsome.
      unfold checked_expressions.
      exists (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
      intuition.
      * rewrite usedVars_eq_compat; eauto.
        simpl.
        set_tac.
      * erewrite FloverMapFacts.P.F.find_o; eauto.
      * rewrite FloverMapFacts.P.F.add_eq_o; auto.
      * erewrite expr_compare_eq_eval_compat; eauto.
      * eapply validRanges_eq_compat; eauto.
        simpl; split; auto.
        apply validAffineBounds_validRanges.
        exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) REAL 0), i, e.
984
        repeat split; auto.
985 986 987 988
    + rewrite FloverMapFacts.P.F.add_neq_o in Hsome; auto.
      congruence.
Qed.

989
Definition validAffineBounds_IH_e A P E Gamma fVars dVars e :=
990 991 992 993
  forall (noise : nat) (exprAfs : expressionsAffine) (inoise : nat)
    (iexpmap : FloverMap.t (affine_form Q)) (map1 : nat -> option noise_type),
    (forall e : FloverMap.key,
        (exists af : affine_form Q, FloverMap.find (elt:=affine_form Q) e iexpmap = Some af) ->
994
        checked_expressions A E Gamma fVars dVars e iexpmap inoise map1) ->
995 996 997 998 999 1000
    (inoise > 0)%nat ->
    (forall n : nat, (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 (usedVars e -- dVars) fVars ->
    fVars_P_sound fVars E P ->
1001
    validTypes e Gamma DeltaMapR ->
1002
    exists (map2 : noise_mapping) (af : affine_form Q) (vR : R) (aiv : intv)
1003 1004 1005 1006 1007 1008 1009 1010 1011
      (aerr : error),
      contained_map map1 map2 /\
      contained_flover_map iexpmap exprAfs /\
      FloverMap.find (elt:=intv * error) e A = Some (aiv, aerr) /\
      isSupersetIntv (toIntv af) aiv = true /\
      FloverMap.find (elt:=affine_form Q) e exprAfs = Some af /\
      fresh noise af /\
      (forall n : nat, (n >= noise)%nat -> map2 n = None) /\
      (noise >= inoise)%nat /\
1012
      eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp e)) vR REAL /\
1013
      validRanges e A E (toRTMap (toRExpMap Gamma)) /\
1014 1015 1016 1017
      af_evals (afQ2R af) vR map2 /\
      (forall e : FloverMap.key,
          FloverMap.find (elt:=affine_form Q) e iexpmap = None ->
          (exists af0 : affine_form Q, FloverMap.find (elt:=affine_form Q) e exprAfs = Some af0) ->
1018
          checked_expressions A E Gamma fVars dVars e exprAfs noise map2).
1019

1020 1021
Lemma validAffineBounds_sound_unop A P E Gamma fVars dVars u e:
  validAffineBounds_IH_e A P E Gamma fVars dVars e ->
1022 1023 1024 1025
  forall (noise : nat) (exprAfs : expressionsAffine) (inoise : nat)
    (iexpmap : FloverMap.t (affine_form Q)) (map1 : nat -> option noise_type),
  (forall e0 : FloverMap.key,
   (exists af : affine_form Q, FloverMap.find (elt:=affine_form Q) e0 iexpmap = Some af) ->
1026
   checked_expressions A E Gamma fVars dVars e0 iexpmap inoise map1) ->
1027 1028 1029 1030 1031 1032
  (inoise > 0)%nat ->
  (forall n : nat, (n >= inoise)%nat -> map1 n = None) ->
  validAffineBounds (Unop u e) A P dVars iexpmap inoise = Some (exprAfs, noise) ->
  affine_dVars_range_valid dVars E A inoise iexpmap map1 ->
  NatSet.Subset (usedVars (Unop u e) -- dVars) fVars ->
  fVars_P_sound fVars E P ->
1033
  validTypes (Unop u e) Gamma DeltaMapR ->
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  exists (map2 : noise_mapping) (af : affine_form Q) (vR : R) (aiv : intv)
  (aerr : error),
    contained_map map1 map2 /\
    contained_flover_map iexpmap exprAfs /\
    FloverMap.find (elt:=intv * error) (Unop u e) A = Some (aiv, aerr) /\
    isSupersetIntv (toIntv af) aiv = true /\
    FloverMap.find (elt:=affine_form Q) (Unop u e) exprAfs = Some af /\
    fresh noise af /\
    (forall n : nat, (n >= noise)%nat -> map2 n = None) /\
    (noise >= inoise)%nat /\
1044
    eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR (toREval (toRExp (Unop u e))) vR REAL /\
1045
    validRanges (Unop u e) A E (toRTMap (toRExpMap Gamma)) /\
1046 1047 1048 1049
    af_evals (afQ2R af) vR map2 /\
    (forall e0 : FloverMap.key,
     FloverMap.find (elt:=affine_form Q) e0 iexpmap = None ->
     (exists af0 : affine_form Q, FloverMap.find (elt:=affine_form Q) e0 exprAfs = Some af0) ->
1050
     checked_expressions A E Gamma fVars dVars e0 exprAfs noise map2).
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
Proof.
  intros IHe * visitedExpr inoisegtz validmap1 validBounds dVarsValid varsDisjoint fVarsSound varsTyped;
    simpl in validBounds.
  destruct (FloverMap.find (elt:=affine_form Q) (Unop u e) iexpmap) eqn: Hvisited.
  {
    pose proof visitedExpr as visitedExpr'.
    inversion validBounds; subst; clear validBounds.
    specialize (visitedExpr (Unop u e)).
    destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
    - eexists; eauto.
    - exists map1, af, vR, aiv, aerr.
      intuition.
  }
  unfold updateExpMap, updateExpMapSucc, updateExpMapIncr in validBounds.
  destruct (FloverMap.find (elt:=intv * error) (Unop u e) A) as [p |] eqn: Hares; simpl in validBounds; try congruence.
  destruct p as [aiv aerr].
  destruct (validAffineBounds e A P dVars iexpmap inoise) eqn: Hsubvalid; simpl in validBounds; try congruence.
  destruct p as [subexprAff subnoise].
  destruct (FloverMap.find (elt:=affine_form Q) e subexprAff) as [af |] eqn: He; simpl in validBounds; try congruence.
1070
  validTypes_split.
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  destruct (IHe subnoise subexprAff inoise iexpmap map1)
    as [ihmap [af' [vR [subaiv [subaerr [Hcont [Hcontf [Hsubares [Hsubsup [Haf [subfresh [Hsubvalidmap [Hsubnoise [subeval [subranges [subaff HvisitedExpr]]]]]]]]]]]]]]]];
    auto; clear IHe.
  assert (af' = af) by congruence; subst.
  destruct u.
  - destruct (isSupersetIntv (toIntv (AffineArithQ.negate_aff af)) aiv) eqn: Hsup; try congruence.
    exists ihmap, (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
    assert (af_evals (afQ2R (AffineArithQ.negate_aff af)) (- vR) ihmap) as ?
        by (rewrite afQ2R_negate_aff; now apply negate_aff_sound).
    inversion validBounds; subst; clear validBounds.
    rewrite plus_0_r.
    assert (fresh subnoise (AffineArithQ.negate_aff af)) by
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1083
        (unfold AffineArithQ.negate_aff; now apply AffineArithQ.mult_aff_const_fresh_compat).
1084 1085
    assert (eval_expr E (toRTMap (toRExpMap Gamma)) DeltaMapR
                      (toREval (toRExp (Unop Neg e))) (- vR) REAL)
1086
      as eval_real.
1087
    {eapply Unop_neg'; try eauto.
1088 1089 1090
      destruct varsTyped as (mt & find_t & ? & valid_exec).
      - eapply toRExpMap_some in find_t; eauto.
      - auto. }
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    repeat split; auto.
    + pose proof contained_flover_map_extension as H'.
      specialize (H' _ iexpmap _ (AffineArithQ.negate_aff af) Hvisited).
      etransitivity; try eassumption.
      apply contained_flover_map_add_compat.
      assumption.
    + rewrite FloverMapFacts.P.F.add_eq_o; try auto.
      apply Q_orderedExps.exprCompare_refl.
    + apply validAffineBounds_validRanges.
      exists ihmap, (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1101
      repeat split; auto.
1102 1103
    + intros e' Hnone Hsome.
      destruct Hsome as [afS Hsome].
1104
      {