IntervalValidation.v 26.6 KB
Newer Older
Heiko Becker's avatar
Heiko Becker committed
1
(**
Heiko Becker's avatar
Heiko Becker committed
2 3 4 5 6
    Interval arithmetic checker and its soundness proof.
    The function validIntervalbounds checks wether the given analysis result is
    a valid range arithmetic for each sub term of the given expression e.
    The computation is done using our formalized interval arithmetic.
    The function is used in CertificateChecker.v to build the full checker.
Heiko Becker's avatar
Heiko Becker committed
7
**)
8
Require Import Coq.QArith.QArith Coq.QArith.Qreals QArith.Qminmax Coq.Lists.List Coq.micromega.Psatz.
9
Require Import Daisy.Infra.Abbrevs Daisy.Infra.RationalSimps Daisy.Infra.RealRationalProps.
10 11
Require Import Daisy.Infra.Ltacs Daisy.Infra.RealSimps.
Require Export Daisy.IntervalArithQ Daisy.IntervalArith Daisy.ssaPrgs.
12

13
Fixpoint validIntervalbounds (e:exp Q) (absenv:analysisResult) (P:precond) (validVars:NatSet.t) :=
14 15
  let (intv, _) := absenv e in
    match e with
16
    | Var _ m v =>
17 18 19
      if NatSet.mem v validVars
      then true
      else isSupersetIntv (P v) intv && (Qleb (ivlo (P v)) (ivhi (P v)))
Heiko Becker's avatar
Heiko Becker committed
20 21
    | Const n => isSupersetIntv (n,n) intv
    | Unop o f =>
22 23 24
      if validIntervalbounds f absenv P validVars
      then
        let (iv, _) := absenv f in
Heiko Becker's avatar
Heiko Becker committed
25
        match o with
26
        | Neg =>
Heiko Becker's avatar
Heiko Becker committed
27
          let new_iv := negateIntv iv in
28 29
          isSupersetIntv new_iv intv
        | Inv =>
30 31 32 33 34 35
          if (((Qleb (ivhi iv) 0) && (negb (Qeq_bool (ivhi iv) 0))) ||
              ((Qleb 0 (ivlo iv)) && (negb (Qeq_bool (ivlo iv) 0))))
          then
            let new_iv := invertIntv iv in
            isSupersetIntv new_iv intv
          else false
Heiko Becker's avatar
Heiko Becker committed
36
        end
37
      else false
Heiko Becker's avatar
Heiko Becker committed
38
    | Binop op f1 f2 =>
39 40 41 42 43
      if ((validIntervalbounds f1 absenv P validVars) &&
          (validIntervalbounds f2 absenv P validVars))
      then
        let (iv1,_) := absenv f1 in
        let (iv2,_) := absenv f2 in
Heiko Becker's avatar
Heiko Becker committed
44
          match op with
45 46 47 48 49 50 51 52 53
          | Plus =>
            let new_iv := addIntv iv1 iv2 in
            isSupersetIntv new_iv intv
          | Sub =>
            let new_iv := subtractIntv iv1 iv2 in
            isSupersetIntv new_iv intv
          | Mult =>
            let new_iv := multIntv iv1 iv2 in
            isSupersetIntv new_iv intv
Heiko Becker's avatar
Heiko Becker committed
54
          | Div =>
55 56 57 58 59 60
            if (((Qleb (ivhi iv2) 0) && (negb (Qeq_bool (ivhi iv2) 0))) ||
                ((Qleb 0 (ivlo iv2)) && (negb (Qeq_bool (ivlo iv2) 0))))
            then
              let new_iv := divideIntv iv1 iv2 in
              isSupersetIntv new_iv intv
            else false
61
          end
62
      else false
63 64 65 66
    | Downcast m f1 =>
      let (iv1, _) := absenv f1 in
      andb (validIntervalbounds f1 absenv P validVars) (andb (isSupersetIntv intv iv1) (isSupersetIntv iv1 intv))
           (* TODO: intv = iv1 might be a hard constraint... *)
67 68
    end.

69
Fixpoint validIntervalboundsCmd (f:cmd Q) (absenv:analysisResult) (P:precond) (validVars:NatSet.t) :bool:=
70
  match f with
71
  | Let m x e g =>
72
    if (validIntervalbounds e absenv P validVars &&
73 74
        Qeq_bool (fst (fst (absenv e))) (fst (fst (absenv (Var Q m x)))) &&
        Qeq_bool (snd (fst (absenv e))) (snd (fst (absenv (Var Q m x)))))
75 76
    then validIntervalboundsCmd g absenv P (NatSet.add x validVars)
    else false
77 78
  |Ret e =>
   validIntervalbounds e absenv P validVars
79 80
  end.

