Expressions.v 12.1 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 Import Daisy.Infra.RealRationalProps.
Require Export Daisy.Infra.Abbrevs Daisy.Infra.RealSimps Daisy.Infra.NatSet Daisy.IntervalArithQ Daisy.IntervalArith Daisy.Infra.MachineType.
8

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

15
Definition binopEqBool (b1:binop) (b2:binop) :=
16 17 18 19 20 21 22
  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.

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

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

41
Definition unopEqBool (o1:unop) (o2:unop) :=
42 43 44 45 46 47 48
  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.
49
   Errors are added in the expression evaluation level later.
50
 **)
51
Definition evalUnop (o:unop) (v:R):=
52 53 54 55 56
  match o with
  |Neg => (- v)%R
  |Inv => (/ v)%R
  end .

57 58


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

70 71 72 73
(**
  Boolean equality function on expressions.
  Used in certificates to define the analysis result as function
**)
74
Fixpoint expEqBool (e1:exp Q) (e2:exp Q) :=
75
  match e1 with
76
  |Var _ m1 v1 =>
77
   match e2 with
78
   |Var _ m2 v2 => andb (mTypeEqBool m1 m2) (v1 =? v2)
79 80 81 82
   | _=> false
   end
  |Const n1 =>
   match e2 with
83
   |Const n2 => (Qeq_bool n1 n2)
84 85
   | _=> false
   end
86 87
  |Unop o1 e11 =>
   match e2 with
88
   |Unop o2 e22 => andb (unopEqBool o1 o2) (expEqBool e11 e22)
89 90 91
   |_ => false
   end
  |Binop o1 e11 e12 =>
92
   match e2 with
93
   |Binop o2 e21 e22 => andb (binopEqBool o1 o2) (andb (expEqBool e11 e21) (expEqBool e12 e22))
94 95
   |_ => false
   end
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  |Downcast m1 f1 =>
   match e2 with
   |Downcast m2 f2 => andb (mTypeEqBool m1 m2) (expEqBool f1 f2)
   |_ => false                   
   end
  end.


Fixpoint toRExp (e:exp Q) :=
  match e with
  |Var _ m v => Var R m v
  |Const n => Const (Q2R n)
  |Unop o e1 => Unop o (toRExp e1)
  |Binop o e1 e2 => Binop o (toRExp e1) (toRExp e2)
  |Downcast m e1 => Downcast m (toRExp e1)
  end.

Fixpoint toREval (e:exp R) :=
  match e with
  | Var _ _ v => Var R M0 v
  | Const n => Const n
  | Unop o e1 => Unop o (toREval e1)
  | Binop o e1 e2 => Binop o (toREval e1) (toREval e2)
  | Downcast _ e1 => (toREval e1)
120
  end.
121

122 123 124 125 126 127 128 129 130
Definition toREvalEnv (E:env) : env :=
  fun (n:nat) =>
    let s := (E n) in
    match s with
    | None => None
    | Some (r, _) => Some (r, M0)
    end.


131 132 133 134
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
135
  (r * (1 + e))%R.
Heiko Becker's avatar
Heiko Becker committed
136

137
(**
138 139 140 141 142 143
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.
144
Unary negation is special! We do not have a new error here since IEE 754 gives us a sign bit
145
**)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
Inductive eval_exp (E:env) :(exp R) -> R -> mType -> Prop :=
| Var_load m m1 x v:
    isMorePrecise m m1 = true ->
    (**mTypeEqBool m m1 = true ->*)
    E x = Some (v, m1) ->
    eval_exp E (Var R m1 x) v m
| Const_dist m n delta:
    Rle (Rabs delta) (Q2R (meps m)) ->
    eval_exp E (Const n) (perturb n delta) m
| Unop_neg m f1 v1:
    eval_exp E f1 v1 m ->
    eval_exp E (Unop Neg f1) (evalUnop Neg v1) m
| Unop_inv m f1 v1 delta:
    Rle (Rabs delta) (Q2R (meps m)) ->
    eval_exp E f1 v1 m ->
    eval_exp E (Unop Inv f1) (perturb (evalUnop Inv v1) delta) m
| Binop_dist m m1 m2 op f1 f2 v1 v2 delta:
    isJoinOf m m1 m2 = true ->
    Rle (Rabs delta) (Q2R (meps m)) ->
    eval_exp E f1 v1 m1 ->
    eval_exp E f2 v2 m2 ->
    eval_exp E (Binop op f1 f2) (perturb (evalBinop op v1 v2) delta) m
| Downcast_dist m m1 f1 v1 delta:
    (*    Qle_bool (meps m1) (meps m) = true ->*)
    isMorePrecise m1 m = true ->
    Rle (Rabs delta) (Q2R (meps m)) ->
    eval_exp E f1 v1 m1 ->
    eval_exp E (Downcast m f1) (perturb v1 delta) m.
174 175 176

Fixpoint freeVars (V:Type) (e:exp V) :NatSet.t :=
  match e with
177
  | Var _ _ x => NatSet.singleton x
