Fixpoint getSomes(A : Type)(ls : list (option A)) :=
  match ls with
    | nil => nil
    | o :: ls' =>
      match o with
        | None => getSomes ls'
        | Some x => x :: (getSomes ls')
      end
  end.

(* Specifications: *)
Lemma nth_nil: forall (A : Set)(i : nat)(def : A), nth i nil def = def.
Theorem listReplace_None_Permutation : forall (A : Set) n ls (y : A), nth n ls None = None -> Permutation (getSomes (listReplace ls n (Some y) None)) (y :: (getSomes ls)).
Theorem listReplace_getSomes_Permutation_h : forall (A : Set) l1' l2, Permutation l1' l2 -> forall l1 n2 (y : A), l1' = getSomes l1 -> nth n2 l1 None = None -> Permutation (getSomes (listReplace l1 n2 (Some y) None)) (y :: l2).
Theorem listReplace_getSomes_Permutation : forall (A : Set) l1 l2 n2 (y : A), nth n2 l1 None = None -> Permutation (getSomes l1) l2 -> Permutation (getSomes (listReplace l1 n2 (Some y) None)) (y :: l2).
Lemma nth_listReplace_ne : forall (i1 i2 : nat)(A : Set)(ls : list A)(a def : A), i1 <> i2 -> nth i1 (listReplace ls i2 a def) def = nth i1 ls def.
Theorem listReplace_length : forall (A : Set)(ls : list A)(i : nat)(a def : A), i < length ls -> length (listReplace ls i a def) = length ls.
Lemma listReplace_in_nil : forall (A : Set)(i : nat)(a1 a2 def : A), In a1 (listReplace nil i a2 def) -> a1 = a2 \/ a1 = def.
Lemma listReplace_in : forall (A : Set)(ls : list A)(a1 a2 def : A)(i : nat), In a1 (listReplace ls i a2 def) -> (In a1 ls \/ a1 = a2 \/ a1 = def).
Theorem firstn_map : forall (A B : Set)(f : A -> B)(ls : list A) n, firstn n (map f ls) = map f (firstn n ls).
Lemma firstn_app_eq : forall (A : Set)(ls1 ls2 : list A), firstn (length ls1) (ls1 ++ ls2) = ls1.
Theorem map_nth_in : forall (A B : Set)(ls : list A)(f : A -> B) i defa defb, i < length ls -> nth i (map f ls) defb = f (nth i ls defa).
Lemma flatten_app : forall (A : Set)(ls1 ls2 : list (list A)), flatten (ls1 ++ ls2) = flatten ls1 ++ flatten ls2.