81 82 83 84 85 86 87 88 89
Fixpoint erasure (e:exp Q) :exp Q :=
  match e with
  |Var _ m x => Var Q M0 x
  |Unop u e => Unop u (erasure e)
  |Binop b e1 e2 => Binop b (erasure e1) (erasure e2)
  |Downcast _ e => Downcast M0 (erasure e)
  |_ => e
  end.

90
Theorem ivbounds_approximatesPrecond_sound f absenv P V:
91
  validIntervalbounds (erasure f) absenv P V = true ->
Raphaël Monat's avatar
Raphaël Monat committed
92
  forall v, NatSet.In v (NatSet.diff (Expressions.usedVars f) V) ->
93
       Is_true(isSupersetIntv (P v) (fst (absenv (Var Q M0 v)))).
94
Proof.
Heiko Becker's avatar
Heiko Becker committed
95
  induction f; unfold validIntervalbounds.
96
  - simpl. intros approx_true v v_in_fV; simpl in *.
97 98 99
    rewrite NatSet.diff_spec in v_in_fV.
    rewrite NatSet.singleton_spec in v_in_fV;
      destruct v_in_fV; subst.
100
    destruct (absenv (Var Q M0 n)); simpl in *.
101 102 103 104
    case_eq (NatSet.mem n V); intros case_mem;
      rewrite case_mem in approx_true; simpl in *.
    + rewrite NatSet.mem_spec in case_mem.
      contradiction.
105 106 107
    + apply Is_true_eq_left in approx_true.
      apply andb_prop_elim in approx_true.
      destruct approx_true; auto.
108 109
  - intros approx_true v0 v_in_fV; simpl in *.
    inversion v_in_fV.
Heiko Becker's avatar
Heiko Becker committed
110
  - intros approx_unary_true v v_in_fV.
Raphaël Monat's avatar
Raphaël Monat committed
111
    unfold usedVars in v_in_fV.
Heiko Becker's avatar
Heiko Becker committed
112
    apply Is_true_eq_left in approx_unary_true.
113 114
    simpl in *.
    destruct (absenv (Unop u (erasure f))); destruct (absenv (erasure f)); simpl in *.
Heiko Becker's avatar
Heiko Becker committed
115 116 117 118
    apply andb_prop_elim in approx_unary_true.
    destruct approx_unary_true.
    apply IHf; try auto.
    apply Is_true_eq_true; auto.
119
  - intros approx_bin_true v v_in_fV.
120 121 122 123
    simpl in v_in_fV.
    rewrite NatSet.diff_spec in v_in_fV.
    destruct v_in_fV as [ v_in_fV v_not_in_V].
    rewrite NatSet.union_spec in v_in_fV.
124
    apply Is_true_eq_left in approx_bin_true.
125 126 127
    simpl in *.
    destruct (absenv (Binop b (erasure f1) (erasure f2))); destruct (absenv (erasure f1));
      destruct (absenv (erasure f2)); simpl in *.
128 129 130 131
    apply andb_prop_elim in approx_bin_true.
    destruct approx_bin_true.
    apply andb_prop_elim in H.
    destruct H.
132
    destruct v_in_fV.
Heiko Becker's avatar
Heiko Becker committed
133
    + apply IHf1; auto.
134
      apply Is_true_eq_true; auto.
135
      rewrite NatSet.diff_spec; split; auto.
Heiko Becker's avatar
Heiko Becker committed
136
    + apply IHf2; auto.
137
      apply Is_true_eq_true; auto.
138
      rewrite NatSet.diff_spec; split; auto.
139 140
  - admit.
Admitted.
141

Heiko Becker's avatar
Heiko Becker committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
Corollary Q2R_max4 a b c d:
  Q2R (IntervalArithQ.max4 a b c d) = max4 (Q2R a) (Q2R b) (Q2R c) (Q2R d).
Proof.
  unfold IntervalArithQ.max4, max4; repeat rewrite Q2R_max; auto.
Qed.

Corollary Q2R_min4 a b c d:
  Q2R (IntervalArithQ.min4 a b c d) = min4 (Q2R a) (Q2R b) (Q2R c) (Q2R d).
Proof.
  unfold IntervalArith.min4, min4; repeat rewrite Q2R_min; auto.
Qed.

Ltac env_assert absenv e name :=
  assert (exists iv err, absenv e = (iv,err)) as name by (destruct (absenv e); repeat eexists; auto).

157
Lemma validBoundsDiv_uneq_zero e1 e2 absenv P V ivlo_e2 ivhi_e2 err:
158
  absenv e2 = ((ivlo_e2,ivhi_e2), err) ->
159
  validIntervalbounds (Binop Div e1 e2) absenv P V = true ->
160 161 162 163 164 165 166
  (ivhi_e2 < 0) \/ (0 < ivlo_e2).
