]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/basics/lists/list.ma
c89bb78560b5ac1b1a9dbb061a372e85318afd9a
[helm.git] / matita / matita / lib / basics / lists / list.ma
1 (*
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.                     
5     ||I||                                                                 
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_______________________________________________________________ *)
11
12 include "basics/types.ma".
13 include "arithmetics/nat.ma".
14
15 inductive list (A:Type[0]) : Type[0] :=
16   | nil: list A
17   | cons: A -> list A -> list A.
18
19 notation "hvbox(hd break :: tl)"
20   right associative with precedence 47
21   for @{'cons $hd $tl}.
22
23 notation "[ list0 x sep ; ]"
24   non associative with precedence 90
25   for ${fold right @'nil rec acc @{'cons $x $acc}}.
26
27 notation "hvbox(l1 break @ l2)"
28   right associative with precedence 47
29   for @{'append $l1 $l2 }.
30
31 interpretation "nil" 'nil = (nil ?).
32 interpretation "cons" 'cons hd tl = (cons ? hd tl).
33
34 definition is_nil: ∀A:Type[0].list A → Prop ≝
35  λA.λl.match l with [ nil ⇒ True | cons hd tl ⇒ False ].
36
37 theorem nil_cons:
38   ∀A:Type[0].∀l:list A.∀a:A. a::l ≠ [].
39   #A #l #a @nmk #Heq (change with (is_nil ? (a::l))) >Heq //
40 qed.
41
42 (*
43 let rec id_list A (l: list A) on l :=
44   match l with
45   [ nil => []
46   | (cons hd tl) => hd :: id_list A tl ]. *)
47
48 let rec append A (l1: list A) l2 on l1 ≝ 
49   match l1 with
50   [ nil ⇒  l2
51   | cons hd tl ⇒  hd :: append A tl l2 ].
52
53 definition hd ≝ λA.λl: list A.λd:A.
54   match l with [ nil ⇒ d | cons a _ ⇒ a].
55
56 definition tail ≝  λA.λl: list A.
57   match l with [ nil ⇒  [] | cons hd tl ⇒  tl].
58   
59 definition option_hd ≝ 
60   λA.λl:list A. match l with
61   [ nil ⇒ None ?
62   | cons a _ ⇒ Some ? a ].
63
64 interpretation "append" 'append l1 l2 = (append ? l1 l2).
65
66 theorem append_nil: ∀A.∀l:list A.l @ [] = l.
67 #A #l (elim l) normalize // qed.
68
69 theorem associative_append: 
70  ∀A.associative (list A) (append A).
71 #A #l1 #l2 #l3 (elim l1) normalize // qed.
72
73 theorem append_cons:∀A.∀a:A.∀l,l1.l@(a::l1)=(l@[a])@l1.
74 #A #a #l #l1 >associative_append // qed.
75
76 theorem nil_append_elim: ∀A.∀l1,l2: list A.∀P:?→?→Prop. 
77   l1@l2=[] → P (nil A) (nil A) → P l1 l2.
78 #A #l1 #l2 #P (cases l1) normalize //
79 #a #l3 #heq destruct
80 qed.
81
82 theorem nil_to_nil:  ∀A.∀l1,l2:list A.
83   l1@l2 = [] → l1 = [] ∧ l2 = [].
84 #A #l1 #l2 #isnil @(nil_append_elim A l1 l2) /2/
85 qed.
86
87 (**************************** iterators ******************************)
88
89 let rec map (A,B:Type[0]) (f: A → B) (l:list A) on l: list B ≝
90  match l with [ nil ⇒ nil ? | cons x tl ⇒ f x :: (map A B f tl)].
91
92 lemma map_append : ∀A,B,f,l1,l2.
93   (map A B f l1) @ (map A B f l2) = map A B f (l1@l2).
94 #A #B #f #l1 elim l1
95 [ #l2 @refl
96 | #h #t #IH #l2 normalize //
97 ] qed.
98   
99 let rec foldr (A,B:Type[0]) (f:A → B → B) (b:B) (l:list A) on l :B ≝  
100  match l with [ nil ⇒ b | cons a l ⇒ f a (foldr A B f b l)].
101  
102 definition filter ≝ 
103   λT.λp:T → bool.
104   foldr T (list T) (λx,l0.if p x then x::l0 else l0) (nil T).
105
106 (* compose f [a1;...;an] [b1;...;bm] = 
107   [f a1 b1; ... ;f an b1; ... ;f a1 bm; f an bm] *)
108  
109 definition compose ≝ λA,B,C.λf:A→B→C.λl1,l2.
110     foldr ?? (λi,acc.(map ?? (f i) l2)@acc) [ ] l1.
111
112 lemma filter_true : ∀A,l,a,p. p a = true → 
113   filter A p (a::l) = a :: filter A p l.
114 #A #l #a #p #pa (elim l) normalize >pa normalize // qed.
115
116 lemma filter_false : ∀A,l,a,p. p a = false → 
117   filter A p (a::l) = filter A p l.
118 #A #l #a #p #pa (elim l) normalize >pa normalize // qed.
119
120 theorem eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
121 #A #B #f #g #l #eqfg (elim l) normalize // qed.
122
123 (**************************** reverse *****************************)
124 let rec rev_append S (l1,l2:list S) on l1 ≝
125   match l1 with 
126   [ nil ⇒ l2
127   | cons a tl ⇒ rev_append S tl (a::l2)
128   ]
129 .
130
131 definition reverse ≝λS.λl.rev_append S l [].
132
133 lemma reverse_single : ∀S,a. reverse S [a] = [a]. 
134 // qed.
135
136 lemma rev_append_def : ∀S,l1,l2. 
137   rev_append S l1 l2 = (reverse S l1) @ l2 .
138 #S #l1 elim l1 normalize // 
139 qed.
140
141 lemma reverse_cons : ∀S,a,l. reverse S (a::l) = (reverse S l)@[a].
142 #S #a #l whd in ⊢ (??%?); // 
143 qed.
144
145 lemma reverse_append: ∀S,l1,l2. 
146   reverse S (l1 @ l2) = (reverse S l2)@(reverse S l1).
147 #S #l1 elim l1 [normalize // | #a #tl #Hind #l2 >reverse_cons
148 >reverse_cons // qed.
149
150 lemma reverse_reverse : ∀S,l. reverse S (reverse S l) = l.
151 #S #l elim l // #a #tl #Hind >reverse_cons >reverse_append 
152 normalize // qed.
153
154 (* an elimination principle for lists working on the tail;
155 useful for strings *)
156 lemma list_elim_left: ∀S.∀P:list S → Prop. P (nil S) →
157 (∀a.∀tl.P tl → P (tl@[a])) → ∀l. P l.
158 #S #P #Pnil #Pstep #l <(reverse_reverse … l) 
159 generalize in match (reverse S l); #l elim l //
160 #a #tl #H >reverse_cons @Pstep //
161 qed.
162
163 (**************************** length ******************************)
164
165 let rec length (A:Type[0]) (l:list A) on l ≝ 
166   match l with 
167     [ nil ⇒ 0
168     | cons a tl ⇒ S (length A tl)].
169
170 notation "|M|" non associative with precedence 65 for @{'norm $M}.
171 interpretation "norm" 'norm l = (length ? l).
172
173 lemma length_tail: ∀A,l. length ? (tail A l) = pred (length ? l).
174 #A #l elim l // 
175 qed.
176
177 lemma length_append: ∀A.∀l1,l2:list A. 
178   |l1@l2| = |l1|+|l2|.
179 #A #l1 elim l1 // normalize /2/
180 qed.
181
182 lemma length_map: ∀A,B,l.∀f:A→B. length ? (map ?? f l) = length ? l.
183 #A #B #l #f elim l // #a #tl #Hind normalize //
184 qed.
185
186 (****************************** nth ********************************)
187 let rec nth n (A:Type[0]) (l:list A) (d:A)  ≝  
188   match n with
189     [O ⇒ hd A l d
190     |S m ⇒ nth m A (tail A l) d].
191
192 lemma nth_nil: ∀A,a,i. nth i A ([]) a = a.
193 #A #a #i elim i normalize //
194 qed.
195
196 (****************************** nth_opt ********************************)
197 let rec nth_opt (A:Type[0]) (n:nat) (l:list A) on l : option A ≝
198 match l with
199 [ nil ⇒ None ?
200 | cons h t ⇒ match n with [ O ⇒ Some ? h | S m ⇒ nth_opt A m t ]
201 ].
202
203 (**************************** All *******************************)
204
205 let rec All (A:Type[0]) (P:A → Prop) (l:list A) on l : Prop ≝
206 match l with
207 [ nil ⇒ True
208 | cons h t ⇒ P h ∧ All A P t
209 ].
210
211 lemma All_mp : ∀A,P,Q. (∀a.P a → Q a) → ∀l. All A P l → All A Q l.
212 #A #P #Q #H #l elim l normalize //
213 #h #t #IH * /3/
214 qed.
215
216 lemma All_nth : ∀A,P,n,l.
217   All A P l →
218   ∀a.
219   nth_opt A n l = Some A a →
220   P a.
221 #A #P #n elim n
222 [ * [ #_ #a #E whd in E:(??%?); destruct
223     | #hd #tl * #H #_ #a #E whd in E:(??%?); destruct @H
224     ]
225 | #m #IH *
226   [ #_ #a #E whd in E:(??%?); destruct
227   | #hd #tl * #_ whd in ⊢ (? → ∀_.??%? → ?); @IH
228   ]
229 ] qed.
230
231 (**************************** Exists *******************************)
232
233 let rec Exists (A:Type[0]) (P:A → Prop) (l:list A) on l : Prop ≝
234 match l with
235 [ nil ⇒ False
236 | cons h t ⇒ (P h) ∨ (Exists A P t)
237 ].
238
239 lemma Exists_append : ∀A,P,l1,l2.
240   Exists A P (l1 @ l2) → Exists A P l1 ∨ Exists A P l2.
241 #A #P #l1 elim l1
242 [ normalize /2/
243 | #h #t #IH #l2 *
244   [ #H /3/
245   | #H cases (IH l2 H) /3/
246   ]
247 ] qed.
248
249 lemma Exists_append_l : ∀A,P,l1,l2.
250   Exists A P l1 → Exists A P (l1@l2).
251 #A #P #l1 #l2 elim l1
252 [ *
253 | #h #t #IH *
254   [ #H %1 @H
255   | #H %2 @IH @H
256   ]
257 ] qed.
258
259 lemma Exists_append_r : ∀A,P,l1,l2.
260   Exists A P l2 → Exists A P (l1@l2).
261 #A #P #l1 #l2 elim l1
262 [ #H @H
263 | #h #t #IH #H %2 @IH @H
264 ] qed.
265
266 lemma Exists_add : ∀A,P,l1,x,l2. Exists A P (l1@l2) → Exists A P (l1@x::l2).
267 #A #P #l1 #x #l2 elim l1
268 [ normalize #H %2 @H
269 | #h #t #IH normalize * [ #H %1 @H | #H %2 @IH @H ]
270 qed.
271
272 lemma Exists_mid : ∀A,P,l1,x,l2. P x → Exists A P (l1@x::l2).
273 #A #P #l1 #x #l2 #H elim l1
274 [ %1 @H
275 | #h #t #IH %2 @IH
276 ] qed.
277
278 lemma Exists_map : ∀A,B,P,Q,f,l.
279 Exists A P l →
280 (∀a.P a → Q (f a)) →
281 Exists B Q (map A B f l).
282 #A #B #P #Q #f #l elim l //
283 #h #t #IH * [ #H #F %1 @F @H | #H #F %2 @IH [ @H | @F ] ] qed.
284
285 lemma Exists_All : ∀A,P,Q,l.
286   Exists A P l →
287   All A Q l →
288   ∃x. P x ∧ Q x.
289 #A #P #Q #l elim l [ * | #hd #tl #IH * [ #H1 * #H2 #_ %{hd} /2/ | #H1 * #_ #H2 @IH // ]
290 qed.
291
292 (**************************** fold *******************************)
293
294 let rec fold (A,B:Type[0]) (op:B → B → B) (b:B) (p:A→bool) (f:A→B) (l:list A) on l :B ≝  
295  match l with 
296   [ nil ⇒ b 
297   | cons a l ⇒
298      if p a then op (f a) (fold A B op b p f l)
299      else fold A B op b p f l].
300       
301 notation "\fold  [ op , nil ]_{ ident i ∈ l | p} f"
302   with precedence 80
303 for @{'fold $op $nil (λ${ident i}. $p) (λ${ident i}. $f) $l}.
304
305 notation "\fold [ op , nil ]_{ident i ∈ l } f"
306   with precedence 80
307 for @{'fold $op $nil (λ${ident i}.true) (λ${ident i}. $f) $l}.
308
309 interpretation "\fold" 'fold op nil p f l = (fold ? ? op nil p f l).
310
311 theorem fold_true: 
312 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f:A→B. p a = true → 
313   \fold[op,nil]_{i ∈ a::l| p i} (f i) = 
314     op (f a) \fold[op,nil]_{i ∈ l| p i} (f i). 
315 #A #B #a #l #p #op #nil #f #pa normalize >pa // qed.
316
317 theorem fold_false: 
318 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f.
319 p a = false → \fold[op,nil]_{i ∈ a::l| p i} (f i) = 
320   \fold[op,nil]_{i ∈ l| p i} (f i).
321 #A #B #a #l #p #op #nil #f #pa normalize >pa // qed.
322
323 theorem fold_filter: 
324 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f:A →B.
325   \fold[op,nil]_{i ∈ l| p i} (f i) = 
326     \fold[op,nil]_{i ∈ (filter A p l)} (f i).
327 #A #B #a #l #p #op #nil #f elim l //  
328 #a #tl #Hind cases(true_or_false (p a)) #pa 
329   [ >filter_true // > fold_true // >fold_true //
330   | >filter_false // >fold_false // ]
331 qed.
332
333 record Aop (A:Type[0]) (nil:A) : Type[0] ≝
334   {op :2> A → A → A; 
335    nill:∀a. op nil a = a; 
336    nilr:∀a. op a nil = a;
337    assoc: ∀a,b,c.op a (op b c) = op (op a b) c
338   }.
339
340 theorem fold_sum: ∀A,B. ∀I,J:list A.∀nil.∀op:Aop B nil.∀f.
341   op (\fold[op,nil]_{i∈I} (f i)) (\fold[op,nil]_{i∈J} (f i)) =
342     \fold[op,nil]_{i∈(I@J)} (f i).
343 #A #B #I #J #nil #op #f (elim I) normalize 
344   [>nill //|#a #tl #Hind <assoc //]
345 qed.
346
347 (********************** lhd and ltl ******************************)
348
349 let rec lhd (A:Type[0]) (l:list A) n on n ≝ match n with
350    [ O   ⇒ nil …
351    | S n ⇒ match l with [ nil ⇒ nil … | cons a l ⇒ a :: lhd A l n ]
352    ].
353
354 let rec ltl (A:Type[0]) (l:list A) n on n ≝ match n with
355    [ O   ⇒ l
356    | S n ⇒ ltl A (tail … l) n
357    ].
358
359 lemma lhd_nil: ∀A,n. lhd A ([]) n = [].
360 #A #n elim n //
361 qed.
362
363 lemma ltl_nil: ∀A,n. ltl A ([]) n = [].
364 #A #n elim n normalize //
365 qed.
366
367 lemma lhd_cons_ltl: ∀A,n,l. lhd A l n @ ltl A l n = l.
368 #A #n elim n -n //
369 #n #IHn #l elim l normalize //
370 qed.
371
372 lemma length_ltl: ∀A,n,l. |ltl A l n| = |l| - n.
373 #A #n elim n -n /2/
374 #n #IHn *; normalize /2/
375 qed.
376
377 (********************** find ******************************)
378 let rec find (A,B:Type[0]) (f:A → option B) (l:list A) on l : option B ≝
379 match l with
380 [ nil ⇒ None B
381 | cons h t ⇒
382     match f h with
383     [ None ⇒ find A B f t
384     | Some b ⇒ Some B b
385     ]
386 ].
387
388 (********************** position_of ******************************)
389 let rec position_of_aux (A:Type[0]) (found: A → bool) (l:list A) (acc:nat) on l : option nat ≝
390 match l with
391 [ nil ⇒ None ?
392 | cons h t ⇒
393    match found h with [true ⇒ Some … acc | false ⇒ position_of_aux … found t (S acc)]].
394
395 definition position_of: ∀A:Type[0]. (A → bool) → list A → option nat ≝
396  λA,found,l. position_of_aux A found l 0.
397
398
399 (********************** make_list ******************************)
400 let rec make_list (A:Type[0]) (a:A) (n:nat) on n : list A ≝
401 match n with
402 [ O ⇒ [ ]
403 | S m ⇒ a::(make_list A a m)
404 ].