AffineValidation.v 60.2 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.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
6
Require Import Flover.IntervalValidation.
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 24
Definition nozeroiv iv :=
  ((Qleb (ivhi iv) 0) && (negb (Qeq_bool (ivhi iv) 0))) ||
            ((Qleb 0 (ivlo iv)) && (negb (Qeq_bool (ivlo iv) 0))).

Fixpoint validAffineBounds (e: exp 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 52 53
      let (exprsAf', n') := valid in
      olet af := FloverMap.find e' exprsAf' in
      match o with 
      | 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 77
      | Div =>
          let iv2 := toIntv af2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
78
          if nozeroiv iv2
Nikita Zyuzin's avatar
Nikita Zyuzin committed
79
          then
80
            updateExpMapIncr e (AffineArithQ.divide_aff af1 af2 n2) n2 exprsAf2 intv 2
Nikita Zyuzin's avatar
Nikita Zyuzin committed
81 82 83
          else None
      end
    | Fma e1 e2 e3 =>
Nikita Zyuzin's avatar
Nikita Zyuzin committed
84
      olet valid1 := validAffineBounds e1 A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
85 86
      let (exprsAf1, n1) := valid1 in
      olet af1 := FloverMap.find e1 exprsAf1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
87
      olet valid2 := validAffineBounds e2 A P validVars exprsAf1 n1 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
88 89
      let (exprsAf2, n2) := valid2 in
      olet af2 := FloverMap.find e2 exprsAf2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
90
      olet valid3 := validAffineBounds e3 A P validVars exprsAf2 n2 in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
91 92
      let (exprsAf3, n3) := valid3 in
      olet af3 := FloverMap.find e3 exprsAf3 in
93
        updateExpMapSucc e (AffineArithQ.plus_aff af1 (AffineArithQ.mult_aff af2 af3 n3)) n3 exprsAf3 intv
Nikita Zyuzin's avatar
Nikita Zyuzin committed
94
    | Downcast _ e' => 
Nikita Zyuzin's avatar
Nikita Zyuzin committed
95
      olet valid' := validAffineBounds e' A P validVars exprsAf currentMaxNoise in
Nikita Zyuzin's avatar
Nikita Zyuzin committed
96 97 98 99 100
      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
101
        Some (FloverMap.add e af' exprsAf', n')
Nikita Zyuzin's avatar
Nikita Zyuzin committed
102
      else None
103
    end
Nikita Zyuzin's avatar
Nikita Zyuzin committed
104
  end.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
105

Nikita Zyuzin's avatar
Nikita Zyuzin committed
106 107 108 109 110
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.

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

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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.

133 134 135 136 137 138 139
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.

140 141 142 143 144 145 146 147 148 149
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.
150 151 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

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
179 180
Lemma afQ2R_mult_aff_aux af1 af2:
  afQ2R (AffineArithQ.mult_aff_aux (af1, af2)) = mult_aff_aux (afQ2R af1, afQ2R af2).
181 182 183 184 185 186 187
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
188 189 190 191 192 193
  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.
194
    simpl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
195 196 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
    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.
234
      simpl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
235 236
      destruct Req_dec_sum as [Heq' | Heq'];
        try (apply Rmult_integral in Heq'; destruct Heq'; try lra).
237
      f_equal.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
238 239
      * rewrite Q2R_mult.
        do 2 rewrite afQ2R_radius.
240
        reflexivity.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
241 242
      * apply afQ2R_mult_aff_aux.
Qed.
243

Nikita Zyuzin's avatar
Nikita Zyuzin committed
244 245 246
Lemma afQ2R_negate_aff af:
  afQ2R (AffineArithQ.negate_aff af) = negate_aff (afQ2R af).
Proof.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  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
271
Lemma afQ2R_inverse_aff af n:
272
  above_zero (afQ2R af) \/ below_zero (afQ2R af) ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
273 274
  afQ2R (AffineArithQ.inverse_aff af n) = inverse_aff (afQ2R af) n.
Proof.
275
  intros above_below.
276 277
  unfold AffineArithQ.inverse_aff.
  unfold inverse_aff.
278 279 280
  unfold above_zero, below_zero in above_below.
  unfold ivhi, IVhi, ivlo, IVlo in *.
  rewrite <- to_interval_to_intv in above_below.
281
  rewrite <- to_interval_to_intv.
282 283 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
  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.
351 352
    apply Qlt_Rlt in Heq.
    destruct (Rlt_dec (Q2R (get_const af + AffineArithQ.radius af)) 0); try lra.
353 354 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
    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.
    }
395
    simpl.
396 397 398 399 400 401 402 403 404 405 406 407 408 409
    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
410

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

Nikita Zyuzin's avatar
Nikita Zyuzin committed
421
Lemma afQ2R_fresh n a:
Nikita Zyuzin's avatar
Nikita Zyuzin committed
422
  fresh n a <-> fresh n (afQ2R a).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
423
Proof.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
424 425 426 427 428 429 430 431
  split; induction a; intros *. 
  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
432 433
Qed.

434 435 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
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
466 467 468
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,
469 470 471
         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
472
         (forall n, (n >= noise)%nat -> map1 n = None) /\
473 474
         FloverMap.find (Var Q v) A = Some (iv, err) /\
         E v = Some vR /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
475
         af_evals (afQ2R af) vR map1.
476

477 478
Definition affine_fVars_P_sound (fVars:NatSet.t) (E:env) (P:precond) :Prop :=
  forall v,
479
    NatSet.In v fVars ->
480 481
    exists vR, E v = Some vR /\
          (Q2R (fst (P v)) <= vR <= Q2R (snd (P v)))%R.
482
          
483
Definition affine_vars_typed (S: NatSet.t) (Gamma: nat -> option mType) :=
484 485 486
  forall v, NatSet.In v S ->
       exists m: mType, Gamma v = Some m.

487
Definition checked_expressions (A: analysisResult) E Gamma iexpmap inoise map1 :=
Nikita Zyuzin's avatar
Nikita Zyuzin committed
488 489 490 491 492 493 494 495 496 497
      forall (e: exp Q),
          (exists (af: affine_form Q), FloverMap.find e iexpmap = Some af) ->
          exists af vR aiv aerr,
            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) /\
            eval_exp E (toRMap Gamma) (toREval (toRExp e)) vR M0 /\
            af_evals (afQ2R af) vR map1.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
