source: src/utilities/lists.ma @ 2897

Last change on this file since 2897 was 2897, checked in by campbell, 7 years ago

Minor tidying.

File size: 10.7 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
112lemma All2_tail : ∀A,B,P,a,b.
113  All2 A B P a b →
114  All2 A B P (tail A a) (tail B b).
115#A #B #P * [2: #ah #at] * [2,4: #bh #bt]
116* //
117qed.
118
119let 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 ≝
120match l return λl. All A P l → ? with
121[ nil ⇒ λ_. nil B
122| cons hd tl ⇒ λH. cons B (f hd (proj1 … H)) (map_All A B P f tl (proj2 … H))
123] H.
124
125lemma All_rev : ∀A,P,l.All A P l → All A P (rev ? l).
126#A#P#l elim l //
127#h #t #Hi * #Ph #Pt normalize
128>rev_append_def
129@All_append
130[ @(Hi Pt)
131| %{Ph I}
132]
133qed.
134
135lemma Exists_rev : ∀A,P,l.Exists A P l → Exists A P (rev ? l).
136#A#P#l elim l //
137#h #t #Hi normalize >rev_append_def
138* [ #Ph @Exists_append_r %{Ph} | #Pt @Exists_append_l @(Hi Pt)]
139qed.
140
141include "utilities/option.ma".
142
143lemma find_All : ∀A,B.∀P : A → Prop.∀Q : B → Prop.∀f.(∀x.P x → opt_All ? Q (f x)) →
144  ∀l. All ? P l → opt_All ? Q (find … f l).
145#A#B#P#Q#f#H#l elim l [#_%]
146#x #l' #Hi
147* #Px #AllPl'
148whd in ⊢ (???%);
149lapply (H x Px)
150lapply (refl ? (f x))
151generalize in ⊢ (???%→?); #o elim o [2: #b] #fx_eq >fx_eq [//]
152#_ whd in ⊢ (???%); @(Hi AllPl')
153qed.
154
155include "utilities/monad.ma".
156
157definition Append : ∀A.Aop ? (nil A) ≝ λA.mk_Aop ? ? (append ?) ? ? ?.
158// qed.
159
160definition List ≝ MakeMonadProps
161  list
162  (λX,x.[x])
163  (λX,Y,l,f.foldr … (λx.append ? (f x)) [ ] l)
164  ????. normalize
165[ / by /
166| #X#m elim m normalize //
167| #X#Y#Z #m #f#g
168  elim m normalize [//]
169  #x#l' #Hi elim (f x)
170  [@Hi] normalize #hd #tl #IH >associative_append >IH %
171|#X#Y#m #f #g #H elim m normalize [//]
172  #hd #tl #IH >H >IH %
173] qed.
174
175alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
176unification hint 0 ≔ X ;
177N ≟ max_def List
178(*---------------------------*)⊢
179list X ≡ monad N X.
180
181definition In ≝ λA.λl : list A.λx : A.Exists A (eq ? x) l.
182
183lemma All_In : ∀A,P,l,x. All A P l → In A l x → P x.
184#A#P#l#x #Pl #xl elim (Exists_All … xl Pl)
185#x' * #EQx' >EQx' //
186qed.
187
188lemma In_All : ∀A,P,l.(∀x.In A l x → P x) → All A P l.
189#A#P#l elim l
190[#_ %
191|#x #l' #IH #H %
192  [ @H % %
193  | @IH #y #G @H %2 @G
194  ]
195]
196qed.
197
198
199lemma nth_opt_append_l : ∀A,l1,l2,n.|l1| > n → nth_opt A n (l1 @ l2) = nth_opt A n l1.
200#A#l1 elim l1 normalize
201[ #l2 #n #ABS elim (absurd ? ABS ?) //
202| #x #l' #IH #l2 #n cases n normalize
203  [//
204  | #n #H @IH @le_S_S_to_le assumption
205  ]
206]
207qed.
208
209lemma nth_opt_append_r : ∀A,l1,l2,n.|l1| ≤ n → nth_opt A n (l1 @ l2) = nth_opt A (n - |l1|) l2.
210#A#l1 elim l1
211[#l2 #n #_ <minus_n_O %
212|#x #l' #IH #l2 #n normalize in match (|?|); whd in match (nth_opt ???);
213  cases n -n
214  [  #ABS elim (absurd ? ABS ?) //
215  | #n #H whd in ⊢ (??%?); >minus_S_S @IH @le_S_S_to_le assumption
216  ]
217]
218qed.
219
220lemma nth_opt_append_hit_l : ∀A,l1,l2,n,x. nth_opt A n l1 = Some ? x →
221  nth_opt A n (l1 @ l2) = Some ? x.
222#A #l1 elim l1 normalize
223[ #l2 #n #x #ABS destruct
224| #hd #tl #IH #l2 * [2: #n] #x normalize /2 by /
225]
226qed.
227
228lemma nth_opt_append_miss_l : ∀A,l1,l2,n. nth_opt A n l1 = None ? →
229  nth_opt A n (l1 @ l2) = nth_opt A (n - |l1|) l2.
230#A #l1 elim l1 normalize
231[ #l2 #n #_ <minus_n_O %
232| #hd #tl #IH #l2 * [2: #n] normalize
233  [ @IH
234  | #ABS destruct(ABS)
235  ]
236]
237qed.
238
239 
240(* count occurrences satisfying a test *)
241let rec count A (f : A → bool) (l : list A) on l : ℕ ≝
242  match l with
243  [ nil ⇒ 0
244  | cons x l' ⇒ (if f x then 1 else 0) + count A f l'
245  ].
246 
247theorem count_append : ∀A,f,l1,l2.count A f (l1@l2) = count A f l1 + count A f l2.
248#A #f #l1 elim l1
249[ #l2 %
250| #hd #tl #IH #l2 normalize elim (f hd) normalize >IH %
251]
252qed.
253
254
255
256lemma flatten_append : ∀A,l1,l2.
257  flatten A (l1@l2) = (flatten A l1)@(flatten A l2).
258#A #l1 #l2
259elim l1
260[ %
261| #h #t #IH whd in ⊢ (??%(??%?));
262  change with (flatten ??) in match (foldr ?????); >IH
263  change with (flatten ??) in match (foldr ?????);
264  >associative_append %
265] qed.
266
267
268include "utilities/option.ma".
269
270lemma position_of_from_aux : ∀A,test,l,n.
271  position_of_aux A test l n = !n' ← position_of A test l; return n + n'.
272#A #test #l elim l
273[ normalize #_ %
274| #hd #tl #IH #n
275  normalize in ⊢ (??%?); >IH
276  normalize elim (test hd) normalize
277  [ <plus_n_O %
278  | >(IH 1) whd in match (position_of ???);
279    elim (position_of_aux ??? 0) normalize
280    [ % | #x <plus_n_Sm % ]
281  ]
282]
283qed.
284
285definition position_of_safe ≝ λA,test,l.λprf : count A test l ≥ 1.
286  opt_safe ? (position_of A test l) ?.
287  lapply prf -prf elim l normalize
288  [ #ABS elim (absurd ? ABS (not_le_Sn_O 0))
289  |#hd #tl #IH elim (test hd) normalize
290    [2: >position_of_from_aux #H
291      change with (position_of_aux ????) in match (position_of ???);
292      >(opt_to_opt_safe … (IH H)) @opt_safe_elim normalize #x]
293    #_ % #ABS destruct(ABS)
294qed.
295
296definition index_of ≝
297  λA,test,l.λprf : eqb (count A test l) 1.position_of_safe A test l ?.
298  lapply prf -prf @eqb_elim #EQ * >EQ %
299qed.
300
301lemma position_of_append_hit : ∀A,test,l1,l2,x.
302  position_of A test (l1@l2) = Some ? x →
303    position_of A test l1 = Some ? x ∨
304      (position_of A test l1 = None ? ∧
305        ! p ← position_of A test l2 ; return (|l1| + p) = Some ? x).
306#A#test#l1 elim l1
307[ #l2 #x change with l2 in match (? @ l2); #EQ >EQ %2
308  % %
309| #hd #tl #IH #l2 #x
310  normalize elim (test hd) normalize
311  [#H %{H}
312  | >position_of_from_aux
313    lapply (refl … (position_of A test (tl@l2)))
314    generalize in ⊢ (???%→?); * [2: #n'] #Heq >Heq
315    normalize #EQ destruct(EQ)
316    elim (IH … Heq) -IH
317    [ #G %
318    | * #G #H
319     lapply (refl … (position_of A test l2))
320     generalize in ⊢ (???%→?); * [2: #x'] #H' >H' in H; normalize
321     #EQ destruct (EQ) %2 %]
322    >position_of_from_aux
323    [1,2: >G % | >H' %]
324  ]
325]
326qed.
327
328lemma position_of_hit : ∀A,test,l,x.position_of A test l = Some ? x →
329  count A test l ≥ 1.
330#A#test#l elim l normalize
331[#x #ABS destruct(ABS)
332|#hd #tl #IH #x elim (test hd) normalize [#EQ destruct(EQ) //]
333  >position_of_from_aux
334  lapply (refl … (position_of ? test tl))
335  generalize in ⊢ (???%→?); * [2: #x'] #Heq >Heq normalize
336  #EQ destruct(EQ) @(IH … Heq)
337]
338qed.
339
340lemma position_of_miss : ∀A,test,l.position_of A test l = None ? →
341  count A test l = 0.
342#A#test#l elim l normalize
343[ #_ %
344|#hd #tl #IH elim (test hd) normalize [#EQ destruct(EQ)]
345  >position_of_from_aux
346  lapply (refl … (position_of ? test tl))
347  generalize in ⊢ (???%→?); * [2: #x'] #Heq >Heq normalize
348  #EQ destruct(EQ) @(IH … Heq)
349]
350qed.
351
352
353lemma position_of_append_miss : ∀A,test,l1,l2.
354  position_of A test (l1@l2) = None ? →
355    position_of A test l1 = None ? ∧ position_of A test l2 = None ?.
356#A#test#l1 elim l1
357[ #l2 change with l2 in match (? @ l2); #EQ >EQ % %
358| #hd #tl #IH #l2
359  normalize elim (test hd) normalize
360  [#H destruct(H)
361  | >position_of_from_aux
362    lapply (refl … (position_of A test (tl@l2)))
363    generalize in ⊢ (???%→?); * [2: #n'] #Heq >Heq
364    normalize #EQ destruct(EQ)
365    elim (IH … Heq) #H1 #H2
366    >position_of_from_aux
367    >position_of_from_aux
368    >H1 >H2 normalize % %
369  ]
370]
371qed.
372
373
374(* A not terribly efficient sort for testing purposes *)
375
376let rec ordered_insert (A:Type[0]) (lt:A → A → bool) (a:A) (l:list A) on l : list A ≝
377match l with
378[ nil ⇒ [a]
379| cons h t ⇒ if lt a h then a::h::t else h::(ordered_insert A lt a t)
380].
381
382let rec insert_sort (A:Type[0]) (lt:A → A → bool) (l:list A) on l : list A ≝
383match l with
384[ nil ⇒ [ ]
385| cons h t ⇒ ordered_insert A lt h (insert_sort A lt t)
386].
387
388(* range from 0 to n excluded with proof of minoration *)
389
390let rec range_strong_internal (index, how_many, end : ℕ) on how_many :
391  index + how_many = end →
392  list (Σn.n<end) ≝
393  match how_many return λhow_many.index + how_many = end → ? with
394  [ O ⇒ λ_.[ ]
395  | S k ⇒ λprf.«index, ?» :: range_strong_internal (S index) k end ?
396  ].
397<prf
398[ >(plus_n_O index) in ⊢ (?%?); @monotonic_lt_plus_r @le_S_S @le_O_n
399| @plus_n_Sm
400]
401qed.
402
403definition range_strong : ∀end.list (Σn.n < end) ≝
404λend.range_strong_internal 0 ? end (refl …).
Note: See TracBrowser for help on using the repository browser.