IntervalValidation.v 33.8 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
Require Import Daisy.Infra.Ltacs Daisy.Infra.RealSimps Daisy.Typing.
11
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 _ _ v =>
17 18 19
      if NatSet.mem v validVars
      then true
      else isSupersetIntv (P v) intv && (Qleb (ivlo (P v)) (ivhi (P v)))
20
    | Const _ n => isSupersetIntv (n,n) intv
Heiko Becker's avatar
Heiko Becker committed
21
    | 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
    | Downcast _ f1 =>
64 65 66
      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 81
  end.

Theorem ivbounds_approximatesPrecond_sound f absenv P V:
82 83
  validIntervalbounds f absenv P V = true ->
  forall v m, NatSet.In v (NatSet.diff (Expressions.usedVars f) V) ->
='s avatar
= committed
84
              (typeMap f) (Var Q m v) = Some m ->
85
       Is_true(isSupersetIntv (P v) (fst (absenv (Var Q m v)))).
86
Proof.
Heiko Becker's avatar
Heiko Becker committed
87
  induction f; unfold validIntervalbounds.
88 89
  - simpl. intros approx_true v m0 v_in_fV typef; simpl in *.
    case_eq (mTypeEqBool m m0 && (n =? v)); intros; rewrite H in typef; inversion typef; subst.
90 91 92
    rewrite NatSet.diff_spec in v_in_fV.
    rewrite NatSet.singleton_spec in v_in_fV;
      destruct v_in_fV; subst.
93
    destruct (absenv (Var Q m0 n)); simpl in *.
94 95 96 97
    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.
98 99 100
    + apply Is_true_eq_left in approx_true.
      apply andb_prop_elim in approx_true.
      destruct approx_true; auto.
101 102 103 104
  - intros approx_true v0 m0 v_in_fV typef; simpl in *.
    inversion v_in_fV. 
  - intros approx_unary_true v m0 v_in_fV typef; simpl in *.  
    unfold typeExpression in typef; inversion typef.
Heiko Becker's avatar
Heiko Becker committed
105
    apply Is_true_eq_left in approx_unary_true.
106
    simpl in *.
107
    destruct (absenv (Unop u f)); destruct (absenv f); simpl in *.
Heiko Becker's avatar
Heiko Becker committed
108 109 110 111
    apply andb_prop_elim in approx_unary_true.
    destruct approx_unary_true.
    apply IHf; try auto.
    apply Is_true_eq_true; auto.
112
  - intros approx_bin_true v m0 v_in_fV typef.
113 114 115 116
    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.
117
    apply Is_true_eq_left in approx_bin_true.
='s avatar
= committed
118 119 120 121 122
    case_eq (typeMap f1 (Var Q m0 v));
      case_eq (typeMap f2 (Var Q m0 v)); intros; auto; subst.
    + pose proof (typeMapVarDet _ _ _ H);
        pose proof (typeMapVarDet _ _ _ H0); subst.
      (* pose proof (detTypingBinop f1 f2 b _ _ typef H0 H) as [H01 H02]; subst. *)
123 124 125 126 127 128 129 130 131
      destruct (absenv (Binop b f1 f2)); destruct (absenv f1);
        destruct (absenv f2); simpl in *.
      apply andb_prop_elim in approx_bin_true.
      destruct approx_bin_true.
      apply andb_prop_elim in H1.
      destruct H1.
      apply IHf1; auto.
      apply Is_true_eq_true; auto.
      rewrite NatSet.diff_spec; split; auto.
132
      eapply typedVarIsUsed; eauto.
='s avatar
= committed
133
    + simpl in *; rewrite H0 in typef; inversion typef; subst.
134 135 136 137 138 139 140
      destruct (absenv (Binop b f1 f2)); destruct (absenv f1);
        destruct (absenv f2); simpl in *.
      apply andb_prop_elim in approx_bin_true.
      destruct approx_bin_true.
      apply andb_prop_elim in H1.
      destruct H1.
      apply IHf1; auto.
141
      apply Is_true_eq_true; auto.
142
      rewrite NatSet.diff_spec; split; auto.
143
      eapply typedVarIsUsed; eauto.
144 145 146 147 148 149 150 151
    + simpl in *; rewrite H0,H in typef; inversion typef; subst.
      destruct (absenv (Binop b f1 f2)); destruct (absenv f1);
        destruct (absenv f2); simpl in *.
      apply andb_prop_elim in approx_bin_true.
      destruct approx_bin_true.
      apply andb_prop_elim in H1.
      destruct H1.
      apply IHf2; auto.
