Expressions.v 10.1 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 6
Require Import Daisy.Infra.RealRationalProps.
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
(** TODO: simplify pattern matching **)
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
Lemma binopEqBool_refl b:
  binopEqBool b b = true.
Proof.
  case b; auto.
Qed.

41 42 43 44 45 46
(**
   Expressions will use unary operators.
   Define them first
 **)
Inductive unop: Type := Neg | Inv.

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

63 64


65
(**
66 67
  Define expressions parametric over some value type V.
  Will ease reasoning about different instantiations later.
68
**)
69
Inductive exp (V:Type): Type :=
70
  Var: mType -> nat -> exp V
71
| Const: mType -> V -> exp V
72
| Unop: unop -> exp V -> exp V
73 74
| Binop: binop -> exp V -> exp V -> exp V
| Downcast: mType -> exp V -> exp V.
75

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

109 110 111 112 113 114 115 116 117 118 119
Lemma expEqBool_refl e:
  expEqBool e e = true.
Proof.
  induction e; apply andb_true_iff; split; simpl in *; auto; try (apply EquivEqBoolEq; auto). 
  - 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.
120 121 122 123

Fixpoint toRExp (e:exp Q) :=
  match e with
  |Var _ m v => Var R m v
124
  |Const m n => Const m (Q2R n)
125 126 127 128 129 130 131 132
  |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
133
  | Const _ n => Const M0 n
134 135
  | Unop o e1 => Unop o (toREval e1)
  | Binop o e1 e2 => Binop o (toREval e1) (toREval e2)
136
  | Downcast _ e1 =>  (toREval e1)
137
  end.
138

139 140 141 142 143 144 145 146 147
Definition toREvalEnv (E:env) : env :=
  fun (n:nat) =>
    let s := (E n) in
    match s with
    | None => None
    | Some (r, _) => Some (r, M0)
    end.


148 149 150 151
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
152
  (r * (1 + e))%R.
Heiko Becker's avatar
Heiko Becker committed
153

154
(**
155
Define expression evaluation relation parametric by an "error" epsilon.
156 157 158
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.
159
**)
160
Inductive eval_exp (E:env) :(exp R) -> R -> mType -> Prop :=
161 162
| Var_load m (*m1*) x v:
    (* isMorePrecise m m1 = true ->*)
163
    (**mTypeEqBool m m1 = true ->*)
164 165
    E x = Some (v, m) ->
    eval_exp E (Var R m x) v m
166 167
| Const_dist m n delta:
    Rle (Rabs delta) (Q2R (meps m)) ->
168
    eval_exp E (Const m n) (perturb n delta) m
169 170 171 172 173 174 175 176 177 178 179 180 181 182
| 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:
183
    (* Downcast expression f1 (evaluating to machine type m1), to a machine type m, less precise than m1.*)
184 185 186 187
    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.
188

189 190 191 192 193
(**
  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 :=
194
  match e with
195
  | Var _ _ x => NatSet.singleton x
196 197
  | Unop u e1 => usedVars e1
  | Binop b e1 e2 => NatSet.union (usedVars e1) (usedVars e2)
198
  | Downcast _ e1 => usedVars e1
199 200
  | _ => NatSet.empty
  end.
201

202
(**
203
  If |delta| <= 0 then perturb v delta is exactly v.
204
**)
205
Lemma delta_0_deterministic (v:R) (delta:R):
Heiko Becker's avatar
Heiko Becker committed
206 207 208 209 210
  (Rabs delta <= 0)%R ->
  perturb v delta = v.
Proof.
  intros abs_0; apply Rabs_0_impl_eq in abs_0; subst.
  unfold perturb.
211
  lra.
Heiko Becker's avatar
Heiko Becker committed
212 213
Qed.

214 215
    
Lemma general_meps_0_deterministic (f:exp R) (E:env):
216 217
  forall v1 v2 m1,
    m1 = M0 ->
218
    eval_exp E (toREval f) v1 m1 ->
219
    eval_exp E (toREval f) v2 M0 ->
220 221
    v1 = v2.
Proof.
222
  induction f; intros v1 v2 m1 m10_eq eval_v1 eval_v2.
223 224
  - 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.
225
    rewrite H7 in H3; inversion H3; subst; auto.
226 227 228 229
  - 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.
230
    + apply Ropp_eq_compat. apply (IHf v0 v3 M0); auto.     
231 232
    + inversion H4.
    + inversion H5.
233
    + rewrite (IHf v0 v3 M0); auto.
234 235
  - 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.
236 237 238 239 240 241 242 243
    assert (M0 = M0) as M00 by auto.
    pose proof (ifM0isJoin_l M0 m0 m2 M00 H2); auto.
    pose proof (ifM0isJoin_r M0 m0 m2 M00 H2); auto.
    pose proof (ifM0isJoin_l M0 m4 m5 M00 H11); auto.
    pose proof (ifM0isJoin_r M0 m4 m5 M00 H11); auto.
    subst.
    rewrite (IHf1 v0 v4 M0); auto.
    rewrite (IHf2 v5 v3 M0); auto.
244 245
  - simpl toREval in eval_v1.
    simpl toREval in eval_v2.
246
    apply (IHf v1 v2 m1); auto.
247 248 249 250
Qed.


  
251
(**
252
Evaluation with 0 as machine epsilon is deterministic
253
**)
254
Lemma meps_0_deterministic (f:exp R) (E:env):
255
  forall v1 v2,
256 257
  eval_exp E (toREval f) v1 M0 ->
  eval_exp E (toREval f) v2 M0 ->
258 259
  v1 = v2.
Proof.
260
  intros v1 v2 ev1 ev2.
261 262
  assert (M0 = M0) by auto.
  apply (general_meps_0_deterministic f H ev1 ev2). 
263 264
Qed.

265

266 267 268 269
(**
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
270
variables in the Environment.
271
**)
272 273 274 275 276 277 278
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.
279
Proof.
280 281
  intros eval_float.
  inversion eval_float; subst.
282 283 284 285 286 287 288 289
  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. *)
290 291
Qed.

292 293 294 295 296 297 298 299
(* (** *)
(* 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).
300
Proof.
301
  intros eval_un.
302
    inversion eval_un; subst.
303
    exists v1; exists m.
304
    repeat split; try auto.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    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. *)
320
Qed.
321

322 323 324 325 326 327
(**
  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.
328

329
(**
330
  Define evaluation of boolean expressions
331
 **)
332 333 334 335 336 337 338 339 340 341 342 343 344 345
(* 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. *)