Expressions.v 73.3 KB
Newer Older
1
(**
2
  Formalization of the base exprression language for the flover framework
3
 **)
4 5
From Coq
     Require Import QArith.QArith Structures.Orders Recdef.
6

7 8 9 10 11
From Flover.Infra
     Require Import RealRationalProps RationalSimps Ltacs.

From Flover.Infra
     Require Export Abbrevs NatSet MachineType.
12

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

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

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

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

46 47
Lemma binopEq_compat_eq b1 b2:
  binopEq b1 b2 = true <-> b1 = b2.
48
Proof.
49 50 51 52 53 54 55 56 57 58
  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.
59 60
Qed.

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

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

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

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

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

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

102
Definition evalFma (v1:R) (v2:R) (v3:R):=
Joachim Bard's avatar
Joachim Bard committed
103
  evalBinop Plus (evalBinop Mult v1 v2) v3.
104

105
(**
106
  Define exprressions parametric over some value type V.
107
  Will ease reasoning about different instantiations later.
108
**)
109 110 111 112 113 114
Inductive expr (V:Type): Type :=
  Var: nat -> expr V
| Const: mType -> V -> expr V
| Unop: unop -> expr V -> expr V
| Binop: binop -> expr V -> expr V -> expr V
| Fma: expr V -> expr V -> expr V -> expr V
115 116
| Downcast: mType -> expr V -> expr V
(* TODO: do we need mType in let-exprs? *)
Joachim Bard's avatar
Joachim Bard committed
117 118
| Let: mType -> nat -> expr V -> expr V -> expr V.
(* | Cond: expr V -> expr V -> expr V -> expr V.*)
119

120
Fixpoint toRExp (e:expr Q) :=
121
  match e with
122 123 124 125
  | 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)
126
  | Fma e1 e2 e3 => Fma (toRExp e1) (toRExp e2) (toRExp e3)
127
  | Downcast m e1 => Downcast m (toRExp e1)
128
  | Let m v e1 e2 => Let m v (toRExp e1) (toRExp e2)
Joachim Bard's avatar
Joachim Bard committed
129
  (* | Cond e1 e2 e3 => Cond (toRExp e1) (toRExp e2) (toRExp e3)*)
130
  end.
131

132
Fixpoint toREval (e:expr R) :=
133
  match e with
134
  | Var _ v => Var R v
135
  | Const _ n => Const REAL n
136 137
  | Unop o e1 => Unop o (toREval e1)
  | Binop o e1 e2 => Binop o (toREval e1) (toREval e2)
138
  | Fma e1 e2 e3 => Fma (toREval e1) (toREval e2) (toREval e3)
139
  | Downcast _ e1 =>   Downcast REAL (toREval e1)
140
  | Let _ v e1 e2 => Let REAL v (toREval e1) (toREval e2)
Joachim Bard's avatar
Joachim Bard committed
141
 (* | Cond e1 e2 e3 => Cond (toREval e1) (toREval e2) (toREval e3) *)
142
  end.
143

144 145
Definition toRMap (d:expr R -> option mType) (e: expr R) :=
  match d e with
146
  | Some m => Some REAL
='s avatar
= committed
147
  | None => None
148
  end.
149

150 151
Arguments toRMap _ _/.

152
(**
153
  Define the set of "used" variables of an expression to be the set of variables
154
  occuring in it
155
**)
156
(*
157 158 159
Fixpoint usedVars (V:Type) (e:expr V) :NatSet.t :=
  match e with
  | Var _ x => NatSet.singleton x
160
  | Const _ _ => NatSet.empty
161 162 163 164
  | Unop u e1 => usedVars e1
  | Binop b e1 e2 => NatSet.union (usedVars e1) (usedVars e2)
  | Fma e1 e2 e3 => NatSet.union (usedVars e1) (NatSet.union (usedVars e2) (usedVars e3))
  | Downcast _ e1 => usedVars e1
165
  | Let _ x e1 e2 => NatSet.union (NatSet.singleton x) (NatSet.union (usedVars e1) (usedVars e2))
166 167 168 169 170 171 172 173 174 175 176 177 178
  | Cond e1 e2 e3 => usedVars e1  usedVars e2  usedVars e3
  end.
*)

Fixpoint freeVars (V:Type) (e:expr V) :NatSet.t :=
  match e with
  | Var _ x => NatSet.singleton x
  | Const _ _ => NatSet.empty
  | Unop u e1 => freeVars e1
  | Binop b e1 e2 => NatSet.union (freeVars e1) (freeVars e2)
  | Fma e1 e2 e3 => NatSet.union (freeVars e1) (NatSet.union (freeVars e2) (freeVars e3))
  | Downcast _ e1 => freeVars e1
  | Let _ x e1 e2 => freeVars e1  NatSet.remove x (freeVars e2)
Joachim Bard's avatar
Joachim Bard committed
179
(*  | Cond e1 e2 e3 => freeVars e1  freeVars e2  freeVars e3 *)
180
  end.
Heiko Becker's avatar
Heiko Becker committed
181

182
Module Type OrderType := Coq.Structures.Orders.OrderedType.
183

184 185
Module ExprOrderedType (V_ordered:OrderType) <: OrderType.
  Module V_orderedFacts := OrdersFacts.OrderedTypeFacts (V_ordered).
186

187 188
  Definition V := V_ordered.t.
  Definition t := expr V.
189

