Expressions.v 7.28 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 Import Daisy.Infra.RealSimps Daisy.Infra.Abbrevs.
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 15 16 17 18 19 20 21

Definition binop_eq_bool (b1:binop) (b2:binop) :=
  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 eval_binop (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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

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

Definition unop_eq_bool (o1:unop) (o2:unop) :=
  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.
 **)
Definition eval_unop (o:unop) (v:R):=
  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 74 75 76 77 78 79 80 81 82 83 84 85 86 87

Fixpoint exp_eq_bool (e1:exp Q) (e2:exp Q) :=
  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
88 89 90 91 92 93
  |Unop o1 e11 =>
   match e2 with
   |Unop o2 e22 => andb (unop_eq_bool o1 o2) (exp_eq_bool e11 e22)
   |_ => false
   end
  |Binop o1 e11 e12 =>
94
   match e2 with
95
   |Binop o2 e21 e22 => andb (binop_eq_bool o1 o2) (andb (exp_eq_bool e11 e21) (exp_eq_bool e12 e22))
96 97 98
   |_ => false
   end
  end.
99 100 101 102 103
(**
  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
104

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

132 133 134
(**
If |delta| <= 0 then perturb v delta is exactly v
**)
Heiko Becker's avatar
Heiko Becker committed
135 136 137 138 139 140 141 142 143 144 145 146
Lemma perturb_0_val (v:R) (delta:R):
  (Rabs delta <= 0)%R ->
  perturb v delta = v.
Proof.
  intros abs_0; apply Rabs_0_impl_eq in abs_0; subst.
  unfold perturb.
  rewrite Rmult_plus_distr_l.
  rewrite Rmult_0_r.
  rewrite Rmult_1_r.
  rewrite Rplus_0_r; auto.
Qed.

147 148 149
(**
Evaluation with 0 as epsilon is deterministic
**)
150
Lemma eval_0_det (e:exp R) (env:env_ty):
151 152 153 154 155 156
  forall v1 v2,
  eval_exp R0 env e v1 ->
  eval_exp R0 env e v2 ->
  v1 = v2.
Proof.
  induction e; intros v1 v2 eval_v1 eval_v2;
157 158 159 160 161 162 163 164
    inversion eval_v1; inversion eval_v2; [ auto | | | | | | | ];
      repeat try rewrite perturb_0_val; subst; auto.
  - rewrite (IHe v0 v3); auto.
  - inversion H3.
  - inversion H4.
  - rewrite (IHe v0 v3); auto.
  - rewrite (IHe1 v0 v4); auto.
    rewrite (IHe2 v3 v5); auto.
165 166
Qed.

167 168 169 170 171 172 173
(**
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
variables in the environment.
This needs the property that variables are not perturbed as opposed to parameters
**)
174
Lemma existential_rewriting_binary (b:binop) (e1:exp R) (e2:exp R) (eps:R) (cenv:env_ty) (v:R):
175 176 177 178
  (eval_exp eps cenv (Binop b e1 e2) v <->
   exists v1 v2,
     eval_exp eps cenv e1 v1 /\
     eval_exp eps cenv e2 v2 /\
Heiko Becker's avatar
Heiko Becker committed
179
     eval_exp eps (updEnv 2 v2 (updEnv 1 v1 cenv)) (Binop b (Var R 1) (Var R 2)) v).
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
Proof.
  split.
  - intros eval_bin.
    inversion eval_bin; subst.
    exists v1, v2.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
    constructor; auto.
  - intros exists_val.
    destruct exists_val as [v1 [v2 [eval_e1 [eval_e2 eval_e_env]]]].
    inversion eval_e_env; subst.
    inversion H4; inversion H5; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
Qed.

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

Lemma existential_rewriting_unary (e:exp R) (eps:R) (cenv:env_ty) (v:R):
  (eval_exp eps cenv (Unop Inv e) v <->
   exists v1,
     eval_exp eps cenv e v1 /\
     eval_exp eps (updEnv 1 v1 cenv) (Unop Inv (Var R 1)) v).
Proof.
  split.
  - intros eval_un.
    inversion eval_un; subst.
    exists v1.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
  - intros exists_val.
    destruct exists_val as [v1 [eval_e1 eval_e_env]].
    inversion eval_e_env; subst.
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
Qed.

219 220 221 222 223 224 225 226
(**
  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
227
 **)
228
Inductive bval (eps:R) (env:env_ty) : (bexp R) -> Prop -> Prop :=
229 230 231 232 233 234 235 236
  leq_eval (e1:exp R) (e2:exp R) (v1:R) (v2:R):
    eval_exp eps env e1 v1 ->
    eval_exp eps env e2 v2 ->
    bval eps env (leq e1 e2) (Rle v1 v2)
|less_eval (e1:exp R) (e2:exp R) (v1:R) (v2:R):
    eval_exp eps env e1 v1 ->
    eval_exp eps env e2 v2 ->
    bval eps env (less e1 e2) (Rlt v1 v2).
237 238 239 240 241
(**
 Simplify arithmetic later by making > >= only abbreviations
**)
Definition gr := fun (V:Type) (e1: exp V) (e2: exp V) => less e2 e1.
Definition greq := fun (V:Type) (e1:exp V) (e2: exp V) => leq e2 e1.