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