1 | include "basics/types.ma". |
---|
2 | include "ASM/String.ma". |
---|
3 | include "utilities/binary/positive.ma". |
---|
4 | include "common/Errors.ma". |
---|
5 | include "utilities/option.ma". |
---|
6 | |
---|
7 | (* identifiers and their generators are tagged to differentiate them, and to |
---|
8 | provide extra type checking. *) |
---|
9 | |
---|
10 | (* in common/PreIdentifiers.ma, via Errors.ma. |
---|
11 | inductive identifier (tag:String) : Type[0] ≝ |
---|
12 | an_identifier : Pos → identifier tag. |
---|
13 | *) |
---|
14 | |
---|
15 | record universe (tag:String) : Type[0] ≝ |
---|
16 | { |
---|
17 | next_identifier : Pos |
---|
18 | }. |
---|
19 | |
---|
20 | definition new_universe : ∀tag:String. universe tag ≝ |
---|
21 | λtag. mk_universe tag one. |
---|
22 | |
---|
23 | (* Fresh identifier generation uses delayed overflow checking. To make sure |
---|
24 | that the identifiers really were fresh, use the check_universe_ok function |
---|
25 | below afterwards. *) |
---|
26 | definition fresh : ∀tag:String. universe tag → identifier tag × (universe tag) ≝ |
---|
27 | λtag. |
---|
28 | λuniv: universe tag. |
---|
29 | let id ≝ next_identifier ? univ in |
---|
30 | 〈an_identifier tag id, mk_universe tag (succ id)〉. |
---|
31 | |
---|
32 | definition eq_identifier : ∀t. identifier t → identifier t → bool ≝ |
---|
33 | λt,l,r. |
---|
34 | match l with |
---|
35 | [ an_identifier l' ⇒ |
---|
36 | match r with |
---|
37 | [ an_identifier r' ⇒ |
---|
38 | eqb l' r' |
---|
39 | ] |
---|
40 | ]. |
---|
41 | |
---|
42 | lemma eq_identifier_elim : ∀P:bool → Type[0]. ∀t,x,y. |
---|
43 | (x = y → P true) → (x ≠ y → P false) → |
---|
44 | P (eq_identifier t x y). |
---|
45 | #P #t * #x * #y #T #F |
---|
46 | change with (P (eqb ??)) |
---|
47 | @(eqb_elim x y P) [ /2/ | * #H @F % #E destruct /2/ ] |
---|
48 | qed. |
---|
49 | |
---|
50 | definition word_of_identifier ≝ |
---|
51 | λt. |
---|
52 | λl: identifier t. |
---|
53 | match l with |
---|
54 | [ an_identifier l' ⇒ l' |
---|
55 | ]. |
---|
56 | |
---|
57 | lemma eq_identifier_refl : ∀tag,id. eq_identifier tag id id = true. |
---|
58 | #tag * #id whd in ⊢ (??%?); >eqb_n_n @refl |
---|
59 | qed. |
---|
60 | |
---|
61 | lemma eq_identifier_false : ∀tag,x,y. x≠y → eq_identifier tag x y = false. |
---|
62 | #tag * #x * #y #NE normalize @not_eq_to_eqb_false /2/ |
---|
63 | qed. |
---|
64 | |
---|
65 | definition identifier_eq : ∀tag:String. ∀x,y:identifier tag. (x=y) + (x≠y). |
---|
66 | #tag * #x * #y lapply (refl ? (eqb x y)) cases (eqb x y) in ⊢ (???% → %); |
---|
67 | #E [ % | %2 ] |
---|
68 | lapply E @eqb_elim |
---|
69 | [ #H #_ >H @refl | 2,3: #_ #H destruct | #H #_ % #H' destruct /2/ ] |
---|
70 | qed. |
---|
71 | |
---|
72 | definition identifier_of_nat : ∀tag:String. nat → identifier tag ≝ |
---|
73 | λtag,n. an_identifier tag (succ_pos_of_nat n). |
---|
74 | |
---|
75 | |
---|
76 | (* Maps from identifiers to arbitrary types. *) |
---|
77 | |
---|
78 | include "common/PositiveMap.ma". |
---|
79 | |
---|
80 | inductive identifier_map (tag:String) (A:Type[0]) : Type[0] ≝ |
---|
81 | an_id_map : positive_map A → identifier_map tag A. |
---|
82 | |
---|
83 | definition empty_map : ∀tag:String. ∀A. identifier_map tag A ≝ |
---|
84 | λtag,A. an_id_map tag A (pm_leaf A). |
---|
85 | |
---|
86 | let rec lookup tag A (m:identifier_map tag A) (l:identifier tag) on m : option A ≝ |
---|
87 | lookup_opt A (match l with [ an_identifier l' ⇒ l' ]) |
---|
88 | (match m with [ an_id_map m' ⇒ m' ]). |
---|
89 | |
---|
90 | definition lookup_def ≝ |
---|
91 | λtag,A,m,l,d. match lookup tag A m l with [ None ⇒ d | Some x ⇒ x]. |
---|
92 | |
---|
93 | let rec member tag A (m:identifier_map tag A) (l:identifier tag) on m : bool ≝ |
---|
94 | match lookup tag A m l with [ None ⇒ false | _ ⇒ true ]. |
---|
95 | |
---|
96 | (* Always adds the identifier to the map. *) |
---|
97 | let rec add tag A (m:identifier_map tag A) (l:identifier tag) (a:A) on m : identifier_map tag A ≝ |
---|
98 | an_id_map tag A (insert A (match l with [ an_identifier l' ⇒ l' ]) a |
---|
99 | (match m with [ an_id_map m' ⇒ m' ])). |
---|
100 | |
---|
101 | lemma lookup_add_hit : ∀tag,A,m,i,a. |
---|
102 | lookup tag A (add tag A m i a) i = Some ? a. |
---|
103 | #tag #A * #m * #i #a |
---|
104 | @lookup_opt_insert_hit |
---|
105 | qed. |
---|
106 | |
---|
107 | lemma lookup_add_miss : ∀tag,A,m,i,j,a. |
---|
108 | i ≠ j → |
---|
109 | lookup tag A (add tag A m j a) i = lookup tag A m i. |
---|
110 | #tag #A * #m * #i * #j #a #H |
---|
111 | @lookup_opt_insert_miss /2/ |
---|
112 | qed. |
---|
113 | |
---|
114 | lemma lookup_add_oblivious : ∀tag,A,m,i,j,a. |
---|
115 | (lookup tag A m i ≠ None ?) → |
---|
116 | lookup tag A (add tag A m j a) i ≠ None ?. |
---|
117 | #tag #A #m #i #j #a #H |
---|
118 | cases (identifier_eq ? i j) |
---|
119 | [ #E >E >lookup_add_hit % #N destruct |
---|
120 | | #NE >lookup_add_miss // |
---|
121 | ] qed. |
---|
122 | |
---|
123 | lemma lookup_add_cases : ∀tag,A,m,i,j,a,v. |
---|
124 | lookup tag A (add tag A m i a) j = Some ? v → |
---|
125 | (i=j ∧ v = a) ∨ lookup tag A m j = Some ? v. |
---|
126 | #tag #A #m #i #j #a #v |
---|
127 | cases (identifier_eq ? i j) |
---|
128 | [ #E >E >lookup_add_hit #H %1 destruct % // |
---|
129 | | #NE >lookup_add_miss /2/ |
---|
130 | ] qed. |
---|
131 | |
---|
132 | (* Extract every identifier, value pair from the map. *) |
---|
133 | definition elements : ∀tag,A. identifier_map tag A → list (identifier tag × A) ≝ |
---|
134 | λtag,A,m. |
---|
135 | fold ?? (λl,a,el. 〈an_identifier tag l, a〉::el) |
---|
136 | (match m with [ an_id_map m' ⇒ m' ]) [ ]. |
---|
137 | |
---|
138 | axiom MissingId : String. |
---|
139 | |
---|
140 | (* Only updates an existing entry; fails with an error otherwise. *) |
---|
141 | definition update : ∀tag,A. identifier_map tag A → identifier tag → A → res (identifier_map tag A) ≝ |
---|
142 | λtag,A,m,l,a. |
---|
143 | match update A (match l with [ an_identifier l' ⇒ l' ]) a |
---|
144 | (match m with [ an_id_map m' ⇒ m' ]) with |
---|
145 | [ None ⇒ Error ? ([MSG MissingId; CTX tag l]) (* missing identifier *) |
---|
146 | | Some m' ⇒ OK ? (an_id_map tag A m') |
---|
147 | ]. |
---|
148 | |
---|
149 | definition foldi: |
---|
150 | ∀A, B: Type[0]. |
---|
151 | ∀tag: String. |
---|
152 | (identifier tag -> A -> B -> B) -> identifier_map tag A -> B -> B ≝ |
---|
153 | λA,B,tag,f,m,b. |
---|
154 | match m with |
---|
155 | [ an_id_map m' ⇒ fold A B (λbv. f (an_identifier ? bv)) m' b ]. |
---|
156 | |
---|
157 | (* A predicate that an identifier is in a map, and a failure-avoiding lookup |
---|
158 | and update using it. *) |
---|
159 | |
---|
160 | definition present : ∀tag,A. identifier_map tag A → identifier tag → Prop ≝ |
---|
161 | λtag,A,m,i. lookup … m i ≠ None ?. |
---|
162 | |
---|
163 | lemma member_present : ∀tag,A,m,id. |
---|
164 | member tag A m id = true → present tag A m id. |
---|
165 | #tag #A * #m #id normalize cases (lookup_opt A ??) normalize |
---|
166 | [ #E destruct |
---|
167 | | #x #E % #E' destruct |
---|
168 | ] qed. |
---|
169 | |
---|
170 | include "ASM/Util.ma". |
---|
171 | |
---|
172 | definition lookup_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A ≝ |
---|
173 | λtag,A,m,id. match lookup ?? m id return λx. x ≠ None ? → ? with [ Some a ⇒ λ_. a | None ⇒ λH.⊥ ]. |
---|
174 | cases H #H' cases (H' (refl ??)) qed. |
---|
175 | |
---|
176 | lemma lookup_lookup_present : ∀tag,A,m,id,p. |
---|
177 | lookup tag A m id = Some ? (lookup_present tag A m id p). |
---|
178 | #tag #A #m #id #p |
---|
179 | whd in p ⊢ (???(??%)); |
---|
180 | cases (lookup tag A m id) in p ⊢ %; |
---|
181 | [ * #H @⊥ @H @refl |
---|
182 | | #a #H @refl |
---|
183 | ] qed. |
---|
184 | |
---|
185 | definition update_present : ∀tag,A. ∀m:identifier_map tag A. ∀id. present ?? m id → A → identifier_map tag A ≝ |
---|
186 | λtag,A,m,l,p,a. |
---|
187 | let l' ≝ match l with [ an_identifier l' ⇒ l' ] in |
---|
188 | let m' ≝ match m with [ an_id_map m' ⇒ m' ] in |
---|
189 | let u' ≝ update A l' a m' in |
---|
190 | match u' return λx. update ???? = x → ? with |
---|
191 | [ None ⇒ λE.⊥ |
---|
192 | | Some m' ⇒ λ_. an_id_map tag A m' |
---|
193 | ] (refl ? u'). |
---|
194 | cases l in p E; cases m; -l' -m' #m' #l' |
---|
195 | whd in ⊢ (% → ?); |
---|
196 | whd in ⊢ (?(??(???%%)?) → ??(??%?%)? → ?); |
---|
197 | #NL #U cases NL #H @H @(update_fail … U) |
---|
198 | qed. |
---|
199 | |
---|
200 | lemma update_still_present : ∀tag,A,m,id,a,id'. |
---|
201 | ∀H:present tag A m id. |
---|
202 | ∀H':present tag A m id'. |
---|
203 | present tag A (update_present tag A m id' H' a) id. |
---|
204 | #tag #A * #m * #id #a * #id' #H #H' |
---|
205 | whd whd in ⊢ (?(??(???(%??????)?)?)); normalize nodelta |
---|
206 | cases (identifier_eq ? (an_identifier tag id) (an_identifier tag id')) |
---|
207 | [ #E >E @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); >(update_lookup_opt_same ????? U) |
---|
208 | % #E' destruct |
---|
209 | | #NE @refute_none_by_refl #m' #U whd in ⊢ (?(??%?)); whd in ⊢ (?(??(??%%)?)); |
---|
210 | <(update_lookup_opt_other ????? U id) [ @H | % #E cases NE >E #H @H @refl ] |
---|
211 | ] qed. |
---|
212 | |
---|
213 | (* Sets *) |
---|
214 | |
---|
215 | inductive identifier_set (tag:String) : Type[0] ≝ |
---|
216 | an_id_set : positive_map unit → identifier_set tag. |
---|
217 | |
---|
218 | definition empty_set : ∀tag:String. identifier_set tag ≝ |
---|
219 | λtag. an_id_set tag (pm_leaf unit). |
---|
220 | |
---|
221 | let rec add_set (tag:String) (s:identifier_set tag) (i:identifier tag) on s : identifier_set tag ≝ |
---|
222 | an_id_set tag (insert unit (match i with [ an_identifier i' ⇒ i' ]) |
---|
223 | it (match s with [ an_id_set s' ⇒ s' ])). |
---|
224 | |
---|
225 | definition singleton_set : ∀tag:String. identifier tag → identifier_set tag ≝ |
---|
226 | λtag,i. add_set tag (empty_set tag) i. |
---|
227 | |
---|
228 | let rec mem_set (tag:String) (s:identifier_set tag) (i:identifier tag) on s : bool ≝ |
---|
229 | match lookup_opt ? (match i with [ an_identifier i' ⇒ i' ]) |
---|
230 | (match s with [ an_id_set s' ⇒ s' ]) with |
---|
231 | [ None ⇒ false |
---|
232 | | Some _ ⇒ true |
---|
233 | ]. |
---|
234 | |
---|
235 | let rec union_set (tag:String) (s:identifier_set tag) (s':identifier_set tag) on s : identifier_set tag ≝ |
---|
236 | an_id_set tag (merge unit (match s with [ an_id_set s0 ⇒ s0 ]) |
---|
237 | (match s' with [ an_id_set s1 ⇒ s1 ])). |
---|
238 | |
---|
239 | interpretation "identifier set union" 'union a b = (union_set ? a b). |
---|
240 | notation "∅" non associative with precedence 90 for @{ 'empty }. |
---|
241 | interpretation "empty identifier set" 'empty = (empty_set ?). |
---|
242 | interpretation "singleton identifier set" 'singl a = (add_set ? (empty_set ?) a). |
---|
243 | interpretation "identifier set membership" 'mem a b = (mem_set ? b a). |
---|
244 | |
---|
245 | lemma union_empty_l : ∀tag.∀s:identifier_set tag. ∅ ∪ s = s. |
---|
246 | #tag * // |
---|
247 | qed. |
---|
248 | |
---|
249 | lemma union_empty_r : ∀tag.∀s:identifier_set tag. s ∪ ∅ = s. |
---|
250 | #tag * * // qed. |
---|