Expressions.v 7.17 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.
6
Require Export Daisy.Infra.Abbrevs Daisy.Infra.RealSimps.
7
8
9
10
11
12
Set Implicit Arguments.
(**
  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
48
49
  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.
   Errors are added on the expression evaluation level later.
 **)
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
  Note that we differentiate between wether we use a variable from the environment or a parameter.
  Parameters do not have error bounds in the invariants, so they must be perturbed, but variables from the
  program will be perturbed upon binding, so we do not need to perturb them.
**)
63
64
Inductive exp (V:Type): Type :=
  Var: nat -> exp V
65
| Param: nat -> exp V
66
| Const: V -> exp V
67
| Unop: unop -> exp V -> exp V
68
| Binop: binop -> exp V -> 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  match e1 with
  |Var _ v1 =>
   match e2 with
   |Var _ v2 => v1 =? v2
   | _=> false
   end
  |Param _ v1 =>
   match e2 with
   |Param _ v2 => v1 =? v2
   | _=> false
   end
  |Const n1 =>
   match e2 with
   |Const n2 => Qeq_bool n1 n2
   | _=> false
   end
91
92
  |Unop o1 e11 =>
   match e2 with
93
   |Unop o2 e22 => andb (unopEqBool o1 o2) (expEqBool e11 e22)
94
95
96
   |_ => false
   end
  |Binop o1 e11 e12 =>
97
   match e2 with
98
   |Binop o2 e21 e22 => andb (binopEqBool o1 o2) (andb (expEqBool e11 e21) (expEqBool e12 e22))
99
100
101
   |_ => false
   end
  end.
102

103
104
105
106
107
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
  Rmult r (Rplus 1 e).
Heiko Becker's avatar
Heiko Becker committed
108

109
(**
110
111
112
113
114
115
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.
116
Unary negation is special! We do not have a new error here since IEE 754 gives us a sign bit
117
**)
118
119
Inductive eval_exp (eps:R) (E:env) : (exp R) -> R -> Prop :=
  Var_load x: eval_exp eps E (Var R x) (E x)
Heiko Becker's avatar
Heiko Becker committed
120
121
| Param_acc x delta:
    ((Rabs delta) <= eps)%R ->
122
    eval_exp eps E (Param R x) (perturb (E x) delta)
123
124
| Const_dist n delta:
    Rle (Rabs delta) eps ->
125
126
    eval_exp eps E (Const n) (perturb n delta)
| Unop_neg f1 v1: eval_exp eps E f1 v1 -> eval_exp eps E (Unop Neg f1) (evalUnop Neg v1)
127
| Unop_inv f1 v1 delta:
128
    Rle (Rabs delta) eps ->
129
130
    eval_exp eps E f1 v1 ->
    eval_exp eps E (Unop Inv f1) (perturb (evalUnop Inv v1) delta)
131
132
| Binop_dist op f1 f2 v1 v2 delta:
    Rle (Rabs delta) eps ->
133
134
135
                eval_exp eps E f1 v1 ->
                eval_exp eps E f2 v2 ->
                eval_exp eps E (Binop op f1 f2) (perturb (evalBinop op v1 v2) delta).
136

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

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

169
170
171
172
(**
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
173
variables in the Eironment.
174
This relies on the property that variables are not perturbed as opposed to parameters
175
**)
176
177
Lemma binary_unfolding (b:binop) (f1:exp R) (f2:exp R) (eps:R) (cE:env) (v:R):
  (eval_exp eps cE (Binop b f1 f2) v <->
178
   exists v1 v2,
179
180
181
     eval_exp eps cE f1 v1 /\
     eval_exp eps cE f2 v2 /\
     eval_exp eps (updEnv 2 v2 (updEnv 1 v1 cE)) (Binop b (Var R 1) (Var R 2)) v).
182
183
184
185
186
187
Proof.
  split.
  - intros eval_bin.
    inversion eval_bin; subst.
    exists v1, v2.
    repeat split; try auto.
188
    constructor; try auto;
189
190
    constructor; auto.
  - intros exists_val.
191
192
    destruct exists_val as [v1 [v2 [eval_f1 [eval_f2 eval_e_E]]]].
    inversion eval_e_E; subst.
193
194
195
196
197
    inversion H4; inversion H5; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
Qed.

198
199
200
(**
Analogous lemma for unary expressions.
**)
201
202
Lemma unary_unfolding (e:exp R) (eps:R) (cE:env) (v:R):
  (eval_exp eps cE (Unop Inv e) v <->
203
   exists v1,
204
205
     eval_exp eps cE e v1 /\
     eval_exp eps (updEnv 1 v1 cE) (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
220
221
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
Qed.

222
223
224
225
226
227
228
229
(**
  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
230
 **)
231
Inductive bval (eps:R) (E:env) : (bexp R) -> Prop -> Prop :=
232
  leq_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R):
233
234
235
    eval_exp eps E f1 v1 ->
    eval_exp eps E f2 v2 ->
    bval eps E (leq f1 f2) (Rle v1 v2)
236
|less_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R):
237
238
239
    eval_exp eps E f1 v1 ->
    eval_exp eps E f2 v2 ->
    bval eps E (less f1 f2) (Rlt v1 v2).
240
241
242
(**
 Simplify arithmetic later by making > >= only abbreviations
**)
243
244
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.