178 179
  | Unop u e1 => freeVars e1
  | Binop b e1 e2 => NatSet.union (freeVars e1) (freeVars e2)
180
  | Downcast _ e1 => freeVars e1
181 182
  | _ => NatSet.empty
  end.
183

184
(**
185
If |delta| <= 0 then perturb v delta is exactly v.
186
**)
187
Lemma delta_0_deterministic (v:R) (delta:R):
Heiko Becker's avatar
Heiko Becker committed
188 189 190 191 192
  (Rabs delta <= 0)%R ->
  perturb v delta = v.
Proof.
  intros abs_0; apply Rabs_0_impl_eq in abs_0; subst.
  unfold perturb.
193
  lra.
Heiko Becker's avatar
Heiko Becker committed
194 195
Qed.

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 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

(* Lemma compat_eexp (E:env) (f f':exp R) (v:R): *)
(*   forall m1, *)
(*     mTypeEq m1 M0 -> *)
(*     f' = toREval f -> *)
(*     eval_exp E f' v m1 <-> *)
(*     eval_exp E f' v M0. *)
(* Proof. *)
(*   intros m1 meq. *)
(*   induction f'; intros; split; intros; inversion H0; subst; auto. *)
(*   - constructor; auto. *)
(*     unfold isMorePrecise in *. *)
(*     unfold mTypeEqBool in *. *)
(*     case_eq (Qeq_bool (meps m) (meps M0)); intros; auto. *)
(*     rewrite H1 in H3. *)
(*     apply mTypeEquivs in meq; unfold mTypeEqBool in meq; rewrite meq in H3. *)
(*     inversion H3. *)
(*   - admit. *)
(*   - constructor; auto. *)
(*     unfold mTypeEq in meq. *)
(*     apply Qeq_eqR in meq. *)
(*     rewrite <- meq; auto. *)
(*   - admit. *)
(*   - constructor; auto. *)

(*   -  constructor. *)
(*     unfold mTypeEq in meq. *)
(*     apply Qeq_eqR in meq. *)
(*     rewrite meq in H. *)
(*     auto. *)
(*   - constructor. *)
(*     apply (IHeval_exp meq); auto. *)
(*   - constructor. *)
(*     unfold mTypeEq in meq; apply Qeq_eqR in meq; rewrite <- meq; auto. *)
(*     apply IHeval_exp; auto. *)
(*   - assert (isJoinOf M0 m1 m2 = true) as join012. *)
(*     apply (eq_compat_join m M0 m1 m2); auto. *)
(*     apply (Binop_dist M0 op delta join012); auto. *)
(*     unfold mTypeEq in meq; apply Qeq_eqR in meq; rewrite <- meq; auto. *)
(*   - apply (Downcast_dist m delta). *)
    
(* Qed. *)


(*     Lemma bla (m:mType) E f v: *)
(*   (meps m) == 0 -> *)
(*   eval_exp E (toREval f) v m <-> eval_exp E (toREval f) v M0. *)
(* Proof. *)
(*   intros. *)
(*   assert (mTypeEq m M0). *)
(*   unfold mTypeEq. *)
(*   rewrite H. *)
(*   simpl (meps M0). *)
(*   lra. *)
(*   rewrite H0. *)
(*   split; trivial. *)
(* Qed. *)

Lemma general_meps_0_deterministic (f:exp R) (E:env):
  forall v1 v2 m1 m2,
    (meps m1) == 0 ->
    (meps m2) == 0 ->
    eval_exp E (toREval f) v1 m1 ->
    eval_exp E (toREval f) v2 m2 ->
    v1 = v2.
Proof.
  induction f; intros v1 v2 m1 m2 meps1 meps2 eval_v1 eval_v2.
  - inversion eval_v1; inversion eval_v2; subst; auto;
      try repeat (repeat rewrite delta_0_deterministic; simpl in *; rewrite Q2R0_is_0 in *; subst; auto); simpl.
    rewrite H4 in H10; inversion H10; subst; auto.
  - inversion eval_v1; inversion eval_v2; subst; auto;
      try repeat (repeat rewrite delta_0_deterministic; simpl in *; rewrite Q2R0_is_0 in *; subst; auto); simpl.
    rewrite (Qeq_eqR (meps m1) 0 meps1) in H0.
    rewrite (Qeq_eqR (meps m2) 0 meps2) in H4.
    rewrite Q2R0_is_0 in *.
    rewrite delta_0_deterministic; auto. symmetry. rewrite delta_0_deterministic; auto.
  - inversion eval_v1; inversion eval_v2; subst; auto;
      try repeat (repeat rewrite delta_0_deterministic; simpl in *; rewrite Q2R0_is_0 in *; subst; auto); simpl.
    + apply Ropp_eq_compat. apply (IHf v0 v3 m1 m2); auto.     
    + inversion H4.
    + inversion H5.
    + rewrite (Qeq_eqR (meps m1) 0 meps1) in H1.
      rewrite (Qeq_eqR (meps m2) 0 meps2) in H7.
      rewrite Q2R0_is_0 in *.
      rewrite delta_0_deterministic; auto. symmetry. rewrite delta_0_deterministic; auto.
      rewrite (IHf v0 v3 m1 m2); auto.
  - inversion eval_v1; inversion eval_v2; subst; auto;
      try repeat (repeat rewrite delta_0_deterministic; simpl in *; rewrite Q2R0_is_0 in *; subst; auto); simpl.
    rewrite (Qeq_eqR (meps m1) 0 meps1) in H3.
    rewrite (Qeq_eqR (meps m2) 0 meps2) in H12.
    rewrite Q2R0_is_0 in *.
    rewrite delta_0_deterministic; auto. symmetry. rewrite delta_0_deterministic; auto.
    rewrite (IHf1 v0 v4 m0 m5); auto.
    rewrite (IHf2 v5 v3 m6 m3); auto.
    apply (ifM0isJoin_r m2 m5 m6); auto.
    apply (ifM0isJoin_r m1 m0 m3); auto.
    apply (ifM0isJoin_l m1 m0 m3); auto.
    apply (ifM0isJoin_l m2 m5 m6); auto.
  - simpl toREval in eval_v1.
    simpl toREval in eval_v2.
    apply (IHf v1 v2 m1 m2); auto.
Qed.


  
301
(**
302
Evaluation with 0 as machine epsilon is deterministic
303
**)
304
Lemma meps_0_deterministic (f:exp R) (E:env):
305
  forall v1 v2,
306 307
  eval_exp E (toREval f) v1 M0 ->
  eval_exp E (toREval f) v2 M0 ->
308 309
  v1 = v2.
Proof.
310 311 312
  intros v1 v2 ev1 ev2.
  assert ((meps M0) == 0) by (simpl; lra).
  apply (general_meps_0_deterministic f H H ev1 ev2). 
313 314
Qed.

315

316 317 318 319
(**
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
320
variables in the Eironment.
321
This relies on the property that variables are not perturbed as opposed to parameters
322
**)
323 324 325 326 327 328 329
Lemma binary_unfolding b f1 f2 m E vF:
  eval_exp E (Binop b f1 f2) vF m ->
  exists vF1 vF2 m1 m2,
  eval_exp E f1 vF1 m1 /\
  eval_exp E f2 vF2 m2 /\
  eval_exp  (updEnv 2 m2 vF2 (updEnv 1 m1 vF1 emptyEnv))
           (Binop b (Var R m1 1) (Var R m2 2)) vF m.
330
Proof.
331 332
  intros eval_float.
  inversion eval_float; subst.
333 334 335 336 337 338 339 340
  exists v1 ; exists v2; exists m1; exists m2; repeat split; try auto.
  eapply Binop_dist; eauto.
  pose proof (isMorePrecise_refl m1).
  eapply Var_load; eauto.
  pose proof (isMorePrecise_refl m2).
  (* unfold mTypeEqBool; apply Qeq_bool_iff; apply Qeq_refl. *)
  eapply Var_load; eauto.
  (* unfold mTypeEqBool; apply Qeq_bool_iff; apply Qeq_refl. *)
341 342
Qed.

343 344 345 346 347 348 349 350
(* (** *)
(* Analogous lemma for unary expressions. *)
(* **) *)
Lemma unary_unfolding (e:exp R) (m:mType) (E:env) (v:R):
  (eval_exp E (Unop Inv e) v m ->
   exists v1 m1,
     eval_exp E e v1 m1 /\
     eval_exp (updEnv 1 m1 v1 E) (Unop Inv (Var R m1 1)) v m).
351
Proof.
352
  intros eval_un.
353
    inversion eval_un; subst.
354
    exists v1; exists m.
355
    repeat split; try auto.
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    econstructor; try auto.
    pose proof (isMorePrecise_refl m).
    econstructor; eauto.
  (* - intros exists_val. *)
  (*   destruct exists_val as [v1 [m1 [eval_f1 eval_e_E]]]. *)
  (*   inversion eval_e_E; subst. *)
  (*   inversion H1; subst. *)
  (*   econstructor; eauto. *)
  (*   unfold updEnv in H6. *)
  (*   simpl in H6. *)
  (*   inversion H6. *)
  (*   rewrite <- H2. *)
    
  (*   rewrite <- H1. *)
  (*   auto. *)
371
Qed.
372

373

374 375 376 377 378 379
(**
  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.
380

381 382
(**
  Define evaluation of booleans for reals
383
 **)
384 385 386 387 388 389 390 391 392 393 394 395 396 397
(* Inductive bval (E:env): (bexp R) -> Prop -> Prop := *)
(*   leq_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R): *)
(*     eval_exp E f1 v1 -> *)
(*     eval_exp E f2 v2 -> *)
(*     bval E (leq f1 f2) (Rle v1 v2) *)
(* |less_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R): *)
(*     eval_exp E f1 v1 -> *)
(*     eval_exp E f2 v2 -> *)
(*     bval E (less f1 f2) (Rlt v1 v2). *)
(* (** *)
(*  Simplify arithmetic later by making > >= only abbreviations *)
(* **) *)
(* 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. *)