Expressions.v 15.6 KB
Newer Older
1 2
(**
Formalization of the base expression language for the daisy framework
3
Required in all files, since we will always reason about expressions.
4
 **)
5
Require Import Coq.Reals.Reals Coq.micromega.Psatz Coq.QArith.QArith Coq.QArith.Qreals.
6 7
Require Export Daisy.Infra.Abbrevs Daisy.Infra.RealSimps Daisy.Infra.NatSet Daisy.IntervalArithQ Daisy.IntervalArith.

8 9 10 11 12
(**
  Expressions will use binary operators.
  Define them first
**)
Inductive binop : Type := Plus | Sub | Mult | Div.
13

14
Definition binopEqBool (b1:binop) (b2:binop) :=
15 16 17 18 19 20 21
  match b1 with
    Plus => match b2 with Plus => true |_ => false end
  | Sub => match b2 with Sub => true |_ => false end
  | Mult => match b2 with Mult => true |_ => false end
  | Div => match b2 with Div => true |_ => false end
  end.

22 23 24 25
(**
  Next define an evaluation function for binary operators on reals.
  Errors are added on the expression evaluation level later.
 **)
26
Definition evalBinop (o:binop) (v1:R) (v2:R) :=
27 28 29 30 31 32
  match o with
  | Plus => Rplus v1 v2
  | Sub => Rminus v1 v2
  | Mult => Rmult v1 v2
  | Div => Rdiv v1 v2
  end.
33 34 35 36 37 38 39

(**
   Expressions will use unary operators.
   Define them first
 **)
Inductive unop: Type := Neg | Inv.

40
Definition unopEqBool (o1:unop) (o2:unop) :=
41 42 43 44 45 46 47
  match o1 with
  |Neg => match o2 with |Neg => true |_=> false end
  |Inv => match o2 with |Inv => true |_ => false end
  end.

(**
   Define evaluation for unary operators on reals.
48
   Errors are added in the expression evaluation level later.
49
 **)
50
Definition evalUnop (o:unop) (v:R):=
51 52 53 54 55
  match o with
  |Neg => (- v)%R
  |Inv => (/ v)%R
  end .

56
(**
57 58
  Define expressions parametric over some value type V.
  Will ease reasoning about different instantiations later.
59
**)
60 61 62
Inductive exp (V:Type): Type :=
  Var: nat -> exp V
| Const: V -> exp V
63
| Unop: unop -> exp V -> exp V
64
| Binop: binop -> exp V -> exp V -> exp V.
65

66 67 68 69
(**
  Boolean equality function on expressions.
  Used in certificates to define the analysis result as function
**)
70
Fixpoint expEqBool (e1:exp Q) (e2:exp Q) :=
71 72 73 74 75 76 77 78 79 80 81
  match e1 with
  |Var _ v1 =>
   match e2 with
   |Var _ v2 => v1 =? v2
   | _=> false
   end
  |Const n1 =>
   match e2 with
   |Const n2 => Qeq_bool n1 n2
   | _=> false
   end
82 83
  |Unop o1 e11 =>
   match e2 with
84
   |Unop o2 e22 => andb (unopEqBool o1 o2) (expEqBool e11 e22)
85 86 87
   |_ => false
   end
  |Binop o1 e11 e12 =>
88
   match e2 with
89
   |Binop o2 e21 e22 => andb (binopEqBool o1 o2) (andb (expEqBool e11 e21) (expEqBool e12 e22))
90 91 92
   |_ => false
   end
  end.
93

94 95 96 97
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
98
  (r * (1 + e))%R.
Heiko Becker's avatar
Heiko Becker committed
99

100
(**
101 102 103 104 105 106
Define expression evaluation relation parametric by an "error" epsilon.
This value will be used later to express float computations using a perturbation
of the real valued computation by (1 + delta), where |delta| <= machine epsilon.

It is important that variables are not perturbed when loading from an environment.
This is the case, since loading a float value should not increase an additional error.
107
Unary negation is special! We do not have a new error here since IEE 754 gives us a sign bit
108
**)
109 110 111 112 113 114 115
Inductive eval_exp (eps:R) (E:env) (P:precond) :(exp R) -> R -> Prop :=
| Var_load x v delta_lo delta_hi:
    E x = Some v ->
    (Rabs delta_lo <= eps)%R ->
    (Rabs delta_hi <= eps)%R ->
    (perturb (Q2R (ivlo (P x))) delta_lo <= v <= perturb (Q2R (ivhi (P x))) delta_hi)%R ->
    eval_exp eps E P (Var R x) v
