ExpressionSemantics.v 13.7 KB
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ``````From Coq Require Import Reals.Reals. From Flover.Infra Require Import RealRationalProps RationalSimps Ltacs. From Flover.Infra Require Export ExpressionAbbrevs. (** Finally, define an error function that computes an errorneous value for a given type. For a fixed-point datatype, truncation is used and any floating-point type is perturbed. As we need not compute on this function we define it in Prop. **) Definition perturb (rVal:R) (m:mType) (delta:R) :R := match m with (* The Real-type has no error *) |REAL => rVal (* Fixed-point numbers have an absolute error *) |F w f => rVal + delta (* Floating-point numbers have a relative error *) | _ => rVal * (1 + delta) end. Hint Unfold perturb. (** Define expression evaluation relation parametric by an "error" epsilon. The result value exprresses float computations according to the IEEE standard, using a perturbation of the real valued computation by (1 + delta), where |delta| <= machine epsilon. **) Open Scope R_scope. `````` Nikita Zyuzin committed Sep 17, 2018 35 36 37 38 39 40 ``````Definition nonDestrUpd (T: Type) M k (v: T) := match (M k) with | Some v1 => M | None => (fun x => if R_orderedExps.eq_dec x k then Some v else M x) end. `````` 41 42 ``````Inductive eval_expr (E:env) (Gamma: expr R -> option mType) `````` Nikita Zyuzin committed Sep 17, 2018 43 44 `````` :(expr R -> option R) -> (expr R) -> R -> mType -> Prop := | Var_load DeltaMap m x v: `````` 45 46 `````` Gamma (Var R x) = Some m -> E x = Some v -> `````` Nikita Zyuzin committed Sep 17, 2018 47 48 49 `````` eval_expr E Gamma DeltaMap (Var R x) v m | Const_dist DeltaMap m n delta: (forall delta', DeltaMap (Const m n) = Some delta' -> delta = delta') -> `````` 50 `````` Rabs delta <= mTypeToR m -> `````` Nikita Zyuzin committed Sep 17, 2018 51 52 53 `````` eval_expr E Gamma (nonDestrUpd DeltaMap (Const m n) delta) (Const m n) (perturb n m delta) m (* eval_expr E Gamma DeltaMap (Const m n) (perturb n m delta) m *) | Unop_neg DeltaMap m mN f1 v1: `````` 54 55 `````` Gamma (Unop Neg f1) = Some mN -> isCompat m mN = true -> `````` Nikita Zyuzin committed Sep 17, 2018 56 57 58 59 `````` eval_expr E Gamma DeltaMap f1 v1 m -> eval_expr E Gamma DeltaMap (Unop Neg f1) (evalUnop Neg v1) mN | Unop_inv DeltaMap m mN f1 v1 delta: (forall delta', DeltaMap (Unop Inv f1) = Some delta' -> delta = delta') -> `````` 60 61 62 `````` Gamma (Unop Inv f1) = Some mN -> isCompat m mN = true -> Rabs delta <= mTypeToR mN -> `````` Nikita Zyuzin committed Sep 17, 2018 63 `````` eval_expr E Gamma DeltaMap f1 v1 m -> `````` 64 `````` (~ v1 = 0)%R -> `````` Nikita Zyuzin committed Sep 17, 2018 65 66 67 68 `````` eval_expr E Gamma (nonDestrUpd DeltaMap (Unop Inv f1) delta) (Unop Inv f1) (perturb (evalUnop Inv v1) mN delta) mN | Downcast_dist DeltaMap m m1 f1 v1 delta: (forall delta', DeltaMap (Downcast m f1) = Some delta' -> delta = delta') -> `````` 69 70 71 `````` Gamma (Downcast m f1) = Some m -> isMorePrecise m1 m = true -> Rabs delta <= mTypeToR m -> `````` Nikita Zyuzin committed Sep 17, 2018 72 73 74 75 76 `````` eval_expr E Gamma DeltaMap f1 v1 m1 -> eval_expr E Gamma (nonDestrUpd DeltaMap (Downcast m f1) delta) (Downcast m f1) (perturb v1 m delta) m | Binop_dist DeltaMap m1 m2 op f1 f2 v1 v2 delta m: (forall delta', DeltaMap (Binop op f1 f2) = Some delta' -> delta = delta') -> `````` 77 78 79 `````` Gamma (Binop op f1 f2) = Some m -> isJoin m1 m2 m = true -> Rabs delta <= mTypeToR m -> `````` Nikita Zyuzin committed Sep 17, 2018 80 81 `````` eval_expr E Gamma DeltaMap f1 v1 m1 -> eval_expr E Gamma DeltaMap f2 v2 m2 -> `````` 82 `````` ((op = Div) -> (~ v2 = 0)%R) -> `````` Nikita Zyuzin committed Sep 17, 2018 83 84 85 86 `````` eval_expr E Gamma (nonDestrUpd DeltaMap (Binop op f1 f2) delta) (Binop op f1 f2) (perturb (evalBinop op v1 v2) m delta) m | Fma_dist DeltaMap m1 m2 m3 m f1 f2 f3 v1 v2 v3 delta: (forall delta', DeltaMap (Fma f1 f2 f3) = Some delta' -> delta = delta') -> `````` 87 `````` Gamma (Fma f1 f2 f3) = Some m -> `````` Heiko Becker committed Jul 27, 2018 88 `````` isJoin3 m1 m2 m3 m = true -> `````` 89 `````` Rabs delta <= mTypeToR m -> `````` Nikita Zyuzin committed Sep 17, 2018 90 91 92 93 94 `````` eval_expr E Gamma DeltaMap f1 v1 m1 -> eval_expr E Gamma DeltaMap f2 v2 m2 -> eval_expr E Gamma DeltaMap f3 v3 m3 -> eval_expr E Gamma (nonDestrUpd DeltaMap (Fma f1 f2 f3) delta) (Fma f1 f2 f3) (perturb (evalFma v1 v2 v3) m delta) m. `````` 95 96 97 98 99 100 101 102 `````` Close Scope R_scope. Hint Constructors eval_expr. (** *) (* Show some simpler (more general) rule lemmata *) (* **) `````` Nikita Zyuzin committed Sep 17, 2018 103 104 ``````Lemma Const_dist' DeltaMap DeltaMap' m n delta v m' E Gamma: (forall delta', DeltaMap' (Const m n) = Some delta' -> delta = delta') -> `````` 105 `````` Rle (Rabs delta) (mTypeToR m') -> `````` Nikita Zyuzin committed Sep 17, 2018 106 `````` DeltaMap = nonDestrUpd DeltaMap' (Const m n) delta -> `````` 107 108 `````` v = perturb n m delta -> m' = m -> `````` Nikita Zyuzin committed Sep 17, 2018 109 `````` eval_expr E Gamma DeltaMap (Const m n) v m'. `````` 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 ``````Proof. intros; subst; auto. Qed. Hint Resolve Const_dist'. Lemma Unop_neg' m mN f1 v1 v m' E Gamma: eval_expr E Gamma f1 v1 m -> v = evalUnop Neg v1 -> Gamma (Unop Neg f1) = Some mN -> isCompat m mN = true -> m' = mN -> eval_expr E Gamma (Unop Neg f1) v m'. Proof. intros; subst; eauto. Qed. Hint Resolve Unop_neg'. Lemma Unop_inv' m mN f1 v1 delta v m' E Gamma: Rle (Rabs delta) (mTypeToR m') -> eval_expr E Gamma f1 v1 m -> (~ v1 = 0)%R -> v = perturb (evalUnop Inv v1) mN delta -> Gamma (Unop Inv f1) = Some mN -> isCompat m mN = true -> m' = mN -> eval_expr E Gamma (Unop Inv f1) v m'. Proof. intros; subst; eauto. Qed. Hint Resolve Unop_inv'. Lemma Downcast_dist' m m1 f1 v1 delta v m' E Gamma: isMorePrecise m1 m = true -> Rle (Rabs delta) (mTypeToR m') -> eval_expr E Gamma f1 v1 m1 -> v = (perturb v1 m delta) -> Gamma (Downcast m f1) = Some m -> m' = m -> eval_expr E Gamma (Downcast m f1) v m'. Proof. intros; subst; eauto. Qed. Hint Resolve Downcast_dist'. Lemma Binop_dist' m1 m2 op f1 f2 v1 v2 delta v m m' E Gamma: Rle (Rabs delta) (mTypeToR m') -> eval_expr E Gamma f1 v1 m1 -> eval_expr E Gamma f2 v2 m2 -> ((op = Div) -> (~ v2 = 0)%R) -> v = perturb (evalBinop op v1 v2) m' delta -> Gamma (Binop op f1 f2) = Some m -> isJoin m1 m2 m = true -> m = m' -> eval_expr E Gamma (Binop op f1 f2) v m'. Proof. intros; subst; eauto. Qed. Hint Resolve Binop_dist'. Lemma Fma_dist' m1 m2 m3 f1 f2 f3 v1 v2 v3 delta v m' E Gamma m: Rle (Rabs delta) (mTypeToR m') -> eval_expr E Gamma f1 v1 m1 -> eval_expr E Gamma f2 v2 m2 -> eval_expr E Gamma f3 v3 m3 -> v = perturb (evalFma v1 v2 v3) m' delta -> Gamma (Fma f1 f2 f3) = Some m -> `````` Heiko Becker committed Jul 27, 2018 181 `````` isJoin3 m1 m2 m3 m = true -> `````` 182 183 184 185 186 187 188 189 `````` m = m' -> eval_expr E Gamma (Fma f1 f2 f3) v m'. Proof. intros; subst; eauto. Qed. Hint Resolve Fma_dist'. `````` Heiko Becker committed Aug 02, 2018 190 191 192 ``````Lemma Gamma_det e E1 E2 Gamma v1 v2 m1 m2: eval_expr E1 Gamma e v1 m1 -> eval_expr E2 Gamma e v2 m2 -> `````` Heiko Becker committed Jul 27, 2018 193 `````` m1 = m2. `````` Heiko Becker committed Jul 27, 2018 194 ``````Proof. `````` Heiko Becker committed Jul 27, 2018 195 196 197 198 199 200 201 202 `````` induction e; intros * eval_e1 eval_e2; inversion eval_e1; subst; inversion eval_e2; subst; try auto; match goal with | [H1: Gamma ?e = Some ?m1, H2: Gamma ?e = Some ?m2 |- _ ] => rewrite H1 in H2; inversion H2; subst end; auto. `````` Heiko Becker committed Jul 27, 2018 203 204 ``````Qed. `````` 205 ``````Lemma toRTMap_eval_REAL f: `````` Heiko Becker committed Jul 27, 2018 206 `````` forall v E Gamma m, eval_expr E (toRTMap Gamma) (toREval f) v m -> m = REAL. `````` 207 ``````Proof. `````` Heiko Becker committed Jul 27, 2018 208 `````` induction f; intros * eval_f; inversion eval_f; subst. `````` 209 210 `````` repeat match goal with `````` Heiko Becker committed Jul 27, 2018 211 `````` | H: context[toRTMap _ _] |- _ => unfold toRTMap in H `````` 212 213 214 215 `````` | H: context[match ?Gamma ?v with | _ => _ end ] |- _ => destruct (Gamma v) eqn:? | H: Some ?m1 = Some ?m2 |- _ => inversion H; try auto | H: None = Some ?m |- _ => inversion H end; try auto. `````` Heiko Becker committed Jul 27, 2018 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 `````` - auto. - rewrite (IHf _ _ _ _ H5) in H2. unfold isCompat in H2. destruct m; type_conv; subst; try congruence; auto. - rewrite (IHf _ _ _ _ H4) in H2. unfold isCompat in H2. destruct m; type_conv; subst; try congruence; auto. - rewrite (IHf1 _ _ _ _ H5) in H3. rewrite (IHf2 _ _ _ _ H8) in H3. unfold isJoin in H3; simpl in H3. destruct m; try congruence; auto. - rewrite (IHf1 _ _ _ _ H5) in H3. rewrite (IHf2 _ _ _ _ H8) in H3. rewrite (IHf3 _ _ _ _ H9) in H3. unfold isJoin3 in H3; simpl in H3. destruct m; try congruence; auto. - auto. `````` 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 ``````Qed. (** If |delta| <= 0 then perturb v delta is exactly v. **) Lemma delta_0_deterministic (v:R) m (delta:R): (Rabs delta <= 0)%R -> perturb v m delta = v. Proof. intros abs_0; apply Rabs_0_impl_eq in abs_0; subst. unfold perturb. destruct m; lra. Qed. (** Evaluation with 0 as machine epsilon is deterministic **) Lemma meps_0_deterministic (f:expr R) (E:env) Gamma: forall v1 v2, `````` Heiko Becker committed Jul 27, 2018 251 252 `````` eval_expr E (toRTMap Gamma) (toREval f) v1 REAL -> eval_expr E (toRTMap Gamma) (toREval f) v2 REAL -> `````` 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 `````` v1 = v2. Proof. induction f; intros v1 v2 ev1 ev2. - inversion ev1; inversion ev2; subst. rewrite H1 in H6. inversion H6; auto. - inversion ev1; inversion ev2; subst. simpl in *; subst; auto. - inversion ev1; inversion ev2; subst; try congruence. + rewrite (IHf v0 v3); [ auto | |]; destruct m, m0; cbn in *; congruence. + cbn in *. Flover_compute; rewrite (IHf v0 v3); [auto | | ]; destruct m, m0; cbn in *; congruence. - inversion ev1; inversion ev2; subst. `````` 268 269 270 271 `````` assert (m0 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m3 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m1 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m2 = REAL) by (eapply toRTMap_eval_REAL; eauto). `````` 272 273 274 275 `````` subst. rewrite (IHf1 v0 v4); try auto. rewrite (IHf2 v3 v5); try auto. - inversion ev1; inversion ev2; subst. `````` 276 277 278 279 280 281 `````` assert (m0 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m1 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m2 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m3 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m4 = REAL) by (eapply toRTMap_eval_REAL; eauto). assert (m5 = REAL) by (eapply toRTMap_eval_REAL; eauto). `````` 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 `````` subst. rewrite (IHf1 v0 v5); try auto. rewrite (IHf2 v3 v6); try auto. rewrite (IHf3 v4 v7); try auto. - inversion ev1; inversion ev2; subst. apply REAL_least_precision in H2; apply REAL_least_precision in H9; subst. rewrite (IHf v0 v3); try auto. Qed. (** Helping lemmas. Needed in soundness proof. For each evaluation of using an arbitrary epsilon, we can replace it by evaluating the subexprressions and then binding the result values to different variables in the Environment. **) Lemma binary_unfolding b f1 f2 E v1 v2 m1 m2 m Gamma delta: (b = Div -> ~(v2 = 0 )%R) -> (Rabs delta <= mTypeToR m)%R -> eval_expr E Gamma f1 v1 m1 -> eval_expr E Gamma f2 v2 m2 -> eval_expr E Gamma (Binop b f1 f2) (perturb (evalBinop b v1 v2) m delta) m -> eval_expr (updEnv 2 v2 (updEnv 1 v1 emptyEnv)) (updDefVars (Binop b (Var R 1) (Var R 2)) m (updDefVars (Var R 2) m2 (updDefVars (Var R 1) m1 Gamma))) (Binop b (Var R 1) (Var R 2)) (perturb (evalBinop b v1 v2) m delta) m. Proof. intros no_div_zero err_v eval_f1 eval_f2 eval_float. inversion eval_float; subst. `````` Heiko Becker committed Jul 27, 2018 311 `````` rewrite H2 in *. `````` Heiko Becker committed Jul 27, 2018 312 313 314 315 316 317 318 `````` repeat (match goal with | [H1: eval_expr ?E ?Gamma ?f ?v1 ?m1, H2: eval_expr ?E ?Gamma ?f ?v2 ?m2 |- _] => assert (m1 = m2) by (eapply Gamma_det; eauto); revert H1 H2 end); intros; subst. `````` Heiko Becker committed Jul 27, 2018 319 `````` eapply Binop_dist' with (v1:=v1) (v2:=v2) (delta:=delta); try eauto. `````` Heiko Becker committed Jul 27, 2018 320 321 322 323 `````` - eapply Var_load; eauto. - eapply Var_load; eauto. - unfold updDefVars. unfold R_orderedExps.compare; rewrite R_orderedExps.exprCompare_refl; auto. `````` Heiko Becker committed Jul 27, 2018 324 ``````Qed. `````` 325 326 327 328 329 330 331 332 `````` Lemma fma_unfolding f1 f2 f3 E v1 v2 v3 m1 m2 m3 m Gamma delta: (Rabs delta <= mTypeToR m)%R -> eval_expr E Gamma f1 v1 m1 -> eval_expr E Gamma f2 v2 m2 -> eval_expr E Gamma f3 v3 m3 -> eval_expr E Gamma (Fma f1 f2 f3) (perturb (evalFma v1 v2 v3) m delta) m -> eval_expr (updEnv 3 v3 (updEnv 2 v2 (updEnv 1 v1 emptyEnv))) `````` Heiko Becker committed Jul 27, 2018 333 `````` (updDefVars (Fma (Var R 1) (Var R 2) (Var R 3) ) m `````` 334 `````` (updDefVars (Var R 3) m3 (updDefVars (Var R 2) m2 `````` Heiko Becker committed Jul 27, 2018 335 `````` (updDefVars (Var R 1) m1 Gamma)))) `````` 336 337 `````` (Fma (Var R 1) (Var R 2) (Var R 3)) (perturb (evalFma v1 v2 v3) m delta) m. Proof. `````` Heiko Becker committed Jul 27, 2018 338 339 340 341 342 343 `````` intros err_v eval_f1 eval_f2 eval_f3 eval_float. inversion eval_float; subst. repeat (match goal with | [H1: eval_expr ?E ?Gamma ?f ?v1 ?m1, H2: eval_expr ?E ?Gamma ?f ?v2 ?m2 |- _] => assert (m1 = m2) `````` Heiko Becker committed Jul 27, 2018 344 `````` by (eapply Gamma_det; eauto); `````` Heiko Becker committed Jul 27, 2018 345 346 347 348 349 350 351 352 353 354 `````` revert H1 H2 end). intros; subst. rewrite H2. eapply Fma_dist' with (v1:=v1) (v2:=v2) (v3:=v3) (delta:=delta); try eauto. - eapply Var_load; eauto. - eapply Var_load; eauto. - eapply Var_load; eauto. - cbn; auto. Qed. `````` 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 `````` Lemma eval_eq_env e: forall E1 E2 Gamma v m, (forall x, E1 x = E2 x) -> eval_expr E1 Gamma e v m -> eval_expr E2 Gamma e v m. Proof. induction e; intros; (match_pat (eval_expr _ _ _ _ _) (fun H => inversion H; subst; simpl in H)); try eauto. eapply Var_load; auto. rewrite <- (H n); auto. Qed. Lemma eval_expr_ignore_bind e: forall x v m Gamma E, eval_expr E Gamma e v m -> ~ NatSet.In x (usedVars e) -> `````` Heiko Becker committed Aug 02, 2018 373 374 `````` forall v_new, eval_expr (updEnv x v_new E) Gamma e v m. `````` 375 376 377 378 379 380 381 382 ``````Proof. induction e; intros * eval_e no_usedVar *; cbn in *; inversion eval_e; subst; try eauto. - assert (n <> x). { hnf. intros. subst. apply no_usedVar; set_tac. } rewrite <- Nat.eqb_neq in H. eapply Var_load. + unfold updDefVars. `````` Heiko Becker committed Jul 27, 2018 383 384 385 `````` cbn. apply beq_nat_false in H. destruct (n ?= x)%nat eqn:?; try auto. `````` 386 387 388 389 390 391 392 `````` + unfold updEnv. rewrite H; auto. - eapply Binop_dist'; eauto; [ eapply IHe1 | eapply IHe2]; eauto; hnf; intros; eapply no_usedVar; set_tac. `````` Heiko Becker committed Jul 27, 2018 393 `````` - eapply Fma_dist'; eauto; `````` 394 395 396 `````` [eapply IHe1 | eapply IHe2 | eapply IHe3]; eauto; hnf; intros; eapply no_usedVar; `````` Heiko Becker committed Jul 27, 2018 397 398 `````` set_tac. Qed. `````` 399 `````` `````` Heiko Becker committed Jul 26, 2018 400 ``````Lemma swap_Gamma_eval_expr e E vR m Gamma1 Gamma2: `````` Heiko Becker committed Aug 16, 2018 401 `````` (forall e, Gamma1 e = Gamma2 e) -> `````` Heiko Becker committed Jul 26, 2018 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 `````` eval_expr E Gamma1 e vR m -> eval_expr E Gamma2 e vR m. Proof. revert E vR Gamma1 Gamma2 m; induction e; intros * Gamma_eq eval_e; inversion eval_e; subst; simpl in *; [ eapply Var_load | eapply Const_dist' | eapply Unop_neg' | eapply Unop_inv' | eapply Binop_dist' | eapply Fma_dist' | eapply Downcast_dist' ]; try eauto; rewrite <- Gamma_eq; auto. Qed. `````` 418 419 420 421 422 423 ``````Lemma Rmap_updVars_comm Gamma n m: forall x, updDefVars n REAL (toRMap Gamma) x = toRMap (updDefVars n m Gamma) x. Proof. unfold updDefVars, toRMap; simpl. intros x; destruct (R_orderedExps.compare x n); auto. `````` Nikita Zyuzin committed Sep 17, 2018 424 ``Qed.``