source: src/common/Identifiers.ma @ 2111

Last change on this file since 2111 was 2111, checked in by sacerdot, 8 years ago

Cleanup: lemmas/theorems/axioms moved to the right places.

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