2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department of the University of Bologna, Italy.
8 \ / This file is distributed under the terms of the
9 \ / GNU General Public License Version 2
10 V_______________________________________________________________ *)
13 (* boolean functions over lists *)
15 include "basics/lists/list.ma".
16 include "basics/sets.ma".
17 include "basics/deqsets.ma".
19 (********* isnilb *********)
20 let rec isnilb A (l: list A) on l ≝
26 (********* search *********)
28 let rec memb (S:DeqSet) (x:S) (l: list S) on l ≝
31 | cons a tl ⇒ (x == a) ∨ memb S x tl
34 interpretation "boolean membership" 'mem a l = (memb ? a l).
36 lemma memb_hd: ∀S,a,l. memb S a (a::l) = true.
37 #S #a #l normalize >(proj2 … (eqb_true S …) (refl S a)) //
40 lemma memb_cons: ∀S,a,b,l.
41 memb S a l = true → memb S a (b::l) = true.
42 #S #a #b #l normalize cases (a==b) normalize //
45 lemma memb_single: ∀S,a,x. memb S a [x] = true → a = x.
46 #S #a #x normalize cases (true_or_false … (a==x)) #H
47 [#_ >(\P H) // |>H normalize #abs @False_ind /2/]
50 lemma memb_append: ∀S,a,l1,l2.
51 memb S a (l1@l2) = true →
52 memb S a l1= true ∨ memb S a l2 = true.
53 #S #a #l1 elim l1 normalize [#l2 #H %2 //]
54 #b #tl #Hind #l2 cases (a==b) normalize /2/
57 lemma memb_append_l1: ∀S,a,l1,l2.
58 memb S a l1= true → memb S a (l1@l2) = true.
59 #S #a #l1 elim l1 normalize
60 [normalize #le #abs @False_ind /2/
61 |#b #tl #Hind #l2 cases (a==b) normalize /2/
65 lemma memb_append_l2: ∀S,a,l1,l2.
66 memb S a l2= true → memb S a (l1@l2) = true.
67 #S #a #l1 elim l1 normalize //
68 #b #tl #Hind #l2 cases (a==b) normalize /2/
71 lemma memb_exists: ∀S,a,l.memb S a l = true →
73 #S #a #l elim l [normalize #abs @False_ind /2/]
74 #b #tl #Hind #H cases (orb_true_l … H)
75 [#eqba @(ex_intro … (nil S)) @(ex_intro … tl) >(\P eqba) //
76 |#mem_tl cases (Hind mem_tl) #l1 * #l2 #eqtl
77 @(ex_intro … (b::l1)) @(ex_intro … l2) >eqtl //
81 lemma not_memb_to_not_eq: ∀S,a,b,l.
82 memb S a l = false → memb S b l = true → a==b = false.
83 #S #a #b #l cases (true_or_false (a==b)) //
84 #eqab >(\P eqab) #H >H #abs @False_ind /2/
87 lemma memb_map: ∀S1,S2,f,a,l. memb S1 a l= true →
88 memb S2 (f a) (map … f l) = true.
89 #S1 #S2 #f #a #l elim l normalize [//]
90 #x #tl #memba cases (true_or_false (a==x))
91 [#eqx >eqx >(\P eqx) >(\b (refl … (f x))) normalize //
92 |#eqx >eqx cases (f a==f x) normalize /2/
96 lemma memb_compose: ∀S1,S2,S3,op,a1,a2,l1,l2.
97 memb S1 a1 l1 = true → memb S2 a2 l2 = true →
98 memb S3 (op a1 a2) (compose S1 S2 S3 op l1 l2) = true.
99 #S1 #S2 #S3 #op #a1 #a2 #l1 elim l1 [normalize //]
100 #x #tl #Hind #l2 #memba1 #memba2 cases (orb_true_l … memba1)
101 [#eqa1 >(\P eqa1) @memb_append_l1 @memb_map //
102 |#membtl @memb_append_l2 @Hind //
106 (**************** unicity test *****************)
108 let rec uniqueb (S:DeqSet) l on l : bool ≝
111 | cons a tl ⇒ notb (memb S a tl) ∧ uniqueb S tl
114 (* unique_append l1 l2 add l1 in fornt of l2, but preserving unicity *)
116 let rec unique_append (S:DeqSet) (l1,l2: list S) on l1 ≝
120 let r ≝ unique_append S tl l2 in
121 if memb S a r then r else a::r
124 lemma memb_unique_append: ∀S,a,l1,l2.
125 memb S a (unique_append S l1 l2) = true →
126 memb S a l1= true ∨ memb S a l2 = true.
127 #S #a #l1 elim l1 normalize [#l2 #H %2 //]
128 #b #tl #Hind #l2 cases (true_or_false … (a==b)) #Hab >Hab normalize /2/
129 cases (memb S b (unique_append S tl l2)) normalize
130 [@Hind | >Hab normalize @Hind]
133 lemma unique_append_elim: ∀S:DeqSet.∀P: S → Prop.∀l1,l2.
134 (∀x. memb S x l1 = true → P x) → (∀x. memb S x l2 = true → P x) →
135 ∀x. memb S x (unique_append S l1 l2) = true → P x.
136 #S #P #l1 #l2 #Hl1 #Hl2 #x #membx cases (memb_unique_append … membx)
140 lemma unique_append_unique: ∀S,l1,l2. uniqueb S l2 = true →
141 uniqueb S (unique_append S l1 l2) = true.
142 #S #l1 elim l1 normalize // #a #tl #Hind #l2 #uniquel2
143 cases (true_or_false … (memb S a (unique_append S tl l2)))
144 #H >H normalize [@Hind //] >H normalize @Hind //
147 lemma memb_map_inj: ∀A,B:DeqSet.∀f:A→B.∀l,a. injective A B f →
148 memb ? (f a) (map ?? f l) = true → memb ? a l = true.
149 #A #B #f #l #a #injf elim l
151 |#a1 #tl #Hind #Ha cases (orb_true_l … Ha)
152 [#eqf @orb_true_r1 @(\b ?) >(injf … (\P eqf)) //
153 |#membtl @orb_true_r2 /2/
158 lemma unique_map_inj: ∀A,B:DeqSet.∀f:A→B.∀l. injective A B f →
159 uniqueb A l = true → uniqueb B (map ?? f l) = true .
160 #A #B #f #l #injf elim l
162 |#a #tl #Hind #Htl @true_to_andb_true
163 [@sym_eq @noteq_to_eqnot @sym_not_eq
164 @(not_to_not ?? (memb_map_inj … injf …) )
165 <(andb_true_l ?? Htl) /2/
166 |@Hind @(andb_true_r ?? Htl)
171 (******************* sublist *******************)
173 λS,l1,l2.∀a. memb S a l1 = true → memb S a l2 = true.
175 lemma sublist_length: ∀S,l1,l2.
176 uniqueb S l1 = true → sublist S l1 l2 → |l1| ≤ |l2|.
178 #a #tl #Hind #l2 #unique #sub
179 cut (∃l3,l4.l2=l3@(a::l4)) [@memb_exists @sub //]
180 * #l3 * #l4 #eql2 >eql2 >length_append normalize
181 applyS le_S_S <length_append @Hind [@(andb_true_r … unique)]
182 >eql2 in sub; #sub #x #membx
183 cases (memb_append … (sub x (orb_true_r2 … membx)))
184 [#membxl3 @memb_append_l1 //
185 |#membxal4 cases (orb_true_l … membxal4)
186 [#eqxa @False_ind lapply (andb_true_l … unique)
187 <(\P eqxa) >membx normalize /2/ |#membxl4 @memb_append_l2 //
192 lemma sublist_unique_append_l1:
193 ∀S,l1,l2. sublist S l1 (unique_append S l1 l2).
194 #S #l1 elim l1 normalize [#l2 #S #abs @False_ind /2/]
196 normalize cases (true_or_false … (a==x)) #eqax >eqax
197 [<(\P eqax) cases (true_or_false (memb S a (unique_append S tl l2)))
198 [#H >H normalize // | #H >H normalize >(\b (refl … a)) //]
199 |cases (memb S x (unique_append S tl l2)) normalize
200 [/2/ |>eqax normalize /2/]
204 lemma sublist_unique_append_l2:
205 ∀S,l1,l2. sublist S l2 (unique_append S l1 l2).
206 #S #l1 elim l1 [normalize //] #x #tl #Hind normalize
207 #l2 #a cases (memb S x (unique_append S tl l2)) normalize
208 [@Hind | cases (a==x) normalize // @Hind]
211 lemma decidable_sublist:∀S,l1,l2.
212 (sublist S l1 l2) ∨ ¬(sublist S l1 l2).
214 [%1 #a normalize in ⊢ (%→?); #abs @False_ind /2/
216 [cases (true_or_false (memb S a l2)) #memba
217 [%1 whd #x #membx cases (orb_true_l … membx)
218 [#eqax >(\P eqax) // |@subtl]
219 |%2 @(not_to_not … (eqnot_to_noteq … true memba)) #H1 @H1 @memb_hd
221 |%2 @(not_to_not … subtl) #H1 #x #H2 @H1 @memb_cons //
226 (********************* filtering *****************)
228 lemma memb_filter_memb: ∀S,f,a,l.
229 memb S a (filter S f l) = true → memb S a l = true.
230 #S #f #a #l elim l [normalize //]
231 #b #tl #Hind normalize (cases (f b)) normalize
232 cases (a==b) normalize // @Hind
235 lemma uniqueb_filter : ∀S,l,f.
236 uniqueb S l = true → uniqueb S (filter S f l) = true.
238 #a #tl #Hind #Hunique cases (andb_true … Hunique)
239 #memba #uniquetl cases (true_or_false … (f a)) #Hfa
240 [>filter_true // @true_to_andb_true
241 [@sym_eq @noteq_to_eqnot @(not_to_not … (eqnot_to_noteq … (sym_eq … memba)))
242 #H @sym_eq @(memb_filter_memb … f) <H //
249 lemma filter_true: ∀S,f,a,l.
250 memb S a (filter S f l) = true → f a = true.
251 #S #f #a #l elim l [normalize #H @False_ind /2/]
252 #b #tl #Hind cases (true_or_false (f b)) #H
253 normalize >H normalize [2:@Hind]
254 cases (true_or_false (a==b)) #eqab
255 [#_ >(\P eqab) // | >eqab normalize @Hind]
258 lemma memb_filter: ∀S,f,l,x. memb S x (filter ? f l) = true →
259 memb S x l = true ∧ (f x = true).
262 lemma memb_filter_l: ∀S,f,x,l. (f x = true) → memb S x l = true →
263 memb S x (filter ? f l) = true.
264 #S #f #x #l #fx elim l normalize //
265 #b #tl #Hind cases (true_or_false (x==b)) #eqxb
266 [<(\P eqxb) >(\b (refl … x)) >fx normalize >(\b (refl … x)) normalize //
267 |>eqxb cases (f b) normalize [>eqxb normalize @Hind| @Hind]
271 (********************* exists *****************)
273 let rec exists (A:Type[0]) (p:A → bool) (l:list A) on l : bool ≝
276 | cons h t ⇒ orb (p h) (exists A p t)
279 lemma Exists_exists : ∀A,P,l.
282 #A #P #l elim l [ * | #hd #tl #IH * [ #H %{hd} @H | @IH ]