Require Import ExtLib.Data.Fin.

Set Implicit Arguments.
Set Strict Implicit.
Set Asymmetric Patterns.

Section parametric.
  Variable T : Type.

  Inductive vector : nat -> Type :=
  | Vnil : vector 0
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  | Vcons : forall {n}, T -> vector n -> vector (S n).

  Definition vector_hd n (v : vector (S n)) : T :=
    match v in vector n' return match n' with
                                  | 0 => unit
                                  | S _ => T
                                end with
      | Vnil => tt
      | Vcons _ x _ => x
    end.

  Definition vector_tl n (v : vector (S n)) : vector n :=
    match v in vector n' return match n' with
                                  | 0 => unit
                                  | S n => vector n
                                end with
      | Vnil => tt
      | Vcons _ _ x => x
    end.

  Theorem vector_eta : forall n (v : vector n),
                         v = match n as n return vector n -> vector n with
                               | 0 => fun _ => Vnil
                               | S n => fun v => Vcons (vector_hd v) (vector_tl v)
                             end v.
  Proof.
(* Goal: forall (n : nat) (v : vector n), @eq (vector n) v (match n as n0 return (forall _ : vector n0, vector n0) with | O => fun _ : vector O => Vnil | S n0 => fun v0 : vector (S n0) => @Vcons n0 (@vector_hd n0 v0) (@vector_tl n0 v0) end v) *)
    destruct v; auto.
  Qed.

