Require Export Omega.
Section def.
Variable A : Set.
 
Inductive vect : nat ->  Set :=
  vnil: (vect O)
 | vcons: (n : nat) (vect n) -> A ->  (vect (S n)) .
End def.
 
Fixpoint unzip [A,B:Set;n:nat;v:(vect (prod A B) n)]: (prod (vect A n) (vect B n)) :=
  <[n:nat][v:(vect (prod A B) n)](prod (vect A n) (vect B n))>Cases v of
    vnil => (pair (vect A O) (vect B O) (vnil A) (vnil B))
  | (vcons n' v' (pair x y)) => let (l1,l2) = (unzip A B n' v') in  
(pair (vect A  (S n')) (vect B (S n')) (vcons A n' l1 x) (vcons B n' l2 y))
  end.
 
Definition input1 := (vnil Z * Z).
 
Definition input2 := (vcons Z * Z O input1 (ZERO, ZERO)).
 
Definition input3 :=
   (vcons Z * Z (S O) input2 ((POS (xO xH)), (POS (xO (xO xH))))).
 
Definition input4 :=
   (vcons Z * Z (S (S O)) input3 ((POS xH), (POS (xI xH)))).
Eval Compute in input4.
Eval Compute in (unzip Z Z (S (S (S O))) input4).
(*# let rec unzip l = match l with [] ->[],[] | (x,y)::xs -> 
  let (l1,l2)=(unzip xs) in (x::l1,y::l2);;
  val unzip : ('a * 'b) list -> 'a list * 'b list = <fun>
  # unzip [(1,3); (2,4); (0,0)];;
  - : int list * int list = [1; 2; 0], [3; 4; 0]
  # *)
 
Definition zip:
 (A, B : Set) (n : nat) (vect A n) -> (vect B n) ->  (vect A * B n).
Intros A B.
(* n v v'.*)
Fix 1.
Intros n; Case n.
Intros; Exact (vnil A * B).
Intros n0.
Intros v; Inversion v.
Intros v'; Inversion v' in H0.
Apply vcons.
Apply zip.
Exact H0.
Exact H3.
Exact <A, B> (H1, H4).
Defined.
Eval Compute in (let (l1,l2)=(unzip Z Z (3) input4) in (zip Z Z (3) l1 l2)).
(* Reduction only works if terms are transparent *)
 
Definition eq_add_S_tr : (n, m : nat) (S n) = (S m) ->  n = m :=
   [n, m : nat] [H : (S n) = (S m)]  (f_equal nat nat pred (S n) (S m) H).
 
Fixpoint zip2 [A : Set; B : Set; n : nat] :
 (vect A n) -> (vect B n) ->  (vect A * B n) :=
 <[n : nat] (vect A n) -> (vect B n) ->  (vect A * B n)> Cases n of
     O => [_ : ?] [_ : ?]  (vnil A * B)
    | (S n0) =>
        [v : (vect A (S n0))]
         (<[n : nat] n = (S n0) -> (vect B (S n0)) ->  (vect A * B (S n0))>
            Cases v of
              vnil =>
                [H0 : O = (S n0)]
                 (False_rec (vect B (S n0)) ->  (vect A * B (S n0)) (O_S n0 H0))
             | (vcons n1 v1 x1) =>
                 [H2 : (S n1) = (S n0)]
                 [v' : (vect B (S n0))]
                  (<[n : nat] n = (S n0) ->  (vect A * B (S n0))> Cases v' of
                       vnil =>
                         [H3 : O = (S n0)]
                          (False_rec (vect A * B (S n0)) (O_S n0 H3))
                      | (vcons n1' v1' x1') =>
                          [H6 : (S n1') = (S n0)]
                           (vcons
                            A * B n0
                            (zip2
                             A B n0
                             (eq_rec
                              nat n1 [n : nat]  (vect A n) v1 n0
                              (eq_add_S_tr n1 n0 H2))
                             (eq_rec
                              nat n1' [n : nat]  (vect B n) v1' n0
                              (eq_add_S_tr n1' n0 H6))) (x1, x1'))
                   end (refl_equal nat (S n0)))
          end (refl_equal nat (S n0)))
 end.
 
Eval Compute in (let (l1,l2)=(unzip Z Z (3) input4) in (zip2 Z Z (3) l1 l2)).
 
Fixpoint zip3 [A : Set; B : Set; n : nat; v : (vect A n)] :
 (vect B n) ->  (vect A * B n) :=
 <[k : nat] (vect B k) ->  (vect A * B k)> Cases v of
     vnil => [v' : ?]  (vnil A * B)
    | (vcons n1 v1 x1) =>
        [v' : (vect B (S n1))]
         (<[k : nat] [v' : (vect B k)] k = (S n1) ->  (vect A * B k)>
            Cases v' of
              vnil => [h : ?]  (vnil A * B)
             | (vcons n2 v2 x2) =>
                 [h : ?]
                  (vcons
                   A * B n2
                   (zip3
                    A B n2
                    (eq_rec
                     nat n1 [n : nat]  (vect A n) v1 n2
                     (eq_add_S_tr n1 n2 (sym_eq ? ? ? h))) v2) (x1, x2))
          end (refl_equal nat (S n1)))
 end.
 
Eval Compute in (let (l1,l2)=(unzip Z Z (3) input4) in (zip3 Z Z (3) l1 l2)).
 
Fixpoint map [A : Set; B : Set; f : A ->  B; n : nat; v : (vect A n)] :
 (vect B n) :=
 <[k : nat] [v : (vect A k)]  (vect B k)> Cases v of
     vnil => (vnil B)
    | (vcons n0 v0 x0) => (vcons B n0 (map A B f n0 v0) (f x0))
 end.
 
Definition Svect := [A : Set]  (sigS nat (vect A)).
Section essai.
Variable A : Set.
 
Definition SVlt': (sigS nat (vect A)) -> (sigS nat (vect A)) ->  Prop.
Intros v v'.
Elim v.
Elim v'.
Intros.
Exact (lt x x0).
Defined.
 
Definition SVlt :=
   [v, v' : (sigS nat (vect A))]
    (lt Cases v of (existS n _) => n end Cases v' of (existS n _) => n end).

End essai.

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