190
  Open Scope positive_scope.
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  Fixpoint exprCompare (e1:expr V) (e2:expr 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
        match m1, m2 with
        | F w1 f1, F w2 f2 =>
          match w1 ?= w2 with
          |Eq => (f1 ?= f2)%N
          | c => c
          end
        | F w f, _ => Lt
        | _, F w f => Gt
        | _, _ => (if morePrecise m1 m2 then Lt else Gt)
        end
    | Const _ _, Var _ _ => Gt
    | Const _ _, _ => Lt
    | Unop u1 e1, Unop u2 e2 =>
      if unopEq u1 u2
      then exprCompare e1 e2
      else (if unopEq u1 Neg then Lt else Gt)
216 217 218
    | Unop _ _, Var _ _ => Gt
    | Unop _ _, Const _ _ => Gt
    | Unop _ _, _ => Lt
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    | Downcast m1 e1, Downcast m2 e2 =>
      if mTypeEq m1 m2
      then exprCompare e1 e2
      else
        match m1, m2 with
        | F w1 f1, F w2 f2 =>
          match w1 ?= w2 with
          |Eq => (f1 ?= f2)%N
          | c => c
          end
        | F w f, _ => Lt
        | _, F w f => Gt
        | _, _ => (if morePrecise m1 m2 then Lt else Gt)
        end
233 234 235 236
    | Downcast _ _, Var _ _ => Gt
    | Downcast _ _, Const _ _ => Gt
    | Downcast _ _, Unop _ _ => Gt
    | Downcast _ _, _ => Lt
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    | 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 exprCompare e11 e21 with
        | Eq => exprCompare e12 e22
        | Lt => Lt
        | Gt => Gt
        end
      | _ => res
      end
260 261 262 263 264
    | Binop _ _ _, Var _ _ => Gt
    | Binop _ _ _, Const _ _ => Gt
    | Binop _ _ _, Unop _ _ => Gt
    | Binop _ _ _, Downcast _ _ => Gt
    | Binop _ _ _, _ => Lt
265 266 267 268 269 270 271 272 273 274
    | Fma e11 e12 e13, Fma e21 e22 e23 =>
      match exprCompare e11 e21 with
      | Eq => match exprCompare e12 e22 with
             | Eq => exprCompare e13 e23
             | Lt => Lt
             | Gt => Gt
             end
      | Lt => Lt
      | Gt => Gt
      end
275 276 277 278 279 280 281 282
    | Fma _ _ _, Var _ _ => Gt
    | Fma _ _ _, Const _ _ => Gt
    | Fma _ _ _, Unop _ _ => Gt
    | Fma _ _ _, Downcast _ _ => Gt
    | Fma _ _ _, Binop _ _ _ => Gt
    | Fma _ _ _ , _ => Lt
    | Let m1 n1 e11 e12, Let m2 n2 e21 e22 =>
      if mTypeEq m1 m2
Joachim Bard's avatar
Joachim Bard committed
283 284 285 286 287 288 289 290 291
      then
        match Nat.compare n1 n2 with
        | Eq =>
          match exprCompare e11 e21 with
          | Eq => exprCompare e12 e22
          | r => r
          end
        | r => r
        end
292
      else
Joachim Bard's avatar
Joachim Bard committed
293 294 295 296 297 298 299 300 301 302 303
        match Nat.compare n1 n2 with
        | Eq =>
          match m1, m2 with
          | F w1 f1, F w2 f2 =>
            match w1 ?= w2 with
            |Eq => (f1 ?= f2)%N
            | c => c
            end
          | F w f, _ => Lt
          | _, F w f => Gt
          | _, _ => (if morePrecise m1 m2 then Lt else Gt)
304
          end
Joachim Bard's avatar
Joachim Bard committed
305
        | r => r
306 307 308 309 310 311 312
        end
    | Let _ _ _ _, Var _ _ => Gt
    | Let _ _ _ _, Const _ _ => Gt
    | Let _ _ _ _, Unop _ _ => Gt
    | Let _ _ _ _, Downcast _ _ => Gt
    | Let _ _ _ _, Binop _ _ _ => Gt
    | Let _ _ _ _, Fma _ _ _ => Gt
Joachim Bard's avatar
Joachim Bard committed
313
    (*
314 315 316 317 318 319 320 321 322 323
    | Let _ _ _ _, _ => Lt
    | Cond e11 e12 e13, Cond e21 e22 e23 =>
      match exprCompare e11 e21 with
      | Eq => match exprCompare e12 e22 with
             | Eq => exprCompare e13 e23
             | r => r
             end
      | r => r
      end
    | Cond _ _ _, _ => Gt
Joachim Bard's avatar
Joachim Bard committed
324
     *)
325
    end.
326

327 328 329 330 331 332 333 334 335
  Lemma exprCompare_refl e: exprCompare 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.
    - now rewrite IHe1, IHe2, IHe3.
    - rewrite mTypeEq_refl; auto.
Joachim Bard's avatar
Joachim Bard committed
336
    - now rewrite mTypeEq_refl, Nat.compare_refl, IHe1, IHe2.
Joachim Bard's avatar
Joachim Bard committed
337
    (* - now rewrite IHe1, IHe2, IHe3. *)
338
  Qed.
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
  Lemma exprCompare_eq_trans e1 :
    forall e2 e3,
      exprCompare e1 e2 = Eq ->
      exprCompare e2 e3 = Eq ->
      exprCompare 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:?; type_conv | ].
      + rewrite mTypeEq_refl.
        rewrite V_orderedFacts.compare_eq_iff in *;
          eapply V_orderedFacts.eq_trans; eauto.
      + rewrite <- mTypeEq_compat_eq_false in Heqb0; rewrite Heqb0.
        destruct m0; destruct m1; auto.
      + destruct (mTypeEq m m1) eqn:?; type_conv;
          destruct m0; destruct m1; simpl in *; try congruence.
        * destruct (w0 ?= w) eqn:?; destruct (f0 ?= f)%N eqn:?;
                   try congruence.
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct m; try congruence.
          destruct (w1 ?= w) eqn:?; destruct (f1 ?= f)%N eqn:?;
                   try congruence.
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
    - 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 (exprCompare e1_1 e2_1) eqn:?;
                   destruct (exprCompare e2_1 e3_1) eqn:?;
                   try congruence; try erewrite IHe1_1; eauto.
    - destruct (exprCompare e1_1 e2_1) eqn:?;
        destruct (exprCompare e2_1 e3_1) eqn:?;
      destruct (exprCompare e1_2 e2_2) eqn:?;
        destruct (exprCompare e2_2 e3_2) eqn:?;
      try congruence; try erewrite IHe1_1, IHe1_2; eauto.
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?;
               type_conv;
        try rewrite mTypeEq_refl.
      + eapply IHe1; eauto.
      + destruct (mTypeEq m0 m1) eqn:?; type_conv; congruence.
      + destruct (mTypeEq m m1) eqn:?; type_conv; congruence.
      + destruct (mTypeEq m m1) eqn:?; type_conv; try congruence;
          destruct (morePrecise _ m0); try congruence;
            destruct m0,m1;
            cbn in *; try congruence.
        * destruct (w0 ?= w) eqn:?; destruct (f0 ?= f)%N eqn:?; try congruence.
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct (w0 ?= w) eqn:?; destruct (f0 ?= f)%N eqn:?; try congruence.
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct m; try congruence.
          destruct (w1 ?= w) eqn:?; destruct (f1 ?= f)%N eqn:?; try congruence.
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct m; try congruence.
          destruct (w1 ?= w) eqn:?; destruct (f1 ?= f)%N eqn:?; try congruence.
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
417 418 419 420
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?;
               type_conv;
        try rewrite mTypeEq_refl.
Joachim Bard's avatar
Joachim Bard committed
421 422 423 424
      + destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        apply nat_compare_eq in Hn. subst.
        destruct (n0 ?= n1)%nat eqn:?;
          destruct (exprCompare e1_1 e2_1) eqn:?;
425 426
          destruct (exprCompare e2_1 e3_1) eqn:?; try discriminate.
        erewrite IHe1_1; eauto.
Joachim Bard's avatar
Joachim Bard committed
427 428 429 430 431 432 433 434 435 436
      + destruct (mTypeEq m0 m1) eqn:?; type_conv; try congruence.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        apply nat_compare_eq in Hn. subst.
        destruct (n0 ?= n1)%nat eqn:?;
          destruct (exprCompare e1_1 e2_1) eqn:?;
          destruct (exprCompare e2_1 e3_1) eqn:?; congruence.
      + destruct (mTypeEq m m1) eqn:?; type_conv; try congruence.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        apply nat_compare_eq in Hn. subst.
        destruct (n0 ?= n1)%nat eqn:?; congruence.
437
      + destruct (mTypeEq m m1) eqn:?; type_conv;
Joachim Bard's avatar
Joachim Bard committed
438 439 440
          destruct (n ?= n0)%nat eqn:Hn; try discriminate;
          apply nat_compare_eq in Hn; subst;
          destruct (n0 ?= n1)%nat eqn:?; try congruence;
441 442 443
          destruct m0,m1;
          cbn in *; try congruence.
        * destruct (w0 ?= w) eqn:?; destruct (f0 ?= f)%N eqn:?; try congruence.
Joachim Bard's avatar
Joachim Bard committed
444 445
          apply Ndec.Pcompare_Peqb in Heqc0;
            apply N.compare_eq in Heqc1.
446 447 448
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct m; try congruence.
          destruct (w1 ?= w) eqn:?; destruct (f1 ?= f)%N eqn:?; try congruence.
Joachim Bard's avatar
Joachim Bard committed
449 450
          apply Ndec.Pcompare_Peqb in Heqc0;
            apply N.compare_eq in Heqc1.
451
            rewrite Pos.eqb_eq in *; subst; congruence.
