(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 2 of the License, or  *)
(*  (at your option) any later version.  This file is also distributed *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** This file collects a number of definitions and theorems that are
    used throughout the development.  It complements the Coq standard
    library. *)

Require Export String.
Require Export ZArith.
Require Export Znumtheory.
Require Export List.
Require Export Bool.

Global Set Asymmetric Patterns.

(** * Useful tactics *)

Ltac inv H := inversion H; clear H; subst.

Ltac predSpec pred predspec x y :=
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  generalize (predspec x y); case (pred x y); intro.

Ltac caseEq name :=
  generalize (eq_refl name); pattern name at -1 in |- *; case name.

Ltac destructEq name :=
  destruct name eqn:?.

Ltac decEq :=
  match goal with
  | [ |- _ = _ ] => f_equal
  | [ |- (?X ?A <> ?X ?B) ] =>
      cut (A <> B); [intro; congruence | try discriminate]
  end.

Ltac byContradiction :=
  cut False; [contradiction|idtac].

Ltac omegaContradiction :=
  cut False; [contradiction|omega].

Lemma modusponens: forall (P Q: Prop), P -> (P -> Q) -> Q.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
Proof. auto. Qed.

Ltac exploit x :=
    refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _ _) _)
 || refine (modusponens _ _ (x _ _ _) _)
 || refine (modusponens _ _ (x _ _) _)
 || refine (modusponens _ _ (x _) _).

(** * Definitions and theorems over the type [positive] *)

Definition peq: forall (x y: positive), {x = y} + {x <> y} := Pos.eq_dec.
Global Opaque peq.

Lemma peq_true:
  forall (A: Type) (x: positive) (a b: A), (if peq x x then a else b) = a.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (peq x x); intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  elim n; auto.
Qed.

Lemma peq_false:
  forall (A: Type) (x y: positive) (a b: A), x <> y -> (if peq x y then a else b) = b.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (peq x y); intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  elim H; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
Qed.

Definition Plt: positive -> positive -> Prop := Pos.lt.

Lemma Plt_ne:
  forall (x y: positive), Plt x y -> x <> y.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold Plt; intros. red; intro. subst y. eelim Pos.lt_irrefl; eauto.
Qed.
Hint Resolve Plt_ne: coqlib.

Lemma Plt_trans:
  forall (x y z: positive), Plt x y -> Plt y z -> Plt x z.
Proof (Pos.lt_trans).

Lemma Plt_succ:
  forall (x: positive), Plt x (Pos.succ x).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold Plt; intros. apply Pos.lt_succ_r. apply Pos.le_refl.
Qed.
Hint Resolve Plt_succ: coqlib.

Lemma Plt_trans_succ:
  forall (x y: positive), Plt x y -> Plt x (Pos.succ y).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. apply Plt_trans with y. assumption. apply Plt_succ.
Qed.
Hint Resolve Plt_succ: coqlib.

Lemma Plt_succ_inv:
  forall (x y: positive), Plt x (Pos.succ y) -> Plt x y \/ x = y.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold Plt; intros. rewrite Pos.lt_succ_r in H.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply Pos.le_lteq; auto.
Qed.

Definition plt (x y: positive) : {Plt x y} + {~ Plt x y}.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold Plt, Pos.lt; intros. destruct (Pos.compare x y).
  - right; congruence.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  - left; auto.
  - right; congruence.
Defined.
Global Opaque plt.

Definition Ple: positive -> positive -> Prop := Pos.le.

Lemma Ple_refl: forall (p: positive), Ple p p.
Proof (Pos.le_refl).

Lemma Ple_trans: forall (p q r: positive), Ple p q -> Ple q r -> Ple p r.
Proof (Pos.le_trans).

Lemma Plt_Ple: forall (p q: positive), Plt p q -> Ple p q.
Proof (Pos.lt_le_incl).

Lemma Ple_succ: forall (p: positive), Ple p (Pos.succ p).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. apply Plt_Ple. apply Plt_succ.
Qed.

Lemma Plt_Ple_trans:
  forall (p q r: positive), Plt p q -> Ple q r -> Plt p r.
Proof (Pos.lt_le_trans).

Lemma Plt_strict: forall p, ~ Plt p p.
Proof (Pos.lt_irrefl).

Hint Resolve Ple_refl Plt_Ple Ple_succ Plt_strict: coqlib.

Ltac xomega := unfold Plt, Ple in *; zify; omega.
Ltac xomegaContradiction := exfalso; xomega.

(** Peano recursion over positive numbers. *)

Section POSITIVE_ITERATION.

Lemma Plt_wf: well_founded Plt.
Proof.
(* Goal: @well_founded positive Plt *)
  apply well_founded_lt_compat with Pos.to_nat.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. apply nat_of_P_lt_Lt_compare_morphism. exact H.
Qed.

Variable A: Type.
Variable v1: A.
Variable f: positive -> A -> A.

Lemma Ppred_Plt:
  forall x, x <> xH -> Plt (Pos.pred x) x.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. elim (Pos.succ_pred_or x); intro. contradiction.
(* Goal: Plt y (Pos.succ y) *)
  set (y := Pos.pred x) in *. rewrite <- H0. apply Plt_succ.
Qed.

Let iter (x: positive) (P: forall y, Plt y x -> A) : A :=
  match peq x xH with
  | left EQ => v1
  | right NOTEQ => f (Pos.pred x) (P (Pos.pred x) (Ppred_Plt x NOTEQ))
  end.

Definition positive_rec : positive -> A :=
  Fix Plt_wf (fun _ => A) iter.

Lemma unroll_positive_rec:
  forall x,
  positive_rec x = iter x (fun y _ => positive_rec y).
Proof.
(* Goal: forall x : positive, @eq A (positive_rec x) (iter x (fun (y : positive) (_ : Plt y x) => positive_rec y)) *)
  unfold positive_rec. apply (Fix_eq Plt_wf (fun _ => A) iter).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. unfold iter. case (peq x 1); intro. auto. decEq. apply H.
Qed.

Lemma positive_rec_base:
  positive_rec 1%positive = v1.
Proof.
(* Goal: @eq A (iter (Pos.succ x) (fun (y : positive) (_ : Plt y (Pos.succ x)) => positive_rec y)) (f x (positive_rec x)) *)
  rewrite unroll_positive_rec. unfold iter. case (peq 1 1); intro.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto. elim n; auto.
Qed.

Lemma positive_rec_succ:
  forall x, positive_rec (Pos.succ x) = f x (positive_rec x).
Proof.
(* Goal: @eq A (iter (Pos.succ x) (fun (y : positive) (_ : Plt y (Pos.succ x)) => positive_rec y)) (f x (positive_rec x)) *)
  intro. rewrite unroll_positive_rec. unfold iter.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  case (peq (Pos.succ x) 1); intro.
(* Goal: @eq A v1 (f x (positive_rec x)) *)
(* Goal: @eq A (f (Pos.pred (Pos.succ x)) (positive_rec (Pos.pred (Pos.succ x)))) (f x (positive_rec x)) *)
  destruct x; simpl in e; discriminate.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite Pos.pred_succ. auto.
Qed.

Lemma positive_Peano_ind:
  forall (P: positive -> Prop),
  P xH ->
  (forall x, P x -> P (Pos.succ x)) ->
  forall x, P x.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: P x *)
  apply (well_founded_ind Plt_wf P).
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  case (peq x0 xH); intro.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  subst x0; auto.
  elim (Pos.succ_pred_or x0); intro. contradiction. rewrite <- H2.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply H0. apply H1. apply Ppred_Plt. auto.
