X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=matita%2Fmatita%2Flib%2Fbasics%2Flists%2Flist.ma;h=ed988b760d283732144e949e2b8c189b4df46283;hb=HEAD;hp=48ee3078afa691590299a790033311db25ef64d2;hpb=ecf5a379cf6b646426f296c9a33d4ee0e5b2d04a;p=helm.git diff --git a/matita/matita/lib/basics/lists/list.ma b/matita/matita/lib/basics/lists/list.ma index 48ee3078a..ed988b760 100644 --- a/matita/matita/lib/basics/lists/list.ma +++ b/matita/matita/lib/basics/lists/list.ma @@ -11,6 +11,7 @@ include "basics/types.ma". include "arithmetics/nat.ma". +include "basics/core_notation/card_1.ma". inductive list (A:Type[0]) : Type[0] := | nil: list A @@ -92,6 +93,34 @@ lemma cons_injective_r : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → l1 = l2. #A #a1 #a2 #l1 #l2 #Heq destruct // qed. +(* option cons *) + +definition option_cons ≝ λsig.λc:option sig.λl. + match c with [ None ⇒ l | Some c0 ⇒ c0::l ]. + +lemma opt_cons_tail_expand : ∀A,l.l = option_cons A (option_hd ? l) (tail ? l). +#A * // +qed. + +(* comparing lists *) + +lemma compare_append : ∀A,l1,l2,l3,l4. l1@l2 = l3@l4 → +∃l:list A.(l1 = l3@l ∧ l4=l@l2) ∨ (l3 = l1@l ∧ l2=l@l4). +#A #l1 elim l1 + [#l2 #l3 #l4 #Heq %{l3} %2 % // @Heq + |#a1 #tl1 #Hind #l2 #l3 cases l3 + [#l4 #Heq %{(a1::tl1)} %1 % // @sym_eq @Heq + |#a3 #tl3 #l4 normalize in ⊢ (%→?); #Heq cases (Hind l2 tl3 l4 ?) + [#l * * #Heq1 #Heq2 %{l} + [%1 % // >Heq1 >(cons_injective_l ????? Heq) // + |%2 % // >Heq1 >(cons_injective_l ????? Heq) // + ] + |@(cons_injective_r ????? Heq) + ] + ] + ] +qed. + (**************************** iterators ******************************) let rec map (A,B:Type[0]) (f: A → B) (l:list A) on l: list B ≝ @@ -181,6 +210,10 @@ lemma length_tail: ∀A,l. length ? (tail A l) = pred (length ? l). #A #l elim l // qed. +lemma length_tail1 : ∀A,l.0 < |l| → |tail A l| < |l|. +#A * normalize // +qed. + lemma length_append: ∀A.∀l1,l2:list A. |l1@l2| = |l1|+|l2|. #A #l1 elim l1 // normalize /2/ @@ -200,6 +233,19 @@ lemma lenght_to_nil: ∀A.∀l:list A. #A * // #a #tl normalize #H destruct qed. +lemma lists_length_split : + ∀A.∀l1,l2:list A.(∃la,lb.(|la| = |l1| ∧ l2 = la@lb) ∨ (|la| = |l2| ∧ l1 = la@lb)). +#A #l1 elim l1 +[ #l2 %{[ ]} %{l2} % % % +| #hd1 #tl1 #IH * + [ %{[ ]} %{(hd1::tl1)} %2 % % + | #hd2 #tl2 cases (IH tl2) #x * #y * + [ * #IH1 #IH2 %{(hd2::x)} %{y} % normalize % // + | * #IH1 #IH2 %{(hd1::x)} %{y} %2 normalize % // ] + ] +] +qed. + (****************** traversing two lists in parallel *****************) lemma list_ind2 : ∀T1,T2:Type[0].∀l1:list T1.∀l2:list T2.∀P:list T1 → list T2 → Prop. @@ -316,6 +362,109 @@ lemma mem_map_forward: ∀A,B.∀f:A→B.∀a,l. ] qed. +(****************************** mem filter ***************************) +lemma mem_filter: ∀S,f,a,l. + mem S a (filter S f l) → mem S a l. +#S #f #a #l elim l [normalize //] +#b #tl #Hind normalize (cases (f b)) normalize + [* [#eqab %1 @eqab | #H %2 @Hind @H] + |#H %2 @Hind @H] +qed. + +lemma mem_filter_true: ∀S,f,a,l. + mem S a (filter S f l) → f a = true. +#S #f #a #l elim l [normalize @False_ind ] +#b #tl #Hind cases (true_or_false (f b)) #H +normalize >H normalize [2:@Hind] +* [#eqab // | @Hind] +qed. + +lemma mem_filter_l: ∀S,f,x,l. (f x = true) → mem S x l → +mem S x (filter ? f l). +#S #f #x #l #fx elim l [@False_ind] +#b #tl #Hind * + [#eqxb (filter_true ???? fx) %1 % + |#Htl cases (true_or_false (f b)) #fb + [>(filter_true ???? fb) %2 @Hind @Htl + |>(filter_false ???? fb) @Hind @Htl + ] + ] +qed. + +lemma filter_case: ∀A,p,l,x. mem ? x l → + mem ? x (filter A p l) ∨ mem ? x (filter A (λx.¬ p x) l). +#A #p #l elim l + [#x @False_ind + |#a #tl #Hind #x * + [#eqxa >eqxa cases (true_or_false (p a)) #Hcase + [%1 >(filter_true A tl a p Hcase) %1 % + |%2 >(filter_true A tl a ??) [%1 % | >Hcase %] + ] + |#memx cases (Hind … memx) -memx #memx + [%1 cases (true_or_false (p a)) #Hpa + [>(filter_true A tl a p Hpa) %2 @memx + |>(filter_false A tl a p Hpa) @memx + ] + |cases (true_or_false (p a)) #Hcase + [%2 >(filter_false A tl a) [@memx |>Hcase %] + |%2 >(filter_true A tl a) [%2 @memx|>Hcase %] + ] + ] + ] + ] +qed. + +lemma filter_length2: ∀A,p,l. |filter A p l|+|filter A (λx.¬ p x) l| = |l|. +#A #p #l elim l // +#a #tl #Hind cases (true_or_false (p a)) #Hcase + [>(filter_true A tl a p Hcase) >(filter_false A tl a ??) + [@(eq_f ?? S) @Hind | >Hcase %] + |>(filter_false A tl a p Hcase) >(filter_true A tl a ??) + [Hcase %] + ] +qed. + +(***************************** unique *******************************) +let rec unique A (l:list A) on l ≝ + match l with + [nil ⇒ True + |cons a tl ⇒ ¬ mem A a tl ∧ unique A tl]. + +lemma unique_filter : ∀S,l,f. + unique S l → unique S (filter S f l). +#S #l #f elim l // +#a #tl #Hind * +#memba #uniquetl cases (true_or_false … (f a)) #Hfa + [>(filter_true ???? Hfa) % + [@(not_to_not … memba) @mem_filter |/2/ ] + |>filter_false /2/ + ] +qed. + +lemma filter_eqb : ∀m,l. unique ? l → + (mem ? m l ∧ filter ? (eqb m) l = [m])∨(¬mem ? m l ∧ filter ? (eqb m) l = []). +#m #l elim l + [#_ %2 % [% @False_ind | //] + |#a #tl #Hind * #Hmema #Hunique + cases (Hind Hunique) + [* #Hmemm #Hind %1 % [%2 //] + >filter_false // @not_eq_to_eqb_false % #eqma @(absurd ? Hmemm) // + |* #Hmemm #Hind cases (decidable_eq_nat m a) #eqma + [%1 filter_true [2: @eq_to_eqb_true //] >Hind // + |%2 % + [@(not_to_not … Hmemm) * // #H @False_ind @(absurd … H) // + |>filter_false // @not_eq_to_eqb_false @eqma + ] + ] + ] + ] +qed. + +lemma length_filter_eqb: ∀m,l. unique ? l → + |filter ? (eqb m) l| ≤ 1. +#m #l #Huni cases (filter_eqb m l Huni) * #_ #H >H // +qed. + (***************************** split *******************************) let rec split_rev A (l:list A) acc n on n ≝ match n with @@ -472,6 +621,11 @@ lemma Allr_fwd_cons: ∀A,R,a,l. Allr A R (a::l) → Allr A R l. #A #R #a * // #a0 #l * // qed-. +lemma Allr_fwd_append_dx: ∀A,R,l1,l2. Allr A R (l1@l2) → Allr A R l2. +#A #R #l1 elim l1 -l1 // #a1 #l1 #IHl1 #l2 #H +lapply (Allr_fwd_cons … (l1@l2) H) -H /2 width=1/ +qed-. + (**************************** Exists *******************************) let rec Exists (A:Type[0]) (P:A → Prop) (l:list A) on l : Prop ≝