(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)


(* Contribution to the Coq Library   V6.3 (July 1999)                    *)

(****************************************************************************)
(*                 The Calculus of Inductive Constructions                  *)
(*                                                                          *)
(*                                Projet Coq                                *)
(*                                                                          *)
(*                     INRIA                        ENS-CNRS                *)
(*              Rocquencourt                        Lyon                    *)
(*                                                                          *)
(*                                Coq V5.10                                 *)
(*                                                                          *)
(****************************************************************************)
(*                               Opposite.v                                 *)
(****************************************************************************)
(* G. Huet - V5.8  Nov. 1994 *)
(*    ported V5.10 June 1995 *)

Require Import Bool.
Require Import Words.
Require Import Alternate.


(************)
(* Opposite *)
(************)

Inductive opposite : word -> word -> Prop :=
    opp : forall (u v : word) (b : bool), opposite (bit b u) (bit (negb b) v).

Hint Resolve opp.

Lemma not_opp_empty_r : forall u : word, ~ opposite u empty.
Proof.
(* Goal: forall u : word, not (opposite empty u) *)
unfold not in |- *; intros u op.
(* Goal: False *)
inversion op.
Qed.

Lemma not_opp_empty_l : forall u : word, ~ opposite empty u.
Proof.
(* Goal: forall u : word, not (opposite empty u) *)
unfold not in |- *; intros u op.
(* Goal: False *)
inversion op.
Qed.

Lemma not_opp_same :
 forall (u v : word) (b : bool), ~ opposite (bit b u) (bit b v).
Proof.
(* Goal: forall (u v : word) (b : bool), not (opposite (bit b u) (bit b v)) *)
unfold not in |- *; intros u v b op.
(* Goal: False *)
inversion op.
(* Goal: False *)
apply (no_fixpoint_negb b); trivial.
Qed.

Lemma alt_neg_opp :
 forall (u v : word) (b : bool),
 odd u -> alt b u -> odd v -> alt (negb b) v -> opposite u v.
Proof.
(* Goal: forall (u v : word) (b : bool) (_ : alt b u) (_ : alt b v), not (opposite u v) *)
simple induction u.
(* Goal: forall (v : word) (b : bool) (_ : odd empty) (_ : alt b empty) (_ : odd v) (_ : alt (negb b) v), opposite empty v *)
(* Goal: forall (b : bool) (w : word) (_ : forall (v : word) (b0 : bool) (_ : odd w) (_ : alt b0 w) (_ : odd v) (_ : alt (negb b0) v), opposite w v) (v : word) (b0 : bool) (_ : odd (bit b w)) (_ : alt b0 (bit b w)) (_ : odd v) (_ : alt (negb b0) v), opposite (bit b w) v *)
intros v b odd_empty; absurd (odd empty); trivial.
(* Goal: forall (b : bool) (w : word) (_ : forall (v : word) (b0 : bool) (_ : alt b0 w) (_ : alt b0 v), not (opposite w v)) (v : word) (b0 : bool) (_ : alt b0 (bit b w)) (_ : alt b0 v), not (opposite (bit b w) v) *)
intros b u' H v; elim v.
(* Goal: forall (b0 : bool) (_ : odd (bit b u')) (_ : alt b0 (bit b u')) (_ : odd empty) (_ : alt (negb b0) empty), opposite (bit b u') empty *)
(* Goal: forall (b0 : bool) (w : word) (_ : forall (b1 : bool) (_ : odd (bit b u')) (_ : alt b1 (bit b u')) (_ : odd w) (_ : alt (negb b1) w), opposite (bit b u') w) (b1 : bool) (_ : odd (bit b u')) (_ : alt b1 (bit b u')) (_ : odd (bit b0 w)) (_ : alt (negb b1) (bit b0 w)), opposite (bit b u') (bit b0 w) *)
intros b' H1 H2 odd_empty.
(* Goal: forall _ : alt (negb b') empty, opposite (bit b u') empty *)
(* Goal: forall (b0 : bool) (w : word) (_ : forall (b1 : bool) (_ : odd (bit b u')) (_ : alt b1 (bit b u')) (_ : odd w) (_ : alt (negb b1) w), opposite (bit b u') w) (b1 : bool) (_ : odd (bit b u')) (_ : alt b1 (bit b u')) (_ : odd (bit b0 w)) (_ : alt (negb b1) (bit b0 w)), opposite (bit b u') (bit b0 w) *)
absurd (odd empty); trivial.
(* Goal: forall (b0 : bool) (w : word) (_ : forall (b1 : bool) (_ : odd (bit b u')) (_ : alt b1 (bit b u')) (_ : odd w) (_ : alt (negb b1) w), opposite (bit b u') w) (b1 : bool) (_ : odd (bit b u')) (_ : alt b1 (bit b u')) (_ : odd (bit b0 w)) (_ : alt (negb b1) (bit b0 w)), opposite (bit b u') (bit b0 w) *)
intros b' v' H' b'' H1 H2 H3 H4.
(* Goal: opposite (bit b u') (bit b' v') *)
elim (alt_eq (negb b'') b' v'); trivial.
(* Goal: not (opposite (bit b u') (bit b'' v')) *)
elim (alt_eq b'' b u'); trivial.
Qed.

Lemma alt_not_opp :
 forall (u v : word) (b : bool), alt b u -> alt b v -> ~ opposite u v.
Proof.
(* Goal: forall (u v : word) (b : bool) (_ : alt b u) (_ : alt b v), not (opposite u v) *)
simple induction u.
(* Goal: forall (v : word) (b : bool) (_ : alt b empty) (_ : alt b v), not (opposite empty v) *)
(* Goal: forall (b : bool) (w : word) (_ : forall (v : word) (b0 : bool) (_ : alt b0 w) (_ : alt b0 v), not (opposite w v)) (v : word) (b0 : bool) (_ : alt b0 (bit b w)) (_ : alt b0 v), not (opposite (bit b w) v) *)
intros; apply not_opp_empty_l.
(* Goal: forall (b : bool) (w : word) (_ : forall (v : word) (b0 : bool) (_ : alt b0 w) (_ : alt b0 v), not (opposite w v)) (v : word) (b0 : bool) (_ : alt b0 (bit b w)) (_ : alt b0 v), not (opposite (bit b w) v) *)
intros b u' H v; elim v.
(* Goal: forall (b0 : bool) (_ : alt b0 (bit b u')) (_ : alt b0 empty), not (opposite (bit b u') empty) *)
(* Goal: forall (b0 : bool) (w : word) (_ : forall (b1 : bool) (_ : alt b1 (bit b u')) (_ : alt b1 w), not (opposite (bit b u') w)) (b1 : bool) (_ : alt b1 (bit b u')) (_ : alt b1 (bit b0 w)), not (opposite (bit b u') (bit b0 w)) *)
intros; apply not_opp_empty_r.
(* Goal: forall (b0 : bool) (w : word) (_ : forall (b1 : bool) (_ : alt b1 (bit b u')) (_ : alt b1 w), not (opposite (bit b u') w)) (b1 : bool) (_ : alt b1 (bit b u')) (_ : alt b1 (bit b0 w)), not (opposite (bit b u') (bit b0 w)) *)
intros b' v' H1 b'' H2 H3.
(* Goal: not (opposite (bit b u') (bit b' v')) *)
elim (alt_eq b'' b' v'); trivial.
(* Goal: not (opposite (bit b u') (bit b'' v')) *)
elim (alt_eq b'' b u'); trivial.
(* Goal: not (opposite (bit b'' u') (bit b'' v')) *)
apply not_opp_same.
Qed.
