1 | include "basics/types.ma". |
---|
2 | include "ASM/String.ma". |
---|
3 | include "utilities/binary/positive.ma". |
---|
4 | include "utilities/lists.ma". |
---|
5 | include "utilities/extralib.ma". |
---|
6 | include "common/Errors.ma". |
---|
7 | |
---|
8 | (* identifiers and their generators are tagged to differentiate them, and to |
---|
9 | provide extra type checking. *) |
---|
10 | |
---|
11 | (* in common/PreIdentifiers.ma, via Errors.ma. |
---|
12 | inductive identifier (tag:String) : Type[0] ≝ |
---|
13 | an_identifier : Pos → identifier tag. |
---|
14 | *) |
---|
15 | |
---|
16 | record universe (tag:String) : Type[0] ≝ |
---|
17 | { |
---|
18 | next_identifier : Pos |
---|
19 | }. |
---|
20 | |
---|
21 | definition new_universe : ∀tag:String. universe tag ≝ |
---|
22 | λtag. mk_universe tag one. |
---|
23 | |
---|
24 | let rec fresh (tag:String) (u:universe tag) on u : identifier tag × (universe tag) ≝ |
---|
25 | let id ≝ next_identifier ? u in |
---|
26 | 〈an_identifier tag id, mk_universe tag (succ id)〉. |
---|
27 | |
---|
28 | |
---|
29 | let rec fresh_for_univ tag (id:identifier tag) (u:universe tag) on id : Prop ≝ |
---|
30 | match id with [ an_identifier p ⇒ p < next_identifier … u ]. |
---|
31 | |
---|
32 | |
---|
33 | lemma fresh_is_fresh : ∀tag,id,u,u'. |
---|
34 | 〈id,u〉 = fresh tag u' → |
---|
35 | fresh_for_univ tag id u. |
---|
36 | #tag * #id * #u * #u' #E whd in E:(???%); destruct // |
---|
37 | qed. |
---|
38 | |
---|
39 | lemma fresh_remains_fresh : ∀tag,id,id',u,u'. |
---|
40 | fresh_for_univ tag id u → |
---|
41 | 〈id',u'〉 = fresh tag u → |
---|
42 | fresh_for_univ tag id u'. |
---|
43 | #tag * #id * #id' * #u * #u' normalize #H #E destruct /2 by le_S/ |
---|
44 | qed. |
---|
45 | |
---|
46 | lemma fresh_distinct : ∀tag,id,id',u,u'. |
---|
47 | fresh_for_univ tag id u → |
---|
48 | 〈id',u'〉 = fresh tag u → |
---|
49 | id ≠ id'. |
---|
50 | #tag * #id * #id' * #u * #u' normalize #H #E destruct % #E' destruct /2 by absurd/ |
---|
51 | qed. |
---|
52 | |
---|
53 | |
---|
54 | let rec env_fresh_for_univ tag A (env:list (identifier tag × A)) (u:universe tag) on u : Prop ≝ |
---|
55 | All ? (λida. fresh_for_univ tag (\fst ida) u) env. |
---|
56 | |
---|
57 | lemma fresh_env_extend : ∀tag,A,env,u,u',id,a. |
---|
58 | env_fresh_for_univ tag A env u → |
---|
59 | 〈id,u'〉 = fresh tag u → |
---|
60 | env_fresh_for_univ tag A (〈id,a〉::env) u'. |
---|
61 | #tag #A #env * #u * #u' #id #a |
---|
62 | #H #E whd % [ @(fresh_is_fresh … E) | @(All_mp … H) * #id #a #H' /2 by fresh_remains_fresh/ ] |
---|
63 | qed. |
---|
64 | |
---|
65 | definition eq_identifier : ∀t. identifier t → identifier t → bool ≝ |
---|
66 | λt,l,r. |
---|
67 | match l with |
---|
68 | [ an_identifier l' ⇒ |
---|
69 | match r with |
---|
70 | [ an_identifier r' ⇒ |
---|
71 | eqb l' r' |
---|
72 | ] |
---|
73 | ]. |
---|
74 | |
---|
75 | lemma eq_identifier_elim : ∀P:bool → Type[0]. ∀t,x,y. |
---|
76 | (x = y → P true) → (x ≠ y → P false) → |
---|
77 | P (eq_identifier t x y). |
---|
78 | #P #t * #x * #y #T #F |
---|
79 | change with (P (eqb ??)) |
---|
80 | @(eqb_elim x y P) [ /2 by / | * #H @F % #E destruct /2 by / ] |
---|
81 | qed. |
---|
82 | |
---|
83 | lemma eq_identifier_eq: |
---|
84 | ∀tag: String. |
---|
85 | ∀l. |
---|
86 | ∀r. |
---|
87 | eq_identifier tag l r = true → l = r. |
---|
88 | #tag #l #r cases l cases r |
---|
89 | #pos_l #pos_r |
---|
90 | cases pos_l cases pos_r |
---|
91 | [1: |
---|
92 | #_ % |
---|
93 | |2,3,4,7: |
---|
94 | #p1_l normalize in ⊢ (% → ?); |
---|
95 | #absurd destruct(absurd) |
---|
96 | |5,9: |
---|
97 | #p1_l #p1_r normalize in ⊢ (% → ?); |
---|
98 | #relevant lapply (eqb_true_to_eq … relevant) #EQ >EQ % |
---|
99 | |*: |
---|
100 | #p_l #p_r normalize in ⊢ (% → ?); |
---|
101 | #absurd destruct(absurd) |
---|
102 | ] |
---|
103 | qed. |
---|
104 | |
---|
105 | axiom neq_identifier_neq: |
---|
106 | ∀tag: String. |
---|
107 | ∀l, r: identifier tag. |
---|
108 | eq_identifier tag l r = false → (l = r → False). |
---|
109 | |
---|
110 | include "basics/deqsets.ma". |
---|
111 | definition Deq_identifier : String → DeqSet ≝ λtag. |
---|
112 | mk_DeqSet (identifier tag) (eq_identifier tag) ?. |
---|
113 | #x#y @eq_identifier_elim /2 by conj/ * #H % [#ABS destruct(ABS) | #G elim (H G)] |
---|
114 | qed. |
---|
115 | |
---|
116 | unification hint 0 ≔ tag; D ≟ Deq_identifier tag |
---|
117 | (*-----------------------------------------------------*)⊢ |
---|
118 | identifier tag ≡ carr D. |
---|
119 | |
---|
120 | definition word_of_identifier ≝ |
---|
121 | λt. |
---|
122 | λl: identifier t. |
---|
123 | match l with |
---|
124 | [ an_identifier l' ⇒ l' |
---|
125 | ]. |
---|
126 | |
---|
127 | lemma eq_identifier_refl : ∀tag,id. eq_identifier tag id id = true. |
---|
128 | #tag * #id whd in ⊢ (??%?); >eqb_n_n @refl |
---|
129 | qed. |
---|
130 | |
---|
131 | axiom eq_identifier_sym: |
---|
132 | ∀tag: String. |
---|
133 | ∀l : identifier tag. |
---|
134 | ∀r : identifier tag. |
---|
135 | eq_identifier tag l r = eq_identifier tag r l. |
---|
136 | |
---|
137 | lemma eq_identifier_false : ∀tag,x,y. x≠y → eq_identifier tag x y = false. |
---|
138 | #tag * #x * #y #NE normalize @not_eq_to_eqb_false /2 by not_to_not/ |
---|
139 | qed. |
---|
140 | |
---|
141 | definition identifier_eq : ∀tag:String. ∀x,y:identifier tag. (x=y) + (x≠y). |
---|
142 | #tag * #x * #y lapply (refl ? (eqb x y)) cases (eqb x y) in ⊢ (???% → %); |
---|
143 | #E [ % | %2 ] |
---|
144 | lapply E @eqb_elim |
---|
145 | [ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2 by absurd/ ] |
---|
146 | qed. |
---|
147 | |
---|
148 | definition identifier_of_nat : ∀tag:String. nat → identifier tag ≝ |
---|
149 | λtag,n. an_identifier tag (succ_pos_of_nat n). |
---|
150 | |
---|
151 | |
---|
152 | (* States that all identifiers in an environment are distinct from one another. *) |
---|
153 | let rec distinct_env tag (A:Type[0]) (l:list (identifier tag × A)) on l : Prop ≝ |
---|
154 | match l with |
---|
155 | [ nil ⇒ True |
---|
156 | | cons hd tl ⇒ All ? (λia. \fst hd ≠ \fst ia) tl ∧ |
---|
157 | distinct_env tag A tl |
---|
158 | ]. |
---|
159 | |
---|
160 | lemma distinct_env_append_l : ∀tag,A,l,r. distinct_env tag A (l@r) → distinct_env tag A l. |
---|
161 | #tag #A #l elim l |
---|
162 | [ // |
---|
163 | | * #id #a #tl #IH #r * #H1 #H2 % /2 by All_append_l/ |
---|
164 | ] qed. |
---|
165 | |
---|
166 | lemma distinct_env_append_r : ∀tag,A,l,r. distinct_env tag A (l@r) → distinct_env tag A r. |
---|
167 | #tag #A #l elim l |
---|
168 | [ // |
---|
169 | | * #id #a #tl #IH #r * #H1 #H2 /2 by / |
---|
170 | ] qed. |
---|
171 | |
---|
172 | (* check_distinct_env is quadratic - we could pay more attention when building maps to make sure that |
---|
173 | the original environment was distinct. *) |
---|
174 | |
---|
175 | axiom DuplicateVariable : String. |
---|
176 | |
---|
177 | let rec check_member_env tag (A:Type[0]) (id:identifier tag) (l:list (identifier tag × A)) on l : res (All ? (λia. id ≠ \fst ia) l) ≝ |
---|
178 | match l return λl.res (All ?? l) with |
---|
179 | [ nil ⇒ OK ? I |
---|
180 | | cons hd tl ⇒ |
---|
181 | match identifier_eq tag id (\fst hd) with |
---|
182 | [ inl _ ⇒ Error ? [MSG DuplicateVariable; CTX ? id] |
---|
183 | | inr NE ⇒ |
---|
184 | do Htl ← check_member_env tag A id tl; |
---|
185 | OK ? (conj ?? NE Htl) |
---|
186 | ] |
---|
187 | ]. |
---|
188 | |
---|
189 | let rec check_distinct_env tag (A:Type[0]) (l:list (identifier tag × A)) on l : res (distinct_env tag A l) ≝ |
---|
190 | match l return λl.res (distinct_env tag A l) with |
---|
191 | [ nil ⇒ OK ? I |
---|
192 | | cons hd tl ⇒ |
---|
193 | do Hhd ← check_member_env tag A (\fst hd) tl; |
---|
194 | do Htl ← check_distinct_env tag A tl; |
---|
195 | OK ? (conj ?? Hhd Htl) |
---|
196 | ]. |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | (* Maps from identifiers to arbitrary types. *) |
---|
202 | |
---|
203 | include "common/PositiveMap.ma". |
---|
204 | |
---|
205 | inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝ |
---|
206 | an_id_map : positive_map A → identifier_map tag A. |
---|
207 | |
---|
208 | definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝ |
---|
209 | λtag,A. an_id_map tag A (pm_leaf A). |
---|
210 | |
---|
211 | let rec lookup tag A (m:identifier_map tag A) (l:identifier tag) on m : option A ≝ |
---|
212 | lookup_opt A (match l with [ an_identifier l' ⇒ l' ]) |
---|
213 | (match m with [ an_id_map m' ⇒ m' ]). |
---|
214 | |
---|
215 | definition lookup_def ≝ |
---|
216 | λtag,A,m,l,d. match lookup tag A m l with [ None ⇒ d | Some x ⇒ x]. |
---|
217 | |
---|
218 | definition member ≝ |
---|
219 | λtag,A.λm:identifier_map tag A.λl:identifier tag. |
---|
220 | match lookup tag A m l with [ None ⇒ false | _ ⇒ true ]. |
---|
221 | |
---|
222 | interpretation "identifier map membership" 'mem a b = (member ?? b a). |
---|
223 | |
---|
224 | definition lookup_safe : ∀tag,A.∀m : identifier_map tag A.∀i.i∈m → A ≝ |
---|
225 | λtag,A,m,i. |
---|
226 | match lookup … m i return λx.match x in option return λ_.bool with [ _ ⇒ ?] → ? with |
---|
227 | [ Some x ⇒ λ_.x |
---|
228 | | None ⇒ λprf.⊥ |
---|
229 | ]. @prf qed. |
---|
230 | |
---|
231 | lemma lookup_eq_safe : ∀tag,A,m,i,prf.lookup tag A m i = Some ? (lookup_safe tag A m i prf). |
---|
232 | #tag #A #m #i whd in match (i∈m); |
---|
233 | whd in match lookup_safe; normalize nodelta |
---|
234 | cases (lookup ????) normalize nodelta [*] // qed. |
---|
235 | |
---|
236 | (* Always adds the identifier to the map. *) |
---|
237 | let rec add tag A (m:identifier_map tag A) (l:identifier tag) (a:A) on m : identifier_map tag A ≝ |
---|
238 | an_id_map tag A (insert A (match l with [ an_identifier l' ⇒ l' ]) a |
---|
239 | (match m with [ an_id_map m' ⇒ m' ])). |
---|
240 | |
---|
241 | lemma lookup_add_hit : ∀tag,A,m,i,a. |
---|
242 | lookup tag A (add tag A m i a) i = Some ? a. |
---|
243 | #tag #A * #m * #i #a |
---|
244 | @lookup_opt_insert_hit |
---|
245 | qed. |
---|
246 | |
---|
247 | lemma lookup_def_add_hit : ∀tag,A,m,i,a,d. |
---|
248 | lookup_def tag A (add tag A m i a) i d = a. |
---|
249 | #tag #A * #m * #i #a #d |
---|
250 | @lookup_insert_hit |
---|
251 | qed. |
---|
252 | |
---|
253 | lemma lookup_add_miss : ∀tag,A,m,i,j,a. |
---|
254 | i ≠ j → |
---|
255 | lookup tag A (add tag A m j a) i = lookup tag A m i. |
---|
256 | #tag #A * #m * #i * #j #a #H |
---|
257 | @lookup_opt_insert_miss /2 by not_to_not/ |
---|
258 | qed. |
---|
259 | |
---|
260 | axiom lookup_def_add_miss : ∀tag,A,m,i,j,a,d. |
---|
261 | i ≠ j → |
---|
262 | lookup_def tag A (add tag A m j a) i d = lookup_def tag A m i d. |
---|
263 | |
---|
264 | lemma lookup_add_oblivious : ∀tag,A,m,i,j,a. |
---|
265 | (lookup tag A m i ≠ None ?) → |
---|
266 | lookup tag A (add tag A m j a) i ≠ None ?. |
---|
267 | #tag #A #m #i #j #a #H |
---|
268 | cases (identifier_eq ? i j) |
---|
269 | [ #E >E >lookup_add_hit % #N destruct |
---|
270 | | #NE >lookup_add_miss // |
---|
271 | ] qed. |
---|
272 | |
---|
273 | lemma lookup_add_cases : ∀tag,A,m,i,j,a,v. |
---|
274 | lookup tag A (add tag A m i a) j = Some ? v → |
---|
275 | (i=j ∧ v = a) ∨ lookup tag A m j = Some ? v. |
---|
276 | #tag #A #m #i #j #a #v |
---|
277 | cases (identifier_eq ? i j) |
---|
278 | [ #E >E >lookup_add_hit #H %1 destruct % // |
---|
279 | | #NE >lookup_add_miss /2 by or_intror, sym_not_eq/ |
---|
280 | ] qed. |
---|
281 | |
---|
282 | (* Extract every identifier, value pair from the map. *) |
---|
283 | definition elements : ∀tag,A. identifier_map tag A → list (identifier tag × A) ≝ |
---|
284 | λtag,A,m. |
---|
285 | fold ?? (λl,a,el. 〈an_identifier tag l, a〉::el) |
---|
286 | (match m with [ an_id_map m' ⇒ m' ]) [ ]. |
---|
287 | |
---|
288 | (* Test a predicate on all of the entries in a map. The predicate is given a |
---|
289 | proof that the entry appears in the map. *) |
---|
290 | |
---|
291 | definition idmap_all : ∀tag,A. ∀m:identifier_map tag A. (∀id,a. lookup tag A m id = Some A a → bool) → bool ≝ |
---|
292 | λtag,A,m,f. pm_all A (match m with [ an_id_map m' ⇒ m' ]) |
---|
293 | (λp,a,H. f (an_identifier tag p) a ?). |
---|
294 | cases m in H ⊢ %; #m' normalize // |
---|
295 | qed. |
---|
296 | |
---|
297 | inductive idmap_pred_graph : ∀tag,A,m,id,a,L. ∀f:(∀id,a. lookup tag A m id = Some A a → bool). bool → Prop ≝ |
---|
298 | | idmappg : ∀tag,A,m,id,a,L,f. idmap_pred_graph tag A m id a L f (f id a L). |
---|
299 | |
---|
300 | lemma idmap_pred_irr : ∀tag,A,m,id,a,L,L'. ∀f:(∀id,a. lookup tag A m id = Some A a → bool). |
---|
301 | f id a L = f id a L'. |
---|
302 | #tag #A #m #id #a #L #L' #f |
---|
303 | cut (idmap_pred_graph tag A m id a L f (f id a L)) [ % ] |
---|
304 | cases (f id a L) #H |
---|
305 | cut (idmap_pred_graph tag A m id a L' f ?) [ 2,5: @H | 1,4: skip ] * // |
---|
306 | qed. |
---|
307 | |
---|
308 | lemma idmap_all_ok : ∀tag,A,m,f. |
---|
309 | bool_to_Prop (idmap_all tag A m f) ↔ (∀id,a,H. f id a H). |
---|
310 | #tag #A * #m #f |
---|
311 | whd in match (idmap_all ????); @(iff_trans … (pm_all_ok …)) % |
---|
312 | [ #H * #id #a #PR lapply (H id a PR) #X @eq_true_to_b <X @idmap_pred_irr |
---|
313 | | #H #p #a #PR @H |
---|
314 | ] qed. |
---|
315 | |
---|
316 | |
---|
317 | axiom MissingId : String. |
---|
318 | |
---|
319 | (* Only updates an existing entry; fails with an error otherwise. *) |
---|
320 | definition update : ∀tag,A. identifier_map tag A → identifier tag → A → res (identifier_map tag A) ≝ |
---|
321 | λtag,A,m,l,a. |
---|
322 | match update A (match l with [ an_identifier l' ⇒ l' ]) a |
---|
323 | (match m with [ an_id_map m' ⇒ m' ]) with |
---|
324 | [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *) |
---|
325 | | Some m' ⇒ OK ? (an_id_map tag A m') |
---|
326 | ]. |
---|
327 | |
---|
328 | (* Remove an entry from a map (and leave it equivalent, otherwise) *) |
---|
329 | definition remove : ∀tag,A. identifier_map tag A → identifier tag → identifier_map tag A ≝ |
---|
330 | λtag,A,m,id. an_id_map tag A (pm_set A (match id with [ an_identifier p ⇒ p ]) (None ?) |
---|
331 | (match m with [ an_id_map m ⇒ m ])). |
---|
332 | |
---|
333 | lemma lookup_remove_hit : ∀tag,A,m,id. |
---|
334 | lookup tag A (remove tag A m id) id = None ?. |
---|
335 | #tag #A * #m * #id @lookup_opt_pm_set_hit |
---|
336 | qed. |
---|
337 | |
---|
338 | lemma lookup_remove_miss : ∀tag,A,m,id,id'. |
---|
339 | id ≠ id' → |
---|
340 | lookup tag A (remove tag A m id') id = lookup tag A m id. |
---|
341 | #tag #A * #m * #id * #id' #NE |
---|
342 | @lookup_opt_pm_set_miss |
---|
343 | /2/ |
---|
344 | qed. |
---|
345 | |
---|
346 | (* Fold over the entries in a map. There are some lemmas to help reason about |
---|
347 | this near the bottom of the file (they require sets). *) |
---|
348 | |
---|
349 | definition foldi: |
---|
350 | ∀A, B: Type[0]. |
---|
351 | ∀tag: String. |
---|
352 | (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝ |
---|
353 | λA,B,tag,f,m,b. |
---|
354 | match m with |
---|
355 | [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ]. |
---|
356 | |
---|
357 | (* An informative, dependently-typed, fold. *) |
---|
358 | |
---|
359 | definition fold_inf: |
---|
360 | ∀A, B: Type[0]. |
---|
361 | ∀tag: String. |
---|
362 | ∀m:identifier_map tag A. |
---|
363 | (∀id:identifier tag. ∀a:A. lookup … m id = Some A a → B → B) → B → B ≝ |
---|
364 | λA,B,tag,m. |
---|
365 | match m return λm. (∀id:identifier tag. ∀a:A. lookup … m id = Some A a → B → B) → B → B with |
---|
366 | [ an_id_map m' ⇒ λf,b. pm_fold_inf A B m' (λbv,a,H. f (an_identifier ? bv) a H) b ]. |
---|
367 | |
---|
368 | (* Find one element of a map that satisfies a predicate *) |
---|
369 | definition find : ∀tag,A. identifier_map tag A → (identifier tag → A → bool) → |
---|
370 | option (identifier tag × A) ≝ |
---|
371 | λtag,A,m,p. |
---|
372 | match m with [ an_id_map m' ⇒ |
---|
373 | option_map … (λx. 〈an_identifier tag (\fst x), \snd x〉) |
---|
374 | (pm_find … m' (λid. p (an_identifier tag id))) ]. |
---|
375 | |
---|
376 | lemma find_lookup : ∀tag,A,m,p,id,a. |
---|
377 | find tag A m p = Some ? 〈id,a〉 → |
---|
378 | lookup … m id = Some ? a. |
---|
379 | #tag #A * #m #p * #id #a #FIND |
---|
380 | @(pm_find_lookup A (λid. p (an_identifier tag id)) id a m) |
---|
381 | whd in FIND:(??%?); cases (pm_find ???) in FIND ⊢ %; |
---|
382 | [ normalize #E destruct |
---|
383 | | * #id' #a' normalize #E destruct % |
---|
384 | ] qed. |
---|
385 | |
---|
386 | lemma find_none : ∀tag,A,m,p,id,a. |
---|
387 | find tag A m p = None ? → |
---|
388 | lookup … m id = Some ? a → |
---|
389 | ¬ p id a. |
---|
390 | #tag #A * #m #p * #id #a #FIND |
---|
391 | @(pm_find_none A m (λid. p (an_identifier tag id))) |
---|
392 | whd in FIND:(??%?); cases (pm_find ???) in FIND ⊢ %; |
---|
393 | [ normalize #E destruct % |
---|
394 | | * #id' #a' normalize #E destruct |
---|
395 | ] qed. |
---|
396 | |
---|
397 | |
---|
398 | lemma find_predicate : ∀tag,A,m,p,id,a. |
---|
399 | find tag A m p = Some ? 〈id,a〉 → |
---|
400 | p id a. |
---|
401 | #tag #A * #m #p * #id #a #FIND whd in FIND:(??%?); |
---|
402 | @(pm_find_predicate A m (λid. p (an_identifier tag id)) id a) |
---|
403 | cases (pm_find ???) in FIND ⊢ %; |
---|
404 | [ normalize #E destruct |
---|
405 | | * #id' #a' normalize #E destruct % |
---|
406 | ] qed. |
---|
407 | |
---|
408 | (* A predicate that an identifier is in a map, and a failure-avoiding lookup |
---|
409 | and update using it. *) |
---|
410 | |
---|
411 | definition present : ∀tag,A. identifier_map tag A → identifier tag → Prop ≝ |
---|
412 | λtag,A,m,i. lookup … m i ≠ None ?. |
---|
413 | |
---|
414 | lemma member_present : ∀tag,A,m,id. |
---|
415 | member tag A m id = true → present tag A m id. |
---|
416 | #tag #A * #m #id normalize cases (lookup_opt A ??) normalize |
---|
417 | [ #E destruct |
---|
418 | | #x #E % #E' destruct |
---|
419 | ] qed. |
---|
420 | |
---|
421 | lemma present_member : ∀tag,A,m,id. |
---|
422 | present tag A m id → member tag A m id. |
---|
423 | #tag #A #m #id whd in ⊢ (% → ?%); cases (lookup ????) // * #H cases (H (refl ??)) |
---|
424 | qed. |
---|
425 | |
---|
426 | definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝ |
---|
427 | λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ]. |
---|
428 | cases H #H' cases (H' (refl ??)) qed. |
---|
429 | |
---|
430 | lemma lookup_lookup_present : ∀tag,A,m,id,p. |
---|
431 | lookup tag A m id = Some ? (lookup_present tag A m id p). |
---|
432 | #tag #A #m #id #p |
---|
433 | whd in p ⊢ (???(??%)); |
---|
434 | cases (lookup tag A m id) in p ⊢ %; |
---|
435 | [ * #H @⊥ @H @refl |
---|
436 | | #a #H @refl |
---|
437 | ] qed. |
---|
438 | |
---|
439 | lemma lookup_is_present : ∀tag,T,m,id,t. |
---|
440 | lookup tag T m id = Some T t → |
---|
441 | present ?? m id. |
---|
442 | #tag #T #m #id #t #L normalize >L % #E destruct |
---|
443 | qed. |
---|
444 | |
---|
445 | lemma lookup_present_eq : ∀tag,T,m,id,t. |
---|
446 | lookup tag T m id = Some T t → |
---|
447 | ∀H. lookup_present tag T m id H = t. |
---|
448 | #tag #T #m #id #t #L #H |
---|
449 | lapply (lookup_lookup_present … H) >L #E destruct % |
---|
450 | qed. |
---|
451 | |
---|
452 | |
---|
453 | definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝ |
---|
454 | λtag,A,m,l,p,a. |
---|
455 | let l' ≝ match l with [ an_identifier l' ⇒ l' ] in |
---|
456 | let m' ≝ match m with [ an_id_map m' ⇒ m' ] in |
---|
457 | let u' ≝ update A l' a m' in |
---|
458 | match u' return λx. update ???? = x → ? with |
---|
459 | [ None ⇒ λE.⊥ |
---|
460 | | Some m' ⇒ λ_. an_id_map tag A m' |
---|
461 | ] (refl ? u'). |
---|
462 | cases l in p E; cases m; -l' -m' #m' #l' |
---|
463 | whd in ⊢ (% → ?); |
---|
464 | whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?); |
---|
465 | #NL #U cases NL #H @H @(update_fail … U) |
---|
466 | qed. |
---|
467 | |
---|
468 | lemma update_still_present : ∀tag,A,m,id,a,id'. |
---|
469 | ∀H:present tag A m id. |
---|
470 | ∀H':present tag A m id'. |
---|
471 | present tag A (update_present tag A m id' H' a) id. |
---|
472 | #tag #A * #m * #id #a * #id' #H #H' |
---|
473 | whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta |
---|
474 | cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id')) |
---|
475 | [ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U) |
---|
476 | % #E' destruct |
---|
477 | | #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?)); |
---|
478 | <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ] |
---|
479 | ] qed. |
---|
480 | |
---|
481 | lemma lookup_present_add_hit: |
---|
482 | ∀tag, A, map, k, v, k_pres. |
---|
483 | lookup_present tag A (add … map k v) k k_pres = v. |
---|
484 | #tag #a #map #k #v #k_pres |
---|
485 | lapply (lookup_lookup_present … (add … map k v) … k_pres) |
---|
486 | >lookup_add_hit #Some_assm destruct(Some_assm) |
---|
487 | <e0 % |
---|
488 | qed. |
---|
489 | |
---|
490 | lemma lookup_present_add_miss: |
---|
491 | ∀tag, A, map, k, k', v, k_pres', k_pres''. |
---|
492 | k' ≠ k → |
---|
493 | lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''. |
---|
494 | #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm |
---|
495 | lapply (lookup_lookup_present … (add … map k v) ? k_pres') |
---|
496 | >lookup_add_miss try assumption |
---|
497 | #Some_assm |
---|
498 | lapply (lookup_lookup_present … map k') >Some_assm #Some_assm' |
---|
499 | lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption |
---|
500 | qed. |
---|
501 | |
---|
502 | lemma present_add_present: |
---|
503 | ∀tag, a, map, k, k', v. |
---|
504 | k' ≠ k → |
---|
505 | present tag a (add tag a map k v) k' → |
---|
506 | present tag a map k'. |
---|
507 | #tag #a #map #k #k' #v #neq_hyp #present_hyp |
---|
508 | whd in match present; normalize nodelta |
---|
509 | whd in match present in present_hyp; normalize nodelta in present_hyp; |
---|
510 | cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp |
---|
511 | lapply (lookup_add_cases tag ?????? Some_eq_hyp) * |
---|
512 | [1: |
---|
513 | * #k_eq_hyp @⊥ /2/ |
---|
514 | |2: |
---|
515 | #Some_eq_hyp' /2/ |
---|
516 | ] |
---|
517 | qed. |
---|
518 | |
---|
519 | lemma present_add_hit: |
---|
520 | ∀tag, a, map, k, v. |
---|
521 | present tag a (add tag a map k v) k. |
---|
522 | #tag #a #map #k #v |
---|
523 | whd >lookup_add_hit |
---|
524 | % #absurd destruct |
---|
525 | qed. |
---|
526 | |
---|
527 | lemma present_add_miss: |
---|
528 | ∀tag, a, map, k, k', v. |
---|
529 | present tag a map k' → present tag a (add tag a map k v) k'. |
---|
530 | #tag #a #map #k #k' #v #present_assm |
---|
531 | whd @lookup_add_oblivious assumption |
---|
532 | qed. |
---|
533 | |
---|
534 | lemma present_add_cases: ∀tag,A,map,k,v,k'. |
---|
535 | present tag A (add tag A map k v) k' → |
---|
536 | k = k' ∨ (k ≠ k' ∧ present tag A map k'). |
---|
537 | #tag #A #map #k #v #k' normalize |
---|
538 | cases (identifier_eq ? k k') |
---|
539 | [ #E /2/ |
---|
540 | | #NE >lookup_add_miss /3/ |
---|
541 | ] qed. |
---|
542 | |
---|
543 | |
---|
544 | let rec fresh_for_map tag A (id:identifier tag) (m:identifier_map tag A) on id : Prop ≝ |
---|
545 | lookup … m id = None A. |
---|
546 | |
---|
547 | lemma fresh_for_empty_map : ∀tag,A,id. |
---|
548 | fresh_for_map tag A id (empty_map tag A). |
---|
549 | #tag #A * #id // |
---|
550 | qed. |
---|
551 | |
---|
552 | definition fresh_map_for_univ ≝ |
---|
553 | λtag,A. λm:identifier_map tag A. λu:universe tag. |
---|
554 | ∀id. present tag A m id → fresh_for_univ tag id u. |
---|
555 | |
---|
556 | lemma fresh_fresh_for_map : ∀tag,A,m,id,u,u'. |
---|
557 | fresh_map_for_univ tag A m u → |
---|
558 | 〈id,u'〉 = fresh tag u → |
---|
559 | fresh_for_map tag A id m. |
---|
560 | #tag #A * #m * #id * #u * #u' whd in ⊢ (% → ???% → %); |
---|
561 | #FMU #E destruct lapply (FMU (an_identifier tag u)) whd in ⊢ ((% → %) → ?); |
---|
562 | generalize in ⊢ ((?(??%?) → ?) → ??%?); * |
---|
563 | [ // | #a #H @False_ind lapply (H ?) /2 by absurd/ % #E destruct |
---|
564 | qed. |
---|
565 | |
---|
566 | lemma fresh_map_preserved : ∀tag,A,m,u,u',id. |
---|
567 | fresh_map_for_univ tag A m u → |
---|
568 | 〈id,u'〉 = fresh tag u → |
---|
569 | fresh_map_for_univ tag A m u'. |
---|
570 | #tag #A #m #u * #u' #id whd in ⊢ (% → ? → %); #H #E |
---|
571 | #id' #PR @(fresh_remains_fresh … E) @H // |
---|
572 | qed. |
---|
573 | |
---|
574 | lemma fresh_map_add : ∀tag,A,m,u,id,a. |
---|
575 | fresh_map_for_univ tag A m u → |
---|
576 | fresh_for_univ tag id u → |
---|
577 | fresh_map_for_univ tag A (add tag A m id a) u. |
---|
578 | #tag #A * #m #u #id #a #Hm #Hi |
---|
579 | #id' #PR cases (identifier_eq tag id' id) |
---|
580 | [ #E >E @Hi |
---|
581 | | #NE @Hm whd in PR; |
---|
582 | change with (add tag A (an_id_map tag A m) id a) in PR:(?(??(???%?)?)); |
---|
583 | >lookup_add_miss in PR; // |
---|
584 | ] qed. |
---|
585 | |
---|
586 | lemma present_not_fresh : ∀tag,A,m,id,id'. |
---|
587 | present tag A m id → |
---|
588 | fresh_for_map tag A id' m → |
---|
589 | id ≠ id'. |
---|
590 | #tag #A #m #id * #id' whd in ⊢ (% → % → ?); |
---|
591 | * #NE #E % #E' destruct @(NE E) |
---|
592 | qed. |
---|
593 | |
---|
594 | lemma fresh_for_map_add : ∀tag,A,id,m,id',a. |
---|
595 | id ≠ id' → |
---|
596 | fresh_for_map tag A id m → |
---|
597 | fresh_for_map tag A id (add tag A m id' a). |
---|
598 | #tag #A * #id #m #id' #a #NE #F |
---|
599 | whd >lookup_add_miss // |
---|
600 | qed. |
---|
601 | |
---|
602 | (* Extending the domain of a map (without necessarily preserving contents). *) |
---|
603 | |
---|
604 | definition extends_domain : ∀tag,A. identifier_map tag A → identifier_map tag A → Prop ≝ |
---|
605 | λtag,A,m1,m2. ∀l. present ?? m1 l → present ?? m2 l. |
---|
606 | |
---|
607 | lemma extends_dom_trans : ∀tag,A,m1,m2,m3. |
---|
608 | extends_domain tag A m1 m2 → extends_domain tag A m2 m3 → extends_domain tag A m1 m3. |
---|
609 | #tag #A #m1 #m2 #m3 #H1 #H2 #l #P1 @H2 @H1 @P1 qed. |
---|
610 | |
---|
611 | |
---|
612 | (* Sets *) |
---|
613 | |
---|
614 | definition identifier_set ≝ λtag.identifier_map tag unit. |
---|
615 | |
---|
616 | definition empty_set : ∀tag.identifier_set tag ≝ λtag.empty_map …. |
---|
617 | |
---|
618 | |
---|
619 | definition add_set : ∀tag.identifier_set tag → identifier tag → identifier_set tag ≝ |
---|
620 | λtag,s,i.add … s i it. |
---|
621 | |
---|
622 | definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝ |
---|
623 | λtag,i. add_set tag (empty_set tag) i. |
---|
624 | |
---|
625 | let rec union_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_set tag ≝ |
---|
626 | an_id_map tag unit (merge … (λo,o'.match o with [Some _ ⇒ Some ? it | None ⇒ !_ o'; return it]) |
---|
627 | (match s with [ an_id_map s0 ⇒ s0 ]) |
---|
628 | (match s' with [ an_id_map s1 ⇒ s1 ])). |
---|
629 | |
---|
630 | |
---|
631 | (* set minus is generalised to maps *) |
---|
632 | let rec minus_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_map tag A ≝ |
---|
633 | an_id_map tag A (merge A B A (λo,o'.match o' with [None ⇒ o | Some _ ⇒ None ?]) |
---|
634 | (match s with [ an_id_map s0 ⇒ s0 ]) |
---|
635 | (match s' with [ an_id_map s1 ⇒ s1 ])). |
---|
636 | |
---|
637 | notation "a ∖ b" left associative with precedence 55 for @{'setminus $a $b}. |
---|
638 | |
---|
639 | interpretation "identifier set union" 'union a b = (union_set ??? a b). |
---|
640 | notation "∅" non associative with precedence 90 for @{ 'empty }. |
---|
641 | interpretation "empty identifier set" 'empty = (empty_set ?). |
---|
642 | interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a). |
---|
643 | interpretation "identifier map difference" 'setminus a b = (minus_set ??? a b). |
---|
644 | |
---|
645 | definition IdentifierSet : String → Setoid ≝ λtag. |
---|
646 | mk_Setoid (identifier_set tag) (λs,s'.∀i.i ∈ s = (i ∈ s')) ???. |
---|
647 | // qed. |
---|
648 | |
---|
649 | unification hint 0 ≔ tag; |
---|
650 | S ≟ IdentifierSet tag |
---|
651 | (*-----------------------------*)⊢ |
---|
652 | identifier_set tag ≡ std_supp S. |
---|
653 | unification hint 0 ≔ tag; |
---|
654 | S ≟ IdentifierSet tag |
---|
655 | (*-----------------------------*)⊢ |
---|
656 | identifier_map tag unit ≡ std_supp S. |
---|
657 | |
---|
658 | lemma mem_set_add : ∀tag,A.∀i,j : identifier tag.∀s,x. |
---|
659 | i ∈ add ? A s j x = (eq_identifier ? i j ∨ i ∈ s). |
---|
660 | #tag #A *#i *#j *#s #x normalize |
---|
661 | @(eqb_elim i j) |
---|
662 | [#EQ destruct |
---|
663 | >(lookup_opt_insert_hit A x j) |
---|
664 | |#NEQ >(lookup_opt_insert_miss … s NEQ) |
---|
665 | ] elim (lookup_opt A j s) normalize // qed. |
---|
666 | |
---|
667 | lemma mem_set_add_id : ∀tag,A,i,s,x.bool_to_Prop (i ∈ add tag A s i x). |
---|
668 | #tag #A #i #s #x >mem_set_add |
---|
669 | @eq_identifier_elim [#_ %| #ABS elim (absurd … (refl ? i) ABS)] qed. |
---|
670 | |
---|
671 | lemma in_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i. |
---|
672 | if i ∈ m then (∃s.lookup … m i = Some ? s) else (lookup … m i = None ?). |
---|
673 | #tag #A * #m * #i normalize |
---|
674 | elim (lookup_opt A i m) normalize |
---|
675 | [ % | #x %{x} % ] |
---|
676 | qed. |
---|
677 | |
---|
678 | lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s. |
---|
679 | #tag * normalize #m >map_opt_id_eq_ext // * % |
---|
680 | qed. |
---|
681 | |
---|
682 | lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s. |
---|
683 | #tag * * [//] *[2: *] #l#r normalize |
---|
684 | >map_opt_id_eq_ext [1,3: >map_opt_id_eq_ext [2,4: *] |*: *] // |
---|
685 | qed. |
---|
686 | |
---|
687 | lemma minus_empty_l : ∀tag,A.∀s:identifier_map tag A. ∅ ∖ s ≅ ∅. |
---|
688 | #tag #A * * [//] *[2:#x]#l#r * * normalize [1,4://] |
---|
689 | #p >lookup_opt_map elim (lookup_opt ???) normalize // |
---|
690 | qed. |
---|
691 | |
---|
692 | lemma minus_empty_r : ∀tag,A.∀s:identifier_map tag A. s ∖ ∅ = s. |
---|
693 | #tag #A * * [//] *[2:#x]#l#r normalize |
---|
694 | >map_opt_id >map_opt_id // |
---|
695 | qed. |
---|
696 | |
---|
697 | lemma mem_set_union : ∀tag.∀i : identifier tag.∀s,s' : identifier_set tag. |
---|
698 | i ∈ (s ∪ s') = (i ∈ s ∨ i ∈ s'). |
---|
699 | #tag * #i * #s * #s' normalize |
---|
700 | >lookup_opt_merge [2: @refl] |
---|
701 | elim (lookup_opt ???) |
---|
702 | elim (lookup_opt ???) |
---|
703 | normalize // qed. |
---|
704 | |
---|
705 | lemma mem_set_minus : ∀tag,A,B.∀i : identifier tag.∀s : identifier_map tag A. |
---|
706 | ∀s' : identifier_map tag B. |
---|
707 | i ∈ (s ∖ s') = (i ∈ s ∧ ¬ i ∈ s'). |
---|
708 | #tag #A #B * #i * #s * #s' normalize |
---|
709 | >lookup_opt_merge [2: @refl] |
---|
710 | elim (lookup_opt ???) |
---|
711 | elim (lookup_opt ???) |
---|
712 | normalize // qed. |
---|
713 | |
---|
714 | lemma set_eq_ext_node : ∀tag.∀o,o',l,l',r,r'. |
---|
715 | an_id_map tag ? (pm_node ? o l r) ≅ an_id_map … (pm_node ? o' l' r') → |
---|
716 | o = o' ∧ an_id_map tag ? l ≅ an_id_map … l' ∧ an_id_map tag ? r ≅ an_id_map … r'. |
---|
717 | #tag#o#o'#l#l'#r#r'#H |
---|
718 | %[ |
---|
719 | %[ lapply (H (an_identifier ? one)) |
---|
720 | elim o [2: *] elim o' [2,4: *] normalize // #EQ destruct |
---|
721 | | *#p lapply (H (an_identifier ? (p0 p))) normalize // |
---|
722 | ]| *#p lapply (H (an_identifier ? (p1 p))) normalize // |
---|
723 | ] |
---|
724 | qed. |
---|
725 | |
---|
726 | lemma set_eq_ext_leaf : ∀tag,A.∀o,l,r. |
---|
727 | (∀i.i∈an_id_map tag A (pm_node ? o l r) = false) → |
---|
728 | o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false). |
---|
729 | #tag#A#o#l#r#H |
---|
730 | %[ |
---|
731 | %[ lapply (H (an_identifier ? one)) |
---|
732 | elim o [2: #a] normalize // #EQ destruct |
---|
733 | | *#p lapply (H (an_identifier ? (p0 p))) normalize // |
---|
734 | ]| *#p lapply (H (an_identifier ? (p1 p))) normalize // |
---|
735 | ] |
---|
736 | qed. |
---|
737 | |
---|
738 | |
---|
739 | definition id_map_size : ∀tag : String.∀A. identifier_map tag A → ℕ ≝ |
---|
740 | λtag,A,s.match s with [an_id_map p ⇒ |p|]. |
---|
741 | |
---|
742 | interpretation "identifier map domain size" 'norm s = (id_map_size ?? s). |
---|
743 | |
---|
744 | lemma set_eq_ext_empty_to_card : ∀tag,A.∀s : identifier_map tag A. (∀i.i∈s = false) → |s| = 0. |
---|
745 | #tag#A * #s elim s [//] |
---|
746 | #o#l#r normalize in ⊢((?→%)→(?→%)→?); #Hil #Hir #H |
---|
747 | elim (set_eq_ext_leaf … H) * #EQ destruct #Hl #Hr normalize |
---|
748 | >(Hil Hl) >(Hir Hr) // qed. |
---|
749 | |
---|
750 | lemma set_eq_ext_to_card : ∀tag.∀s,s' : identifier_set tag. s ≅ s' → |s| = |s'|. |
---|
751 | #tag *#s elim s |
---|
752 | [** [//] #o#l#r #H |
---|
753 | >(set_eq_ext_empty_to_card … (std_symm … H)) // |
---|
754 | | #o#l#r normalize in ⊢((?→?→??%?)→(?→?→??%?)→?); |
---|
755 | #Hil #Hir ** |
---|
756 | [#H @(set_eq_ext_empty_to_card … H)] |
---|
757 | #o'#l'#r' #H elim (set_eq_ext_node … H) * #EQ destruct(EQ) #Hl #Hr |
---|
758 | normalize >(Hil ? Hl) >(Hir ? Hr) // |
---|
759 | ] qed. |
---|
760 | |
---|
761 | lemma add_size: ∀tag,A,s,i,x. |
---|
762 | |add tag A s i x| = (if i ∈ s then 0 else 1) + |s|. |
---|
763 | #tag #A *#s *#i #x |
---|
764 | lapply (insert_size ? i x s) |
---|
765 | lapply (refl ? (lookup_opt ? i s)) |
---|
766 | generalize in ⊢ (???%→?); * [2: #x'] |
---|
767 | normalize #EQ >EQ normalize // |
---|
768 | qed. |
---|
769 | |
---|
770 | lemma mem_set_O_lt_card : ∀tag,A.∀i.∀s : identifier_map tag A. i ∈ s → |s| > 0. |
---|
771 | #tag #A * #i * #s normalize #H |
---|
772 | @(lookup_opt_O_lt_size … i) |
---|
773 | % #EQ >EQ in H; normalize * |
---|
774 | qed. |
---|
775 | |
---|
776 | (* NB: no control on values if applied to maps *) |
---|
777 | definition set_subset ≝ λtag,A,B.λs : identifier_map tag A. |
---|
778 | λs' : identifier_map tag B. ∀i.i ∈ s → (bool_to_Prop (i ∈ s')). |
---|
779 | |
---|
780 | interpretation "identifier set subset" 'subseteq s s' = (set_subset ??? s s'). |
---|
781 | |
---|
782 | lemma add_subset : |
---|
783 | ∀tag,A,B.∀i : identifier tag.∀x.∀s : identifier_map ? A.∀s' : identifier_map ? B. |
---|
784 | i ∈ s' → s ⊆ s' → add … s i x ⊆ s'. |
---|
785 | #tag#A#B#i#x#s#s' #H #G #j |
---|
786 | >mem_set_add |
---|
787 | @eq_identifier_elim #H' [* >H' @H | #js @(G ? js)] |
---|
788 | qed. |
---|
789 | |
---|
790 | definition set_forall : ∀tag,A.(identifier tag → Prop) → |
---|
791 | identifier_map tag A → Prop ≝ λtag,A,P,m.∀i. i ∈ m → P i. |
---|
792 | |
---|
793 | lemma set_forall_add : ∀tag,P,m,i.set_forall tag ? P m → P i → |
---|
794 | set_forall tag ? P (add_set ? m i). |
---|
795 | #tag#P#m#i#Pm#Pi#j |
---|
796 | >mem_set_add |
---|
797 | @eq_identifier_elim |
---|
798 | [#EQ destruct(EQ) #_ @Pi |
---|
799 | |#_ @Pm |
---|
800 | ] |
---|
801 | qed. |
---|
802 | |
---|
803 | include "utilities/proper.ma". |
---|
804 | |
---|
805 | lemma minus_subset : ∀tag,A,B.minus_set tag A B ⊨ set_subset … ++> set_subset … -+> set_subset …. |
---|
806 | #tag#A#B#s#s' #H #s'' #s''' #G #i |
---|
807 | >mem_set_minus >mem_set_minus |
---|
808 | #H' elim (andb_Prop_true … H') -H' #is #nis'' |
---|
809 | >(H … is) |
---|
810 | elim (true_or_false_Prop (i∈s''')) |
---|
811 | [ #is''' >(G … is''') in nis''; * |
---|
812 | | #nis''' >nis''' % |
---|
813 | ] |
---|
814 | qed. |
---|
815 | |
---|
816 | lemma subset_node : ∀tag,A,B.∀o,o',l,l',r,r'. |
---|
817 | an_id_map tag A (pm_node ? o l r) ⊆ an_id_map tag B (pm_node ? o' l' r') → |
---|
818 | opt_All ? (λ_.o' ≠ None ?) o ∧ an_id_map tag ? l ⊆ an_id_map tag ? l' ∧ |
---|
819 | an_id_map tag ? r ⊆ an_id_map tag ? r'. |
---|
820 | #tag#A#B#o#o'#l#l'#r#r'#H |
---|
821 | %[% |
---|
822 | [ lapply (H (an_identifier ? (one))) elim o [2: #a] elim o' [2:#b] |
---|
823 | normalize // [#_ % #ABS destruct(ABS) | #G lapply (G I) *] |
---|
824 | | *#p lapply (H (an_identifier ? (p0 p))) |
---|
825 | ] |
---|
826 | | *#p lapply (H (an_identifier ? (p1 p))) |
---|
827 | ] #H @H |
---|
828 | qed. |
---|
829 | |
---|
830 | lemma subset_leaf : ∀tag,A.∀o,l,r. |
---|
831 | an_id_map tag A (pm_node ? o l r) ⊆ ∅ → |
---|
832 | o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false). |
---|
833 | #tag#A#o#l#r#H |
---|
834 | %[ |
---|
835 | %[ lapply (H (an_identifier ? one)) |
---|
836 | elim o [2: #a] normalize // #EQ lapply(EQ I) * |
---|
837 | | *#p lapply (H (an_identifier ? (p0 p))) |
---|
838 | ] |
---|
839 | | *#p lapply (H (an_identifier ? (p1 p))) |
---|
840 | ] normalize elim (lookup_opt ? p ?) normalize |
---|
841 | // #a #H lapply (H I) * |
---|
842 | qed. |
---|
843 | |
---|
844 | lemma subset_card : ∀tag,A,B.∀s : identifier_map tag A.∀s' : identifier_map tag B. |
---|
845 | s ⊆ s' → |s| ≤ |s'|. |
---|
846 | #tag #A #B *#s elim s |
---|
847 | [ // |
---|
848 | | #o#l#r #Hil #Hir ** |
---|
849 | [ #H elim (subset_leaf … H) * #EQ >EQ #Hl #Hr |
---|
850 | lapply (set_eq_ext_empty_to_card … Hl) |
---|
851 | lapply (set_eq_ext_empty_to_card … Hr) |
---|
852 | normalize // |
---|
853 | | #o' #l' #r' #H elim (subset_node … H) * |
---|
854 | elim o [2: #a] elim o' [2,4: #a'] |
---|
855 | [3: #G normalize in G; elim(absurd ? (refl ??) G) |
---|
856 | |*: #_ #Hl #Hr lapply (Hil ? Hl) lapply (Hir ? Hr) |
---|
857 | normalize #H1 #H2 |
---|
858 | [@le_S_S | @(transitive_le … (|l'|+|r'|)) [2: / by /]] |
---|
859 | @le_plus assumption |
---|
860 | ] |
---|
861 | ] |
---|
862 | ] |
---|
863 | qed. |
---|
864 | |
---|
865 | lemma mem_set_empty : ∀tag,A.∀i: identifier tag. i∈empty_map tag A = false. |
---|
866 | #tag #A * #i normalize % |
---|
867 | qed. |
---|
868 | |
---|
869 | lemma mem_set_singl_to_eq : ∀tag.∀i,j : identifier tag.i∈{(j)} → i = j. |
---|
870 | #tag |
---|
871 | #i #j >mem_set_add >mem_set_empty |
---|
872 | #H elim (orb_true_l … H) -H |
---|
873 | [@eq_identifier_elim [//] #_] #EQ destruct |
---|
874 | qed. |
---|
875 | |
---|
876 | lemma subset_add_set : ∀tag,i,s.s ⊆ add_set tag s i. |
---|
877 | #tag#i#s#j #H >mem_set_add >H |
---|
878 | >commutative_orb % |
---|
879 | qed. |
---|
880 | |
---|
881 | lemma add_set_monotonic : ∀tag,i,s,s'.s ⊆ s' → add_set tag s i ⊆ add_set tag s' i. |
---|
882 | #tag#i#s#s' #H #j >mem_set_add >mem_set_add |
---|
883 | @orb_elim elim (eq_identifier ???) |
---|
884 | whd lapply (H j) /2 by / |
---|
885 | qed. |
---|
886 | |
---|
887 | lemma transitive_subset : ∀tag,A.transitive ? (set_subset tag A A). |
---|
888 | #tag#A#s#s'#s''#H#G#i #is |
---|
889 | @(G … (H … is)) |
---|
890 | qed. |
---|
891 | |
---|
892 | definition set_from_list : ∀tag.list (identifier tag) → identifier_map tag unit ≝ |
---|
893 | λtag.foldl … (add_set ?) ∅. |
---|
894 | |
---|
895 | coercion id_set_from_list : ∀tag.∀l : list (identifier tag).identifier_map tag unit ≝ |
---|
896 | set_from_list on _l : list (identifier ?) to identifier_map ? unit. |
---|
897 | |
---|
898 | lemma mem_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i. |
---|
899 | i∈m → lookup … m i ≠ None ?. |
---|
900 | #tag#A * #m #i |
---|
901 | whd in match (i∈?); |
---|
902 | elim (lookup ????) normalize [2: #x] |
---|
903 | * % #EQ destruct(EQ) |
---|
904 | qed. |
---|
905 | |
---|
906 | |
---|
907 | |
---|
908 | lemma mem_list_as_set : ∀tag.∀l : list (identifier tag). |
---|
909 | ∀i.i ∈ l → In ? l i. |
---|
910 | #tag #l @(list_elim_left … l) |
---|
911 | [ #i * |
---|
912 | | #t #h #Hi #i |
---|
913 | whd in ⊢ (?(???%?)→?); |
---|
914 | >foldl_append |
---|
915 | whd in ⊢ (?(???%?)→?); |
---|
916 | >mem_set_add |
---|
917 | @eq_identifier_elim |
---|
918 | [ #EQi destruct(EQi) |
---|
919 | #_ @Exists_append_r % % |
---|
920 | | #_ #H @Exists_append_l @Hi assumption |
---|
921 | ] |
---|
922 | ] |
---|
923 | qed. |
---|
924 | |
---|
925 | lemma list_as_set_mem : ∀tag.∀l : list (identifier tag). |
---|
926 | ∀i.In ? l i → i ∈ l. |
---|
927 | #tag #l @(list_elim_left … l) |
---|
928 | [ #i * |
---|
929 | | #t #h #Hi #i #H |
---|
930 | whd in ⊢ (?(???%?)); |
---|
931 | >foldl_append |
---|
932 | whd in ⊢ (?(???%?)); |
---|
933 | elim (Exists_append … H) -H |
---|
934 | [ #H >mem_set_add |
---|
935 | @eq_identifier_elim [//] #_ normalize |
---|
936 | @Hi @H |
---|
937 | | * [2: *] #EQi destruct(EQi) >mem_set_add_id % |
---|
938 | ] |
---|
939 | ] |
---|
940 | qed. |
---|
941 | |
---|
942 | lemma list_as_set_All : ∀tag,P.∀ l : list (identifier tag). |
---|
943 | (∀i.i ∈ l → P i) → All ? P l. |
---|
944 | #tag #P #l @(list_elim_left … l) |
---|
945 | [ #_ % |
---|
946 | | #x #l' #Hi |
---|
947 | whd in match (set_from_list … (l'@[x])); |
---|
948 | >foldl_append |
---|
949 | #H @All_append |
---|
950 | [ @Hi #i #G @H |
---|
951 | whd in ⊢ (?(???%?)); |
---|
952 | >mem_set_add @orb_Prop_r @G |
---|
953 | | % [2: %] |
---|
954 | @H |
---|
955 | whd in ⊢ (?(???%?)); |
---|
956 | @mem_set_add_id |
---|
957 | ] |
---|
958 | ] |
---|
959 | qed. |
---|
960 | |
---|
961 | lemma All_list_as_set : ∀tag,P.∀ l : list (identifier tag). |
---|
962 | All ? P l → ∀i.i ∈ l → P i. |
---|
963 | #tag #P #l @(list_elim_left … l) |
---|
964 | [ * #i * |
---|
965 | | #x #l' #Hi #H |
---|
966 | lapply (All_append_l … H) |
---|
967 | lapply (All_append_r … H) |
---|
968 | * #Px * #Pl' #i |
---|
969 | whd in match (set_from_list … (l'@[x])); |
---|
970 | >foldl_append |
---|
971 | >mem_set_add |
---|
972 | @eq_identifier_elim |
---|
973 | [ #EQx >EQx #_ @Px |
---|
974 | | #_ whd in match (?∨?); @Hi @Pl' |
---|
975 | ] |
---|
976 | ] |
---|
977 | qed. |
---|
978 | |
---|
979 | lemma map_mem_prop : |
---|
980 | ∀tag,A.∀m : identifier_map tag A.∀i. |
---|
981 | lookup ?? m i ≠ None ? → i ∈ m. |
---|
982 | #p #globals #m #i |
---|
983 | lapply (in_map_domain … m i) |
---|
984 | cases (i∈m) |
---|
985 | [ * #x #_ #_ % |
---|
986 | | #EQ >EQ * #ABS @ABS % |
---|
987 | ] qed. |
---|
988 | |
---|
989 | |
---|
990 | (* Attempt to choose an entry in the map/set, and if successful return the entry |
---|
991 | and the map/set without it. *) |
---|
992 | |
---|
993 | definition choose : ∀tag,A. identifier_map tag A → option (identifier tag × A × (identifier_map tag A)) ≝ |
---|
994 | λtag,A,m. |
---|
995 | match pm_choose A (match m with [ an_id_map m' ⇒ m' ]) with |
---|
996 | [ None ⇒ None ? |
---|
997 | | Some x ⇒ Some ? 〈〈an_identifier tag (\fst (\fst x)), \snd (\fst x)〉, an_id_map tag A (\snd x)〉 |
---|
998 | ]. |
---|
999 | |
---|
1000 | lemma choose_empty : ∀tag,A,m. |
---|
1001 | choose tag A m = None ? ↔ ∀id. lookup tag A m id = None ?. |
---|
1002 | #tag #A * #m lapply (pm_choose_empty A m) * #H1 #H2 % |
---|
1003 | [ normalize #C * @H1 cases (pm_choose A m) in C ⊢ %; [ // | normalize #x #E destruct ] |
---|
1004 | | normalize #L lapply (pm_choose_empty A m) cases (pm_choose A m) |
---|
1005 | [ * #H1 #H2 normalize // | #x * #_ #H lapply (H ?) [ #p @(L (an_identifier ? p)) | #E destruct ] ] |
---|
1006 | ] qed. |
---|
1007 | |
---|
1008 | lemma choose_some : ∀tag,A,m,id,a,m'. |
---|
1009 | choose tag A m = Some ? 〈〈id,a〉,m'〉 → |
---|
1010 | lookup tag A m id = Some A a ∧ |
---|
1011 | lookup tag A m' id = None A ∧ |
---|
1012 | (∀id'. id = id' ∨ lookup tag A m id' = lookup tag A m' id'). |
---|
1013 | #tag #A * #m * #id #a * #m' #C |
---|
1014 | lapply (pm_choose_some A m id a m' ?) |
---|
1015 | [ whd in C:(??%?); cases (pm_choose A m) in C ⊢ %; normalize [ #E destruct | * * #x #y #z #E destruct % ] ] |
---|
1016 | * * * #L1 #L2 #L3 #_ |
---|
1017 | % [ % [ @L1 | @L2 ] | * #id' cases (L3 id') [ /2/ | #L4 %2 @L4 ] ] |
---|
1018 | qed. |
---|
1019 | |
---|
1020 | lemma choose_some_subset : ∀tag,A,m,id,a,m'. |
---|
1021 | choose tag A m = Some ? 〈〈id,a〉,m'〉 → |
---|
1022 | m' ⊆ m. |
---|
1023 | #tag #A #m #id #a #m' #C |
---|
1024 | cases (choose_some … m' C) * #L1 #L2 #L3 |
---|
1025 | #id' whd in ⊢ (?% → ?%); |
---|
1026 | cases (L3 id') |
---|
1027 | [ #E destruct >L2 * |
---|
1028 | | #L4 >L4 // |
---|
1029 | ] qed. |
---|
1030 | |
---|
1031 | lemma choose_some_card : ∀tag,A,m,id,a,m'. |
---|
1032 | choose tag A m = Some ? 〈〈id,a〉,m'〉 → |
---|
1033 | |m| = S (|m'|). |
---|
1034 | #tag #A * #m * #id #a * #m' #C |
---|
1035 | lapply (pm_choose_some A m id a m' ?) |
---|
1036 | [ whd in C:(??%?); cases (pm_choose A m) in C ⊢ %; normalize [ #E destruct | * * #x #y #z #E destruct % ] ] |
---|
1037 | * #_ #H @H |
---|
1038 | qed. |
---|
1039 | |
---|
1040 | (* Remove an element from a map/set, returning the element and a new map/set. *) |
---|
1041 | |
---|
1042 | definition try_remove : ∀tag,A. identifier_map tag A → identifier tag → option (A × (identifier_map tag A)) ≝ |
---|
1043 | λtag,A,m,id. |
---|
1044 | match pm_try_remove A (match id with [ an_identifier id' ⇒ id']) (match m with [ an_id_map m' ⇒ m' ]) with |
---|
1045 | [ None ⇒ None ? |
---|
1046 | | Some x ⇒ Some ? 〈\fst x, an_id_map tag A (\snd x)〉 |
---|
1047 | ]. |
---|
1048 | |
---|
1049 | lemma try_remove_empty : ∀tag,A,m,id. |
---|
1050 | try_remove tag A m id = None ? ↔ lookup tag A m id = None ?. |
---|
1051 | #tag #A * #m * #id lapply (pm_try_remove_none A id m) * #H1 #H2 % |
---|
1052 | [ normalize #C @H1 cases (pm_try_remove A id m) in C ⊢ %; [ // | normalize #x #E destruct ] |
---|
1053 | | normalize #L >H2 // |
---|
1054 | ] qed. |
---|
1055 | |
---|
1056 | lemma try_remove_some : ∀tag,A,m,id,a,m'. |
---|
1057 | try_remove tag A m id = Some ? 〈a,m'〉 → |
---|
1058 | lookup tag A m id = Some A a ∧ |
---|
1059 | lookup tag A m' id = None A ∧ |
---|
1060 | (∀id'. id = id' ∨ lookup tag A m id' = lookup tag A m' id'). |
---|
1061 | #tag #A * #m * #id #a * #m' #C |
---|
1062 | lapply (pm_try_remove_some A id m a m' ?) |
---|
1063 | [ whd in C:(??%?); cases (pm_try_remove A id m) in C ⊢ %; normalize [ #E destruct | * #x #y #E destruct % ] ] |
---|
1064 | * * * #L1 #L2 #L3 #_ |
---|
1065 | % [ % [ @L1 | @L2 ] | * #id' cases (L3 id') [ /2/ | #L4 %2 @L4 ] ] |
---|
1066 | qed. |
---|
1067 | |
---|
1068 | lemma try_remove_some_card : ∀tag,A,m,id,a,m'. |
---|
1069 | try_remove tag A m id = Some ? 〈a,m'〉 → |
---|
1070 | |m| = S (|m'|). |
---|
1071 | #tag #A * #m * #id #a * #m' #C |
---|
1072 | lapply (pm_try_remove_some A id m a m' ?) |
---|
1073 | [ whd in C:(??%?); cases (pm_try_remove A id m) in C ⊢ %; normalize [ #E destruct | * #x #y #E destruct % ] ] |
---|
1074 | * #_ #H @H |
---|
1075 | qed. |
---|
1076 | |
---|
1077 | lemma try_remove_this : ∀tag,A,m,id,a. |
---|
1078 | lookup tag A m id = Some A a → |
---|
1079 | ∃m'. try_remove tag A m id = Some ? 〈a,m'〉. |
---|
1080 | #tag #A * #m * #id #a #L |
---|
1081 | cases (pm_try_remove_some' A id m a L) |
---|
1082 | #m' #R %{(an_id_map tag A m')} whd in ⊢ (??%?); >R % |
---|
1083 | qed. |
---|
1084 | |
---|
1085 | (* Link a map with the set consisting of its domain. *) |
---|
1086 | |
---|
1087 | definition id_set_of_map : ∀tag,A. identifier_map tag A → identifier_set tag ≝ |
---|
1088 | λtag,A,m. an_id_map tag unit (map … (λ_. it) (match m with [ an_id_map m' ⇒ m'])). |
---|
1089 | |
---|
1090 | lemma id_set_of_map_subset : ∀tag,A,m. |
---|
1091 | id_set_of_map tag A m ⊆ m. |
---|
1092 | #tag #A * #m * #id normalize |
---|
1093 | >lookup_opt_map normalize cases (lookup_opt ???) // |
---|
1094 | qed. |
---|
1095 | |
---|
1096 | lemma id_set_of_map_present : ∀tag,A,m,id. |
---|
1097 | present tag A m id ↔ present tag unit (id_set_of_map … m) id. |
---|
1098 | #tag #A * #m * #id % |
---|
1099 | normalize @not_to_not |
---|
1100 | >lookup_opt_map cases (lookup_opt ???) normalize // |
---|
1101 | #a #E destruct |
---|
1102 | qed. |
---|
1103 | |
---|
1104 | lemma id_set_of_map_card : ∀tag,A,m. |
---|
1105 | |m| = |id_set_of_map tag A m|. |
---|
1106 | #tag #A * #m whd in ⊢ (??%%); >map_size // |
---|
1107 | qed. |
---|
1108 | |
---|
1109 | |
---|
1110 | (* Transforming a list into a set. *) |
---|
1111 | |
---|
1112 | definition set_of_list : ∀tag. list (identifier tag) → identifier_set tag ≝ |
---|
1113 | λtag,l. foldl ?? (λs,id. add_set tag s id) ∅ l. |
---|
1114 | |
---|
1115 | lemma fold_add_set_monotone : ∀tag,l,s,id. |
---|
1116 | present tag unit s id → |
---|
1117 | present tag unit (foldl ?? (λs,id. add_set tag s id) s l) id. |
---|
1118 | #tag #l elim l |
---|
1119 | [ // |
---|
1120 | | #h #t #IH #s #id #PR |
---|
1121 | whd in ⊢ (???%?); @IH |
---|
1122 | @lookup_add_oblivious |
---|
1123 | @PR |
---|
1124 | ] qed. |
---|
1125 | |
---|
1126 | lemma in_set_of_list : ∀tag,l,id. |
---|
1127 | Exists ? (λid'. id' = id) l → |
---|
1128 | present ?? (set_of_list tag l) id. |
---|
1129 | #tag #l #id whd in match (set_of_list ??); generalize in match ∅; elim l |
---|
1130 | [ #s * |
---|
1131 | | #id' #tl #IH #s * |
---|
1132 | [ #E whd in ⊢ (???%?); destruct |
---|
1133 | @fold_add_set_monotone // |
---|
1134 | | @IH |
---|
1135 | ] |
---|
1136 | ] qed. |
---|
1137 | |
---|
1138 | lemma in_set_of_list' : ∀tag,l,id. |
---|
1139 | present ?? (set_of_list tag l) id → |
---|
1140 | Exists ? (λid'. id = id') l. |
---|
1141 | #tag #l #id whd in match (set_of_list ??); |
---|
1142 | cut (¬present ?? ∅ id) [ /3/ ] |
---|
1143 | generalize in match ∅; |
---|
1144 | elim l |
---|
1145 | [ #s #F #T @⊥ @(absurd … T F) |
---|
1146 | | #id' #tl #IH #s #F #PR whd in PR:(???%?); |
---|
1147 | cases (identifier_eq … id id') |
---|
1148 | [ #E destruct /2/ |
---|
1149 | | #NE %2 @(IH … PR) |
---|
1150 | @(not_to_not … F) /2/ |
---|
1151 | ] |
---|
1152 | ] qed. |
---|
1153 | |
---|
1154 | |
---|
1155 | (* Returns the domain of a map as the canonical set (one made only from the |
---|
1156 | empty set and addition. *) |
---|
1157 | definition domain_of_map : ∀tag,A. identifier_map tag A → identifier_set tag ≝ |
---|
1158 | λtag,A,m. an_id_map tag unit (domain_of_pm A (match m with [ an_id_map m ⇒ m ])). |
---|
1159 | lemma domain_of_map_present : ∀tag,A,m,id. |
---|
1160 | present tag A m id ↔ present tag unit (domain_of_map … m) id. |
---|
1161 | #tag #A * #m * #p @domain_of_pm_present |
---|
1162 | qed. |
---|
1163 | |
---|
1164 | (* Some lemmas for reasoning about folds. *) |
---|
1165 | |
---|
1166 | lemma foldi_ind : ∀A,B,tag,f,m,b. ∀P:B → identifier_set tag → Prop. |
---|
1167 | P b (empty_set …) → |
---|
1168 | (∀k,ks,a,b. ¬present ?? ks k → lookup ?? m k = Some ? a → P b ks → P (f k a b) (add_set tag ks k)) → |
---|
1169 | P (foldi A B tag f m b) (domain_of_map … m). |
---|
1170 | #A #B #tag #f * #m #b #P #P0 #STEP |
---|
1171 | whd in match (foldi ??????); change with (an_id_map ?? (domain_of_pm A m)) in match (domain_of_map ???); |
---|
1172 | @pm_fold_ind |
---|
1173 | [ @P0 |
---|
1174 | | #p #ps #a #b0 #FR #L #pre @(STEP (an_identifier tag p) (an_id_map tag unit ps)) |
---|
1175 | [ normalize >FR /3/ |
---|
1176 | | @L |
---|
1177 | | @pre |
---|
1178 | ] |
---|
1179 | ] qed. |
---|
1180 | |
---|
1181 | lemma foldi_ind' : ∀A,B,tag,f,m,b,b'. ∀P:B → identifier_set tag → Prop. |
---|
1182 | P b (empty_set …) → |
---|
1183 | (∀k,ks,a,b. ¬present ?? ks k → lookup ?? m k = Some ? a → P b ks → P (f k a b) (add_set tag ks k)) → |
---|
1184 | foldi A B tag f m b = b' → |
---|
1185 | P b' (domain_of_map … m). |
---|
1186 | #H1 #H2 #H3 #H4 #H5 #H6 #H7 #H8 #H9 #H10 #H11 destruct @foldi_ind /2/ |
---|
1187 | qed. |
---|
1188 | |
---|