Expressions.v 35.7 KB
Newer Older
1
(**
2
  Formalization of the base expression language for the daisy framework
3
 **)
Heiko Becker's avatar
Heiko Becker committed
4 5 6
From Coq
Require Import Reals.Reals micromega.Psatz QArith.QArith QArith.Qreals
Structures.Orders.
7 8 9 10
Require Import Daisy.Infra.RealRationalProps Daisy.Infra.RationalSimps
               Daisy.Infra.Ltacs.
Require Export Daisy.Infra.Abbrevs Daisy.Infra.RealSimps Daisy.Infra.NatSet
        Daisy.IntervalArithQ Daisy.IntervalArith Daisy.Infra.MachineType.
11

12 13 14 15 16
(**
  Expressions will use binary operators.
  Define them first
**)
Inductive binop : Type := Plus | Sub | Mult | Div.
17

18
Definition binopEq (b1:binop) (b2:binop) :=
='s avatar
= committed
19 20 21 22 23 24
  match b1, b2 with
  | Plus, Plus => true
  | Sub,  Sub  => true
  | Mult, Mult => true
  | Div,  Div  => true
  | _,_ => false
25 26
  end.

27 28 29 30
(**
  Next define an evaluation function for binary operators on reals.
  Errors are added on the expression evaluation level later.
 **)
31
Definition evalBinop (o:binop) (v1:R) (v2:R) :=
32 33 34 35 36 37
  match o with
  | Plus => Rplus v1 v2
  | Sub => Rminus v1 v2
  | Mult => Rmult v1 v2
  | Div => Rdiv v1 v2
  end.
38

39 40
Lemma binopEq_refl b:
  binopEq b b = true.
41 42 43 44
Proof.
  case b; auto.
Qed.

45 46
Lemma binopEq_compat_eq b1 b2:
  binopEq b1 b2 = true <-> b1 = b2.
47
Proof.
48 49 50 51 52 53 54 55 56 57
  split; case b1; case b2; intros; simpl in *; congruence.
Qed.

Lemma binopEq_compat_eq_false b1 b2:
  binopEq b1 b2 = false <-> ~ (b1 = b2).
Proof.
  split; intros neq.
  - hnf; intros; subst. rewrite binopEq_refl in neq.
    congruence.
  - destruct b1; destruct b2; cbv; congruence.
58 59
Qed.

60 61 62 63 64 65
(**
   Expressions will use unary operators.
   Define them first
 **)
Inductive unop: Type := Neg | Inv.

66
Definition unopEq (o1:unop) (o2:unop) :=
='s avatar
= committed
67 68 69 70
  match o1, o2 with
  | Neg, Neg => true
  | Inv, Inv => true
  | _ , _ => false
71 72
  end.

73 74
Lemma unopEq_refl b:
  unopEq b b = true.
75 76 77 78
Proof.
  case b; auto.
Qed.

79 80 81 82 83 84
Lemma unopEq_sym u1 u2:
  unopEq u1 u2 = unopEq u2 u1.
Proof.
  destruct u1,u2; compute; auto.
Qed.

85 86
Lemma unopEq_compat_eq b1 b2:
  unopEq b1 b2 = true <-> b1 = b2.
87
Proof.
88
  split; case b1; case b2; intros; simpl in *; congruence.
89 90
Qed.

91 92
(**
   Define evaluation for unary operators on reals.
93
   Errors are added in the expression evaluation level later.
94
 **)
95
Definition evalUnop (o:unop) (v:R):=
96 97 98 99 100
  match o with
  |Neg => (- v)%R
  |Inv => (/ v)%R
  end .

101
Definition evalFma (v1:R) (v2:R) (v3:R):=
Nikita Zyuzin's avatar
Nikita Zyuzin committed
102
  evalBinop Plus v1 (evalBinop Mult v2 v3).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
103

104
(**
105 106
  Define expressions parametric over some value type V.
  Will ease reasoning about different instantiations later.
107
**)
108 109
Inductive exp (V:Type): Type :=
  Var: nat -> exp V
110
| Const: mType -> V -> exp V
111
| Unop: unop -> exp V -> exp V
112
| Binop: binop -> exp V -> exp V -> exp V
113
| Fma: exp V -> exp V -> exp V -> exp V
114
| Downcast: mType -> exp V -> exp V.
115

116 117 118 119
(**
  Boolean equality function on expressions.
  Used in certificates to define the analysis result as function
**)
120
Fixpoint expEq (e1:exp Q) (e2:exp Q) :=
='s avatar
= committed
121
  match e1, e2 with
122
  | Var _ v1, Var _ v2 => (v1 =? v2)
123 124 125 126 127 128
  | Const m1 n1, Const m2 n2 =>
    (mTypeEq m1 m2) && (Qeq_bool n1 n2)
  | Unop o1 e11, Unop o2 e22 =>
    (unopEq o1 o2) && (expEq e11 e22)
  | Binop o1 e11 e12, Binop o2 e21 e22 =>
    (binopEq o1 o2) && (expEq e11 e21) && (expEq e12 e22)
129 130
  | Fma e11 e12 e13, Fma e21 e22 e23 =>
    (expEq e11 e21) && (expEq e12 e22) && (expEq e13 e23)
131 132
  | Downcast m1 f1, Downcast m2 f2 =>
    (mTypeEq m1 m2) && (expEq f1 f2)
='s avatar
= committed
133
  | _, _ => false
134 135
  end.

136 137
Lemma expEq_refl e:
  expEq e e = true.
138
Proof.
139
  induction e; try (apply andb_true_iff; split); simpl in *; auto .
140
  - symmetry; apply beq_nat_refl.
141
  - apply mTypeEq_refl.
142 143 144
  - apply Qeq_bool_iff; lra.
  - case u; auto.
  - case b; auto.
145
  - firstorder.
146
  - apply mTypeEq_refl.
147
Qed.
148

149 150
Lemma expEq_sym e e':
  expEq e e' = expEq e' e.
151 152 153
Proof.
  revert e'.
  induction e; intros e'; destruct e'; simpl; try auto.