Joachim Bard's avatar
Joachim Bard committed
452
            (*
453 454 455 456 457
    - destruct (exprCompare e1_1 e2_1) eqn:?;
        destruct (exprCompare e2_1 e3_1) eqn:?;
      destruct (exprCompare e1_2 e2_2) eqn:?;
        destruct (exprCompare e2_2 e3_2) eqn:?;
      try congruence; try erewrite IHe1_1, IHe1_2; eauto.
Joachim Bard's avatar
Joachim Bard committed
458
*)
Joachim Bard's avatar
Joachim Bard committed
459
  Qed.
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
  Lemma exprCompare_antisym e1:
    forall e2,
      exprCompare e1 e2 = CompOpp (exprCompare 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).
        * destruct m, m0; cbn in *; try congruence.
          rewrite N.compare_antisym.
          rewrite Pos.compare_antisym.
          rewrite Pos.leb_compare in *.
          destruct (w0 ?= w); simpl in *; try congruence.
        * destruct m, m0; unfold morePrecise in *; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
    - 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 (exprCompare 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.
Joachim Bard's avatar
Joachim Bard committed
507
    - destruct (exprCompare e1_1 e2_1) eqn:first_comp;
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
      destruct (exprCompare e1_2 e2_2) eqn:second_comp;
      rewrite IHe1_1, IHe1_2 in *; simpl in *;
        rewrite CompOpp_iff in first_comp;
        rewrite CompOpp_iff in second_comp;
        rewrite first_comp, second_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).
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
Joachim Bard's avatar
Joachim Bard committed
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
    - rewrite mTypeEq_sym.
      destruct (mTypeEq m0 m) eqn:?;
               type_conv; try auto.
      + rewrite Nat.compare_antisym.
        destruct (n0 ?= n)%nat; cbn; auto.
        destruct (exprCompare e1_1 e2_1) eqn:first_comp;
          destruct (exprCompare e1_2 e2_2) eqn:second_comp;
          rewrite IHe1_1, IHe1_2 in *;
          rewrite CompOpp_iff in first_comp;
          rewrite CompOpp_iff in second_comp;
          rewrite first_comp, second_comp; cbn; auto.
      + rewrite Nat.compare_antisym.
        destruct (n0 ?= n)%nat; cbn; auto.
        destruct (morePrecise m m0) eqn:?;
                 destruct (morePrecise m0 m) eqn:?;
                 try (split; auto; fail).
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
        * destruct m, m0; simpl in *; try congruence.
          setoid_rewrite N.compare_antisym at 2.
          setoid_rewrite Pos.compare_antisym at 2.
          destruct (w ?= w0) eqn:?;
                   destruct (f ?= f0)%N eqn:?; simpl; auto.
Joachim Bard's avatar
Joachim Bard committed
575
          (*
Joachim Bard's avatar
Joachim Bard committed
576 577 578 579 580 581
    - destruct (exprCompare e1_1 e2_1) eqn:first_comp;
      destruct (exprCompare e1_2 e2_2) eqn:second_comp;
      rewrite IHe1_1, IHe1_2 in *; simpl in *;
        rewrite CompOpp_iff in first_comp;
        rewrite CompOpp_iff in second_comp;
        rewrite first_comp, second_comp; simpl; try auto.
Joachim Bard's avatar
Joachim Bard committed
582
*)
Joachim Bard's avatar
Joachim Bard committed
583
  Qed.
584

585 586 587 588 589
  Lemma exprCompare_eq_sym e1 e2:
      exprCompare e1 e2 = Eq <-> exprCompare e2 e1 = Eq.
  Proof.
    now split; intros H; rewrite exprCompare_antisym; rewrite H.
  Qed.
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 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
  Lemma exprCompare_lt_eq_is_lt e1:
    forall e2 e3,
      exprCompare e1 e2 = Lt -> exprCompare e2 e3 = Eq -> exprCompare 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. type_conv; subst. destruct m0, m1; try congruence;
        try destruct (morePrecise _ _) eqn:?; try congruence.
        destruct (w ?= w0) eqn:?; destruct (f ?= f0)%N eqn:?; try congruence.
        apply Ndec.Pcompare_Peqb in Heqc;
          apply N.compare_eq in Heqc0;
          rewrite Pos.eqb_eq in *; subst; congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb; destruct (morePrecise m m1) eqn:?; congruence.
      + destruct (mTypeEq m m1); type_conv.
        * destruct (morePrecise m0 m1);  destruct m, m0, m1; try congruence;
          destruct (w0 ?= w1) eqn:?; try congruence;
            apply Ndec.Pcompare_Peqb in Heqc;
              apply N.compare_eq in compare_eq;
              rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct (morePrecise m m1); destruct (morePrecise m0 m1);
            destruct m, m0, m1; try congruence;
          destruct (w0 ?= w1) eqn:?; try congruence;
            apply Ndec.Pcompare_Peqb in Heqc;
              apply N.compare_eq in compare_eq;
              rewrite Pos.eqb_eq in *; subst; 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 (exprCompare e1_1 e2_1) eqn:?;
               destruct (exprCompare e2_1 e3_1) eqn:?;
               try congruence;
          try (erewrite IHe1_1; eauto; fail "");
          try erewrite exprCompare_eq_trans; eauto.
    - destruct (exprCompare e1_1 e2_1) eqn:?;
        destruct (exprCompare e2_1 e3_1) eqn:?;
        try congruence;
        try (erewrite IHe1_1; eauto; fail "");
        try erewrite exprCompare_eq_trans; eauto.
      destruct (exprCompare e1_2 e2_2) eqn:?;
        destruct (exprCompare e2_2 e3_2) eqn:?;
        try congruence;
        try (erewrite IHe1_2; eauto; fail "");
        try erewrite exprCompare_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. type_conv; subst. destruct m0, m1; try congruence;
        try destruct (morePrecise _ _) eqn:?; try congruence.
        destruct (w ?= w0) eqn:?; try congruence;
          apply Ndec.Pcompare_Peqb in Heqc;
          apply N.compare_eq in compare_eq;
          rewrite Pos.eqb_eq in *; subst; congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb; destruct (morePrecise m m1) eqn:?; congruence.
      + destruct (mTypeEq m m1); type_conv.
        * destruct (morePrecise m0 m1);  destruct m, m0, m1; try congruence;
            destruct (w0 ?= w1) eqn:?; try congruence;
            apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in compare_eq;
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct (morePrecise m m1); destruct (morePrecise m0 m1);
            destruct m, m0, m1; try congruence;
            destruct (w0 ?= w1) eqn:?; try congruence;
            apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in compare_eq;
            rewrite Pos.eqb_eq in *; subst; congruence.
