Lvc.Infra.Get

Require Import Arith Coq.Lists.List Setoid Coq.Lists.SetoidList.
Require Export Infra.Option EqDec AutoIndTac Util LengthEq.

Set Implicit Arguments.

Positional membership in a list


Inductive getT (X:Type) : list XnatXType :=
| getTLB xl x : getT (x::xl) 0 x
| getTLS n xl x : getT xl n xgetT (::xl) (S n) x.

Inductive get (X:Type) : list XnatXProp :=
| getLB xl x : get (x::xl) 0 x
| getLS n xl x : get xl n xget (::xl) (S n) x.

Get is informative anyway.

Lemma get_getT X (x:X) n L
  : get L n xgetT L n x.

Lemma getT_get X (x:X) n L
  : getT L n xget L n x.

Properties of get


Lemma get_functional X (xl:list X) n (x :X) :
  get xl n xget xl n x = .

Lemma get_shift X (L:list X) k L1 blk :
  get L k blkget (L1 ++ L) (length L1+k) blk.

Lemma shift_get X (L:list X) k L1 blk :
  get (L1 ++ L) (length L1+k) blkget L k blk.

Lemma get_app X (L :list X) k x
  : get L k xget (L ++ ) k x.

Lemma get_nth_default {X:Type} L n m (default:X):
get L n mnth n L default = m.

Lemma get_length {X:Type} L n (s:X)
(getl : get L n s) :
n < length L.

Lemma get_nth X L n m (d:X) :
get L n mnth n L d = m.

Lemma nth_default_nil X (v : X) x : nth_default v nil x = v.

Lemma get_nth_error X (L : list X) k x :
  get L k xnth_error L k = Some x.

Lemma nth_error_nth X (L:list X) d x n
  : nth_error L n = Some xnth n L d = x.

Lemma nth_get {X:Type} L n s {default:X}
(lt : n < length L)
(nthL : nth n L default = s):
get L n s.

Lemma nth_get_neq {X:Type} L n s {default:X}
(neq:s default)
(nthL : nth n L default = s):
get L n s.

Lemma nth_error_get X (L : list X) k x :
  nth_error L k = Some xget L k x.

Lemma nth_error_app X (L :list X) k x
 : nth_error L k = Some xnth_error (L++) k = Some x.

Lemma nth_error_shift X (L :list X) x
  : nth_error (L++(x::)) (length L) = Some x.

Lemma nth_app_shift X (L :list X) x d
  : x < length Lnth x (L++) d = nth x L d.

Ltac get_functional :=
  match goal with
    | [ H : get ?XL ?n _, : get ?XL ?n _ |- _ ] ⇒
      simplify_eq (get_functional H ); intros; clear H
    | _fail "no matching get assumptions"
  end.

Ltac eval_nth_get :=
  match goal with
    | [ H : get ?XL ?n _ |- (bind (nth_error ?XL ?n) _) = _ ] ⇒
        rewrite (get_nth_error H); simpl
    | _fail "no matching get assumptions"
  end.

Lemma get_dec {X} (L:list X) n
      : { x | get L n x } + { x, get L n xFalse }.

Hint Constructors get : get.
Hint Resolve get_functional : get.
Hint Resolve get_shift : get.
Hint Resolve nth_error_get : get.

Ltac simplify_get := try repeat get_functional; repeat eval_nth_get; eauto with get.

Lemma nth_shift X x (L :list X) d
  : nth (length L+x) (L++) d = nth x d.

Lemma get_range X (L:list X) n v
  : get L n vn < length L.

Lemma get_in_range X (L:list X) n
  : n < length L{ x:X & get L n x }.

Lemma nth_in X L x (d:X)
  : x < length LIn (nth x L d) L.

In and get

Lemma in_get X `{EqDec X eq} (xl : list X) (x : X) :
  In x xl{ n : nat & get xl n x }.

Lemma get_in X `{EqDec X eq} (xl : list X) (x : X) n :
  get xl n xIn x xl.

Some helpful tactics

Lemma list_map_eq X Y Z (f:XZ) g (L:list X) (:list Y) n x
  : List.map f L = List.map g
    → get L n x y, get n y f x = g y.

Lemma map_get_1 X Y (L:list X) (f:XY) n x
  : get L n xget (List.map f L) n (f x).

Lemma map_get_2 X Y (L:list X) (f:XY) n x
  : get (List.map f L) n x : X, get L n .

Lemma map_get_3 X Y (L:list X) (f:XY) n x
  : getT (List.map f L) n x{ : X & (getT L n × (f = x))%type }.

Lemma map_get_4 X Y (L:list X) (f:XY) n x
  : get (List.map f L) n x{ : X | get L n f = x }.

Lemma map_get X Y (L:list X) (f:XY) n blk Z
  : get L n blk
  → get (List.map f L) n Z
  → Z = f blk.

Lemma get_length_eq X Y (L:list X) (:list Y) n x
  : get L n xlength L = length y, get n y.

Require Compare_dec.

Lemma get_in_range_app X L n (x:X)
  : n < length Lget (L ++ ) n xget L n x.

Lemma get_subst X (L :list X) x n
  : get (L ++ x :: ) n
    → get L n
        (x = n = length L)
        (get (n - S (length L)) n > length L).

Lemma get_app_cases X (L :list X) n
  : get (L ++ ) n
    → get L n
        (get (n - length L) n length L).

Lemma get_app_right X (L :list X) n x
  : = (length + n)get L n xget ( ++ L) x.

Lemma get_length_app X (L :list X) x
  : get (L ++ x :: ) (length L) x.

Lemma get_app_le X (L :list X) n x (LE:n < length L)
  : get (L ++ ) n xget L n x.

Lemma tl_map X Y L (f:XY)
  : List.map f (tl L) = tl (List.map f L).

Some further helpful lemmata
Lemma nth_error_default {X:Type} v E a (x : X) : Some v = nth_error E av = nth_default x E a.