Require Export definitions.
Require Export lemmas.
 
(* these induction principles relies on properties of dependent lists
   established in lemmas.v *)
Lemma induc:
 (A : Set)
 (P : (n : nat) (vect A n) -> (vect A n) ->  Prop)
 (P O (vnil A) (vnil A)) ->
 ((n : nat)
  (v : (vect A n))
  (v' : (vect A n))
  (P n v v') -> (a, a' : A)  (P (S n) (vcons A n v a) (vcons A n v' a'))) ->
 (n : nat) (v, v' : (vect A n))  (P n v v').
Intros A P Hnil Hvcons n v.
Elim v.
Intros v'.
Replace v' with (vnil A) ; [Idtac | Rewrite uniq ; Auto].
Exact Hnil.
Intros n0 v0 HR a v'.
Elim (decompose A n0 v').
Intros ve Hve ; Elim Hve ; Intros xe Heq.
Rewrite Heq.
Apply Hvcons.
Apply HR.
Qed.
 
Lemma induc2:
 (A : Set;B:Set)
 (P : (n : nat) (vect A n) -> (vect B n) ->  Prop)
 (P O (vnil A) (vnil B)) ->
 ((n : nat)
  (v : (vect A n))
  (v' : (vect B n))
  (P n v v') -> (a: A; b : B)  (P (S n) (vcons A n v a) (vcons B n v' b))) ->
 (n : nat) (v:(vect A n);v' : (vect B n))  (P n v v').
Intros A B P Hnil Hvcons n.
Elim n.
Intros v v'.
Replace v with (vnil A) ; [Idtac | Rewrite uniq ; Auto].
Replace v' with (vnil B) ; [Idtac | Rewrite uniq ; Auto].
Exact Hnil.
Intros n0 HR v v'.
Elim (decompose A n0 v).
Intros ve Hve ; Elim Hve ; Intros xe Heq.
Rewrite Heq.
Elim (decompose B n0 v').
Intros ve' Hve' ; Elim Hve' ; Intros xe' Heq'.
Rewrite Heq'.
Apply Hvcons.
Apply HR.
Qed.

(* using induc2 *)
Lemma unzip_zip3 : (A,B:Set)(n:nat)(v:(vect A n))(v':(vect B n))
 (unzip A B n (zip3 A B n v v'))=(pair ? ? v v').
Intros A B n v v'.
EApply (induc2 A  B [n:nat;v:(vect A n);v':(vect B n)](unzip A B n (zip3 A B n v v'))=(pair ? ? v v')).
Simpl; Trivial.
Intros.
Simpl.
Rewrite H; Trivial.
Qed.

(* using induc2 *)
Lemma unzip_zip2 : (A,B:Set)(n:nat)(v:(vect A n))(v':(vect B n))
 (unzip A B n (zip2 A B n v v'))=(pair ? ? v v').
Intros A B n v v'.
EApply (induc2 A  B [n:nat;v:(vect A n);v':(vect B n)](unzip A B n (zip2 A B n v v'))=(pair ? ? v v')).
Simpl; Trivial.
Intros.
Simpl.
Rewrite H; Trivial.
Qed.

(* using induc2 *)
Lemma unzip_zip : (A,B:Set)(n:nat)(v:(vect A n))(v':(vect B n))
 (unzip A B n (zip A B n v v'))=(pair ? ? v v').
Intros A B n v v'.
EApply (induc2 A  B [n:nat;v:(vect A n);v':(vect B n)](unzip A B n (zip A B n v v'))=(pair ? ? v v')).
Simpl; Trivial.
Intros.
Simpl.
Rewrite H; Trivial.
Qed.
(* one_step is not mandatory but makes the proofs clearer *)
Lemma one_step : (A,B:Set)(n0:nat)(v0:(vect A*B n0))(a: A*B)
(unzip A B (S n0) (vcons A*B n0 v0 a))=(let (x,y)=a in let (l1',l2')=
(unzip A B n0 v0) in ((vcons A n0 l1' x),(vcons B n0 l2' y))).
Intros A B n0 v0 a; Auto.
Qed.

(* by induction on v *)
Lemma zip_unzip2 : (A,B:Set)(n:nat)(v:(vect A*B n))
let (l1,l2)=(unzip A B n v) in (zip2 A B n l1 l2)=v.
Intros A B n v.
Elim v.
Simpl; Auto.
Intros n0 v0.
Intros.
Simpl.
Generalize H; Clear H.
Case  (unzip A B n0 v0).
Case a ; Intros ax ay.
Simpl.
Intros v1 v2 HR.
Rewrite HR.
Trivial.
Qed.

Lemma zip_unzip : (A,B:Set)(n:nat)(v:(vect A*B n))
let (l1,l2)=(unzip A B n v) in (zip A B n l1 l2)=v.
Intros A B n v.
Elim v.
Simpl; Auto.
Intros n0 v0.
Intros.
Rewrite one_step.
Generalize H; Clear H.
Case  (unzip A B n0 v0).
Case a ; Intros ax ay.
Simpl.
Intros v1 v2 HR.
Rewrite HR.
Trivial.
Qed.

Lemma zip3_unzip : (A,B:Set)(n:nat)(v:(vect A*B n))
let (l1,l2)=(unzip A B n v) in (zip3 A B n l1 l2)=v.
Intros A B n v.
Elim v.
Simpl; Auto.
Intros n0 v0.
Intros.
Rewrite one_step.
Generalize H; Clear H.
Case  (unzip A B n0 v0).
Case a ; Intros ax ay.
Simpl.
Intros v1 v2 HR.
Rewrite HR.
Trivial.
Qed.

(* 
   Local Variables: 
   mode: coq 
   coq-prog-name: "/usr/local/coq-7.2/bin/coqtop -emacs -q"
   compile-command: "make exp.vo"
   End:
*)