Joachim Bard's avatar
Joachim Bard committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?.
      + type_conv; subst. rewrite mTypeEq_refl.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        * apply nat_compare_eq in Hn. subst.
          destruct (n0 ?= n1)%nat eqn:?; try discriminate.
          destruct (exprCompare e1_1 e2_1) eqn:?;
            destruct (exprCompare e2_1 e3_1) eqn:?;
            try congruence;
            try (erewrite IHe1_1; eauto; fail "").
          erewrite (exprCompare_eq_trans e1_1); eauto.
        * destruct (n0 ?= n1)%nat eqn:Hn1; try discriminate.
          apply nat_compare_eq in Hn1. subst.
          now rewrite Hn.
      + rewrite mTypeEq_compat_eq in Heqb; subst.
        rewrite Heqb0. type_conv.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        * apply nat_compare_eq in Hn. subst.
          destruct (n0 ?= n1)%nat eqn:?; try discriminate.
          destruct m0, m1; try discriminate.
          destruct (w ?= w0) eqn:?, (f ?= f0)%N eqn:?; try discriminate.
          apply Ndec.Pcompare_Peqb in Heqc0;
            apply N.compare_eq in Heqc1;
            rewrite Pos.eqb_eq in *; subst; congruence.
        * destruct (n0 ?= n1)%nat eqn:Hn1; try discriminate.
          apply nat_compare_eq in Hn1. subst.
          now rewrite Hn.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb. type_conv.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        * apply nat_compare_eq in Hn. subst.
          destruct (n0 ?= n1)%nat eqn:?; try discriminate.
          auto.
        * destruct (n0 ?= n1)%nat eqn:Hn1; try discriminate.
          apply nat_compare_eq in Hn1. subst.
          now rewrite Hn.
      + destruct (mTypeEq m m1); type_conv;
          destruct (n ?= n0)%nat eqn:Hn;
          destruct (n0 ?= n1)%nat eqn:Hn1; try discriminate;
          destruct m0, m1; try discriminate;
          destruct (w ?= w0) eqn:?, (f ?= f0)%N eqn:?; try discriminate;
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
Joachim Bard's avatar
Joachim Bard committed
717
        (*
Joachim Bard's avatar
Joachim Bard committed
718 719 720 721 722 723 724 725 726 727
    - destruct (exprCompare e1_1 e2_1) eqn:?;
        destruct (exprCompare e2_1 e3_1) eqn:?;
        try congruence;
        try (erewrite IHe1_1; eauto; fail "");
        try erewrite exprCompare_eq_trans; eauto.
      destruct (exprCompare e1_2 e2_2) eqn:?;
        destruct (exprCompare e2_2 e3_2) eqn:?;
        try congruence;
        try (erewrite IHe1_2; eauto; fail "");
        try erewrite exprCompare_eq_trans; eauto.
Joachim Bard's avatar
Joachim Bard committed
728
*)
Joachim Bard's avatar
Joachim Bard committed
729
  Qed.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
730

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
  Lemma exprCompare_eq_lt_is_lt e1:
    forall e2 e3,
      exprCompare e1 e2 = Eq -> exprCompare e2 e3 = Lt -> exprCompare 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;
        try destruct (morePrecise _ _) eqn:?; try congruence;
        destruct m, m1; try congruence; type_conv;
          destruct (w ?= w0) eqn:case1;
                   destruct (f ?= f0)%N eqn:case2;
                   try congruence;
            apply Ndec.Pcompare_Peqb in case1;
            apply N.compare_eq in case2;
            rewrite Pos.eqb_eq in *; subst; congruence.
      + type_conv; subst.
        destruct (mTypeEq m m1); type_conv; destruct m, m0, m1; try congruence;
          try (repeat destruct (morePrecise _ _)); try congruence;
          destruct (w ?= w0) eqn:case1;
                   destruct (f ?= f0)%N eqn:case2;
                   try congruence;
            apply Ndec.Pcompare_Peqb in case1;
            apply N.compare_eq in case2;
            rewrite Pos.eqb_eq in *; subst; 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 (exprCompare e1_1 e2_1) eqn:?;
                   destruct (exprCompare e2_1 e3_1) eqn:?;
                   try congruence;
          try (erewrite IHe1_1; eauto; fail "");
          try erewrite exprCompare_eq_trans; eauto.
    - destruct (exprCompare e1_1 e2_1) eqn:?;
        destruct (exprCompare e2_1 e3_1) eqn:?;
        try congruence;
        try (erewrite IHe1_1; eauto; fail "");
        try erewrite exprCompare_eq_trans; eauto.
      destruct (exprCompare e1_2 e2_2) eqn:?;
        destruct (exprCompare e2_2 e3_2) eqn:?;
        try congruence;
        try (erewrite IHe1_2; eauto; fail "");
        try erewrite exprCompare_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 m, m1; try (repeat destruct (morePrecise _ _)); try congruence.
          destruct (w ?= w0) eqn:case1;
                   destruct (f ?= f0)%N eqn:case2;
                   try congruence;
            apply Ndec.Pcompare_Peqb in case1;
            apply N.compare_eq in case2;
            rewrite Pos.eqb_eq in *; subst; cbn in *;
              repeat rewrite Pos.eqb_refl in *; simpl in *; try congruence.
          rewrite N.eqb_neq in *; congruence.
      + type_conv; subst.
        destruct (mTypeEq m m1); type_conv; destruct m, m0, m1; try congruence;
          try (repeat destruct (morePrecise _ _)); try congruence;
          destruct (w ?= w0) eqn:case1;
                   destruct (f ?= f0)%N eqn:case2;
                   try congruence;
            apply Ndec.Pcompare_Peqb in case1;
            apply N.compare_eq in case2;
            rewrite Pos.eqb_eq in *; subst; cbn in *;
              repeat rewrite Pos.eqb_refl in *; simpl in *; congruence.
Joachim Bard's avatar
Joachim Bard committed
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 844
    - destruct (mTypeEq m m0) eqn:?;
               destruct (mTypeEq m0 m1) eqn:?.
      + type_conv; subst. rewrite mTypeEq_refl.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        * apply nat_compare_eq in Hn. subst.
          destruct (n0 ?= n1)%nat eqn:?; try congruence.
          destruct (exprCompare e1_1 e2_1) eqn:?;
            destruct (exprCompare e2_1 e3_1) eqn:?;
            try congruence;
            try (erewrite IHe1_1; eauto; fail "").
          erewrite (exprCompare_eq_trans e1_1); eauto.
      + rewrite mTypeEq_compat_eq in Heqb; subst.
        rewrite Heqb0. type_conv.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        apply nat_compare_eq in Hn; subst.
          destruct (n0 ?= n1)%nat eqn:?; congruence.
      + rewrite mTypeEq_compat_eq in Heqb0; subst.
        rewrite Heqb. type_conv.
        destruct (n ?= n0)%nat eqn:Hn; try discriminate.
        destruct m, m1; try discriminate.
        destruct (w ?= w0) eqn:?, (f ?= f0)%N eqn:?; try discriminate.
        apply Ndec.Pcompare_Peqb in Heqc;
          apply N.compare_eq in Heqc0;
          rewrite Pos.eqb_eq in *; subst; congruence.
      + destruct (mTypeEq m m1); type_conv;
          destruct (n ?= n0)%nat eqn:Hn;
          (* destruct (n0 ?= n1)%nat eqn:Hn1; try discriminate; *)
          destruct m, m0; try discriminate;
          destruct (w ?= w0) eqn:?, (f ?= f0)%N eqn:?; try discriminate;
          apply Ndec.Pcompare_Peqb in Heqc;
            apply N.compare_eq in Heqc0;
            rewrite Pos.eqb_eq in *; subst; congruence.
Joachim Bard's avatar
Joachim Bard committed
845
        (*
Joachim Bard's avatar
Joachim Bard committed
846 847 848 849 850 851 852 853 854 855
    - destruct (exprCompare e1_1 e2_1) eqn:?;
        destruct (exprCompare e2_1 e3_1) eqn:?;
        try congruence;
        try (erewrite IHe1_1; eauto; fail "");
        try erewrite exprCompare_eq_trans; eauto.
      destruct (exprCompare e1_2 e2_2) eqn:?;
        destruct (exprCompare e2_2 e3_2) eqn:?;
        try congruence;
        try (erewrite IHe1_2; eauto; fail "");
        try erewrite exprCompare_eq_trans; eauto.
Joachim Bard's avatar
Joachim Bard committed
856
*)
Joachim Bard's avatar
Joachim Bard committed
857
  Qed.
Nikita Zyuzin's avatar
Nikita Zyuzin committed
858

859 860
  Definition eq e1 e2 :=
    exprCompare e1 e2 = Eq.
861

862 863
  Definition lt (e1:expr V) (e2: expr V):=
    exprCompare e1 e2 = Lt.
864

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
  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 (exprCompare x1 x1) eqn:?; try congruence.
      + destruct (exprCompare x1 x1) eqn:?; destruct (exprCompare x2 x2) eqn:?; try congruence.
      + rewrite mTypeEq_refl in lt_x.
        apply IHx; auto.
883
      + rewrite mTypeEq_refl in *.
