Expressions.v 6.58 KB
Newer Older
1

2
(**
3
  Formalization of the base expression language for the daisy framework
4
 **)
5
Require Import Coq.Reals.Reals Coq.micromega.Psatz Coq.QArith.QArith Coq.QArith.Qreals.
6
Require Export Daisy.Infra.Abbrevs Daisy.Infra.RealSimps Daisy.Infra.NatSet.
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) :=
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
Define expression evaluation relation parametric by an "error" epsilon.
102 103 104
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.
105
**)
106 107
Inductive eval_exp (eps:R) (E:env) :(exp R) -> R -> Prop :=
| Var_load x v:
108
    E x = Some v ->
109
    eval_exp eps E (Var R x) v
110 111
| Const_dist n delta:
    Rle (Rabs delta) eps ->
112
    eval_exp eps E (Const n) (perturb n delta)
113
| Unop_neg f1 v1:
114 115
    eval_exp eps E f1 v1 ->
    eval_exp eps E (Unop Neg f1) (evalUnop Neg v1)
116
| Unop_inv f1 v1 delta:
117
    Rle (Rabs delta) eps ->
118
    eval_exp eps E f1 v1 ->
119
    (~ v1 = 0)%R ->
120
    eval_exp eps E (Unop Inv f1) (perturb (evalUnop Inv v1) delta)
121 122
| Binop_dist op f1 f2 v1 v2 delta:
    Rle (Rabs delta) eps ->
123 124
    eval_exp eps E f1 v1 ->
    eval_exp eps E f2 v2 ->
125
    ((op = Div) -> (~ v2 = 0)%R) ->
126
    eval_exp eps E (Binop op f1 f2) (perturb (evalBinop op v1 v2) delta).
127

128 129 130 131 132
(**
  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 :=
133 134
  match e with
  | Var _ x => NatSet.singleton x
135 136
  | Unop u e1 => usedVars e1
  | Binop b e1 e2 => NatSet.union (usedVars e1) (usedVars e2)
137 138
  | _ => 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):
156
  forall v1 v2,
157 158
  eval_exp 0 E f v1 ->
  eval_exp 0 E 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
  - rewrite H3 in H0; inversion H0;
165
      subst; auto.
166
  - rewrite (IHf v0 v3); auto.
167
  - inversion H3.
168
  - inversion H5.
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 Environment.
179
**)
180 181 182 183 184
Lemma binary_unfolding b f1 f2 eps E vF:
  eval_exp eps E (Binop b f1 f2) vF ->
  exists vF1 vF2,
  eval_exp eps E f1 vF1 /\
  eval_exp eps E f2 vF2 /\
185
  eval_exp eps (updEnv 2 vF2 (updEnv 1 vF1 emptyEnv))
186
           (Binop b (Var R 1) (Var R 2)) vF.
187
Proof.
188 189 190 191 192 193 194 195
  intros eval_float.
  inversion eval_float; subst.
  exists v1 ; exists v2; repeat split; try auto.
  constructor; try auto.
  - constructor.
    unfold updEnv; cbv; auto.
  - constructor.
    unfold updEnv; cbv; auto.
196 197
Qed.

198 199 200
(**
Analogous lemma for unary expressions.
**)
201 202
Lemma unary_unfolding (e:exp R) (eps:R) (E:env) (v:R):
  (eval_exp eps E (Unop Inv e) v <->
203
   exists v1,
204 205
     eval_exp eps E e v1 /\
     eval_exp eps (updEnv 1 v1 E) (Unop Inv (Var R 1)) v).
206 207 208 209 210 211 212 213 214
Proof.
  split.
  - intros eval_un.
    inversion eval_un; subst.
    exists v1.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
  - intros exists_val.
215 216
    destruct exists_val as [v1 [eval_f1 eval_e_E]].
    inversion eval_e_E; subst.
217 218 219
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
220
    inversion H3; subst; auto.
221
Qed.
222

223 224 225 226 227 228
(**
  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.
229

230
(**
231
  Define evaluation of boolean expressions
232
 **)
233
Inductive bval (eps:R) (E:env): (bexp R) -> Prop -> Prop :=
234
  leq_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R):
235 236 237
    eval_exp eps E f1 v1 ->
    eval_exp eps E f2 v2 ->
    bval eps E (leq f1 f2) (Rle v1 v2)
238
|less_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R):
239 240 241
    eval_exp eps E f1 v1 ->
    eval_exp eps E f2 v2 ->
    bval eps E (less f1 f2) (Rlt v1 v2).
242 243 244
(**
 Simplify arithmetic later by making > >= only abbreviations
**)
245 246
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.