source: src/common/Identifiers.ma @ 2182

Last change on this file since 2182 was 2182, checked in by tranquil, 8 years ago

updated linearisation pass

File size: 26.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
288axiom MissingId : String.
289
290(* Only updates an existing entry; fails with an error otherwise. *)
291definition update : ∀tag,A. identifier_map tag A → identifier tag → A → res (identifier_map tag A) ≝
292λtag,A,m,l,a.
293  match update A (match l with [ an_identifier l' ⇒ l' ]) a
294                 (match m with [ an_id_map m' ⇒ m' ]) with
295  [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *)
296  | Some m' ⇒ OK ? (an_id_map tag A m')
297  ].
298
299definition foldi:
300  ∀A, B: Type[0].
301  ∀tag: String.
302  (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝
303λA,B,tag,f,m,b.
304  match m with
305  [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ].
306
307(* A predicate that an identifier is in a map, and a failure-avoiding lookup
308   and update using it. *)
309
310definition present : ∀tag,A. identifier_map tag A → identifier tag → Prop ≝
311λtag,A,m,i. lookup … m i ≠ None ?.
312
313lemma member_present : ∀tag,A,m,id.
314  member tag A m id = true → present tag A m id.
315#tag #A * #m #id normalize cases (lookup_opt A ??) normalize
316[ #E destruct
317| #x #E % #E' destruct
318] qed.
319
320include "ASM/Util.ma".
321
322definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝
323λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ].
324cases H #H'  cases (H' (refl ??)) qed.
325
326lemma lookup_lookup_present : ∀tag,A,m,id,p.
327  lookup tag A m id = Some ? (lookup_present tag A m id p).
328#tag #A #m #id #p
329whd in p ⊢ (???(??%));
330cases (lookup tag A m id) in p ⊢ %;
331[ * #H @⊥ @H @refl
332| #a #H @refl
333] qed.
334
335definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝
336λtag,A,m,l,p,a.
337  let l' ≝ match l with [ an_identifier l' ⇒ l' ] in
338  let m' ≝ match m with [ an_id_map m' ⇒ m' ] in
339  let u' ≝ update A l' a m' in
340  match u' return λx. update ???? = x → ? with
341  [ None ⇒ λE.⊥
342  | Some m' ⇒ λ_. an_id_map tag A m'
343  ] (refl ? u').
344cases l in p E; cases m; -l' -m' #m' #l'
345whd in ⊢ (% → ?);
346 whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?);
347#NL #U cases NL #H @H @(update_fail … U)
348qed.
349
350lemma update_still_present : ∀tag,A,m,id,a,id'.
351  ∀H:present tag A m id.
352  ∀H':present tag A m id'.
353  present tag A (update_present tag A m id' H' a) id.
354#tag #A * #m * #id #a * #id' #H #H'
355whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta
356cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id'))
357[ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U)
358  % #E' destruct
359| #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?));
360  <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ]
361] qed.
362
363lemma lookup_present_add_hit:
364  ∀tag, A, map, k, v, k_pres.
365    lookup_present tag A (add … map k v) k k_pres = v.
366  #tag #a #map #k #v #k_pres
367  lapply (lookup_lookup_present … (add … map k v) … k_pres)
368  >lookup_add_hit #Some_assm destruct(Some_assm)
369  <e0 %
370qed.
371
372lemma lookup_present_add_miss:
373  ∀tag, A, map, k, k', v, k_pres', k_pres''.
374    k' ≠ k →
375      lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''.
376  #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm
377  lapply (lookup_lookup_present … (add … map k v) ? k_pres')
378  >lookup_add_miss try assumption
379  #Some_assm
380  lapply (lookup_lookup_present … map k') >Some_assm #Some_assm'
381  lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption
382qed.
383
384lemma present_add_present:
385  ∀tag, a, map, k, k', v.
386    k' ≠ k →
387      present tag a (add tag a map k v) k' →
388        present tag a map k'.
389  #tag #a #map #k #k' #v #neq_hyp #present_hyp
390  whd in match present; normalize nodelta
391  whd in match present in present_hyp; normalize nodelta in present_hyp;
392  cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp
393  lapply (lookup_add_cases tag ?????? Some_eq_hyp) *
394  [1:
395    * #k_eq_hyp @⊥ /2/
396  |2:
397    #Some_eq_hyp' /2/
398  ]
399qed.
400
401lemma present_add_hit:
402  ∀tag, a, map, k, v.
403    present tag a (add tag a map k v) k.
404  #tag #a #map #k #v
405  whd >lookup_add_hit
406  % #absurd destruct
407qed.
408
409lemma present_add_miss:
410  ∀tag, a, map, k, k', v.
411    k' ≠ k → present tag a map k' → present tag a (add tag a map k v) k'.
412  #tag #a #map #k #k' #v #neq_assm #present_assm
413  whd >lookup_add_miss assumption
414qed.
415
416
417let rec fresh_for_map tag A (id:identifier tag) (m:identifier_map tag A) on id : Prop ≝
418  lookup … m id = None A.
419
420lemma fresh_for_empty_map : ∀tag,A,id.
421  fresh_for_map tag A id (empty_map tag A).
422#tag #A * #id //
423qed.
424
425definition fresh_map_for_univ ≝
426λtag,A. λm:identifier_map tag A. λu:universe tag.
427  ∀id. present tag A m id → fresh_for_univ tag id u.
428
429lemma fresh_fresh_for_map : ∀tag,A,m,id,u,u'.
430  fresh_map_for_univ tag A m u →
431  〈id,u'〉 = fresh tag u →
432  fresh_for_map tag A id m.
433#tag #A * #m * #id * #u * #u' whd in ⊢ (% → ???% → %);
434#FMU #E destruct lapply (FMU (an_identifier tag u)) whd in ⊢ ((% → %) → ?);
435generalize in ⊢ ((?(??%?) → ?) → ??%?); *
436[ // | #a #H @False_ind lapply (H ?) /2 by absurd/ % #E destruct
437qed.
438
439lemma fresh_map_preserved : ∀tag,A,m,u,u',id.
440  fresh_map_for_univ tag A m u →
441  〈id,u'〉 = fresh tag u →
442  fresh_map_for_univ tag A m u'.
443#tag #A #m #u * #u' #id whd in ⊢ (% → ? → %); #H #E
444#id' #PR @(fresh_remains_fresh … E) @H //
445qed.
446
447lemma fresh_map_add : ∀tag,A,m,u,id,a.
448  fresh_map_for_univ tag A m u →
449  fresh_for_univ tag id u →
450  fresh_map_for_univ tag A (add tag A m id a) u.
451#tag #A * #m #u #id #a #Hm #Hi
452#id' #PR cases (identifier_eq tag id' id)
453[ #E >E @Hi
454| #NE @Hm whd in PR;
455  change with (add tag A (an_id_map tag A m) id a) in PR:(?(??(???%?)?));
456  >lookup_add_miss in PR; //
457] qed.
458
459lemma present_not_fresh : ∀tag,A,m,id,id'.
460  present tag A m id →
461  fresh_for_map tag A id' m →
462  id ≠ id'.
463#tag #A #m #id * #id' whd in ⊢ (% → % → ?);
464* #NE #E % #E' destruct @(NE E)
465qed.
466
467lemma fresh_for_map_add : ∀tag,A,id,m,id',a.
468  id ≠ id' →
469  fresh_for_map tag A id m →
470  fresh_for_map tag A id (add tag A m id' a).
471#tag #A * #id #m #id' #a #NE #F
472whd >lookup_add_miss //
473qed.
474
475
476(* Sets *)
477
478definition identifier_set ≝ λtag.identifier_map tag unit.
479
480definition empty_set : ∀tag.identifier_set tag ≝ λtag.empty_map ….
481
482
483definition add_set : ∀tag.identifier_set tag → identifier tag → identifier_set tag ≝
484  λtag,s,i.add … s i it.
485
486definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝
487λtag,i. add_set tag (empty_set tag) i.
488
489let rec union_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_set tag ≝
490  an_id_map tag unit (merge … (λo,o'.match o with [Some _ ⇒ Some ? it | None ⇒ !_ o'; return it])
491    (match s with [ an_id_map s0 ⇒ s0 ])
492    (match s' with [ an_id_map s1 ⇒ s1 ])).
493
494
495(* set minus is generalised to maps *)
496let rec minus_set (tag:String) A B (s:identifier_map tag A) (s':identifier_map tag B) on s : identifier_map tag A ≝
497  an_id_map tag A (merge A B A (λo,o'.match o' with [None ⇒ o | Some _ ⇒ None ?])
498    (match s with [ an_id_map s0 ⇒ s0 ])
499    (match s' with [ an_id_map s1 ⇒ s1 ])).
500
501notation "a ∖ b" left associative with precedence 55 for @{'setminus $a $b}.
502
503interpretation "identifier set union" 'union a b = (union_set ??? a b).
504notation "∅" non associative with precedence 90 for @{ 'empty }.
505interpretation "empty identifier set" 'empty = (empty_set ?).
506interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a).
507interpretation "identifier map difference" 'setminus a b = (minus_set ??? a b).
508
509definition IdentifierSet : String → Setoid ≝ λtag.
510  mk_Setoid (identifier_set tag) (λs,s'.∀i.i ∈ s = (i ∈ s')) ???.
511  // qed.
512
513unification hint 0 ≔ tag;
514S ≟ IdentifierSet tag
515(*-----------------------------*)⊢
516identifier_set tag ≡ std_supp S.
517unification hint 0 ≔ tag;
518S ≟ IdentifierSet tag
519(*-----------------------------*)⊢
520identifier_map tag unit ≡ std_supp S.
521
522lemma mem_set_add : ∀tag,A.∀i,j : identifier tag.∀s,x.
523  i ∈ add ? A s j x = (eq_identifier ? i j ∨ i ∈ s).
524#tag #A *#i *#j *#s #x normalize
525@(eqb_elim i j)
526[#EQ destruct
527  >(lookup_opt_insert_hit A x j)
528|#NEQ >(lookup_opt_insert_miss … s NEQ)
529] elim (lookup_opt  A j s) normalize // qed.
530
531lemma mem_set_add_id : ∀tag,A,i,s,x.bool_to_Prop (i ∈ add tag A s i x).
532#tag #A #i #s #x >mem_set_add
533@eq_identifier_elim [#_ %| #ABS elim (absurd … (refl ? i) ABS)] qed.
534
535lemma in_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i.
536  if i ∈ m then (∃s.lookup … m i = Some ? s) else (lookup … m i = None ?).
537#tag #A * #m * #i normalize
538elim (lookup_opt A i m) normalize
539[ % | #x %{x} % ]
540qed.
541
542lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s.
543#tag * normalize #m >map_opt_id_eq_ext // * %
544qed.
545
546lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s.
547#tag * * [//] *[2: *] #l#r normalize
548>map_opt_id_eq_ext [1,3: >map_opt_id_eq_ext [2,4: *] |*: *] //
549qed.
550
551lemma minus_empty_l : ∀tag,A.∀s:identifier_map tag A. ∅ ∖ s ≅ ∅.
552#tag #A * * [//] *[2:#x]#l#r * * normalize [1,4://]
553#p >lookup_opt_map elim (lookup_opt ???) normalize //
554qed.
555
556lemma minus_empty_r : ∀tag,A.∀s:identifier_map tag A. s ∖ ∅ = s.
557#tag #A * * [//] *[2:#x]#l#r normalize
558>map_opt_id >map_opt_id //
559qed.
560
561lemma mem_set_union : ∀tag.∀i : identifier tag.∀s,s' : identifier_set tag.
562  i ∈ (s ∪ s') = (i ∈ s ∨ i ∈ s').
563#tag * #i * #s * #s' normalize
564>lookup_opt_merge [2: @refl]
565elim (lookup_opt ???)
566elim (lookup_opt ???)
567normalize // qed.
568
569lemma mem_set_minus : ∀tag,A,B.∀i : identifier tag.∀s : identifier_map tag A.
570  ∀s' : identifier_map tag B.
571  i ∈ (s ∖ s') = (i ∈ s ∧ ¬ i ∈ s').
572#tag #A #B * #i * #s * #s' normalize
573>lookup_opt_merge [2: @refl]
574elim (lookup_opt ???)
575elim (lookup_opt ???)
576normalize // qed.
577
578lemma set_eq_ext_node : ∀tag.∀o,o',l,l',r,r'.
579  an_id_map tag ? (pm_node ? o l r) ≅ an_id_map … (pm_node ? o' l' r') →
580    o = o' ∧ an_id_map tag ? l ≅ an_id_map … l' ∧ an_id_map tag ? r ≅ an_id_map … r'.
581#tag#o#o'#l#l'#r#r'#H
582%[
583%[ lapply (H (an_identifier ? one))
584   elim o [2: *] elim o' [2,4: *] normalize // #EQ destruct
585 | *#p lapply (H (an_identifier ? (p0 p))) normalize //
586]| *#p lapply (H (an_identifier ? (p1 p))) normalize //
587]
588qed.
589
590lemma set_eq_ext_leaf : ∀tag,A.∀o,l,r.
591  (∀i.i∈an_id_map tag A (pm_node ? o l r) = false) →
592    o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false).
593#tag#A#o#l#r#H
594%[
595%[ lapply (H (an_identifier ? one))
596   elim o [2: #a] normalize // #EQ destruct
597 | *#p lapply (H (an_identifier ? (p0 p))) normalize //
598]| *#p lapply (H (an_identifier ? (p1 p))) normalize //
599]
600qed.
601
602
603definition id_map_size : ∀tag : String.∀A. identifier_map tag A → ℕ ≝
604  λtag,A,s.match s with [an_id_map p ⇒ |p|].
605
606interpretation "identifier map domain size" 'norm s = (id_map_size ?? s).
607
608lemma set_eq_ext_empty_to_card : ∀tag,A.∀s : identifier_map tag A. (∀i.i∈s = false) → |s| = 0.
609#tag#A * #s elim s [//]
610#o#l#r normalize in ⊢((?→%)→(?→%)→?); #Hil #Hir #H
611elim (set_eq_ext_leaf … H) * #EQ destruct #Hl #Hr normalize
612>(Hil Hl) >(Hir Hr) // qed.
613
614lemma set_eq_ext_to_card : ∀tag.∀s,s' : identifier_set tag. s ≅ s' → |s| = |s'|.
615#tag *#s elim s
616[** [//] #o#l#r #H
617  >(set_eq_ext_empty_to_card … (std_symm … H)) //
618| #o#l#r normalize in ⊢((?→?→??%?)→(?→?→??%?)→?);
619  #Hil #Hir **
620  [#H @(set_eq_ext_empty_to_card … H)]
621  #o'#l'#r' #H elim (set_eq_ext_node … H) * #EQ destruct(EQ) #Hl #Hr
622  normalize >(Hil ? Hl) >(Hir ? Hr) //
623] qed.
624
625lemma add_size: ∀tag,A,s,i,x.
626  |add tag A s i x| = (if i ∈ s then 0 else 1) + |s|.
627#tag #A *#s *#i #x
628lapply (insert_size ? i x s)
629lapply (refl ? (lookup_opt ? i s))
630generalize in ⊢ (???%→?); * [2: #x']
631normalize #EQ >EQ normalize //
632qed.
633
634lemma mem_set_O_lt_card : ∀tag,A.∀i.∀s : identifier_map tag A. i ∈ s → |s| > 0.
635#tag #A * #i * #s normalize #H
636@(lookup_opt_O_lt_size … i)
637% #EQ >EQ in H; normalize *
638qed.
639
640(* NB: no control on values if applied to maps *)
641definition set_subset ≝ λtag,A,B.λs : identifier_map tag A.
642  λs' : identifier_map tag B. ∀i.i ∈ s → (bool_to_Prop (i ∈ s')).
643
644interpretation "identifier set subset" 'subseteq s s' = (set_subset ??? s s').
645
646lemma add_subset :
647  ∀tag,A,B.∀i : identifier tag.∀x.∀s : identifier_map ? A.∀s' : identifier_map ? B.
648    i ∈ s' → s ⊆ s' → add … s i x ⊆ s'.
649#tag#A#B#i#x#s#s' #H #G #j
650>mem_set_add
651@eq_identifier_elim #H' [* >H' @H | #js @(G ? js)]
652qed.
653
654definition set_forall : ∀tag,A.(identifier tag → Prop) →
655  identifier_map tag A → Prop ≝ λtag,A,P,m.∀i. i ∈ m → P i.
656 
657lemma set_forall_add : ∀tag,P,m,i.set_forall tag ? P m → P i →
658  set_forall tag ? P (add_set ? m i).
659#tag#P#m#i#Pm#Pi#j
660>mem_set_add
661@eq_identifier_elim
662[#EQ destruct(EQ) #_ @Pi
663|#_ @Pm
664]
665qed.
666
667include "utilities/proper.ma".
668
669lemma minus_subset : ∀tag,A,B.minus_set tag A B ⊨ set_subset … ++> set_subset … -+> set_subset ….
670#tag#A#B#s#s' #H #s'' #s''' #G #i
671>mem_set_minus >mem_set_minus
672#H' elim (andb_Prop_true … H') -H' #is #nis''
673>(H … is)
674elim (true_or_false_Prop (i∈s'''))
675[ #is''' >(G … is''') in nis''; *
676| #nis''' >nis''' %
677]
678qed.
679
680lemma subset_node : ∀tag,A,B.∀o,o',l,l',r,r'.
681  an_id_map tag A (pm_node ? o l r) ⊆ an_id_map tag B (pm_node ? o' l' r') →
682    opt_All ? (λ_.o' ≠ None ?) o ∧ an_id_map tag ? l ⊆ an_id_map tag  ? l' ∧
683      an_id_map tag ? r ⊆ an_id_map tag ? r'.
684#tag#A#B#o#o'#l#l'#r#r'#H
685%[%
686  [ lapply (H (an_identifier ? (one))) elim o [2: #a] elim o' [2:#b]
687    normalize // [#_ % #ABS destruct(ABS) | #G lapply (G I) *]
688  | *#p lapply (H (an_identifier ? (p0 p)))
689  ]
690 | *#p lapply (H (an_identifier ? (p1 p)))
691] #H @H
692qed.
693
694lemma subset_leaf : ∀tag,A.∀o,l,r.
695  an_id_map tag A (pm_node ? o l r) ⊆ ∅ →
696    o = None ? ∧ (∀i.i∈an_id_map tag ? l = false) ∧ (∀i.i∈an_id_map tag ? r = false).
697#tag#A#o#l#r#H
698%[
699%[ lapply (H (an_identifier ? one))
700   elim o [2: #a] normalize // #EQ lapply(EQ I) *
701 | *#p lapply (H (an_identifier ? (p0 p)))
702 ]
703|  *#p lapply (H (an_identifier ? (p1 p)))
704] normalize elim (lookup_opt ? p ?) normalize
705// #a #H lapply (H I) *
706qed.
707
708lemma subset_card : ∀tag,A,B.∀s : identifier_map tag A.∀s' : identifier_map tag B.
709  s ⊆ s' → |s| ≤ |s'|.
710#tag #A #B *#s elim s
711[ //
712| #o#l#r #Hil #Hir **
713  [ #H elim (subset_leaf … H) * #EQ >EQ #Hl #Hr
714    lapply (set_eq_ext_empty_to_card … Hl)
715    lapply (set_eq_ext_empty_to_card … Hr)
716    normalize //
717  | #o' #l' #r' #H elim (subset_node … H) *
718    elim o [2: #a] elim o' [2,4: #a']
719    [3: #G normalize in G; elim(absurd ? (refl ??) G)
720    |*: #_ #Hl #Hr lapply (Hil ? Hl) lapply (Hir ? Hr)
721      normalize #H1 #H2
722      [@le_S_S | @(transitive_le … (|l'|+|r'|)) [2: / by /]]
723      @le_plus assumption
724    ]
725  ]
726]
727qed.
728
729lemma mem_set_empty : ∀tag,A.∀i: identifier tag. i∈empty_map tag A = false.
730#tag #A * #i normalize %
731qed.
732
733lemma mem_set_singl_to_eq : ∀tag.∀i,j : identifier tag.i∈{(j)} → i = j.
734#tag
735#i #j >mem_set_add >mem_set_empty
736#H elim (orb_true_l … H) -H
737[@eq_identifier_elim [//] #_] #EQ destruct
738qed.
739
740lemma subset_add_set : ∀tag,i,s.s ⊆ add_set tag s i.
741#tag#i#s#j #H >mem_set_add >H
742>commutative_orb %
743qed.
744
745lemma add_set_monotonic : ∀tag,i,s,s'.s ⊆ s' → add_set tag s i ⊆ add_set tag s' i.
746#tag#i#s#s' #H #j >mem_set_add >mem_set_add
747@orb_elim elim (eq_identifier ???)
748whd lapply (H j) /2 by /
749qed.
750
751lemma transitive_subset : ∀tag,A.transitive ? (set_subset tag A A).
752#tag#A#s#s'#s''#H#G#i #is
753@(G … (H … is))
754qed.
755
756definition set_from_list : ∀tag.list (identifier tag) → identifier_map tag unit ≝
757  λtag.foldl … (add_set ?) ∅.
758
759coercion id_set_from_list : ∀tag.∀l : list (identifier tag).identifier_map tag unit ≝
760  set_from_list on _l : list (identifier ?) to identifier_map ? unit.
761
762lemma mem_map_domain : ∀tag,A.∀m : identifier_map tag A.∀i.
763i∈m → lookup … m i ≠ None ?.
764#tag#A * #m #i
765whd in match (i∈?);
766elim (lookup ????) normalize [2: #x]
767* % #EQ destruct(EQ)
768qed.
769
770
771
772lemma mem_list_as_set : ∀tag.∀l : list (identifier tag).
773  ∀i.i ∈ l → In ? l i.
774#tag #l @(list_elim_left … l)
775[ #i *
776| #t #h #Hi  #i
777  whd in ⊢ (?(???%?)→?);
778  >foldl_append
779  whd in ⊢ (?(???%?)→?);
780  >mem_set_add
781  @eq_identifier_elim
782  [ #EQi destruct(EQi)
783    #_ @Exists_append_r % %
784  | #_ #H @Exists_append_l @Hi assumption
785  ]
786]
787qed.
788
789lemma list_as_set_mem : ∀tag.∀l : list (identifier tag).
790  ∀i.In ? l i → i ∈ l.
791#tag #l @(list_elim_left … l)
792[ #i *
793| #t #h #Hi #i #H
794  whd in ⊢ (?(???%?));
795  >foldl_append
796  whd in ⊢ (?(???%?));
797  elim (Exists_append … H) -H
798  [ #H >mem_set_add
799    @eq_identifier_elim [//] #_ normalize
800    @Hi @H
801  | * [2: *] #EQi destruct(EQi) >mem_set_add_id %
802  ]
803]
804qed.
805
806lemma list_as_set_All : ∀tag,P.∀ l : list (identifier tag).
807  (∀i.i ∈ l → P i) → All ? P l.
808#tag #P #l @(list_elim_left … l)
809[ #_ %
810| #x #l' #Hi
811  whd in match (l'@[x] : identifier_map tag unit);
812  >foldl_append
813  #H @All_append
814  [ @Hi #i #G @H
815    whd in ⊢ (?(???%?));
816    >mem_set_add @orb_Prop_r @G
817  | % [2: %]
818    @H
819    whd in ⊢ (?(???%?));
820    @mem_set_add_id
821  ]
822]
823qed.
824
825lemma All_list_as_set : ∀tag,P.∀ l : list (identifier tag).
826  All ? P l → ∀i.i ∈ l → P i.
827#tag #P #l @(list_elim_left … l)
828[ * #i *
829| #x #l' #Hi #H
830  lapply (All_append_l … H)
831  lapply (All_append_r … H)
832  * #Px * #Pl' #i
833  whd in match (l'@[x] : identifier_map ??);
834  >foldl_append
835  >mem_set_add
836  @eq_identifier_elim
837  [ #EQx >EQx #_ @Px
838  | #_ whd in match (?∨?); @Hi @Pl'
839  ]
840]
841qed. 
842
843lemma map_mem_prop :
844  ∀tag,A.∀m : identifier_map tag A.∀i.
845  lookup ?? m i ≠ None ? → i ∈ m.
846#p #globals #m #i
847lapply (in_map_domain … m i)
848cases (i∈m)
849[ * #x #_ #_ %
850| #EQ >EQ * #ABS @ABS %
851] qed.
852
853
854
Note: See TracBrowser for help on using the repository browser.