498 499 500

Lemma validAffineBounds_sound (e: exp Q) (A: analysisResult) (P: precond)
      fVars dVars (E: env) Gamma exprAfs noise iexpmap inoise map1: 
501
      checked_expressions A E Gamma iexpmap inoise map1 ->
502
      (inoise > 0)%nat ->
503
      (forall n, (n >= inoise)%nat -> map1 n = None) ->
504
      validAffineBounds e A P dVars iexpmap inoise = Some (exprAfs, noise) ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
505
      affine_dVars_range_valid dVars E A inoise iexpmap map1 ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
506
      NatSet.Subset (NatSet.diff (Expressions.usedVars e) dVars) fVars ->
507 508
      affine_fVars_P_sound fVars E P ->
      affine_vars_typed (NatSet.union fVars dVars) Gamma ->
509 510
      exists map2 af vR aiv aerr,
        contained_map map1 map2 /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
511
        contained_flover_map iexpmap exprAfs /\
512 513
        FloverMap.find e A = Some (aiv, aerr) /\
        isSupersetIntv (toIntv af) aiv = true /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
514
        FloverMap.find e exprAfs = Some af /\
515
        fresh noise af /\
516
        (forall n, (n >= noise)%nat -> map2 n = None) /\
517
        (noise >= inoise)%nat /\
Nikita Zyuzin's avatar
Nikita Zyuzin committed
518
        eval_exp E (toRMap Gamma) (toREval (toRExp e)) vR M0 /\
519
        af_evals (afQ2R af) vR map2.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
520
Proof.
521
  revert noise exprAfs inoise iexpmap map1.
522
  induction e;
523
    intros * visitedExpr inoisegtz validmap1 validBounds dVarsValid varsDisjoint fVarsSound varsTyped;
524
    simpl in validBounds.
525
  - specialize (dVarsValid n).
526
    specialize (fVarsSound n).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
527
    specialize (varsTyped n).
528 529 530 531 532 533 534 535 536 537
    specialize (visitedExpr (Var Q n)).
    destruct (FloverMap.find (elt:=affine_form Q) (Var Q n) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
      destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
      - eexists; reflexivity.
      - exists map1, af, vR, aiv, aerr.
        intuition. 
        congruence.
    }
538 539
    destruct (FloverMap.find (elt:=intv * error) (Var Q n) A) as [p | p] eqn: Hares; simpl in validBounds; try congruence.
    destruct p as [aiv aerr].
Nikita Zyuzin's avatar
Nikita Zyuzin committed
540
    destruct (n mem dVars) eqn: Hmem.
