Expressions.v 11.6 KB
Newer Older
1
(**
2
  Formalization of the base expression language for the daisy framework
3
 **)
4
Require Import Coq.Reals.Reals Coq.micromega.Psatz Coq.QArith.QArith Coq.QArith.Qreals.
5
Require Import Daisy.Infra.RealRationalProps Daisy.Infra.RationalSimps Daisy.Infra.Ltacs.
6
Require Export Daisy.Infra.Abbrevs Daisy.Infra.RealSimps Daisy.Infra.NatSet Daisy.IntervalArithQ Daisy.IntervalArith Daisy.Infra.MachineType.
7

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) :=
='s avatar
= committed
15 16 17 18 19 20
  match b1, b2 with
  | Plus, Plus => true
  | Sub,  Sub  => true
  | Mult, Mult => true
  | Div,  Div  => true
  | _,_ => false
21 22
  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
Lemma binopEqBool_refl b:
  binopEqBool b b = true.
Proof.
  case b; auto.
Qed.

41 42 43 44 45 46
Lemma binopEqBool_prop b1 b2:
  binopEqBool b1 b2 = true <-> b1 = b2.
Proof.
  split; case b1; case b2; intros; simpl in *; try congruence; auto.
Qed.

47 48 49 50 51 52
(**
   Expressions will use unary operators.
   Define them first
 **)
Inductive unop: Type := Neg | Inv.

53
Definition unopEqBool (o1:unop) (o2:unop) :=
='s avatar
= committed
54 55 56 57
  match o1, o2 with
  | Neg, Neg => true
  | Inv, Inv => true
  | _ , _ => false
58 59
  end.

60 61 62 63 64 65
Lemma unopEqBool_refl b:
  unopEqBool b b = true.
Proof.
  case b; auto.
Qed.

66 67 68 69 70 71
Lemma unopEqBool_prop b1 b2:
  unopEqBool b1 b2 = true <-> b1 = b2.
Proof.
  split; case b1; case b2; intros; simpl in *; try congruence; auto.
Qed.

72 73
(**
   Define evaluation for unary operators on reals.
74
   Errors are added in the expression evaluation level later.
75
 **)
76
Definition evalUnop (o:unop) (v:R):=
77 78 79 80 81
  match o with
  |Neg => (- v)%R
  |Inv => (/ v)%R
  end .

82
(**
83 84
  Define expressions parametric over some value type V.
  Will ease reasoning about different instantiations later.
85
**)
86 87
Inductive exp (V:Type): Type :=
  Var: nat -> exp V
88
| Const: mType -> V -> exp V
89
| Unop: unop -> exp V -> exp V
90 91
| Binop: binop -> exp V -> exp V -> exp V
| Downcast: mType -> exp V -> exp V.
92

93 94 95 96
(**
  Boolean equality function on expressions.
  Used in certificates to define the analysis result as function
**)
97
Fixpoint expEqBool (e1:exp Q) (e2:exp Q) :=
='s avatar
= committed
98
  match e1, e2 with
99
  | Var _ v1, Var _ v2 => (v1 =? v2)
='s avatar
= committed
100 101 102 103 104
  | Const m1 n1, Const m2 n2 => andb (mTypeEqBool m1 m2) (Qeq_bool n1 n2)
  | Unop o1 e11, Unop o2 e22 => andb (unopEqBool o1 o2) (expEqBool e11 e22)
  | Binop o1 e11 e12, Binop o2 e21 e22 => andb (binopEqBool o1 o2) (andb (expEqBool e11 e21) (expEqBool e12 e22))
  | Downcast m1 f1, Downcast m2 f2 => andb (mTypeEqBool m1 m2) (expEqBool f1 f2)
  | _, _ => false
105 106
  end.

107
Lemma expEqBool_refl e:
108 109
  expEqBool e e = true.
Proof.
='s avatar
= committed
110
  induction e; try (apply andb_true_iff; split); simpl in *; auto; try (apply EquivEqBoolEq; auto).
111 112 113 114 115 116 117
  - symmetry; apply beq_nat_refl.
  - apply Qeq_bool_iff; lra.
  - case u; auto.
  - case b; auto.
  - apply andb_true_iff; split.
    apply IHe1. apply IHe2.
Qed.
118

119 120 121 122 123
Lemma expEqBool_sym e e':
  expEqBool e e' = expEqBool e' e.
Proof.
  revert e'.
  induction e; intros e'; destruct e'; simpl; try auto.
124
  - apply beq_nat_sym.
125 126 127 128 129 130
  - f_equal.
    + apply mTypeEqBool_sym; auto.
    + apply Qeq_bool_sym.
  - f_equal.
    + destruct u; auto.
    + apply IHe.
='s avatar
= committed
131
  - f_equal.
132 133 134 135 136 137 138 139 140
    + destruct b; auto.
    + f_equal.
      * apply IHe1.
      * apply IHe2.
  - f_equal.
    + apply mTypeEqBool_sym; auto.
    + apply IHe.
Qed.

='s avatar
= committed
141 142 143 144 145
Lemma expEqBool_trans e f g:
  expEqBool e f = true ->
  expEqBool f g = true ->
  expEqBool e g = true.