154
  - apply Nat.eqb_sym.
155
  - f_equal.
156
    + apply mTypeEq_sym; auto.
157 158 159 160
    + apply Qeq_bool_sym.
  - f_equal.
    + destruct u; auto.
    + apply IHe.
='s avatar
= committed
161
  - f_equal.
162
    + f_equal.
163
      * destruct b; auto.
164
      * apply IHe1.
165
    + apply IHe2.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
166
  - f_equal.
167 168
    + f_equal; auto.
    + auto.
169
  - f_equal.
170
    + apply mTypeEq_sym; auto.
171 172 173
    + apply IHe.
Qed.

174 175 176 177
Lemma expEq_trans e f g:
  expEq e f = true ->
  expEq f g = true ->
  expEq e g = true.
='s avatar
= committed
178
Proof.
179
  revert e f g; induction e;
180
    destruct f; intros g eq1 eq2;
Heiko Becker's avatar
Heiko Becker committed
181
      destruct g; cbn in *;
182
        try rewrite Nat.eqb_eq in *;
Heiko Becker's avatar
Heiko Becker committed
183 184
        Daisy_compute; try congruence; type_conv; subst; try auto.
  - rewrite mTypeEq_refl; simpl.
185
    rewrite Qeq_bool_iff in *; lra.
Heiko Becker's avatar
Heiko Becker committed
186
  - rewrite unopEq_compat_eq in *; subst.
187
    rewrite unopEq_refl; simpl.
='s avatar
= committed
188
    eapply IHe; eauto.
Heiko Becker's avatar
Heiko Becker committed
189
  - rewrite binopEq_compat_eq in *; subst.
190
    rewrite binopEq_refl; simpl.
191 192
    apply andb_true_iff.
    split; [eapply IHe1; eauto | eapply IHe2; eauto].
193
  - rewrite andb_true_iff.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
194
    rewrite andb_true_iff.
195
    split; [split; [eapply IHe1; eauto | eapply IHe2; eauto] | eapply IHe3; eauto].
Heiko Becker's avatar
Heiko Becker committed
196
  - rewrite mTypeEq_refl; simpl.
='s avatar
= committed
197 198 199
    eapply IHe; eauto.
Qed.

Heiko Becker's avatar
Heiko Becker committed
200 201 202 203
Module Type OrderType := Coq.Structures.Orders.OrderedType.

Module ExpOrderedType (V_ordered:OrderType) <: OrderType.
  Module V_orderedFacts := OrdersFacts.OrderedTypeFacts (V_ordered).
204 205

  Definition V := V_ordered.t.