Qed.

End POSITIVE_ITERATION.

(** * Definitions and theorems over the type [Z] *)

Definition zeq: forall (x y: Z), {x = y} + {x <> y} := Z.eq_dec.

Lemma zeq_true:
  forall (A: Type) (x: Z) (a b: A), (if zeq x x then a else b) = a.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zeq x x); intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  elim n; auto.
Qed.

Lemma zeq_false:
  forall (A: Type) (x y: Z) (a b: A), x <> y -> (if zeq x y then a else b) = b.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zeq x y); intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  elim H; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
Qed.

Open Scope Z_scope.

Definition zlt: forall (x y: Z), {x < y} + {x >= y} := Z_lt_dec.

Lemma zlt_true:
  forall (A: Type) (x y: Z) (a b: A),
  x < y -> (if zlt x y then a else b) = a.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zlt x y); intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: @eq Z b Z0 *)
(* Goal: @eq Z (Z.div x y) Z0 *)
  omegaContradiction.
Qed.

Lemma zlt_false:
  forall (A: Type) (x y: Z) (a b: A),
  x >= y -> (if zlt x y then a else b) = b.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zlt x y); intros.
(* Goal: @eq Z b Z0 *)
(* Goal: @eq Z (Z.div x y) Z0 *)
  omegaContradiction.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
Qed.

Definition zle: forall (x y: Z), {x <= y} + {x > y} := Z_le_gt_dec.

Lemma zle_true:
  forall (A: Type) (x y: Z) (a b: A),
  x <= y -> (if zle x y then a else b) = a.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zle x y); intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: @eq Z b Z0 *)
(* Goal: @eq Z (Z.div x y) Z0 *)
  omegaContradiction.
Qed.

Lemma zle_false:
  forall (A: Type) (x y: Z) (a b: A),
  x > y -> (if zle x y then a else b) = b.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zle x y); intros.
(* Goal: @eq Z b Z0 *)
(* Goal: @eq Z (Z.div x y) Z0 *)
  omegaContradiction.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
Qed.

(** Properties of powers of two. *)

Lemma two_power_nat_O : two_power_nat O = 1.
Proof. reflexivity. Qed.

Lemma two_power_nat_pos : forall n : nat, two_power_nat n > 0.
Proof.
(* Goal: forall n : nat, Z.gt (two_power_nat n) Z0 *)
  induction n. rewrite two_power_nat_O. omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite two_power_nat_S. omega.
Qed.

Lemma two_power_nat_two_p:
  forall x, two_power_nat x = two_p (Z.of_nat x).
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction x. auto.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite two_power_nat_S. rewrite Nat2Z.inj_succ. rewrite two_p_S. omega. omega.
Qed.

Lemma two_p_monotone:
  forall x y, 0 <= x <= y -> two_p x <= two_p y.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  replace (two_p x) with (two_p x * 1) by omega.
  replace y with (x + (y - x)) by omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite two_p_is_exp; try omega.
(* Goal: Z.le (Z.mul (Z.add hi (Zpos xH)) (Zpos xH)) (Z.mul (Z.add hi (Zpos xH)) b) *)
(* Goal: and (Z.le (Z.mul lo b) a) (Z.lt a (Z.mul (Z.add hi (Zpos xH)) b)) *)
(* Goal: and (Z.le lo (Z.div a b)) (Z.le (Z.div a b) hi) *)
  apply Zmult_le_compat_l.
  assert (two_p (y - x) > 0). apply two_p_gt_ZERO. omega. omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  assert (two_p x > 0). apply two_p_gt_ZERO. omega. omega.
Qed.

Lemma two_p_monotone_strict:
  forall x y, 0 <= x < y -> two_p x < two_p y.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. assert (two_p x <= two_p (y - 1)). apply two_p_monotone; omega.
  assert (two_p (y - 1) > 0). apply two_p_gt_ZERO. omega.
  replace y with (Z.succ (y - 1)) by omega. rewrite two_p_S. omega. omega.
Qed.

Lemma two_p_strict:
  forall x, x >= 0 -> x < two_p x.
Proof.
(* Goal: forall (x : Z) (_ : Z.ge x Z0), Z.lt x (two_p x) *)
  intros x0 GT. pattern x0. apply natlike_ind.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  simpl. omega.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. rewrite two_p_S; auto. generalize (two_p_gt_ZERO x H). omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
Qed.

Lemma two_p_strict_2:
  forall x, x >= 0 -> 2 * x - 1 < two_p x.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. assert (x = 0 \/ x - 1 >= 0) by omega. destruct H0.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  subst. vm_compute. auto.
  replace (two_p x) with (2 * two_p (x - 1)).
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  generalize (two_p_strict _ H0). omega.
  rewrite <- two_p_S. decEq. omega. omega.
Qed.

(** Properties of [Zmin] and [Zmax] *)

Lemma Zmin_spec:
  forall x y, Z.min x y = if zlt x y then x else y.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zlt x y); unfold Z.lt, Z.ge; intro z.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold Z.min. rewrite z. auto.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  unfold Z.min. caseEq (x ?= y); intro.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply Z.compare_eq. auto.
(* Goal: @eq A a b *)
(* Goal: @eq A b b *)
  contradiction.
(* Goal: @eq (option B) (@Some B (f a)) (@Some B (f a)) *)
(* Goal: @eq (option B) (@nth_error B (@map A B f l) n) (@option_map A B f (@nth_error A l n)) *)
  reflexivity.
Qed.

Lemma Zmax_spec:
  forall x y, Z.max x y = if zlt y x then x else y.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. case (zlt y x); unfold Z.lt, Z.ge; intro z.
  unfold Z.max. rewrite <- (Zcompare_antisym y x).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite z. simpl. auto.
  unfold Z.max. rewrite <- (Zcompare_antisym y x).
(* Goal: iff (@In A x (@cons A y l)) (or (@eq A y x) (@In A x l)) *)
  caseEq (y ?= x); intro; simpl.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  symmetry. apply Z.compare_eq. auto.
(* Goal: @eq A a b *)
(* Goal: @eq A b b *)
  contradiction. reflexivity.
Qed.

Lemma Zmax_bound_l:
  forall x y z, x <= y -> x <= Z.max y z.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. generalize (Z.le_max_l y z). omega.
Qed.
Lemma Zmax_bound_r:
  forall x y z, x <= z -> x <= Z.max y z.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. generalize (Z.le_max_r y z). omega.
Qed.

(** Properties of Euclidean division and modulus. *)

Lemma Zdiv_small:
  forall x y, 0 <= x < y -> x / y = 0.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. assert (y > 0). omega.
  assert (forall a b,
    0 <= a < y ->
    0 <= y * b + a < y ->
    b = 0).
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
  assert (b = 0 \/ b > 0 \/ (-b) > 0). omega.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  elim H3; intro.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  elim H4; intro.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  assert (y * b >= y * 1). apply Zmult_ge_compat_l. omega. omega.
(* Goal: @eq Z b Z0 *)
(* Goal: @eq Z (Z.div x y) Z0 *)
  omegaContradiction.
  assert (y * (-b) >= y * 1). apply Zmult_ge_compat_l. omega. omega.
  rewrite <- Zopp_mult_distr_r in H6. omegaContradiction.
(* Goal: @eq Z (Z.div x y) Z0 *)
  apply H1 with (x mod y).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply Z_mod_lt. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite <- Z_div_mod_eq. auto. auto.