Proof.
146
  revert e f g; induction e;
147 148 149 150 151 152 153 154
    destruct f; intros g eq1 eq2;
      destruct g; simpl in *; try congruence;
        try rewrite Nat.eqb_eq in *;
        subst; try auto.
  - andb_to_prop eq1;
      andb_to_prop eq2.
    apply EquivEqBoolEq in L.
    apply EquivEqBoolEq in L0; subst.
='s avatar
= committed
155
    rewrite mTypeEqBool_refl; simpl.
156 157 158 159 160
    rewrite Qeq_bool_iff in *; lra.
  - andb_to_prop eq1;
      andb_to_prop eq2.
    rewrite unopEqBool_prop in *; subst.
    rewrite unopEqBool_refl; simpl.
='s avatar
= committed
161
    eapply IHe; eauto.
162 163 164 165 166 167 168 169 170
  - andb_to_prop eq1;
      andb_to_prop eq2.
    rewrite binopEqBool_prop in *; subst.
    rewrite binopEqBool_refl; simpl.
    apply andb_true_iff.
    split; [eapply IHe1; eauto | eapply IHe2; eauto].
  - andb_to_prop eq1;
      andb_to_prop eq2.
    rewrite EquivEqBoolEq in *; subst.
='s avatar
= committed
171 172 173 174
    rewrite mTypeEqBool_refl; simpl.
    eapply IHe; eauto.
Qed.

175 176
Fixpoint toRExp (e:exp Q) :=
  match e with
177
  |Var _ v => Var R v
178
  |Const m n => Const m (Q2R n)
179 180 181
  |Unop o e1 => Unop o (toRExp e1)
  |Binop o e1 e2 => Binop o (toRExp e1) (toRExp e2)
  |Downcast m e1 => Downcast m (toRExp e1)
182
  end.
183

184 185
Fixpoint toREval (e:exp R) :=
  match e with
186
  | Var _ v => Var R v
187
  | Const _ n => Const M0 n
188 189
  | Unop o e1 => Unop o (toREval e1)
  | Binop o e1 e2 => Binop o (toREval e1) (toREval e2)
190
  | Downcast _ e1 =>  (toREval e1)
191
  end.
192

='s avatar
= committed
193 194 195 196
Fixpoint toREvalVars (d:nat -> option mType) (n:nat) :=
  match d n with
  | Some m => Some M0
  | None => None
197
  end.
198

199 200 201 202
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
203
  (r * (1 + e))%R.
Heiko Becker's avatar
Heiko Becker committed
204

205
(**
206
Define expression evaluation relation parametric by an "error" epsilon.
207 208 209
The result value expresses float computations according to the IEEE standard,
using a perturbation of the real valued computation by (1 + delta), where
|delta| <= machine epsilon.
210
**)
211
Inductive eval_exp (E:env) (defVars: nat -> option mType) :(exp R) -> R -> mType -> Prop :=
212
| Var_load m x v:
213
    defVars x = Some m ->
214
    E x = Some v ->
215
    eval_exp E defVars (Var R x) v m
216 217
| Const_dist m n delta:
    Rle (Rabs delta) (Q2R (meps m)) ->
218
    eval_exp E defVars (Const m n) (perturb n delta) m
219
| Unop_neg m f1 v1:
220 221
    eval_exp E defVars f1 v1 m ->
    eval_exp E defVars (Unop Neg f1) (evalUnop Neg v1) m
222 223
| Unop_inv m f1 v1 delta:
    Rle (Rabs delta) (Q2R (meps m)) ->
224 225
    eval_exp E defVars  f1 v1 m ->
    eval_exp E defVars (Unop Inv f1) (perturb (evalUnop Inv v1) delta) m
226
| Downcast_dist m m1 f1 v1 delta:
227
    (* Downcast expression f1 (evaluating to machine type m1), to a machine type m, less precise than m1.*)
228 229
    isMorePrecise m1 m = true ->
    Rle (Rabs delta) (Q2R (meps m)) ->
230
    eval_exp E defVars f1 v1 m1 ->
231 232 233 234 235
    eval_exp E defVars (Downcast m f1) (perturb v1 delta) m
| Binop_dist m1 m2 op f1 f2 v1 v2 delta:
    Rle (Rabs delta) (Q2R (meps (computeJoin m1 m2))) ->
    eval_exp E defVars f1 v1 m1 ->
    eval_exp E defVars f2 v2 m2 ->
236
    ((op = Div) -> (~ v2 = 0)%R) ->
237
    eval_exp E defVars (Binop op f1 f2) (perturb (evalBinop op v1 v2) delta)  (computeJoin m1 m2).
238

239 240 241 242 243
(**
  Define the set of "used" variables of an expression to be the set of variables
  occuring in it
**)
Fixpoint usedVars (V:Type) (e:exp V) :NatSet.t :=
244 245
  match e with
  | Var _ x => NatSet.singleton x
246 247
  | Unop u e1 => usedVars e1
  | Binop b e1 e2 => NatSet.union (usedVars e1) (usedVars e2)
248
  | Downcast _ e1 => usedVars e1
