Expressions.v 4.33 KB
Newer Older
1 2 3
(**
Formalization of the base expression language for the daisy framework
 **)
Heiko Becker's avatar
Heiko Becker committed
4 5
Require Import Coq.Reals.Reals Coq.micromega.Psatz Coq.QArith.QArith Interval.Interval_tactic.
Require Import Daisy.Infra.RealConstruction Daisy.Infra.RealSimps Daisy.Infra.Abbrevs.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Set Implicit Arguments.
(**
  Expressions will use binary operators.
  Define them first
**)
Inductive binop : Type := Plus | Sub | Mult | Div.
(**
  Next define an evaluation function for binary operators on reals.
  Errors are added on the expression evaluation level later.
 **)
Fixpoint eval_binop (o:binop) (v1:R) (v2:R) :=
  match o with
  | Plus => Rplus v1 v2
  | Sub => Rminus v1 v2
  | Mult => Rmult v1 v2
  | Div => Rdiv v1 v2
  end.
23
(**
24 25
  Define expressions parametric over some value type V.
  Will ease reasoning about different instantiations later.
26 27 28 29
  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.
**)
30 31
Inductive exp (V:Type): Type :=
  Var: nat -> exp V
32
| Param: nat -> exp V
33 34
| Const: V -> exp V
| Binop: binop -> exp V -> exp V -> exp V.
35

36 37 38 39 40
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
  Rmult r (Rplus 1 e).
41
(**
42
  Abbreviation for the environment types
43 44
 **)
Definition env_ty := nat -> R.
Heiko Becker's avatar
Heiko Becker committed
45 46
Definition analysisResult :Type := exp Q -> intv * error.

47
(**
48 49
  Define expression evaluation relation parametric by an "error" delta.
  This value will be used later to express float computations using a perturbation
50 51
  of the real valued computation by (1 + d)
**)
52
Inductive eval_exp (eps:R) (env:env_ty) : (exp R) -> R -> Prop :=
53
  Var_load x: eval_exp eps env (Var R x) (env x)
Heiko Becker's avatar
Heiko Becker committed
54 55 56
| Param_acc x delta:
    ((Rabs delta) <= eps)%R ->
    eval_exp eps env (Param R x) (perturb (env x) delta)
57 58 59 60 61 62 63 64 65
| Const_dist n delta:
    Rle (Rabs delta) eps ->
    eval_exp eps env (Const n) (perturb n delta)
|Binop_dist op e1 e2 v1 v2 delta:
   Rle (Rabs delta) eps ->
                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).

Heiko Becker's avatar
Heiko Becker committed
66 67 68 69 70 71 72 73 74 75 76 77
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.

78
Lemma eval_0_det (e:exp R) (env:env_ty):
79 80 81 82 83 84 85 86 87 88 89 90 91
  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;
    inversion eval_v1; inversion eval_v2; [ auto | | | ];
      repeat try rewrite perturb_0_val; auto.
  subst.
  rewrite (IHe1 v0 v4); auto.
  rewrite (IHe2 v3 v5); auto.
Qed.

92 93 94 95 96
Lemma existential_rewriting (b:binop) (e1:exp R) (e2:exp R) (eps:R) (cenv:env_ty) (v:R):
  (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
97
     eval_exp eps (updEnv 2 v2 (updEnv 1 v1 cenv)) (Binop b (Var R 1) (Var R 2)) v).
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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.

115 116 117 118 119 120 121 122
(**
  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
123
 **)
124
Inductive bval (eps:R) (env:env_ty) : (bexp R) -> Prop -> Prop :=
125 126 127 128 129 130 131 132
  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).
133 134 135 136 137
(**
 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.