Qed.

Lemma Zmod_small:
  forall x y, 0 <= x < y -> x mod y = x.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. assert (y > 0). omega.
(* Goal: @eq Z (Z.modulo x y) x *)
  generalize (Z_div_mod_eq x y H0).
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite (Zdiv_small x y H). omega.
Qed.

Lemma Zmod_unique:
  forall x y a b,
  x = a * y + b -> 0 <= b < y -> x mod y = b.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. subst x. rewrite Z.add_comm.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite Z_mod_plus. apply Zmod_small. auto. omega.
Qed.

Lemma Zdiv_unique:
  forall x y a b,
  x = a * y + b -> 0 <= b < y -> x / y = a.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. subst x. rewrite Z.add_comm.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite Z_div_plus. rewrite (Zdiv_small b y H0). omega. omega.
Qed.

Lemma Zdiv_Zdiv:
  forall a b c,
  b > 0 -> c > 0 -> (a / b) / c = a / (b * c).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  generalize (Z_div_mod_eq a b H). generalize (Z_mod_lt a b H). intros.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  generalize (Z_div_mod_eq (a/b) c H0). generalize (Z_mod_lt (a/b) c H0). intros.
(* Goal: @eq Z (Z.div (Z.div a b) c) (Z.div a (Z.mul b c)) *)
  set (q1 := a / b) in *. set (r1 := a mod b) in *.
(* Goal: @eq Z (Z.div q1 c) (Z.div a (Z.mul b c)) *)
  set (q2 := q1 / c) in *. set (r2 := q1 mod c) in *.
(* Goal: @eq Z q2 (Z.div a (Z.mul b c)) *)
  symmetry. apply Zdiv_unique with (r2 * b + r1).
(* Goal: @eq Z a (Z.add (Z.mul q2 (Z.mul b c)) (Z.add (Z.mul r2 b) r1)) *)
(* Goal: and (Z.le Z0 (Z.add (Z.mul r2 b) r1)) (Z.lt (Z.add (Z.mul r2 b) r1) (Z.mul b c)) *)
  rewrite H2. rewrite H4. ring.
(* Goal: and (Z.le Z0 (Z.mul x c)) (Z.le (Z.mul x c) (Z.sub (Z.mul y c) c)) *)
  split.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  assert (0 <= r2 * b). apply Z.mul_nonneg_nonneg. omega. omega. omega.
(* Goal: Z.lt (Z.add (Z.mul r2 b) r1) (Z.mul b c) *)
  assert ((r2 + 1) * b <= c * b).
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  apply Zmult_le_compat_r. omega. omega.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  replace ((r2 + 1) * b) with (r2 * b + b) in H5 by ring.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  replace (c * b) with (b * c) in H5 by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
Qed.

Lemma Zmult_le_compat_l_neg :
  forall n m p:Z, n >= m -> p <= 0 -> p * n <= p * m.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  assert ((-p) * n >= (-p) * m). apply Zmult_ge_compat_l. auto. omega.
  replace (p * n) with (- ((-p) * n)) by ring.
  replace (p * m) with (- ((-p) * m)) by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
Qed.

Lemma Zdiv_interval_1:
  forall lo hi a b,
  lo <= 0 -> hi > 0 -> b > 0 ->
  lo * b <= a < hi * b ->
  lo <= a/b < hi.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  generalize (Z_div_mod_eq a b H1). generalize (Z_mod_lt a b H1). intros.
(* Goal: and (Z.le lo (Z.div a b)) (Z.lt (Z.div a b) hi) *)
  set (q := a/b) in *. set (r := a mod b) in *.
(* Goal: and (Z.le Z0 (Z.mul x c)) (Z.le (Z.mul x c) (Z.sub (Z.mul y c) c)) *)
  split.
(* Goal: Z.le lo q *)
(* Goal: Z.lt q hi *)
  assert (lo < (q + 1)).
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  apply Zmult_lt_reg_r with b. omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  apply Z.le_lt_trans with a. omega.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  replace ((q + 1) * b) with (b * q + b) by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  apply Zmult_lt_reg_r with b. omega.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  replace (q * b) with (b * q) by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
Qed.

Lemma Zdiv_interval_2:
  forall lo hi a b,
  lo <= a <= hi -> lo <= 0 -> hi >= 0 -> b > 0 ->
  lo <= a/b <= hi.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
(* Goal: and (Z.le lo (Z.div a b)) (Z.le (Z.div a b) hi) *)
  assert (lo <= a / b < hi+1).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply Zdiv_interval_1. omega. omega. auto.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  assert (lo * b <= lo * 1). apply Zmult_le_compat_l_neg. omega. omega.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  replace (lo * 1) with lo in H3 by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  assert ((hi + 1) * 1 <= (hi + 1) * b). apply Zmult_le_compat_l. omega. omega.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  replace ((hi + 1) * 1) with (hi + 1) in H4 by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
Qed.

Lemma Zmod_recombine:
  forall x a b,
  a > 0 -> b > 0 ->
  x mod (a * b) = ((x/b) mod a) * b + (x mod b).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros.
  set (xb := x/b).
  apply Zmod_unique with (xb/a).
  generalize (Z_div_mod_eq x b H0); fold xb; intro EQ1.
  generalize (Z_div_mod_eq xb a H); intro EQ2.
  rewrite EQ2 in EQ1.
(* Goal: @eq Z (Z.mul x p) (Z.mul p x) *)
  eapply eq_trans. eexact EQ1. ring.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  generalize (Z_mod_lt x b H0). intro.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  generalize (Z_mod_lt xb a H). intro.
  assert (0 <= xb mod a * b <= a * b - b).
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
    split. apply Z.mul_nonneg_nonneg; omega.
    replace (a * b - b) with ((a - 1) * b) by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
    apply Zmult_le_compat; omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
Qed.

(** Properties of divisibility. *)

Lemma Zdivides_trans:
  forall x y z, (x | y) -> (y | z) -> (x | z).
Proof.
(* Goal: and (Z.le Z0 a) (Z.le a (Z.sub b c)) *)
  intros x y z [a A] [b B]; subst. exists (a*b); ring.
Qed.

Definition Zdivide_dec:
  forall (p q: Z), p > 0 -> { (p|q) } + { ~(p|q) }.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. destruct (zeq (Z.modulo q p) 0).