(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Fixpoint get {n : nat} (f : fin n) : vector n -> T :=
    match f in fin n return vector n -> T with
      | F0 n => @vector_hd _
      | FS n f => fun v => get f (vector_tl v)
    end.

(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Fixpoint put {n : nat} (f : fin n) (t : T) : vector n -> vector n :=
    match f in fin n return vector n -> vector n with
      | F0 _ => fun v => Vcons t (vector_tl v)
      | FS _ f => fun v => Vcons (vector_hd v) (put f t (vector_tl v))
    end.


(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Theorem get_put_eq : forall {n} (v : vector n) (f : fin n) val,
                         get f (put f val v) = val.
  Proof.
(* Goal: forall (n : nat) (v : vector n) (f f' : fin n) (val : T) (_ : not (@eq (fin n) f f')), @eq T (@get n f (@put n f' val v)) (@get n f v) *)
    induction n.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    { inversion f. }
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    { remember (S n). destruct f.
(* Goal: forall val : T, @eq T (@get (S n0) (@F0 n0) (@put (S n0) (@F0 n0) val v)) val *)
(* Goal: forall val : T, @eq T (@get (S n0) (@FS n0 f) (@put (S n0) (@FS n0 f) val v)) val *)
      inversion Heqn0; subst; intros; reflexivity.
      inversion Heqn0; subst; simpl; auto. }
  Qed.

(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Theorem get_put_neq : forall {n} (v : vector n) (f f' : fin n) val,
                          f <> f' ->
                          get f (put f' val v) = get f v.
  Proof.
(* Goal: forall (n : nat) (v : vector n) (f f' : fin n) (val : T) (_ : not (@eq (fin n) f f')), @eq T (@get n f (@put n f' val v)) (@get n f v) *)
    induction n.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    { inversion f. }
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    { remember (S n); destruct f.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
      { inversion Heqn0; clear Heqn0; subst; intros.
(* Goal: @eq T (@get (S n) (@F0 n) (@put (S n) f' val v)) (@get (S n) (@F0 n) v) *)
        destruct (fin_case f'); try congruence.
        destruct H0; subst. auto. }
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
      { inversion Heqn0; clear Heqn0; subst; intros.
(* Goal: @eq T (@get (S n) (@FS n f) (@put (S n) f' val v)) (@get (S n) (@FS n f) v) *)
        destruct (fin_case f').
(* Goal: @eq T (@get (S n) (@F0 n) (@put (S n) (@FS n x) val v)) (@get (S n) (@F0 n) v) *)
        subst; auto.
(* Goal: @eq T (@get (S n) (@FS n f) (@put (S n) f' val v)) (@get (S n) (@FS n f) v) *)
        destruct H0; subst. simpl.
        eapply IHn. congruence. } }
  Qed.

  Section ForallV.
    Variable P : T -> Prop.

    Inductive ForallV  : forall n, vector n -> Prop :=
    | ForallV_nil : ForallV Vnil
    | ForallV_cons : forall n e es, P e -> @ForallV n es -> ForallV (Vcons e es).

    Definition ForallV_vector_hd n (v : vector (S n)) (f : ForallV v) : P (vector_hd v) :=
      match f in @ForallV n v return match n as n return vector n -> Prop with
                                       | 0 => fun _ => True
                                       | S _ => fun v => P (vector_hd v)
                                     end v
      with
        | ForallV_nil => I
        | ForallV_cons _ _ _ pf _ => pf
      end.

    Definition ForallV_vector_tl n (v : vector (S n)) (f : ForallV v) : ForallV (vector_tl v) :=
      match f in @ForallV n v return match n as n return vector n -> Prop with
                                       | 0 => fun _ => True
                                       | S _ => fun v => ForallV (vector_tl v)
                                     end v
      with
        | ForallV_nil => I
        | ForallV_cons _ _ _ _ pf => pf
      end.
  End ForallV.

  Section vector_dec.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    Variable Tdec : forall a b : T, {a = b} + {a <> b}.

(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    Fixpoint vector_dec {n} (a : vector n)
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
      : forall b : vector n, {a = b} + {a <> b} :=
      match a in vector n
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
            return forall b : vector n, {a = b} + {a <> b}
      with
      | Vnil => fun b => left match b in vector 0 with
                                | Vnil => eq_refl
                                end
      | Vcons _ a a' => fun b =>
        match b as b in vector (S n)
              return forall a',
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
                (forall a : vector n, {a' = a} + {a' <> a}) ->
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
                {Vcons a a' = b} + {Vcons a a' <> b}
        with
        | Vcons _ b b' => fun a' rec =>
                            match Tdec a b , rec b' with
                            | left pf , left pf' =>
                              left match pf , pf' with
                                   | eq_refl , eq_refl => eq_refl
                                   end
                            | right pf , _ =>
                              right (fun x : Vcons a a' = Vcons b b' =>
                                       pf match x in _ = z
                                                return a = vector_hd z
                                          with
                                          | eq_refl => eq_refl
                                          end)
                            | left _ , right pf =>
                              right (fun x : Vcons a a' = Vcons b b' =>
                                       pf match x in _ = z
                                                return a' = vector_tl z
                                          with
                                          | eq_refl => eq_refl
                                          end)
                            end
        end a' (@vector_dec _ a')
      end.
  End vector_dec.

  Section vector_in.
    Variable a : T.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
    Inductive vector_In : forall {n}, vector n -> Prop :=
    | vHere : forall n rst, @vector_In (S n) (Vcons a rst)
    | vNext : forall n rst b, @vector_In n rst ->
                              @vector_In (S n) (Vcons b rst).
  End vector_in.

(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Lemma ForallV_vector_In : forall {n} t (vs : vector n) P,
      ForallV P vs ->
      vector_In t vs -> P t.
  Proof.
(* Goal: forall (n : nat) (t : T) (vs : vector n) (P : forall _ : T, Prop) (_ : @ForallV P n vs) (_ : @vector_In t n vs), P t *)
    induction 2.
    - eapply (ForallV_vector_hd H).
    - eapply IHvector_In. eapply (ForallV_vector_tl H).
  Qed.

End parametric.

Section vector_map.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Context {T U : Type}.
  Variable f : T -> U.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
  Fixpoint vector_map {n} (v : vector T n) : vector U n :=
    match v with
    | Vnil => Vnil _
    | Vcons _ v vs => Vcons (f v) (vector_map vs)
    end.
End vector_map.

Arguments vector T n.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
Arguments vector_hd {T n} _.
(* Goal: forall (f' : fin (S n0)) (val : T) (_ : not (@eq (fin (S n0)) (@FS n0 f) f')), @eq T (@get (S n0) (@FS n0 f) (@put (S n0) f' val v)) (@get (S n0) (@FS n0 f) v) *)
Arguments vector_tl {T n} _.