Proof.
  intros absenv_eq validBounds.
  unfold validIntervalbounds in validBounds.
  env_assert absenv (Binop Div e1 e2) abs_div; destruct abs_div as [iv_div [err_div abs_div]].
  env_assert absenv e1 abs_e1; destruct abs_e1 as [iv_e1 [err_e1 abs_e1]].
  rewrite abs_div, abs_e1, absenv_eq in validBounds.
167
  repeat (rewrite <- andb_lazy_alt in validBounds).
168 169 170
  apply Is_true_eq_left in validBounds.
  apply andb_prop_elim in validBounds.
  destruct validBounds as [_ validBounds]; apply andb_prop_elim in validBounds.
171
  destruct validBounds as [nodiv0 _].
172
  apply Is_true_eq_true in nodiv0.
173
  unfold isSupersetIntv in *; simpl in *.
174
  apply le_neq_bool_to_lt_prop; auto.
175 176
Qed.

177 178 179 180 181 182
Fixpoint getRetExp (V:Type) (f:cmd V) :=
  match f with
  |Let x e g => getRetExp g
  | Ret e => e
  end.

183
Theorem validIntervalbounds_sound (f:exp Q) (absenv:analysisResult) (P:precond) fVars dVars E:
Heiko Becker's avatar
Heiko Becker committed
184
  forall vR,
185 186
    validIntervalbounds f absenv P dVars = true ->
    (forall v, NatSet.mem v dVars = true ->
187
          exists vR, E v = Some vR /\
188
                (Q2R (fst (fst (absenv (Var Q v)))) <= vR <= Q2R (snd (fst (absenv (Var Q v)))))%R) ->
Raphaël Monat's avatar
Raphaël Monat committed
189
    NatSet.Subset (NatSet.diff (Expressions.usedVars f) dVars) fVars ->
190
    (forall v, NatSet.mem v fVars = true ->
191 192 193
          exists vR, E v = Some vR /\
                (Q2R (fst (P v)) <= vR <= Q2R (snd (P v)))%R) ->
    eval_exp 0%R E (toRExp f) vR ->
Heiko Becker's avatar
Heiko Becker committed
194
  (Q2R (fst (fst(absenv f))) <= vR <= Q2R (snd (fst (absenv f))))%R.
195
Proof.
Raphaël Monat's avatar
Raphaël Monat committed
196
  induction f; intros vR valid_bounds valid_definedVars usedVars_subset valid_usedVars eval_f.
197 198 199
  - unfold validIntervalbounds in valid_bounds.
    env_assert absenv (Var Q n) absenv_var.
    destruct absenv_var as [ iv [err absenv_var]].
Raphaël Monat's avatar
Raphaël Monat committed
200
    specialize (valid_usedVars n).
201 202
    rewrite absenv_var in *; simpl in *.
    inversion eval_f; subst.
203
    case_eq (NatSet.mem n dVars); intros case_mem; rewrite case_mem in *; simpl in *.