(* Goal: or (@eq A hd x) (@In A x a0) *)
(* Goal: False *)
(* Goal: @list_norepet A (@app A a0 (@cons A x b)) *)
(* Goal: forall (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
  left. exists (q / p).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  transitivity (p * (q / p) + (q mod p)). apply Z_div_mod_eq; auto.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  transitivity (p * (q / p)). omega. ring.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  right; red; intros. elim n. apply Z_div_exact_1; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  inv H0. rewrite Z_div_mult; auto. ring.
Defined.
Global Opaque Zdivide_dec.

Lemma Zdivide_interval:
  forall a b c,
  0 < c -> 0 <= a < b -> (c | a) -> (c | b) -> 0 <= a <= b - c.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. destruct H1 as [x EQ1]. destruct H2 as [y EQ2]. subst. destruct H0.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  split. omega. exploit Zmult_lt_reg_r; eauto. intros.
  replace (y * c - c) with ((y - 1) * c) by ring.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  apply Zmult_le_compat_r; omega.
Qed.

(** Conversion from [Z] to [nat]. *)

Definition nat_of_Z: Z -> nat := Z.to_nat.

Lemma nat_of_Z_of_nat:
  forall n, nat_of_Z (Z.of_nat n) = n.
Proof.
(* Goal: forall n : nat, @eq nat (nat_of_Z (Z.of_nat n)) n *)
  exact Nat2Z.id.
Qed.

Lemma nat_of_Z_max:
  forall z, Z.of_nat (nat_of_Z z) = Z.max z 0.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. unfold Z.max. destruct z; simpl; auto.
(* Goal: @eq Z (Z.of_nat (Pos.to_nat p)) (Zpos p) *)
  change (Z.of_nat (Z.to_nat (Zpos p)) = Zpos p).
(* Goal: @eq Z (Z.of_nat (Z.to_nat z)) z *)
  apply Z2Nat.id. compute; intuition congruence.
Qed.

Lemma nat_of_Z_eq:
  forall z, z >= 0 -> Z.of_nat (nat_of_Z z) = z.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold nat_of_Z; intros. apply Z2Nat.id. omega.
Qed.

Lemma nat_of_Z_neg:
  forall n, n <= 0 -> nat_of_Z n = O.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  destruct n; unfold Z.le; simpl; auto. congruence.
Qed.

Lemma nat_of_Z_plus:
  forall p q,
  p >= 0 -> q >= 0 ->
  nat_of_Z (p + q) = (nat_of_Z p + nat_of_Z q)%nat.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold nat_of_Z; intros. apply Z2Nat.inj_add; omega.
Qed.


(** Alignment: [align n amount] returns the smallest multiple of [amount]
  greater than or equal to [n]. *)

Definition align (n: Z) (amount: Z) :=
  ((n + amount - 1) / amount) * amount.

Lemma align_le: forall x y, y > 0 -> x <= align x y.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. unfold align.
  generalize (Z_div_mod_eq (x + y - 1) y H). intro.
  replace ((x + y - 1) / y * y)
     with ((x + y - 1) - (x + y - 1) mod y).
  generalize (Z_mod_lt (x + y - 1) y H). omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite Z.mul_comm. omega.
Qed.

Lemma align_divides: forall x y, y > 0 -> (y | align x y).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. unfold align. apply Z.divide_factor_r.
Qed.

(** * Definitions and theorems on the data types [option], [sum] and [list] *)

Set Implicit Arguments.

(** Comparing option types. *)

Definition option_eq (A: Type) (eqA: forall (x y: A), {x=y} + {x<>y}):
  forall (x y: option A), {x=y} + {x<>y}.
Proof. decide equality. Defined.
Global Opaque option_eq.

(** Lifting a relation to an option type. *)

Inductive option_rel (A B: Type) (R: A -> B -> Prop) : option A -> option B -> Prop :=
  | option_rel_none: option_rel R None None
  | option_rel_some: forall x y, R x y -> option_rel R (Some x) (Some y).

(** Mapping a function over an option type. *)

Definition option_map (A B: Type) (f: A -> B) (x: option A) : option B :=
  match x with
  | None => None
  | Some y => Some (f y)
  end.

(** Mapping a function over a sum type. *)

Definition sum_left_map (A B C: Type) (f: A -> B) (x: A + C) : B + C :=
  match x with
  | inl y => inl C (f y)
  | inr z => inr B z
  end.

(** Properties of [List.nth] (n-th element of a list). *)

Hint Resolve in_eq in_cons: coqlib.

Lemma nth_error_in:
  forall (A: Type) (n: nat) (l: list A) (x: A),
  List.nth_error l n = Some x -> In x l.
Proof.
(* Goal: iff (@In A x (@cons A y l)) (or (@eq A y x) (@In A x l)) *)
  induction n; simpl.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
   destruct l; intros.
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@nil A))) *)
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@cons A a l))) *)
    discriminate.
(* Goal: @In A x (@cons A a l) *)
(* Goal: forall (l : list A) (x : A) (_ : @eq (option A) match l with | nil => @None A | cons a l0 => @nth_error A l0 n end (@Some A x)), @In A x l *)
    injection H; intro; subst a. apply in_eq.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
   destruct l; intros.
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@nil A))) *)
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@cons A a l))) *)
    discriminate.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    apply in_cons. auto.
Qed.
Hint Resolve nth_error_in: coqlib.

Lemma nth_error_nil:
  forall (A: Type) (idx: nat), nth_error (@nil A) idx = None.
Proof.
(* Goal: @eq (option B) (@Some B (f a)) (@Some B (f a)) *)
(* Goal: @eq (option B) (@nth_error B (@map A B f l) n) (@option_map A B f (@nth_error A l n)) *)
  induction idx; simpl; intros; reflexivity.
Qed.
Hint Resolve nth_error_nil: coqlib.

(** Compute the length of a list, with result in [Z]. *)

Fixpoint list_length_z_aux (A: Type) (l: list A) (acc: Z) {struct l}: Z :=
  match l with
  | nil => acc
  | hd :: tl => list_length_z_aux tl (Z.succ acc)
  end.

Remark list_length_z_aux_shift:
  forall (A: Type) (l: list A) n m,
  list_length_z_aux l n = list_length_z_aux l m + (n - m).
Proof.
(* Goal: iff (@In A x (@cons A y l)) (or (@eq A y x) (@In A x l)) *)
  induction l; intros; simpl.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  omega.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  replace (n - m) with (Z.succ n - Z.succ m) by omega. auto.
Qed.

Definition list_length_z (A: Type) (l: list A) : Z :=
  list_length_z_aux l 0.

Lemma list_length_z_cons:
  forall (A: Type) (hd: A) (tl: list A),
  list_length_z (hd :: tl) = list_length_z tl + 1.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. unfold list_length_z. simpl.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite (list_length_z_aux_shift tl 1 0). omega.
Qed.

Lemma list_length_z_pos:
  forall (A: Type) (l: list A),
  list_length_z l >= 0.
Proof.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  induction l; simpl. unfold list_length_z; simpl. omega.
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  rewrite list_length_z_cons. omega.
Qed.

Lemma list_length_z_map:
  forall (A B: Type) (f: A -> B) (l: list A),
  list_length_z (map f l) = list_length_z l.
Proof.
(* Goal: @eq (option B) (@Some B (f a)) (@Some B (f a)) *)
(* Goal: @eq (option B) (@nth_error B (@map A B f l) n) (@option_map A B f (@nth_error A l n)) *)
  induction l. reflexivity. simpl. repeat rewrite list_length_z_cons. congruence.
Qed.

(** Extract the n-th element of a list, as [List.nth_error] does,
    but the index [n] is of type [Z]. *)

Fixpoint list_nth_z (A: Type) (l: list A) (n: Z) {struct l}: option A :=
  match l with
  | nil => None
  | hd :: tl => if zeq n 0 then Some hd else list_nth_z tl (Z.pred n)
  end.

Lemma list_nth_z_in:
  forall (A: Type) (l: list A) n x,
  list_nth_z l n = Some x -> In x l.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l; simpl; intros.
(* Goal: @eq B ifso ifnot *)
(* Goal: @eq B ifnot ifnot *)
  congruence.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  destruct (zeq n 0). left; congruence. right; eauto.
Qed.

Lemma list_nth_z_map:
  forall (A B: Type) (f: A -> B) (l: list A) n,
  list_nth_z (List.map f l) n = option_map f (list_nth_z l n).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l; simpl; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  destruct (zeq n 0). auto. eauto.
Qed.

