source: src/common/Identifiers.ma @ 1928

Last change on this file since 1928 was 1928, checked in by mulligan, 8 years ago

Moved code from in ASM/ASMCosts*.ma and ASM/CostsProof.ma that should rightfully be in another file. Added a new file, ASM/UtilBranch.ma for code that should rightfully be in ASM/Util.ma but is incomplete (i.e. daemons).

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