Section Lists_replace.
Variable A : Set.
(* Remplace l'element de l a la position par new *)
Fixpoint replace (l : list A) : nat -> A -> list A :=
fun (position : nat) (new : A) =>
match l return (list A) with
| nil =>
(* nil *)  nil | x :: l' => (* (cons x l') *)
match position return (list A) with
| O =>
(* O *)  new :: l' (* (S n') *)
| S n' => x :: replace l' n' new
end
end.
(***************)
simple induction l. simpl in |- *. intros. absurd (i < 0). apply lt_n_O.
exact H.
simple induction i. intros. simpl in |- *.
unfold elemlist in |- *. rewrite strip_O. simpl in |- *. rewrite trunc_O; trivial with arith.
intros. simpl in |- *. clear H0. unfold elemlist in |- *. rewrite strip_cons_S.
rewrite elemlist_inv.
apply H. apply lt_S_n. generalize H1. simpl in |- *. trivial with arith.
Qed.

(* On montre que tous les elements, sauf celui que l'on remplace, ne sont pas modifies: *)
Lemma replace_keep_others :
forall (l : list A) (i p : nat) (x : A),
i < length l ->
p < length l -> i <> p -> elemlist A (replace l p x) i = elemlist A l i.