Lemma list_nth_z_range:
  forall (A: Type) (l: list A) n x,
  list_nth_z l n = Some x -> 0 <= n < list_length_z l.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l; simpl; intros.
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@nil A))) *)
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@cons A a l))) *)
  discriminate.
(* Goal: and (Z.le Z0 n) (Z.lt n (@list_length_z A (@cons A a l))) *)
  rewrite list_length_z_cons. destruct (zeq n 0).
(* Goal: forall _ : and (Z.le Z0 (Z.pred n)) (Z.lt (Z.pred n) (@list_length_z A l)), and (Z.le Z0 n) (Z.lt n (Z.add (@list_length_z A l) (Zpos xH))) *)
  generalize (list_length_z_pos l); omega.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exploit IHl; eauto. omega.
Qed.

(** Properties of [List.incl] (list inclusion). *)

Lemma incl_cons_inv:
  forall (A: Type) (a: A) (b c: list A),
  incl (a :: b) c -> incl b c.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold incl; intros. apply H. apply in_cons. auto.
Qed.
Hint Resolve incl_cons_inv: coqlib.

Lemma incl_app_inv_l:
  forall (A: Type) (l1 l2 m: list A),
  incl (l1 ++ l2) m -> incl l1 m.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold incl; intros. apply H. apply in_or_app. left; assumption.
Qed.

Lemma incl_app_inv_r:
  forall (A: Type) (l1 l2 m: list A),
  incl (l1 ++ l2) m -> incl l2 m.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold incl; intros. apply H. apply in_or_app. right; assumption.
Qed.

Hint Resolve  incl_tl incl_refl incl_app_inv_l incl_app_inv_r: coqlib.

Lemma incl_same_head:
  forall (A: Type) (x: A) (l1 l2: list A),
  incl l1 l2 -> incl (x::l1) (x::l2).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros; red; simpl; intros. intuition.
Qed.

(** Properties of [List.map] (mapping a function over a list). *)