204 205
    + specialize (valid_definedVars n case_mem).
      destruct valid_definedVars as [vR' [E_n_eq precond_sound]].
206 207
      rewrite E_n_eq in *.
      inversion H0; subst.
208
      rewrite absenv_var in *; auto.
209 210 211
    + repeat (rewrite delta_0_deterministic in *; try auto).
      unfold isSupersetIntv in valid_bounds.
      andb_to_prop valid_bounds.
212 213 214 215
      apply Qle_bool_iff in L0;
        apply Qle_bool_iff in R0.
      apply Qle_Rle in L0;
        apply Qle_Rle in R0.
216
      simpl in *.
217 218 219 220
      assert (NatSet.mem n fVars = true) as in_fVars.
      * assert (NatSet.In n (NatSet.singleton n))
          as in_singleton by (rewrite NatSet.singleton_spec; auto).
        rewrite NatSet.mem_spec.
Raphaël Monat's avatar
Raphaël Monat committed
221 222
        hnf in usedVars_subset.
        apply usedVars_subset.
223
        rewrite NatSet.diff_spec, NatSet.singleton_spec.
224
        split; try auto.
225 226 227
        hnf; intros in_dVars.
        rewrite <- NatSet.mem_spec in in_dVars.
        rewrite in_dVars in case_mem; congruence.
Raphaël Monat's avatar
Raphaël Monat committed
228 229
      * specialize (valid_usedVars in_fVars);
          destruct valid_usedVars as [vR' [vR_def P_valid]].
230 231
        rewrite vR_def in H0; inversion H0; subst.
        lra.
232
  - unfold validIntervalbounds in valid_bounds.
Heiko Becker's avatar
Heiko Becker committed
233
    destruct (absenv (Const v)) as [intv err]; simpl.
234 235
    apply Is_true_eq_left in valid_bounds.
    apply andb_prop_elim in valid_bounds.
Heiko Becker's avatar
Heiko Becker committed
236
    destruct valid_bounds as [valid_lo valid_hi].
Heiko Becker's avatar
Heiko Becker committed
237
    inversion eval_f; subst.
238
    rewrite delta_0_deterministic; auto.
239 240
    unfold contained; simpl.
    split.
Heiko Becker's avatar
Heiko Becker committed
241
    + apply Is_true_eq_true in valid_lo.
242
      unfold Qleb in *.
Heiko Becker's avatar
Heiko Becker committed
243 244 245 246 247 248
      apply Qle_bool_iff in valid_lo.
      apply Qle_Rle in valid_lo; auto.
    + apply Is_true_eq_true in valid_hi.
      unfold Qleb in *.
      apply Qle_bool_iff in valid_hi.
      apply Qle_Rle in valid_hi; auto.
249
  - case_eq (absenv (Unop u f)); intros intv err absenv_unop.
Heiko Becker's avatar
Heiko Becker committed
250 251 252 253 254 255 256 257 258 259
    destruct intv as [unop_lo unop_hi]; simpl.
    unfold validIntervalbounds in valid_bounds.
    rewrite absenv_unop in valid_bounds.
    case_eq (absenv f); intros intv_f err_f absenv_f.
    rewrite absenv_f in valid_bounds.
    apply Is_true_eq_left in valid_bounds.
    apply andb_prop_elim in valid_bounds.
    destruct valid_bounds as [valid_rec valid_unop].
    apply Is_true_eq_true in valid_rec.
    inversion eval_f; subst.
Raphaël Monat's avatar
Raphaël Monat committed
260
    + specialize (IHf v1 valid_rec valid_definedVars usedVars_subset valid_usedVars H2).
Heiko Becker's avatar
Heiko Becker committed
261
      rewrite absenv_f in IHf; simpl in IHf.
262 263 264 265 266 267
      (* TODO: Make lemma *)
      unfold isSupersetIntv in valid_unop.
      apply andb_prop_elim in valid_unop.
      destruct valid_unop as [valid_lo valid_hi].
      apply Is_true_eq_true in valid_lo; apply Is_true_eq_true in valid_hi.
      apply Qle_bool_iff in valid_lo; apply Qle_bool_iff in valid_hi.
268
      pose proof (interval_negation_valid (iv :=(Q2R (fst intv_f),(Q2R (snd intv_f)))) (a :=v1)) as negation_valid.
269 270 271
      unfold contained, negateInterval in negation_valid; simpl in *.
      apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
      destruct IHf.
272
      split.
273 274 275 276 277
      * eapply Rle_trans. apply valid_lo.
        rewrite Q2R_opp; lra.
      * eapply Rle_trans.
        Focus 2. apply valid_hi.
        rewrite Q2R_opp; lra.
Raphaël Monat's avatar
Raphaël Monat committed
278
    + specialize (IHf v1 valid_rec valid_definedVars usedVars_subset valid_usedVars H3).
Heiko Becker's avatar
Heiko Becker committed
279
      rewrite absenv_f in IHf; simpl in IHf.
280
      apply andb_prop_elim in valid_unop.
281
      destruct valid_unop as [nodiv0 valid_unop].
282 283 284 285 286 287 288 289 290 291 292 293
      (* TODO: Make lemma *)
      unfold isSupersetIntv in valid_unop.
      apply andb_prop_elim in valid_unop.
      destruct valid_unop as [valid_lo valid_hi].
      apply Is_true_eq_true in valid_lo; apply Is_true_eq_true in valid_hi.
      apply Qle_bool_iff in valid_lo; apply Qle_bool_iff in valid_hi.
      assert ((Q2R (ivhi intv_f) < 0)%R \/ (0 < Q2R (ivlo intv_f))%R) as nodiv0_prop.
       * apply Is_true_eq_true in nodiv0.
         apply le_neq_bool_to_lt_prop in nodiv0.
         destruct nodiv0.
         { left; rewrite <- Q2R0_is_0; apply Qlt_Rlt; auto. }
         { right; rewrite <- Q2R0_is_0; apply Qlt_Rlt; auto. }
294
       * pose proof (interval_inversion_valid (iv :=(Q2R (fst intv_f),(Q2R (snd intv_f)))) (a :=v1)) as inv_valid.
295 296 297
         unfold contained, invertInterval in inv_valid; simpl in *.
         apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
         destruct IHf.
298
         rewrite delta_0_deterministic; auto.
299
         unfold perturb; split.
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
         { eapply Rle_trans. apply valid_lo.
           destruct nodiv0_prop as [nodiv0_neg | nodiv0_pos].
           (* TODO: Extract lemma maybe *)
           - assert (0 < - (Q2R (snd intv_f)))%R as negation_pos by lra.
             assert (- (Q2R (snd intv_f)) <= - v1)%R as negation_flipped_hi by lra.
             apply Rinv_le_contravar in negation_flipped_hi; try auto.
             rewrite <- Ropp_inv_permute in negation_flipped_hi; try lra.
             rewrite <- Ropp_inv_permute in negation_flipped_hi; try lra.
             apply Ropp_le_contravar in negation_flipped_hi.
             repeat rewrite Ropp_involutive in negation_flipped_hi;
               rewrite Q2R_inv; auto.
             hnf; intros is_0.
             rewrite <- Q2R0_is_0 in nodiv0_neg.
             apply Rlt_Qlt in nodiv0_neg; lra.
           - rewrite Q2R_inv.
             apply Rinv_le_contravar; try lra.
             hnf; intros is_0.
             assert (Q2R (fst intv_f) <= Q2R (snd intv_f))%R by lra.
             rewrite <- Q2R0_is_0 in nodiv0_pos.
             apply Rlt_Qlt in nodiv0_pos; apply Rle_Qle in H2; lra.
         }
         { eapply Rle_trans.
           Focus 2. apply valid_hi.
           destruct nodiv0_prop as [nodiv0_neg | nodiv0_pos].
           - assert (Q2R (fst intv_f) < 0)%R as fst_lt_0 by lra.
             assert (0 < - (Q2R (fst intv_f)))%R as negation_pos by lra.
             assert (- v1 <= - (Q2R (fst intv_f)))%R as negation_flipped_lo by lra.
             apply Rinv_le_contravar in negation_flipped_lo; try auto.
             rewrite <- Ropp_inv_permute in negation_flipped_lo; try lra.
             rewrite <- Ropp_inv_permute in negation_flipped_lo; try lra.
             apply Ropp_le_contravar in negation_flipped_lo.
             repeat rewrite Ropp_involutive in negation_flipped_lo;
               rewrite Q2R_inv; auto.
             hnf; intros is_0.
             rewrite <- Q2R0_is_0 in negation_pos.
             rewrite <- Q2R_opp in negation_pos.
             apply Rlt_Qlt in negation_pos; lra.
             assert (0 < - (Q2R (snd intv_f)))%R by lra.
             lra.
           - rewrite Q2R_inv.
             apply Rinv_le_contravar; try lra.
             hnf; intros is_0.
             assert (Q2R (fst intv_f) <= Q2R (snd intv_f))%R by lra.
             rewrite <- Q2R0_is_0 in nodiv0_pos.
             apply Rlt_Qlt in nodiv0_pos; apply Rle_Qle in H2; lra.
         }
346
  - inversion eval_f; subst.
347 348
    rewrite delta_0_deterministic in eval_f; auto.
    rewrite delta_0_deterministic; auto.
Heiko Becker's avatar
Heiko Becker committed
349
    simpl in valid_bounds.
Heiko Becker's avatar
Heiko Becker committed
350 351 352 353
    case_eq (absenv (Binop b f1 f2)); intros iv err absenv_bin.
    case_eq (absenv f1); intros iv1 err1 absenv_f1.
    case_eq (absenv f2); intros iv2 err2 absenv_f2.
    rewrite absenv_bin, absenv_f1, absenv_f2 in valid_bounds.
Heiko Becker's avatar
Heiko Becker committed
354 355 356 357 358 359
    apply Is_true_eq_left in valid_bounds.
    apply andb_prop_elim in valid_bounds.
    destruct valid_bounds as [valid_rec valid_bin].
    apply andb_prop_elim in valid_rec.
    destruct valid_rec as [valid_e1 valid_e2].
    apply Is_true_eq_true in valid_e1; apply Is_true_eq_true in  valid_e2.
360 361
    specialize (IHf1 v1 valid_e1 valid_definedVars);
      specialize (IHf2 v2 valid_e2 valid_definedVars).
Heiko Becker's avatar
Heiko Becker committed
362 363
    rewrite absenv_f1 in IHf1.
    rewrite absenv_f2 in IHf2.
364 365 366
    assert ((Q2R (fst (fst (iv1, err1))) <= v1 <= Q2R (snd (fst (iv1, err1))))%R) as valid_bounds_e1.
    + apply IHf1; try auto.
      intros v in_diff_e1.
Raphaël Monat's avatar
Raphaël Monat committed
367
      apply usedVars_subset.
368 369
      simpl. rewrite NatSet.diff_spec,NatSet.union_spec.
      rewrite NatSet.diff_spec in in_diff_e1.
Raphaël Monat's avatar
Raphaël Monat committed
370
      destruct in_diff_e1 as [ in_usedVars not_dVar].
371
      split; try auto.
372 373 374
    + assert (Q2R (fst (fst (iv2, err2))) <= v2 <= Q2R (snd (fst (iv2, err2))))%R as valid_bounds_e2.
      * apply IHf2; try auto.
        intros v in_diff_e2.
Raphaël Monat's avatar
Raphaël Monat committed
375
        apply usedVars_subset.
376 377 378
        simpl. rewrite NatSet.diff_spec, NatSet.union_spec.
        rewrite NatSet.diff_spec in in_diff_e2.
        destruct in_diff_e2; split; auto.
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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
      * destruct b; simpl in *.
        { pose proof (interval_addition_valid (iv1 :=(Q2R (fst iv1),Q2R (snd iv1))) (iv2 :=(Q2R (fst iv2), Q2R (snd iv2)))) as valid_add.
          unfold validIntervalAdd in valid_add.
          specialize (valid_add v1 v2 valid_bounds_e1 valid_bounds_e2).
          unfold contained in valid_add.
          unfold isSupersetIntv in valid_bin.
          apply andb_prop_elim in valid_bin; destruct valid_bin as [valid_lo valid_hi].
          apply Is_true_eq_true in valid_lo; apply Is_true_eq_true in valid_hi.
          apply Qle_bool_iff in valid_lo; apply Qle_bool_iff in valid_hi.
          apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
          destruct valid_add as [valid_add_lo valid_add_hi].
          split.
          - eapply Rle_trans. apply valid_lo.
            unfold ivlo. unfold addIntv.
            simpl in valid_add_lo.
            repeat rewrite <- Q2R_plus in valid_add_lo.
            rewrite <- Q2R_min4 in valid_add_lo.
            unfold absIvUpd; auto.
          - eapply Rle_trans.
            Focus 2.
            apply valid_hi.
            unfold ivlo, addIntv.
            simpl in valid_add_hi.
            repeat rewrite <- Q2R_plus in valid_add_hi.
            rewrite <- Q2R_max4 in valid_add_hi.
            unfold absIvUpd; auto. }
        { pose proof (interval_subtraction_valid (iv1 := (Q2R (fst iv1),Q2R (snd iv1))) (iv2 :=(Q2R (fst iv2), Q2R (snd iv2)))) as valid_sub.
          specialize (valid_sub v1 v2 valid_bounds_e1 valid_bounds_e2).
          unfold contained in valid_sub.
          unfold isSupersetIntv in valid_bin.
          apply andb_prop_elim in valid_bin; destruct valid_bin as [valid_lo valid_hi].
          apply Is_true_eq_true in valid_lo; apply Is_true_eq_true in valid_hi.
          apply Qle_bool_iff in valid_lo; apply Qle_bool_iff in valid_hi.
          apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
          destruct valid_sub as [valid_sub_lo valid_sub_hi].
          split.
          - eapply Rle_trans. apply valid_lo.
            unfold ivlo. unfold subtractIntv.
            simpl in valid_sub_lo.
            repeat rewrite <- Rsub_eq_Ropp_Rplus in valid_sub_lo.
            repeat rewrite <- Q2R_minus in valid_sub_lo.
            rewrite <- Q2R_min4 in valid_sub_lo.
            unfold absIvUpd; auto.
          - eapply Rle_trans.
            Focus 2.
            apply valid_hi.
            unfold ivlo, addIntv.
            simpl in valid_sub_hi.
            repeat rewrite <- Rsub_eq_Ropp_Rplus in valid_sub_hi.
            repeat rewrite <- Q2R_minus in valid_sub_hi.
            rewrite <- Q2R_max4 in valid_sub_hi.
            unfold absIvUpd; auto. }
        { pose proof (interval_multiplication_valid (iv1 :=(Q2R (fst iv1),Q2R (snd iv1))) (iv2:=(Q2R (fst iv2), Q2R (snd iv2)))) as valid_mul.
          specialize (valid_mul v1 v2 valid_bounds_e1 valid_bounds_e2).
          unfold contained in valid_mul.
          unfold isSupersetIntv in valid_bin.
          apply andb_prop_elim in valid_bin; destruct valid_bin as [valid_lo valid_hi].
          apply Is_true_eq_true in valid_lo; apply Is_true_eq_true in valid_hi.
          apply Qle_bool_iff in valid_lo; apply Qle_bool_iff in valid_hi.
          apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
          destruct valid_mul as [valid_mul_lo valid_mul_hi].
          split.
          - eapply Rle_trans. apply valid_lo.
            unfold ivlo. unfold multIntv.
            simpl in valid_mul_lo.
            repeat rewrite <- Q2R_mult in valid_mul_lo.
            rewrite <- Q2R_min4 in valid_mul_lo.
            unfold absIvUpd; auto.
          - eapply Rle_trans.
            Focus 2.
            apply valid_hi.
            unfold ivlo, addIntv.
            simpl in valid_mul_hi.
            repeat rewrite <- Q2R_mult in valid_mul_hi.
            rewrite <- Q2R_max4 in valid_mul_hi.
            unfold absIvUpd; auto. }
        { pose proof (interval_division_valid (a:=v1) (b:=v2) (iv1:=(Q2R (fst iv1), Q2R (snd iv1))) (iv2:=(Q2R (fst iv2),Q2R (snd iv2)))) as valid_div.
456
          rewrite <- andb_lazy_alt in valid_bin.
457 458
          unfold contained in valid_div.
          unfold isSupersetIntv in valid_bin.
459 460
          apply andb_prop_elim in valid_bin; destruct valid_bin as [nodiv0 valid_bin].
          (** CONTINUE **)
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
          apply andb_prop_elim in valid_bin; destruct valid_bin as [valid_lo valid_hi].
          apply Is_true_eq_true in valid_lo; apply Is_true_eq_true in valid_hi.
          apply Qle_bool_iff in valid_lo; apply Qle_bool_iff in valid_hi.
          apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
          apply orb_prop_elim in nodiv0.
          assert (snd iv2 < 0 \/ 0 < fst iv2).
          - destruct nodiv0 as [lt_0 | lt_0];
              apply andb_prop_elim in lt_0; destruct lt_0 as [le_0 neq_0];
                apply Is_true_eq_true in le_0; apply Is_true_eq_true in neq_0;
                  apply negb_true_iff in neq_0; apply Qeq_bool_neq in neq_0;
                    rewrite Qle_bool_iff in le_0;
                    rewrite Qle_lteq in le_0; destruct le_0 as [lt_0 | eq_0];
                      [ | exfalso; apply neq_0; auto | | exfalso; apply neq_0; symmetry; auto]; auto.
          - destruct valid_div as [valid_div_lo valid_div_hi]; simpl; try auto.
            + rewrite <- Q2R0_is_0.
              destruct H; [left | right]; apply Qlt_Rlt; auto.
            + unfold divideInterval, IVlo, IVhi in valid_div_lo, valid_div_hi.
              simpl in *.
              assert (Q2R (fst iv2) <= (Q2R (snd iv2)))%R by lra.
              assert (~ snd iv2 == 0).
              * destruct H; try lra.
                hnf; intros ivhi2_0.
                apply Rle_Qle in H0.
                rewrite ivhi2_0 in H0.
                lra.
              * assert (~ fst iv2 == 0).
                { destruct H; try lra.
                  hnf; intros ivlo2_0.
                  apply Rle_Qle in H0.
                  rewrite ivlo2_0 in H0.
                  lra. }
                { split.
                  - eapply Rle_trans. apply valid_lo.
                    unfold ivlo. unfold multIntv.
                    simpl in valid_div_lo.
                    rewrite <- Q2R_inv in valid_div_lo; [ | auto].
                    rewrite <- Q2R_inv in valid_div_lo; [ | auto].
                    repeat rewrite <- Q2R_mult in valid_div_lo.
                    rewrite <- Q2R_min4 in valid_div_lo; auto.
                  - eapply Rle_trans.
                    Focus 2.
                    apply valid_hi.
                    simpl in valid_div_hi.
                    rewrite <- Q2R_inv in valid_div_hi; [ | auto].
                    rewrite <- Q2R_inv in valid_div_hi; [ | auto].
                    repeat rewrite <- Q2R_mult in valid_div_hi.
                    rewrite <- Q2R_max4 in valid_div_hi; auto. } }
508
Qed.
509 510

Theorem validIntervalboundsCmd_sound (f:cmd Q) (absenv:analysisResult):
511 512 513 514
  forall E vR fVars dVars outVars elo ehi err P,
    ssaPrg f (NatSet.union fVars dVars) outVars ->
    bstep (toRCmd f) E 0%R vR  ->
    (forall v, NatSet.mem v dVars = true ->
515 516
          exists vR,
            E v = Some vR /\
517 518 519 520 521
            (Q2R (fst (fst (absenv (Var Q v)))) <= vR <= Q2R (snd (fst (absenv (Var Q v)))))%R) ->
    (forall v, NatSet.mem v fVars = true ->
          exists vR,
            E v = Some vR /\
            (Q2R (fst (P v)) <= vR <= Q2R (snd (P v)))%R) ->
Raphaël Monat's avatar
Raphaël Monat committed
522
    NatSet.Subset (NatSet.diff (Commands.usedVars f) dVars) fVars ->
523
    validIntervalboundsCmd f absenv P dVars = true ->
524
    absenv (getRetExp f) = ((elo, ehi), err) ->
Heiko Becker's avatar
Heiko Becker committed
525
    (Q2R elo <=  vR <= Q2R ehi)%R.
526 527
Proof.
  induction f;
528
    intros E vR fVars dVars outVars elo ehi err P ssa_f eval_f dVars_sound
Raphaël Monat's avatar
Raphaël Monat committed
529
           fVars_valid usedVars_subset valid_bounds_f absenv_f.
530 531 532 533
  - inversion ssa_f; subst.
    inversion eval_f; subst.
    unfold validIntervalboundsCmd in valid_bounds_f.
    andb_to_prop valid_bounds_f.
534 535
    inversion ssa_f; subst.
    specialize (IHf (updEnv n v E) vR fVars (NatSet.add n dVars)).
536
    eapply IHf; eauto.
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    + assert (NatSet.Equal (NatSet.add n (NatSet.union fVars dVars)) (NatSet.union fVars (NatSet.add n dVars))).
      * hnf. intros a; split; intros in_set.
        { rewrite NatSet.add_spec, NatSet.union_spec in in_set.
          rewrite NatSet.union_spec, NatSet.add_spec.
          destruct in_set as [P1 | [ P2 | P3]]; auto. }
        { rewrite NatSet.add_spec, NatSet.union_spec.
          rewrite NatSet.union_spec, NatSet.add_spec in in_set.
          destruct in_set as [P1 | [ P2 | P3]]; auto. }
      * eapply ssa_equal_set; eauto.
        symmetry; eauto.
    + intros v0 mem_v0.
      unfold updEnv.
      case_eq (v0 =? n); intros v0_eq.
      * rename R1 into eq_lo;
          rename R0 into eq_hi.
        apply Qeq_bool_iff in eq_lo;
          apply Qeq_eqR in eq_lo.
        apply Qeq_bool_iff in eq_hi;
          apply Qeq_eqR in eq_hi.
        rewrite Nat.eqb_eq in v0_eq; subst.
        rewrite <- eq_lo, <- eq_hi.
        exists v; split; auto.
        eapply validIntervalbounds_sound; eauto.
Raphaël Monat's avatar
Raphaël Monat committed
560 561 562
        simpl in usedVars_subset.
        hnf. intros a in_usedVars.
        apply usedVars_subset.
563
        rewrite NatSet.diff_spec, NatSet.remove_spec, NatSet.union_spec.
Raphaël Monat's avatar
Raphaël Monat committed
564 565
        rewrite NatSet.diff_spec in in_usedVars.
        destruct in_usedVars as [ in_usedVars not_dVar].
566 567
        repeat split; try auto.
        { hnf; intros; subst.
Raphaël Monat's avatar
Raphaël Monat committed
568 569
          apply validVars_subset_usedVars in H2.
          specialize (H2 n in_usedVars).
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
          rewrite <- NatSet.mem_spec in H2.
          rewrite H2 in H5; congruence. }
      * apply dVars_sound. rewrite NatSet.mem_spec.
        rewrite NatSet.mem_spec in mem_v0.
        rewrite NatSet.add_spec in mem_v0.
        destruct mem_v0; try auto.
        rewrite Nat.eqb_neq in v0_eq.
        exfalso; apply v0_eq; auto.
    + intros v0 mem_fVars.
      unfold updEnv.
      case_eq (v0 =? n); intros case_v0; auto.
      rewrite Nat.eqb_eq in case_v0; subst.
      assert (NatSet.mem n (NatSet.union fVars dVars) = true) as in_union.
      * rewrite NatSet.mem_spec, NatSet.union_spec; rewrite <- NatSet.mem_spec; auto.
      * rewrite in_union in *; congruence.
    + clear L R1 R0 R IHf.
      hnf. intros a a_freeVar.
      rewrite NatSet.diff_spec in a_freeVar.
      destruct a_freeVar as [a_freeVar a_no_dVar].
Raphaël Monat's avatar
Raphaël Monat committed
589
      apply usedVars_subset.
590 591 592 593 594 595 596 597 598
      simpl.
      rewrite NatSet.diff_spec, NatSet.remove_spec, NatSet.union_spec.
      repeat split; try auto.
      * hnf; intros; subst.
        apply a_no_dVar.
        rewrite NatSet.add_spec; auto.
      * hnf; intros a_dVar.
        apply a_no_dVar.
        rewrite NatSet.add_spec; auto.
599 600 601
  - unfold validIntervalboundsCmd in valid_bounds_f.
    inversion eval_f; subst.
    unfold updEnv.
602
    assert (Q2R (fst (fst (absenv e))) <= vR <= Q2R (snd (fst (absenv e))))%R.
603 604
    + eapply validIntervalbounds_sound; eauto.
    + simpl in *. rewrite absenv_f in *; auto.
605
Qed.