source: src/common/Identifiers.ma @ 1627

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

Add some notions of freshness, and start using them for temporary
generation (not yet complete).

File size: 11.3 KB
Line 
1include "basics/types.ma".
2include "ASM/String.ma".
3include "utilities/binary/positive.ma".
4include "common/Errors.ma".
5
6(* identifiers and their generators are tagged to differentiate them, and to
7   provide extra type checking. *)
8
9(* in common/PreIdentifiers.ma, via Errors.ma.
10inductive identifier (tag:String) : Type[0] ≝
11  an_identifier : Pos → identifier tag.
12*)
13
14record universe (tag:String) : Type[0] ≝
15{
16  next_identifier : Pos
17}.
18
19definition new_universe : ∀tag:String. universe tag ≝
20  λtag. mk_universe tag one.
21
22let rec fresh (tag:String) (u:universe tag) on u : identifier tag × (universe tag) ≝
23  let id ≝ next_identifier ? u in
24  〈an_identifier tag id, mk_universe tag (succ id)〉.
25
26
27let rec fresh_for_univ tag (id:identifier tag) (u:universe tag) on id : Prop ≝
28  match id with [ an_identifier p ⇒ p < next_identifier … u ].
29
30
31lemma fresh_is_fresh : ∀tag,id,u,u'.
32  〈id,u〉 = fresh tag u' →
33  fresh_for_univ tag id u.
34#tag * #id * #u * #u' #E whd in E:(???%); destruct //
35qed.
36
37lemma fresh_remains_fresh : ∀tag,id,id',u,u'.
38  fresh_for_univ tag id u →
39  〈id',u'〉 = fresh tag u →
40  fresh_for_univ tag id u'.
41#tag * #id * #id' * #u * #u' normalize #H #E destruct /2/
42qed.
43
44lemma fresh_distinct : ∀tag,id,id',u,u'.
45  fresh_for_univ tag id u →
46  〈id',u'〉 = fresh tag u →
47  id ≠ id'.
48#tag * #id * #id' * #u * #u' normalize #H #E destruct % #E' destruct /2/
49qed.
50
51
52let rec env_fresh_for_univ tag A (env:list (identifier tag × A)) (u:universe tag) on u : Prop ≝
53  All ? (λida. fresh_for_univ tag (\fst ida) u) env.
54
55lemma fresh_env_extend : ∀tag,A,env,u,u',id,a.
56  env_fresh_for_univ tag A env u →
57  〈id,u'〉 = fresh tag u →
58  env_fresh_for_univ tag A (〈id,a〉::env) u'.
59#tag #A #env * #u * #u' #id #a
60#H #E whd % [ @(fresh_is_fresh … E) | @(All_mp … H) * #id #a #H' /2/ ]
61qed.
62
63
64definition eq_identifier : ∀t. identifier t → identifier t → bool ≝
65  λt,l,r.
66  match l with
67  [ an_identifier l' ⇒
68    match r with
69    [ an_identifier r' ⇒
70      eqb l' r'
71    ]
72  ].
73
74lemma eq_identifier_elim : ∀P:bool → Type[0]. ∀t,x,y.
75  (x = y → P true) → (x ≠ y → P false) →
76  P (eq_identifier t x y).
77#P #t * #x * #y #T #F
78change with (P (eqb ??))
79@(eqb_elim x y P) [ /2/ | * #H @F % #E destruct /2/ ]
80qed.
81   
82definition word_of_identifier ≝
83  λt.
84  λl: identifier t.
85  match l with   
86  [ an_identifier l' ⇒ l'
87  ].
88
89lemma eq_identifier_refl : ∀tag,id. eq_identifier tag id id = true.
90#tag * #id whd in ⊢ (??%?); >eqb_n_n @refl
91qed.
92
93axiom eq_identifier_sym:
94  ∀tag: String.
95  ∀l  : identifier tag.
96  ∀r  : identifier tag.
97    eq_identifier tag l r = eq_identifier tag r l.
98
99lemma eq_identifier_false : ∀tag,x,y. x≠y → eq_identifier tag x y = false.
100#tag * #x * #y #NE normalize @not_eq_to_eqb_false /2/
101qed.
102
103definition identifier_eq : ∀tag:String. ∀x,y:identifier tag. (x=y) + (x≠y).
104#tag * #x * #y lapply (refl ? (eqb x y)) cases (eqb x y) in ⊢ (???% → %);
105#E [ % | %2 ]
106lapply E @eqb_elim
107[ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2/ ]
108qed.
109
110definition identifier_of_nat : ∀tag:String. nat → identifier tag ≝
111  λtag,n. an_identifier tag (succ_pos_of_nat  n).
112
113
114(* Maps from identifiers to arbitrary types. *)
115
116include "common/PositiveMap.ma".
117
118inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝
119  an_id_map : positive_map A → identifier_map tag A.
120 
121definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝
122  λtag,A. an_id_map tag A (pm_leaf A).
123
124let rec lookup tag A (m:identifier_map tag A) (l:identifier tag) on m : option A ≝
125  lookup_opt A (match l with [ an_identifier l' ⇒ l' ])
126               (match m with [ an_id_map m' ⇒ m' ]).
127
128definition lookup_def ≝
129λtag,A,m,l,d. match lookup tag A m l with [ None ⇒ d | Some x ⇒ x].
130
131let rec member tag A (m:identifier_map tag A) (l:identifier tag) on m : bool ≝
132  match lookup tag A m l with [ None ⇒ false | _ ⇒ true ].
133
134(* Always adds the identifier to the map. *)
135let rec add tag A (m:identifier_map tag A) (l:identifier tag) (a:A) on m : identifier_map tag A ≝
136  an_id_map tag A (insert A (match l with [ an_identifier l' ⇒ l' ]) a
137                            (match m with [ an_id_map m' ⇒ m' ])).
138
139lemma lookup_add_hit : ∀tag,A,m,i,a.
140  lookup tag A (add tag A m i a) i = Some ? a.
141#tag #A * #m * #i #a
142@lookup_opt_insert_hit
143qed.
144
145lemma lookup_def_add_hit : ∀tag,A,m,i,a,d.
146  lookup_def tag A (add tag A m i a) i d = a.
147#tag #A * #m * #i #a #d
148@lookup_insert_hit
149qed.
150
151lemma lookup_add_miss : ∀tag,A,m,i,j,a.
152  i ≠ j →
153  lookup tag A (add tag A m j a) i = lookup tag A m i.
154#tag #A * #m * #i * #j #a #H
155@lookup_opt_insert_miss /2/
156qed.
157
158axiom lookup_def_add_miss : ∀tag,A,m,i,j,a,d.
159  i ≠ j →
160  lookup_def tag A (add tag A m j a) i d = lookup_def tag A m i d.
161
162lemma lookup_add_oblivious : ∀tag,A,m,i,j,a.
163  (lookup tag A m i ≠ None ?) →
164  lookup tag A (add tag A m j a) i ≠ None ?.
165#tag #A #m #i #j #a #H
166cases (identifier_eq ? i j)
167[ #E >E >lookup_add_hit % #N destruct
168| #NE >lookup_add_miss //
169] qed.
170
171lemma lookup_add_cases : ∀tag,A,m,i,j,a,v.
172  lookup tag A (add tag A m i a) j = Some ? v →
173  (i=j ∧ v = a) ∨ lookup tag A m j = Some ? v.
174#tag #A #m #i #j #a #v
175cases (identifier_eq ? i j)
176[ #E >E >lookup_add_hit #H %1 destruct % //
177| #NE >lookup_add_miss /2/
178] qed.
179
180(* Extract every identifier, value pair from the map. *)
181definition elements : ∀tag,A. identifier_map tag A → list (identifier tag × A) ≝
182λtag,A,m.
183  fold ?? (λl,a,el. 〈an_identifier tag l, a〉::el)
184          (match m with [ an_id_map m' ⇒ m' ]) [ ].
185
186axiom MissingId : String.
187
188(* Only updates an existing entry; fails with an error otherwise. *)
189definition update : ∀tag,A. identifier_map tag A → identifier tag → A → res (identifier_map tag A) ≝
190λtag,A,m,l,a.
191  match update A (match l with [ an_identifier l' ⇒ l' ]) a
192                 (match m with [ an_id_map m' ⇒ m' ]) with
193  [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *)
194  | Some m' ⇒ OK ? (an_id_map tag A m')
195  ].
196
197definition foldi:
198  ∀A, B: Type[0].
199  ∀tag: String.
200  (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝
201λA,B,tag,f,m,b.
202  match m with
203  [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ].
204
205(* A predicate that an identifier is in a map, and a failure-avoiding lookup
206   and update using it. *)
207
208definition present : ∀tag,A. identifier_map tag A → identifier tag → Prop ≝
209λtag,A,m,i. lookup … m i ≠ None ?.
210
211lemma member_present : ∀tag,A,m,id.
212  member tag A m id = true → present tag A m id.
213#tag #A * #m #id normalize cases (lookup_opt A ??) normalize
214[ #E destruct
215| #x #E % #E' destruct
216] qed.
217
218include "ASM/Util.ma".
219
220definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝
221λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ].
222cases H #H'  cases (H' (refl ??)) qed.
223
224lemma lookup_lookup_present : ∀tag,A,m,id,p.
225  lookup tag A m id = Some ? (lookup_present tag A m id p).
226#tag #A #m #id #p
227whd in p ⊢ (???(??%));
228cases (lookup tag A m id) in p ⊢ %;
229[ * #H @⊥ @H @refl
230| #a #H @refl
231] qed.
232
233definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝
234λtag,A,m,l,p,a.
235  let l' ≝ match l with [ an_identifier l' ⇒ l' ] in
236  let m' ≝ match m with [ an_id_map m' ⇒ m' ] in
237  let u' ≝ update A l' a m' in
238  match u' return λx. update ???? = x → ? with
239  [ None ⇒ λE.⊥
240  | Some m' ⇒ λ_. an_id_map tag A m'
241  ] (refl ? u').
242cases l in p E; cases m; -l' -m' #m' #l'
243whd in ⊢ (% → ?);
244 whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?);
245#NL #U cases NL #H @H @(update_fail … U)
246qed.
247
248lemma update_still_present : ∀tag,A,m,id,a,id'.
249  ∀H:present tag A m id.
250  ∀H':present tag A m id'.
251  present tag A (update_present tag A m id' H' a) id.
252#tag #A * #m * #id #a * #id' #H #H'
253whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta
254cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id'))
255[ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U)
256  % #E' destruct
257| #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?));
258  <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ]
259] qed.
260
261
262let rec fresh_for_map tag A (id:identifier tag) (m:identifier_map tag A) on id : Prop ≝
263  lookup … m id = None A.
264
265definition fresh_map_for_univ ≝
266λtag,A. λm:identifier_map tag A. λu:universe tag.
267  ∀id. present tag A m id → fresh_for_univ tag id u.
268
269lemma fresh_fresh_for_map : ∀tag,A,m,id,u,u'.
270  fresh_map_for_univ tag A m u →
271  〈id,u'〉 = fresh tag u →
272  fresh_for_map tag A id m.
273#tag #A * #m * #id * #u * #u' whd in ⊢ (% → ???% → %);
274#FMU #E destruct lapply (FMU (an_identifier tag u)) whd in ⊢ ((% → %) → ?);
275generalize in ⊢ ((?(??%?) → ?) → ??%?); *
276[ // | #a #H @False_ind lapply (H ?) /2/ % #E destruct
277qed.
278
279lemma fresh_map_preserved : ∀tag,A,m,u,u',id.
280  fresh_map_for_univ tag A m u →
281  〈id,u'〉 = fresh tag u →
282  fresh_map_for_univ tag A m u'.
283#tag #A #m #u * #u' #id whd in ⊢ (% → ? → %); #H #E
284#id' #PR @(fresh_remains_fresh … E) @H //
285qed.
286
287lemma fresh_map_add : ∀tag,A,m,u,id,a.
288  fresh_map_for_univ tag A m u →
289  fresh_for_univ tag id u →
290  fresh_map_for_univ tag A (add tag A m id a) u.
291#tag #A * #m #u #id #a #Hm #Hi
292#id' #PR cases (identifier_eq tag id' id)
293[ #E >E @Hi
294| #NE @Hm whd in PR;
295  change with (add tag A (an_id_map tag A m) id a) in PR:(?(??(???%?)?));
296  >lookup_add_miss in PR; //
297] qed.
298
299lemma present_not_fresh : ∀tag,A,m,id,id'.
300  present tag A m id →
301  fresh_for_map tag A id' m →
302  id ≠ id'.
303#tag #A #m #id * #id' whd in ⊢ (% → % → ?);
304* #NE #E % #E' destruct @(NE E)
305qed.
306
307(* Sets *)
308
309inductive identifier_set (tag:String) : Type[0] ≝
310  an_id_set : positive_map unit → identifier_set tag.
311
312definition empty_set : ∀tag:String. identifier_set tag ≝
313λtag. an_id_set tag (pm_leaf unit).
314
315let rec add_set (tag:String) (s:identifier_set tag) (i:identifier tag) on s : identifier_set tag ≝
316  an_id_set tag (insert unit (match i with [ an_identifier i' ⇒ i' ])
317                          it (match s with [ an_id_set s' ⇒ s' ])).
318
319definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝
320λtag,i. add_set tag (empty_set tag) i.
321
322let rec mem_set (tag:String) (s:identifier_set tag) (i:identifier tag) on s : bool ≝
323  match lookup_opt ? (match i with [ an_identifier i' ⇒ i' ])
324                     (match s with [ an_id_set s' ⇒ s' ]) with
325  [ None ⇒ false
326  | Some _ ⇒ true
327  ].
328
329let rec union_set (tag:String) (s:identifier_set tag) (s':identifier_set tag) on s : identifier_set tag ≝
330  an_id_set tag (merge unit (match s with [ an_id_set s0 ⇒ s0 ])
331                            (match s' with [ an_id_set s1 ⇒ s1 ])).
332
333interpretation "identifier set union" 'union a b = (union_set ? a b).
334notation "∅" non associative with precedence 90 for @{ 'empty }.
335interpretation "empty identifier set" 'empty = (empty_set ?).
336interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a).
337interpretation "identifier set membership" 'mem a b = (mem_set ? b a).
338
339lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s.
340#tag * //
341qed.
342
343lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s.
344#tag * * // qed.
Note: See TracBrowser for help on using the repository browser.