source: src/utilities/lists.ma @ 2292

Last change on this file since 2292 was 2292, checked in by campbell, 8 years ago

More RTLabs invariants.

File size: 9.0 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
61let rec All2 (A,B:Type[0]) (P:A → B → Prop) (la:list A) (lb:list B) on la : Prop ≝
62match la with
63[ nil ⇒ match lb with [ nil ⇒ True | _ ⇒ False ]
64| cons ha ta ⇒
65    match lb with [ nil ⇒ False | cons hb tb ⇒ P ha hb ∧ All2 A B P ta tb ]
66].
67
68lemma All2_length : ∀A,B,P,la,lb. All2 A B P la lb → |la| = |lb|.
69#A #B #P #la elim la
70[ * [ // | #x #y * ]
71| #ha #ta #IH * [ * | #hb #tb * #H1 #H2 whd in ⊢ (??%%); >(IH … H2) @refl
72] qed.
73
74lemma 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.
75#A #B #P #Q #la elim la
76[ * [ // | #h #t #_ * ]
77| #ha #ta #IH * [ // | #hb #tb #H * #H1 #H2 % [ @H @H1 | @(IH … H2) @H ] ]
78] qed.
79
80lemma All2_left : ∀A,B,P,la,lb.
81  All2 A B P la lb → All A (λa.∃b.P a b) la.
82#A #B #P #la elim la
83[ //
84| #hda #tla #IH * [ * ] #hdb #tlb * #p #H % [ %{hdb} // | /2/ ]
85] qed.
86
87lemma All2_right : ∀A,B,P,la,lb.
88  All2 A B P la lb → All B (λb.∃a.P a b) lb.
89#A #B #P #la elim la
90[ * // #h #t *
91| #hda #tla #IH * [ * ] #hdb #tlb * #p #H % [ %{hda} // | /2/ ]
92] qed.
93
94let 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 ≝
95match l return λl. All A P l → ? with
96[ nil ⇒ λ_. nil B
97| cons hd tl ⇒ λH. cons B (f hd (proj1 … H)) (map_All A B P f tl (proj2 … H))
98] H.
99
100lemma All_rev : ∀A,P,l.All A P l → All A P (rev ? l).
101#A#P#l elim l //
102#h #t #Hi * #Ph #Pt normalize
103>rev_append_def
104@All_append
105[ @(Hi Pt)
106| %{Ph I}
107]
108qed.
109
110lemma Exists_rev : ∀A,P,l.Exists A P l → Exists A P (rev ? l).
111#A#P#l elim l //
112#h #t #Hi normalize >rev_append_def
113* [ #Ph @Exists_append_r %{Ph} | #Pt @Exists_append_l @(Hi Pt)]
114qed.
115
116include "utilities/option.ma".
117
118lemma find_All : ∀A,B.∀P : A → Prop.∀Q : B → Prop.∀f.(∀x.P x → opt_All ? Q (f x)) →
119  ∀l. All ? P l → opt_All ? Q (find … f l).
120#A#B#P#Q#f#H#l elim l [#_%]
121#x #l' #Hi
122* #Px #AllPl'
123whd in ⊢ (???%);
124lapply (H x Px)
125lapply (refl ? (f x))
126generalize in ⊢ (???%→?); #o elim o [2: #b] #fx_eq >fx_eq [//]
127#_ whd in ⊢ (???%); @(Hi AllPl')
128qed.
129
130include "utilities/monad.ma".
131
132definition Append : ∀A.Aop ? (nil A) ≝ λA.mk_Aop ? ? (append ?) ? ? ?.
133// qed.
134
135definition List ≝ MakeMonadProps
136  list
137  (λX,x.[x])
138  (λX,Y,l,f.foldr … (λx.append ? (f x)) [ ] l)
139  ????. normalize
140[ / by /
141| #X#m elim m normalize //
142| #X#Y#Z #m #f#g
143  elim m normalize [//]
144  #x#l' #Hi elim (f x)
145  [@Hi] normalize #hd #tl #IH >associative_append >IH %
146|#X#Y#m #f #g #H elim m normalize [//]
147  #hd #tl #IH >H >IH %
148] qed.
149
150alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
151unification hint 0 ≔ X ;
152N ≟ max_def List
153(*---------------------------*)⊢
154list X ≡ monad N X.
155
156definition In ≝ λA.λl : list A.λx : A.Exists A (eq ? x) l.
157
158lemma All_In : ∀A,P,l,x. All A P l → In A l x → P x.
159#A#P#l#x #Pl #xl elim (Exists_All … xl Pl)
160#x' * #EQx' >EQx' //
161qed.
162
163lemma In_All : ∀A,P,l.(∀x.In A l x → P x) → All A P l.
164#A#P#l elim l
165[#_ %
166|#x #l' #IH #H %
167  [ @H % %
168  | @IH #y #G @H %2 @G
169  ]
170]
171qed.
172
173
174lemma nth_opt_append_l : ∀A,l1,l2,n.|l1| > n → nth_opt A n (l1 @ l2) = nth_opt A n l1.
175#A#l1 elim l1 normalize
176[ #l2 #n #ABS elim (absurd ? ABS ?) //
177| #x #l' #IH #l2 #n cases n normalize
178  [//
179  | #n #H @IH @le_S_S_to_le assumption
180  ]
181]
182qed.
183
184lemma nth_opt_append_r : ∀A,l1,l2,n.|l1| ≤ n → nth_opt A n (l1 @ l2) = nth_opt A (n - |l1|) l2.
185#A#l1 elim l1
186[#l2 #n #_ <minus_n_O %
187|#x #l' #IH #l2 #n normalize in match (|?|); whd in match (nth_opt ???);
188  cases n -n
189  [  #ABS elim (absurd ? ABS ?) //
190  | #n #H whd in ⊢ (??%?); >minus_S_S @IH @le_S_S_to_le assumption
191  ]
192]
193qed.
194
195lemma nth_opt_append_hit_l : ∀A,l1,l2,n,x. nth_opt A n l1 = Some ? x →
196  nth_opt A n (l1 @ l2) = Some ? x.
197#A #l1 elim l1 normalize
198[ #l2 #n #x #ABS destruct
199| #hd #tl #IH #l2 * [2: #n] #x normalize /2 by /
200]
201qed.
202
203lemma nth_opt_append_miss_l : ∀A,l1,l2,n. nth_opt A n l1 = None ? →
204  nth_opt A n (l1 @ l2) = nth_opt A (n - |l1|) l2.
205#A #l1 elim l1 normalize
206[ #l2 #n #_ <minus_n_O %
207| #hd #tl #IH #l2 * [2: #n] normalize
208  [ @IH
209  | #ABS destruct(ABS)
210  ]
211]
212qed.
213
214 
215(* count occurrences satisfying a test *)
216let rec count A (f : A → bool) (l : list A) on l : ℕ ≝
217  match l with
218  [ nil ⇒ 0
219  | cons x l' ⇒ (if f x then 1 else 0) + count A f l'
220  ].
221 
222theorem count_append : ∀A,f,l1,l2.count A f (l1@l2) = count A f l1 + count A f l2.
223#A #f #l1 elim l1
224[ #l2 %
225| #hd #tl #IH #l2 normalize elim (f hd) normalize >IH %
226]
227qed.
228
229
230include "utilities/option.ma".
231
232lemma position_of_from_aux : ∀A,test,l,n.
233  position_of_aux A test l n = !n' ← position_of A test l; return n + n'.
234#A #test #l elim l
235[ normalize #_ %
236| #hd #tl #IH #n
237  normalize in ⊢ (??%?); >IH
238  normalize elim (test hd) normalize
239  [ <plus_n_O %
240  | >(IH 1) whd in match (position_of ???);
241    elim (position_of_aux ??? 0) normalize
242    [ % | #x <plus_n_Sm % ]
243  ]
244]
245qed.
246
247definition position_of_safe ≝ λA,test,l.λprf : count A test l ≥ 1.
248  opt_safe ? (position_of A test l) ?.
249  lapply prf -prf elim l normalize
250  [ #ABS elim (absurd ? ABS (not_le_Sn_O 0))
251  |#hd #tl #IH elim (test hd) normalize
252    [2: >position_of_from_aux #H
253      change with (position_of_aux ????) in match (position_of ???);
254      >(opt_to_opt_safe … (IH H)) @opt_safe_elim normalize #x]
255    #_ % #ABS destruct(ABS)
256qed.
257
258definition index_of ≝
259  λA,test,l.λprf : eqb (count A test l) 1.position_of_safe A test l ?.
260  lapply prf -prf @eqb_elim #EQ * >EQ %
261qed.
262
263lemma position_of_append_hit : ∀A,test,l1,l2,x.
264  position_of A test (l1@l2) = Some ? x →
265    position_of A test l1 = Some ? x ∨
266      (position_of A test l1 = None ? ∧
267        ! p ← position_of A test l2 ; return (|l1| + p) = Some ? x).
268#A#test#l1 elim l1
269[ #l2 #x change with l2 in match (? @ l2); #EQ >EQ %2
270  % %
271| #hd #tl #IH #l2 #x
272  normalize elim (test hd) normalize
273  [#H %{H}
274  | >position_of_from_aux
275    lapply (refl … (position_of A test (tl@l2)))
276    generalize in ⊢ (???%→?); * [2: #n'] #Heq >Heq
277    normalize #EQ destruct(EQ)
278    elim (IH … Heq) -IH
279    [ #G %
280    | * #G #H
281     lapply (refl … (position_of A test l2))
282     generalize in ⊢ (???%→?); * [2: #x'] #H' >H' in H; normalize
283     #EQ destruct (EQ) %2 %]
284    >position_of_from_aux
285    [1,2: >G % | >H' %]
286  ]
287]
288qed.
289
290lemma position_of_hit : ∀A,test,l,x.position_of A test l = Some ? x →
291  count A test l ≥ 1.
292#A#test#l elim l normalize
293[#x #ABS destruct(ABS)
294|#hd #tl #IH #x elim (test hd) normalize [#EQ destruct(EQ) //]
295  >position_of_from_aux
296  lapply (refl … (position_of ? test tl))
297  generalize in ⊢ (???%→?); * [2: #x'] #Heq >Heq normalize
298  #EQ destruct(EQ) @(IH … Heq)
299]
300qed.
301
302lemma position_of_miss : ∀A,test,l.position_of A test l = None ? →
303  count A test l = 0.
304#A#test#l elim l normalize
305[ #_ %
306|#hd #tl #IH elim (test hd) normalize [#EQ destruct(EQ)]
307  >position_of_from_aux
308  lapply (refl … (position_of ? test tl))
309  generalize in ⊢ (???%→?); * [2: #x'] #Heq >Heq normalize
310  #EQ destruct(EQ) @(IH … Heq)
311]
312qed.
313
314
315lemma position_of_append_miss : ∀A,test,l1,l2.
316  position_of A test (l1@l2) = None ? →
317    position_of A test l1 = None ? ∧ position_of A test l2 = None ?.
318#A#test#l1 elim l1
319[ #l2 change with l2 in match (? @ l2); #EQ >EQ % %
320| #hd #tl #IH #l2
321  normalize elim (test hd) normalize
322  [#H destruct(H)
323  | >position_of_from_aux
324    lapply (refl … (position_of A test (tl@l2)))
325    generalize in ⊢ (???%→?); * [2: #n'] #Heq >Heq
326    normalize #EQ destruct(EQ)
327    elim (IH … Heq) #H1 #H2
328    >position_of_from_aux
329    >position_of_from_aux
330    >H1 >H2 normalize % %
331  ]
332]
333qed.
334
335
Note: See TracBrowser for help on using the repository browser.