Lvc.Infra.MoreList

Require Import OrderedTypeEx Util List Get Computable DecSolve AllInRel.

Set Implicit Arguments.

Lemmas and tactics for lists


Lemma app_nil_eq X (L:list X) xl
  : L = xl ++ Lxl = nil.

Lemma cons_app X (x:X) xl
  : x::xl = (x::nil)++xl.

Fixpoint tabulate X (x:X) n : list X :=
  match n with
    | 0 ⇒ nil
    | S nx::tabulate x n
  end.

Section ParametricZip.
  Variables X Y Z : Type.
  Hypothesis f : XYZ : Type.

  Fixpoint zip (L:list X) (:list Y) : list Z :=
    match L, with
      | x::L, y::f x y::zip L
      | _, _nil
    end.

  Lemma zip_get L n (x:X) (y:Y)
  : get L n xget n yget (zip L ) n (f x y).

  Lemma get_zip L n (z:Z)
  : get (zip L ) n z
    → { x : X & {y : Y | get L n x get n y f x y = z } } .

  Lemma zip_tl L
    : tl (zip L ) = zip (tl L) (tl ).

End ParametricZip.


Lemma map_zip X Y Z (f: XYZ) W (g: ZW) L
: map g (zip f L ) = zip (fun x yg (f x y)) L .

Lemma zip_map_l X Y Z (f: XYZ) W (g: WX) L
: zip f (map g L) = zip (fun x yf (g x) y) L .

Lemma zip_map_r X Y Z (f: XYZ) W (g: WY) L
: zip f L (map g ) = zip (fun x yf x (g y)) L .

Lemma zip_ext X Y Z (f :XYZ) L
 : ( x y, f x y = x y) → zip f L = zip L .

Lemma zip_length X Y Z (f:XYZ) L
      : length (zip f L ) = min (length L) (length ).

Lemma zip_length2 {X Y Z} {f:XYZ} DL ZL
: length DL = length ZL
  → length (zip f DL ZL) = length DL.

Section ParametricMapIndex.
  Variables X Y : Type.
  Hypothesis f : natXY : Type.

  Fixpoint mapi_impl (n:nat) (L:list X) : list Y :=
    match L with
      | x::Lf n x::mapi_impl (S n) L
      | _nil
    end.

  Definition mapi := mapi_impl 0.

  Lemma mapi_get_impl L i y n
  : getT (mapi_impl i L) n y{ x : X & (getT L n x × (f (n+i) x = y))%type }.

  Lemma mapi_get L n y
  : get (mapi L) n y{ x : X | get L n x f n x = y }.

  Lemma mapi_length L {n}
  : length (mapi_impl n L) = length L.

End ParametricMapIndex.


Lemma map_impl_mapi X Y Z L {n} (f:natXY) (g:YZ)
 : List.map g (mapi_impl f n L) = mapi_impl (fun n xg (f n x)) n L.

Lemma map_mapi X Y Z L (f:natXY) (g:YZ)
 : List.map g (mapi f L) = mapi (fun n xg (f n x)) L.

Lemma mapi_map_ext X Y L (f:natXY) (g:XY) n
 : ( x n, g x = f n x)
   → List.map g L = mapi_impl f n L.

Lemma map_ext_get_eq X Y L (f:XY) (g:XY)
 : ( x n, get L n xg x = f x)
   → List.map g L = List.map f L.

Lemma map_ext_get X Y (R:YYProp) L (f:XY) (g:XY)
 : ( x n, get L n xR (g x) (f x))
   → PIR2 R (List.map g L) (List.map f L).

Ltac list_eqs :=
  match goal with
    | [ : ?x :: ?L = ? ++ ?L |- _ ] ⇒
      rewrite cons_app in ; eapply app_inv_tail in
    | [ H : ?L = ? ++ ?L |- _ ] ⇒
      let A := fresh "A" in
        eapply app_nil_eq in H
    | _fail "no matching assumptions"
  end.

Ltac inv_map H :=
  match type of H with
    | get (List.map ?f ?L) ?n ?x
      match goal with
        | [ : get ?L ?n ?y |- _ ] ⇒
          let EQ := fresh "EQ" in pose proof (map_get f H) as EQ; invc EQ
        | _let X := fresh "X" in let EQ := fresh "EQ" in
              pose proof (map_get_4 _ f H) as X; destruct X as [? [? EQ]]; invc EQ
      end
  end.

Lemma list_eq_get {X:Type} (L :list X) eqA n x
  : list_eq eqA L get L n x , get n eqA x .

Instance list_R_dec A (R:AAProp)
         `{ a b, Computable (R a b)} (L:list A) (:list A) :
  Computable ( n a b, get L n aget n bR a b).

Instance list_eq_computable X (R:XXProp) `{ x y, Computable (R x y)}
: (L :list X), Computable (list_eq R L ).

Ltac inv_mapi H :=
  match type of H with
    | get (mapi ?f ?L) ?n ?x
      match goal with
        | [ : get ?L ?n ?y |- _ ] ⇒
          let EQ := fresh "EQ" in pose proof (mapi_get f H) as EQ; invc EQ
        | _let X := fresh "X" in let EQ := fresh "EQ" in
              pose proof (mapi_get f _ H) as X; destruct X as [? [? EQ]]; invc EQ;
             clear_trivial_eqs
      end
  end.

Instance list_get_computable X (Y:list X) (R:XProp) `{ (x:X), Computable (R x)}
: Computable ( n y, get Y n yR y).