Joachim Bard's avatar
Joachim Bard committed
884
        rewrite Nat.compare_refl in *.
885 886
        destruct (exprCompare x1 x1) eqn:?;
                 destruct (exprCompare x2 x2) eqn:?; congruence.
Joachim Bard's avatar
Joachim Bard committed
887
        (*
888 889 890
      + destruct (exprCompare x1 x1) eqn:?;
                 destruct (exprCompare x2 x2) eqn:?;
                 destruct (exprCompare x3 x3) eqn:?; congruence.
Joachim Bard's avatar
Joachim Bard committed
891
*)
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    - 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:?;
                     destruct m0, m1;
                     try congruence;
            try pose proof (morePrecise_antisym _ _ Heqb1 Heqb2);
            type_conv; try congruence; unfold morePrecise in *; simpl in *;
              try congruence;
              rewrite Pos.compare_antisym in lt_e2_e3;
              rewrite N.compare_antisym in lt_e2_e3;
              destruct (w0 ?= w) eqn:case1;
                destruct (f0 ?= f)%N eqn:case2;
                cbn in *;
                try congruence. }
          { destruct (morePrecise m m0) eqn:?;
                     destruct (morePrecise m0 m1) eqn:?;
                     try congruence.
            - erewrite morePrecise_trans; eauto;
                type_conv; subst;
                  destruct m, m0, m1; try congruence.
              destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                try (apply Ndec.Pcompare_Peqb in case_w0);
                try (apply Ndec.Pcompare_Peqb in case_w1);
                try rewrite Pos.eqb_eq in *;
                subst;
                try congruence;
                try rewrite case_w0;
                try rewrite case_w1; try auto;
                try rewrite Pos.compare_refl;
                [ rewrite N.compare_lt_iff in *;
                  eapply N.lt_trans; eauto  | ].
              assert (w ?= w1 = Lt).
              { rewrite Pos.compare_lt_iff in *;
                  eapply Pos.lt_trans; eauto. }
              rewrite H; auto.
            - type_conv; subst; destruct m, m0, m1; try congruence.
              destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                try (apply Ndec.Pcompare_Peqb in case_w0);
                try (apply Ndec.Pcompare_Peqb in case_w1);
                try rewrite Pos.eqb_eq in *;
                subst;
                try congruence;
                try rewrite case_w0;
                try rewrite case_w1; try auto;
                try rewrite Pos.compare_refl;
                [ rewrite N.compare_lt_iff in *;
                  eapply N.lt_trans; eauto  | ].
              assert (w ?= w1 = Lt).
              { rewrite Pos.compare_lt_iff in *;
                  eapply Pos.lt_trans; eauto. }
              rewrite H; auto.
            - type_conv; subst; destruct m, m0, m1; try congruence.
              destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                try (apply Ndec.Pcompare_Peqb in case_w0);
                try (apply Ndec.Pcompare_Peqb in case_w1);
                try rewrite Pos.eqb_eq in *;
                subst;
                try congruence;
                try rewrite case_w0;
                try rewrite case_w1; try auto;
                try rewrite Pos.compare_refl;
                [ rewrite N.compare_lt_iff in *;
                  eapply N.lt_trans; eauto  | ].
              assert (w ?= w1 = Lt).
              { rewrite Pos.compare_lt_iff in *;
                  eapply Pos.lt_trans; eauto. }
              rewrite H; auto.
            - type_conv; subst; destruct m, m0, m1; try congruence.
              destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                try (apply Ndec.Pcompare_Peqb in case_w0);
                try (apply Ndec.Pcompare_Peqb in case_w1);
                try rewrite Pos.eqb_eq in *;
                subst;
                try congruence;
                try rewrite case_w0;
                try rewrite case_w1; try auto;
                try rewrite Pos.compare_refl;
                [ rewrite N.compare_lt_iff in *;
                  eapply N.lt_trans; eauto  | ].
              assert (w ?= w1 = Lt).
              { rewrite Pos.compare_lt_iff in *;
                  eapply Pos.lt_trans; eauto. }
              rewrite H; auto. }
      + destruct (unopEq u u0) eqn:?;
                 destruct (unopEq u0 u1) eqn:?;
                 try rewrite unopEq_compat_eq in *; subst;
          [ destruct (exprCompare 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 (exprCompare e1_1 y1) eqn:?; try congruence;
          destruct (exprCompare y1 z1) eqn:?; try congruence;
          try (erewrite exprCompare_eq_trans; eauto; fail);
          try (erewrite exprCompare_eq_lt_is_lt; eauto; fail);
          try (erewrite exprCompare_lt_eq_is_lt; eauto; fail);
          try (erewrite IHe1_1; eauto).
      + destruct (exprCompare e1_1 y1) eqn:?; try congruence;
          destruct (exprCompare y1 z1) eqn:?; try congruence;
          try (erewrite exprCompare_eq_lt_is_lt; eauto; fail);
          try (erewrite exprCompare_lt_eq_is_lt; eauto; fail);
          try (erewrite IHe1_1; eauto; fail).
        apply (exprCompare_eq_trans _ _ _ Heqc) in Heqc0;
          rewrite Heqc0.
        destruct (exprCompare e1_2 y2) eqn:?; try congruence;
          destruct (exprCompare y2 z2) eqn:?; try congruence;
          try (erewrite exprCompare_eq_trans; eauto; fail);
          try (erewrite exprCompare_eq_lt_is_lt; eauto; fail);
          try (erewrite exprCompare_lt_eq_is_lt; eauto; fail);
          try (erewrite IHe1_2; 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:prec1;
                     destruct (morePrecise m0 m1) eqn:prec2;
                     destruct m1, m0;
                     try rewrite mTypeEq_refl in *; try congruence;
                       try pose proof (morePrecise_antisym _ _ prec1 prec2);
                       type_conv; try congruence;
                         simpl in *; try congruence;
            rewrite Pos.compare_antisym in lt_e2_e3;
            rewrite N.compare_antisym in lt_e2_e3;
            destruct (w ?= w0) eqn:?; destruct (f ?= f0)%N eqn:?;
                     cbn in *; try congruence. }
          { type_conv; subst.
            destruct (morePrecise m1 m0) eqn:prec1;
                     destruct (morePrecise m0 m1) eqn:prec2;
                     destruct m, m0, m1; simpl in *; try congruence; try auto;
                       try rewrite prec1 in *; try rewrite prec2 in *; try congruence;
                         destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                try (apply Ndec.Pcompare_Peqb in case_w0);
                try (apply Ndec.Pcompare_Peqb in case_w1);
                try rewrite Pos.eqb_eq in *;
                try rewrite N.eqb_eq in *;
                subst;
                try congruence;
                try rewrite case_w0;
                try rewrite case_w1; try auto;
                try rewrite Pos.compare_refl;
                try (rewrite N.compare_lt_iff in *; eapply N.lt_trans; eauto);
                assert (w ?= w1 = Lt) as G
                    by (rewrite Pos.compare_lt_iff in *;
                        eapply Pos.lt_trans; eauto);
                rewrite G; auto. }
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
      + destruct (mTypeEq m m0) eqn:?;
                 destruct (mTypeEq m0 m1) eqn:?;
                 [type_conv; subst; rewrite mTypeEq_refl | | | ].
        { destruct (n ?= n0)%nat eqn:c1; destruct (n0 ?= n1)%nat eqn:c2; try congruence.
          - apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
            rewrite Nat.compare_refl.
            destruct (exprCompare e1_1 y1) eqn:?;
                     destruct (exprCompare y1 z1) eqn:?; try congruence.
            + erewrite exprCompare_eq_trans; eauto.
            + erewrite exprCompare_eq_lt_is_lt; eauto.
            + erewrite exprCompare_lt_eq_is_lt; eauto.
            + erewrite IHe1_1; eauto.
          - apply Nat.compare_eq in c1. subst.
            now rewrite c2.
          - apply Nat.compare_eq in c2. subst.
            now rewrite c1.
          - apply nat_compare_lt in c1. apply nat_compare_lt in c2.
            assert (c3: (n ?= n1)%nat = Lt) by (apply nat_compare_lt; omega).
            now rewrite c3. }
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
        { destruct (n ?= n0)%nat eqn:c1; destruct (n0 ?= n1)%nat eqn:c2; try congruence.
          - apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
            rewrite Nat.compare_refl.
            apply mTypeEq_compat_eq in Heqb. subst.
            rewrite Heqb0. auto.
          - apply Nat.compare_eq in c1. subst.
            rewrite c2. now destruct (mTypeEq m m1).
          - apply Nat.compare_eq in c2. subst.
            rewrite c1. now destruct (mTypeEq m m1).
          - apply nat_compare_lt in c1. apply nat_compare_lt in c2.
            assert (c3: (n ?= n1)%nat = Lt) by (apply nat_compare_lt; omega).
            rewrite c3. now destruct (mTypeEq m m1). }
        { destruct (n ?= n0)%nat eqn:c1; destruct (n0 ?= n1)%nat eqn:c2; try congruence.
          - apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
            rewrite Nat.compare_refl.
            apply mTypeEq_compat_eq in Heqb0. subst.
            rewrite Heqb. auto.
          - apply Nat.compare_eq in c1. subst.
            rewrite c2. now destruct (mTypeEq m m1).
          - apply Nat.compare_eq in c2. subst.
            rewrite c1. now destruct (mTypeEq m m1).
          - apply nat_compare_lt in c1. apply nat_compare_lt in c2.
            assert (c3: (n ?= n1)%nat = Lt) by (apply nat_compare_lt; omega).
            rewrite c3. now destruct (mTypeEq m m1). }
        { destruct (n ?= n0)%nat eqn:c1; destruct (n0 ?= n1)%nat eqn:c2; try congruence.
          - apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
            rewrite Nat.compare_refl.
            destruct (mTypeEq m m1) eqn:?.
            + type_conv.
              destruct (morePrecise m1 m0) eqn:prec1;
                destruct (morePrecise m0 m1) eqn:prec2;
                destruct m0, m1; simpl in *; try congruence; try auto;
                  destruct (w ?= w0) eqn:case_w0; rewrite Pos.compare_antisym in lt_e1_e2;
                    rewrite case_w0 in *; cbn in *; try congruence;
                      rewrite N.compare_antisym, lt_e2_e3 in lt_e1_e2; cbn in *; congruence.
            + type_conv; subst.
              destruct (morePrecise m1 m0) eqn:prec1;
                destruct (morePrecise m0 m1) eqn:prec2;
                destruct m, m0, m1; simpl in *; try congruence; try auto;
                  destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                    try (apply Ndec.Pcompare_Peqb in case_w0);
                    try (apply Ndec.Pcompare_Peqb in case_w1);
                    try rewrite Pos.eqb_eq in *;
                    try rewrite N.eqb_eq in *;
                    subst;
                    try congruence;
                    try rewrite case_w0;
                    try rewrite case_w1; try auto;
                      try rewrite Pos.compare_refl;
                      try (rewrite N.compare_lt_iff in *; eapply N.lt_trans; eauto);
                      assert (w ?= w1 = Lt) as G
                          by (rewrite Pos.compare_lt_iff in *;
                              eapply Pos.lt_trans; eauto);
                      rewrite G; auto.
          - apply Nat.compare_eq in c1. subst.
            rewrite c2. now destruct (mTypeEq m m1).
          - apply Nat.compare_eq in c2. subst.
            rewrite c1. now destruct (mTypeEq m m1).
          - apply nat_compare_lt in c1. apply nat_compare_lt in c2.
            assert (c3: (n ?= n1)%nat = Lt) by (apply nat_compare_lt; omega).
            rewrite c3. now destruct (mTypeEq m m1). }
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

        (*
         destruct (morePrecise m m0); destruct m, m0; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
        * 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:prec1;
                     destruct (morePrecise m0 m1) eqn:prec2;
                     destruct m1, m0;
                     try rewrite mTypeEq_refl in *; try congruence;
                       try pose proof (morePrecise_antisym _ _ prec1 prec2);
                       type_conv; try congruence;
                         simpl in *; try congruence;
            rewrite Pos.compare_antisym in lt_e2_e3;
            rewrite N.compare_antisym in lt_e2_e3;
            destruct (w ?= w0) eqn:?; destruct (f ?= f0)%N eqn:?;
                     cbn in *; try congruence. }
          { type_conv; subst.
            destruct (morePrecise m1 m0) eqn:prec1;
                     destruct (morePrecise m0 m1) eqn:prec2;
                     destruct m, m0, m1; simpl in *; try congruence; try auto;
                       try rewrite prec1 in *; try rewrite prec2 in *; try congruence;
                         destruct (w ?= w0) eqn:case_w0; destruct (w0 ?= w1) eqn:case_w1;
                try (apply Ndec.Pcompare_Peqb in case_w0);
                try (apply Ndec.Pcompare_Peqb in case_w1);
                try rewrite Pos.eqb_eq in *;
                try rewrite N.eqb_eq in *;
                subst;
                try congruence;
                try rewrite case_w0;
                try rewrite case_w1; try auto;
                try rewrite Pos.compare_refl;
                try (rewrite N.compare_lt_iff in *; eapply N.lt_trans; eauto);
                assert (w ?= w1 = Lt) as G
                    by (rewrite Pos.compare_lt_iff in *;
                        eapply Pos.lt_trans; eauto);
                rewrite G; auto. }
*)
Joachim Bard's avatar
Joachim Bard committed
1194
        (* case for Cond
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
      + destruct (exprCompare e1_1 y1) eqn:?; try congruence;
          destruct (exprCompare y1 z1) eqn:?; try congruence;
          try (erewrite exprCompare_eq_lt_is_lt; eauto; fail);
          try (erewrite exprCompare_lt_eq_is_lt; eauto; fail);
          try (erewrite IHe1_1; eauto; fail).
        apply (exprCompare_eq_trans _ _ _ Heqc) in Heqc0;
          rewrite Heqc0.
        destruct (exprCompare e1_2 y2) eqn:?; try congruence;
          destruct (exprCompare y2 z2) eqn:?; try congruence;
          try (erewrite exprCompare_eq_trans; eauto; fail);
          try (erewrite exprCompare_eq_lt_is_lt; eauto; fail);
          try (erewrite exprCompare_lt_eq_is_lt; eauto; fail);
          try (erewrite IHe1_2; eauto).
Joachim Bard's avatar
Joachim Bard committed
1208
*)
1209
  Qed.
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  Instance 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); destruct m1, m2; try congruence;
        destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
          apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
        rewrite Pos.eqb_eq in *; subst; rewrite mTypeEq_refl in *; congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
          apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
        rewrite Pos.eqb_eq in *; subst; rewrite mTypeEq_refl in *; congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
          apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
        rewrite Pos.eqb_eq in *; subst; rewrite mTypeEq_refl in *; 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 (exprCompare e1_1 e2_1) eqn:?;
                 destruct (exprCompare e3_1 e4_1) eqn:?;
                 try congruence;
        destruct (exprCompare e1_1 e3_1) eqn:?;
                 destruct (exprCompare 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 (split; auto; fail);
        destruct (exprCompare e1_1 e2_1) eqn:?;
                 destruct (exprCompare e3_1 e4_1) eqn:?;
                 try congruence;
        destruct (exprCompare e1_1 e3_1) eqn:?;
                 destruct (exprCompare 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 (split; auto; fail);
        destruct (exprCompare e1_2 e2_2) eqn:?;
                 destruct (exprCompare e3_2 e4_2) eqn:?;
                 try congruence;
        destruct (exprCompare e1_2 e3_2) eqn:?;
                 destruct (exprCompare e2_2 e4_2) eqn:?;
                 try (split; congruence);
        try (split; try congruence; intros);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite IHe1_2 in *; congruence);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite <- IHe1_2 in *; congruence);
        try congruence;
        erewrite exprCompare_eq_trans; eauto;
        erewrite exprCompare_eq_trans; eauto;
        rewrite exprCompare_antisym;
        now (try rewrite e3_eq_e4; try rewrite e1_eq_e2).
    -  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); destruct m1, m2; try congruence;
        destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
          apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
        rewrite Pos.eqb_eq in *; subst; rewrite mTypeEq_refl in *; congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
          apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
        rewrite Pos.eqb_eq in *; subst; rewrite mTypeEq_refl in *; congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
          apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
        rewrite Pos.eqb_eq in *; subst; rewrite mTypeEq_refl in *; congruence.
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
    -  destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq m1 m2) eqn:?;
               [type_conv | | |].
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         destruct (mTypeEq m0 m2); try reflexivity; try congruence.
         destruct (exprCompare e1_1 e2_1) eqn:?;
                  destruct (exprCompare e3_1 e4_1) eqn:?;
                  try congruence.
         destruct (n0 ?= n2)%nat; try tauto.
         destruct (exprCompare e1_1 e3_1) eqn:?;
                  destruct (exprCompare e2_1 e4_1) eqn:?;
                  try (split; congruence).
         * now specialize (IHe1_2 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *.
         * split; try congruence. intros H.
           specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite <- Heqc2. tauto.
         * split; try congruence. intros H.
           specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite <- Heqc2. tauto.
         * split; try congruence. intros H.
           specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite <- Heqc1. tauto.
         * split; try congruence. intros H.
           specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite <- Heqc1. tauto.
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         type_conv.
         destruct (morePrecise m1 m2); destruct m1, m2; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         type_conv.
         destruct (morePrecise m m0); destruct m, m0; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         type_conv.
         destruct (morePrecise m m0); destruct m, m0; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
Joachim Bard's avatar
Joachim Bard committed
1345
         (*
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    - try (split; auto; fail);
        destruct (exprCompare e1_1 e2_1) eqn:?;
                 destruct (exprCompare e3_1 e4_1) eqn:?;
                 try congruence;
        destruct (exprCompare e1_1 e3_1) eqn:?;
                 destruct (exprCompare 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 (split; auto; fail);
        destruct (exprCompare e1_2 e2_2) eqn:?;
                 destruct (exprCompare e3_2 e4_2) eqn:?;
                 try congruence;
        destruct (exprCompare e1_2 e3_2) eqn:?;
                 destruct (exprCompare e2_2 e4_2) eqn:?;
                 try (split; congruence);
        try (split; try congruence; intros);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite IHe1_2 in *; congruence);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite <- IHe1_2 in *; congruence);
        try congruence;
        erewrite exprCompare_eq_trans; eauto;
        erewrite exprCompare_eq_trans; eauto;
        rewrite exprCompare_antisym;
        now (try rewrite e3_eq_e4; try rewrite e1_eq_e2).
Joachim Bard's avatar
Joachim Bard committed
1372
*)
1373
  Qed.