Lemma list_map_exten:
  forall (A B: Type) (f f': A -> B) (l: list A),
  (forall x, In x l -> f x = f' x) ->
  List.map f' l = List.map f l.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l; simpl; intros.
(* Goal: @eq (option B) (@Some B (f a)) (@Some B (f a)) *)
(* Goal: @eq (option B) (@nth_error B (@map A B f l) n) (@option_map A B f (@nth_error A l n)) *)
  reflexivity.
  rewrite <- H. rewrite IHl. reflexivity.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. apply H. tauto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  tauto.
Qed.

Lemma list_map_compose:
  forall (A B C: Type) (f: A -> B) (g: B -> C) (l: list A),
  List.map g (List.map f l) = List.map (fun x => g(f x)) l.
Proof.
(* Goal: @eq (option B) (@Some B (f a)) (@Some B (f a)) *)
(* Goal: @eq (option B) (@nth_error B (@map A B f l) n) (@option_map A B f (@nth_error A l n)) *)
  induction l; simpl. reflexivity. rewrite IHl; reflexivity.
Qed.

Lemma list_map_identity:
  forall (A: Type) (l: list A),
  List.map (fun (x:A) => x) l = l.
Proof.
(* Goal: @eq B ifso ifnot *)
(* Goal: @eq B ifnot ifnot *)
  induction l; simpl; congruence.
Qed.

Lemma list_map_nth:
  forall (A B: Type) (f: A -> B) (l: list A) (n: nat),
  nth_error (List.map f l) n = option_map f (nth_error l n).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l; simpl; intros.
(* Goal: @eq (option B) (@Some B (f a)) (@Some B (f a)) *)
(* Goal: @eq (option B) (@nth_error B (@map A B f l) n) (@option_map A B f (@nth_error A l n)) *)
  repeat rewrite nth_error_nil. reflexivity.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  destruct n; simpl. reflexivity. auto.
Qed.

Lemma list_length_map:
  forall (A B: Type) (f: A -> B) (l: list A),
  List.length (List.map f l) = List.length l.
Proof.
(* Goal: @eq B ifso ifnot *)
(* Goal: @eq B ifnot ifnot *)
  induction l; simpl; congruence.
Qed.

Lemma list_in_map_inv:
  forall (A B: Type) (f: A -> B) (l: list A) (y: B),
  In y (List.map f l) -> exists x:A, y = f x /\ In x l.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l; simpl; intros.
(* Goal: @eq A a b *)
(* Goal: @eq A b b *)
  contradiction.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  elim H; intro.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exists a; intuition auto.
(* Goal: @ex A (fun x : A => and (@eq B y (f x)) (or (@eq A a x) (@In A x l))) *)
  generalize (IHl y H0). intros [x [EQ IN]].
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exists x; tauto.
Qed.

Lemma list_append_map:
  forall (A B: Type) (f: A -> B) (l1 l2: list A),
  List.map f (l1 ++ l2) = List.map f l1 ++ List.map f l2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l1; simpl; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto. rewrite IHl1. auto.
Qed.

Lemma list_append_map_inv:
  forall (A B: Type) (f: A -> B) (m1 m2: list B) (l: list A),
  List.map f l = m1 ++ m2 ->
  exists l1, exists l2, List.map f l1 = m1 /\ List.map f l2 = m2 /\ l = l1 ++ l2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction m1; simpl; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exists (@nil A); exists l; auto.
(* Goal: @ex (list A) (fun l1 : list A => @ex (list A) (fun l2 : list A => and (@eq (list B) (@map A B f l1) (@cons B a m1)) (and (@eq (list B) (@map A B f l2) m2) (@eq (list A) l (@app A l1 l2))))) *)
  destruct l; simpl in H; inv H.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exploit IHm1; eauto. intros [l1 [l2 [P [Q R]]]]. subst l.
(* Goal: @eq B ifso ifnot *)
(* Goal: @eq B ifnot ifnot *)
  exists (a0 :: l1); exists l2; intuition. simpl; congruence.
Qed.

(** Folding a function over a list *)

Section LIST_FOLD.

Variables A B: Type.
Variable f: A -> B -> B.

(** This is exactly [List.fold_left] from Coq's standard library,
  with [f] taking arguments in a different order. *)

Fixpoint list_fold_left (accu: B) (l: list A) : B :=
  match l with nil => accu | x :: l' => list_fold_left (f x accu) l' end.

(** This is exactly [List.fold_right] from Coq's standard library,
  except that it runs in constant stack space. *)

Definition list_fold_right (l: list A) (base: B) : B :=
  list_fold_left base (List.rev' l).

Remark list_fold_left_app:
  forall l1 l2 accu,
  list_fold_left accu (l1 ++ l2) = list_fold_left (list_fold_left accu l1) l2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l1; simpl; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite IHl1. auto.
Qed.

Lemma list_fold_right_eq:
  forall l base,
  list_fold_right l base =
  match l with nil => base | x :: l' => f x (list_fold_right l' base) end.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold list_fold_right; intros.
(* Goal: @eq B (list_fold_left base (@rev' A l)) match l with | nil => base | cons x l' => f x (list_fold_left base (@rev' A l')) end *)
  destruct l.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  auto.
  unfold rev'. rewrite <- ! rev_alt. simpl.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite list_fold_left_app. simpl. auto.
Qed.

Lemma list_fold_right_spec:
  forall l base, list_fold_right l base = List.fold_right f base l.
Proof.
(* Goal: @eq B ifso ifnot *)
(* Goal: @eq B ifnot ifnot *)
  induction l; simpl; intros; rewrite list_fold_right_eq; congruence.
Qed.

End LIST_FOLD.

(** Properties of list membership. *)

Lemma in_cns:
  forall (A: Type) (x y: A) (l: list A), In x (y :: l) <-> y = x \/ In x l.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. simpl. tauto.
Qed.

Lemma in_app:
  forall (A: Type) (x: A) (l1 l2: list A), In x (l1 ++ l2) <-> In x l1 \/ In x l2.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. split; intro. apply in_app_or. auto. apply in_or_app. auto.
Qed.

Lemma list_in_insert:
  forall (A: Type) (x: A) (l1 l2: list A) (y: A),
  In x (l1 ++ l2) -> In x (l1 ++ y :: l2).
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. apply in_or_app; simpl. elim (in_app_or _ _ _ H); intro; auto.
Qed.

(** [list_disjoint l1 l2] holds iff [l1] and [l2] have no elements
  in common. *)

Definition list_disjoint (A: Type) (l1 l2: list A) : Prop :=
  forall (x y: A), In x l1 -> In y l2 -> x <> y.

Lemma list_disjoint_cons_l:
  forall (A: Type) (a: A) (l1 l2: list A),
  list_disjoint l1 l2 -> ~In a l2 -> list_disjoint (a :: l1) l2.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold list_disjoint; simpl; intros. destruct H1. congruence. apply H; auto.
Qed.

Lemma list_disjoint_cons_r:
  forall (A: Type) (a: A) (l1 l2: list A),
  list_disjoint l1 l2 -> ~In a l1 -> list_disjoint l1 (a :: l2).
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold list_disjoint; simpl; intros. destruct H2. congruence. apply H; auto.
Qed.

Lemma list_disjoint_cons_left:
  forall (A: Type) (a: A) (l1 l2: list A),
  list_disjoint (a :: l1) l2 -> list_disjoint l1 l2.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold list_disjoint; simpl; intros. apply H; tauto.
Qed.

Lemma list_disjoint_cons_right:
  forall (A: Type) (a: A) (l1 l2: list A),
  list_disjoint l1 (a :: l2) -> list_disjoint l1 l2.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  unfold list_disjoint; simpl; intros. apply H; tauto.
Qed.

Lemma list_disjoint_notin:
  forall (A: Type) (l1 l2: list A) (a: A),
  list_disjoint l1 l2 -> In a l1 -> ~(In a l2).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold list_disjoint; intros; red; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply H with a a; auto.
Qed.

Lemma list_disjoint_sym:
  forall (A: Type) (l1 l2: list A),
  list_disjoint l1 l2 -> list_disjoint l2 l1.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  unfold list_disjoint; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply not_eq_sym. apply H; auto.
Qed.

Lemma list_disjoint_dec:
  forall (A: Type) (eqA_dec: forall (x y: A), {x=y} + {x<>y}) (l1 l2: list A),
  {list_disjoint l1 l2} + {~list_disjoint l1 l2}.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l1; intros.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  left; red; intros. elim H.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  case (In_dec eqA_dec a l2); intro.
(* Goal: not (@In A a (@app A a0 (@cons A x b))) *)
(* Goal: @list_norepet A (@app A a0 (@cons A x b)) *)
(* Goal: forall (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
  right; red; intro. apply (H a a); auto with coqlib.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  case (IHl1 l2); intro.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  left; red; intros. elim H; intro.
(* Goal: @eq A a b *)
(* Goal: @eq A b b *)
    red; intro; subst a y. contradiction.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    apply l; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  right; red; intros. elim n0. eapply list_disjoint_cons_left; eauto.
Defined.

(** [list_equiv l1 l2] holds iff the lists [l1] and [l2] contain the same elements. *)

Definition list_equiv (A : Type) (l1 l2: list A) : Prop :=
  forall x, In x l1 <-> In x l2.

(** [list_norepet l] holds iff the list [l] contains no repetitions,
  i.e. no element occurs twice. *)

Inductive list_norepet (A: Type) : list A -> Prop :=
  | list_norepet_nil:
      list_norepet nil
  | list_norepet_cons:
      forall hd tl,
      ~(In hd tl) -> list_norepet tl -> list_norepet (hd :: tl).

Lemma list_norepet_dec:
  forall (A: Type) (eqA_dec: forall (x y: A), {x=y} + {x<>y}) (l: list A),
  {list_norepet l} + {~list_norepet l}.
Proof.
(* Goal: forall (A : Type) (_ : forall x y : A, sumbool (@eq A x y) (not (@eq A x y))) (l : list A), sumbool (@list_norepet A l) (not (@list_norepet A l)) *)
  induction l.
(* Goal: sumbool (@list_norepet A (@nil A)) (not (@list_norepet A (@nil A))) *)
(* Goal: sumbool (@list_norepet A (@cons A a l)) (not (@list_norepet A (@cons A a l))) *)
  left; constructor.
(* Goal: sumbool (@list_norepet A (@cons A a l)) (not (@list_norepet A (@cons A a l))) *)
  destruct IHl.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
  case (In_dec eqA_dec a l); intro.
(* Goal: not (@In A a (@app A a0 (@cons A x b))) *)
(* Goal: @list_norepet A (@app A a0 (@cons A x b)) *)
(* Goal: forall (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
  right. red; intro. inversion H. contradiction.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  left. constructor; auto.
(* Goal: not (@In A a (@app A a0 (@cons A x b))) *)
(* Goal: @list_norepet A (@app A a0 (@cons A x b)) *)
(* Goal: forall (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
  right. red; intro. inversion H. contradiction.
Defined.

Lemma list_map_norepet:
  forall (A B: Type) (f: A -> B) (l: list A),
  list_norepet l ->
  (forall x y, In x l -> In y l -> x <> y -> f x <> f y) ->
  list_norepet (List.map f l).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction 1; simpl; intros.
(* Goal: @list_norepet A (@nil A) *)
(* Goal: @list_norepet A (@list_drop A n tl) *)
  constructor.
(* Goal: @list_norepet A (@nil A) *)
(* Goal: @list_norepet A (@list_drop A n tl) *)
  constructor.
(* Goal: not (@In A a (@app A a0 (@cons A x b))) *)
(* Goal: @list_norepet A (@app A a0 (@cons A x b)) *)
(* Goal: forall (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
  red; intro. generalize (list_in_map_inv f _ _ H2).
(* Goal: forall _ : @ex A (fun x : A => and (@eq B (f hd) (f x)) (@In A x tl)), False *)
(* Goal: @list_norepet B (@map A B f tl) *)
  intros [x [EQ IN]]. generalize EQ. change (f hd <> f x).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply H1. tauto. tauto.
(* Goal: @eq A a b *)
(* Goal: @eq A b b *)
  red; intro; subst x. contradiction.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  apply IHlist_norepet. intros. apply H1. tauto. tauto. auto.
Qed.

Remark list_norepet_append_commut:
  forall (A: Type) (a b: list A),
  list_norepet (a ++ b) -> list_norepet (b ++ a).
Proof.
(* Goal: forall (A : Type) (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
  intro A.
  assert (forall (x: A) (b: list A) (a: list A),
           list_norepet (a ++ b) -> ~(In x a) -> ~(In x b) ->
           list_norepet (a ++ x :: b)).
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
    induction a; simpl; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    constructor; auto.
(* Goal: not (@In A a (@app A a0 (@cons A x b))) *)
(* Goal: @list_norepet A (@app A a0 (@cons A x b)) *)
(* Goal: forall (a b : list A) (_ : @list_norepet A (@app A a b)), @list_norepet A (@app A b a) *)
    inversion H. constructor. red; intro.
(* Goal: forall _ : @eq Z (Z.sub (Z.add x y) (Zpos xH)) (Z.add (Z.mul y (Z.div (Z.sub (Z.add x y) (Zpos xH)) y)) (Z.modulo (Z.sub (Z.add x y) (Zpos xH)) y)), Z.le x (Z.mul (Z.div (Z.sub (Z.add x y) (Zpos xH)) y) y) *)
    elim (in_app_or _ _ _ H6); intro.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    elim H4. apply in_or_app. tauto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    elim H7; intro. subst a. elim H0. left. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    elim H4. apply in_or_app. tauto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    auto.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction a; simpl; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite <- app_nil_end. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  inversion H0. apply H. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  red; intro; elim H3. apply in_or_app. tauto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  red; intro; elim H3. apply in_or_app. tauto.
Qed.

Lemma list_norepet_app:
  forall (A: Type) (l1 l2: list A),
  list_norepet (l1 ++ l2) <->
  list_norepet l1 /\ list_norepet l2 /\ list_disjoint l1 l2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction l1; simpl; intros; split; intros.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intuition. constructor. red;simpl;auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  tauto.
(* Goal: and (Z.le Z0 a) (Z.le a (Z.sub b c)) *)
  inversion H; subst. rewrite IHl1 in H3. rewrite in_app in H2.
(* Goal: and (@list_norepet A l1) (and (@list_norepet A l2) (@list_disjoint A l1 l2)) *)
  intuition.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  constructor; auto. red; intros. elim H2; intro. congruence. auto.
(* Goal: and (Z.le Z0 a) (Z.le a (Z.sub b c)) *)
  destruct H as [B [C D]]. inversion B; subst.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  constructor. rewrite in_app. intuition. elim (D a a); auto. apply in_eq.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite IHl1. intuition. red; intros. apply D; auto. apply in_cons; auto.
Qed.

Lemma list_norepet_append:
  forall (A: Type) (l1 l2: list A),
  list_norepet l1 -> list_norepet l2 -> list_disjoint l1 l2 ->
  list_norepet (l1 ++ l2).
Proof.
(* Goal: forall (A : Type) (l1 l2 : list A) (_ : @list_norepet A (@app A l1 l2)), @list_norepet A l1 *)
  generalize list_norepet_app; firstorder.
Qed.

Lemma list_norepet_append_right:
  forall (A: Type) (l1 l2: list A),
  list_norepet (l1 ++ l2) -> list_norepet l2.
Proof.
(* Goal: forall (A : Type) (l1 l2 : list A) (_ : @list_norepet A (@app A l1 l2)), @list_norepet A l1 *)
  generalize list_norepet_app; firstorder.
Qed.

Lemma list_norepet_append_left:
  forall (A: Type) (l1 l2: list A),
  list_norepet (l1 ++ l2) -> list_norepet l1.
Proof.
(* Goal: forall (A : Type) (l1 l2 : list A) (_ : @list_norepet A (@app A l1 l2)), @list_norepet A l1 *)
  generalize list_norepet_app; firstorder.
Qed.

(** [is_tail l1 l2] holds iff [l2] is of the form [l ++ l1] for some [l]. *)

Inductive is_tail (A: Type): list A -> list A -> Prop :=
  | is_tail_refl:
      forall c, is_tail c c
  | is_tail_cons:
      forall i c1 c2, is_tail c1 c2 -> is_tail c1 (i :: c2).

Lemma is_tail_in:
  forall (A: Type) (i: A) c1 c2, is_tail (i :: c1) c2 -> In i c2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction c2; simpl; intros.
(* Goal: or (@eq A a i) (@In A i c2) *)
  inversion H.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  inversion H. tauto. right; auto.
Qed.

Lemma is_tail_cons_left:
  forall (A: Type) (i: A) c1 c2, is_tail (i :: c1) c2 -> is_tail c1 c2.
Proof.
(* Goal: or (@eq A a i) (@In A i c2) *)
  induction c2; intros; inversion H.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  constructor. constructor. constructor. auto.
Qed.

Hint Resolve is_tail_refl is_tail_cons is_tail_in is_tail_cons_left: coqlib.

Lemma is_tail_incl:
  forall (A: Type) (l1 l2: list A), is_tail l1 l2 -> incl l1 l2.
Proof.
(* Goal: forall (A : Type) (l1 l2 : list A) (_ : @is_tail A l1 l2), @incl A l1 l2 *)
  induction 1; eauto with coqlib.
Qed.

Lemma is_tail_trans:
  forall (A: Type) (l1 l2: list A),
  is_tail l1 l2 -> forall (l3: list A), is_tail l2 l3 -> is_tail l1 l3.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction 1; intros. auto. apply IHis_tail. eapply is_tail_cons_left; eauto.
Qed.

(** [list_forall2 P [x1 ... xN] [y1 ... yM]] holds iff [N = M] and
  [P xi yi] holds for all [i]. *)

Section FORALL2.

Variable A: Type.
Variable B: Type.
Variable P: A -> B -> Prop.

Inductive list_forall2: list A -> list B -> Prop :=
  | list_forall2_nil:
      list_forall2 nil nil
  | list_forall2_cons:
      forall a1 al b1 bl,
      P a1 b1 ->
      list_forall2 al bl ->
      list_forall2 (a1 :: al) (b1 :: bl).

Lemma list_forall2_app:
  forall a2 b2 a1 b1,
  list_forall2 a1 b1 -> list_forall2 a2 b2 ->
  list_forall2 (a1 ++ a2) (b1 ++ b2).
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction 1; intros; simpl. auto. constructor; auto.
Qed.

Lemma list_forall2_length:
  forall l1 l2,
  list_forall2 l1 l2 -> length l1 = length l2.
Proof.
(* Goal: @eq B ifso ifnot *)
(* Goal: @eq B ifnot ifnot *)
  induction 1; simpl; congruence.
Qed.

Lemma list_forall2_in_left:
  forall x1 l1 l2,
  list_forall2 l1 l2 -> In x1 l1 -> exists x2, In x2 l2 /\ P x1 x2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction 1; simpl; intros. contradiction. destruct H1.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  subst; exists b1; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exploit IHlist_forall2; eauto. intros (x2 & U & V); exists x2; auto.
Qed.

Lemma list_forall2_in_right:
  forall x2 l1 l2,
  list_forall2 l1 l2 -> In x2 l2 -> exists x1, In x1 l1 /\ P x1 x2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction 1; simpl; intros. contradiction. destruct H1.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  subst; exists a1; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  exploit IHlist_forall2; eauto. intros (x1 & U & V); exists x1; auto.
Qed.

End FORALL2.

Lemma list_forall2_imply:
  forall (A B: Type) (P1: A -> B -> Prop) (l1: list A) (l2: list B),
  list_forall2 P1 l1 l2 ->
  forall (P2: A -> B -> Prop),
  (forall v1 v2, In v1 l1 -> In v2 l2 -> P1 v1 v2 -> P2 v1 v2) ->
  list_forall2 P2 l1 l2.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  induction 1; intros.
(* Goal: @list_norepet A (@nil A) *)
(* Goal: @list_norepet A (@list_drop A n tl) *)
  constructor.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  constructor. auto with coqlib. apply IHlist_forall2; auto.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. auto with coqlib.
Qed.

(** Dropping the first N elements of a list. *)

Fixpoint list_drop (A: Type) (n: nat) (x: list A) {struct n} : list A :=
  match n with
  | O => x
  | S n' => match x with nil => nil | hd :: tl => list_drop n' tl end
  end.

Lemma list_drop_incl:
  forall (A: Type) (x: A) n (l: list A), In x (list_drop n l) -> In x l.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction n; simpl; intros. auto.
(* Goal: P2 v1 v2 *)
  destruct l; auto with coqlib.
Qed.

Lemma list_drop_norepet:
  forall (A: Type) n (l: list A), list_norepet l -> list_norepet (list_drop n l).
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction n; simpl; intros. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  inv H. constructor. auto.
Qed.

Lemma list_map_drop:
  forall (A B: Type) (f: A -> B) n (l: list A),
  list_drop n (map f l) = map f (list_drop n l).
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction n; simpl; intros. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  destruct l; simpl; auto.
Qed.

(** A list of [n] elements, all equal to [x]. *)

Fixpoint list_repeat {A: Type} (n: nat) (x: A) {struct n} :=
  match n with
  | O => nil
  | S m => x :: list_repeat m x
  end.

Lemma length_list_repeat:
  forall (A: Type) n (x: A), length (list_repeat n x) = n.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction n; simpl; intros. auto. decEq; auto.
Qed.

Lemma in_list_repeat:
  forall (A: Type) n (x: A) y, In y (list_repeat n x) -> y = x.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction n; simpl; intros. elim H. destruct H; auto.
Qed.

(** * Definitions and theorems over boolean types *)

Definition proj_sumbool {P Q: Prop} (a: {P} + {Q}) : bool :=
  if a then true else false.

Coercion proj_sumbool: sumbool >-> bool.

Lemma proj_sumbool_true:
  forall (P Q: Prop) (a: {P}+{Q}), proj_sumbool a = true -> P.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros P Q a. destruct a; simpl. auto. congruence.
Qed.

Lemma proj_sumbool_is_true:
  forall (P: Prop) (a: {P}+{~P}), P -> proj_sumbool a = true.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. unfold proj_sumbool. destruct a. auto. contradiction.
Qed.

Ltac InvBooleans :=
  match goal with
  | [ H: _ && _ = true |- _ ] =>
      destruct (andb_prop _ _ H); clear H; InvBooleans
  | [ H: _ || _ = false |- _ ] =>
      destruct (orb_false_elim _ _ H); clear H; InvBooleans
  | [ H: proj_sumbool ?x = true |- _ ] =>
      generalize (proj_sumbool_true _ H); clear H; intro; InvBooleans
  | _ => idtac
  end.

Section DECIDABLE_EQUALITY.

Variable A: Type.
Variable dec_eq: forall (x y: A), {x=y} + {x<>y}.
Variable B: Type.

Lemma dec_eq_true:
  forall (x: A) (ifso ifnot: B),
  (if dec_eq x x then ifso else ifnot) = ifso.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. destruct (dec_eq x x). auto. congruence.
Qed.

Lemma dec_eq_false:
  forall (x y: A) (ifso ifnot: B),
  x <> y -> (if dec_eq x y then ifso else ifnot) = ifnot.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. destruct (dec_eq x y). congruence. auto.
Qed.

Lemma dec_eq_sym:
  forall (x y: A) (ifso ifnot: B),
  (if dec_eq x y then ifso else ifnot) =
  (if dec_eq y x then ifso else ifnot).
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros. destruct (dec_eq x y).
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  subst y. rewrite dec_eq_true. auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  rewrite dec_eq_false; auto.
Qed.

End DECIDABLE_EQUALITY.

Section DECIDABLE_PREDICATE.

Variable P: Prop.
Variable dec: {P} + {~P}.
Variable A: Type.

Lemma pred_dec_true:
  forall (a b: A), P -> (if dec then a else b) = a.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. destruct dec. auto. contradiction.
Qed.

Lemma pred_dec_false:
  forall (a b: A), ~P -> (if dec then a else b) = b.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  intros. destruct dec. contradiction. auto.
Qed.

End DECIDABLE_PREDICATE.

(** * Well-founded orderings *)

Require Import Relations.

(** A non-dependent version of lexicographic ordering. *)

Section LEX_ORDER.

Variable A: Type.
Variable B: Type.
Variable ordA: A -> A -> Prop.
Variable ordB: B -> B -> Prop.

Inductive lex_ord: A*B -> A*B -> Prop :=
  | lex_ord_left: forall a1 b1 a2 b2,
      ordA a1 a2 -> lex_ord (a1,b1) (a2,b2)
  | lex_ord_right: forall a b1 b2,
      ordB b1 b2 -> lex_ord (a,b1) (a,b2).

Lemma wf_lex_ord:
  well_founded ordA -> well_founded ordB -> well_founded lex_ord.
Proof.
(* Goal: forall (_ : @well_founded A ordA) (_ : @well_founded B ordB), @well_founded (prod A B) lex_ord *)
  intros Awf Bwf.
  assert (forall a, Acc ordA a -> forall b, Acc ordB b -> Acc lex_ord (a, b)).
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
    induction 1. induction 1. constructor; intros. inv H3.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    apply H0. auto. apply Bwf.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
    apply H2; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  red; intros. destruct a as [a b]. apply H; auto.
Qed.

Lemma transitive_lex_ord:
  transitive _ ordA -> transitive _ ordB -> transitive _ lex_ord.
Proof.
(* Goal: forall (a b : A) (_ : not P), @eq A (if dec then a else b) b *)
  intros trA trB; red; intros.
(* Goal: @eq Z q (Z.mul p (Z.div q p)) *)
  inv H; inv H0.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  left; eapply trA; eauto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  left; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  left; auto.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  right; eapply trB; eauto.
Qed.

End LEX_ORDER.

(** * Nonempty lists *)

Inductive nlist (A: Type) : Type :=
  | nbase: A -> nlist A
  | ncons: A -> nlist A -> nlist A.

Definition nfirst {A: Type} (l: nlist A) :=
  match l with nbase a => a | ncons a l' => a end.

Fixpoint nlast {A: Type} (l: nlist A) :=
  match l with nbase a => a | ncons a l' => nlast l' end.

Fixpoint nIn {A: Type} (x: A) (l: nlist A) : Prop :=
  match l with
  | nbase a => a = x
  | ncons a l => a = x \/ nIn x l
  end.

Inductive nlist_forall2 {A B: Type} (R: A -> B -> Prop) : nlist A -> nlist B -> Prop :=
  | nbase_forall2: forall a b, R a b -> nlist_forall2 R (nbase a) (nbase b)
  | ncons_forall2: forall a l b m, R a b -> nlist_forall2 R l m -> nlist_forall2 R (ncons a l) (ncons b m).

Lemma nlist_forall2_imply:
  forall (A B: Type) (P1: A -> B -> Prop) (l1: nlist A) (l2: nlist B),
  nlist_forall2 P1 l1 l2 ->
  forall (P2: A -> B -> Prop),
  (forall v1 v2, nIn v1 l1 -> nIn v2 l2 -> P1 v1 v2 -> P2 v1 v2) ->
  nlist_forall2 P2 l1 l2.
Proof.
(* Goal: ordA a1 x *)
(* Goal: @Acc B ordB b1 *)
(* Goal: @Acc (prod A B) lex_ord (@pair A B x b1) *)
(* Goal: @well_founded (prod A B) lex_ord *)
  induction 1; simpl; intros; constructor; auto.
Qed.
