]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/basics/lists/list.ma
Dropping a coercion and some hints due to conflicts with CerCo
[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 lemma cons_injective_l : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → a1 = a2.
88 #A #a1 #a2 #l1 #l2 #Heq destruct //
89 qed.
90
91 lemma cons_injective_r : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → l1 = l2.
92 #A #a1 #a2 #l1 #l2 #Heq destruct //
93 qed.
94
95 (**************************** iterators ******************************)
96
97 let rec map (A,B:Type[0]) (f: A → B) (l:list A) on l: list B ≝
98  match l with [ nil ⇒ nil ? | cons x tl ⇒ f x :: (map A B f tl)].
99
100 lemma map_append : ∀A,B,f,l1,l2.
101   (map A B f l1) @ (map A B f l2) = map A B f (l1@l2).
102 #A #B #f #l1 elim l1
103 [ #l2 @refl
104 | #h #t #IH #l2 normalize //
105 ] qed.
106   
107 let rec foldr (A,B:Type[0]) (f:A → B → B) (b:B) (l:list A) on l :B ≝  
108  match l with [ nil ⇒ b | cons a l ⇒ f a (foldr A B f b l)].
109  
110 definition filter ≝ 
111   λT.λp:T → bool.
112   foldr T (list T) (λx,l0.if p x then x::l0 else l0) (nil T).
113
114 (* compose f [a1;...;an] [b1;...;bm] = 
115   [f a1 b1; ... ;f an b1; ... ;f a1 bm; f an bm] *)
116  
117 definition compose ≝ λA,B,C.λf:A→B→C.λl1,l2.
118     foldr ?? (λi,acc.(map ?? (f i) l2)@acc) [ ] l1.
119
120 lemma filter_true : ∀A,l,a,p. p a = true → 
121   filter A p (a::l) = a :: filter A p l.
122 #A #l #a #p #pa (elim l) normalize >pa normalize // qed.
123
124 lemma filter_false : ∀A,l,a,p. p a = false → 
125   filter A p (a::l) = filter A p l.
126 #A #l #a #p #pa (elim l) normalize >pa normalize // qed.
127
128 theorem eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
129 #A #B #f #g #l #eqfg (elim l) normalize // qed.
130
131 (**************************** reverse *****************************)
132 let rec rev_append S (l1,l2:list S) on l1 ≝
133   match l1 with 
134   [ nil ⇒ l2
135   | cons a tl ⇒ rev_append S tl (a::l2)
136   ]
137 .
138
139 definition reverse ≝λS.λl.rev_append S l [].
140
141 lemma reverse_single : ∀S,a. reverse S [a] = [a]. 
142 // qed.
143
144 lemma rev_append_def : ∀S,l1,l2. 
145   rev_append S l1 l2 = (reverse S l1) @ l2 .
146 #S #l1 elim l1 normalize // 
147 qed.
148
149 lemma reverse_cons : ∀S,a,l. reverse S (a::l) = (reverse S l)@[a].
150 #S #a #l whd in ⊢ (??%?); // 
151 qed.
152
153 lemma reverse_append: ∀S,l1,l2. 
154   reverse S (l1 @ l2) = (reverse S l2)@(reverse S l1).
155 #S #l1 elim l1 [normalize // | #a #tl #Hind #l2 >reverse_cons
156 >reverse_cons // qed.
157
158 lemma reverse_reverse : ∀S,l. reverse S (reverse S l) = l.
159 #S #l elim l // #a #tl #Hind >reverse_cons >reverse_append 
160 normalize // qed.
161
162 (* an elimination principle for lists working on the tail;
163 useful for strings *)
164 lemma list_elim_left: ∀S.∀P:list S → Prop. P (nil S) →
165 (∀a.∀tl.P tl → P (tl@[a])) → ∀l. P l.
166 #S #P #Pnil #Pstep #l <(reverse_reverse … l) 
167 generalize in match (reverse S l); #l elim l //
168 #a #tl #H >reverse_cons @Pstep //
169 qed.
170
171 (**************************** length ******************************)
172
173 let rec length (A:Type[0]) (l:list A) on l ≝ 
174   match l with 
175     [ nil ⇒ 0
176     | cons a tl ⇒ S (length A tl)].
177
178 notation "|M|" non associative with precedence 65 for @{'norm $M}.
179 interpretation "norm" 'norm l = (length ? l).
180
181 lemma length_tail: ∀A,l. length ? (tail A l) = pred (length ? l).
182 #A #l elim l // 
183 qed.
184
185 lemma length_append: ∀A.∀l1,l2:list A. 
186   |l1@l2| = |l1|+|l2|.
187 #A #l1 elim l1 // normalize /2/
188 qed.
189
190 lemma length_map: ∀A,B,l.∀f:A→B. length ? (map ?? f l) = length ? l.
191 #A #B #l #f elim l // #a #tl #Hind normalize //
192 qed.
193
194 lemma length_reverse: ∀A.∀l:list A. 
195   |reverse A l| = |l|.
196 #A #l elim l // #a #l0 #IH >reverse_cons >length_append normalize //
197 qed.
198
199 lemma lenght_to_nil: ∀A.∀l:list A.
200   |l| = 0 → l = [ ].
201 #A * // #a #tl normalize #H destruct
202 qed.
203  
204 (****************** traversing two lists in parallel *****************)
205 lemma list_ind2 : 
206   ∀T1,T2:Type[0].∀l1:list T1.∀l2:list T2.∀P:list T1 → list T2 → Prop.
207   length ? l1 = length ? l2 →
208   (P [] []) → 
209   (∀tl1,tl2,hd1,hd2. P tl1 tl2 → P (hd1::tl1) (hd2::tl2)) → 
210   P l1 l2.
211 #T1 #T2 #l1 #l2 #P #Hl #Pnil #Pcons
212 generalize in match Hl; generalize in match l2;
213 elim l1
214 [#l2 cases l2 // normalize #t2 #tl2 #H destruct
215 |#t1 #tl1 #IH #l2 cases l2
216    [normalize #H destruct
217    |#t2 #tl2 #H @Pcons @IH normalize in H; destruct // ]
218 ]
219 qed.
220
221 lemma list_cases2 : 
222   ∀T1,T2:Type[0].∀l1:list T1.∀l2:list T2.∀P:Prop.
223   length ? l1 = length ? l2 →
224   (l1 = [] → l2 = [] → P) → 
225   (∀hd1,hd2,tl1,tl2.l1 = hd1::tl1 → l2 = hd2::tl2 → P) → P.
226 #T1 #T2 #l1 #l2 #P #Hl @(list_ind2 … Hl)
227 [ #Pnil #Pcons @Pnil //
228 | #tl1 #tl2 #hd1 #hd2 #IH1 #IH2 #Hp @Hp // ]
229 qed.
230
231 (*********************** properties of append ***********************)
232 lemma append_l1_injective : 
233   ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l1 = l2.
234 #a #l1 #l2 #l3 #l4 #Hlen @(list_ind2 … Hlen) //
235 #tl1 #tl2 #hd1 #hd2 #IH normalize #Heq destruct @eq_f /2/
236 qed.
237   
238 lemma append_l2_injective : 
239   ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l3 = l4.
240 #a #l1 #l2 #l3 #l4 #Hlen @(list_ind2 … Hlen) normalize //
241 #tl1 #tl2 #hd1 #hd2 #IH normalize #Heq destruct /2/
242 qed.
243
244 lemma append_l1_injective_r :
245   ∀A.∀l1,l2,l3,l4:list A. |l3| = |l4| → l1@l3 = l2@l4 → l1 = l2.
246 #a #l1 #l2 #l3 #l4 #Hlen #Heq lapply (eq_f … (reverse ?) … Heq)
247 >reverse_append >reverse_append #Heq1
248 lapply (append_l2_injective … Heq1) [ // ] #Heq2
249 lapply (eq_f … (reverse ?) … Heq2) //
250 qed.
251   
252 lemma append_l2_injective_r : 
253   ∀A.∀l1,l2,l3,l4:list A. |l3| = |l4| → l1@l3 = l2@l4 → l3 = l4.
254 #a #l1 #l2 #l3 #l4 #Hlen #Heq lapply (eq_f … (reverse ?) … Heq)
255 >reverse_append >reverse_append #Heq1
256 lapply (append_l1_injective … Heq1) [ // ] #Heq2
257 lapply (eq_f … (reverse ?) … Heq2) //
258 qed.
259
260 lemma length_rev_append: ∀A.∀l,acc:list A. 
261   |rev_append ? l acc| = |l|+|acc|.
262 #A #l elim l // #a #tl #Hind normalize 
263 #acc >Hind normalize // 
264 qed.
265
266 (****************************** mem ********************************)
267 let rec mem A (a:A) (l:list A) on l ≝
268   match l with
269   [ nil ⇒ False
270   | cons hd tl ⇒ a=hd ∨ mem A a tl
271   ]. 
272
273 lemma mem_map: ∀A,B.∀f:A→B.∀l,b. 
274   mem ? b (map … f l) → ∃a. mem ? a l ∧ f a = b.
275 #A #B #f #l elim l 
276   [#b normalize @False_ind
277   |#a #tl #Hind #b normalize *
278     [#eqb @(ex_intro … a) /3/
279     |#memb cases (Hind … memb) #a * #mema #eqb
280      @(ex_intro … a) /3/
281     ]
282   ]
283 qed.
284
285 lemma mem_map_forward: ∀A,B.∀f:A→B.∀a,l. 
286   mem A a l → mem B (f a) (map ?? f l).
287  #A #B #f #a #l elim l
288   [normalize @False_ind
289   |#b #tl #Hind * 
290     [#eqab <eqab normalize %1 % |#memtl normalize %2 @Hind @memtl]
291   ]
292 qed.
293
294 (***************************** split *******************************)
295 let rec split_rev A (l:list A) acc n on n ≝ 
296   match n with 
297   [O ⇒ 〈acc,l〉
298   |S m ⇒ match l with 
299     [nil ⇒ 〈acc,[]〉
300     |cons a tl ⇒ split_rev A tl (a::acc) m
301     ]
302   ].
303   
304 definition split ≝ λA,l,n.
305   let 〈l1,l2〉 ≝ split_rev A l [] n in 〈reverse ? l1,l2〉.
306
307 lemma split_rev_len: ∀A,n,l,acc. n ≤ |l| →
308   |\fst (split_rev A l acc n)| = n+|acc|.
309 #A #n elim n // #m #Hind *
310   [normalize #acc #Hfalse @False_ind /2/
311   |#a #tl #acc #Hlen normalize >Hind 
312     [normalize // |@le_S_S_to_le //]
313   ]
314 qed.
315
316 lemma split_len: ∀A,n,l. n ≤ |l| →
317   |\fst (split A l n)| = n.
318 #A #n #l #Hlen normalize >(eq_pair_fst_snd ?? (split_rev …))
319 normalize >length_reverse  >(split_rev_len … [ ] Hlen) normalize //
320 qed.
321   
322 lemma split_rev_eq: ∀A,n,l,acc. n ≤ |l| → 
323   reverse ? acc@ l = 
324     reverse ? (\fst (split_rev A l acc n))@(\snd (split_rev A l acc n)).
325  #A #n elim n //
326  #m #Hind * 
327    [#acc whd in ⊢ ((??%)→?); #False_ind /2/ 
328    |#a #tl #acc #Hlen >append_cons <reverse_single <reverse_append 
329     @(Hind tl) @le_S_S_to_le @Hlen
330    ]
331 qed.
332  
333 lemma split_eq: ∀A,n,l. n ≤ |l| → 
334   l = (\fst (split A l n))@(\snd (split A l n)).
335 #A #n #l #Hlen change with ((reverse ? [ ])@l) in ⊢ (??%?);
336 >(split_rev_eq … Hlen) normalize 
337 >(eq_pair_fst_snd ?? (split_rev A l [] n)) %
338 qed.
339
340 lemma split_exists: ∀A,n.∀l:list A. n ≤ |l| → 
341   ∃l1,l2. l = l1@l2 ∧ |l1| = n.
342 #A #n #l #Hlen @(ex_intro … (\fst (split A l n)))
343 @(ex_intro … (\snd (split A l n))) % /2/
344 qed.
345   
346 (****************************** flatten ******************************)
347 definition flatten ≝ λA.foldr (list A) (list A) (append A) [].
348
349 lemma flatten_to_mem: ∀A,n,l,l1,l2.∀a:list A. 0 < n →
350   (∀x. mem ? x l → |x| = n) → |a| = n → flatten ? l = l1@a@l2  →
351     (∃q.|l1| = n*q)  → mem ? a l.
352 #A #n #l elim l
353   [normalize #l1 #l2 #a #posn #Hlen #Ha #Hnil @False_ind
354    cut (|a|=0) [@sym_eq @le_n_O_to_eq 
355    @(transitive_le ? (|nil A|)) // >Hnil >length_append >length_append //] /2/
356   |#hd #tl #Hind #l1 #l2 #a #posn #Hlen #Ha 
357    whd in match (flatten ??); #Hflat * #q cases q
358     [<times_n_O #Hl1 
359      cut (a = hd) [>(lenght_to_nil… Hl1) in Hflat; 
360      whd in ⊢ ((???%)→?); #Hflat @sym_eq @(append_l1_injective … Hflat)
361      >Ha >Hlen // %1 //   
362      ] /2/
363     |#q1 #Hl1 lapply (split_exists … n l1 ?) //
364      * #l11 * #l12 * #Heql1 #Hlenl11 %2
365      @(Hind l12 l2 … posn ? Ha) 
366       [#x #memx @Hlen %2 //
367       |@(append_l2_injective ? hd l11) 
368         [>Hlenl11 @Hlen %1 %
369         |>Hflat >Heql1 >associative_append %
370         ]
371       |@(ex_intro …q1) @(injective_plus_r n) 
372        <Hlenl11 in ⊢ (??%?); <length_append <Heql1 >Hl1 //
373       ]
374     ]
375   ]
376 qed.
377
378 (****************************** nth ********************************)
379 let rec nth n (A:Type[0]) (l:list A) (d:A)  ≝  
380   match n with
381     [O ⇒ hd A l d
382     |S m ⇒ nth m A (tail A l) d].
383
384 lemma nth_nil: ∀A,a,i. nth i A ([]) a = a.
385 #A #a #i elim i normalize //
386 qed.
387
388 (****************************** nth_opt ********************************)
389 let rec nth_opt (A:Type[0]) (n:nat) (l:list A) on l : option A ≝
390 match l with
391 [ nil ⇒ None ?
392 | cons h t ⇒ match n with [ O ⇒ Some ? h | S m ⇒ nth_opt A m t ]
393 ].
394
395 (**************************** All *******************************)
396
397 let rec All (A:Type[0]) (P:A → Prop) (l:list A) on l : Prop ≝
398 match l with
399 [ nil ⇒ True
400 | cons h t ⇒ P h ∧ All A P t
401 ].
402
403 lemma All_mp : ∀A,P,Q. (∀a.P a → Q a) → ∀l. All A P l → All A Q l.
404 #A #P #Q #H #l elim l normalize //
405 #h #t #IH * /3/
406 qed.
407
408 lemma All_nth : ∀A,P,n,l.
409   All A P l →
410   ∀a.
411   nth_opt A n l = Some A a →
412   P a.
413 #A #P #n elim n
414 [ * [ #_ #a #E whd in E:(??%?); destruct
415     | #hd #tl * #H #_ #a #E whd in E:(??%?); destruct @H
416     ]
417 | #m #IH *
418   [ #_ #a #E whd in E:(??%?); destruct
419   | #hd #tl * #_ whd in ⊢ (? → ∀_.??%? → ?); @IH
420   ]
421 ] qed.
422
423 (**************************** Exists *******************************)
424
425 let rec Exists (A:Type[0]) (P:A → Prop) (l:list A) on l : Prop ≝
426 match l with
427 [ nil ⇒ False
428 | cons h t ⇒ (P h) ∨ (Exists A P t)
429 ].
430
431 lemma Exists_append : ∀A,P,l1,l2.
432   Exists A P (l1 @ l2) → Exists A P l1 ∨ Exists A P l2.
433 #A #P #l1 elim l1
434 [ normalize /2/
435 | #h #t #IH #l2 *
436   [ #H /3/
437   | #H cases (IH l2 H) /3/
438   ]
439 ] qed.
440
441 lemma Exists_append_l : ∀A,P,l1,l2.
442   Exists A P l1 → Exists A P (l1@l2).
443 #A #P #l1 #l2 elim l1
444 [ *
445 | #h #t #IH *
446   [ #H %1 @H
447   | #H %2 @IH @H
448   ]
449 ] qed.
450
451 lemma Exists_append_r : ∀A,P,l1,l2.
452   Exists A P l2 → Exists A P (l1@l2).
453 #A #P #l1 #l2 elim l1
454 [ #H @H
455 | #h #t #IH #H %2 @IH @H
456 ] qed.
457
458 lemma Exists_add : ∀A,P,l1,x,l2. Exists A P (l1@l2) → Exists A P (l1@x::l2).
459 #A #P #l1 #x #l2 elim l1
460 [ normalize #H %2 @H
461 | #h #t #IH normalize * [ #H %1 @H | #H %2 @IH @H ]
462 qed.
463
464 lemma Exists_mid : ∀A,P,l1,x,l2. P x → Exists A P (l1@x::l2).
465 #A #P #l1 #x #l2 #H elim l1
466 [ %1 @H
467 | #h #t #IH %2 @IH
468 ] qed.
469
470 lemma Exists_map : ∀A,B,P,Q,f,l.
471 Exists A P l →
472 (∀a.P a → Q (f a)) →
473 Exists B Q (map A B f l).
474 #A #B #P #Q #f #l elim l //
475 #h #t #IH * [ #H #F %1 @F @H | #H #F %2 @IH [ @H | @F ] ] qed.
476
477 lemma Exists_All : ∀A,P,Q,l.
478   Exists A P l →
479   All A Q l →
480   ∃x. P x ∧ Q x.
481 #A #P #Q #l elim l [ * | #hd #tl #IH * [ #H1 * #H2 #_ %{hd} /2/ | #H1 * #_ #H2 @IH // ]
482 qed.
483
484 (**************************** fold *******************************)
485
486 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 ≝  
487  match l with 
488   [ nil ⇒ b 
489   | cons a l ⇒
490      if p a then op (f a) (fold A B op b p f l)
491      else fold A B op b p f l].
492       
493 notation "\fold  [ op , nil ]_{ ident i ∈ l | p} f"
494   with precedence 80
495 for @{'fold $op $nil (λ${ident i}. $p) (λ${ident i}. $f) $l}.
496
497 notation "\fold [ op , nil ]_{ident i ∈ l } f"
498   with precedence 80
499 for @{'fold $op $nil (λ${ident i}.true) (λ${ident i}. $f) $l}.
500
501 interpretation "\fold" 'fold op nil p f l = (fold ? ? op nil p f l).
502
503 theorem fold_true: 
504 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f:A→B. p a = true → 
505   \fold[op,nil]_{i ∈ a::l| p i} (f i) = 
506     op (f a) \fold[op,nil]_{i ∈ l| p i} (f i). 
507 #A #B #a #l #p #op #nil #f #pa normalize >pa // qed.
508
509 theorem fold_false: 
510 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f.
511 p a = false → \fold[op,nil]_{i ∈ a::l| p i} (f i) = 
512   \fold[op,nil]_{i ∈ l| p i} (f i).
513 #A #B #a #l #p #op #nil #f #pa normalize >pa // qed.
514
515 theorem fold_filter: 
516 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f:A →B.
517   \fold[op,nil]_{i ∈ l| p i} (f i) = 
518     \fold[op,nil]_{i ∈ (filter A p l)} (f i).
519 #A #B #a #l #p #op #nil #f elim l //  
520 #a #tl #Hind cases(true_or_false (p a)) #pa 
521   [ >filter_true // > fold_true // >fold_true //
522   | >filter_false // >fold_false // ]
523 qed.
524
525 record Aop (A:Type[0]) (nil:A) : Type[0] ≝
526   {op :2> A → A → A; 
527    nill:∀a. op nil a = a; 
528    nilr:∀a. op a nil = a;
529    assoc: ∀a,b,c.op a (op b c) = op (op a b) c
530   }.
531
532 theorem fold_sum: ∀A,B. ∀I,J:list A.∀nil.∀op:Aop B nil.∀f.
533   op (\fold[op,nil]_{i∈I} (f i)) (\fold[op,nil]_{i∈J} (f i)) =
534     \fold[op,nil]_{i∈(I@J)} (f i).
535 #A #B #I #J #nil #op #f (elim I) normalize 
536   [>nill //|#a #tl #Hind <assoc //]
537 qed.
538
539 (********************** lhd and ltl ******************************)
540
541 let rec lhd (A:Type[0]) (l:list A) n on n ≝ match n with
542    [ O   ⇒ nil …
543    | S n ⇒ match l with [ nil ⇒ nil … | cons a l ⇒ a :: lhd A l n ]
544    ].
545
546 let rec ltl (A:Type[0]) (l:list A) n on n ≝ match n with
547    [ O   ⇒ l
548    | S n ⇒ ltl A (tail … l) n
549    ].
550
551 lemma lhd_nil: ∀A,n. lhd A ([]) n = [].
552 #A #n elim n //
553 qed.
554
555 lemma ltl_nil: ∀A,n. ltl A ([]) n = [].
556 #A #n elim n normalize //
557 qed.
558
559 lemma lhd_cons_ltl: ∀A,n,l. lhd A l n @ ltl A l n = l.
560 #A #n elim n -n //
561 #n #IHn #l elim l normalize //
562 qed.
563
564 lemma length_ltl: ∀A,n,l. |ltl A l n| = |l| - n.
565 #A #n elim n -n // 
566 #n #IHn *; normalize /2/
567 qed.
568
569 (********************** find ******************************)
570 let rec find (A,B:Type[0]) (f:A → option B) (l:list A) on l : option B ≝
571 match l with
572 [ nil ⇒ None B
573 | cons h t ⇒
574     match f h with
575     [ None ⇒ find A B f t
576     | Some b ⇒ Some B b
577     ]
578 ].
579
580 (********************** position_of ******************************)
581 let rec position_of_aux (A:Type[0]) (found: A → bool) (l:list A) (acc:nat) on l : option nat ≝
582 match l with
583 [ nil ⇒ None ?
584 | cons h t ⇒
585    match found h with [true ⇒ Some … acc | false ⇒ position_of_aux … found t (S acc)]].
586
587 definition position_of: ∀A:Type[0]. (A → bool) → list A → option nat ≝
588  λA,found,l. position_of_aux A found l 0.
589
590
591 (********************** make_list ******************************)
592 let rec make_list (A:Type[0]) (a:A) (n:nat) on n : list A ≝
593 match n with
594 [ O ⇒ [ ]
595 | S m ⇒ a::(make_list A a m)
596 ].