116 117
| Const_dist n delta:
    Rle (Rabs delta) eps ->
118
    eval_exp eps E P (Const n) (perturb n delta)
119
| Unop_neg f1 v1:
120 121
    eval_exp eps E P f1 v1 ->
    eval_exp eps E P (Unop Neg f1) (evalUnop Neg v1)
122
| Unop_inv f1 v1 delta:
123
    Rle (Rabs delta) eps ->
124 125
    eval_exp eps E P f1 v1 ->
    eval_exp eps E P (Unop Inv f1) (perturb (evalUnop Inv v1) delta)
126 127
| Binop_dist op f1 f2 v1 v2 delta:
    Rle (Rabs delta) eps ->
128 129 130 131 132 133 134 135 136 137 138
    eval_exp eps E P f1 v1 ->
    eval_exp eps E P f2 v2 ->
    eval_exp eps E P (Binop op f1 f2) (perturb (evalBinop op v1 v2) delta).

Fixpoint freeVars (V:Type) (e:exp V) :NatSet.t :=
  match e with
  | Var _ x => NatSet.singleton x
  | Unop u e1 => freeVars e1
  | Binop b e1 e2 => NatSet.union (freeVars e1) (freeVars e2)
  | _ => NatSet.empty
  end.
139

140
(**
141
If |delta| <= 0 then perturb v delta is exactly v.
142
**)
143
Lemma delta_0_deterministic (v:R) (delta:R):
Heiko Becker's avatar
Heiko Becker committed
144 145 146 147 148
  (Rabs delta <= 0)%R ->
  perturb v delta = v.
Proof.
  intros abs_0; apply Rabs_0_impl_eq in abs_0; subst.
  unfold perturb.
149
  lra.
Heiko Becker's avatar
Heiko Becker committed
150 151
Qed.

152
(**
153
Evaluation with 0 as machine epsilon is deterministic
154
**)
155
Lemma meps_0_deterministic (f:exp R) (E:env) (P:precond):
156
  forall v1 v2,
157 158
  eval_exp 0 E P f v1 ->
  eval_exp 0 E P f v2 ->
159 160
  v1 = v2.
Proof.
161 162
  induction f; intros v1 v2 eval_v1 eval_v2;
    inversion eval_v1; inversion eval_v2;
163
      repeat try rewrite delta_0_deterministic; subst; auto.
164 165
  - rewrite H6 in H0; inversion H0;
      subst; auto.
166
  - rewrite (IHf v0 v3); auto.
167 168
  - inversion H3.
  - inversion H4.
169 170 171
  - rewrite (IHf v0 v3); auto.
  - rewrite (IHf1 v0 v4); auto.
    rewrite (IHf2 v3 v5); auto.
172 173
Qed.

174 175 176 177
(**
Helping lemma. Needed in soundness proof.
For each evaluation of using an arbitrary epsilon, we can replace it by
evaluating the subexpressions and then binding the result values to different
178
variables in the Eironment.
179
This relies on the property that variables are not perturbed as opposed to parameters
180
**)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
Lemma binary_unfolding b f1 f2 eps E1 E2 P vF1 vF2 vR1 vR2 f1_lo f1_hi f2_lo f2_hi err1 err2 delta:
  eval_exp eps E2 P (Binop b f1 f2) (perturb (evalBinop b vF1 vF2) delta) ->
  eval_exp eps E2 P f1 vF1 ->
  eval_exp eps E2 P f2 vF2 ->
  eval_exp 0%R E1 P f1 vR1 ->
  eval_exp 0%R E1 P f2 vR2 ->
  contained vR1 (Q2R f1_lo, Q2R f1_hi) ->
  contained vR2 (Q2R f2_lo, Q2R f2_hi) ->
  (0 <= eps)%R ->
  (Rabs delta <= eps)%R ->
  (Rabs (vR1 - vF1) <= Q2R err1)%R ->
  (Rabs (vR2 - vF2) <= Q2R err2)%R ->
  eval_exp eps (updEnv 2 vF2 (updEnv 1 vF1 emptyEnv))
           (fun n => if n =? 1 then (f1_lo - err1, f1_hi + err1) else (f2_lo - err2, f2_hi + err2))
           (Binop b (Var R 1) (Var R 2)) (perturb (evalBinop b vF1 vF2) delta).
