source: src/common/Identifiers.ma @ 2314

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

Move generic definitions from recent commit to appropriate places.

File size: 32.4 KB
Line 
1include "basics/types.ma".
2include "ASM/String.ma".
3include "utilities/binary/positive.ma".
4include "utilities/lists.ma".
5include "utilities/extralib.ma".
6include "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.
12inductive identifier (tag:String) : Type[0] ≝
13  an_identifier : Pos → identifier tag.
14*)
15
16record universe (tag:String) : Type[0] ≝
17{
18  next_identifier : Pos
19}.
20
21definition new_universe : ∀tag:String. universe tag ≝
22  λtag. mk_universe tag one.
23
24let 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
29let 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
33lemma 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 //
37qed.
38
39lemma 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/
44qed.
45
46lemma 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/
51qed.
52
53
54let 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
57lemma 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/ ]
63qed.
64
65definition 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
75lemma 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
79change with (P (eqb ??))
80@(eqb_elim x y P) [ /2 by / | * #H @F % #E destruct /2 by / ]
81qed.
82
83lemma 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  ]
103qed.
104
105axiom neq_identifier_neq:
106  ∀tag: String.
107  ∀l, r: identifier tag.
108    eq_identifier tag l r = false → (l = r → False).
109
110include "basics/deqsets.ma".
111definition 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)]
114qed.
115
116unification hint 0 ≔ tag; D ≟ Deq_identifier tag
117(*-----------------------------------------------------*)⊢
118identifier tag ≡ carr D.
119
120definition word_of_identifier ≝
121  λt.
122  λl: identifier t.
123  match l with   
124  [ an_identifier l' ⇒ l'
125  ].
126
127lemma eq_identifier_refl : ∀tag,id. eq_identifier tag id id = true.
128#tag * #id whd in ⊢ (??%?); >eqb_n_n @refl
129qed.
130
131axiom 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
137lemma 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/
139qed.
140
141definition 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 ]
144lapply E @eqb_elim
145[ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2 by absurd/ ]
146qed.
147
148definition 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. *)
153let rec distinct_env tag (A:Type[0]) (l:list (identifier tag × A)) on l : Prop ≝
154match l with
155[ nil ⇒ True
156| cons hd tl ⇒ All ? (λia. \fst hd ≠ \fst ia) tl ∧
157               distinct_env tag A tl
158].
159
160lemma 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
166lemma 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
175axiom DuplicateVariable : String.
176
177let 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) ≝
178match 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
189let rec check_distinct_env tag (A:Type[0]) (l:list (identifier tag × A)) on l : res (distinct_env tag A l) ≝
190match 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
203include "common/PositiveMap.ma".
204
205inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝
206  an_id_map : positive_map A → identifier_map tag A.
207 
208definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝
209  λtag,A. an_id_map tag A (pm_leaf A).
210
211let 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
215definition lookup_def ≝
216λtag,A,m,l,d. match lookup tag A m l with [ None ⇒ d | Some x ⇒ x].
217
218definition member ≝
219  λtag,A.λm:identifier_map tag A.λl:identifier tag.
220  match lookup tag A m l with [ None ⇒ false | _ ⇒ true ].
221
222interpretation "identifier map membership" 'mem a b = (member ?? b a).
223
224definition lookup_safe : ∀tag,A.∀m : identifier_map tag A.∀i.i∈m → A ≝
225λtag,A,m,i.
226match lookup … m i return λx.match x in option return λ_.bool with [ _ ⇒ ?] → ? with
227[ Some x ⇒ λ_.x
228| None ⇒ λprf.⊥
229]. @prf qed.
230
231lemma 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);
233whd in match lookup_safe; normalize nodelta
234cases (lookup ????) normalize nodelta [*] // qed.
235
236(* Always adds the identifier to the map. *)
237let 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
241lemma 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
245qed.
246
247lemma 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
251qed.
252
253lemma 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/
258qed.
259
260axiom 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
264lemma 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
268cases (identifier_eq ? i j)
269[ #E >E >lookup_add_hit % #N destruct
270| #NE >lookup_add_miss //
271] qed.
272
273lemma 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
277cases (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. *)
283definition 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
291definition 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 ?).
294cases m in H ⊢ %; #m' normalize //
295qed.
296
297inductive 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
300lemma 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
303cut (idmap_pred_graph tag A m id a L f (f id a L)) [ % ]
304cases (f id a L) #H
305cut (idmap_pred_graph tag A m id a L' f ?) [ 2,5: @H | 1,4: skip ] * //
306qed.
307
308lemma 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
311whd 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
317axiom MissingId : String.
318
319(* Only updates an existing entry; fails with an error otherwise. *)
320definition 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
328definition foldi:
329  ∀A, B: Type[0].
330  ∀tag: String.
331  (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝
332λA,B,tag,f,m,b.
333  match m with
334  [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ].
335
336(* A predicate that an identifier is in a map, and a failure-avoiding lookup
337   and update using it. *)
338
339definition present : ∀tag,A. identifier_map tag A → identifier tag → Prop ≝
340λtag,A,m,i. lookup … m i ≠ None ?.
341
342lemma member_present : ∀tag,A,m,id.
343  member tag A m id = true → present tag A m id.
344#tag #A * #m #id normalize cases (lookup_opt A ??) normalize
345[ #E destruct
346| #x #E % #E' destruct
347] qed.
348
349lemma present_member : ∀tag,A,m,id.
350  present tag A m id → member tag A m id.
351#tag #A #m #id whd in ⊢ (% → ?%); cases (lookup ????) // * #H cases (H (refl ??))
352qed.
353
354definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝
355λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ].
356cases H #H'  cases (H' (refl ??)) qed.
357
358lemma lookup_lookup_present : ∀tag,A,m,id,p.
359  lookup tag A m id = Some ? (lookup_present tag A m id p).
360#tag #A #m #id #p
361whd in p ⊢ (???(??%));
362cases (lookup tag A m id) in p ⊢ %;
363[ * #H @⊥ @H @refl
364| #a #H @refl
365] qed.
366
367lemma lookup_is_present : ∀tag,T,m,id,t.
368  lookup tag T m id = Some T t →
369  present ?? m id.
370#tag #T #m #id #t #L normalize >L % #E destruct
371qed.
372
373lemma lookup_present_eq : ∀tag,T,m,id,t.
374  lookup tag T m id = Some T t →
375  ∀H. lookup_present tag T m id H = t.
376#tag #T #m #id #t #L #H
377lapply (lookup_lookup_present … H) >L #E destruct %
378qed.
379
380
381definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝
382λtag,A,m,l,p,a.
383  let l' ≝ match l with [ an_identifier l' ⇒ l' ] in
384  let m' ≝ match m with [ an_id_map m' ⇒ m' ] in
385  let u' ≝ update A l' a m' in
386  match u' return λx. update ???? = x → ? with
387  [ None ⇒ λE.⊥
388  | Some m' ⇒ λ_. an_id_map tag A m'
389  ] (refl ? u').
390cases l in p E; cases m; -l' -m' #m' #l'
391whd in ⊢ (% → ?);
392 whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?);
393#NL #U cases NL #H @H @(update_fail … U)
394qed.
395
396lemma update_still_present : ∀tag,A,m,id,a,id'.
397  ∀H:present tag A m id.
398  ∀H':present tag A m id'.
399  present tag A (update_present tag A m id' H' a) id.
400#tag #A * #m * #id #a * #id' #H #H'
401whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta
402cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id'))
403[ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U)
404  % #E' destruct
405| #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?));
406  <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ]
407] qed.
408
409lemma lookup_present_add_hit:
410  ∀tag, A, map, k, v, k_pres.
411    lookup_present tag A (add … map k v) k k_pres = v.
412  #tag #a #map #k #v #k_pres
413  lapply (lookup_lookup_present … (add … map k v) … k_pres)
414  >lookup_add_hit #Some_assm destruct(Some_assm)
415  <e0 %
416qed.
417
418lemma lookup_present_add_miss:
419  ∀tag, A, map, k, k', v, k_pres', k_pres''.
420    k' ≠ k →
421      lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''.
422  #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm
423  lapply (lookup_lookup_present … (add … map k v) ? k_pres')
424  >lookup_add_miss try assumption
425  #Some_assm
426  lapply (lookup_lookup_present … map k') >Some_assm #Some_assm'
427  lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption
428qed.
429
430lemma present_add_present:
431  ∀tag, a, map, k, k', v.
432    k' ≠ k →
433      present tag a (add tag a map k v) k' →
434        present tag a map k'.
435  #tag #a #map #k #k' #v #neq_hyp #present_hyp
436  whd in match present; normalize nodelta
437  whd in match present in present_hyp; normalize nodelta in present_hyp;
438  cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp
439  lapply (lookup_add_cases tag ?????? Some_eq_hyp) *
440  [1:
441    * #k_eq_hyp @⊥ /2/
442  |2:
443    #Some_eq_hyp' /2/
444  ]
445qed.
446
447lemma present_add_hit:
448  ∀tag, a, map, k, v.
449    present tag a (add tag a map k v) k.
450  #tag #a #map #k #v
451  whd >lookup_add_hit
452  % #absurd destruct
453qed.
454
455lemma present_add_miss:
456  ∀tag, a, map, k, k', v.
457    k' ≠ k → present tag a map k' → present tag a (add tag a map k v) k'.
458  #tag #a #map #k #k' #v #neq_assm #present_assm
459  whd >lookup_add_miss assumption
460qed.
461
462
463let rec fresh_for_map tag A (id:identifier tag) (m:identifier_map tag A) on id : Prop ≝
464  lookup … m id = None A.
465
466lemma fresh_for_empty_map : ∀tag,A,id.
467  fresh_for_map tag A id (empty_map tag A).
468#tag #A * #id //
469qed.
470
471definition fresh_map_for_univ ≝
472λtag,A. λm:identifier_map tag A. λu:universe tag.
473  ∀id. present tag A m id → fresh_for_univ tag id u.
474
475lemma fresh_fresh_for_map : ∀tag,A,m,id,u,u'.
476  fresh_map_for_univ tag A m u →
477  〈id,u'〉 = fresh tag u →
478  fresh_for_map tag A id m.
479#tag #A * #m * #id * #u * #u' whd in ⊢ (% → ???% → %);
480#FMU #E destruct lapply (FMU (an_identifier tag u)) whd in ⊢ ((% → %) → ?);
481generalize in ⊢ ((?(??%?) → ?) → ??%?); *
482[ // | #a #H @False_ind lapply (H ?) /2 by absurd/ % #E destruct
483qed.
484
485lemma fresh_map_preserved : ∀tag,A,m,u,u',id.
486  fresh_map_for_univ tag A m u →
487  〈id,u'〉 = fresh tag u →
488  fresh_map_for_univ tag A m u'.
489#tag #A #m #u * #u' #id whd in ⊢ (% → ? → %); #H #E
490#id' #PR @(fresh_remains_fresh … E) @H //
491qed.
492
493lemma fresh_map_add : ∀tag,A,m,u,id,a.
494  fresh_map_for_univ tag A m u →
495  fresh_for_univ tag id u →
496  fresh_map_for_univ tag A (add tag A m id a) u.
497#tag #A * #m #u #id #a #Hm #Hi
498#id' #PR cases (identifier_eq tag id' id)
499[ #E >E @Hi
500| #NE @Hm whd in PR;
501  change with (add tag A (an_id_map tag A m) id a) in PR:(?(??(???%?)?));
502  >lookup_add_miss in PR; //
503] qed.
504
505lemma present_not_fresh : ∀tag,A,m,id,id'.
506  present tag A m id →
507  fresh_for_map tag A id' m →
508  id ≠ id'.
509#tag #A #m #id * #id' whd in ⊢ (% → % → ?);
510* #NE #E % #E' destruct @(NE E)
511qed.
512
513lemma fresh_for_map_add : ∀tag,A,id,m,id',a.
514  id ≠ id' →
515  fresh_for_map tag A id m →
516  fresh_for_map tag A id (add tag A m id' a).
517#tag #A * #id #m #id' #a #NE #F
518whd >lookup_add_miss //
519qed.
520
521
522(* Sets *)
523
524definition identifier_set ≝ λtag.identifier_map tag unit.
525
526definition empty_set : ∀tag.identifier_set tag ≝ λtag.empty_map ….
527
528
529definition add_set : ∀tag.identifier_set tag → identifier tag → identifier_set tag ≝
530  λtag,s,i.add … s i it.
531
532definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝
533λtag,i. add_set tag (empty_set tag) i.
534
535let rec union_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_set tag ≝
536  an_id_map tag unit (merge … (λo,o'.match o with [Some _ ⇒ Some ? it | None ⇒ !_ o'; return it])
537    (match s with [ an_id_map s0 ⇒ s0 ])
538    (match s' with [ an_id_map s1 ⇒ s1 ])).
539
540
541(* set minus is generalised to maps *)
542let rec minus_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_map tag A ≝
543  an_id_map tag A (merge A B A (λo,o'.match o' with [None ⇒ o | Some _ ⇒ None ?])
544    (match s with [ an_id_map s0 ⇒ s0 ])
545    (match s' with [ an_id_map s1 ⇒ s1 ])).
546
547notation "a ∖ b" left associative with precedence 55 for @{'setminus $a $b}.
548
549interpretation "identifier set union" 'union a b = (union_set ??? a b).
550notation "∅" non associative with precedence 90 for @{ 'empty }.
551interpretation "empty identifier set" 'empty = (empty_set ?).
552interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a).
553interpretation "identifier map difference" 'setminus a b = (minus_set ??? a b).
554
555definition IdentifierSet : String → Setoid ≝ λtag.
556  mk_Setoid (identifier_set tag) (λs,s'.∀i.i ∈ s = (i ∈ s')) ???.
557  // qed.
558
559unification hint 0 ≔ tag;
560S ≟ IdentifierSet tag
561(*-----------------------------*)⊢
562identifier_set tag ≡ std_supp S.
563unification hint 0 ≔ tag;
564S ≟ IdentifierSet tag
565(*-----------------------------*)⊢
566identifier_map tag unit ≡ std_supp S.
567
568lemma mem_set_add : ∀tag,A.∀i,j : identifier tag.∀s,x.
569  i ∈ add ? A s j x = (eq_identifier ? i j ∨ i ∈ s).
570#tag #A *#i *#j *#s #x normalize
571@(eqb_elim i j)
572[#EQ destruct
573  >(lookup_opt_insert_hit A x j)
574|#NEQ >(lookup_opt_insert_miss … s NEQ)
575] elim (lookup_opt  A j s) normalize // qed.
576
577lemma mem_set_add_id : ∀tag,A,i,s,x.bool_to_Prop (i ∈ add tag A s i x).
578#tag #A #i #s #x >mem_set_add
579@eq_identifier_elim [#_ %| #ABS elim (absurd … (refl ? i) ABS)] qed.
580
581lemma in_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i.
582  if i ∈ m then (∃s.lookup … m i = Some ? s) else (lookup … m i = None ?).
583#tag #A * #m * #i normalize
584elim (lookup_opt A i m) normalize
585[ % | #x %{x} % ]
586qed.
587
588lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s.
589#tag * normalize #m >map_opt_id_eq_ext // * %
590qed.
591
592lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s.
593#tag * * [//] *[2: *] #l#r normalize
594>map_opt_id_eq_ext [1,3: >map_opt_id_eq_ext [2,4: *] |*: *] //
595qed.
596
597lemma minus_empty_l : ∀tag,A.∀s:identifier_map tag A. ∅ ∖ s ≅ ∅.
598#tag #A * * [//] *[2:#x]#l#r * * normalize [1,4://]
599#p >lookup_opt_map elim (lookup_opt ???) normalize //
600qed.
601
602lemma minus_empty_r : ∀tag,A.∀s:identifier_map tag A. s ∖ ∅ = s.
603#tag #A * * [//] *[2:#x]#l#r normalize
604>map_opt_id >map_opt_id //
605qed.
606
607lemma mem_set_union : ∀tag.∀i : identifier tag.∀s,s' : identifier_set tag.
608  i ∈ (s ∪ s') = (i ∈ s ∨ i ∈ s').
609#tag * #i * #s * #s' normalize
610>lookup_opt_merge [2: @refl]
611elim (lookup_opt ???)
612elim (lookup_opt ???)
613normalize // qed.
614
615lemma mem_set_minus : ∀tag,A,B.∀i : identifier tag.∀s : identifier_map tag A.
616  ∀s' : identifier_map tag B.
617  i ∈ (s ∖ s') = (i ∈ s ∧ ¬ i ∈ s').
618#tag #A #B * #i * #s * #s' normalize
619>lookup_opt_merge [2: @refl]
620elim (lookup_opt ???)
621elim (lookup_opt ???)
622normalize // qed.
623
624lemma set_eq_ext_node : ∀tag.∀o,o',l,l',r,r'.
625  an_id_map tag ? (pm_node ? o l r) ≅ an_id_map … (pm_node ? o' l' r') →
626    o = o' ∧ an_id_map tag ? l ≅ an_id_map … l' ∧ an_id_map tag ? r ≅ an_id_map … r'.
627#tag#o#o'#l#l'#r#r'#H
628%[
629%[ lapply (H (an_identifier ? one))
630   elim o [2: *] elim o' [2,4: *] normalize // #EQ destruct
631 | *#p lapply (H (an_identifier ? (p0 p))) normalize //
632]| *#p lapply (H (an_identifier ? (p1 p))) normalize //
633]
634qed.
635
636lemma set_eq_ext_leaf : ∀tag,A.∀o,l,r.
637  (∀i.i∈an_id_map tag A (pm_node ? o l r) = false) →
638    o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false).
639#tag#A#o#l#r#H
640%[
641%[ lapply (H (an_identifier ? one))
642   elim o [2: #a] normalize // #EQ destruct
643 | *#p lapply (H (an_identifier ? (p0 p))) normalize //
644]| *#p lapply (H (an_identifier ? (p1 p))) normalize //
645]
646qed.
647
648
649definition id_map_size : ∀tag : String.∀A. identifier_map tag A → ℕ ≝
650  λtag,A,s.match s with [an_id_map p ⇒ |p|].
651
652interpretation "identifier map domain size" 'norm s = (id_map_size ?? s).
653
654lemma set_eq_ext_empty_to_card : ∀tag,A.∀s : identifier_map tag A. (∀i.i∈s = false) → |s| = 0.
655#tag#A * #s elim s [//]
656#o#l#r normalize in ⊢((?→%)→(?→%)→?); #Hil #Hir #H
657elim (set_eq_ext_leaf … H) * #EQ destruct #Hl #Hr normalize
658>(Hil Hl) >(Hir Hr) // qed.
659
660lemma set_eq_ext_to_card : ∀tag.∀s,s' : identifier_set tag. s ≅ s' → |s| = |s'|.
661#tag *#s elim s
662[** [//] #o#l#r #H
663  >(set_eq_ext_empty_to_card … (std_symm … H)) //
664| #o#l#r normalize in ⊢((?→?→??%?)→(?→?→??%?)→?);
665  #Hil #Hir **
666  [#H @(set_eq_ext_empty_to_card … H)]
667  #o'#l'#r' #H elim (set_eq_ext_node … H) * #EQ destruct(EQ) #Hl #Hr
668  normalize >(Hil ? Hl) >(Hir ? Hr) //
669] qed.
670
671lemma add_size: ∀tag,A,s,i,x.
672  |add tag A s i x| = (if i ∈ s then 0 else 1) + |s|.
673#tag #A *#s *#i #x
674lapply (insert_size ? i x s)
675lapply (refl ? (lookup_opt ? i s))
676generalize in ⊢ (???%→?); * [2: #x']
677normalize #EQ >EQ normalize //
678qed.
679
680lemma mem_set_O_lt_card : ∀tag,A.∀i.∀s : identifier_map tag A. i ∈ s → |s| > 0.
681#tag #A * #i * #s normalize #H
682@(lookup_opt_O_lt_size … i)
683% #EQ >EQ in H; normalize *
684qed.
685
686(* NB: no control on values if applied to maps *)
687definition set_subset ≝ λtag,A,B.λs : identifier_map tag A.
688  λs' : identifier_map tag B. ∀i.i ∈ s → (bool_to_Prop (i ∈ s')).
689
690interpretation "identifier set subset" 'subseteq s s' = (set_subset ??? s s').
691
692lemma add_subset :
693  ∀tag,A,B.∀i : identifier tag.∀x.∀s : identifier_map ? A.∀s' : identifier_map ? B.
694    i ∈ s' → s ⊆ s' → add … s i x ⊆ s'.
695#tag#A#B#i#x#s#s' #H #G #j
696>mem_set_add
697@eq_identifier_elim #H' [* >H' @H | #js @(G ? js)]
698qed.
699
700definition set_forall : ∀tag,A.(identifier tag → Prop) →
701  identifier_map tag A → Prop ≝ λtag,A,P,m.∀i. i ∈ m → P i.
702 
703lemma set_forall_add : ∀tag,P,m,i.set_forall tag ? P m → P i →
704  set_forall tag ? P (add_set ? m i).
705#tag#P#m#i#Pm#Pi#j
706>mem_set_add
707@eq_identifier_elim
708[#EQ destruct(EQ) #_ @Pi
709|#_ @Pm
710]
711qed.
712
713include "utilities/proper.ma".
714
715lemma minus_subset : ∀tag,A,B.minus_set tag A B ⊨ set_subset … ++> set_subset … -+> set_subset ….
716#tag#A#B#s#s' #H #s'' #s''' #G #i
717>mem_set_minus >mem_set_minus
718#H' elim (andb_Prop_true … H') -H' #is #nis''
719>(H … is)
720elim (true_or_false_Prop (i∈s'''))
721[ #is''' >(G … is''') in nis''; *
722| #nis''' >nis''' %
723]
724qed.
725
726lemma subset_node : ∀tag,A,B.∀o,o',l,l',r,r'.
727  an_id_map tag A (pm_node ? o l r) ⊆ an_id_map tag B (pm_node ? o' l' r') →
728    opt_All ? (λ_.o' ≠ None ?) o ∧ an_id_map tag ? l ⊆ an_id_map tag  ? l' ∧
729      an_id_map tag ? r ⊆ an_id_map tag ? r'.
730#tag#A#B#o#o'#l#l'#r#r'#H
731%[%
732  [ lapply (H (an_identifier ? (one))) elim o [2: #a] elim o' [2:#b]
733    normalize // [#_ % #ABS destruct(ABS) | #G lapply (G I) *]
734  | *#p lapply (H (an_identifier ? (p0 p)))
735  ]
736 | *#p lapply (H (an_identifier ? (p1 p)))
737] #H @H
738qed.
739
740lemma subset_leaf : ∀tag,A.∀o,l,r.
741  an_id_map tag A (pm_node ? o l r) ⊆ ∅ →
742    o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false).
743#tag#A#o#l#r#H
744%[
745%[ lapply (H (an_identifier ? one))
746   elim o [2: #a] normalize // #EQ lapply(EQ I) *
747 | *#p lapply (H (an_identifier ? (p0 p)))
748 ]
749|  *#p lapply (H (an_identifier ? (p1 p)))
750] normalize elim (lookup_opt ? p ?) normalize
751// #a #H lapply (H I) *
752qed.
753
754lemma subset_card : ∀tag,A,B.∀s : identifier_map tag A.∀s' : identifier_map tag B.
755  s ⊆ s' → |s| ≤ |s'|.
756#tag #A #B *#s elim s
757[ //
758| #o#l#r #Hil #Hir **
759  [ #H elim (subset_leaf … H) * #EQ >EQ #Hl #Hr
760    lapply (set_eq_ext_empty_to_card … Hl)
761    lapply (set_eq_ext_empty_to_card … Hr)
762    normalize //
763  | #o' #l' #r' #H elim (subset_node … H) *
764    elim o [2: #a] elim o' [2,4: #a']
765    [3: #G normalize in G; elim(absurd ? (refl ??) G)
766    |*: #_ #Hl #Hr lapply (Hil ? Hl) lapply (Hir ? Hr)
767      normalize #H1 #H2
768      [@le_S_S | @(transitive_le … (|l'|+|r'|)) [2: / by /]]
769      @le_plus assumption
770    ]
771  ]
772]
773qed.
774
775lemma mem_set_empty : ∀tag,A.∀i: identifier tag. i∈empty_map tag A = false.
776#tag #A * #i normalize %
777qed.
778
779lemma mem_set_singl_to_eq : ∀tag.∀i,j : identifier tag.i∈{(j)} → i = j.
780#tag
781#i #j >mem_set_add >mem_set_empty
782#H elim (orb_true_l … H) -H
783[@eq_identifier_elim [//] #_] #EQ destruct
784qed.
785
786lemma subset_add_set : ∀tag,i,s.s ⊆ add_set tag s i.
787#tag#i#s#j #H >mem_set_add >H
788>commutative_orb %
789qed.
790
791lemma add_set_monotonic : ∀tag,i,s,s'.s ⊆ s' → add_set tag s i ⊆ add_set tag s' i.
792#tag#i#s#s' #H #j >mem_set_add >mem_set_add
793@orb_elim elim (eq_identifier ???)
794whd lapply (H j) /2 by /
795qed.
796
797lemma transitive_subset : ∀tag,A.transitive ? (set_subset tag A A).
798#tag#A#s#s'#s''#H#G#i #is
799@(G … (H … is))
800qed.
801
802definition set_from_list : ∀tag.list (identifier tag) → identifier_map tag unit ≝
803  λtag.foldl … (add_set ?) ∅.
804
805coercion id_set_from_list : ∀tag.∀l : list (identifier tag).identifier_map tag unit ≝
806  set_from_list on _l : list (identifier ?) to identifier_map ? unit.
807
808lemma mem_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i.
809i∈m → lookup … m i ≠ None ?.
810#tag#A * #m #i
811whd in match (i∈?);
812elim (lookup ????) normalize [2: #x]
813* % #EQ destruct(EQ)
814qed.
815
816
817
818lemma mem_list_as_set : ∀tag.∀l : list (identifier tag).
819  ∀i.i ∈ l → In ? l i.
820#tag #l @(list_elim_left … l)
821[ #i *
822| #t #h #Hi  #i
823  whd in ⊢ (?(???%?)→?);
824  >foldl_append
825  whd in ⊢ (?(???%?)→?);
826  >mem_set_add
827  @eq_identifier_elim
828  [ #EQi destruct(EQi)
829    #_ @Exists_append_r % %
830  | #_ #H @Exists_append_l @Hi assumption
831  ]
832]
833qed.
834
835lemma list_as_set_mem : ∀tag.∀l : list (identifier tag).
836  ∀i.In ? l i → i ∈ l.
837#tag #l @(list_elim_left … l)
838[ #i *
839| #t #h #Hi #i #H
840  whd in ⊢ (?(???%?));
841  >foldl_append
842  whd in ⊢ (?(???%?));
843  elim (Exists_append … H) -H
844  [ #H >mem_set_add
845    @eq_identifier_elim [//] #_ normalize
846    @Hi @H
847  | * [2: *] #EQi destruct(EQi) >mem_set_add_id %
848  ]
849]
850qed.
851
852lemma list_as_set_All : ∀tag,P.∀ l : list (identifier tag).
853  (∀i.i ∈ l → P i) → All ? P l.
854#tag #P #l @(list_elim_left … l)
855[ #_ %
856| #x #l' #Hi
857  whd in match (set_from_list … (l'@[x]));
858  >foldl_append
859  #H @All_append
860  [ @Hi #i #G @H
861    whd in ⊢ (?(???%?));
862    >mem_set_add @orb_Prop_r @G
863  | % [2: %]
864    @H
865    whd in ⊢ (?(???%?));
866    @mem_set_add_id
867  ]
868]
869qed.
870
871lemma All_list_as_set : ∀tag,P.∀ l : list (identifier tag).
872  All ? P l → ∀i.i ∈ l → P i.
873#tag #P #l @(list_elim_left … l)
874[ * #i *
875| #x #l' #Hi #H
876  lapply (All_append_l … H)
877  lapply (All_append_r … H)
878  * #Px * #Pl' #i
879  whd in match (set_from_list … (l'@[x]));
880  >foldl_append
881  >mem_set_add
882  @eq_identifier_elim
883  [ #EQx >EQx #_ @Px
884  | #_ whd in match (?∨?); @Hi @Pl'
885  ]
886]
887qed. 
888
889lemma map_mem_prop :
890  ∀tag,A.∀m : identifier_map tag A.∀i.
891  lookup ?? m i ≠ None ? → i ∈ m.
892#p #globals #m #i
893lapply (in_map_domain … m i)
894cases (i∈m)
895[ * #x #_ #_ %
896| #EQ >EQ * #ABS @ABS %
897] qed.
898
899
900(* Attempt to choose an entry in the map/set, and if successful return the entry
901   and the map/set without it. *)
902
903definition choose : ∀tag,A. identifier_map tag A → option (identifier tag × A × (identifier_map tag A)) ≝
904λtag,A,m.
905  match pm_choose A (match m with [ an_id_map m' ⇒ m' ]) with
906  [ None ⇒ None ?
907  | Some x ⇒ Some ? 〈〈an_identifier tag (\fst (\fst x)), \snd (\fst x)〉, an_id_map tag A (\snd x)〉
908  ].
909
910lemma choose_empty : ∀tag,A,m.
911  choose tag A m = None ? ↔ ∀id. lookup tag A m id = None ?.
912#tag #A * #m lapply (pm_choose_empty A m) * #H1 #H2 %
913[ normalize #C * @H1 cases (pm_choose A m) in C ⊢ %; [ // | normalize #x #E destruct ]
914| normalize #L lapply (pm_choose_empty A m) cases (pm_choose A m)
915  [ * #H1 #H2 normalize // | #x * #_ #H lapply (H ?) [ #p @(L (an_identifier ? p)) | #E destruct ] ]
916] qed.
917
918lemma choose_some : ∀tag,A,m,id,a,m'.
919  choose tag A m = Some ? 〈〈id,a〉,m'〉 →
920  lookup tag A m id = Some A a ∧
921  lookup tag A m' id = None A ∧
922  (∀id'. id = id' ∨ lookup tag A m id' = lookup tag A m' id').
923#tag #A * #m * #id #a * #m' #C
924lapply (pm_choose_some A m id a m' ?)
925[ whd in C:(??%?); cases (pm_choose A m) in C ⊢ %; normalize [ #E destruct | * * #x #y #z #E destruct % ] ]
926* * * #L1 #L2 #L3 #_
927% [ % [ @L1 | @L2 ] | * #id' cases (L3 id') [ /2/ | #L4 %2 @L4 ] ]
928qed.
929
930lemma choose_some_subset : ∀tag,A,m,id,a,m'.
931  choose tag A m = Some ? 〈〈id,a〉,m'〉 →
932  m' ⊆ m.
933#tag #A #m #id #a #m' #C
934cases (choose_some … m' C) * #L1 #L2 #L3
935#id' whd in ⊢ (?% → ?%);
936cases (L3 id')
937[ #E destruct >L2 *
938| #L4 >L4 //
939] qed.
940
941lemma choose_some_card : ∀tag,A,m,id,a,m'.
942  choose tag A m = Some ? 〈〈id,a〉,m'〉 →
943  |m| = S (|m'|).
944#tag #A * #m * #id #a * #m' #C
945lapply (pm_choose_some A m id a m' ?)
946[ whd in C:(??%?); cases (pm_choose A m) in C ⊢ %; normalize [ #E destruct | * * #x #y #z #E destruct % ] ]
947* #_ #H @H
948qed.
949
950(* Remove an element from a map/set, returning the element and a new map/set. *)
951
952definition try_remove : ∀tag,A. identifier_map tag A → identifier tag → option (A × (identifier_map tag A)) ≝
953λtag,A,m,id.
954  match pm_try_remove A (match id with [ an_identifier id' ⇒ id']) (match m with [ an_id_map m' ⇒ m' ]) with
955  [ None ⇒ None ?
956  | Some x ⇒ Some ? 〈\fst x, an_id_map tag A (\snd x)〉
957  ].
958
959lemma try_remove_empty : ∀tag,A,m,id.
960  try_remove tag A m id = None ? ↔ lookup tag A m id = None ?.
961#tag #A * #m * #id lapply (pm_try_remove_none A id m) * #H1 #H2 %
962[ normalize #C @H1 cases (pm_try_remove A id m) in C ⊢ %; [ // | normalize #x #E destruct ]
963| normalize #L >H2 //
964] qed.
965
966lemma try_remove_some : ∀tag,A,m,id,a,m'.
967  try_remove tag A m id = Some ? 〈a,m'〉 →
968  lookup tag A m id = Some A a ∧
969  lookup tag A m' id = None A ∧
970  (∀id'. id = id' ∨ lookup tag A m id' = lookup tag A m' id').
971#tag #A * #m * #id #a * #m' #C
972lapply (pm_try_remove_some A id m a m' ?)
973[ whd in C:(??%?); cases (pm_try_remove A id m) in C ⊢ %; normalize [ #E destruct | * #x #y #E destruct % ] ]
974* * * #L1 #L2 #L3 #_
975% [ % [ @L1 | @L2 ] | * #id' cases (L3 id') [ /2/ | #L4 %2 @L4 ] ]
976qed.
977
978lemma try_remove_some_card : ∀tag,A,m,id,a,m'.
979  try_remove tag A m id = Some ? 〈a,m'〉 →
980  |m| = S (|m'|).
981#tag #A * #m * #id #a * #m' #C
982lapply (pm_try_remove_some A id m a m' ?)
983[ whd in C:(??%?); cases (pm_try_remove A id m) in C ⊢ %; normalize [ #E destruct | * #x #y #E destruct % ] ]
984* #_ #H @H
985qed.
986
987lemma try_remove_this : ∀tag,A,m,id,a.
988  lookup tag A m id = Some A a →
989  ∃m'. try_remove tag A m id = Some ? 〈a,m'〉.
990#tag #A * #m * #id #a #L
991cases (pm_try_remove_some' A id m a L)
992#m' #R %{(an_id_map tag A m')} whd in ⊢ (??%?); >R %
993qed.
994 
995(* Link a map with the set consisting of its domain. *)
996
997definition id_set_of_map : ∀tag,A. identifier_map tag A → identifier_set tag ≝
998λtag,A,m. an_id_map tag unit (map … (λ_. it) (match m with [ an_id_map m' ⇒ m'])).
999
1000lemma id_set_of_map_subset : ∀tag,A,m.
1001  id_set_of_map tag A m ⊆ m.
1002#tag #A * #m * #id normalize
1003>lookup_opt_map normalize cases (lookup_opt ???) //
1004qed.
1005
1006lemma id_set_of_map_present : ∀tag,A,m,id.
1007  present tag A m id ↔ present tag unit (id_set_of_map … m) id.
1008#tag #A * #m * #id %
1009normalize @not_to_not
1010>lookup_opt_map cases (lookup_opt ???) normalize //
1011#a #E destruct
1012qed.
1013
1014lemma id_set_of_map_card : ∀tag,A,m.
1015  |m| = |id_set_of_map tag A m|.
1016#tag #A * #m whd in ⊢ (??%%); >map_size //
1017qed.
1018
1019
1020
Note: See TracBrowser for help on using the repository browser.