(* 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                                 *)
(*                                                                          *)
(****************************************************************************)
(*                               Gilbreath.v                                 *)
(****************************************************************************)
(* G. Huet - V5.8  Nov. 1994 *)
(*    ported V5.10 June 1995 *)

Require Import Bool.
Require Import Words.
Require Import Alternate.
Require Import Opposite.
Require Import Paired.
Require Import Shuffle.

(****************************)
(* The Gilbreath card trick *)
(****************************)

Section Context.

Variable x : word.
Hypothesis Even_x : even x.
Variable b : bool. (* witness for (alternate x) *)
Hypothesis A : alt b x.
Variable u v : word.
Hypothesis C : conc u v x.
Variable w : word.
Hypothesis S : shuffle u v w.

Lemma Alt_u : alt b u.
Proof.
(* Goal: alt b u *)
apply alt_conc_l with v x.
(* Goal: conc u v x *)
(* Goal: alt b x *)
apply C.
(* Goal: alt b x *)
apply A.
Qed.

Section Case1_.
Hypothesis Odd_u : odd u.

Lemma Not_even_u : ~ even u.
Proof.
(* Goal: not (even u) *)
red in |- *; intro.
(* Goal: False *)
elim not_odd_and_even with u; trivial.
Qed.

Lemma Odd_v : odd v.
Proof.
(* Goal: even v *)
elim even_conc with u v x.
(* Goal: forall _ : and (even u) (alt b v), alt b v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; trivial.
(* Goal: forall _ : and (even u) (alt b v), alt (negb b) v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; intro; elim Not_even_u; trivial.
(* Goal: conc u v x *)
(* Goal: alt b x *)
apply C.
(* Goal: even x *)
apply Even_x.
Qed.

Remark Alt_v_neg : alt (negb b) v.
Proof.
(* Goal: alt b v *)
elim alt_conc_r with u v x b.
(* Goal: forall _ : and (even u) (alt b v), alt b v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; trivial.
(* Goal: forall _ : and (even u) (alt b v), alt (negb b) v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; intro; elim Not_even_u; trivial.
(* Goal: conc u v x *)
(* Goal: alt b x *)
apply C.
(* Goal: alt b x *)
apply A.
Qed.

Lemma Opp_uv : opposite u v.
Proof.
(* Goal: opposite u v *)
apply alt_neg_opp with b.
(* Goal: odd u *)
(* Goal: alt b u *)
(* Goal: odd v *)
(* Goal: alt (negb b) v *)
apply Odd_u.
(* Goal: alt b u *)
apply Alt_u.
(* Goal: odd v *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Odd_v.
(* Goal: alt (negb b) v *)
(* Goal: paired w *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Alt_v_neg.
Qed.

Lemma Case1 : paired w.
Proof.
(* Goal: paired_rot b w *)
elim Shuffling with u v w b.
simple induction 1; simple induction 2; simple induction 1;
 simple induction 2; intros P1 P2.
(* Goal: paired_rot b w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply P1.
(* Goal: alt (negb b) v *)
(* Goal: paired w *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Alt_v_neg.
(* Goal: paired w *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
elim not_odd_and_even with v; trivial.
(* Goal: odd v *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Odd_v.
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
simple induction 1; intro; elim Not_even_u; trivial.
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply S.
(* Goal: alt b u *)
apply Alt_u.
Qed.
End Case1_.

Section Case2_.
Hypothesis Even_u : even u.

Lemma Not_odd_u : ~ odd u.
Proof.
(* Goal: False *)
red in |- *; intro; elim not_odd_and_even with u; trivial.
Qed.

Lemma Even_v : even v.
Proof.
(* Goal: even v *)
elim even_conc with u v x.
(* Goal: forall _ : and (odd u) (alt (negb b) v), alt b v *)
(* Goal: forall _ : and (even u) (alt b v), alt b v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; intro; elim Not_odd_u; trivial.
(* Goal: forall _ : and (even u) (alt b v), alt b v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; trivial.
(* Goal: conc u v x *)
(* Goal: alt b x *)
apply C.
(* Goal: even x *)
apply Even_x.
Qed.

Remark Alt_v : alt b v.
Proof.
(* Goal: alt b v *)
elim alt_conc_r with u v x b.
(* Goal: forall _ : and (odd u) (alt (negb b) v), alt b v *)
(* Goal: forall _ : and (even u) (alt b v), alt b v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; intro; elim Not_odd_u; trivial.
(* Goal: forall _ : and (even u) (alt b v), alt b v *)
(* Goal: conc u v x *)
(* Goal: alt b x *)
intro H; elim H; trivial.
(* Goal: conc u v x *)
(* Goal: alt b x *)
apply C.
(* Goal: alt b x *)
apply A.
Qed.

Lemma Not_opp_uv : ~ opposite u v.
Proof.
(* Goal: not (opposite u v) *)
apply alt_not_opp with b.
(* Goal: alt b u *)
apply Alt_u.
(* Goal: alt b v *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Alt_v.
Qed.

Lemma Case2 : paired (rotate w).
Proof.
(* Goal: paired (rotate w) *)
apply paired_rotate with b.
(* Goal: paired_rot b w *)
elim Shuffling with u v w b.
(* Goal: forall _ : and (odd u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired w) (forall _ : alt b v, paired_bet b w))) (and (even v) (and (forall _ : alt b v, paired_odd_l b w) (forall _ : alt (negb b) v, paired_odd_r (negb b) w)))), paired_rot b w *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired_rot b w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
simple induction 1; intro; elim Not_odd_u; trivial.
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired_rot b w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
simple induction 1; simple induction 2.
(* Goal: paired w *)
(* Goal: forall _ : and (even u) (or (and (odd v) (and (forall _ : alt (negb b) v, paired_odd_r b w) (forall _ : alt b v, paired_odd_l b w))) (and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)))), paired w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
simple induction 1; intros; elim not_odd_and_even with v; trivial.
(* Goal: even v *)
(* Goal: forall _ : and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)), paired_rot b w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Even_v.
(* Goal: forall _ : and (even v) (and (forall _ : alt b v, paired_rot b w) (forall _ : alt (negb b) v, paired w)), paired_rot b w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
simple induction 1; simple induction 2; intros P1 P2.
(* Goal: paired_rot b w *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply P1.
(* Goal: alt b v *)
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply Alt_v.
(* Goal: shuffle u v w *)
(* Goal: alt b u *)
apply S.
(* Goal: alt b u *)
apply Alt_u.
Qed.

End Case2_.

(* We recall from the prelude the definition of the conditional :
Definition IF := [P,Q,R:Prop](P /\ Q) \/ ((~P) /\ R)
Syntax IF "IF _ then _ else _" *)

Lemma Main : IF opposite u v then paired w else paired (rotate w).
Proof.
(* Goal: IF_then_else (opposite u v) (paired w) (paired (rotate w)) *)
unfold IF_then_else in |- *; elim odd_or_even with u; intros.
(* (odd u) : Case 1 *)
(* Goal: or (and (opposite u v) (paired w)) (and (not (opposite u v)) (paired (rotate w))) *)
(* Goal: or (and (opposite u v) (paired w)) (and (not (opposite u v)) (paired (rotate w))) *)
left; split.
(* Goal: opposite u v *)
(* Goal: paired w *)
(* Goal: or (and (opposite u v) (paired w)) (and (not (opposite u v)) (paired (rotate w))) *)
apply Opp_uv; trivial.
(* Goal: paired w *)
(* Goal: or (and (opposite u v) (paired w)) (and (not (opposite u v)) (paired (rotate w))) *)
apply Case1; trivial.
(* (even u) : Case 2 *)
(* Goal: or (and (opposite u v) (paired w)) (and (not (opposite u v)) (paired (rotate w))) *)
right; split.
(* Goal: not (opposite u v) *)
(* Goal: paired (rotate w) *)
apply Not_opp_uv; trivial.
(* Goal: paired (rotate w) *)
apply Case2; trivial.
Qed.

End Context.


(*********************)
(* Gilbreath's trick *)
(*********************)


Theorem Gilbreath :
 forall x : word,
 even x ->
 alternate x ->
 forall u v : word,
 conc u v x ->
 forall w : word,
 shuffle u v w -> IF opposite u v then paired w else paired (rotate w).

Proof.
(* Goal: forall (x : word) (_ : even x) (_ : alternate x) (u v : word) (_ : conc u v x) (w : word) (_ : shuffle u v w), IF_then_else (opposite u v) (paired w) (paired (rotate w)) *)
simple induction 2; intros. (* Existential intro *)
(* Goal: IF_then_else (opposite u v) (paired w) (paired (rotate w)) *)
apply Main with x b; trivial.
Qed.
