aboutsummaryrefslogtreecommitdiff
path: root/theories/Lists
diff options
context:
space:
mode:
authorletouzey2006-03-15 10:22:27 +0000
committerletouzey2006-03-15 10:22:27 +0000
commit150d190dfc60e462dfacafcfed3cabb58ff95365 (patch)
treec650e4f52de0d687b412b4f251d85484e90372b0 /theories/Lists
parenta2cc7cc4e0aba3bd60129f4352926f5512cd8bf6 (diff)
Ajout de theories/FSets contenant la partie "light" de FSets et FMap:
pas d'implementations par AVL, mais celles par lists, ainsi que les foncteurs de proprietes. Au passage, ajout de MoreList (complements de List) et SetoidList (quelques relations sur des listes considerees modulo un eq ou lt non standard. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/coq/trunk@8628 85f007b7-540e-0410-9357-904b9bb8a0f7
Diffstat (limited to 'theories/Lists')
-rw-r--r--theories/Lists/MoreList.v455
-rw-r--r--theories/Lists/SetoidList.v237
2 files changed, 692 insertions, 0 deletions
diff --git a/theories/Lists/MoreList.v b/theories/Lists/MoreList.v
new file mode 100644
index 0000000000..fd264c4231
--- /dev/null
+++ b/theories/Lists/MoreList.v
@@ -0,0 +1,455 @@
+(***********************************************************************)
+(* v * The Coq Proof Assistant / The Coq Development Team *)
+(* <O___,, * INRIA-Rocquencourt & LRI-CNRS-Orsay *)
+(* \VV/ *************************************************************)
+(* // * This file is distributed under the terms of the *)
+(* * GNU Lesser General Public License Version 2.1 *)
+(***********************************************************************)
+
+(* $Id: MoreList.v,v 1.4 2006/03/13 04:59:24 letouzey Exp $ *)
+
+(** This file contains some complements to [List.v], in particular
+ results about lengths of the different lists operations (but not only)
+*)
+
+Require Export List.
+Require Import Arith.
+Require Import Bool.
+Set Implicit Arguments.
+Unset Strict Implicit.
+
+Section MoreLists.
+
+Variable A B C:Set.
+
+Implicit Types l : list A.
+
+
+Section Nth.
+
+Lemma nth_overflow : forall l n d, length l <= n -> nth n l d = d.
+Proof.
+induction l; destruct n; simpl; intros; auto.
+inversion H.
+apply IHl; auto with arith.
+Qed.
+
+Lemma nth_indep :
+ forall l n d d', n < length l -> nth n l d = nth n l d'.
+Proof.
+induction l; simpl; intros; auto.
+inversion H.
+destruct n; simpl; auto with arith.
+Qed.
+
+End Nth.
+
+Section App.
+
+Lemma app_length : forall l l', length (l++l') = length l + length l'.
+Proof.
+induction l; simpl; auto.
+Qed.
+
+Lemma app_nth1 :
+ forall l l' d n, n < length l -> nth n (l++l') d = nth n l d.
+Proof.
+induction l.
+intros.
+inversion H.
+intros l' d n.
+case n; simpl; auto.
+intros; rewrite IHl; auto with arith.
+Qed.
+
+Lemma app_nth2 :
+ forall l l' d n, n >= length l -> nth n (l++l') d = nth (n-length l) l' d.
+Proof.
+induction l.
+intros.
+simpl.
+rewrite <- minus_n_O; auto.
+intros l' d n.
+case n; simpl; auto.
+intros.
+inversion H.
+intros.
+rewrite IHl; auto with arith.
+Qed.
+
+End App.
+
+Section Fold.
+
+Lemma fold_left_length :
+ forall l, fold_left (fun x _ => S x) l 0 = length l.
+Proof.
+cut (forall l n, fold_left (fun x _ => S x) l n = n + length l).
+intros.
+exact (H l 0).
+induction l; simpl; auto.
+intros; rewrite IHl.
+simpl; auto with arith.
+Qed.
+
+Lemma fold_left_app : forall (l l':list B)(f : A -> B -> A)(i:A),
+ fold_left f (l++l') i = fold_left f l' (fold_left f l i).
+Proof.
+induction l.
+simpl; auto.
+intros.
+simpl.
+auto.
+Qed.
+
+Lemma fold_right_app : forall l l' (f:A->B->B)(i:B),
+ fold_right f i (l++l') = fold_right f (fold_right f i l') l.
+Proof.
+induction l.
+simpl; auto.
+simpl; intros.
+f_equal; auto.
+Qed.
+
+Lemma fold_left_rev_right : forall l (f:A->B->B)(i:B),
+ fold_right f i (rev l) = fold_left (fun x y => f y x) l i.
+Proof.
+induction l.
+simpl; auto.
+intros.
+simpl.
+rewrite fold_right_app; simpl.
+auto.
+Qed.
+
+End Fold.
+
+Section Rev.
+
+Lemma In_rev : forall l x, In x l <-> In x (rev l).
+Proof.
+induction l.
+simpl; intuition.
+intros.
+simpl.
+intuition.
+subst.
+apply in_or_app; right; simpl; auto.
+apply in_or_app; left; firstorder.
+destruct (in_app_or _ _ _ H); firstorder.
+Qed.
+
+Lemma rev_length : forall l, length (rev l) = length l.
+Proof.
+induction l;simpl; auto.
+rewrite app_length.
+rewrite IHl.
+simpl; rewrite plus_comm; auto.
+Qed.
+
+Lemma rev_nth : forall l d n, n < length l ->
+ nth n (rev l) d = nth (length l - S n) l d.
+Proof.
+induction l.
+intros; inversion H.
+intros.
+simpl in H.
+simpl (rev (a :: l)).
+simpl (length (a :: l) - S n).
+inversion H.
+rewrite <- minus_n_n; simpl.
+rewrite <- rev_length.
+rewrite app_nth2; auto.
+rewrite <- minus_n_n; auto.
+rewrite app_nth1; auto.
+rewrite (minus_plus_simpl_l_reverse (length l) n 1).
+replace (1 + length l) with (S (length l)); auto with arith.
+rewrite <- minus_Sn_m; auto with arith; simpl.
+apply IHl; auto.
+rewrite rev_length; auto.
+Qed.
+
+End Rev.
+
+Section Rev_acc.
+
+(** An alternative tail-recursive definition of [rev] *)
+
+Fixpoint rev_acc (l l': list A) {struct l} : list A :=
+ match l with
+ | nil => l'
+ | a::l => rev_acc l (a::l')
+ end.
+
+Lemma rev_acc_rev : forall l l', rev_acc l l' = rev l ++ l'.
+Proof.
+induction l; simpl; auto; intros.
+rewrite <- ass_app; firstorder.
+Qed.
+
+Lemma rev_alt : forall l, rev l = rev_acc l nil.
+Proof.
+intros; rewrite rev_acc_rev.
+apply app_nil_end.
+Qed.
+
+End Rev_acc.
+
+Section Seq.
+
+(** [seq] computes the sequence of [len] contiguous integers
+ that starts at [start]. For instance, [seq 2 3] is [2::3::4::nil]. *)
+
+Fixpoint seq (start len:nat) {struct len} : list nat :=
+ match len with
+ | 0 => nil
+ | S len => start :: seq (S start) len
+ end.
+
+Lemma seq_length : forall len start, length (seq start len) = len.
+Proof.
+induction len; simpl; auto.
+Qed.
+
+Lemma seq_nth : forall len start n d,
+ n < len -> nth n (seq start len) d = start+n.
+Proof.
+induction len; intros.
+inversion H.
+simpl seq.
+destruct n; simpl.
+auto with arith.
+rewrite IHlen;simpl; auto with arith.
+Qed.
+
+Lemma seq_shift : forall len start,
+ map S (seq start len) = seq (S start) len.
+Proof.
+induction len; simpl; auto.
+intros.
+rewrite IHlen.
+auto with arith.
+Qed.
+
+End Seq.
+
+Section Map.
+
+Variable f : A-> B.
+
+Lemma In_map : forall l y, In y (map f l) <-> exists x, f x = y /\ In x l.
+Proof.
+induction l; firstorder (subst; auto).
+Qed.
+
+Lemma map_length : forall l, length (map f l) = length l.
+Proof.
+induction l; simpl; auto.
+Qed.
+
+Lemma map_nth : forall l d n,
+ nth n (map f l) (f d) = f (nth n l d).
+Proof.
+induction l; simpl map; destruct n; firstorder.
+Qed.
+
+Lemma map_app : forall l l',
+ map f (l++l') = (map f l) ++ (map f l').
+Proof.
+induction l; simpl; auto.
+intros; rewrite IHl; auto.
+Qed.
+
+Lemma map_rev : forall l, map f (rev l) = rev (map f l).
+Proof.
+induction l; simpl; auto.
+rewrite map_app.
+rewrite IHl; auto.
+Qed.
+
+Lemma map_map : forall (f:A->B)(g:B->C) l,
+ map g (map f l) = map (fun x => g (f x)) l.
+Proof.
+induction l; simpl; auto.
+rewrite IHl; auto.
+Qed.
+
+Lemma map_ext :
+ forall g, (forall a, f a = g a) -> forall l, map f l = map g l.
+Proof.
+induction l; simpl; auto.
+rewrite H; rewrite IHl; auto.
+Qed.
+
+End Map.
+
+Section SplitLast.
+
+(** [last l d] returns the last elements of the list [l],
+ or the default value [d] if [l] is empty. *)
+
+Fixpoint last (l:list A)(d:A) {struct l} : A :=
+ match l with
+ | nil => d
+ | a :: nil => a
+ | a :: l => last l d
+ end.
+
+(** [removelast l] remove the last element of [l] *)
+
+Fixpoint removelast (l:list A) {struct l} : list A :=
+ match l with
+ | nil => nil
+ | a :: nil => nil
+ | a :: l => a :: removelast l
+ end.
+
+Lemma app_removelast_last :
+ forall l d, l<>nil -> l = removelast l ++ (last l d :: nil).
+Proof.
+induction l.
+destruct 1; auto.
+intros d _.
+destruct l; auto.
+pattern (a0::l) at 1; rewrite IHl with d; auto; discriminate.
+Qed.
+
+Lemma exists_last :
+ forall l, l<>nil -> { l' : list A & { a : A | l = l'++a::nil}}.
+Proof.
+induction l.
+destruct 1; auto.
+intros _.
+destruct l.
+exists (@nil A); exists a; auto.
+destruct IHl as [l' (a',H)]; try discriminate.
+rewrite H.
+exists (a::l'); exists a'; auto.
+Qed.
+
+End SplitLast.
+
+Section SplitN.
+
+Fixpoint firstn (n:nat)(l:list A) {struct n} : list A :=
+ match n with
+ | 0 => nil
+ | S n => match l with
+ | nil => nil
+ | a::l => a::(firstn n l)
+ end
+ end.
+
+Fixpoint skipn (n:nat)(l:list A) { struct n } : list A :=
+ match n with
+ | 0 => l
+ | S n => match l with
+ | nil => nil
+ | a::l => skipn n l
+ end
+ end.
+
+Lemma firstn_skipn : forall n l, firstn n l ++ skipn n l = l.
+Proof.
+induction n.
+simpl; auto.
+destruct l; simpl; auto.
+f_equal; auto.
+Qed.
+
+End SplitN.
+
+Section Bool.
+
+Variable f : A -> bool.
+
+(** find whether a boolean function can be satisfied by an
+ elements of the list. *)
+
+Fixpoint existsb (l:list A) {struct l}: bool :=
+ match l with
+ | nil => false
+ | a::l => f a || existsb l
+ end.
+
+Lemma existsb_exists :
+ forall l, existsb l = true <-> exists x, In x l /\ f x = true.
+Proof.
+induction l; simpl; intuition.
+inversion H.
+firstorder.
+destruct (orb_prop _ _ H1); firstorder.
+firstorder.
+subst.
+rewrite H2; auto.
+Qed.
+
+
+Lemma existsb_nth : forall l n d, n < length l ->
+ existsb l = false -> f (nth n l d) = false.
+Proof.
+induction l.
+inversion 1.
+simpl; intros.
+destruct (orb_false_elim _ _ H0); clear H0; auto.
+destruct n ; auto.
+rewrite IHl; auto with arith.
+Qed.
+
+(** find whether a boolean function is satisfied by
+ all the elements of a list. *)
+
+Fixpoint forallb (l:list A) {struct l} : bool :=
+ match l with
+ | nil => true
+ | a::l => f a && forallb l
+ end.
+
+Lemma forallb_forall :
+ forall l, forallb l = true <-> (forall x, In x l -> f x = true).
+Proof.
+induction l; simpl; intuition.
+destruct (andb_prop _ _ H1).
+congruence.
+destruct (andb_prop _ _ H1); auto.
+assert (forallb l = true).
+apply H0; intuition.
+rewrite H1; auto.
+Qed.
+
+(** [filter] *)
+
+Fixpoint filter (l:list A) {struct l} : list A :=
+ match l with
+ | nil => nil
+ | x :: l => if f x then x::(filter l) else filter l
+ end.
+
+Lemma filter_In : forall x l, In x (filter l) <-> In x l /\ f x = true.
+Proof.
+induction l; simpl.
+intuition.
+intros.
+case_eq (f a); intros; simpl; intuition congruence.
+Qed.
+
+End Bool.
+
+End MoreLists.
+
+Hint Rewrite
+ rev_involutive (* rev (rev l) = l *)
+ rev_unit (* rev (l ++ a :: nil) = a :: rev l *)
+ map_nth (* nth n (map f l) (f d) = f (nth n l d) *)
+ map_length (* length (map f l) = length l *)
+ seq_length (* length (seq start len) = len *)
+ app_length (* length (l ++ l') = length l + length l' *)
+ rev_length (* length (rev l) = length l *)
+ : list.
+
+Hint Rewrite <-
+ app_nil_end (* l = l ++ nil *)
+ : list.
+
+Ltac simpl_list := autorewrite with list.
+Ltac ssimpl_list := autorewrite with list using simpl.
diff --git a/theories/Lists/SetoidList.v b/theories/Lists/SetoidList.v
new file mode 100644
index 0000000000..9d3581a5c8
--- /dev/null
+++ b/theories/Lists/SetoidList.v
@@ -0,0 +1,237 @@
+(***********************************************************************)
+(* v * The Coq Proof Assistant / The Coq Development Team *)
+(* <O___,, * INRIA-Rocquencourt & LRI-CNRS-Orsay *)
+(* \VV/ *************************************************************)
+(* // * This file is distributed under the terms of the *)
+(* * GNU Lesser General Public License Version 2.1 *)
+(***********************************************************************)
+
+(* $Id: Lib.v,v 1.2 2006/02/26 15:59:48 letouzey Exp $ *)
+
+Require Export List.
+Require Export MoreList.
+Require Export Sorting.
+Require Export Setoid.
+Set Implicit Arguments.
+Unset Strict Implicit.
+
+(** * Logical relations over lists with respect to a setoid equality
+ or ordering. *)
+
+(** This can be seen as a complement of predicate [lelistA] and [sort]
+ found in [Sorting]. *)
+
+Section Type_with_equality.
+Variable A : Set.
+Variable eqA : A -> A -> Prop.
+
+(** Being in a list modulo an equality relation over type [A]. *)
+
+Inductive InA (x : A) : list A -> Prop :=
+ | InA_cons_hd : forall y l, eqA x y -> InA x (y :: l)
+ | InA_cons_tl : forall y l, InA x l -> InA x (y :: l).
+
+Hint Constructors InA.
+
+(** An alternative definition of [InA]. *)
+
+Lemma InA_alt : forall x l, InA x l <-> exists y, eqA x y /\ In y l.
+Proof.
+ induction l; intuition.
+ inversion H.
+ firstorder.
+ inversion H1; firstorder.
+ firstorder; subst; auto.
+Qed.
+
+(** A list without redundancy. *)
+
+Inductive noredun : list A -> Prop :=
+ | noredun_nil : noredun nil
+ | noredun_cons : forall x l, ~ In x l -> noredun l -> noredun (x::l).
+
+
+(** Similarly, a list without redundancy modulo the equality over [A]. *)
+
+Inductive noredunA : list A -> Prop :=
+ | noredunA_nil : noredunA nil
+ | noredunA_cons : forall x l, ~ InA x l -> noredunA l -> noredunA (x::l).
+
+Hint Constructors noredunA.
+
+
+(** Results concerning lists modulo [eqA] *)
+
+Hypothesis eqA_refl : forall x, eqA x x.
+Hypothesis eqA_sym : forall x y, eqA x y -> eqA y x.
+Hypothesis eqA_trans : forall x y z, eqA x y -> eqA y z -> eqA x z.
+
+Hint Resolve eqA_refl eqA_trans.
+Hint Immediate eqA_sym.
+
+Lemma InA_eqA : forall l x y, eqA x y -> InA x l -> InA y l.
+Proof.
+ intros s x y.
+ do 2 rewrite InA_alt.
+ intros H (z,(U,V)).
+ exists z; split; eauto.
+Qed.
+Hint Immediate InA_eqA.
+
+Lemma In_InA : forall l x, In x l -> InA x l.
+Proof.
+ simple induction l; simpl in |- *; intuition.
+ subst; auto.
+Qed.
+Hint Resolve In_InA.
+
+(** Results concerning lists modulo [eqA] and [ltA] *)
+
+Variable ltA : A -> A -> Prop.
+
+Hypothesis ltA_trans : forall x y z, ltA x y -> ltA y z -> ltA x z.
+Hypothesis ltA_not_eqA : forall x y, ltA x y -> ~ eqA x y.
+Hypothesis ltA_eqA : forall x y z, ltA x y -> eqA y z -> ltA x z.
+Hypothesis eqA_ltA : forall x y z, eqA x y -> ltA y z -> ltA x z.
+
+Hint Resolve ltA_trans.
+Hint Immediate ltA_eqA eqA_ltA.
+
+Notation InfA:=(lelistA ltA).
+Notation SortA:=(sort ltA).
+
+Lemma InfA_ltA :
+ forall l x y, ltA x y -> InfA y l -> InfA x l.
+Proof.
+ intro s; case s; constructor; inversion_clear H0.
+ eapply ltA_trans; eauto.
+Qed.
+
+Lemma InfA_eqA :
+ forall l x y, eqA x y -> InfA y l -> InfA x l.
+Proof.
+ intro s; case s; constructor; inversion_clear H0; eauto.
+Qed.
+Hint Immediate InfA_ltA InfA_eqA.
+
+Lemma SortA_InfA_InA :
+ forall l x a, SortA l -> InfA a l -> InA x l -> ltA a x.
+Proof.
+ simple induction l.
+ intros; inversion H1.
+ intros.
+ inversion_clear H0; inversion_clear H1; inversion_clear H2.
+ eapply ltA_eqA; eauto.
+ eauto.
+Qed.
+
+Lemma In_InfA :
+ forall l x, (forall y, In y l -> ltA x y) -> InfA x l.
+Proof.
+ simple induction l; simpl in |- *; intros; constructor; auto.
+Qed.
+
+Lemma InA_InfA :
+ forall l x, (forall y, InA y l -> ltA x y) -> InfA x l.
+Proof.
+ simple induction l; simpl in |- *; intros; constructor; auto.
+Qed.
+
+(* In fact, this may be used as an alternative definition for InfA: *)
+
+Lemma InfA_alt :
+ forall l x, SortA l -> (InfA x l <-> (forall y, InA y l -> ltA x y)).
+Proof.
+split.
+intros; eapply SortA_InfA_InA; eauto.
+apply InA_InfA.
+Qed.
+
+Lemma SortA_noredunA : forall l, SortA l -> noredunA l.
+Proof.
+ simple induction l; auto.
+ intros x l' H H0.
+ inversion_clear H0.
+ constructor; auto.
+ intro.
+ assert (ltA x x) by eapply SortA_InfA_InA; eauto.
+ elim (ltA_not_eqA H3); auto.
+Qed.
+
+Lemma noredunA_app : forall l l', noredunA l -> noredunA l' ->
+ (forall x, InA x l -> InA x l' -> False) ->
+ noredunA (l++l').
+Proof.
+induction l; simpl; auto; intros.
+inversion_clear H.
+constructor.
+rewrite InA_alt; intros (y,(H4,H5)).
+destruct (in_app_or _ _ _ H5).
+elim H2.
+rewrite InA_alt.
+exists y; auto.
+apply (H1 a).
+auto.
+rewrite InA_alt.
+exists y; auto.
+apply IHl; auto.
+intros.
+apply (H1 x); auto.
+Qed.
+
+
+Lemma noredunA_rev : forall l, noredunA l -> noredunA (rev l).
+Proof.
+induction l.
+simpl; auto.
+simpl; intros.
+inversion_clear H.
+apply noredunA_app; auto.
+constructor; auto.
+intro H2; inversion H2.
+intros x.
+rewrite InA_alt.
+intros (x1,(H2,H3)).
+inversion_clear 1.
+destruct H0.
+apply InA_eqA with x1; eauto.
+apply In_InA.
+rewrite In_rev; auto.
+inversion H4.
+Qed.
+
+
+Lemma InA_app : forall l1 l2 x,
+ InA x (l1 ++ l2) -> InA x l1 \/ InA x l2.
+Proof.
+ induction l1; simpl in *; intuition.
+ inversion_clear H; auto.
+ elim (IHl1 l2 x H0); auto.
+Qed.
+
+ Hint Constructors lelistA sort.
+
+Lemma InfA_app : forall l1 l2 a, InfA a l1 -> InfA a l2 -> InfA a (l1++l2).
+Proof.
+ induction l1; simpl; auto.
+ inversion_clear 1; auto.
+Qed.
+
+Lemma SortA_app :
+ forall l1 l2, SortA l1 -> SortA l2 ->
+ (forall x y, InA x l1 -> InA y l2 -> ltA x y) ->
+ SortA (l1 ++ l2).
+Proof.
+ induction l1; simpl in *; intuition.
+ inversion_clear H.
+ constructor; auto.
+ apply InfA_app; auto.
+ destruct l2; auto.
+Qed.
+
+End Type_with_equality.
+
+Hint Constructors InA.
+Hint Constructors noredunA.
+Hint Constructors sort.
+Hint Constructors lelistA.