source: src/utilities/lists.ma @ 2440

Last change on this file since 2440 was 2440, checked in by piccolo, 8 years ago

fixed range_strong and linearise
(commit by Paolo, he's to blame in case)

File size: 10.2 KB
Line 
1include "basics/lists/list.ma".
2include "ASM/Util.ma". (* coercion from bool to Prop *)
3
4lemma All_map : ∀A,B,P,Q,f,l.
5All A P l →
6(∀a.P a → Q (f a)) →
7All B Q (map A B f l).
8#A #B #P #Q #f #l elim l //
9#h #t #IH * #Ph #Pt #F % [@(F ? Ph) | @(IH Pt F)] qed.
10
11lemma Exists_map : ∀A,B,P,Q,f,l.
12Exists A P l →
13(∀a.P a → Q (f a)) →
14Exists B Q (map A B f l).
15#A #B #P #Q #f #l elim l //
16#h #t #IH * #H #F
17[ %1 @(F ? H) | %2 @(IH H F) ]
18qed.
19
20lemma All_append : ∀A,P,l,r. All A P l → All A P r → All A P (l@r).
21#A #P #l elim l
22[ //
23| #hd #tl #IH #r * #H1 #H2 #H3 % // @IH //
24] qed.
25
26lemma All_append_l : ∀A,P,l,r. All A P (l@r) → All A P l.
27#A #P #l elim l
28[ //
29| #hd #tl #IH #r * #H1 #H2 % /2/
30] qed.
31
32lemma All_append_r : ∀A,P,l,r. All A P (l@r) → All A P r.
33#A #P #l elim l
34[ //
35| #h #t #IH #r * /2/
36] qed.
37
38(* An alternative form of All that can be easier to use sometimes. *)
39lemma All_alt : ∀A,P,l.
40  (∀a,pre,post. l = pre@a::post → P a) →
41  All A P l.
42#A #P #l #H lapply (refl ? l) change with ([ ] @ l) in ⊢ (???% → ?);
43generalize in ⊢ (???(??%?) → ?); elim l in ⊢ (? → ???(???%) → %);
44[ #pre #E %
45| #a #tl #IH #pre #E %
46  [ @(H a pre tl E)
47  | @(IH (pre@[a])) >associative_append @E
48  ]
49] qed.
50
51lemma All_split : ∀A,P,l. All A P l → ∀pre,x,post.l = pre @ x :: post → P x.
52#A #P #l elim l
53[ * * normalize [2: #y #pre'] #x #post #ABS destruct(ABS)
54| #hd #tl #IH * #Phd #Ptl * normalize [2: #y #pre'] #x #post #EQ destruct(EQ)
55  [ @(IH Ptl … (refl …))
56  | @Phd
57  ]
58]
59qed.
60
61(* Boolean predicate version of All *)
62
63let rec all (A:Type[0]) (P:A → bool) (l:list A) on l : bool ≝
64match l with
65[ nil ⇒ true
66| cons h t ⇒ P h ∧ all A P t
67].
68
69lemma all_All : ∀A,P,l. bool_to_Prop (all A P l) ↔ All A (λa.bool_to_Prop (P a)) l.
70#A #P #l elim l
71[ % //
72| #h #t * #IH #IH' %
73  [ whd in ⊢ (?% → %); cases (P h) [ #p % /2/ | * ]
74  | * #p #H whd in ⊢ (?%); >p /2/
75  ]
76] qed.
77
78
79let rec All2 (A,B:Type[0]) (P:A → B → Prop) (la:list A) (lb:list B) on la : Prop ≝
80match la with
81[ nil ⇒ match lb with [ nil ⇒ True | _ ⇒ False ]
82| cons ha ta ⇒
83    match lb with [ nil ⇒ False | cons hb tb ⇒ P ha hb ∧ All2 A B P ta tb ]
84].
85
86lemma All2_length : ∀A,B,P,la,lb. All2 A B P la lb → |la| = |lb|.
87#A #B #P #la elim la
88[ * [ // | #x #y * ]
89| #ha #ta #IH * [ * | #hb #tb * #H1 #H2 whd in ⊢ (??%%); >(IH … H2) @refl
90] qed.
91
92lemma All2_mp : ∀A,B,P,Q,la,lb. (∀a,b. P a b → Q a b) → All2 A B P la lb → All2 A B Q la lb.
93#A #B #P #Q #la elim la
94[ * [ // | #h #t #_ * ]
95| #ha #ta #IH * [ // | #hb #tb #H * #H1 #H2 % [ @H @H1 | @(IH … H2) @H ] ]
96] qed.
97
98lemma All2_left : ∀A,B,P,la,lb.
99  All2 A B P la lb → All A (λa.∃b.P a b) la.
100#A #B #P #la elim la
101[ //
102| #hda #tla #IH * [ * ] #hdb #tlb * #p #H % [ %{hdb} // | /2/ ]
103] qed.
104
105lemma All2_right : ∀A,B,P,la,lb.
106  All2 A B P la lb → All B (λb.∃a.P a b) lb.
107#A #B #P #la elim la
108[ * // #h #t *
109| #hda #tla #IH * [ * ] #hdb #tlb * #p #H % [ %{hda} // | /2/ ]
110] qed.
111
112let rec map_All (A,B:Type[0]) (P:A → Prop) (f:∀a. P a → B) (l:list A) (H:All A P l) on l : list B ≝
113match l return λl. All A P l → ? with
114[ nil ⇒ λ_. nil B
115| cons hd tl ⇒ λH. cons B (f hd (proj1 … H)) (map_All A B P f tl (proj2 … H))
116] H.
117
118lemma All_rev : ∀A,P,l.All A P l → All A P (rev ? l).
119#A#P#l elim l //
120#h #t #Hi * #Ph #Pt normalize
121>rev_append_def
122@All_append
123[ @(Hi Pt)
124| %{Ph I}
125]
126qed.
127
128lemma Exists_rev : ∀A,P,l.Exists A P l → Exists A P (rev ? l).
129#A#P#l elim l //
130#h #t #Hi normalize >rev_append_def
131* [ #Ph @Exists_append_r %{Ph} | #Pt @Exists_append_l @(Hi Pt)]
132qed.
133
134include "utilities/option.ma".
135
136lemma find_All : ∀A,B.∀P : A → Prop.∀Q : B → Prop.∀f.(∀x.P x → opt_All ? Q (f x)) →
137  ∀l. All ? P l → opt_All ? Q (find … f l).
138#A#B#P#Q#f#H#l elim l [#_%]
139#x #l' #Hi
140* #Px #AllPl'
141whd in ⊢ (???%);
142lapply (H x Px)
143lapply (refl ? (f x))
144generalize in ⊢ (???%→?); #o elim o [2: #b] #fx_eq >fx_eq [//]
145#_ whd in ⊢ (???%); @(Hi AllPl')
146qed.
147
148include "utilities/monad.ma".
149
150definition Append : ∀A.Aop ? (nil A) ≝ λA.mk_Aop ? ? (append ?) ? ? ?.
151// qed.
152
153let rec range_strong_internal (start, how_many, end : ℕ)
154  on how_many : start + how_many ≤ end → list (Σn : ℕ.n < end) ≝
155match how_many return λx.start + x ≤ end → ?
156  with
157[ O ⇒ λ_.[ ]
158| S k ⇒ λprf.«start, ?» :: range_strong_internal (S start) k end ?
159].
160[2: //]
161>(plus_n_O start) /2 by plus_lt_to_lt/
162qed.
163
164definition range_strong : ∀end : ℕ. list (Σn.n<end) ≝
165  λend.range_strong_internal 0 end end (le_n …).
166
167definition List ≝ MakeMonadProps
168  list
169  (λX,x.[x])
170  (λX,Y,l,f.foldr … (λx.append ? (f x)) [ ] l)
171  ????. normalize
172[ / by /
173| #X#m elim m normalize //
174| #X#Y#Z #m #f#g
175  elim m normalize [//]
176  #x#l' #Hi elim (f x)
177  [@Hi] normalize #hd #tl #IH >associative_append >IH %
178|#X#Y#m #f #g #H elim m normalize [//]
179  #hd #tl #IH >H >IH %
180] qed.
181
182alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
183unification hint 0 ≔ X ;
184N ≟ max_def List
185(*---------------------------*)⊢
186list X ≡ monad N X.
187
188definition In ≝ λA.λl : list A.λx : A.Exists A (eq ? x) l.
189
190lemma All_In : ∀A,P,l,x. All A P l → In A l x → P x.
191#A#P#l#x #Pl #xl elim (Exists_All … xl Pl)
192#x' * #EQx' >EQx' //
193qed.
194
195lemma In_All : ∀A,P,l.(∀x.In A l x → P x) → All A P l.
196#A#P#l elim l
197[#_ %
198|#x #l' #IH #H %
199  [ @H % %
200  | @IH #y #G @H %2 @G
201  ]
202]
203qed.
204
205
206lemma nth_opt_append_l : ∀A,l1,l2,n.|l1| > n → nth_opt A n (l1 @ l2) = nth_opt A n l1.
207#A#l1 elim l1 normalize
208[ #l2 #n #ABS elim (absurd ? ABS ?) //
209| #x #l' #IH #l2 #n cases n normalize
210  [//
211  | #n #H @IH @le_S_S_to_le assumption
212  ]
213]
214qed.
215
216lemma nth_opt_append_r : ∀A,l1,l2,n.|l1| ≤ n → nth_opt A n (l1 @ l2) = nth_opt A (n - |l1|) l2.
217#A#l1 elim l1
218[#l2 #n #_ <minus_n_O %
219|#x #l' #IH #l2 #n normalize in match (|?|); whd in match (nth_opt ???);
220  cases n -n
221  [  #ABS elim (absurd ? ABS ?) //
222  | #n #H whd in ⊢ (??%?); >minus_S_S @IH @le_S_S_to_le assumption
223  ]
224]
225qed.
226
227lemma nth_opt_append_hit_l : ∀A,l1,l2,n,x. nth_opt A n l1 = Some ? x →
228  nth_opt A n (l1 @ l2) = Some ? x.
229#A #l1 elim l1 normalize
230[ #l2 #n #x #ABS destruct
231| #hd #tl #IH #l2 * [2: #n] #x normalize /2 by /
232]
233qed.
234
235lemma nth_opt_append_miss_l : ∀A,l1,l2,n. nth_opt A n l1 = None ? →
236  nth_opt A n (l1 @ l2) = nth_opt A (n - |l1|) l2.
237#A #l1 elim l1 normalize
238[ #l2 #n #_ <minus_n_O %
239| #hd #tl #IH #l2 * [2: #n] normalize
240  [ @IH
241  | #ABS destruct(ABS)
242  ]
243]
244qed.
245
246 
247(* count occurrences satisfying a test *)
248let rec count A (f : A → bool) (l : list A) on l : ℕ ≝
249  match l with
250  [ nil ⇒ 0
251  | cons x l' ⇒ (if f x then 1 else 0) + count A f l'
252  ].
253 
254theorem count_append : ∀A,f,l1,l2.count A f (l1@l2) = count A f l1 + count A f l2.
255#A #f #l1 elim l1
256[ #l2 %
257| #hd #tl #IH #l2 normalize elim (f hd) normalize >IH %
258]
259qed.
260
261
262include "utilities/option.ma".
263
264lemma position_of_from_aux : ∀A,test,l,n.
265  position_of_aux A test l n = !n' ← position_of A test l; return n + n'.
266#A #test #l elim l
267[ normalize #_ %
268| #hd #tl #IH #n
269  normalize in ⊢ (??%?); >IH
270  normalize elim (test hd) normalize
271  [ <plus_n_O %
272  | >(IH 1) whd in match (position_of ???);
273    elim (position_of_aux ??? 0) normalize
274    [ % | #x <plus_n_Sm % ]
275  ]
276]
277qed.
278
279definition position_of_safe ≝ λA,test,l.λprf : count A test l ≥ 1.
280  opt_safe ? (position_of A test l) ?.
281  lapply prf -prf elim l normalize
282  [ #ABS elim (absurd ? ABS (not_le_Sn_O 0))
283  |#hd #tl #IH elim (test hd) normalize
284    [2: >position_of_from_aux #H
285      change with (position_of_aux ????) in match (position_of ???);
286      >(opt_to_opt_safe … (IH H)) @opt_safe_elim normalize #x]
287    #_ % #ABS destruct(ABS)
288qed.
289
290definition index_of ≝
291  λA,test,l.λprf : eqb (count A test l) 1.position_of_safe A test l ?.
292  lapply prf -prf @eqb_elim #EQ * >EQ %
293qed.
294
295lemma position_of_append_hit : ∀A,test,l1,l2,x.
296  position_of A test (l1@l2) = Some ? x →
297    position_of A test l1 = Some ? x ∨
298      (position_of A test l1 = None ? ∧
299        ! p ← position_of A test l2 ; return (|l1| + p) = Some ? x).
300#A#test#l1 elim l1
301[ #l2 #x change with l2 in match (? @ l2); #EQ >EQ %2
302  % %
303| #hd #tl #IH #l2 #x
304  normalize elim (test hd) normalize
305  [#H %{H}
306  | >position_of_from_aux
307    lapply (refl … (position_of A test (tl@l2)))
308    generalize in ⊢ (???%→?); * [2: #n'] #Heq >Heq
309    normalize #EQ destruct(EQ)
310    elim (IH … Heq) -IH
311    [ #G %
312    | * #G #H
313     lapply (refl … (position_of A test l2))
314     generalize in ⊢ (???%→?); * [2: #x'] #H' >H' in H; normalize
315     #EQ destruct (EQ) %2 %]
316    >position_of_from_aux
317    [1,2: >G % | >H' %]
318  ]
319]
320qed.
321
322lemma position_of_hit : ∀A,test,l,x.position_of A test l = Some ? x →
323  count A test l ≥ 1.
324#A#test#l elim l normalize
325[#x #ABS destruct(ABS)
326|#hd #tl #IH #x elim (test hd) normalize [#EQ destruct(EQ) //]
327  >position_of_from_aux
328  lapply (refl … (position_of ? test tl))
329  generalize in ⊢ (???%→?); * [2: #x'] #Heq >Heq normalize
330  #EQ destruct(EQ) @(IH … Heq)
331]
332qed.
333
334lemma position_of_miss : ∀A,test,l.position_of A test l = None ? →
335  count A test l = 0.
336#A#test#l elim l normalize
337[ #_ %
338|#hd #tl #IH elim (test hd) normalize [#EQ destruct(EQ)]
339  >position_of_from_aux
340  lapply (refl … (position_of ? test tl))
341  generalize in ⊢ (???%→?); * [2: #x'] #Heq >Heq normalize
342  #EQ destruct(EQ) @(IH … Heq)
343]
344qed.
345
346
347lemma position_of_append_miss : ∀A,test,l1,l2.
348  position_of A test (l1@l2) = None ? →
349    position_of A test l1 = None ? ∧ position_of A test l2 = None ?.
350#A#test#l1 elim l1
351[ #l2 change with l2 in match (? @ l2); #EQ >EQ % %
352| #hd #tl #IH #l2
353  normalize elim (test hd) normalize
354  [#H destruct(H)
355  | >position_of_from_aux
356    lapply (refl … (position_of A test (tl@l2)))
357    generalize in ⊢ (???%→?); * [2: #n'] #Heq >Heq
358    normalize #EQ destruct(EQ)
359    elim (IH … Heq) #H1 #H2
360    >position_of_from_aux
361    >position_of_from_aux
362    >H1 >H2 normalize % %
363  ]
364]
365qed.
366
367
368(* Not terribly efficient sort for testing purposes *)
369
370let rec ordered_insert (A:Type[0]) (lt:A → A → bool) (a:A) (l:list A) on l : list A ≝
371match l with
372[ nil ⇒ [a]
373| cons h t ⇒ if lt a h then a::h::t else h::(ordered_insert A lt a t)
374].
375
376let rec insert_sort (A:Type[0]) (lt:A → A → bool) (l:list A) on l : list A ≝
377match l with
378[ nil ⇒ [ ]
379| cons h t ⇒ ordered_insert A lt h (insert_sort A lt t)
380].
Note: See TracBrowser for help on using the repository browser.