196
Proof.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 351 352 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 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
  intros eval_float eval_f1_float eval_f2_float eval_f1_real eval_f2_real
         vR1_contained vR2_contained err_pos delta_valid err1_bound err2_bound.
  pose proof (distance_gives_iv vF1 vR1_contained err1_bound) as vF1_err_iv.
  pose proof (distance_gives_iv vF2 vR2_contained err2_bound) as vF2_err_iv.
  assert (Rabs eps <= eps)%R as eps_valid by (rewrite Rabs_right; lra).
  assert (Rabs (-eps) <= eps)%R as neg_eps_valid.
  - hnf in err_pos.
    destruct err_pos.
    + rewrite Rabs_left; lra.
    + rewrite Rabs_right; lra.
  - constructor; try auto.
    + destruct (Rlt_le_dec (Q2R f1_lo - Q2R err1) 0) as [lo_lt_0 | lo_geq_0];
      destruct (Rlt_le_dec (Q2R f1_hi + Q2R err1) 0) as [hi_lt_0 | hi_geq_0].
      * econstructor; try auto.
        apply eps_valid.
        apply neg_eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF1_err_iv.
        simpl in *.
        destruct vF1_err_iv as [vF1_err_lo vF1_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF1_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_lo - Q2R err1)) at 3.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          eapply Rle_trans.
          apply Rmult_le_compat_r; try lra.
          hnf. left; apply lo_lt_0.
          repeat (rewrite Rmult_0_l); lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF1_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_hi + Q2R err1)) at 1.
          apply Rplus_le_compat; try lra.
          hnf in err_pos.
          destruct err_pos.
          - hnf; left; apply Rmult_lt_0_inverting; lra.
          - subst. lra. }
      * econstructor; try auto.
        apply eps_valid.
        apply eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF1_err_iv.
        simpl in *.
        destruct vF1_err_iv as [vF1_err_lo vF1_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF1_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_lo - Q2R err1)) at 3.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          eapply Rle_trans.
          apply Rmult_le_compat_r; try lra.
          hnf. left; apply lo_lt_0.
          repeat (rewrite Rmult_0_l); lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF1_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_hi + Q2R err1)) at 1.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          apply Rmult_le_compat; lra. }
      * econstructor; try auto.
        apply neg_eps_valid.
        apply neg_eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF1_err_iv.
        simpl in *.
        destruct vF1_err_iv as [vF1_err_lo vF1_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF1_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_lo - Q2R err1)) at 3.
          apply Rplus_le_compat; try lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF1_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_hi + Q2R err1)) at 1.
          apply Rplus_le_compat; try lra. }
      * econstructor; try auto.
        apply neg_eps_valid.
        apply eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF1_err_iv.
        simpl in *.
        destruct vF1_err_iv as [vF1_err_lo vF1_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF1_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_lo - Q2R err1)) at 3.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          rewrite Rmult_comm.
          eapply Rle_trans.
          eapply Rmult_le_compat_r; try lra.
          instantiate (1 := 0%R). lra.
          lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF1_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f1_hi + Q2R err1)) at 1.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          apply Rmult_le_compat; lra. }
    + destruct (Rlt_le_dec (Q2R f2_lo - Q2R err2) 0) as [lo_lt_0 | lo_geq_0];
      destruct (Rlt_le_dec (Q2R f2_hi + Q2R err2) 0) as [hi_lt_0 | hi_geq_0].
      * econstructor; try auto.
        apply eps_valid.
        apply neg_eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF2_err_iv.
        simpl in *.
        destruct vF2_err_iv as [vF2_err_lo vF2_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF2_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_lo - Q2R err2)) at 3.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          eapply Rle_trans.
          apply Rmult_le_compat_r; try lra.
          hnf. left; apply lo_lt_0.
          repeat (rewrite Rmult_0_l); lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF2_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_hi + Q2R err2)) at 1.
          apply Rplus_le_compat; try lra.
          hnf in err_pos.
          destruct err_pos.
          - hnf; left; apply Rmult_lt_0_inverting; lra.
          - subst. lra. }
      * econstructor; try auto.
        apply eps_valid.
        apply eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF2_err_iv.
        simpl in *.
        destruct vF2_err_iv as [vF2_err_lo vF2_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF2_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_lo - Q2R err2)) at 3.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          eapply Rle_trans.
          apply Rmult_le_compat_r; try lra.
          hnf. left; apply lo_lt_0.
          repeat (rewrite Rmult_0_l); lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF2_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_hi + Q2R err2)) at 1.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          apply Rmult_le_compat; lra. }
      * econstructor; try auto.
        apply neg_eps_valid.
        apply neg_eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF2_err_iv.
        simpl in *.
        destruct vF2_err_iv as [vF2_err_lo vF2_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF2_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_lo - Q2R err2)) at 3.
          apply Rplus_le_compat; try lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF2_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_hi + Q2R err2)) at 1.
          apply Rplus_le_compat; try lra. }
      * econstructor; try auto.
        apply neg_eps_valid.
        apply eps_valid.
        simpl.
        unfold perturb.
        unfold contained in vF2_err_iv.
        simpl in *.
        destruct vF2_err_iv as [vF2_err_lo vF2_err_hi].
        split;
          rewrite Rmult_plus_distr_l;
          rewrite Rmult_1_r.
        { rewrite Q2R_minus.
          eapply Rle_trans.
          Focus 2.
          apply vF2_err_lo.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_lo - Q2R err2)) at 3.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          rewrite Rmult_comm.
          eapply Rle_trans.
          eapply Rmult_le_compat_r; try lra.
          instantiate (1 := 0%R). lra.
          lra. }
        { rewrite Q2R_plus.
          eapply Rle_trans.
          apply vF2_err_hi.
          setoid_rewrite <- (Rplus_0_r (Q2R f2_hi + Q2R err2)) at 1.
          apply Rplus_le_compat; try lra.
          rewrite <- (Rmult_0_r 0).
          apply Rmult_le_compat; lra. }