206
  Definition t := exp V.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

  Fixpoint expCompare (e1:exp V) (e2:exp V) :=
    match e1, e2 with
    |Var _ n1, Var _ n2 => Nat.compare n1 n2
    |Var _ n1, _ => Lt
    | Const m1 v1, Const m2 v2 =>
      if mTypeEq m1 m2
      then V_ordered.compare v1 v2
      else (if morePrecise m1 m2 then Lt else Gt)
    | Const _ _, Var _ _ => Gt
    | Const _ _, _ => Lt
    | Unop u1 e1, Unop u2 e2 =>
      if unopEq u1 u2
      then expCompare e1 e2
      else (if unopEq u1 Neg then Lt else Gt)
    | Unop _ _, Binop _ _ _ => Lt
    | Unop _ _, Downcast _ _ => Lt
    | Unop _ _, _ => Gt
    | Downcast m1 e1, Downcast m2 e2 =>
      if mTypeEq m1 m2
      then expCompare e1 e2
      else (if morePrecise m1 m2 then Lt else Gt)
    | Downcast _ _, Binop _ _ _ => Lt
    | Downcast _ _, _ => Gt
    | Binop b1 e11 e12, Binop b2 e21 e22 =>
      let res := match b1, b2 with
                 | Plus, Plus => Eq
                 | Plus, _ => Lt
                 | Sub, Sub => Eq
                 | Sub, Plus => Gt
                 | Sub, _ => Lt
                 | Mult, Mult => Eq
                 | Mult, Div => Lt
                 | Mult, _ => Gt
                 | Div, Div => Eq
                 | Div, _ => Gt
                 end
      in
      match res with
      | Eq =>
        match expCompare e11 e21 with
        | Eq => expCompare e12 e22
        | Lt => Lt
        | Gt => Gt
        end
      | _ => res
      end
    |_ , _ => Gt
    end.

  Lemma expCompare_refl e: expCompare e e = Eq.
  Proof.
    induction e; simpl.
    - apply Nat.compare_refl.
    - rewrite mTypeEq_refl. apply V_orderedFacts.compare_refl.
    - rewrite unopEq_refl; auto.
    - rewrite IHe1, IHe2. destruct b; auto.
    - rewrite mTypeEq_refl; auto.
  Qed.

  (* Lemma expCompare_eq_compat_eq e1: *)
  (*   forall e2, *)
  (*     expCompare e1 e2 = Eq <-> *)
  (*     eq e1 e2. *)
  (* Proof. *)
  (*   induction e1; destruct e2; split; intros * cmp_res; simpl in *; *)
  (*     subst; try congruence. *)
  (*   - rewrite Nat.compare_eq_iff in cmp_res; subst; auto. *)
  (*   - inversion cmp_res; subst; simpl. apply Nat.compare_refl. *)
  (*   - destruct (mTypeEq m m0 *)
  (*     apply V_orderedFacts.compare_eq in cmp_res. *)
  (*     f_equal. *)
  (*     hnf in cmp_res. *)

  Lemma expCompare_eq_trans e1 :
    forall e2 e3,
      expCompare e1 e2 = Eq ->
      expCompare e2 e3 = Eq ->
      expCompare e1 e3 = Eq.
  Proof.
    induction e1; intros * eq12 eq23;
      destruct e2; destruct e3; simpl in *; try congruence.
    - rewrite Nat.compare_eq_iff in *; subst; auto.
    - destruct (mTypeEq m m0) eqn:?;
               [ destruct (mTypeEq m0 m1) eqn:? |
                 destruct (morePrecise m m0) eqn:?; congruence];
        [ | destruct (morePrecise m0 m1) eqn:?; congruence].
      type_conv. rewrite mTypeEq_refl.
      rewrite V_orderedFacts.compare_eq_iff in *;
        eapply V_orderedFacts.eq_trans; eauto.
    - destruct (unopEq u u0) eqn:?;
               destruct (unopEq u0 u1) eqn:?;
               try rewrite unopEq_compat_eq in *; subst;
        try rewrite unopEq_refl;
        try congruence.
      + eapply IHe1; eauto.
      + destruct (unopEq u0 Neg); congruence.
      + destruct (unopEq u Neg); congruence.
      + destruct (unopEq u Neg); congruence.
    - destruct b; destruct b0; try congruence;
        destruct b1; try congruence;
          destruct (expCompare e1_1 e2_1) eqn:?;
                   destruct (expCompare e2_1 e3_1) eqn:?;
                   try congruence; try erewrite IHe1_1; eauto.
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?;
               type_conv;
        try rewrite mTypeEq_refl.
      + eapply IHe1; eauto.
      + destruct (morePrecise m0 m1); congruence.
      + destruct (morePrecise m m1); congruence.
      + destruct (morePrecise m m0); congruence.
  Qed.

  Lemma expCompare_antisym e1:
    forall e2,
      expCompare e1 e2 = CompOpp (expCompare e2 e1).
  Proof.
    induction e1; destruct e2; simpl; try auto.
    - apply Nat.compare_antisym.
    - rewrite mTypeEq_sym.
      destruct (mTypeEq m0 m) eqn:?;
               type_conv; try congruence; try rewrite mTypeEq_refl.
      + apply V_orderedFacts.compare_antisym.
      + destruct (morePrecise m m0) eqn:?;
                 destruct (morePrecise m0 m) eqn:?;
                 try (split; auto; fail).
        * pose proof (morePrecise_antisym _ _ Heqb0 Heqb1); type_conv;
            congruence.
        * destruct m, m0; unfold morePrecise in *; cbv; congruence.
    - rewrite unopEq_sym.
      destruct (unopEq u0 u) eqn:?;
               try rewrite unopEq_compat_eq in *; subst;
        try rewrite unopEq_refl, IHe1; try (apply IHe1).
      destruct (unopEq u Neg) eqn:?; try rewrite unopEq_compat_eq in *;
        destruct (unopEq u0 Neg) eqn:?; try rewrite unopEq_compat_eq in *;
        subst; simpl in *; try congruence.
      destruct u, u0; simpl in *; congruence.
    - destruct b, b0; simpl; try (split; auto; fail);
      destruct (expCompare e1_1 e2_1) eqn:first_comp;
      rewrite IHe1_1 in *; simpl in *;
        rewrite CompOpp_iff in first_comp;
        rewrite first_comp; simpl; try auto.
    - rewrite mTypeEq_sym.
      destruct (mTypeEq m0 m) eqn:?;
               type_conv; try auto.
      + destruct (morePrecise m m0) eqn:?;
                 destruct (morePrecise m0 m) eqn:?;
                 try (split; auto; fail).
        * pose proof (morePrecise_antisym _ _ Heqb0 Heqb1); type_conv;
            congruence.
        * destruct m, m0; unfold morePrecise in *; cbv; congruence.
  Qed.

  Lemma expCompare_lt_eq_is_lt e1:
    forall e2 e3,
      expCompare e1 e2 = Lt -> expCompare e2 e3 = Eq -> expCompare e1 e3 = Lt.
  Proof.
    induction e1; intros * compare_lt compare_eq; destruct e2; simpl in *;
      destruct e3; try congruence.
    - rewrite Nat.compare_eq_iff in compare_eq; subst; auto.
    - destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq  m0 m1) eqn:?.
      + pose proof (V_orderedFacts.compare_compat). unfold Proper in H.
        apply V_orderedFacts.compare_eq_iff in compare_eq.
        specialize (H v v (V_orderedFacts.eq_refl v) v0 v1 compare_eq).
        type_conv; rewrite mTypeEq_refl, <- H; auto.
      + rewrite mTypeEq_compat_eq in Heqb; subst.
        rewrite Heqb0. destruct (morePrecise m0 m1) eqn:?; congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb; destruct (morePrecise m m1) eqn:?; congruence.
      + destruct (morePrecise m0 m1); congruence.
    - destruct (unopEq u u0) eqn:?; destruct (unopEq u0 u1) eqn:?;
               try rewrite unopEq_compat_eq in *; subst;
        try rewrite unopEq_refl; try auto; try congruence.
      + eapply IHe1; eauto.
      + destruct (unopEq u0 Neg); congruence.
      + destruct (unopEq u Neg); try congruence.
        destruct (unopEq u u1); congruence.
      + destruct (unopEq u0 Neg); congruence.
    - destruct b; destruct b0; try congruence;
        destruct b1; try congruence;
          destruct (expCompare e1_1 e2_1) eqn:?;
               destruct (expCompare e2_1 e3_1) eqn:?;
               try congruence;
          try (erewrite IHe1_1; eauto; fail "");
          try erewrite expCompare_eq_trans; eauto.
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?.
      + type_conv; subst. rewrite mTypeEq_refl. eapply IHe1; eauto.
      + destruct (morePrecise m0 m1); congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb. destruct (morePrecise m m1) eqn:?;  congruence.
      + destruct (morePrecise m0 m1); congruence.
  Qed.

  Lemma expCompare_eq_lt_is_lt e1:
    forall e2 e3,
      expCompare e1 e2 = Eq -> expCompare e2 e3 = Lt -> expCompare e1 e3 = Lt.
  Proof.
    induction e1; intros * compare_eq compare_lt; destruct e2; simpl in *;
      destruct e3; try congruence.
    - rewrite Nat.compare_eq_iff in compare_eq; subst; auto.
    - destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq  m0 m1) eqn:?.
      + pose proof (V_orderedFacts.compare_compat). unfold Proper in H.
        apply V_orderedFacts.compare_eq_iff in compare_eq.
        specialize (H v v0 compare_eq v1 v1 (V_orderedFacts.eq_refl v1)).
        type_conv; rewrite mTypeEq_refl, H; auto.
      + rewrite mTypeEq_compat_eq in Heqb; subst.
        rewrite Heqb0. destruct (morePrecise m0 m1) eqn:?; congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb; destruct (morePrecise m m1) eqn:?; congruence.
      + destruct (morePrecise m m0); congruence.
    - destruct (unopEq u u0) eqn:?; destruct (unopEq u0 u1) eqn:?;
               try rewrite unopEq_compat_eq in *; subst;
        try rewrite unopEq_refl; try auto; try congruence.
      + eapply IHe1; eauto.
      + rewrite Heqb0. destruct (unopEq u0 Neg); congruence.
      + destruct (unopEq u Neg); congruence.
      + destruct (unopEq u Neg); congruence.
    - destruct b; destruct b0;
        destruct b1; try congruence;
          destruct (expCompare e1_1 e2_1) eqn:?;
                   destruct (expCompare e2_1 e3_1) eqn:?;
                   try congruence;
          try (erewrite IHe1_1; eauto; fail "");
          try erewrite expCompare_eq_trans; eauto.
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?.
      + type_conv; subst. rewrite mTypeEq_refl. eapply IHe1; eauto.
      + rewrite mTypeEq_compat_eq in Heqb; subst.
        rewrite Heqb0.
        destruct (morePrecise m0 m1); congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb. destruct (morePrecise m m1) eqn:?;  congruence.
      + destruct (morePrecise m m0); congruence.
  Qed.

  Definition eq e1 e2 :=
    expCompare e1 e2 = Eq.

  Definition lt (e1:exp V) (e2: exp V):=
    expCompare e1 e2 = Lt.

  Instance lt_strorder : StrictOrder lt.
  Proof.
    split.
    - unfold Irreflexive.
      unfold Reflexive.
      intros x; unfold complement.
      intros lt_x.
      induction x; unfold lt in *; simpl in lt_x.
      + rewrite PeanoNat.Nat.compare_refl in lt_x. congruence.
      + rewrite mTypeEq_refl, V_orderedFacts.compare_refl in *;
          congruence.
      + rewrite unopEq_refl in *; simpl in *.
        apply IHx; auto.
      + destruct b;
          destruct (expCompare x1 x1) eqn:?; try congruence.
      + rewrite mTypeEq_refl in lt_x.
        apply IHx; auto.
    - unfold Transitive.
      intros e1. unfold lt.
      induction e1; intros * lt_e1_e2 lt_e2_e3;
        simpl in *; destruct y; destruct z;
          simpl in *; try auto; try congruence.
      + rewrite <- nat_compare_lt in *. omega.
      + destruct (mTypeEq m m0) eqn:?;
                 destruct (mTypeEq m0 m1) eqn:?.
        * type_conv;
            rewrite mTypeEq_refl, V_orderedFacts.compare_lt_iff in *;
            eapply V_orderedFacts.lt_trans; eauto.
        * rewrite mTypeEq_compat_eq in Heqb; subst.
          rewrite Heqb0. assumption.
        * rewrite mTypeEq_compat_eq in Heqb0; subst.
          rewrite Heqb; assumption.
        * destruct (mTypeEq m m1) eqn:?.
          { rewrite mTypeEq_compat_eq in Heqb1; subst.
            destruct (morePrecise m0 m1) eqn:?;
                     destruct (morePrecise m1 m0) eqn:?;
                     try congruence.
            pose proof (morePrecise_antisym _ _ Heqb1 Heqb2).
            type_conv; congruence. }
          { destruct (morePrecise m m0) eqn:?;
                     destruct (morePrecise m0 m1) eqn:?;
                     try congruence.
            erewrite morePrecise_trans; eauto. }
      + destruct (unopEq u u0) eqn:?;
                 destruct (unopEq u0 u1) eqn:?;
                 try rewrite unopEq_compat_eq in *; subst;
          [ destruct (expCompare e1 y) eqn:?; try congruence;
            rewrite unopEq_refl;
            eapply IHe1; eauto
          | destruct (unopEq u0 Neg) eqn:?; try congruence;
            rewrite unopEq_compat_eq in *; subst
          | |].
        * rewrite Heqb0; auto.
        * destruct (unopEq u Neg) eqn:?; try congruence; rewrite unopEq_compat_eq in *; subst.
          rewrite Heqb; auto.
        * destruct (unopEq u u1) eqn:?; try congruence.
          rewrite unopEq_compat_eq in Heqb1; subst.
          destruct (unopEq u1 Neg) eqn:?; try congruence;
            destruct (unopEq u0 Neg) eqn:?; try congruence;
            rewrite unopEq_compat_eq in *; subst.
          simpl in *; congruence.
      + destruct b; destruct b0; destruct b1; try congruence;
          destruct (expCompare e1_1 y1) eqn:?; try congruence;
          destruct (expCompare y1 z1) eqn:?; try congruence;
          try (erewrite expCompare_eq_trans; eauto; fail);
          try (erewrite expCompare_eq_lt_is_lt; eauto; fail);
          try (erewrite expCompare_lt_eq_is_lt; eauto; fail);
          try (erewrite IHe1_1; eauto).
      + destruct (mTypeEq m m0) eqn:?;
                 destruct (mTypeEq m0 m1) eqn:?;
                 [type_conv; subst; rewrite mTypeEq_refl | | | ].
        * eapply IHe1; eauto.
        * rewrite mTypeEq_compat_eq in Heqb; subst.
          rewrite Heqb0; destruct (morePrecise m0 m1); congruence.
        * rewrite mTypeEq_compat_eq in Heqb0; subst.
          rewrite Heqb. destruct (morePrecise m m1); congruence.
        * destruct (mTypeEq m m1) eqn:?.
          { rewrite mTypeEq_compat_eq in Heqb1; subst.
            destruct (morePrecise m1 m0) eqn:?; try congruence.
            destruct (morePrecise m0 m1) eqn:?; try congruence.
            pose proof (morePrecise_antisym _ _ Heqb1 Heqb2).
            type_conv; subst. congruence. }
          { destruct (morePrecise m m0) eqn:?; try congruence.
            destruct (morePrecise m0 m1) eqn:?; try congruence.
            pose proof (morePrecise_trans _ _ _  Heqb2 Heqb3).
            rewrite H; auto. }
  Defined.

  Lemma eq_compat: Proper (eq ==> eq ==> iff) eq.
  Proof.
    unfold Proper; hnf.
    intros e1; induction e1;
    intros e2 e1_eq_e2; hnf;
    intros e3 e4 e3_eq_e4;
    unfold lt, eq in *;
    destruct e2,e3,e4; simpl in *; try congruence; try (split; auto; fail).
    - repeat rewrite Nat.compare_eq_iff in *; subst. split; try auto.
    - destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq m1 m2) eqn:?;
               [type_conv | | |].
      + rewrite V_orderedFacts.compare_eq_iff in *.
        rewrite (V_orderedFacts.compare_compat e1_eq_e2 e3_eq_e4).
        split; auto.
      + destruct (morePrecise m1 m2); congruence.
      + destruct (morePrecise m m0); congruence.
      + destruct (morePrecise m m0); congruence.
    - destruct (unopEq u u0) eqn:?;
               destruct (unopEq u1 u2) eqn:?;
               try rewrite unopEq_compat_eq in *; subst;
        try (destruct (unopEq u Neg); congruence);
            try (destruct (unopEq u1 Neg); congruence).
      specialize (IHe1 e2 e1_eq_e2 e3 e4 e3_eq_e4).
      simpl in *. destruct (unopEq u0 u2); try rewrite IHe1; split; auto.
    - destruct b; destruct b0; destruct b1; destruct b2; try congruence;
        try (split; auto; fail);
        destruct (expCompare e1_1 e2_1) eqn:?;
                 destruct (expCompare e3_1 e4_1) eqn:?;
                 try congruence;
        destruct (expCompare e1_1 e3_1) eqn:?;
                 destruct (expCompare e2_1 e4_1) eqn:?;
                 try (split; congruence);
      try (specialize (IHe1_2 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *; rewrite IHe1_2 in *; split; auto; fail);
      try (split; try congruence; intros);
      try (specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *; rewrite IHe1_1 in *; congruence);
      try (specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *; rewrite <- IHe1_1 in *; congruence).
    -  destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq m1 m2) eqn:?;
               [type_conv | | |].
       + specialize (IHe1 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *.
         destruct (mTypeEq m0 m2); try congruence.
         split; auto.
       + destruct (morePrecise m1 m2); congruence.
       + destruct (morePrecise m m0); congruence.
       + destruct (morePrecise m m0); congruence.
  Qed.

  Instance lt_compat: Proper (eq ==> eq ==> iff) lt.
  Proof.
    unfold Proper; hnf.
    intros e1; induction e1;
    intros e2 e1_eq_e2; hnf;
    intros e3 e4 e3_eq_e4;
    unfold lt, eq in *;
    destruct e2,e3,e4; simpl in *; try congruence; try (split; auto; fail).
    - rewrite Nat.compare_eq_iff in *; subst. split; try auto.
    - destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq m1 m2) eqn:?;
               [type_conv | | |].
      + rewrite V_orderedFacts.compare_eq_iff in *.
        rewrite (V_orderedFacts.compare_compat e1_eq_e2 e3_eq_e4).
        split; auto.
      + destruct (morePrecise m1 m2); congruence.
      + destruct (morePrecise m m0); congruence.
      + destruct (morePrecise m m0); congruence.
    - destruct (unopEq u u0) eqn:?;
               destruct (unopEq u1 u2) eqn:?;
               try rewrite unopEq_compat_eq in *; subst;
        try (destruct (unopEq u Neg); congruence);
            try (destruct (unopEq u1 Neg); congruence).
      specialize (IHe1 e2 e1_eq_e2 e3 e4 e3_eq_e4).
      simpl in *. destruct (unopEq u0 u2); try rewrite IHe1; split; auto.
    - pose proof eq_compat as eq_comp. unfold Proper, eq in eq_comp.
      destruct b, b0, b1, b2; try congruence; try (split; auto; fail);
        destruct (expCompare e1_1 e2_1) eqn:?;
                 destruct (expCompare e3_1 e4_1) eqn:?;
                 try congruence;
        destruct (expCompare e1_1 e3_1) eqn:?;
                 destruct (expCompare e2_1 e4_1) eqn:?;
                 try (split; congruence);
        try (specialize (IHe1_2 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *; rewrite IHe1_2 in *; split; auto; fail);
        try (split; try congruence; intros);
        try (specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *; rewrite IHe1_1 in *; congruence);
        try (specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *; rewrite <- IHe1_1 in *; congruence);
        try (rewrite (eq_comp _ _ Heqc _ _ Heqc0) in *; congruence);
        try (rewrite <- (eq_comp _ _ Heqc _ _ Heqc0) in *; congruence).
    -  destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq m1 m2) eqn:?;
               [type_conv | | |].
       + specialize (IHe1 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *.
         destruct (mTypeEq m0 m2); try congruence.
         split; auto.
       + destruct (morePrecise m1 m2); congruence.
       + destruct (morePrecise m m0); congruence.
       + destruct (morePrecise m m0); congruence.
  Defined.

  Lemma compare_spec : forall x y, CompSpec eq lt x y (expCompare x y).
  Proof.
    intros e1 e2.
    destruct (expCompare e1 e2) eqn:?.
    - apply CompEq.
      unfold eq; auto.
    - apply CompLt. unfold lt; auto.
    - apply CompGt. unfold lt.
      rewrite expCompare_antisym in Heqc.
      rewrite CompOpp_iff in Heqc.
      simpl in *; auto.
  Qed.

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
  Instance eq_equiv: Equivalence eq.
  Proof.
    split; unfold Reflexive, Symmetric, Transitive, eq.
    - apply expCompare_refl.
    - intros. rewrite expCompare_antisym in * |-.
      rewrite CompOpp_iff in * |- .
      auto.
    - apply expCompare_eq_trans.
  Defined.

  Parameter eq_dec : forall x y, { eq x y } + { ~ eq x y }.

  Definition eq_refl : forall x, eq x x.
  Proof.
    apply expCompare_refl.
  Defined.

  Definition eq_sym : forall x y, eq x y -> eq y x.
  Proof.
    unfold eq; intros.
    rewrite expCompare_antisym in * |-.
    rewrite CompOpp_iff in * |-.
    auto.
  Defined.

  Definition eq_trans : forall x y z, eq x y -> eq y z -> eq x z.
  Proof.
    apply expCompare_eq_trans.
  Defined.

  Definition lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
  Proof.
    pose proof lt_strorder as [_ Trans].
    apply Trans.
  Defined.
680

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  Definition lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
  Proof.
    intros. unfold lt,eq in *. hnf; intros; congruence.
  Defined.

  Definition compare e1 e2:= expCompare e1 e2.
  (* Definition compare (e1 e2:t) :Compare lt eq e1 e2. *)
  (* Proof. *)
  (*   destruct (expCompare e1 e2) eqn:?. *)
  (*   - eapply EQ. unfold eq; auto. *)
  (*   - eapply LT; auto. *)
  (*   - eapply GT. rewrite expCompare_antisym in * |-. *)
  (*     rewrite CompOpp_iff in *. *)
  (*     auto. *)
  (* Defined. *)

End ExpOrderedType.
698

699 700
Fixpoint toRExp (e:exp Q) :=
  match e with
Nikita Zyuzin's avatar
Nikita Zyuzin committed
701 702 703 704
  | Var _ v => Var R v
  | Const m n => Const m (Q2R n)
  | Unop o e1 => Unop o (toRExp e1)
  | Binop o e1 e2 => Binop o (toRExp e1) (toRExp e2)
705
  | Fma e1 e2 e3 => Fma (toRExp e1) (toRExp e2) (toRExp e3)
Nikita Zyuzin's avatar
Nikita Zyuzin committed
706
  | Downcast m e1 => Downcast m (toRExp e1)
707
  end.
708

709 710
Fixpoint toREval (e:exp R) :=
  match e with
711
  | Var _ v => Var R v
712
  | Const _ n => Const M0 n
713 714
  | Unop o e1 => Unop o (toREval e1)
  | Binop o e1 e2 => Binop o (toREval e1) (toREval e2)
715
  | Fma e1 e2 e3 => Fma (toREval e1) (toREval e2) (toREval e3)
716
  | Downcast _ e1 =>   Downcast M0 (toREval e1)
717
  end.
718

719
Definition toRMap (d:nat -> option mType) (n:nat) :=
='s avatar
= committed
720 721 722
  match d n with
  | Some m => Some M0
  | None => None
723
  end.
724

725 726
Arguments toRMap _ _/.

727 728 729 730
(**
  Define a perturbation function to ease writing of basic definitions
**)
Definition perturb (r:R) (e:R) :=
731
  (r * (1 + e))%R.
Heiko Becker's avatar
Heiko Becker committed
732

733 734
Hint Unfold perturb.

735
(**
736
Define expression evaluation relation parametric by an "error" epsilon.
737 738 739
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.
740
**)
741
Inductive eval_exp (E:env) (Gamma: nat -> option mType) :(exp R) -> R -> mType -> Prop :=
742
| Var_load m x v:
743
    Gamma x = Some m ->
744
    E x = Some v ->
745
    eval_exp E Gamma (Var R x) v m
746
| Const_dist m n delta:
747 748
    Rle (Rabs delta) (Q2R (mTypeToQ m)) ->
    eval_exp E Gamma (Const m n) (perturb n delta) m
749
| Unop_neg m f1 v1:
750 751
    eval_exp E Gamma f1 v1 m ->
    eval_exp E Gamma (Unop Neg f1) (evalUnop Neg v1) m
752
| Unop_inv m f1 v1 delta:
753 754 755 756
    Rle (Rabs delta) (Q2R (mTypeToQ m)) ->
    eval_exp E Gamma  f1 v1 m ->
    (~ v1 = 0)%R  ->
    eval_exp E Gamma (Unop Inv f1) (perturb (evalUnop Inv v1) delta) m
757
| Downcast_dist m m1 f1 v1 delta:
758
    (* Downcast expression f1 (evaluating to machine type m1), to a machine type m, less precise than m1.*)
759
    isMorePrecise m1 m = true ->
760 761 762
    Rle (Rabs delta) (Q2R (mTypeToQ m)) ->
    eval_exp E Gamma f1 v1 m1 ->
    eval_exp E Gamma (Downcast m f1) (perturb v1 delta) m
763
| Binop_dist m1 m2 op f1 f2 v1 v2 delta:
764 765 766
    Rle (Rabs delta) (Q2R (mTypeToQ (join m1 m2))) ->
    eval_exp E Gamma f1 v1 m1 ->
    eval_exp E Gamma f2 v2 m2 ->
767
    ((op = Div) -> (~ v2 = 0)%R) ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
768
    eval_exp E Gamma (Binop op f1 f2) (perturb (evalBinop op v1 v2) delta)  (join m1 m2)
769
| Fma_dist m1 m2 m3 f1 f2 f3 v1 v2 v3 delta:
Nikita Zyuzin's avatar
Nikita Zyuzin committed
770 771 772 773
    Rle (Rabs delta) (Q2R (mTypeToQ (join3 m1 m2 m3))) ->
    eval_exp E Gamma f1 v1 m1 ->
    eval_exp E Gamma f2 v2 m2 ->
    eval_exp E Gamma f3 v3 m3 ->
774 775
    eval_exp E Gamma (Fma f1 f2 f3)
             (perturb (evalFma v1 v2 v3) delta)
Nikita Zyuzin's avatar
Nikita Zyuzin committed
776
             (join3 m1 m2 m3).
777 778 779 780

Hint Constructors eval_exp.

(**
781
  Show some simpler (more general) rule lemmata
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
**)
Lemma Const_dist' m n delta v m' E Gamma:
  Rle (Rabs delta) (Q2R (mTypeToQ m)) ->
  v = perturb n delta ->
  m' = m ->
  eval_exp E Gamma (Const m n) v m'.
Proof.
  intros; subst; auto.
Qed.

Hint Resolve Const_dist'.

Lemma Unop_neg' m f1 v1 v m' E Gamma:
  eval_exp E Gamma f1 v1 m ->
  v = evalUnop Neg v1 ->
  m' = m ->
798
  eval_exp E Gamma (Unop Neg f1) v m'.
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
Proof.
  intros; subst; auto.
Qed.

Hint Resolve Unop_neg'.

Lemma Unop_inv' m f1 v1 delta v m' E Gamma:
  Rle (Rabs delta) (Q2R (mTypeToQ m)) ->
  eval_exp E Gamma  f1 v1 m ->
  (~ v1 = 0)%R  ->
  v = perturb (evalUnop Inv v1) delta ->
  m' = m ->
  eval_exp E Gamma (Unop Inv f1) v m'.
Proof.
  intros; subst; auto.
Qed.

Hint Resolve Unop_inv'.

Lemma Downcast_dist' m m1 f1 v1 delta v m' E Gamma:
  isMorePrecise m1 m = true ->
  Rle (Rabs delta) (Q2R (mTypeToQ m)) ->
  eval_exp E Gamma f1 v1 m1 ->
  v = (perturb v1 delta) ->
  m' = m ->
  eval_exp 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' E Gamma:
  Rle (Rabs delta) (Q2R (mTypeToQ m')) ->
  eval_exp E Gamma f1 v1 m1 ->
  eval_exp E Gamma f2 v2 m2 ->
  ((op = Div) -> (~ v2 = 0)%R) ->
  v = perturb (evalBinop op v1 v2) delta ->
  m' = join m1 m2 ->
  eval_exp E Gamma (Binop op f1 f2) v m'.
Proof.
  intros; subst; auto.
Qed.

Hint Resolve Binop_dist'.
844

845
Lemma Fma_dist' m1 m2 m3 f1 f2 f3 v1 v2 v3 delta v m' E Gamma:
Nikita Zyuzin's avatar
Nikita Zyuzin committed
846 847 848 849
  Rle (Rabs delta) (Q2R (mTypeToQ m')) ->
  eval_exp E Gamma f1 v1 m1 ->
  eval_exp E Gamma f2 v2 m2 ->
  eval_exp E Gamma f3 v3 m3 ->
850
  v = perturb (evalFma v1 v2 v3) delta ->
Nikita Zyuzin's avatar
Nikita Zyuzin committed
851
  m' = join3 m1 m2 m3 ->
852
  eval_exp E Gamma (Fma f1 f2 f3) v m'.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
853 854 855 856
Proof.
  intros; subst; auto.
Qed.

857
Hint Resolve Fma_dist'.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
858

859 860 861 862 863
(**
  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 :=
864 865
  match e with
  | Var _ x => NatSet.singleton x
866 867
  | Unop u e1 => usedVars e1
  | Binop b e1 e2 => NatSet.union (usedVars e1) (usedVars e2)
868
  | Fma e1 e2 e3 => NatSet.union (usedVars e1) (NatSet.union (usedVars e2) (usedVars e3))
869
  | Downcast _ e1 => usedVars e1
870 871
  | _ => NatSet.empty
  end.
872

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
Lemma toRMap_eval_M0 f v E Gamma m:
  eval_exp E (toRMap Gamma) (toREval f) v m -> m = M0.
Proof.
  revert v E Gamma m.
  induction f; intros * eval_f; inversion eval_f; subst;
  repeat
    match goal with
    | H: context[toRMap _ _] |- _ => unfold toRMap in H
    | 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.
  - eapply IHf; eauto.
  - eapply IHf; eauto.
  - assert (m1 = M0)
      by (eapply IHf1; eauto).
    assert (m2 = M0)
      by (eapply IHf2; eauto);
      subst; auto.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
892 893 894 895 896 897 898
  - assert (m1 = M0)
      by (eapply IHf1; eauto).
    assert (m2 = M0)
      by (eapply IHf2; eauto).
    assert (m3 = M0)
      by (eapply IHf3; eauto);
      subst; auto.
899 900
Qed.

901
(**
902
  If |delta| <= 0 then perturb v delta is exactly v.
903
**)
904
Lemma delta_0_deterministic (v:R) (delta:R):
Heiko Becker's avatar
Heiko Becker committed
905 906 907 908
  (Rabs delta <= 0)%R ->
  perturb v delta = v.
Proof.
  intros abs_0; apply Rabs_0_impl_eq in abs_0; subst.
909
  unfold perturb. lra.
Heiko Becker's avatar
Heiko Becker committed
910 911
Qed.

912
(**
913
Evaluation with 0 as machine epsilon is deterministic
914
**)
915
Lemma meps_0_deterministic (f:exp R) (E:env) Gamma:
916
  forall v1 v2,
917 918
  eval_exp E (toRMap Gamma) (toREval f) v1 M0 ->
  eval_exp E (toRMap Gamma) (toREval f) v2 M0 ->
919 920
  v1 = v2.
Proof.
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
  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 *.
    rewrite Q2R0_is_0 in *;
    repeat (rewrite delta_0_deterministic; try auto).
  - inversion ev1; inversion ev2; subst; try congruence.
    + rewrite (IHf v0 v3); eauto.
    + rewrite (IHf v0 v3); eauto.
      simpl in *.
      rewrite Q2R0_is_0 in *;
        repeat (rewrite delta_0_deterministic; try auto).
  - inversion ev1; inversion ev2; subst.
937 938 939 940 941
    assert (m0 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m3 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m1 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m2 = M0) by (eapply toRMap_eval_M0; eauto).
    subst.
942 943 944 945 946
    rewrite (IHf1 v0 v4); try auto.
    rewrite (IHf2 v3 v5); try auto.
    simpl in *.
    rewrite Q2R0_is_0 in *.
    repeat (rewrite delta_0_deterministic; try auto).
Nikita Zyuzin's avatar
Nikita Zyuzin committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960
  - inversion ev1; inversion ev2; subst.
    assert (m0 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m1 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m2 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m3 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m4 = M0) by (eapply toRMap_eval_M0; eauto).
    assert (m5 = M0) by (eapply toRMap_eval_M0; eauto).
    subst.
    rewrite (IHf1 v0 v5); try auto.
    rewrite (IHf2 v3 v6); try auto.
    rewrite (IHf3 v4 v7); try auto.
    simpl in *.
    rewrite Q2R0_is_0 in *.
    repeat (rewrite delta_0_deterministic; try auto).
961 962 963 964 965 966 967
  - inversion ev1; inversion ev2; subst.
    apply M0_least_precision in H1;
      apply M0_least_precision in H7; subst.
    rewrite (IHf v0 v3); try auto.
    simpl in *.
    rewrite Q2R0_is_0 in *.
    repeat (rewrite delta_0_deterministic; try auto).
968 969
Qed.

970
(**
Nikita Zyuzin's avatar
Nikita Zyuzin committed
971
Helping lemmas. Needed in soundness proof.
972 973
For each evaluation of using an arbitrary epsilon, we can replace it by
evaluating the subexpressions and then binding the result values to different
974
variables in the Environment.
975
 **)
976
Lemma binary_unfolding b f1 f2 E v1 v2 m1 m2 Gamma delta:
977
  (b = Div -> ~(v2 = 0 )%R) ->
978
  (Rabs delta <= Q2R (mTypeToQ (join m1 m2)))%R ->
979 980
  eval_exp E Gamma f1 v1 m1 ->
  eval_exp E Gamma f2 v2 m2 ->
981
  eval_exp E Gamma (Binop b f1 f2) (perturb (evalBinop b v1 v2) delta) (join m1 m2) ->
982 983
  eval_exp (updEnv 2 v2 (updEnv 1 v1 emptyEnv))
           (updDefVars 2 m2 (updDefVars 1 m1 Gamma))
984
             (Binop b (Var R 1) (Var R 2)) (perturb (evalBinop b v1 v2) delta) (join m1 m2).
985
Proof.
986 987
  intros no_div_zero eval_f1 eval_f2 eval_float.
  econstructor; try auto.
988 989
Qed.

Nikita Zyuzin's avatar
Nikita Zyuzin committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
Lemma fma_unfolding f1 f2 f3 E v1 v2 v3 m1 m2 m3 Gamma delta:
  (Rabs delta <= Q2R (mTypeToQ (join3 m1 m2 m3)))%R ->
  eval_exp E Gamma f1 v1 m1 ->
  eval_exp E Gamma f2 v2 m2 ->
  eval_exp E Gamma f3 v3 m3 ->
  eval_exp E Gamma (Fma f1 f2 f3) (perturb (evalFma v1 v2 v3) delta) (join3 m1 m2 m3) ->
  eval_exp (updEnv 3 v3 (updEnv 2 v2 (updEnv 1 v1 emptyEnv)))
           (updDefVars 3 m3 (updDefVars 2 m2 (updDefVars 1 m1 Gamma)))
             (Fma (Var R 1) (Var R 2) (Var R 3)) (perturb (evalFma v1 v2 v3) delta) (join3 m1 m2 m3).
Proof.
  econstructor; try auto.
Qed.

Heiko Becker's avatar
Heiko Becker committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
Lemma eval_eq_env e:
  forall E1 E2 Gamma v m,
    (forall x, E1 x = E2 x) ->
    eval_exp E1 Gamma e v m ->
    eval_exp E2 Gamma e v m.
Proof.
  induction e; intros;
    (match_pat (eval_exp _ _ _ _ _) (fun H => inversion H; subst; simpl in *));
    try eauto.
  eapply Var_load; auto.
  rewrite <- (H n); auto.
Qed.

1016
(*
1017 1018 1019
(**
Analogous lemma for unary expressions.
**)
1020 1021
Lemma unary_unfolding (e:exp R) (eps:R) (E:env) (v:R):
  (eval_exp eps E (Unop Inv e) v <->
1022
   exists v1,
1023 1024
     eval_exp eps E e v1 /\
     eval_exp eps (updEnv 1 v1 E) (Unop Inv (Var R 1)) v).
1025 1026 1027 1028 1029 1030 1031 1032 1033
Proof.
  split.
  - intros eval_un.
    inversion eval_un; subst.
    exists v1.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
  - intros exists_val.
1034 1035
    destruct exists_val as [v1 [eval_f1 eval_e_E]].
    inversion eval_e_E; subst.
1036 1037 1038
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
1039
    inversion H3; subst; auto.
1040
Qed. *)
1041

1042
(*   Using the parametric expressions, define boolean expressions for conditionals *)
1043
(* **)
1044 1045 1046
(* Inductive bexp (V:Type) : Type := *)
(*   leq: exp V -> exp V -> bexp V *)
(* | less: exp V -> exp V -> bexp V. *)
1047

1048
(**
1049
  Define evaluation of boolean expressions
1050
 **)
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
(* Inductive bval (E:env): (bexp R) -> Prop -> Prop := *)
(*   leq_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R): *)
(*     eval_exp E f1 v1 -> *)
(*     eval_exp E f2 v2 -> *)
(*     bval E (leq f1 f2) (Rle v1 v2) *)
(* |less_eval (f1:exp R) (f2:exp R) (v1:R) (v2:R): *)
(*     eval_exp E f1 v1 -> *)
(*     eval_exp E f2 v2 -> *)
(*     bval E (less f1 f2) (Rlt v1 v2). *)
(* (** *)
(*  Simplify arithmetic later by making > >= only abbreviations *)
(* **) *)
(* Definition gr := fun (V:Type) (f1: exp V) (f2: exp V) => less f2 f1. *)
Nikita Zyuzin's avatar
Nikita Zyuzin committed
1064
(* Definition greq := fun (V:Type) (f1:exp V) (f2: exp V) => leq f2 f1. *)