source: src/common/Identifiers.ma @ 2305

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

RTLabs cost spec checking function implemented (lacks proof, or much
testing due to a bug elsewhere). Includes some new operations on
PostiveMaps? and identifier_maps/sets.

File size: 31.3 KB
RevLine 
[736]1include "basics/types.ma".
2include "ASM/String.ma".
[1515]3include "utilities/binary/positive.ma".
[1631]4include "utilities/lists.ma".
[1882]5include "utilities/extralib.ma".
[736]6include "common/Errors.ma".
7
[738]8(* identifiers and their generators are tagged to differentiate them, and to
[736]9   provide extra type checking. *)
10
[797]11(* in common/PreIdentifiers.ma, via Errors.ma.
[738]12inductive identifier (tag:String) : Type[0] ≝
[1515]13  an_identifier : Pos → identifier tag.
[797]14*)
15
[738]16record universe (tag:String) : Type[0] ≝
[1055]17{
[1515]18  next_identifier : Pos
[1055]19}.
[736]20
[738]21definition new_universe : ∀tag:String. universe tag ≝
[1515]22  λtag. mk_universe tag one.
[797]23
[1627]24let rec fresh (tag:String) (u:universe tag) on u : identifier tag × (universe tag) ≝
25  let id ≝ next_identifier ? u in
[1515]26  〈an_identifier tag id, mk_universe tag (succ id)〉.
[797]27
[1627]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'.
[1635]43#tag * #id * #id' * #u * #u' normalize #H #E destruct /2 by le_S/
[1627]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'.
[1635]50#tag * #id * #id' * #u * #u' normalize #H #E destruct % #E' destruct /2 by absurd/
[1627]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
[1635]62#H #E whd % [ @(fresh_is_fresh … E) | @(All_mp … H) * #id #a #H' /2 by fresh_remains_fresh/ ]
[1627]63qed.
64
[1070]65definition eq_identifier : ∀t. identifier t → identifier t → bool ≝
66  λt,l,r.
[757]67  match l with
68  [ an_identifier l' ⇒
69    match r with
70    [ an_identifier r' ⇒
[1515]71      eqb l' r'
[757]72    ]
73  ].
[1070]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
[1515]79change with (P (eqb ??))
[1635]80@(eqb_elim x y P) [ /2 by / | * #H @F % #E destruct /2 by / ]
[1070]81qed.
[1949]82
[2111]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
[1949]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
[757]120definition word_of_identifier ≝
121  λt.
122  λl: identifier t.
123  match l with   
124  [ an_identifier l' ⇒ l'
125  ].
126
[1515]127lemma eq_identifier_refl : ∀tag,id. eq_identifier tag id id = true.
[1516]128#tag * #id whd in ⊢ (??%?); >eqb_n_n @refl
[1515]129qed.
130
[1562]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
[1515]137lemma eq_identifier_false : ∀tag,x,y. x≠y → eq_identifier tag x y = false.
[1635]138#tag * #x * #y #NE normalize @not_eq_to_eqb_false /2 by not_to_not/
[1515]139qed.
140
[738]141definition identifier_eq : ∀tag:String. ∀x,y:identifier tag. (x=y) + (x≠y).
[1516]142#tag * #x * #y lapply (refl ? (eqb x y)) cases (eqb x y) in ⊢ (???% → %);
[736]143#E [ % | %2 ]
[1515]144lapply E @eqb_elim
[1635]145[ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2 by absurd/ ]
[736]146qed.
147
[738]148definition identifier_of_nat : ∀tag:String. nat → identifier tag ≝
[1515]149  λtag,n. an_identifier tag (succ_pos_of_nat  n).
[736]150
151
[1631]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[ //
[1635]163| * #id #a #tl #IH #r * #H1 #H2 % /2 by All_append_l/
[1631]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[ //
[1635]169| * #id #a #tl #IH #r * #H1 #H2 /2 by /
[1631]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
[736]201(* Maps from identifiers to arbitrary types. *)
202
[1515]203include "common/PositiveMap.ma".
[736]204
[753]205inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝
[1515]206  an_id_map : positive_map A → identifier_map tag A.
[753]207 
[738]208definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝
[1515]209  λtag,A. an_id_map tag A (pm_leaf A).
[736]210
[1515]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' ]).
[736]214
[1515]215definition lookup_def ≝
216λtag,A,m,l,d. match lookup tag A m l with [ None ⇒ d | Some x ⇒ x].
217
[2182]218definition member ≝
219  λtag,A.λm:identifier_map tag A.λl:identifier tag.
[1515]220  match lookup tag A m l with [ None ⇒ false | _ ⇒ true ].
221
[2182]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
[761]236(* Always adds the identifier to the map. *)
[1515]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' ])).
[736]240
[1070]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
[1562]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
[1070]253lemma lookup_add_miss : ∀tag,A,m,i,j,a.
[1515]254  i ≠ j →
[1070]255  lookup tag A (add tag A m j a) i = lookup tag A m i.
[1515]256#tag #A * #m * #i * #j #a #H
[1635]257@lookup_opt_insert_miss /2 by not_to_not/
[1070]258qed.
259
[1562]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
[1070]264lemma lookup_add_oblivious : ∀tag,A,m,i,j,a.
[1072]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
[1515]268cases (identifier_eq ? i j)
269[ #E >E >lookup_add_hit % #N destruct
270| #NE >lookup_add_miss //
[1070]271] qed.
272
[1316]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 % //
[1635]279| #NE >lookup_add_miss /2 by or_intror, sym_not_eq/
[1316]280] qed.
281
[1058]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.
[1515]285  fold ?? (λl,a,el. 〈an_identifier tag l, a〉::el)
286          (match m with [ an_id_map m' ⇒ m' ]) [ ].
[1058]287
[2303]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
[797]317axiom MissingId : String.
318
[761]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.
[1515]322  match update A (match l with [ an_identifier l' ⇒ l' ]) a
323                 (match m with [ an_id_map m' ⇒ m' ]) with
[797]324  [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *)
[761]325  | Some m' ⇒ OK ? (an_id_map tag A m')
326  ].
[736]327
[1092]328definition foldi:
[1068]329  ∀A, B: Type[0].
330  ∀tag: String.
[1092]331  (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝
332λA,B,tag,f,m,b.
333  match m with
[1515]334  [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ].
[1068]335
[1316]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
[1515]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
[1316]349definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝
350λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ].
351cases H #H'  cases (H' (refl ??)) qed.
352
[1535]353lemma lookup_lookup_present : ∀tag,A,m,id,p.
354  lookup tag A m id = Some ? (lookup_present tag A m id p).
355#tag #A #m #id #p
356whd in p ⊢ (???(??%));
357cases (lookup tag A m id) in p ⊢ %;
358[ * #H @⊥ @H @refl
359| #a #H @refl
360] qed.
361
[2303]362lemma lookup_is_present : ∀tag,T,m,id,t.
363  lookup tag T m id = Some T t →
364  present ?? m id.
365#tag #T #m #id #t #L normalize >L % #E destruct
366qed.
367
368lemma lookup_present_eq : ∀tag,T,m,id,t.
369  lookup tag T m id = Some T t →
370  ∀H. lookup_present tag T m id H = t.
371#tag #T #m #id #t #L #H
372lapply (lookup_lookup_present … H) >L #E destruct %
373qed.
374
375
[1316]376definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝
377λtag,A,m,l,p,a.
378  let l' ≝ match l with [ an_identifier l' ⇒ l' ] in
379  let m' ≝ match m with [ an_id_map m' ⇒ m' ] in
[1515]380  let u' ≝ update A l' a m' in
381  match u' return λx. update ???? = x → ? with
[1316]382  [ None ⇒ λE.⊥
383  | Some m' ⇒ λ_. an_id_map tag A m'
384  ] (refl ? u').
[1515]385cases l in p E; cases m; -l' -m' #m' #l'
[1516]386whd in ⊢ (% → ?);
387 whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?);
[1316]388#NL #U cases NL #H @H @(update_fail … U)
389qed.
390
391lemma update_still_present : ∀tag,A,m,id,a,id'.
392  ∀H:present tag A m id.
393  ∀H':present tag A m id'.
394  present tag A (update_present tag A m id' H' a) id.
395#tag #A * #m * #id #a * #id' #H #H'
[1516]396whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta
[1316]397cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id'))
[1516]398[ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U)
[1316]399  % #E' destruct
[1516]400| #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?));
[1515]401  <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ]
[1316]402] qed.
403
[1928]404lemma lookup_present_add_hit:
405  ∀tag, A, map, k, v, k_pres.
406    lookup_present tag A (add … map k v) k k_pres = v.
407  #tag #a #map #k #v #k_pres
408  lapply (lookup_lookup_present … (add … map k v) … k_pres)
409  >lookup_add_hit #Some_assm destruct(Some_assm)
410  <e0 %
411qed.
[1627]412
[1928]413lemma lookup_present_add_miss:
414  ∀tag, A, map, k, k', v, k_pres', k_pres''.
415    k' ≠ k →
416      lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''.
417  #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm
418  lapply (lookup_lookup_present … (add … map k v) ? k_pres')
419  >lookup_add_miss try assumption
420  #Some_assm
421  lapply (lookup_lookup_present … map k') >Some_assm #Some_assm'
422  lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption
423qed.
424
425lemma present_add_present:
426  ∀tag, a, map, k, k', v.
427    k' ≠ k →
428      present tag a (add tag a map k v) k' →
429        present tag a map k'.
430  #tag #a #map #k #k' #v #neq_hyp #present_hyp
431  whd in match present; normalize nodelta
432  whd in match present in present_hyp; normalize nodelta in present_hyp;
433  cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp
434  lapply (lookup_add_cases tag ?????? Some_eq_hyp) *
435  [1:
436    * #k_eq_hyp @⊥ /2/
437  |2:
438    #Some_eq_hyp' /2/
439  ]
440qed.
441
442lemma present_add_hit:
443  ∀tag, a, map, k, v.
444    present tag a (add tag a map k v) k.
445  #tag #a #map #k #v
446  whd >lookup_add_hit
447  % #absurd destruct
448qed.
449
450lemma present_add_miss:
451  ∀tag, a, map, k, k', v.
452    k' ≠ k → present tag a map k' → present tag a (add tag a map k v) k'.
453  #tag #a #map #k #k' #v #neq_assm #present_assm
454  whd >lookup_add_miss assumption
455qed.
456
457
[1627]458let rec fresh_for_map tag A (id:identifier tag) (m:identifier_map tag A) on id : Prop ≝
459  lookup … m id = None A.
460
[1631]461lemma fresh_for_empty_map : ∀tag,A,id.
462  fresh_for_map tag A id (empty_map tag A).
463#tag #A * #id //
464qed.
465
[1627]466definition fresh_map_for_univ ≝
467λtag,A. λm:identifier_map tag A. λu:universe tag.
468  ∀id. present tag A m id → fresh_for_univ tag id u.
469
470lemma fresh_fresh_for_map : ∀tag,A,m,id,u,u'.
471  fresh_map_for_univ tag A m u →
472  〈id,u'〉 = fresh tag u →
473  fresh_for_map tag A id m.
474#tag #A * #m * #id * #u * #u' whd in ⊢ (% → ???% → %);
475#FMU #E destruct lapply (FMU (an_identifier tag u)) whd in ⊢ ((% → %) → ?);
476generalize in ⊢ ((?(??%?) → ?) → ??%?); *
[1635]477[ // | #a #H @False_ind lapply (H ?) /2 by absurd/ % #E destruct
[1627]478qed.
479
480lemma fresh_map_preserved : ∀tag,A,m,u,u',id.
481  fresh_map_for_univ tag A m u →
482  〈id,u'〉 = fresh tag u →
483  fresh_map_for_univ tag A m u'.
484#tag #A #m #u * #u' #id whd in ⊢ (% → ? → %); #H #E
485#id' #PR @(fresh_remains_fresh … E) @H //
486qed.
487
488lemma fresh_map_add : ∀tag,A,m,u,id,a.
489  fresh_map_for_univ tag A m u →
490  fresh_for_univ tag id u →
491  fresh_map_for_univ tag A (add tag A m id a) u.
492#tag #A * #m #u #id #a #Hm #Hi
493#id' #PR cases (identifier_eq tag id' id)
494[ #E >E @Hi
495| #NE @Hm whd in PR;
496  change with (add tag A (an_id_map tag A m) id a) in PR:(?(??(???%?)?));
497  >lookup_add_miss in PR; //
498] qed.
499
500lemma present_not_fresh : ∀tag,A,m,id,id'.
501  present tag A m id →
502  fresh_for_map tag A id' m →
503  id ≠ id'.
504#tag #A #m #id * #id' whd in ⊢ (% → % → ?);
505* #NE #E % #E' destruct @(NE E)
506qed.
507
[1631]508lemma fresh_for_map_add : ∀tag,A,id,m,id',a.
509  id ≠ id' →
510  fresh_for_map tag A id m →
511  fresh_for_map tag A id (add tag A m id' a).
512#tag #A * #id #m #id' #a #NE #F
513whd >lookup_add_miss //
514qed.
515
516
[779]517(* Sets *)
518
[1882]519definition identifier_set ≝ λtag.identifier_map tag unit.
[779]520
[1882]521definition empty_set : ∀tag.identifier_set tag ≝ λtag.empty_map ….
[779]522
523
[1882]524definition add_set : ∀tag.identifier_set tag → identifier tag → identifier_set tag ≝
525  λtag,s,i.add … s i it.
526
[779]527definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝
528λtag,i. add_set tag (empty_set tag) i.
529
[1882]530let rec union_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_set tag ≝
531  an_id_map tag unit (merge … (λo,o'.match o with [Some _ ⇒ Some ? it | None ⇒ !_ o'; return it])
532    (match s with [ an_id_map s0 ⇒ s0 ])
533    (match s' with [ an_id_map s1 ⇒ s1 ])).
[779]534
[816]535
[1882]536(* set minus is generalised to maps *)
537let rec minus_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_map tag A ≝
538  an_id_map tag A (merge A B A (λo,o'.match o' with [None ⇒ o | Some _ ⇒ None ?])
539    (match s with [ an_id_map s0 ⇒ s0 ])
540    (match s' with [ an_id_map s1 ⇒ s1 ])).
541
[1908]542notation "a ∖ b" left associative with precedence 55 for @{'setminus $a $b}.
[1882]543
544interpretation "identifier set union" 'union a b = (union_set ??? a b).
[816]545notation "∅" non associative with precedence 90 for @{ 'empty }.
546interpretation "empty identifier set" 'empty = (empty_set ?).
547interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a).
[1882]548interpretation "identifier map difference" 'setminus a b = (minus_set ??? a b).
[1092]549
[1882]550definition IdentifierSet : String → Setoid ≝ λtag.
551  mk_Setoid (identifier_set tag) (λs,s'.∀i.i ∈ s = (i ∈ s')) ???.
552  // qed.
553
554unification hint 0 ≔ tag;
555S ≟ IdentifierSet tag
556(*-----------------------------*)⊢
557identifier_set tag ≡ std_supp S.
558unification hint 0 ≔ tag;
559S ≟ IdentifierSet tag
560(*-----------------------------*)⊢
561identifier_map tag unit ≡ std_supp S.
562
563lemma mem_set_add : ∀tag,A.∀i,j : identifier tag.∀s,x.
564  i ∈ add ? A s j x = (eq_identifier ? i j ∨ i ∈ s).
565#tag #A *#i *#j *#s #x normalize
566@(eqb_elim i j)
567[#EQ destruct
568  >(lookup_opt_insert_hit A x j)
569|#NEQ >(lookup_opt_insert_miss … s NEQ)
570] elim (lookup_opt  A j s) normalize // qed.
571
572lemma mem_set_add_id : ∀tag,A,i,s,x.bool_to_Prop (i ∈ add tag A s i x).
573#tag #A #i #s #x >mem_set_add
574@eq_identifier_elim [#_ %| #ABS elim (absurd … (refl ? i) ABS)] qed.
575
576lemma in_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i.
577  if i ∈ m then (∃s.lookup … m i = Some ? s) else (lookup … m i = None ?).
578#tag #A * #m * #i normalize
579elim (lookup_opt A i m) normalize
580[ % | #x %{x} % ]
581qed.
582
[1092]583lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s.
[1882]584#tag * normalize #m >map_opt_id_eq_ext // * %
[1092]585qed.
586
587lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s.
[1882]588#tag * * [//] *[2: *] #l#r normalize
589>map_opt_id_eq_ext [1,3: >map_opt_id_eq_ext [2,4: *] |*: *] //
590qed.
[1631]591
[1882]592lemma minus_empty_l : ∀tag,A.∀s:identifier_map tag A. ∅ ∖ s ≅ ∅.
593#tag #A * * [//] *[2:#x]#l#r * * normalize [1,4://]
594#p >lookup_opt_map elim (lookup_opt ???) normalize //
595qed.
596
597lemma minus_empty_r : ∀tag,A.∀s:identifier_map tag A. s ∖ ∅ = s.
598#tag #A * * [//] *[2:#x]#l#r normalize
599>map_opt_id >map_opt_id //
600qed.
601
602lemma mem_set_union : ∀tag.∀i : identifier tag.∀s,s' : identifier_set tag.
603  i ∈ (s ∪ s') = (i ∈ s ∨ i ∈ s').
604#tag * #i * #s * #s' normalize
605>lookup_opt_merge [2: @refl]
606elim (lookup_opt ???)
607elim (lookup_opt ???)
608normalize // qed.
609
610lemma mem_set_minus : ∀tag,A,B.∀i : identifier tag.∀s : identifier_map tag A.
611  ∀s' : identifier_map tag B.
612  i ∈ (s ∖ s') = (i ∈ s ∧ ¬ i ∈ s').
613#tag #A #B * #i * #s * #s' normalize
614>lookup_opt_merge [2: @refl]
615elim (lookup_opt ???)
616elim (lookup_opt ???)
617normalize // qed.
618
619lemma set_eq_ext_node : ∀tag.∀o,o',l,l',r,r'.
620  an_id_map tag ? (pm_node ? o l r) ≅ an_id_map … (pm_node ? o' l' r') →
621    o = o' ∧ an_id_map tag ? l ≅ an_id_map … l' ∧ an_id_map tag ? r ≅ an_id_map … r'.
622#tag#o#o'#l#l'#r#r'#H
623%[
624%[ lapply (H (an_identifier ? one))
625   elim o [2: *] elim o' [2,4: *] normalize // #EQ destruct
626 | *#p lapply (H (an_identifier ? (p0 p))) normalize //
627]| *#p lapply (H (an_identifier ? (p1 p))) normalize //
628]
629qed.
630
631lemma set_eq_ext_leaf : ∀tag,A.∀o,l,r.
632  (∀i.i∈an_id_map tag A (pm_node ? o l r) = false) →
633    o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false).
634#tag#A#o#l#r#H
635%[
636%[ lapply (H (an_identifier ? one))
637   elim o [2: #a] normalize // #EQ destruct
638 | *#p lapply (H (an_identifier ? (p0 p))) normalize //
639]| *#p lapply (H (an_identifier ? (p1 p))) normalize //
640]
641qed.
642
643
644definition id_map_size : ∀tag : String.∀A. identifier_map tag A → ℕ ≝
645  λtag,A,s.match s with [an_id_map p ⇒ |p|].
646
647interpretation "identifier map domain size" 'norm s = (id_map_size ?? s).
648
649lemma set_eq_ext_empty_to_card : ∀tag,A.∀s : identifier_map tag A. (∀i.i∈s = false) → |s| = 0.
650#tag#A * #s elim s [//]
651#o#l#r normalize in ⊢((?→%)→(?→%)→?); #Hil #Hir #H
652elim (set_eq_ext_leaf … H) * #EQ destruct #Hl #Hr normalize
653>(Hil Hl) >(Hir Hr) // qed.
654
655lemma set_eq_ext_to_card : ∀tag.∀s,s' : identifier_set tag. s ≅ s' → |s| = |s'|.
656#tag *#s elim s
657[** [//] #o#l#r #H
658  >(set_eq_ext_empty_to_card … (std_symm … H)) //
659| #o#l#r normalize in ⊢((?→?→??%?)→(?→?→??%?)→?);
660  #Hil #Hir **
661  [#H @(set_eq_ext_empty_to_card … H)]
662  #o'#l'#r' #H elim (set_eq_ext_node … H) * #EQ destruct(EQ) #Hl #Hr
663  normalize >(Hil ? Hl) >(Hir ? Hr) //
664] qed.
665
666lemma add_size: ∀tag,A,s,i,x.
667  |add tag A s i x| = (if i ∈ s then 0 else 1) + |s|.
668#tag #A *#s *#i #x
669lapply (insert_size ? i x s)
670lapply (refl ? (lookup_opt ? i s))
671generalize in ⊢ (???%→?); * [2: #x']
672normalize #EQ >EQ normalize //
673qed.
674
675lemma mem_set_O_lt_card : ∀tag,A.∀i.∀s : identifier_map tag A. i ∈ s → |s| > 0.
676#tag #A * #i * #s normalize #H
677@(lookup_opt_O_lt_size … i)
678% #EQ >EQ in H; normalize *
679qed.
680
681(* NB: no control on values if applied to maps *)
682definition set_subset ≝ λtag,A,B.λs : identifier_map tag A.
683  λs' : identifier_map tag B. ∀i.i ∈ s → (bool_to_Prop (i ∈ s')).
684
685interpretation "identifier set subset" 'subseteq s s' = (set_subset ??? s s').
686
687lemma add_subset :
688  ∀tag,A,B.∀i : identifier tag.∀x.∀s : identifier_map ? A.∀s' : identifier_map ? B.
689    i ∈ s' → s ⊆ s' → add … s i x ⊆ s'.
690#tag#A#B#i#x#s#s' #H #G #j
691>mem_set_add
692@eq_identifier_elim #H' [* >H' @H | #js @(G ? js)]
693qed.
694
695definition set_forall : ∀tag,A.(identifier tag → Prop) →
696  identifier_map tag A → Prop ≝ λtag,A,P,m.∀i. i ∈ m → P i.
697 
698lemma set_forall_add : ∀tag,P,m,i.set_forall tag ? P m → P i →
699  set_forall tag ? P (add_set ? m i).
700#tag#P#m#i#Pm#Pi#j
701>mem_set_add
702@eq_identifier_elim
703[#EQ destruct(EQ) #_ @Pi
704|#_ @Pm
705]
706qed.
707
708include "utilities/proper.ma".
709
710lemma minus_subset : ∀tag,A,B.minus_set tag A B ⊨ set_subset … ++> set_subset … -+> set_subset ….
711#tag#A#B#s#s' #H #s'' #s''' #G #i
712>mem_set_minus >mem_set_minus
713#H' elim (andb_Prop_true … H') -H' #is #nis''
714>(H … is)
715elim (true_or_false_Prop (i∈s'''))
716[ #is''' >(G … is''') in nis''; *
717| #nis''' >nis''' %
718]
719qed.
720
721lemma subset_node : ∀tag,A,B.∀o,o',l,l',r,r'.
722  an_id_map tag A (pm_node ? o l r) ⊆ an_id_map tag B (pm_node ? o' l' r') →
723    opt_All ? (λ_.o' ≠ None ?) o ∧ an_id_map tag ? l ⊆ an_id_map tag  ? l' ∧
724      an_id_map tag ? r ⊆ an_id_map tag ? r'.
725#tag#A#B#o#o'#l#l'#r#r'#H
726%[%
727  [ lapply (H (an_identifier ? (one))) elim o [2: #a] elim o' [2:#b]
728    normalize // [#_ % #ABS destruct(ABS) | #G lapply (G I) *]
729  | *#p lapply (H (an_identifier ? (p0 p)))
730  ]
731 | *#p lapply (H (an_identifier ? (p1 p)))
732] #H @H
733qed.
734
735lemma subset_leaf : ∀tag,A.∀o,l,r.
736  an_id_map tag A (pm_node ? o l r) ⊆ ∅ →
737    o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false).
738#tag#A#o#l#r#H
739%[
740%[ lapply (H (an_identifier ? one))
741   elim o [2: #a] normalize // #EQ lapply(EQ I) *
742 | *#p lapply (H (an_identifier ? (p0 p)))
743 ]
744|  *#p lapply (H (an_identifier ? (p1 p)))
745] normalize elim (lookup_opt ? p ?) normalize
746// #a #H lapply (H I) *
747qed.
748
749lemma subset_card : ∀tag,A,B.∀s : identifier_map tag A.∀s' : identifier_map tag B.
750  s ⊆ s' → |s| ≤ |s'|.
751#tag #A #B *#s elim s
752[ //
753| #o#l#r #Hil #Hir **
754  [ #H elim (subset_leaf … H) * #EQ >EQ #Hl #Hr
755    lapply (set_eq_ext_empty_to_card … Hl)
756    lapply (set_eq_ext_empty_to_card … Hr)
757    normalize //
758  | #o' #l' #r' #H elim (subset_node … H) *
759    elim o [2: #a] elim o' [2,4: #a']
760    [3: #G normalize in G; elim(absurd ? (refl ??) G)
761    |*: #_ #Hl #Hr lapply (Hil ? Hl) lapply (Hir ? Hr)
762      normalize #H1 #H2
763      [@le_S_S | @(transitive_le … (|l'|+|r'|)) [2: / by /]]
764      @le_plus assumption
765    ]
766  ]
767]
768qed.
769
[2182]770lemma mem_set_empty : ∀tag,A.∀i: identifier tag. i∈empty_map tag A = false.
771#tag #A * #i normalize %
[1882]772qed.
773
774lemma mem_set_singl_to_eq : ∀tag.∀i,j : identifier tag.i∈{(j)} → i = j.
775#tag
776#i #j >mem_set_add >mem_set_empty
777#H elim (orb_true_l … H) -H
778[@eq_identifier_elim [//] #_] #EQ destruct
779qed.
780
781lemma subset_add_set : ∀tag,i,s.s ⊆ add_set tag s i.
782#tag#i#s#j #H >mem_set_add >H
783>commutative_orb %
784qed.
785
786lemma add_set_monotonic : ∀tag,i,s,s'.s ⊆ s' → add_set tag s i ⊆ add_set tag s' i.
787#tag#i#s#s' #H #j >mem_set_add >mem_set_add
788@orb_elim elim (eq_identifier ???)
789whd lapply (H j) /2 by /
790qed.
791
792lemma transitive_subset : ∀tag,A.transitive ? (set_subset tag A A).
793#tag#A#s#s'#s''#H#G#i #is
794@(G … (H … is))
795qed.
796
797definition set_from_list : ∀tag.list (identifier tag) → identifier_map tag unit ≝
798  λtag.foldl … (add_set ?) ∅.
799
800coercion id_set_from_list : ∀tag.∀l : list (identifier tag).identifier_map tag unit ≝
801  set_from_list on _l : list (identifier ?) to identifier_map ? unit.
802
803lemma mem_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i.
804i∈m → lookup … m i ≠ None ?.
805#tag#A * #m #i
806whd in match (i∈?);
807elim (lookup ????) normalize [2: #x]
808* % #EQ destruct(EQ)
809qed.
810
811
812
813lemma mem_list_as_set : ∀tag.∀l : list (identifier tag).
814  ∀i.i ∈ l → In ? l i.
815#tag #l @(list_elim_left … l)
816[ #i *
817| #t #h #Hi  #i
818  whd in ⊢ (?(???%?)→?);
819  >foldl_append
820  whd in ⊢ (?(???%?)→?);
821  >mem_set_add
822  @eq_identifier_elim
823  [ #EQi destruct(EQi)
824    #_ @Exists_append_r % %
825  | #_ #H @Exists_append_l @Hi assumption
826  ]
827]
828qed.
829
830lemma list_as_set_mem : ∀tag.∀l : list (identifier tag).
831  ∀i.In ? l i → i ∈ l.
832#tag #l @(list_elim_left … l)
833[ #i *
834| #t #h #Hi #i #H
835  whd in ⊢ (?(???%?));
836  >foldl_append
837  whd in ⊢ (?(???%?));
838  elim (Exists_append … H) -H
839  [ #H >mem_set_add
840    @eq_identifier_elim [//] #_ normalize
841    @Hi @H
842  | * [2: *] #EQi destruct(EQi) >mem_set_add_id %
843  ]
844]
845qed.
846
847lemma list_as_set_All : ∀tag,P.∀ l : list (identifier tag).
848  (∀i.i ∈ l → P i) → All ? P l.
849#tag #P #l @(list_elim_left … l)
850[ #_ %
851| #x #l' #Hi
[2222]852  whd in match (set_from_list … (l'@[x]));
[1882]853  >foldl_append
854  #H @All_append
855  [ @Hi #i #G @H
856    whd in ⊢ (?(???%?));
857    >mem_set_add @orb_Prop_r @G
858  | % [2: %]
859    @H
860    whd in ⊢ (?(???%?));
861    @mem_set_add_id
862  ]
863]
864qed.
865
866lemma All_list_as_set : ∀tag,P.∀ l : list (identifier tag).
867  All ? P l → ∀i.i ∈ l → P i.
868#tag #P #l @(list_elim_left … l)
869[ * #i *
870| #x #l' #Hi #H
871  lapply (All_append_l … H)
872  lapply (All_append_r … H)
873  * #Px * #Pl' #i
[2222]874  whd in match (set_from_list … (l'@[x]));
[1882]875  >foldl_append
876  >mem_set_add
877  @eq_identifier_elim
878  [ #EQx >EQx #_ @Px
879  | #_ whd in match (?∨?); @Hi @Pl'
880  ]
881]
882qed. 
883
[2155]884lemma map_mem_prop :
885  ∀tag,A.∀m : identifier_map tag A.∀i.
886  lookup ?? m i ≠ None ? → i ∈ m.
887#p #globals #m #i
888lapply (in_map_domain … m i)
889cases (i∈m)
890[ * #x #_ #_ %
891| #EQ >EQ * #ABS @ABS %
[2305]892] qed.
893
894
895(* Attempt to choose an entry in the map/set, and if successful return the entry
896   and the map/set without it. *)
897
898definition choose : ∀tag,A. identifier_map tag A → option (identifier tag × A × (identifier_map tag A)) ≝
899λtag,A,m.
900  match pm_choose A (match m with [ an_id_map m' ⇒ m' ]) with
901  [ None ⇒ None ?
902  | Some x ⇒ Some ? 〈〈an_identifier tag (\fst (\fst x)), \snd (\fst x)〉, an_id_map tag A (\snd x)〉
903  ].
904
905lemma choose_empty : ∀tag,A,m.
906  choose tag A m = None ? ↔ ∀id. lookup tag A m id = None ?.
907#tag #A * #m lapply (pm_choose_empty A m) * #H1 #H2 %
908[ normalize #C * @H1 cases (pm_choose A m) in C ⊢ %; [ // | normalize #x #E destruct ]
909| normalize #L lapply (pm_choose_empty A m) cases (pm_choose A m)
910  [ * #H1 #H2 normalize // | #x * #_ #H lapply (H ?) [ #p @(L (an_identifier ? p)) | #E destruct ] ]
911] qed.
912
913lemma choose_some : ∀tag,A,m,id,a,m'.
914  choose tag A m = Some ? 〈〈id,a〉,m'〉 →
915  lookup tag A m id = Some A a ∧
916  lookup tag A m' id = None A ∧
917  (∀id'. id = id' ∨ lookup tag A m id' = lookup tag A m' id').
918#tag #A * #m * #id #a * #m' #C
919lapply (pm_choose_some A m id a m' ?)
920[ whd in C:(??%?); cases (pm_choose A m) in C ⊢ %; normalize [ #E destruct | * * #x #y #z #E destruct % ] ]
921* * * #L1 #L2 #L3 #_
922% [ % [ @L1 | @L2 ] | * #id' cases (L3 id') [ /2/ | #L4 %2 @L4 ] ]
923qed.
924
925lemma choose_some_subset : ∀tag,A,m,id,a,m'.
926  choose tag A m = Some ? 〈〈id,a〉,m'〉 →
927  m' ⊆ m.
928#tag #A #m #id #a #m' #C
929cases (choose_some … m' C) * #L1 #L2 #L3
930#id' whd in ⊢ (?% → ?%);
931cases (L3 id')
932[ #E destruct >L2 *
933| #L4 >L4 //
934] qed.
935
936lemma choose_some_card : ∀tag,A,m,id,a,m'.
937  choose tag A m = Some ? 〈〈id,a〉,m'〉 →
938  |m| = S (|m'|).
939#tag #A * #m * #id #a * #m' #C
940lapply (pm_choose_some A m id a m' ?)
941[ whd in C:(??%?); cases (pm_choose A m) in C ⊢ %; normalize [ #E destruct | * * #x #y #z #E destruct % ] ]
942* #_ #H @H
943qed.
944
945(* Remove an element from a map/set, returning the element and a new map/set. *)
946
947definition try_remove : ∀tag,A. identifier_map tag A → identifier tag → option (A × (identifier_map tag A)) ≝
948λtag,A,m,id.
949  match pm_try_remove A (match id with [ an_identifier id' ⇒ id']) (match m with [ an_id_map m' ⇒ m' ]) with
950  [ None ⇒ None ?
951  | Some x ⇒ Some ? 〈\fst x, an_id_map tag A (\snd x)〉
952  ].
953
954lemma try_remove_empty : ∀tag,A,m,id.
955  try_remove tag A m id = None ? ↔ lookup tag A m id = None ?.
956#tag #A * #m * #id lapply (pm_try_remove_none A id m) * #H1 #H2 %
957[ normalize #C @H1 cases (pm_try_remove A id m) in C ⊢ %; [ // | normalize #x #E destruct ]
958| normalize #L >H2 //
959] qed.
960
961lemma try_remove_some : ∀tag,A,m,id,a,m'.
962  try_remove tag A m id = Some ? 〈a,m'〉 →
963  lookup tag A m id = Some A a ∧
964  lookup tag A m' id = None A ∧
965  (∀id'. id = id' ∨ lookup tag A m id' = lookup tag A m' id').
966#tag #A * #m * #id #a * #m' #C
967lapply (pm_try_remove_some A id m a m' ?)
968[ whd in C:(??%?); cases (pm_try_remove A id m) in C ⊢ %; normalize [ #E destruct | * #x #y #E destruct % ] ]
969* * * #L1 #L2 #L3 #_
970% [ % [ @L1 | @L2 ] | * #id' cases (L3 id') [ /2/ | #L4 %2 @L4 ] ]
971qed.
972
973lemma try_remove_some_card : ∀tag,A,m,id,a,m'.
974  try_remove tag A m id = Some ? 〈a,m'〉 →
975  |m| = S (|m'|).
976#tag #A * #m * #id #a * #m' #C
977lapply (pm_try_remove_some A id m a m' ?)
978[ whd in C:(??%?); cases (pm_try_remove A id m) in C ⊢ %; normalize [ #E destruct | * #x #y #E destruct % ] ]
979* #_ #H @H
980qed.
981 
982
Note: See TracBrowser for help on using the repository browser.