152
      apply Is_true_eq_true; auto.
153
      rewrite NatSet.diff_spec; split; auto.
154
      eapply typedVarIsUsed; eauto.
155 156 157
    + simpl in *; rewrite H0,H in typef; inversion typef; subst.
  - intros approx_rnd_true v m0 v_in_fV typef.
    simpl in *; destruct (absenv (Downcast m f)); destruct (absenv f).
158 159 160 161 162 163
    apply Is_true_eq_left in approx_rnd_true.
    apply andb_prop_elim in approx_rnd_true.
    destruct approx_rnd_true.
    apply IHf; auto.
    apply Is_true_eq_true in H; auto.
Qed.
164

Heiko Becker's avatar
Heiko Becker committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
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).

180
Lemma validBoundsDiv_uneq_zero e1 e2 absenv P V ivlo_e2 ivhi_e2 err:
181
  absenv e2 = ((ivlo_e2,ivhi_e2), err) ->
182
  validIntervalbounds (Binop Div e1 e2) absenv P V = true ->
183 184 185 186 187 188 189
  (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.
190
  repeat (rewrite <- andb_lazy_alt in validBounds).
191 192 193
  apply Is_true_eq_left in validBounds.
  apply andb_prop_elim in validBounds.
  destruct validBounds as [_ validBounds]; apply andb_prop_elim in validBounds.
194
  destruct validBounds as [nodiv0 _].
195
  apply Is_true_eq_true in nodiv0.
196
  unfold isSupersetIntv in *; simpl in *.
197
  apply le_neq_bool_to_lt_prop; auto.
198 199
Qed.

200
Lemma validVarsUnfolding_l (E:env) (absenv:analysisResult) (f1 f2: exp Q) dVars (b:binop) m0:
='s avatar
= committed
201
  (typeMap (Binop b f1 f2)) (Binop b f1 f2) = Some m0 ->
202 203
  (forall (v : NatSet.elt) (m : mType),
      NatSet.mem v dVars = true ->
='s avatar
= committed
204
      typeMap (Binop b f1 f2) (Var Q m v) = Some m ->
205 206 207 208 209 210
      exists vR : R,
        E v = Some (vR, M0) /\
        (Q2R (fst (fst (absenv (Var Q m v)))) <= vR <= Q2R (snd (fst (absenv (Var Q m v)))))%R)
  ->
  (forall (v : NatSet.elt) (m : mType),
      NatSet.mem v dVars = true ->
='s avatar
= committed
211
      typeMap f1 (Var Q m v) = Some m ->
212 213 214 215 216 217
      exists vR : R,
        E v = Some (vR, M0) /\
        (Q2R (fst (fst (absenv (Var Q m v)))) <= vR <= Q2R (snd (fst (absenv (Var Q m v)))))%R).
Proof.
  intros.
  specialize (H0 v m H1).
='s avatar
= committed
218
  case_eq (typeMap f2 (Var Q m v)); intros; auto.
219 220
  - case_eq (mTypeEqBool m m1); intros.
    + (*apply EquivEqBoolEq in H4. ; rewrite <- H4 in H3.*)
='s avatar
= committed
221 222 223
      assert (typeMap (Binop b f1 f2) (Var Q m v) = Some m).
      simpl typeMap. rewrite H2. 
      auto. 
224
      specialize (H0 H5); auto.
='s avatar
= committed
225
    + pose proof (typeMapVarDet _ _ _ H3).
226 227 228
      rewrite H5 in H4.
      rewrite mTypeEqBool_refl in H4.
      inversion H4.
='s avatar
= committed
229
  - assert (typeMap (Binop b f1 f2) (Var Q m v) = Some m) by (simpl; rewrite H2; auto).
230 231
    specialize (H0 H4).
    auto.
='s avatar
= committed
232
Qed.
233

234
Lemma validVarsUnfolding_r (E:env) (absenv:analysisResult) (f1 f2: exp Q) dVars (b:binop) m0:
='s avatar
= committed
235
  (typeMap (Binop b f1 f2)) (Binop b f1 f2) = Some m0 ->
236 237
  (forall (v : NatSet.elt) (m : mType),
      NatSet.mem v dVars = true ->
='s avatar
= committed
238
      typeMap (Binop b f1 f2) (Var Q m v) = Some m ->
239 240 241 242 243 244
      exists vR : R,
        E v = Some (vR, M0) /\
        (Q2R (fst (fst (absenv (Var Q m v)))) <= vR <= Q2R (snd (fst (absenv (Var Q m v)))))%R)
  ->
  (forall (v : NatSet.elt) (m : mType),
      NatSet.mem v dVars = true ->
='s avatar
= committed
245
      typeMap f2 (Var Q m v) = Some m ->
246 247 248 249 250 251
      exists vR : R,
        E v = Some (vR, M0) /\
        (Q2R (fst (fst (absenv (Var Q m v)))) <= vR <= Q2R (snd (fst (absenv (Var Q m v)))))%R).
Proof.
  intros.
  specialize (H0 v m H1).
='s avatar
= committed
252
  case_eq (typeMap f1 (Var Q m v)); intros; auto.
253 254
  - case_eq (mTypeEqBool m1 m); intros.
    + (*apply EquivEqBoolEq in H4. ; rewrite <- H4 in H3.*)
='s avatar
= committed
255 256
      assert (typeMap (Binop b f1 f2) (Var Q m v) = Some m).
      simpl typeMap; rewrite H3. 
257 258
      apply EquivEqBoolEq in H4; rewrite H4; auto.
      specialize (H0 H5); auto.
='s avatar
= committed
259
    + pose proof (typeMapVarDet _ _ _ H3).
260 261 262
      rewrite H5 in H4.
      rewrite mTypeEqBool_refl in H4.
      inversion H4.
='s avatar
= committed
263
  - assert (typeMap (Binop b f1 f2) (Var Q m v) = Some m) by (simpl; rewrite H2,H3; auto).
264 265 266 267
    specialize (H0 H4).
    auto.
Qed.
    
268
Theorem validIntervalbounds_sound (f:exp Q) (absenv:analysisResult) (P:precond) fVars dVars (E:env):
269
  forall vR m,
='s avatar
= committed
270
    (typeMap f) f = Some m ->
271 272
    validIntervalbounds f absenv P dVars = true ->
    (forall v m, NatSet.mem v dVars = true ->
='s avatar
= committed
273
                 (typeMap f) (Var Q m v) = Some m ->
274
                 exists vR, E v = Some (vR, M0) /\
275
                (Q2R (fst (fst (absenv (Var Q m v)))) <= vR <= Q2R (snd (fst (absenv (Var Q m v)))))%R) ->
Raphaël Monat's avatar
Raphaël Monat committed
276
    NatSet.Subset (NatSet.diff (Expressions.usedVars f) dVars) fVars ->
277
    (forall v, NatSet.mem v fVars = true ->
278
          exists vR, E v = Some (vR, M0) /\
279
                (Q2R (fst (P v)) <= vR <= Q2R (snd (P v)))%R) ->
280
    eval_exp E (toREval (toRExp f)) vR M0 ->
281
  (Q2R (fst (fst (absenv f))) <= vR <= Q2R (snd (fst (absenv f))))%R.
282
Proof.
283
  induction f; intros vR mf typing_ok valid_bounds valid_definedVars usedVars_subset valid_usedVars eval_f.
284
  - unfold validIntervalbounds in valid_bounds.
285
    env_assert absenv (Var Q m n) absenv_var.
286
    destruct absenv_var as [ iv [err absenv_var]].
Raphaël Monat's avatar
Raphaël Monat committed
287
    specialize (valid_usedVars n).
288
    simpl; rewrite absenv_var in *; simpl in *.
289
    inversion eval_f; subst.
290
    case_eq (NatSet.mem n dVars); intros case_mem; rewrite case_mem in *; simpl in *.
291 292
    + specialize (valid_definedVars n m case_mem).
      assert (mTypeEqBool m m && (n =? n) = true).
293
      apply andb_true_iff; split; [ apply EquivEqBoolEq | rewrite <- beq_nat_refl ]; auto.
294 295 296
      (* rewrite H in valid_definedVars. *)
      (* assert (Some m = Some m) by auto. *)
      (* specialize (valid_definedVars H0). *)
297
      destruct valid_definedVars as [vR' [E_n_eq precond_sound]].
298
      rewrite H; auto.
299
      rewrite E_n_eq in *.
300
      inversion H2; subst.
301
      rewrite absenv_var in *; auto.
302 303 304
    + repeat (rewrite delta_0_deterministic in *; try auto).
      unfold isSupersetIntv in valid_bounds.
      andb_to_prop valid_bounds.
305 306 307 308
      apply Qle_bool_iff in L0;
        apply Qle_bool_iff in R0.
      apply Qle_Rle in L0;
        apply Qle_Rle in R0.
309
      simpl in *.
310 311 312 313
      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
314 315
        hnf in usedVars_subset.
        apply usedVars_subset.
316
        rewrite NatSet.diff_spec, NatSet.singleton_spec.
317
        split; try auto.
318 319 320
        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
321 322
      * specialize (valid_usedVars in_fVars);
          destruct valid_usedVars as [vR' [vR_def P_valid]].
323
        rewrite vR_def in H2; inversion H2; subst.
324
        lra.
325
  - unfold validIntervalbounds in valid_bounds.
326
    simpl in *;  destruct (absenv (Const m v)) as [intv err]; simpl in *.
327 328
    apply Is_true_eq_left in valid_bounds.
    apply andb_prop_elim in valid_bounds.
Heiko Becker's avatar
Heiko Becker committed
329
    destruct valid_bounds as [valid_lo valid_hi].
Heiko Becker's avatar
Heiko Becker committed
330
    inversion eval_f; subst.
331
    rewrite delta_0_deterministic; auto.
332 333
    unfold contained; simpl.
    split.
Heiko Becker's avatar
Heiko Becker committed
334
    + apply Is_true_eq_true in valid_lo.
335
      unfold Qleb in *.
Heiko Becker's avatar
Heiko Becker committed
336 337 338 339 340 341
      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.
342 343
    + simpl in H2; rewrite Q2R0_is_0 in H2; auto.
  - case_eq (absenv (Unop u f)); intros intv err absenv_unop.
Heiko Becker's avatar
Heiko Becker committed
344 345
    destruct intv as [unop_lo unop_hi]; simpl.
    unfold validIntervalbounds in valid_bounds.
346
    simpl in valid_bounds; rewrite absenv_unop in valid_bounds.
347
    case_eq (absenv f); intros intv_f err_f absenv_f.
Heiko Becker's avatar
Heiko Becker committed
348 349 350 351 352 353
    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.
='s avatar
= committed
354
    + assert (typeMap f f = Some mf) as typing_f_ok by (simpl in typing_ok; rewrite expEqBool_refl in typing_ok; apply typeGivesTypeMap; auto).
355
      specialize (IHf v1 mf typing_f_ok valid_rec valid_definedVars usedVars_subset valid_usedVars H3).
Heiko Becker's avatar
Heiko Becker committed
356
      rewrite absenv_f in IHf; simpl in IHf.
357 358 359 360 361 362
      (* 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.
363
      pose proof (interval_negation_valid (iv :=(Q2R (fst intv_f),(Q2R (snd intv_f)))) (a :=v1)) as negation_valid.
364 365 366
      unfold contained, negateInterval in negation_valid; simpl in *.
      apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
      destruct IHf.
367
      split.
368
      * eapply Rle_trans. apply valid_lo.
369 370
        rewrite Q2R_opp; lra.
      * eapply Rle_trans.
371
        Focus 2. apply valid_hi.
372
        rewrite Q2R_opp; lra.
='s avatar
= committed
373
    + assert (typeMap f f = Some mf) as typing_f_ok by (simpl in typing_ok; rewrite expEqBool_refl in typing_ok; apply typeGivesTypeMap; auto).
374
      specialize (IHf v1 mf typing_f_ok valid_rec valid_definedVars usedVars_subset valid_usedVars H4).
Heiko Becker's avatar
Heiko Becker committed
375
      rewrite absenv_f in IHf; simpl in IHf.
376
      apply andb_prop_elim in valid_unop.
377
      destruct valid_unop as [nodiv0 valid_unop].
378 379 380 381 382 383 384 385 386 387 388 389
      (* 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. }
390
       * pose proof (interval_inversion_valid (iv :=(Q2R (fst intv_f),(Q2R (snd intv_f)))) (a :=v1)) as inv_valid.
391 392 393
         unfold contained, invertInterval in inv_valid; simpl in *.
         apply Qle_Rle in valid_lo; apply Qle_Rle in valid_hi.
         destruct IHf.
394
         rewrite delta_0_deterministic; auto.
395
         unfold perturb; split.
396
         { eapply Rle_trans. apply valid_lo.
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
           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.
418
           Focus 2. apply valid_hi.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
           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.
         }
442
         { rewrite Q2R0_is_0 in H1; auto. }
443
  - inversion eval_f; subst.
444 445
    rewrite delta_0_deterministic in eval_f; auto.
    rewrite delta_0_deterministic; auto.
Heiko Becker's avatar
Heiko Becker committed
446
    simpl in valid_bounds.
447 448 449
    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.
450
    simpl.
Heiko Becker's avatar
Heiko Becker committed
451
    rewrite absenv_bin, absenv_f1, absenv_f2 in valid_bounds.
Heiko Becker's avatar
Heiko Becker committed
452 453 454 455 456 457
    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.
='s avatar
= committed
458 459 460 461
    destruct m1; destruct m2; cbv in H2; inversion H2.
    pose proof (typeMap_gives_type _ typing_ok).
    simpl in H. case_eq (typeExpression f1); intros; rewrite H0 in H; [ | inversion H ].
    case_eq (typeExpression f2); intros; rewrite H1 in H; inversion H.
462 463
    pose proof (validVarsUnfolding_l _ _ _ _ _ _ typing_ok valid_definedVars) as valid_definedVars_f1.
    pose proof (validVarsUnfolding_r _ _ _ _ _ _ typing_ok valid_definedVars) as valid_definedVars_f2.
='s avatar
= committed
464 465 466 467 468
    (* pose proof (binop_type_unfolding _ _ _ typing_ok) as subtypes. *)
    (* destruct subtypes as [mf1 [mf2 [typing_f1 [typing_f2 join_f1_f2]]]]. *)
    apply typeGivesTypeMap in H0. apply typeGivesTypeMap in H1.
    specialize (IHf1 v1 m H0 valid_e1 valid_definedVars_f1).
      specialize (IHf2 v2 m0 H1 valid_e2 valid_definedVars_f2).
Heiko Becker's avatar
Heiko Becker committed
469 470
    rewrite absenv_f1 in IHf1.
    rewrite absenv_f2 in IHf2.
471 472 473
    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
474
      apply usedVars_subset.
475 476
      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
477
      destruct in_diff_e1 as [ in_usedVars not_dVar].
478
      split; try auto.
479 480 481
    + 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
482
        apply usedVars_subset.
483 484 485
        simpl. rewrite NatSet.diff_spec, NatSet.union_spec.
        rewrite NatSet.diff_spec in in_diff_e2.
        destruct in_diff_e2; split; auto.
486 487 488 489 490 491 492 493 494 495 496 497
      * 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.
498
          - eapply Rle_trans. (*rewrite absenv_bin;*) apply valid_lo.
499 500 501 502 503 504 505
            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.
506
            (*rewrite absenv_bin;*) apply valid_hi.
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
            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.
522
          - eapply Rle_trans. (*rewrite absenv_bin;*) apply valid_lo.
523 524 525 526 527 528 529 530
            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.
531
            (*rewrite absenv_bin;*) apply valid_hi.
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
            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.
548
          - eapply Rle_trans. (*rewrite absenv_bin;*) apply valid_lo.
549 550 551 552 553 554 555
            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.
556
            (*rewrite absenv_bin;*) apply valid_hi.
557 558 559 560 561 562
            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.
563
          rewrite <- andb_lazy_alt in valid_bin.
564 565
          unfold contained in valid_div.
          unfold isSupersetIntv in valid_bin.
566 567
          apply andb_prop_elim in valid_bin; destruct valid_bin as [nodiv0 valid_bin].
          (** CONTINUE **)
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
          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.
='s avatar
= committed
583
              destruct H3; [left | right]; apply Qlt_Rlt; auto.
584 585 586 587 588 589
            + 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.
='s avatar
= committed
590 591
                apply Rle_Qle in H8.
                rewrite ivhi2_0 in H8.
592 593 594 595
                lra.
              * assert (~ fst iv2 == 0).
                { destruct H; try lra.
                  hnf; intros ivlo2_0.
='s avatar
= committed
596 597
                  apply Rle_Qle in H8.
                  rewrite ivlo2_0 in H8.
598 599
                  lra. }
                { split.
600
                  - eapply Rle_trans. (*rewrite absenv_bin;*) apply valid_lo.
601 602 603 604 605 606 607 608
                    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.
609
                    (*rewrite absenv_bin;*) apply valid_hi.
610 611 612 613 614
                    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. } }
615 616 617 618
    + destruct m1; destruct m2; inversion H2.
      simpl in H4; rewrite Q2R0_is_0 in H4; auto.
    + destruct m1; destruct m2; inversion H2.
      simpl in H4; rewrite Q2R0_is_0 in H4; auto.
619
  - unfold validIntervalbounds in valid_bounds.
620 621
    (*simpl erasure in valid_bounds.*)
    simpl in *; destruct (absenv (Downcast m f)); destruct (absenv f); simpl in *.
622 623 624 625
    apply Is_true_eq_left in valid_bounds.
    apply andb_prop_elim in valid_bounds.
    destruct valid_bounds as [vI1 vI2].
    apply andb_prop_elim in vI2.
626
    destruct vI2 as [vI2 vI2'].
627 628 629 630 631 632 633
    apply Is_true_eq_true in vI2.
    apply Is_true_eq_true in vI2'.
    assert (isEqIntv i i0) as Eq by (apply supIntvAntisym; auto).
    destruct Eq as [Eqlo Eqhi].
    simpl in *.
    apply Qeq_eqR in Eqlo; rewrite Eqlo.
    apply Qeq_eqR in Eqhi; rewrite Eqhi.
='s avatar
= committed
634 635 636
    pose proof (expEqBool_refl (Downcast m f)); simpl in H; rewrite H in typing_ok; inversion typing_ok; subst.
    case_eq (typeMap f f); intros. 
    +       apply (IHf vR m); auto.
637 638
      apply Is_true_eq_true in vI1; auto.
    + inversion typing_ok.
639
Qed.
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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
(* Unused, proof not up-to-date *)
(* Theorem validIntervalboundsCmd_sound (f:cmd Q) (absenv:analysisResult): *)
(*   forall E vR fVars dVars outVars elo ehi err P, *)
(*     ssaPrg f (NatSet.union fVars dVars) outVars -> *)
(*     bstep (toREvalCmd (toRCmd f)) E vR M0  -> *)
(*     (forall v m, NatSet.mem v dVars = true -> *)
(*           exists vR, *)
(*             E v = Some (vR, m) /\ *)
(*             (Q2R (fst (fst (absenv (Var Q m v)))) <= vR <= Q2R (snd (fst (absenv (Var Q m v)))))%R) -> *)
(*     (forall v m, NatSet.mem v fVars = true -> *)
(*           exists vR, *)
(*             E v = Some (vR, m) /\ *)
(*             (Q2R (fst (P v)) <= vR <= Q2R (snd (P v)))%R) -> *)
(*     NatSet.Subset (NatSet.diff (Commands.freeVars f) dVars) fVars -> *)
(*     validIntervalboundsCmd f  absenv P dVars = true -> *)
(*     absenv (getRetExp f) = ((elo, ehi), err) -> *)
(*     (Q2R elo <=  vR <= Q2R ehi)%R. *)
(* Proof. *)
(*   induction f; *)
(*     intros *  ssa_f eval_f dVars_sound fVars_valid usedVars_subset valid_bounds_f absenv_f. *)
(*   - inversion ssa_f; subst. *)
(*     inversion eval_f; subst. *)
(*     unfold validIntervalboundsCmd in valid_bounds_f. *)
(*     andb_to_prop valid_bounds_f. *)
(*     inversion ssa_f; subst. *)
(*     specialize (IHf (updEnv n m v E) vR fVars (NatSet.add n dVars)). *)
(*     eapply IHf; eauto. *)
(*     + 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. *)
(*     + admit. *)
(*     + *)
(*       intros v0 m0 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. *)
(*         simpl in usedVars_subset. *)
(*         hnf. intros a in_usedVars. *)
(*         apply usedVars_subset. *)
(*         rewrite NatSet.diff_spec, NatSet.remove_spec, NatSet.union_spec. *)
(*         rewrite NatSet.diff_spec in in_usedVars. *)
(*         destruct in_usedVars as [ in_usedVars not_dVar]. *)
(*         repeat split; try auto. *)
(*         { hnf; intros; subst. *)
(*           specialize (H5 n in_usedVars). *)
(*           rewrite <- NatSet.mem_spec in H5. *)
(*           rewrite H5 in H6; 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]. *)
(*       apply usedVars_subset. *)
(*       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. *)
(*   - unfold validIntervalboundsCmd in valid_bounds_f. *)
(*     inversion eval_f; subst. *)
(*     unfold updEnv. *)
(*     assert (Q2R (fst (fst (absenv (erasure e)))) <= vR <= Q2R (snd (fst (absenv (erasure e)))))%R. *)
(*     + simpl in valid_bounds_f; eapply validIntervalbounds_sound; eauto. *)
(*     + simpl in *. rewrite absenv_f in *; auto. *)
(* Qed. *)