1 | include "basics/types.ma". |
---|
2 | include "utilities/binary/positive.ma". |
---|
3 | include "ASM/Util.ma". (* bool_to_Prop *) |
---|
4 | |
---|
5 | inductive positive_map (A:Type[0]) : Type[0] ≝ |
---|
6 | | pm_leaf : positive_map A |
---|
7 | | pm_node : option A → positive_map A → positive_map A → positive_map A. |
---|
8 | |
---|
9 | let rec lookup_opt (A:Type[0]) (b:Pos) (t:positive_map A) on t : option A ≝ |
---|
10 | match t with |
---|
11 | [ pm_leaf ⇒ None ? |
---|
12 | | pm_node a l r ⇒ |
---|
13 | match b with |
---|
14 | [ one ⇒ a |
---|
15 | | p0 tl ⇒ lookup_opt A tl l |
---|
16 | | p1 tl ⇒ lookup_opt A tl r |
---|
17 | ] |
---|
18 | ]. |
---|
19 | |
---|
20 | definition lookup: ∀A:Type[0].Pos → positive_map A → A → A ≝ |
---|
21 | λA.λb.λt.λx. |
---|
22 | match lookup_opt A b t with |
---|
23 | [ None ⇒ x |
---|
24 | | Some r ⇒ r |
---|
25 | ]. |
---|
26 | |
---|
27 | let rec pm_set (A:Type[0]) (b:Pos) (a:option A) (t:positive_map A) on b : positive_map A ≝ |
---|
28 | match b with |
---|
29 | [ one ⇒ |
---|
30 | match t with |
---|
31 | [ pm_leaf ⇒ pm_node A a (pm_leaf A) (pm_leaf A) |
---|
32 | | pm_node _ l r ⇒ pm_node A a l r |
---|
33 | ] |
---|
34 | | p0 tl ⇒ |
---|
35 | match t with |
---|
36 | [ pm_leaf ⇒ pm_node A (None ?) (pm_set A tl a (pm_leaf A)) (pm_leaf A) |
---|
37 | | pm_node x l r ⇒ pm_node A x (pm_set A tl a l) r |
---|
38 | ] |
---|
39 | | p1 tl ⇒ |
---|
40 | match t with |
---|
41 | [ pm_leaf ⇒ pm_node A (None ?) (pm_leaf A) (pm_set A tl a (pm_leaf A)) |
---|
42 | | pm_node x l r ⇒ pm_node A x l (pm_set A tl a r) |
---|
43 | ] |
---|
44 | ]. |
---|
45 | |
---|
46 | definition insert : ∀A:Type[0]. Pos → A → positive_map A → positive_map A ≝ |
---|
47 | λA,p,a. pm_set A p (Some ? a). |
---|
48 | |
---|
49 | let rec update (A:Type[0]) (b:Pos) (a:A) (t:positive_map A) on b : option (positive_map A) ≝ |
---|
50 | match b with |
---|
51 | [ one ⇒ |
---|
52 | match t with |
---|
53 | [ pm_leaf ⇒ None ? |
---|
54 | | pm_node x l r ⇒ option_map ?? (λ_. pm_node A (Some ? a) l r) x |
---|
55 | ] |
---|
56 | | p0 tl ⇒ |
---|
57 | match t with |
---|
58 | [ pm_leaf ⇒ None ? |
---|
59 | | pm_node x l r ⇒ option_map ?? (λl. pm_node A x l r) (update A tl a l) |
---|
60 | ] |
---|
61 | | p1 tl ⇒ |
---|
62 | match t with |
---|
63 | [ pm_leaf ⇒ None ? |
---|
64 | | pm_node x l r ⇒ option_map ?? (λr. pm_node A x l r) (update A tl a r) |
---|
65 | ] |
---|
66 | ]. |
---|
67 | |
---|
68 | lemma lookup_opt_pm_set_hit : |
---|
69 | ∀A:Type[0].∀v:option A.∀b:Pos.∀t:positive_map A. |
---|
70 | lookup_opt … b (pm_set … b v t) = v. |
---|
71 | #A #v #b elim b |
---|
72 | [ * // |
---|
73 | | #tl #IH * |
---|
74 | [ whd in ⊢ (??%%); @IH |
---|
75 | | #x #l #r @IH |
---|
76 | ] |
---|
77 | | #tl #IH * |
---|
78 | [ whd in ⊢ (??%%); @IH |
---|
79 | | #x #l #r @IH |
---|
80 | ] |
---|
81 | ] qed. |
---|
82 | |
---|
83 | lemma lookup_opt_insert_hit : |
---|
84 | ∀A:Type[0].∀v:A.∀b:Pos.∀t:positive_map A. |
---|
85 | lookup_opt … b (insert … b v t) = Some A v. |
---|
86 | #A #v #b elim b |
---|
87 | [ * // |
---|
88 | | #tl #IH * |
---|
89 | [ whd in ⊢ (??%%); @IH |
---|
90 | | #x #l #r @IH |
---|
91 | ] |
---|
92 | | #tl #IH * |
---|
93 | [ whd in ⊢ (??%%); @IH |
---|
94 | | #x #l #r @IH |
---|
95 | ] |
---|
96 | ] qed. |
---|
97 | |
---|
98 | lemma lookup_insert_hit: |
---|
99 | ∀a: Type[0]. |
---|
100 | ∀v: a. |
---|
101 | ∀b: Pos. |
---|
102 | ∀t: positive_map a. |
---|
103 | ∀d: a. |
---|
104 | lookup … b (insert … b v t) d = v. |
---|
105 | #A #v #b #t #d whd in ⊢ (??%?); >lookup_opt_insert_hit % |
---|
106 | qed. |
---|
107 | |
---|
108 | lemma lookup_opt_pm_set_miss: |
---|
109 | ∀A:Type[0].∀v:option A.∀b,c:Pos.∀t:positive_map A. |
---|
110 | b ≠ c → lookup_opt … b (pm_set … c v t) = lookup_opt … b t. |
---|
111 | #A #v #b elim b |
---|
112 | [ * [ #t * #H elim (H (refl …)) |
---|
113 | | *: #c' #t #NE cases t // |
---|
114 | ] |
---|
115 | | #b' #IH * |
---|
116 | [ * [ #NE @refl | #x #l #r #NE @refl ] |
---|
117 | | #c' * [ #NE whd in ⊢ (??%%); @IH /2/ |
---|
118 | | #x #l #r #NE whd in ⊢ (??%%); @IH /2/ |
---|
119 | ] |
---|
120 | | #c' * // |
---|
121 | ] |
---|
122 | | #b' #IH * |
---|
123 | [ * [ #NE @refl | #x #l #r #NE @refl ] |
---|
124 | | #c' * // |
---|
125 | | #c' * [ #NE whd in ⊢ (??%%); @IH /2/ |
---|
126 | | #x #l #r #NE whd in ⊢ (??%%); @IH /2/ |
---|
127 | ] |
---|
128 | ] |
---|
129 | ] qed. |
---|
130 | |
---|
131 | lemma lookup_opt_insert_miss: |
---|
132 | ∀A:Type[0].∀v:A.∀b,c:Pos.∀t:positive_map A. |
---|
133 | b ≠ c → lookup_opt … b (insert … c v t) = lookup_opt … b t. |
---|
134 | #A #v #b elim b |
---|
135 | [ * [ #t * #H elim (H (refl …)) |
---|
136 | | *: #c' #t #NE cases t // |
---|
137 | ] |
---|
138 | | #b' #IH * |
---|
139 | [ * [ #NE @refl | #x #l #r #NE @refl ] |
---|
140 | | #c' * [ #NE whd in ⊢ (??%%); @IH /2/ |
---|
141 | | #x #l #r #NE whd in ⊢ (??%%); @IH /2/ |
---|
142 | ] |
---|
143 | | #c' * // |
---|
144 | ] |
---|
145 | | #b' #IH * |
---|
146 | [ * [ #NE @refl | #x #l #r #NE @refl ] |
---|
147 | | #c' * // |
---|
148 | | #c' * [ #NE whd in ⊢ (??%%); @IH /2/ |
---|
149 | | #x #l #r #NE whd in ⊢ (??%%); @IH /2/ |
---|
150 | ] |
---|
151 | ] |
---|
152 | ] qed. |
---|
153 | |
---|
154 | let rec fold (A, B: Type[0]) (f: Pos → A → B → B) |
---|
155 | (t: positive_map A) (b: B) on t: B ≝ |
---|
156 | match t with |
---|
157 | [ pm_leaf ⇒ b |
---|
158 | | pm_node a l r ⇒ |
---|
159 | let b ≝ match a with [ None ⇒ b | Some a ⇒ f one a b ] in |
---|
160 | let b ≝ fold A B (λx.f (p0 x)) l b in |
---|
161 | fold A B (λx.f (p1 x)) r b |
---|
162 | ]. |
---|
163 | |
---|
164 | definition domain_of_pm : ∀A. positive_map A → positive_map unit ≝ |
---|
165 | λA,t. fold A (positive_map unit) (λp,a,b. insert unit p it b) t (pm_leaf unit). |
---|
166 | |
---|
167 | (* Build some results about fold using domain_of_pm. *) |
---|
168 | |
---|
169 | lemma pm_fold_ignore_adding_junk : ∀A. ∀m. ∀f,g:Pos → Pos. ∀s. |
---|
170 | (∀p,q. f p ≠ g q) → |
---|
171 | ∀p. lookup_opt unit (f p) (fold A ? (λx,a,b. insert unit (g x) it b) m s) = lookup_opt unit (f p) s. |
---|
172 | #A #m elim m |
---|
173 | [ normalize // |
---|
174 | | #oa #l #r #IHl #IHr |
---|
175 | #f #g #s #not_g #p |
---|
176 | normalize |
---|
177 | >IHr |
---|
178 | [ >IHl |
---|
179 | [ cases oa |
---|
180 | [ normalize % |
---|
181 | | #a normalize >(lookup_opt_insert_miss ?? (f p)) [ % | @not_g ] |
---|
182 | ] |
---|
183 | | #x #y % #E cases (not_g x (p0 y)) #H @H @E |
---|
184 | ] |
---|
185 | | #x #y % #E cases (not_g x (p1 y)) #H @H @E |
---|
186 | ] |
---|
187 | ] qed. |
---|
188 | |
---|
189 | lemma pm_fold_ind_gen : ∀A,B,m,f,b. |
---|
190 | ∀pre:Pos→Pos. (∀x,y.pre x = pre y → x = y) → ∀ps. (∀p. lookup_opt ? (pre p) ps = None ?) → |
---|
191 | ∀P:B → positive_map unit → Prop. |
---|
192 | P b ps → |
---|
193 | (∀p,ps,a,b. lookup_opt unit (pre p) ps = None unit → lookup_opt A p m = Some A a → P b ps → P (f (pre p) a b) (insert unit (pre p) it ps)) → |
---|
194 | P (fold A B (λx. f (pre x)) m b) (fold ?? (λp,a,b. insert unit (pre p) it b) m ps). |
---|
195 | #A #B #m elim m |
---|
196 | [ // |
---|
197 | | #oa #l #r #IHl #IHr |
---|
198 | #f #b #pre #PRE #ps #PS #P #emp #step whd in ⊢ (?%%); |
---|
199 | @IHr |
---|
200 | [ #x #y #E lapply (PRE … E) #E' destruct // |
---|
201 | | #p change with ((λx. pre (p1 x)) p) in match (pre (p1 p)); >pm_fold_ignore_adding_junk |
---|
202 | [ cases oa [ @PS | #a >(lookup_opt_insert_miss ?? (pre (p1 p))) [ @PS | % #E lapply (PRE … E) #E' destruct ] ] |
---|
203 | | #p #q % #E lapply (PRE … E) #E' destruct |
---|
204 | ] |
---|
205 | | @IHl |
---|
206 | [ #x #y #E lapply (PRE … E) #E' destruct // |
---|
207 | | #p cases oa [ @PS | #a >(lookup_opt_insert_miss ?? (pre (p0 p))) [ @PS | % #E lapply (PRE … E) #E' destruct ] ] |
---|
208 | | cases oa in step ⊢ %; [ #_ @emp | #a #step normalize @step [ @PS | % | @emp ] ] |
---|
209 | | #p #ps' #a #b' @step |
---|
210 | ] |
---|
211 | | #p #ps' #a #b' @step |
---|
212 | ] |
---|
213 | ] qed. |
---|
214 | |
---|
215 | lemma pm_fold_ext : ∀A,B,m,f,b. |
---|
216 | fold A B f m b = fold A B (λx:Pos. f x) m b. |
---|
217 | #A #B #m elim m /4/ |
---|
218 | qed. |
---|
219 | |
---|
220 | (* Main result about folds. (Rather than producing a result about just the |
---|
221 | domain of m we could use the whole mapping, but we'd need a function to |
---|
222 | canonicalise maps because their representation can contain some junk.) *) |
---|
223 | |
---|
224 | lemma pm_fold_ind : ∀A,B,f,m,b. ∀P:B → positive_map unit → Prop. |
---|
225 | P b (pm_leaf unit) → |
---|
226 | (∀p,ps,a,b. lookup_opt unit p ps = None unit → lookup_opt A p m = Some A a → P b ps → P (f p a b) (insert unit p it ps)) → |
---|
227 | P (fold A B f m b) (domain_of_pm A m). |
---|
228 | #A #B #f #m #b #P #emp #step |
---|
229 | >pm_fold_ext |
---|
230 | whd in ⊢ (??%); |
---|
231 | @(pm_fold_ind_gen A B m f b (λx.x) ? (pm_leaf unit) ???) |
---|
232 | [ // |
---|
233 | | // |
---|
234 | | @emp |
---|
235 | | @step |
---|
236 | ] qed. |
---|
237 | |
---|
238 | lemma pm_fold_eq : ∀A,B,b,f. |
---|
239 | (∀p,k:Pos. ∀a,s1,s2. lookup_opt B p s1 = lookup_opt B p s2 → lookup_opt B p (f k a s1) = lookup_opt B p (f k a s2)) → |
---|
240 | ∀p,s1,s2. |
---|
241 | lookup_opt B p s1 = lookup_opt B p s2 → |
---|
242 | lookup_opt B p (fold A (positive_map B) f b s1) = lookup_opt B p (fold A (positive_map B) f b s2). |
---|
243 | #A #B #b elim b |
---|
244 | [ #f #H #p #s1 #s2 #H @H |
---|
245 | | #oa #l #r #IHl #IHr #f #H #p #s1 #s2 #H' |
---|
246 | whd in match (fold ???? s1); |
---|
247 | whd in match (fold ???? s2); |
---|
248 | @IHr [ #q #k #a #s1' #s2' #H'' @H @H'' ] |
---|
249 | @IHl [ #q #k #a #s1' #s2' #H'' @H @H'' ] |
---|
250 | cases oa [ @H' | #a @H @H' ] |
---|
251 | ] qed. |
---|
252 | |
---|
253 | |
---|
254 | lemma pm_fold_ignore_prefix : ∀A. ∀m. ∀pre:Pos → Pos. (∀x,y. pre x = pre y → x = y) → |
---|
255 | ∀p. ∀pre'. |
---|
256 | lookup_opt unit (pre p) (fold A ? (λx,a,b. insert unit (pre (pre' x)) it b) m (pm_leaf ?)) = |
---|
257 | lookup_opt unit p (fold A ? (λx,a,b. insert unit (pre' x) it b) m (pm_leaf ?)). |
---|
258 | #A #m #pre #PRE |
---|
259 | cut (∀p. lookup_opt unit (pre p) (pm_leaf unit) = lookup_opt unit p (pm_leaf unit)) [ // ] |
---|
260 | generalize in match (pm_leaf unit) in ⊢ ((? → ??%?) → ? → ? → ??%?); |
---|
261 | generalize in match (pm_leaf unit); |
---|
262 | elim m |
---|
263 | [ #s1 #s2 #S #p #pre' @S |
---|
264 | | #oa #l #r #IHl #IHr |
---|
265 | #s1 #s2 #S #p #pre' |
---|
266 | whd in ⊢ (??(???%)(???%)); |
---|
267 | @(IHr ???? (λx. pre' (p1 x))) |
---|
268 | #p' @IHl |
---|
269 | #p'' cases oa |
---|
270 | [ @S |
---|
271 | | #a cases (decidable_eq_pos p'' (pre' one)) |
---|
272 | [ #E destruct |
---|
273 | >(lookup_opt_insert_hit ?? (pre (pre' one))) |
---|
274 | >(lookup_opt_insert_hit ?? (pre' one)) |
---|
275 | % |
---|
276 | | #NE >(lookup_opt_insert_miss ??? (pre (pre' one))) |
---|
277 | [ >(lookup_opt_insert_miss ??? (pre' one)) // |
---|
278 | | % #E @(absurd … (PRE … E) NE) |
---|
279 | ] |
---|
280 | ] |
---|
281 | ] |
---|
282 | ] qed. |
---|
283 | |
---|
284 | (* Link the domain of a map (used in the result about fold) to the original |
---|
285 | map. *) |
---|
286 | |
---|
287 | lemma domain_of_pm_present : ∀A,m,p. |
---|
288 | lookup_opt A p m ≠ None ? ↔ lookup_opt unit p (domain_of_pm A m) ≠ None ?. |
---|
289 | |
---|
290 | #A #m whd in match (domain_of_pm ??); |
---|
291 | elim m |
---|
292 | [ #p normalize /3/ |
---|
293 | | #oa #l #r #IHl #IHr |
---|
294 | whd in match (fold ???? (pm_leaf unit)); |
---|
295 | * |
---|
296 | [ change with ((λx:Pos. one) one) in ⊢ (??(?(??(??%?)?))); |
---|
297 | >pm_fold_ignore_adding_junk [2: #p #q % #E destruct ] |
---|
298 | change with ((λx:Pos. one) one) in ⊢ (??(?(??(??%?)?))); |
---|
299 | >pm_fold_ignore_adding_junk [2: #p #q % #E destruct ] |
---|
300 | cases oa |
---|
301 | [ normalize /3/ |
---|
302 | | #a change with (insert unit one ??) in ⊢ (??(?(??(???%)?))); |
---|
303 | >(lookup_opt_insert_hit ?? one) normalize |
---|
304 | % #_ % #E destruct |
---|
305 | ] |
---|
306 | | #p >(pm_fold_eq ??????? (pm_leaf unit)) |
---|
307 | [ >pm_fold_ignore_prefix [2: #x #y #E destruct % ] |
---|
308 | @IHr |
---|
309 | | >pm_fold_ignore_adding_junk [2: #x #y % #E destruct ] |
---|
310 | cases oa |
---|
311 | [ % |
---|
312 | | #a >(lookup_opt_insert_miss ?? (p1 p)) [2: % #E destruct ] |
---|
313 | % |
---|
314 | ] |
---|
315 | | #x #y #a #s1 #s2 #S cases (decidable_eq_pos x (p1 y)) |
---|
316 | [ #E destruct |
---|
317 | >(lookup_opt_insert_hit ?? (p1 y)) |
---|
318 | >(lookup_opt_insert_hit ?? (p1 y)) |
---|
319 | % |
---|
320 | | #NE >(lookup_opt_insert_miss ?? x … NE) |
---|
321 | >(lookup_opt_insert_miss ?? x … NE) |
---|
322 | @S |
---|
323 | ] |
---|
324 | ] |
---|
325 | | #p |
---|
326 | >pm_fold_ignore_adding_junk [2: #x #y % #E destruct ] |
---|
327 | >(pm_fold_eq ??????? (pm_leaf unit)) |
---|
328 | [ >pm_fold_ignore_prefix [2: #x #y #E destruct % ] |
---|
329 | @IHl |
---|
330 | | cases oa |
---|
331 | [ % |
---|
332 | | #a >(lookup_opt_insert_miss ?? (p0 p)) [2: % #E destruct ] |
---|
333 | % |
---|
334 | ] |
---|
335 | | #x #y #a #s1 #s2 #S cases (decidable_eq_pos x (p0 y)) |
---|
336 | [ #E destruct |
---|
337 | >(lookup_opt_insert_hit ?? (p0 y)) |
---|
338 | >(lookup_opt_insert_hit ?? (p0 y)) |
---|
339 | % |
---|
340 | | #NE >(lookup_opt_insert_miss ?? x … NE) |
---|
341 | >(lookup_opt_insert_miss ?? x … NE) |
---|
342 | @S |
---|
343 | ] |
---|
344 | ] |
---|
345 | ] |
---|
346 | ] qed. |
---|
347 | |
---|
348 | |
---|
349 | lemma update_fail : ∀A,b,a,t. |
---|
350 | update A b a t = None ? → |
---|
351 | lookup_opt A b t = None ?. |
---|
352 | #A #b #a elim b |
---|
353 | [ * [ // | * // #x #l #r normalize #E destruct ] |
---|
354 | | #b' #IH * [ // | #x #l #r #U @IH whd in U:(??%?); |
---|
355 | cases (update A b' a r) in U ⊢ %; |
---|
356 | [ // | normalize #t #E destruct ] |
---|
357 | ] |
---|
358 | | #b' #IH * [ // | #x #l #r #U @IH whd in U:(??%?); |
---|
359 | cases (update A b' a l) in U ⊢ %; |
---|
360 | [ // | normalize #t #E destruct ] |
---|
361 | ] |
---|
362 | ] qed. |
---|
363 | |
---|
364 | lemma update_lookup_opt_same : ∀A,b,a,t,t'. |
---|
365 | update A b a t = Some ? t' → |
---|
366 | lookup_opt A b t' = Some ? a. |
---|
367 | #A #b #a elim b |
---|
368 | [ * [ #t' normalize #E destruct |
---|
369 | | * [ 2: #x ] #l #r #t' whd in ⊢ (??%? → ??%?); |
---|
370 | #E destruct // |
---|
371 | ] |
---|
372 | | #b' #IH * |
---|
373 | [ #t' normalize #E destruct |
---|
374 | | #x #l #r #t' whd in ⊢ (??%? → ??%?); |
---|
375 | lapply (IH r) |
---|
376 | cases (update A b' a r) |
---|
377 | [ #_ normalize #E destruct |
---|
378 | | #r' #H normalize #E destruct @H @refl |
---|
379 | ] |
---|
380 | ] |
---|
381 | | #b' #IH * |
---|
382 | [ #t' normalize #E destruct |
---|
383 | | #x #l #r #t' whd in ⊢ (??%? → ??%?); |
---|
384 | lapply (IH l) |
---|
385 | cases (update A b' a l) |
---|
386 | [ #_ normalize #E destruct |
---|
387 | | #r' #H normalize #E destruct @H @refl |
---|
388 | ] |
---|
389 | ] |
---|
390 | ] qed. |
---|
391 | |
---|
392 | lemma update_lookup_opt_other : ∀A,b,a,t,t'. |
---|
393 | update A b a t = Some ? t' → |
---|
394 | ∀b'. b ≠ b' → |
---|
395 | lookup_opt A b' t = lookup_opt A b' t'. |
---|
396 | #A #b #a elim b |
---|
397 | [ * [ #t' normalize #E destruct |
---|
398 | | * [2: #x] #l #r #t' normalize #E destruct |
---|
399 | * [ * #H elim (H (refl …)) ] |
---|
400 | #tl #NE normalize // |
---|
401 | ] |
---|
402 | | #b' #IH * |
---|
403 | [ #t' normalize #E destruct |
---|
404 | | #x #l #r #t' normalize |
---|
405 | lapply (IH r) |
---|
406 | cases (update A b' a r) |
---|
407 | [ #_ normalize #E destruct |
---|
408 | | #t'' #H normalize #E destruct * // #b'' #NE @H /2/ |
---|
409 | ] |
---|
410 | ] |
---|
411 | | #b' #IH * |
---|
412 | [ #t' normalize #E destruct |
---|
413 | | #x #l #r #t' normalize |
---|
414 | lapply (IH l) |
---|
415 | cases (update A b' a l) |
---|
416 | [ #_ normalize #E destruct |
---|
417 | | #t'' #H normalize #E destruct * // #b'' #NE @H /2/ |
---|
418 | ] |
---|
419 | ] |
---|
420 | ] qed. |
---|
421 | |
---|
422 | include "utilities/option.ma". |
---|
423 | |
---|
424 | let rec map_opt A B (f : A → option B) (b : positive_map A) on b : positive_map B ≝ |
---|
425 | match b with |
---|
426 | [ pm_leaf ⇒ pm_leaf ? |
---|
427 | | pm_node a l r ⇒ pm_node ? (!x ← a ; f x) |
---|
428 | (map_opt ?? f l) |
---|
429 | (map_opt ?? f r) |
---|
430 | ]. |
---|
431 | |
---|
432 | definition map ≝ λA,B,f.map_opt A B (λx. Some ? (f x)). |
---|
433 | |
---|
434 | lemma lookup_opt_map : ∀A,B,f,b,p. |
---|
435 | lookup_opt … p (map_opt A B f b) = ! x ← lookup_opt … p b ; f x. |
---|
436 | #A#B#f#b elim b [//] |
---|
437 | #a #l #r #Hil #Hir * [//] |
---|
438 | #p |
---|
439 | whd in ⊢ (??(???%)(????%?)); |
---|
440 | whd in ⊢ (??%?); // |
---|
441 | qed. |
---|
442 | |
---|
443 | lemma map_opt_id_eq_ext : ∀A,m,f.(∀x.f x = Some ? x) → map_opt A A f m = m. |
---|
444 | #A #m #f #Hf elim m [//] |
---|
445 | * [2:#x] #l #r #Hil #Hir normalize [>Hf] // |
---|
446 | qed. |
---|
447 | |
---|
448 | lemma map_opt_id : ∀A,m.map_opt A A (λx. Some ? x) m = m. |
---|
449 | #A #m @map_opt_id_eq_ext // |
---|
450 | qed. |
---|
451 | |
---|
452 | let rec merge A B C (choice : option A → option B → option C) |
---|
453 | (a : positive_map A) (b : positive_map B) on a : positive_map C ≝ |
---|
454 | match a with |
---|
455 | [ pm_leaf ⇒ map_opt ?? (λx.choice (None ?) (Some ? x)) b |
---|
456 | | pm_node o l r ⇒ |
---|
457 | match b with |
---|
458 | [ pm_leaf ⇒ map_opt ?? (λx.choice (Some ? x) (None ?)) a |
---|
459 | | pm_node o' l' r' ⇒ |
---|
460 | match (choice o o') with |
---|
461 | [None ⇒ |
---|
462 | match (merge … choice l l') with |
---|
463 | [pm_leaf ⇒ match (merge … choice r r') with |
---|
464 | [pm_leaf ⇒ pm_leaf ? |
---|
465 | |_ ⇒ pm_node ? (None ?) (merge … choice l l') (merge … choice r r') |
---|
466 | ] |
---|
467 | |_ ⇒ pm_node ? (None ?) (merge … choice l l') (merge … choice r r') |
---|
468 | ] |
---|
469 | |Some x ⇒ |
---|
470 | pm_node ? (Some ? x) |
---|
471 | (merge … choice l l') |
---|
472 | (merge … choice r r') |
---|
473 | ] |
---|
474 | ]]. |
---|
475 | |
---|
476 | lemma lookup_opt_merge : ∀A,B,C,choice,a,b,p. |
---|
477 | choice (None ?) (None ?) = None ? → |
---|
478 | lookup_opt … p (merge A B C choice a b) = |
---|
479 | choice (lookup_opt … p a) (lookup_opt … p b). |
---|
480 | #A#B#C#choice#a elim a |
---|
481 | [ normalize #b |
---|
482 | | #o#l#r#Hil#Hir * [2: #o'#l'#r' * [2,3: #x] normalize cases(choice o o') normalize /2 by / |
---|
483 | [3: cases (merge A B C choice l l') normalize /2 by / cases(merge A B C choice r r') |
---|
484 | normalize /2 by / |
---|
485 | |*: #H [<Hir|<Hil] /2 by / cases(merge A B C choice l l') normalize /2 by / |
---|
486 | cases (merge A B C choice r r') normalize /2 by / |
---|
487 | ] |
---|
488 | ]] |
---|
489 | #p #choice_good >lookup_opt_map |
---|
490 | elim (lookup_opt ???) |
---|
491 | normalize // |
---|
492 | qed. |
---|
493 | |
---|
494 | let rec domain_size (A : Type[0]) (b : positive_map A) on b : ℕ ≝ |
---|
495 | match b with |
---|
496 | [ pm_leaf ⇒ 0 |
---|
497 | | pm_node a l r ⇒ |
---|
498 | (match a with |
---|
499 | [ Some _ ⇒ 1 |
---|
500 | | None ⇒ 0 |
---|
501 | ]) + domain_size A l + domain_size A r |
---|
502 | ]. |
---|
503 | |
---|
504 | interpretation "positive map size" 'card p = (domain_size ? p). |
---|
505 | |
---|
506 | lemma map_opt_size : ∀A,B,f,b.|map_opt A B f b| ≤ |b|. |
---|
507 | #A#B#f#b elim b [//] |
---|
508 | *[2:#x]#l#r#Hil#Hir normalize |
---|
509 | [elim (f ?) normalize [@(transitive_le ? ? ? ? (le_n_Sn ?)) | #y @le_S_S ] ] |
---|
510 | @le_plus assumption |
---|
511 | qed. |
---|
512 | |
---|
513 | lemma map_size : ∀A,B,f,b.|map A B f b| = |b|. |
---|
514 | #A#B#f#b elim b [//] |
---|
515 | *[2:#x]#l#r#Hil#Hir normalize |
---|
516 | >Hil >Hir @refl |
---|
517 | qed. |
---|
518 | |
---|
519 | lemma lookup_opt_O_lt_size : ∀A,m,p. lookup_opt A p m ≠ None ? → 0 < |m|. |
---|
520 | #A#m elim m |
---|
521 | [#p normalize #F elim (absurd ? (refl ??) F) |
---|
522 | |* [2: #x] #l #r #Hil #Hir * normalize |
---|
523 | [1,2,3: // |
---|
524 | |4: #F elim (absurd ? (refl ??) F)] |
---|
525 | #p #H [@(transitive_le … (Hir ? H)) | @(transitive_le … (Hil ? H))] // |
---|
526 | qed. |
---|
527 | |
---|
528 | lemma insert_size : ∀A,p,a,m. |insert A p a m| = |
---|
529 | (match lookup_opt … p m with [Some _ ⇒ 0 | None ⇒ 1]) + |m|. |
---|
530 | #A#p elim p |
---|
531 | [ #a * [2: * [2: #x] #l #r] normalize // |
---|
532 | |*: #p #Hi #a * [2,4: * [2,4: #x] #l #r] normalize >Hi // |
---|
533 | ] qed. |
---|
534 | |
---|
535 | (* Testing all of the entries with a boolean predicate, where the predicate |
---|
536 | is given a proof that the entry is in the map. *) |
---|
537 | |
---|
538 | let rec pm_all_aux (A:Type[0]) (m,t:positive_map A) (pre:Pos → Pos) on t : (∀p. lookup_opt A (pre p) m = lookup_opt A p t) → (∀p,a.lookup_opt A p m = Some A a → bool) → bool ≝ |
---|
539 | match t return λt. (∀p. lookup_opt A (pre p) m = lookup_opt A p t) → (∀p,a.lookup_opt A p m = Some A a → bool) → bool with |
---|
540 | [ pm_leaf ⇒ λ_.λ_. true |
---|
541 | | pm_node a l r ⇒ λH,f. |
---|
542 | pm_all_aux A m l (λx. pre (p0 x)) ? f ∧ |
---|
543 | ((match a return λa. (∀p. ? = lookup_opt A p (pm_node ? a ??)) → ? with [ None ⇒ λ_. true | Some a' ⇒ λH. f (pre one) a' ? ]) H) ∧ |
---|
544 | pm_all_aux A m r (λx. pre (p1 x)) ? f |
---|
545 | ]. |
---|
546 | [ >H % |
---|
547 | | #p >H % |
---|
548 | | #p >H % |
---|
549 | ] qed. |
---|
550 | |
---|
551 | definition pm_all : ∀A. ∀m:positive_map A. (∀p,a. lookup_opt A p m = Some A a → bool) → bool ≝ |
---|
552 | λA,m,f. pm_all_aux A m m (λx.x) (λp. refl ??) f. |
---|
553 | |
---|
554 | (* Proof irrelevance doesn't seem to apply to arbitrary variables, but we can |
---|
555 | use the function graph to show that it's fine. *) |
---|
556 | |
---|
557 | inductive pm_all_pred_graph : ∀A,m,p,a,L. ∀f:∀p:Pos.∀a:A.lookup_opt A p m=Some A a→bool. bool → Prop ≝ |
---|
558 | | pmallpg : ∀A,m,p,a,L. ∀f:∀p:Pos.∀a:A.lookup_opt A p m=Some A a→bool. pm_all_pred_graph A m p a L f (f p a L). |
---|
559 | |
---|
560 | lemma pm_all_pred_irr : ∀A,m,p,a,L,L'. |
---|
561 | ∀f:∀p:Pos.∀a:A.lookup_opt A p m=Some A a→bool. |
---|
562 | f p a L = f p a L'. |
---|
563 | #A #m #p #a #L #L' #f |
---|
564 | cut (pm_all_pred_graph A m p a L f (f p a L)) [ % ] |
---|
565 | cases (f p a L) #H cut (pm_all_pred_graph A m p a L' f ?) [2,5: @H | 1,4: skip] |
---|
566 | * // |
---|
567 | qed. |
---|
568 | |
---|
569 | lemma pm_all_aux_ok_aux : ∀A,m,t,pre,H,f,a,L. |
---|
570 | pm_all_aux A m t pre H f → |
---|
571 | lookup_opt A one t = Some A a → |
---|
572 | f (pre one) a L. |
---|
573 | #A #m * |
---|
574 | [ #pre #H #f #a #L #H #L' normalize in L'; destruct |
---|
575 | | * [ #l #r #pre #H #f #a #L #H' #L' normalize in L'; destruct |
---|
576 | | #a' #l #r #pre #H #f #a #L #AUX #L' normalize in L'; destruct |
---|
577 | cases (andb_Prop_true … AUX) #AUX' cases (andb_Prop_true … AUX') |
---|
578 | #_ #HERE #_ whd in HERE:(?%); @eq_true_to_b <HERE @pm_all_pred_irr |
---|
579 | ] |
---|
580 | ] qed. |
---|
581 | |
---|
582 | lemma pm_all_aux_ok : ∀A,m,t,pre,H,f. |
---|
583 | bool_to_Prop (pm_all_aux A m t pre H f) ↔ (∀p,a,H. f (pre p) a H). |
---|
584 | #A #m #t #pre #H #f % |
---|
585 | [ #H' #p generalize in match pre in H H' ⊢ %; -pre generalize in match t; elim p |
---|
586 | [ #t #pre #H #H' #a #L lapply (refl ? (Some A a)) <L in ⊢ (??%? → ?); >H @pm_all_aux_ok_aux // |
---|
587 | | #q #IH * [ #pre #H #H' #a #L @⊥ >H in L; normalize #E destruct |
---|
588 | | #x #l #r #pre #H #H' #a #L @(IH r) |
---|
589 | [ #x >H % | cases (andb_Prop_true … H') #_ // ] |
---|
590 | ] |
---|
591 | | #q #IH * [ #pre #H #H' #a #L @⊥ >H in L; normalize #E destruct |
---|
592 | | #x #l #r #pre #H #H' #a #L @(IH l) |
---|
593 | [ #x >H % | cases (andb_Prop_true … H') #H'' cases (andb_Prop_true … H'') // ] |
---|
594 | ] |
---|
595 | ] |
---|
596 | | #H' generalize in match pre in H H' ⊢ %; -pre elim t |
---|
597 | [ // |
---|
598 | | * [2:#x] #l #r #IHl #IHr #pre #H #H' @andb_Prop |
---|
599 | [ 1,3: @andb_Prop |
---|
600 | [ 1,3: @IHl // |
---|
601 | | @(H' one x) |
---|
602 | | % |
---|
603 | ] |
---|
604 | | 2,4: @IHr // |
---|
605 | ] |
---|
606 | ] |
---|
607 | ] qed. |
---|
608 | |
---|
609 | lemma pm_all_ok : ∀A,m,f. |
---|
610 | bool_to_Prop (pm_all A m f) ↔ (∀p,a,H. f p a H). |
---|
611 | #A #m #f @pm_all_aux_ok |
---|
612 | qed. |
---|
613 | |
---|
614 | |
---|
615 | (* Attempt to choose an entry in the map, and if successful return the entry |
---|
616 | and the map without it. *) |
---|
617 | |
---|
618 | let rec pm_choose (A:Type[0]) (t:positive_map A) on t : option (Pos × A × (positive_map A)) ≝ |
---|
619 | match t with |
---|
620 | [ pm_leaf ⇒ None ? |
---|
621 | | pm_node a l r ⇒ |
---|
622 | match pm_choose A l with |
---|
623 | [ None ⇒ |
---|
624 | match pm_choose A r with |
---|
625 | [ None ⇒ |
---|
626 | match a with |
---|
627 | [ None ⇒ None ? |
---|
628 | | Some a ⇒ Some ? 〈〈one, a〉, pm_leaf A〉 |
---|
629 | ] |
---|
630 | | Some x ⇒ Some ? 〈〈p1 (\fst (\fst x)), \snd (\fst x)〉, pm_node A a l (\snd x)〉 |
---|
631 | ] |
---|
632 | | Some x ⇒ Some ? 〈〈p0 (\fst (\fst x)), \snd (\fst x)〉, pm_node A a (\snd x) r〉 |
---|
633 | ] |
---|
634 | ]. |
---|
635 | |
---|
636 | lemma pm_choose_empty : ∀A,t. |
---|
637 | pm_choose A t = None ? ↔ ∀p. lookup_opt A p t = None A. |
---|
638 | #A #t elim t |
---|
639 | [ % // |
---|
640 | | * |
---|
641 | [ #l #r * #IHl #IHl' * #IHr #IHr' % |
---|
642 | [ #C * |
---|
643 | [ % |
---|
644 | | #p whd in C:(??%?) ⊢ (??%?); |
---|
645 | @IHr cases (pm_choose A r) in C ⊢ %; |
---|
646 | [ // |
---|
647 | | #x normalize cases (pm_choose A l) [2: #y] normalize #E destruct |
---|
648 | ] |
---|
649 | | #p whd in C:(??%?) ⊢ (??%?); |
---|
650 | @IHl cases (pm_choose A l) in C ⊢ %; |
---|
651 | [ // |
---|
652 | | #x normalize #E destruct |
---|
653 | ] |
---|
654 | ] |
---|
655 | | #L whd in ⊢ (??%?); >IHl' [ normalize >IHr' [ % | #p @(L (p1 p)) ] | #p @(L (p0 p)) ] |
---|
656 | ] |
---|
657 | | #a #l #r #IHl #IHr % |
---|
658 | [ normalize cases (pm_choose A l) [2: normalize #x #E destruct ] |
---|
659 | normalize cases (pm_choose A r) [2: #x] normalize #E destruct |
---|
660 | | #L lapply (L one) normalize #E destruct |
---|
661 | ] |
---|
662 | ] |
---|
663 | ] qed. |
---|
664 | |
---|
665 | lemma pm_choose_empty_card : ∀A,t. |
---|
666 | pm_choose A t = None ? ↔ |t| = 0. |
---|
667 | #A #t elim t |
---|
668 | [ /2/ |
---|
669 | | * [ #l #r * #IHl #IHl' * #IHr #IHr' % |
---|
670 | [ normalize lapply IHl cases (pm_choose A l) [2: #x #_ #E normalize in E; destruct ] |
---|
671 | normalize lapply IHr cases (pm_choose A r) [2: #x #_ #_ #E normalize in E; destruct ] |
---|
672 | #H1 #H2 #_ >(H1 (refl ??)) >(H2 (refl ??)) % |
---|
673 | | normalize #CARD >IHl' [ >IHr' ] /2 by refl, le_n_O_to_eq/ |
---|
674 | ] |
---|
675 | | #a #l #r #_ #_ % normalize |
---|
676 | [ cases (pm_choose A l) [ cases (pm_choose A r) [ | #x ] | #x ] |
---|
677 | #E normalize in E; destruct |
---|
678 | | #E destruct |
---|
679 | ] |
---|
680 | ] |
---|
681 | ] qed. |
---|
682 | |
---|
683 | lemma pm_choose_some : ∀A,t,p,a,t'. |
---|
684 | pm_choose A t = Some ? 〈〈p,a〉,t'〉 → |
---|
685 | lookup_opt A p t = Some A a ∧ |
---|
686 | lookup_opt A p t' = None A ∧ |
---|
687 | (∀q. p = q ∨ lookup_opt A q t = lookup_opt A q t') ∧ |
---|
688 | |t| = S (|t'|). |
---|
689 | #A #t elim t |
---|
690 | [ #p #a #t' normalize #E destruct |
---|
691 | | #ao #l #r #IHl #IHr * |
---|
692 | [ #a #t' normalize |
---|
693 | lapply (pm_choose_empty_card A l) |
---|
694 | lapply (pm_choose_empty A l) |
---|
695 | cases (pm_choose A l) normalize [2: #x #_ #_ #E destruct ] * #EMPl #EMPl' * #CARDl #CARDl' |
---|
696 | lapply (pm_choose_empty_card A r) |
---|
697 | lapply (pm_choose_empty A r) |
---|
698 | cases (pm_choose A r) normalize [2: #x #_ #_ #E destruct ] * #EMPr #EMPr' * #CARDr #CARDr' |
---|
699 | cases ao normalize [2:#x] #E destruct |
---|
700 | % [ % /2/ * /2/ #q %2 whd in ⊢ (??%%); /2/ | >CARDl >CARDr // ] |
---|
701 | | #p #a #t' normalize |
---|
702 | lapply (pm_choose_empty_card A l) |
---|
703 | lapply (pm_choose_empty A l) |
---|
704 | cases (pm_choose A l) normalize [2: #x #_ #_ #E destruct ] * #EMPl #EMPl' * #CARDl #CARDl' |
---|
705 | lapply (IHr p) |
---|
706 | cases (pm_choose A r) normalize [ #_ cases ao [2:#a'] normalize #E destruct ] |
---|
707 | * * #p' #a' #t'' #IH #E destruct |
---|
708 | cases (IH ?? (refl ??)) * * #L1 #L2 #L3 #CARD |
---|
709 | % [ % [ % [ // | @L2 ]| * /2/ #q cases (L3 q) /2/ #L4 %2 @L4 ] | cases ao normalize >CARD // ] |
---|
710 | | #p #a #t' normalize |
---|
711 | lapply (IHl p) |
---|
712 | cases (pm_choose A l) normalize |
---|
713 | [ #_ cases (pm_choose A r) normalize [ cases ao [2:#a'] normalize #E destruct |
---|
714 | | #x #E destruct ] |
---|
715 | | * * #p' #a' #t'' #IH #E destruct |
---|
716 | cases (IH ?? (refl ??)) * * #L1 #L2 #L3 #CARD |
---|
717 | % [ % [ % [ // | @L2 ]| * /2/ #q cases (L3 q) /2/ #L4 %2 @L4 ] | cases ao normalize >CARD // ] |
---|
718 | ] |
---|
719 | ] |
---|
720 | ] qed. |
---|
721 | |
---|
722 | (* Try to remove an element, return updated map if successful *) |
---|
723 | |
---|
724 | let rec pm_try_remove (A:Type[0]) (b:Pos) (t:positive_map A) on b : option (A × (positive_map A)) ≝ |
---|
725 | match b with |
---|
726 | [ one ⇒ |
---|
727 | match t with |
---|
728 | [ pm_leaf ⇒ None ? |
---|
729 | | pm_node x l r ⇒ option_map ?? (λx. 〈x, pm_node A (None ?) l r〉) x |
---|
730 | ] |
---|
731 | | p0 tl ⇒ |
---|
732 | match t with |
---|
733 | [ pm_leaf ⇒ None ? |
---|
734 | | pm_node x l r ⇒ option_map ?? (λxl. 〈\fst xl, pm_node A x (\snd xl) r〉) (pm_try_remove A tl l) |
---|
735 | ] |
---|
736 | | p1 tl ⇒ |
---|
737 | match t with |
---|
738 | [ pm_leaf ⇒ None ? |
---|
739 | | pm_node x l r ⇒ option_map ?? (λxr. 〈\fst xr, pm_node A x l (\snd xr)〉) (pm_try_remove A tl r) |
---|
740 | ] |
---|
741 | ]. |
---|
742 | |
---|
743 | lemma pm_try_remove_none : ∀A,b,t. |
---|
744 | pm_try_remove A b t = None ? ↔ lookup_opt A b t = None ?. |
---|
745 | #A #b elim b |
---|
746 | [ * [ /2/ | * [ /2/ | #a #l #r % normalize #E destruct ] ] |
---|
747 | | #p #IH * [ /2/ | #x #l #r % normalize cases (IH r) cases (pm_try_remove A p r) |
---|
748 | [ normalize #H #X @H | * #a #t' #_ normalize #_ #X destruct |
---|
749 | | normalize // | * #a #t' normalize #A #B #C lapply (B C) #E destruct |
---|
750 | ] ] |
---|
751 | | #p #IH * [ /2/ | #x #l #r % normalize cases (IH l) cases (pm_try_remove A p l) |
---|
752 | [ normalize #H #X @H | * #a #t' #_ normalize #_ #X destruct |
---|
753 | | normalize // | * #a #t' normalize #A #B #C lapply (B C) #E destruct |
---|
754 | ] ] |
---|
755 | ] qed. |
---|
756 | |
---|
757 | lemma pm_try_remove_some : ∀A,p,t,a,t'. |
---|
758 | pm_try_remove A p t = Some ? 〈a,t'〉 → |
---|
759 | lookup_opt A p t = Some A a ∧ |
---|
760 | lookup_opt A p t' = None A ∧ |
---|
761 | (∀q. p = q ∨ lookup_opt A q t = lookup_opt A q t') ∧ |
---|
762 | |t| = S (|t'|). |
---|
763 | #A #p elim p |
---|
764 | [ * [ #a #t' #E normalize in E; destruct |
---|
765 | | * [ #l #r #a #t' #E normalize in E; destruct |
---|
766 | | #a' #l #r #a #t' #E normalize in E; destruct % [ % [ % // | * /2/ ]| // ] |
---|
767 | ] |
---|
768 | ] |
---|
769 | | #p' #IH * |
---|
770 | [ #a #t' #E normalize in E; destruct |
---|
771 | | #x #l #r #a #t' whd in ⊢ (??%? → ?); |
---|
772 | lapply (IH r) cases (pm_try_remove A p' r) [ #_ #E normalize in E; destruct ] |
---|
773 | * #a' #r' #H #E whd in E:(??%?); destruct |
---|
774 | cases (H a r' (refl ??)) * * #L1 #L2 #L3 #CARD |
---|
775 | @conj try @conj try @conj |
---|
776 | [ @L1 | @L2 | * /2/ #q cases (L3 q) [ /2/ | #E %2 @E ] | cases x normalize >CARD // ] |
---|
777 | ] |
---|
778 | | #p' #IH * |
---|
779 | [ #a #t' #E normalize in E; destruct |
---|
780 | | #x #l #r #a #t' whd in ⊢ (??%? → ?); |
---|
781 | lapply (IH l) cases (pm_try_remove A p' l) [ #_ #E normalize in E; destruct ] |
---|
782 | * #a' #l' #H #E whd in E:(??%?); destruct |
---|
783 | cases (H a l' (refl ??)) * * #L1 #L2 #L3 #CARD |
---|
784 | @conj try @conj try @conj |
---|
785 | [ @L1 | @L2 | * /2/ #q cases (L3 q) [ /2/ | #E %2 @E ] | cases x normalize >CARD // ] |
---|
786 | ] |
---|
787 | ] qed. |
---|
788 | |
---|
789 | lemma pm_try_remove_some' : ∀A,p,t,a. |
---|
790 | lookup_opt A p t = Some A a → |
---|
791 | ∃t'. pm_try_remove A p t = Some ? 〈a,t'〉. |
---|
792 | #A #p elim p |
---|
793 | [ * [ #a #L normalize in L; destruct |
---|
794 | | #q #l #r #a #L normalize in L; destruct % [2: % | skip ] |
---|
795 | ] |
---|
796 | | #q #IH * |
---|
797 | [ #a #L normalize in L; destruct |
---|
798 | | #x #l #r #a #L cases (IH r a L) #r' #R |
---|
799 | % [2: whd in ⊢ (??%?); >R in ⊢ (??%?); % | skip ] |
---|
800 | ] |
---|
801 | | #q #IH * |
---|
802 | [ #a #L normalize in L; destruct |
---|
803 | | #x #l #r #a #L cases (IH l a L) #l' #R |
---|
804 | % [2: whd in ⊢ (??%?); >R in ⊢ (??%?); % | skip ] |
---|
805 | ] |
---|
806 | ] qed. |
---|
807 | |
---|
808 | (* An "informative" dependently typed fold *) |
---|
809 | |
---|
810 | let rec pm_fold_inf_aux |
---|
811 | (A, B: Type[0]) |
---|
812 | (t: positive_map A) |
---|
813 | (f: ∀p:Pos. ∀a:A. lookup_opt … p t = Some A a → B → B) |
---|
814 | (t': positive_map A) |
---|
815 | (pre: Pos → Pos) |
---|
816 | (b: B) on t': (∀p. lookup_opt … p t' = lookup_opt … (pre p) t) → B ≝ |
---|
817 | match t' return λt'. (∀p. lookup_opt A p t' = lookup_opt A (pre p) t) → B with |
---|
818 | [ pm_leaf ⇒ λ_. b |
---|
819 | | pm_node a l r ⇒ λH. |
---|
820 | let b ≝ match a return λa. lookup_opt A one (pm_node A a ??) = ? → B with [ None ⇒ λ_.b | Some a ⇒ λH. f (pre one) a ? b ] (H one) in |
---|
821 | let b ≝ pm_fold_inf_aux A B t f l (λx. pre (p0 x)) b ? in |
---|
822 | pm_fold_inf_aux A B t f r (λx. pre (p1 x)) b ? |
---|
823 | ]. |
---|
824 | [ #p @(H (p1 p)) | #p @(H (p0 p)) | <H % ] |
---|
825 | qed. |
---|
826 | |
---|
827 | definition pm_fold_inf : ∀A, B: Type[0]. ∀t: positive_map A. |
---|
828 | ∀f: (∀p:Pos. ∀a:A. lookup_opt … p t = Some A a → B → B). B → B ≝ |
---|
829 | λA,B,t,f,b. pm_fold_inf_aux A B t f t (λx.x) b (λp. refl ??). |
---|
830 | |
---|
831 | (* Find an entry in the map matching the given predicate *) |
---|
832 | |
---|
833 | let rec pm_find_aux (pre: Pos → Pos) (A:Type[0]) (t: positive_map A) (p:Pos → A → bool) on t : option (Pos × A) ≝ |
---|
834 | match t with |
---|
835 | [ pm_leaf ⇒ None ? |
---|
836 | | pm_node a l r ⇒ |
---|
837 | let x ≝ match a with |
---|
838 | [ Some a ⇒ if p (pre one) a then Some ? 〈pre one, a〉 else None ? |
---|
839 | | None ⇒ None ? |
---|
840 | ] in |
---|
841 | match x with |
---|
842 | [ Some x ⇒ Some ? x |
---|
843 | | None ⇒ |
---|
844 | match pm_find_aux (λx. pre (p0 x)) A l p with |
---|
845 | [ None ⇒ pm_find_aux (λx. pre (p1 x)) A r p |
---|
846 | | Some y ⇒ Some ? y |
---|
847 | ] |
---|
848 | ] |
---|
849 | ]. |
---|
850 | |
---|
851 | definition pm_find : ∀A:Type[0]. positive_map A → (Pos → A → bool) → option (Pos × A) ≝ |
---|
852 | pm_find_aux (λx.x). |
---|
853 | |
---|
854 | lemma pm_find_aux_pre : ∀A,t,pre,p,q,a. |
---|
855 | pm_find_aux pre A t p = Some ? 〈q,a〉 → |
---|
856 | ∃q'. q = pre q'. |
---|
857 | #A #t elim t |
---|
858 | [ normalize #pre #p #q #a #E destruct |
---|
859 | | * [2:#a] #l #r #IHl #IHr #pre #p #q #a' normalize |
---|
860 | [ cases (p (pre one) a) normalize [ #E destruct /2/ ] ] |
---|
861 | lapply (IHl (λx. pre (p0 x)) p) |
---|
862 | cases (pm_find_aux ?? l ?) |
---|
863 | [ 1,3: #_ normalize |
---|
864 | lapply (IHr (λx. pre (p1 x)) p) |
---|
865 | cases (pm_find_aux ?? r ?) |
---|
866 | [ 1,3: #_ #E destruct |
---|
867 | | *: * #q' #a'' #H #E destruct cases (H ?? (refl ??)) #q'' #E destruct /2/ |
---|
868 | ] |
---|
869 | | *: * #q' #a'' #H normalize #E destruct cases (H ?? (refl ??)) #q'' #E destruct /2/ |
---|
870 | ] |
---|
871 | ] qed. |
---|
872 | |
---|
873 | (* XXX: Hmm... destruct doesn't work properly with this, not sure why *) |
---|
874 | lemma option_pair_f_eq : ∀A,B:Type[0].∀a,a',b,b'. ∀f:A → A. |
---|
875 | (∀a,a'. f a = f a' → a = a') → |
---|
876 | Some (A×B) 〈f a,b〉 = Some ? 〈f a',b'〉 → |
---|
877 | a = a' ∧ b = b'. |
---|
878 | #A #B #a #a' #b #b' #f #EXT #E |
---|
879 | cut (f a = f a') [ destruct (E) destruct (e0) @e2 (* WTF? *) ] |
---|
880 | #E' >(EXT … E') in E ⊢ %; #E destruct /2/ |
---|
881 | qed. |
---|
882 | |
---|
883 | lemma pm_find_lookup : ∀A,p,q,a,t. |
---|
884 | pm_find A t p = Some ? 〈q,a〉 → |
---|
885 | lookup_opt A q t = Some ? a. |
---|
886 | #A #p #q #a normalize #t |
---|
887 | change with ((λx.x) q) in match q in ⊢ (% → ?); |
---|
888 | cut (∀y,z:Pos. (λx.x) y = (λx.x) z → y = z) // |
---|
889 | generalize in match q; -q generalize in match (λx.x); |
---|
890 | elim t |
---|
891 | [ #pre #q #_ normalize #E destruct |
---|
892 | | * [2:#a'] #l #r #IHl #IHr #pre #q #PRE |
---|
893 | normalize [ cases (p (pre one) a') normalize [ #E cases (option_pair_f_eq … pre PRE E) #E1 #E2 destruct normalize % ] ] |
---|
894 | lapply (IHl (λx.pre (p0 x))) |
---|
895 | lapply (pm_find_aux_pre A l (λx.pre (p0 x)) p) |
---|
896 | cases (pm_find_aux ?? l ?) |
---|
897 | [ 1,3: #_ #_ normalize |
---|
898 | lapply (IHr (λx.pre (p1 x))) |
---|
899 | lapply (pm_find_aux_pre A r (λx.pre (p1 x)) p) |
---|
900 | cases (pm_find_aux ?? r ?) |
---|
901 | [ 1,3: #_ #_ #E destruct |
---|
902 | | *: * #q' #a' #H1 #H2 #E destruct cases (H1 ?? (refl ??)) #q'' #E lapply (PRE … E) #E' destruct |
---|
903 | whd in ⊢ (??%?); @H2 // |
---|
904 | #y #z #E lapply (PRE … E) #E' destruct // |
---|
905 | ] |
---|
906 | | *: * #q' #a' #H1 #H2 normalize #E destruct cases (H1 ?? (refl ??)) #q'' #E lapply (PRE … E) #E' destruct |
---|
907 | whd in ⊢ (??%?); @H2 // |
---|
908 | #y #z #E lapply (PRE … E) #E' destruct // |
---|
909 | ] |
---|
910 | ] qed. |
---|
911 | |
---|
912 | lemma pm_find_aux_none : ∀A,t,pre,p,q,a. |
---|
913 | pm_find_aux pre A t p = None ? → |
---|
914 | lookup_opt A q t = Some ? a → |
---|
915 | ¬ p (pre q) a. |
---|
916 | #A #t elim t |
---|
917 | [ #pre #p #q #a #_ normalize #E destruct |
---|
918 | | #oa #l #r #IHl #IHr #pre #p * |
---|
919 | [ #a cases oa |
---|
920 | [ #_ normalize #E destruct |
---|
921 | | #a' #F whd in F:(??%?); whd in F:(??match % with [_⇒?|_⇒?]?); |
---|
922 | #E normalize in E; destruct |
---|
923 | cases (p (pre one) a) in F ⊢ %; // |
---|
924 | normalize #E destruct |
---|
925 | ] |
---|
926 | | #q #a #F #L @(IHr (λx. pre (p1 x)) p q a ? L) |
---|
927 | whd in F:(??%?); cases oa in F; |
---|
928 | [2: #a' normalize cases (p (pre one) a') [ normalize #E destruct ] ] |
---|
929 | normalize cases (pm_find_aux ?? l p) normalize // |
---|
930 | #x #E destruct |
---|
931 | | #q #a #F #L @(IHl (λx. pre (p0 x)) p q a ? L) |
---|
932 | whd in F:(??%?); cases oa in F; |
---|
933 | [2: #a' normalize cases (p (pre one) a') [ normalize #E destruct ] ] |
---|
934 | normalize cases (pm_find_aux ?? l p) normalize // |
---|
935 | ] |
---|
936 | ] qed. |
---|
937 | |
---|
938 | lemma pm_find_none : ∀A,t,p,q,a. |
---|
939 | pm_find A t p = None ? → |
---|
940 | lookup_opt A q t = Some ? a → |
---|
941 | ¬ p q a. |
---|
942 | #A #t |
---|
943 | @(pm_find_aux_none A t (λx.x)) |
---|
944 | qed. |
---|
945 | |
---|
946 | lemma pm_find_predicate : ∀A,t,p,q,a. |
---|
947 | pm_find A t p = Some ? 〈q,a〉 → |
---|
948 | p q a. |
---|
949 | #A #t #p normalize #q |
---|
950 | change with ((λx.x) q) in match q; generalize in match q; -q generalize in match (λx.x); |
---|
951 | elim t |
---|
952 | [ normalize #pre #q #a #E destruct |
---|
953 | | * [2:#a'] #l #r #IHl #IHr #pre #q #a normalize |
---|
954 | [ lapply (refl ? (p (pre one) a')) cases (p (pre one) a') in ⊢ (???% → %); |
---|
955 | [ #E normalize #E' |
---|
956 | cut (pre one = pre q) [ destruct (E') destruct (e0) @e2 (* XXX ! *) ] |
---|
957 | #E'' <E'' in E' ⊢ %; #E' destruct >E % |
---|
958 | | #_ normalize |
---|
959 | ] |
---|
960 | ] |
---|
961 | lapply (IHl (λx.pre (p0 x))) |
---|
962 | lapply (pm_find_aux_pre A l (λx.pre (p0 x)) p) |
---|
963 | cases (pm_find_aux ?? l ?) |
---|
964 | [ 1,3: #_ #_ normalize |
---|
965 | lapply (IHr (λx.pre (p1 x))) |
---|
966 | lapply (pm_find_aux_pre A r (λx.pre (p1 x)) p) |
---|
967 | cases (pm_find_aux ?? r ?) |
---|
968 | [ 1,3: #_ #_ #E destruct |
---|
969 | | *: * #q' #a' #H1 #H2 #E destruct cases (H1 … (refl ??)) #q'' #E >E @H2 >E % |
---|
970 | ] |
---|
971 | | *: * #q' #a' #H1 #H2 normalize #E destruct cases (H1 … (refl ??)) #q'' #E >E @H2 >E % |
---|
972 | ] |
---|
973 | ] qed. |
---|