541
    + rewrite NatSet.mem_spec in Hmem.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
542
      specialize (dVarsValid Hmem).
543 544
      assert (n  fVars  dVars) as H by intuition.
      specialize (varsTyped H) as [m varsTyped].
Nikita Zyuzin's avatar
Nikita Zyuzin committed
545
      destruct dVarsValid as [af [vR [iv [err dVarsValid]]]]; try reflexivity.
546
      inversion validBounds; subst; clear validBounds.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
547
      exists map1, af, vR, iv, err.
548
      intuition; try congruence.
549
    + destruct (isSupersetIntv (toIntv (fromIntv (P n) inoise)) aiv) eqn: Hsup; try congruence.
550
      inversion validBounds; subst; clear validBounds.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
551 552 553 554
      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.
555 556 557 558 559
      specialize (fVarsSound H') as [vR [eMap interval_containment]].
      destruct (Qeq_bool (ivlo (P n)) (ivhi (P n))) eqn: Heq.
      * exists map1, (fromIntv (P n) inoise), vR, aiv, aerr.
        repeat split; auto.
        -- reflexivity.
560
        -- apply contained_flover_map_extension; assumption.
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
        -- rewrite FloverMapFacts.P.F.add_eq_o; try auto.
           apply Q_orderedExps.expCompare_refl.
        -- unfold fresh, fromIntv, get_max_index.
           rewrite Heq.
           simpl; lia.
        -- intros n' Hn'.
           apply validmap1.
           lia.
        -- lia.
        -- constructor; auto.
           simpl.
           rewrite varsTyped.
           reflexivity.
        -- 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 (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.
        }
        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
686
        -- apply contained_flover_map_extension.
687
           assumption.
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        -- rewrite FloverMapFacts.P.F.add_eq_o; try auto.
           apply Q_orderedExps.expCompare_refl.
        -- unfold fresh, fromIntv, get_max_index.
           rewrite Heq.
           simpl; lia.
        -- intros n' Hn'.
           unfold updMap.
           destruct (n' =? inoise) eqn: Hneq.
           ++ apply beq_nat_true in Hneq.
              lia.
           ++ apply validmap1.
              lia.
        -- lia.
        -- constructor; auto.
           simpl.
           rewrite varsTyped.
           reflexivity.
705
  - clear varsTyped varsDisjoint dVarsValid fVarsSound fVars.
706 707 708 709 710 711 712 713 714 715
    specialize (visitedExpr (Const m v)).
    destruct (FloverMap.find (elt:=affine_form Q) (Const m v) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
      destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
      - eexists; reflexivity.
      - exists map1, af, vR, aiv, aerr.
        intuition. 
        congruence.
    }
716 717 718
    destruct (FloverMap.find (elt:=intv * error) (Const m v) A) eqn: Hares;
      simpl in validBounds; try congruence.
    destruct p as [i e].
719 720 721
    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
722 723 724
    rewrite Qle_bool_iff in L, R.
    simpl ivlo in L, R.
    simpl ivhi in L, R.
725 726
    assert (fst i <= v) as L' by assumption.
    assert (v <= snd i) as R' by assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
727 728 729
    apply Qle_Rle in L.
    apply Qle_Rle in R.
    inversion validBounds; subst; clear validBounds.
730
    exists map1, (fromIntv (v, v) noise), (perturb (Q2R v) 0), i, e.
731
    repeat split.
732
    + reflexivity.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
733
    + apply contained_flover_map_extension.
734
      assumption.
735 736 737 738 739 740 741
    + unfold fromIntv, toIntv.
      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).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
742 743
    + rewrite FloverMapFacts.P.F.add_eq_o; try auto.
      apply Q_orderedExps.expCompare_refl.
744 745 746 747 748
    + unfold fromIntv.
      simpl ivlo.
      simpl ivhi.
      rewrite Qeq_bool_refl.
      unfold fresh, get_max_index; rewrite get_max_index_aux_equation. lia.
749
    + assumption.
750
    + lia.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
751 752
    + constructor; apply Rabs_0_equiv.
    + unfold perturb.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
753 754 755 756 757
      unfold fromIntv.
      simpl.
      rewrite Qeq_bool_refl.
      simpl.
      rewrite Q2R_plus.
758 759 760
      rewrite Q2R_div; try lra.
      unfold af_evals, Ropt_eq; simpl.
      lra.
761 762 763 764 765 766 767 768 769 770 771
  - pose proof visitedExpr as visitedExpr'.
    specialize (visitedExpr (Unop u e)).
    destruct (FloverMap.find (elt:=affine_form Q) (Unop u e) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
      destruct visitedExpr as [af [vR [aiv [aerr visitedExpr]]]].
      - eexists; reflexivity.
      - exists map1, af, vR, aiv, aerr.
        intuition. 
        congruence.
    }
772
    unfold updateExpMap, updateExpMapSucc, updateExpMapIncr in validBounds.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
773 774
    destruct (FloverMap.find (elt:=intv * error) (Unop u e) A) as [p | p] eqn: Hares; simpl in validBounds; try congruence.
    destruct p as [aiv aerr].
775
    destruct (validAffineBounds e A P dVars iexpmap inoise) eqn: Hsubvalid; simpl in validBounds; try congruence.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
776 777
    destruct p as [subexprAff subnoise].
    destruct (FloverMap.find (elt:=affine_form Q) e subexprAff) as [af | af] eqn: He; simpl in validBounds; try congruence.
778
    destruct (IHe subnoise subexprAff inoise iexpmap map1)
Nikita Zyuzin's avatar
Nikita Zyuzin committed
779
      as [ihmap [af' [vR [subaiv [subaerr [Hcont [Hcontf [Hsubares [Hsubsup [Haf [subfresh [Hsubvalidmap [Hsubnoise [subeval subaff]]]]]]]]]]]]]];
780
      auto; clear IHe varsTyped varsDisjoint dVarsValid fVarsSound fVars.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
781 782 783
    assert (af' = af) by congruence; subst.
    destruct u.
    + destruct (isSupersetIntv (toIntv (AffineArithQ.negate_aff af)) aiv) eqn: Hsup; try congruence.
784
      exists ihmap, (AffineArithQ.negate_aff af), (-vR)%R, aiv, aerr.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
785
      inversion validBounds; subst; clear validBounds.
786
      repeat split; auto.
787 788 789 790 791
      * 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.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
792 793
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
        apply Q_orderedExps.expCompare_refl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
794 795 796
      * rewrite plus_0_r.
        unfold AffineArithQ.negate_aff.
        now apply AffineArithQ.fresh_mult_aff_const.
797 798
      * rewrite plus_0_r.
        assumption.
799
      * lia.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
800
      * now constructor.
801
      * rewrite afQ2R_negate_aff.
802
        now apply negate_aff_sound.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
803 804 805
    + destruct (nozeroiv (toIntv af)) eqn: noZeroAf; try congruence.
      destruct (isSupersetIntv (toIntv (AffineArithQ.inverse_aff af subnoise)) aiv) eqn: Hsup; try congruence.
      inversion validBounds; subst; clear validBounds.
806
      pose proof (inverse_aff_sound) as invsound.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
807
      assert (fresh subnoise (afQ2R af)) as H' by now apply afQ2R_fresh.
808
      assert (above_zero (afQ2R af) \/ below_zero (afQ2R af)) as H'' by eauto using nozero_above_below.
809 810 811
      specialize (invsound (afQ2R af) vR ihmap subnoise H' H'' subaff) as [qInv inveval].
      exists (updMap ihmap subnoise qInv), (AffineArithQ.inverse_aff af subnoise),
      (perturb (evalUnop Inv vR) 0)%R, aiv, aerr.
812
      repeat split; try auto.
813 814
      * eapply contained_map_trans; try exact Hcont.
        apply contained_map_extension.
815
        apply Hsubvalidmap; lia.
816 817 818 819 820
      * pose proof contained_flover_map_extension as H.
        specialize (H _ iexpmap _ (AffineArithQ.inverse_aff af subnoise) Hvisited).
        etransitivity; try eassumption.
        apply contained_flover_map_add_compat.
        assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
821 822
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
        apply Q_orderedExps.expCompare_refl.
823
      * now apply AffineArithQ.fresh_inverse_aff.
824 825 826 827 828 829 830
      * intros n H.
        unfold updMap.
        assert (n <> subnoise) as Hneq by lia.
        rewrite <- Nat.eqb_neq in Hneq.
        rewrite Hneq.
        apply Hsubvalidmap.
        lia.
831
      * lia.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
832
      * constructor; try apply Rabs_0_equiv; auto.
833
        eauto using above_below_nonzero.
834
      * unfold perturb.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
835 836
        rewrite Rplus_0_r.
        rewrite Rmult_1_r.
837
        simpl evalUnop.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
838
        replace (/ vR)%R with (1 / vR)%R by lra.
839
        now rewrite afQ2R_inverse_aff.
840 841 842 843 844 845 846 847 848
  - destruct (FloverMap.find (elt:=affine_form Q) (Binop b e1 e2) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
      unfold checked_expressions in visitedExpr.
      destruct (visitedExpr (Binop b e1 e2)) as [af [vR [aiv [aerr visitedExpr']]]].
      - eexists; eauto.
      - exists map1, af, vR, aiv, aerr.
        intuition. 
    }
849
    unfold updateExpMap, updateExpMapSucc, updateExpMapIncr in validBounds.
850 851 852 853 854 855 856 857
    destruct (FloverMap.find (elt:=intv * error) (Binop b e1 e2) A) as [p | _] eqn: Hares; simpl in validBounds; try congruence.
    destruct p as [aiv aerr].
    destruct (validAffineBounds e1 A P dVars iexpmap inoise) eqn: Hsubvalid1; simpl in validBounds; try congruence.
    destruct p as [subexprAff1 subnoise1].
    destruct (FloverMap.find (elt:=affine_form Q) e1 subexprAff1) as [af1 | _] eqn: He1; simpl in validBounds; try congruence.
    destruct (validAffineBounds e2 A P dVars subexprAff1 subnoise1) eqn: Hsubvalid2; simpl in validBounds; try congruence.
    destruct p as [subexprAff2 subnoise2].
    destruct (FloverMap.find (elt:=affine_form Q) e2 subexprAff2) as [af2 | _] eqn: He2; simpl in validBounds; try congruence.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
858
    destruct (IHe1 subnoise1 subexprAff1 inoise iexpmap map1) as [ihmap1 [af1' [vR1 [ihaiv1 [ihaerr1 [ihcont1 [ihcontf1 [ihares1 [ihsup1 [Haf1 [subfresh1 [Hsubvalidmap1 [Hsubnoise1 [subeval1 subaff1]]]]]]]]]]]]]];
859 860 861
      try auto; clear IHe1.
    { repeat set_tac. }
    assert (af1' = af1) by congruence; subst.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
862
    destruct (IHe2 subnoise2 subexprAff2 subnoise1 subexprAff1 ihmap1) as [ihmap2 [af2' [vR2 [_ [_ [ihcont2 [ihcontf2 [_ [_ [Haf2 [subfresh2 [Hsubvalidmap2 [Hsubnoise2 [subeval2 subaff2]]]]]]]]]]]]]];
863
      try auto; try lra; clear IHe2; eauto using fresh_n_gt_O.
864 865 866
    {
      admit.
    }
Nikita Zyuzin's avatar
Nikita Zyuzin committed
867 868 869 870 871 872 873 874 875
    {
      unfold affine_dVars_range_valid in dVarsValid |-*.
      intros v vin.
      specialize (dVarsValid v vin).
      destruct dVarsValid as [af [vR [iv [err dVarsValid]]]].
      exists af, vR, iv, err; intuition.
      - eapply fresh_monotonic; try eassumption.
      - eapply af_evals_map_extension; try eassumption.
    }
876 877 878 879 880 881
    { repeat set_tac. }
    assert (af2' = af2) by congruence; subst.
    clear varsTyped varsDisjoint dVarsValid fVarsSound fVars.
    destruct b.
    + destruct (isSupersetIntv (toIntv (AffineArithQ.plus_aff af1 af2)) aiv) eqn: Hsup; try congruence.
      inversion validBounds; subst; clear validBounds.
882
      exists ihmap2, (AffineArithQ.plus_aff af1 af2), (perturb (evalBinop Plus vR1 vR2) 0)%R, aiv, aerr.
883
      repeat split; auto.
884 885 886
      * etransitivity; try exact ihcont1.
        etransitivity; try exact ihcont2.
        reflexivity.
887 888 889 890 891
      * pose proof contained_flover_map_extension as H.
        specialize (H _ iexpmap _ (AffineArithQ.plus_aff af1 af2) Hvisited).
        etransitivity; try eassumption.
        apply contained_flover_map_add_compat.
        clear H.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
892
        etransitivity; try eassumption.
893 894
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
        apply Q_orderedExps.expCompare_refl.
895
      * apply AffineArithQ.plus_aff_preserves_fresh; rewrite plus_0_r; auto.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
896 897 898
        eapply fresh_monotonic.
        exact Hsubnoise2.
        assumption.
899
      * rewrite plus_0_r; assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
900
      * lia.
901 902 903 904 905 906 907
      * replace M0 with (join M0 M0) by trivial.
        apply Binop_dist; try apply Rabs_0_equiv; auto; congruence.
      * unfold perturb.
        rewrite Rplus_0_r.
        rewrite Rmult_1_r.
        simpl evalBinop.
        rewrite afQ2R_plus_aff.
908
        apply plus_aff_sound; auto.
909
        eauto using af_evals_map_extension.
910 911
    + destruct (isSupersetIntv (toIntv (AffineArithQ.subtract_aff af1 af2)) aiv) eqn: Hsup; try congruence.
      inversion validBounds; subst; clear validBounds.
912
      exists ihmap2, (AffineArithQ.subtract_aff af1 af2), (perturb (evalBinop Sub vR1 vR2) 0)%R, aiv, aerr.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
913
      repeat split; auto.
914 915 916
      * etransitivity; try exact ihcont1.
        etransitivity; try exact ihcont2.
        reflexivity.
917 918 919 920 921
      * pose proof contained_flover_map_extension as H.
        specialize (H _ iexpmap _ (AffineArithQ.subtract_aff af1 af2) Hvisited).
        etransitivity; try eassumption.
        apply contained_flover_map_add_compat.
        clear H.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
922
        etransitivity; try eassumption.
923 924
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
        apply Q_orderedExps.expCompare_refl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
925 926 927 928 929 930 931 932
      * unfold AffineArithQ.subtract_aff.
        rewrite plus_0_r.
        apply AffineArithQ.plus_aff_preserves_fresh.
        {
          eapply fresh_monotonic.
          exact Hsubnoise2.
          assumption.
        }
Nikita Zyuzin's avatar
Nikita Zyuzin committed
933 934
        unfold AffineArithQ.negate_aff.
        now apply AffineArithQ.fresh_mult_aff_const.
935
      * rewrite plus_0_r; assumption.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
936
      * lia.
937 938 939 940 941 942 943
      * replace M0 with (join M0 M0) by trivial.
        apply Binop_dist; try apply Rabs_0_equiv; auto; congruence.
      * unfold perturb.
        rewrite Rplus_0_r.
        rewrite Rmult_1_r.
        simpl evalBinop.
        rewrite afQ2R_subtract_aff.
944
        apply subtract_aff_sound; auto.
945
        eauto using af_evals_map_extension.
946 947
    + destruct (isSupersetIntv (toIntv (AffineArithQ.mult_aff af1 af2 subnoise2)) aiv) eqn: Hsup; try congruence.
      inversion validBounds; subst; clear validBounds.
948
      pose proof mult_aff_sound as multsound.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
949 950 951
      assert (fresh subnoise2 (afQ2R af1)) as fresh1
          by (apply afQ2R_fresh; eauto using fresh_monotonic).
      assert (fresh subnoise2 (afQ2R af2)) as fresh2 by now apply afQ2R_fresh.
952
      assert (af_evals (afQ2R af1) vR1 ihmap2) as subaff1' by eauto using af_evals_map_extension.
953 954 955
      specialize (multsound (afQ2R af1) (afQ2R af2) vR1 vR2 ihmap2 subnoise2 fresh1 fresh2 subaff1' subaff2) as [qMult multsound].
      exists (updMap ihmap2 subnoise2 qMult), (AffineArithQ.mult_aff af1 af2 subnoise2),
      (perturb (evalBinop Mult vR1 vR2) 0)%R, aiv, aerr.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
956
      repeat split; auto.
957 958
      * etransitivity; try exact ihcont1.
        etransitivity; try exact ihcont2.
959
        apply contained_map_extension; auto.
960 961 962 963 964
      * pose proof contained_flover_map_extension as H.
        specialize (H _ iexpmap _ (AffineArithQ.mult_aff af1 af2 subnoise2) Hvisited).
        etransitivity; try eassumption.
        apply contained_flover_map_add_compat.
        clear H.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
965
        etransitivity; try eassumption.
966 967
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
        apply Q_orderedExps.expCompare_refl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
968 969 970 971 972 973 974 975 976 977 978 979 980
      * unfold AffineArithQ.mult_aff.
        destruct Qeq_bool.
        -- rewrite orb_true_l.
           apply AffineArithQ.mult_aff_aux_preserves_fresh;
             apply fresh_inc; now rewrite afQ2R_fresh.
        -- destruct Qeq_bool.
           ++ rewrite orb_true_r.
              apply AffineArithQ.mult_aff_aux_preserves_fresh;
                apply fresh_inc; now rewrite afQ2R_fresh.
           ++ simpl.
              apply fresh_noise; try lia.
              apply AffineArithQ.mult_aff_aux_preserves_fresh;
                apply fresh_inc; now rewrite afQ2R_fresh.
981 982 983 984 985 986 987
      * intros n H.
        assert (n <> subnoise2) as Hneq by lia.
        rewrite <- Nat.eqb_neq in Hneq.
        unfold updMap.
        rewrite Hneq.
        apply Hsubvalidmap2.
        lia.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
988
      * lia.
989 990 991 992 993 994 995
      * replace M0 with (join M0 M0) by trivial.
        apply Binop_dist; try apply Rabs_0_equiv; auto; congruence.
      * unfold perturb.
        rewrite Rplus_0_r.
        rewrite Rmult_1_r.
        simpl evalBinop.
        rewrite afQ2R_mult_aff.
996
        assumption.
997 998 999
    + destruct (nozeroiv (toIntv af2)) eqn: Hnozero; try congruence.
      destruct (isSupersetIntv (toIntv (AffineArithQ.divide_aff af1 af2 subnoise2)) aiv) eqn: Hsup; try congruence.
      inversion validBounds; subst; clear validBounds.
1000
      pose proof divide_aff_sound as divsound.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1001 1002 1003
      assert (fresh subnoise2 (afQ2R af1)) as fresh1
          by now apply afQ2R_fresh; eauto using fresh_monotonic.
      assert (fresh subnoise2 (afQ2R af2)) as fresh2 by now apply afQ2R_fresh.
1004
      assert (af_evals (afQ2R af1) vR1 ihmap2) as subaff1' by eauto using af_evals_map_extension.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1005
      assert (above_zero (afQ2R af2) \/ below_zero (afQ2R af2)) as nozero' by eauto using nozero_above_below.
1006 1007 1008
      specialize (divsound (afQ2R af1) (afQ2R af2) vR1 vR2 subnoise2 ihmap2 fresh1 fresh2 nozero' subaff1' subaff2) as [qInv [qMult multsound]].
      exists (updMap (updMap ihmap2 subnoise2 qInv) (subnoise2 + 1) qMult),
      (AffineArithQ.divide_aff af1 af2 subnoise2), (perturb (evalBinop Div vR1 vR2) 0)%R, aiv, aerr.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1009
      repeat split; auto.
1010 1011
      * etransitivity; try exact ihcont1.
        etransitivity; try exact ihcont2.
1012
        apply contained_map_extension2; apply Hsubvalidmap2; lia.
1013 1014 1015 1016 1017
      * pose proof contained_flover_map_extension as H.
        specialize (H _ iexpmap _ (AffineArithQ.divide_aff af1 af2 subnoise2) Hvisited).
        etransitivity; try eassumption.
        apply contained_flover_map_add_compat.
        clear H.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1018
        etransitivity; try eassumption.
1019 1020
      * rewrite FloverMapFacts.P.F.add_eq_o; try auto.
        apply Q_orderedExps.expCompare_refl.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
      * unfold AffineArithQ.divide_aff, AffineArithQ.mult_aff.
        destruct Qeq_bool.
        -- rewrite orb_true_l.
           apply AffineArithQ.mult_aff_aux_preserves_fresh.
           { 
             apply fresh_monotonic with (n := subnoise2);
             try lia; try apply afQ2R_fresh; auto.
           }
           apply fresh_monotonic with (n := (subnoise2 + 1)%nat); try lia.
           now apply AffineArithQ.fresh_inverse_aff.
        -- destruct Qeq_bool.
           ++ rewrite orb_true_r.
              apply AffineArithQ.mult_aff_aux_preserves_fresh.
              { 
                apply fresh_monotonic with (n := subnoise2);
                  try lia; try apply afQ2R_fresh; auto.
              }
              apply fresh_monotonic with (n := (subnoise2 + 1)%nat); try lia.
              now apply AffineArithQ.fresh_inverse_aff.
           ++ simpl.
              apply fresh_noise; try lia.
              apply AffineArithQ.mult_aff_aux_preserves_fresh.
              { 
                apply fresh_monotonic with (n := subnoise2);
                  try lia; try apply afQ2R_fresh; auto.
              }
              apply fresh_monotonic with (n := (subnoise2 + 1)%nat); try lia.
              now apply AffineArithQ.fresh_inverse_aff.
1049 1050 1051 1052 1053 1054 1055 1056 1057
      * intros n H.
        assert (n <> (subnoise2 + 1)%nat) as Hneq by lia.
        assert (n <> subnoise2) as Hneq' by lia.
        rewrite <- Nat.eqb_neq in Hneq.
        rewrite <- Nat.eqb_neq in Hneq'.
        unfold updMap.
        rewrite Hneq', Hneq.
        apply Hsubvalidmap2.
        lia.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1058
      * lia.
1059 1060 1061
      * replace M0 with (join M0 M0) by trivial.
        apply Binop_dist; try apply Rabs_0_equiv; auto.
        intros _.
1062
        eauto using above_below_nonzero.
1063 1064 1065 1066
      * unfold perturb.
        rewrite Rplus_0_r.
        rewrite Rmult_1_r.
        simpl evalBinop.
1067
        rewrite afQ2R_divide_aff; auto.
1068 1069 1070 1071 1072 1073 1074 1075
  - destruct (FloverMap.find (elt:=affine_form Q) (Fma e1 e2 e3) iexpmap) eqn: Hvisited.
    {
      inversion validBounds; subst; clear validBounds.
      destruct (visitedExpr (Fma e1 e2 e3)) as [af [vR [aiv [aerr visitedExpr']]]].
      - eexists; eauto.
      - exists map1, af, vR, aiv, aerr.
        intuition. 
    }
1076
    unfold updateExpMap, updateExpMapSucc, updateExpMapIncr in validBounds.
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    destruct (FloverMap.find (elt:=intv * error) (Fma e1 e2 e3) A) as [p | _] eqn: Hares; simpl in validBounds; try congruence.
    destruct p as [aiv aerr].
    destruct (validAffineBounds e1 A P dVars iexpmap inoise) eqn: Hsubvalid1; simpl in validBounds; try congruence.
    destruct p as [subexprAff1 subnoise1].
    destruct (FloverMap.find (elt:=affine_form Q) e1 subexprAff1) as [af1 | _] eqn: He1; simpl in validBounds; try congruence.
    destruct (validAffineBounds e2 A P dVars subexprAff1 subnoise1) eqn: Hsubvalid2; simpl in validBounds; try congruence.
    destruct p as [subexprAff2 subnoise2].
    destruct (FloverMap.find (elt:=affine_form Q) e2 subexprAff2) as [af2 | _] eqn: He2; simpl in validBounds; try congruence.
    destruct (validAffineBounds e3 A P dVars subexprAff2 subnoise2) eqn: Hsubvalid3; simpl in validBounds; try congruence.
    destruct p as [subexprAff3 subnoise3].
    destruct (FloverMap.find (elt:=affine_form Q) e3 subexprAff3) as [af3 | _] eqn: He3; simpl in validBounds; try congruence.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1088
    destruct (IHe1 subnoise1 subexprAff1 inoise iexpmap map1) as [ihmap1 [af1' [vR1 [_ [_ [ihcont1 [ihcontf1 [_ [_ [Haf1 [subfresh1 [Hsubmapvalid1 [Hsubnoise1 [subeval1 subaff1]]]]]]]]]]]]]];
1089 1090 1091
      try auto; clear IHe1.
    { repeat set_tac. }
    assert (af1' = af1) by congruence; subst.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1092
    destruct (IHe2 subnoise2 subexprAff2 subnoise1 subexprAff1 ihmap1) as [ihmap2 [af2' [vR2 [_ [_ [ihcont2 [ihcontf2 [_ [_ [Haf2 [subfresh2 [Hsubmapvalid2 [Hsubnoise2 [subeval2 subaff2]]]]]]]]]]]]]];
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1093
      try auto; clear IHe2; try lia.
1094 1095 1096
    {
      admit.
    }
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1097 1098 1099 1100 1101 1102 1103 1104 1105
    {
      unfold affine_dVars_range_valid in dVarsValid |-*.
      intros v vin.
      specialize (dVarsValid v vin).
      destruct dVarsValid as [af [vR [iv [err dVarsValid]]]].
      exists af, vR, iv, err; intuition.
      - eapply fresh_monotonic; try eassumption.
      - eapply af_evals_map_extension; try eassumption.
    }
1106 1107
    { repeat set_tac. }
    assert (af2' = af2) by congruence; subst.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1108
    destruct (IHe3 subnoise3 subexprAff3 subnoise2 subexprAff2 ihmap2) as [ihmap3 [af3