1374

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
  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); destruct m1, m2; try congruence;
        destruct (w ?= w0) eqn:case1; destruct (f ?= f0)%N eqn:case2;
          try congruence;
          apply Ndec.Pcompare_Peqb in case1;
          apply N.compare_eq in case2;
          rewrite Pos.eqb_eq in *; subst; cbn in *;
            repeat rewrite N.eqb_refl, Pos.eqb_refl in *; simpl in *; try congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:case1; destruct (f ?= f0)%N eqn:case2;
          try congruence;
          apply Ndec.Pcompare_Peqb in case1;
          apply N.compare_eq in case2;
          rewrite Pos.eqb_eq in *; subst; cbn in *;
            repeat rewrite Pos.eqb_refl, N.eqb_refl in *; simpl in *; try congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:case1; destruct (f ?= f0)%N eqn:case2;
          try congruence;
          apply Ndec.Pcompare_Peqb in case1;
          apply N.compare_eq in case2;
          rewrite Pos.eqb_eq in *; subst; cbn in *;
            repeat rewrite N.eqb_refl, Pos.eqb_refl in *; simpl in *; try 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 (exprCompare e1_1 e2_1) eqn:?;
                 destruct (exprCompare e3_1 e4_1) eqn:?;
                 try congruence;
        destruct (exprCompare e1_1 e3_1) eqn:?;
                 destruct (exprCompare 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).
    - pose proof eq_compat as eq_comp. unfold Proper, eq in eq_comp.
      destruct (exprCompare e1_1 e2_1) eqn:?;
               destruct (exprCompare e3_1 e4_1) eqn:?;
               try congruence;
        destruct (exprCompare e1_1 e3_1) eqn:?;
                 destruct (exprCompare 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 (exprCompare e1_2 e2_2) eqn:?;
               destruct (exprCompare e3_2 e4_2) eqn:?;
               try congruence;
        destruct (exprCompare e1_2 e3_2) eqn:?;
                 destruct (exprCompare e2_2 e4_2) eqn:?;
                 try (split; congruence);
        try (specialize (IHe1_3 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *; rewrite IHe1_3 in *; split; auto; fail);
        try (split; try congruence; intros);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite IHe1_2 in *; congruence);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite <- IHe1_2 in *; congruence);
        try (rewrite (eq_comp _ _ Heqc3 _ _ Heqc4) in *; congruence);
        try (rewrite <- (eq_comp _ _ Heqc3 _ _ Heqc4) in *; congruence);
        try congruence.
      + apply (exprCompare_lt_eq_is_lt _ _ _ H) in e3_eq_e4;
        rewrite exprCompare_eq_sym in e1_eq_e2;
        now apply (exprCompare_eq_lt_is_lt _ _ _ e1_eq_e2).
      + rewrite exprCompare_eq_sym in e3_eq_e4;
        apply (exprCompare_lt_eq_is_lt _ _ _ H) in e3_eq_e4;
        now apply (exprCompare_eq_lt_is_lt _ _ _ e1_eq_e2).
    -  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); destruct m1, m2; try congruence;
        destruct (w ?= w0) eqn:case1; destruct (f ?= f0)%N eqn:case2;
          try congruence;
          apply Ndec.Pcompare_Peqb in case1;
          apply N.compare_eq in case2;
          rewrite Pos.eqb_eq in *; subst; cbn in *;
            repeat rewrite N.eqb_refl, Pos.eqb_refl in *; simpl in *; try congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:case1; destruct (f ?= f0)%N eqn:case2;
          try congruence;
          apply Ndec.Pcompare_Peqb in case1;
          apply N.compare_eq in case2;
          rewrite Pos.eqb_eq in *; subst; cbn in *;
            repeat rewrite Pos.eqb_refl, N.eqb_refl in *; simpl in *; try congruence.
      + destruct (morePrecise m m0); destruct m, m0; try congruence;
        destruct (w ?= w0) eqn:case1; destruct (f ?= f0)%N eqn:case2;
          try congruence;
          apply Ndec.Pcompare_Peqb in case1;
          apply N.compare_eq in case2;
          rewrite Pos.eqb_eq in *; subst; cbn in *;
            repeat rewrite N.eqb_refl, Pos.eqb_refl in *; simpl in *; try congruence.
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
    - pose proof eq_compat as eq_comp. unfold Proper, eq in eq_comp.
       destruct (mTypeEq m m0) eqn:?; destruct (mTypeEq m1 m2) eqn:?;
               [type_conv | | |].
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         destruct (mTypeEq m0 m2); try reflexivity; try congruence.
         destruct (exprCompare e1_1 e2_1) eqn:?;
                  destruct (exprCompare e3_1 e4_1) eqn:?;
                  try congruence.
         destruct (n0 ?= n2)%nat; try tauto.
         destruct (exprCompare e1_1 e3_1) eqn:?;
                  destruct (exprCompare e2_1 e4_1) eqn:?;
                  try (split; congruence).
         * now specialize (IHe1_2 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *.
         * specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite <- IHe1_1 in *. congruence.
         * pose proof (exprCompare_eq_trans _ _ _ Heqc1 Heqc0).
           apply exprCompare_eq_sym in Heqc.
           pose proof (exprCompare_eq_trans _ _ _ Heqc H).
           congruence.
         * specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite IHe1_1 in *. congruence.
         * specialize (IHe1_1 _ Heqc _ _ Heqc0); simpl in *.
           rewrite IHe1_1 in *. congruence.
         * pose proof (exprCompare_eq_trans _ _ _ Heqc Heqc2).
           apply exprCompare_eq_sym in Heqc0.
           pose proof (exprCompare_eq_trans _ _ _ H Heqc0).
           congruence.
         * pose proof (exprCompare_eq_lt_is_lt _ _ _ Heqc Heqc2).
           apply exprCompare_eq_sym in Heqc0.
           pose proof (exprCompare_lt_eq_is_lt _ _ _ H Heqc0).
           congruence.
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         type_conv.
         destruct (morePrecise m1 m2); destruct m1, m2; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
       + destruct (n ?= n0)%nat; try congruence.
         type_conv.
         destruct (morePrecise m m0); destruct m, m0; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
       + destruct (n ?= n0)%nat eqn:c1; destruct (n1 ?= n2)%nat eqn:c2; try congruence.
         apply Nat.compare_eq in c1. apply Nat.compare_eq in c2. subst.
         type_conv.
         destruct (morePrecise m m0); destruct m, m0; try congruence;
           destruct (w ?= w0) eqn:c1; destruct (f ?= f0)%N eqn:c2; try congruence;
             apply Ndec.Pcompare_Peqb in c1; apply N.compare_eq in c2;
               rewrite Pos.eqb_eq in *; subst; congruence.
Joachim Bard's avatar
Joachim Bard committed
1541
         (*
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    - pose proof eq_compat as eq_comp. unfold Proper, eq in eq_comp.
      destruct (exprCompare e1_1 e2_1) eqn:?;
               destruct (exprCompare e3_1 e4_1) eqn:?;
               try congruence;
        destruct (exprCompare e1_1 e3_1) eqn:?;
                 destruct (exprCompare 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 (exprCompare e1_2 e2_2) eqn:?;
               destruct (exprCompare e3_2 e4_2) eqn:?;
               try congruence;
        destruct (exprCompare e1_2 e3_2) eqn:?;
                 destruct (exprCompare e2_2 e4_2) eqn:?;
                 try (split; congruence);
        try (specialize (IHe1_3 _ e1_eq_e2 _ _ e3_eq_e4); simpl in *; rewrite IHe1_3 in *; split; auto; fail);
        try (split; try congruence; intros);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite IHe1_2 in *; congruence);
        try (specialize (IHe1_2 _ Heqc3 _ _ Heqc4); simpl in *; rewrite <- IHe1_2 in *; congruence);
        try (rewrite (eq_comp _ _ Heqc3 _ _ Heqc4) in *; congruence);
        try (rewrite <- (eq_comp _ _ Heqc3 _ _ Heqc4) in *; congruence);
        try congruence.
      + apply (exprCompare_lt_eq_is_lt _ _ _ H) in e3_eq_e4;
        rewrite exprCompare_eq_sym in e1_eq_e2;
        now apply (exprCompare_eq_lt_is_lt _ _ _ e1_eq_e2).
      + rewrite exprCompare_eq_sym in e3_eq_e4;
        apply (exprCompare_lt_eq_is_lt _ _ _ H) in e3_eq_e4;
        now apply (exprCompare_eq_lt_is_lt _ _ _ e1_eq_e2).
Joachim Bard's avatar
Joachim Bard committed
1574
*)
1575
  Qed.
1576

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
  Lemma compare_spec : forall x y, CompSpec eq lt x y (exprCompare x y).
  Proof.
    intros e1 e2.
    destruct (exprCompare e1 e2) eqn:?.
    - apply CompEq.
      unfold eq; auto.
    - apply CompLt. unfold lt; auto.
    - apply CompGt. unfold lt.
      rewrite exprCompare_antisym in Heqc.
      rewrite CompOpp_iff in Heqc.
      simpl in *; auto.
  Qed.
1589

1590 1591 1592 1593 1594 1595 1596 1597 1598
  Instance eq_equiv: Equivalence eq.
  Proof.
    split; unfold Reflexive, Symmetric, Transitive, eq.
    - apply exprCompare_refl.
    - intros. rewrite exprCompare_antisym in * |-.
      rewrite CompOpp_iff in * |- .
      auto.
    - apply exprCompare_eq_trans.
  Defined.
1599

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
  Definition eq_dec : forall x y, { eq x y } + { ~ eq x y }.
  Proof.
    intros. unfold eq. destruct (exprCompare x y) eqn:?; try auto.
    - right; hnf; intros; congruence.
    - right; hnf; intros; congruence.
  Defined.

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

  Definition eq_sym : forall x y, eq x y -> eq y x.
  Proof.
    unfold eq; intros.
    rewrite exprCompare_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 exprCompare_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.

  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:= exprCompare e1 e2.

  Close Scope positive_scope.

End ExprOrderedType.
1641

1642
(*
1643 1644 1645
(**
Analogous lemma for unary expressions.
**)
1646 1647
Lemma unary_unfolding (e:expr R) (eps:R) (E:env) (v:R):
  (eval_expr eps E (Unop Inv e) v <->
1648
   exists v1,
1649 1650
     eval_expr eps E e v1 /\
     eval_expr eps (updEnv 1 v1 E) (Unop Inv (Var R 1)) v).
1651 1652 1653 1654 1655 1656 1657 1658 1659
Proof.
  split.
  - intros eval_un.
    inversion eval_un; subst.
    exists v1.
    repeat split; try auto.
    constructor; try auto.
    constructor; auto.
  - intros exists_val.
1660 1661
    destruct exists_val as [v1 [eval_f1 eval_e_E]].
    inversion eval_e_E; subst.
1662 1663 1664
    inversion H1; subst.
    unfold updEnv in *; simpl in *.
    constructor; auto.
1665
    inversion H3; subst; auto.
1666
Qed. *)
1667

1668
(*   Using the parametric exprressions, define boolean exprressions for conditionals *)
1669
(* **)
1670 1671 1672
(* Inductive bexpr (V:Type) : Type := *)
(*   leq: expr V -> expr V -> bexpr V *)
(* | less: expr V -> expr V -> bexpr V. *)
1673

1674
(**
1675
  Define evaluation of boolean exprressions
1676
 **)
1677 1678 1679 1680
(* Inductive bval (E:env): (bexpr R) -> Prop -> Prop := *)
(*   leq_eval (f1:expr R) (f2:expr R) (v1:R) (v2:R): *)
(*     eval_expr E f1 v1 -> *)
(*     eval_expr E f2 v2 -> *)
1681
(*     bval E (leq f1 f2) (Rle v1 v2) *)
1682 1683 1684
(* |less_eval (f1:expr R) (f2:expr R) (v1:R) (v2:R): *)
(*     eval_expr E f1 v1 -> *)
(*     eval_expr E f2 v2 -> *)
1685 1686 1687