249 250
  | _ => NatSet.empty
  end.
251

252
(**
253
  If |delta| <= 0 then perturb v delta is exactly v.
254
**)
255
Lemma delta_0_deterministic (v:R) (delta:R):
Heiko Becker's avatar
Heiko Becker committed
256 257 258 259 260
  (Rabs delta <= 0)%R ->
  perturb v delta = v.
Proof.
  intros abs_0; apply Rabs_0_impl_eq in abs_0; subst.
  unfold perturb.
261
  lra.
Heiko Becker's avatar
Heiko Becker committed
262 263
Qed.

264
(* TODO: need of `general` case? *)
265
Lemma general_meps_0_deterministic (f:exp R) (E:env) defVars:
266 267
  forall v1 v2 m1,
    m1 = M0 ->
268 269
    eval_exp E defVars (toREval f) v1 m1 ->
    eval_exp E defVars (toREval f) v2 M0 ->
270 271
    v1 = v2.
Proof.
272
  induction f; intros * m10_eq eval_v1 eval_v2.
273 274
  - 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.
275
    rewrite H6 in H1; inversion H1; subst; auto.
276 277 278 279
  - 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.
  - 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.
='s avatar
= committed
280
    + apply Ropp_eq_compat. apply (IHf v0 v3 M0); auto.
281 282
    + inversion H4.
    + inversion H5.
283
    + rewrite (IHf v0 v3 M0); auto.
284 285
  - 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.
286 287
    destruct m0; destruct m2; inversion H5.
    destruct m3; destruct m4; inversion H11.
288
    simpl in *.
289 290
    rewrite (IHf1 v0 v4 M0); auto.
    rewrite (IHf2 v5 v3 M0); auto.
291 292 293
    rewrite Q2R0_is_0 in H2,H12.
    rewrite delta_0_deterministic; auto.
    rewrite delta_0_deterministic; auto.
294 295
  - simpl toREval in eval_v1.
    simpl toREval in eval_v2.
296
    apply (IHf v1 v2 m1); auto.
297 298
Qed.

299 300 301 302 303 304 305 306
(* Lemma rnd_0_deterministic f E m v: *)
(*   eval_exp E (toREval (Downcast m f)) v M0 <-> *)
(*   eval_exp E (toREval f) v M0. *)
(* Proof. *)
(*   split; intros. *)
(*   - simpl in H. auto. *)
(*   - simpl; auto. *)
(* Qed. *)
307

='s avatar
= committed
308

309
(**
310
Evaluation with 0 as machine epsilon is deterministic
311
**)
312
Lemma meps_0_deterministic (f:exp R) (E:env) defVars:
313
  forall v1 v2,
314 315
  eval_exp E defVars (toREval f) v1 M0 ->
  eval_exp E defVars (toREval f) v2 M0 ->
316 317
  v1 = v2.
Proof.
318
  intros v1 v2 ev1 ev2.
319
  assert (M0 = M0) by auto.
='s avatar
= committed
320
  apply (general_meps_0_deterministic f H ev1 ev2).
321 322
Qed.

323 324 325 326
(**
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
327
variables in the Environment.
328
 **)
329 330
Lemma binary_unfolding b f1 f2 m E vF defVars:
  eval_exp E defVars (Binop b f1 f2) vF m ->
331
  exists vF1 vF2 m1 m2,
332
    m = computeJoin m1 m2 /\
333 334
    eval_exp E defVars f1 vF1 m1 /\
    eval_exp E defVars f2 vF2 m2 /\
335
    eval_exp (updEnv 2 vF2 (updEnv 1 vF1 emptyEnv))
='s avatar
= committed
336
             (updDefVars 2 m2 (updDefVars 1 m1 defVars))
337
             (Binop b (Var R 1) (Var R 2)) vF m.
338
Proof.
339 340
  intros eval_float.
  inversion eval_float; subst.
341 342
  exists v1 ; exists v2; exists m1; exists m2; repeat split; try auto.
  eapply Binop_dist; eauto.
343 344 345 346
  - pose proof (isMorePrecise_refl m1).
    eapply Var_load; eauto.
  - pose proof (isMorePrecise_refl m2).
    eapply Var_load; eauto.
347 348
Qed.

349
(*
350 351 352
(**
Analogous lemma for unary expressions.
**)
353 354
Lemma unary_unfolding (e:exp R) (eps:R) (E:env) (v:R):
  (eval_exp eps E (Unop Inv e) v <->
355
   exists v1,
356 357
     eval_exp eps E e v1 /\
     eval_exp eps (updEnv 1 v1 E) (Unop Inv (Var R 1)) v).
358 359 360 361 362 363 364 365 366
Proof.
  split.
  - intros eval_un.
    inversion eval_un; subst.
    exists v1.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
  - intros exists_val.
367 368
    destruct exists_val as [v1 [eval_f1 eval_e_E]].
    inversion eval_e_E; subst.
369 370 371
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
372
    inversion H3; subst; auto.
373
Qed. *)
374

375
(*   Using the parametric expressions, define boolean expressions for conditionals *)
376
(* **)
377 378 379
(* 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 boolean expressions
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. *)