436 437
Qed.

438 439

(*
440 441 442
(**
Analogous lemma for unary expressions.
**)
443 444
Lemma unary_unfolding (e:exp R) (eps:R) (E:env) (P:precond) (v:R):
  (eval_exp eps E P (Unop Inv e) v <->
445
   exists v1,
446 447
     eval_exp eps E P e v1 /\
     eval_exp eps (updEnv 1 v1 E) P (Unop Inv (Var R 1)) v).
448 449 450 451 452 453 454 455 456
Proof.
  split.
  - intros eval_un.
    inversion eval_un; subst.
    exists v1.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
  - intros exists_val.
457 458
    destruct exists_val as [v1 [eval_f1 eval_e_E]].
    inversion eval_e_E; subst.
459 460 461
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
462
    inversion H2; subst; auto.
463
Qed.
464
 *)
465

466 467 468 469 470 471 472 473
(**
  Using the parametric expressions, define boolean expressions for conditionals
**)
Inductive bexp (V:Type) : Type :=
  leq: exp V -> exp V -> bexp V
| less: exp V -> exp V -> bexp V.
(**
  Define evaluation of booleans for reals
474
 **)
475
Inductive bval (eps:R) (E:env) (P:precond): (bexp R) -> Prop -> Prop :=
476
  leq_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R):
477 478 479
    eval_exp eps E P f1 v1 ->
    eval_exp eps E P f2 v2 ->
    bval eps E P (leq f1 f2) (Rle v1 v2)
480
|less_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R):
481 482 483
    eval_exp eps E P f1 v1 ->
    eval_exp eps E P f2 v2 ->
    bval eps E P (less f1 f2) (Rlt v1 v2).
484 485 486
(**
 Simplify arithmetic later by making > >= only abbreviations
**)
487 488
Definition gr := fun (V:Type) (f1: exp V) (f2: exp V) => less f2 f1.
Definition greq := fun (V:Type) (f1:exp V) (f2: exp V) => leq f2 f1.