author | webertj |
Fri, 16 May 2003 16:35:36 +0200 | |
changeset 14033 | bc723de8ec95 |
parent 14027 | 68d247b7b14b |
child 14100 | 804be4c4b642 |
permissions | -rw-r--r-- |
3981 | 1 |
(* Title: HOL/Map.thy |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow, based on a theory by David von Oheimb |
|
13908 | 4 |
Copyright 1997-2003 TU Muenchen |
3981 | 5 |
|
6 |
The datatype of `maps' (written ~=>); strongly resembles maps in VDM. |
|
7 |
*) |
|
8 |
||
13914 | 9 |
header {* Maps *} |
10 |
||
13908 | 11 |
theory Map = List: |
3981 | 12 |
|
13908 | 13 |
types ('a,'b) "~=>" = "'a => 'b option" (infixr 0) |
3981 | 14 |
|
15 |
consts |
|
5300 | 16 |
chg_map :: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)" |
14025 | 17 |
map_add:: "('a ~=> 'b) => ('a ~=> 'b) => ('a ~=> 'b)" (infixl "++" 100) |
5300 | 18 |
dom :: "('a ~=> 'b) => 'a set" |
19 |
ran :: "('a ~=> 'b) => 'b set" |
|
20 |
map_of :: "('a * 'b)list => 'a ~=> 'b" |
|
21 |
map_upds:: "('a ~=> 'b) => 'a list => 'b list => |
|
13910 | 22 |
('a ~=> 'b)" ("_/'(_[|->]_/')" [900,0,0]900) |
23 |
map_le :: "('a ~=> 'b) => ('a ~=> 'b) => bool" (infix "\<subseteq>\<^sub>m" 50) |
|
24 |
||
5300 | 25 |
syntax |
13890 | 26 |
empty :: "'a ~=> 'b" |
5300 | 27 |
map_upd :: "('a ~=> 'b) => 'a => 'b => ('a ~=> 'b)" |
13910 | 28 |
("_/'(_/|->_')" [900,0,0]900) |
3981 | 29 |
|
12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10137
diff
changeset
|
30 |
syntax (xsymbols) |
13908 | 31 |
"~=>" :: "[type, type] => type" (infixr "\<leadsto>" 0) |
5300 | 32 |
map_upd :: "('a ~=> 'b) => 'a => 'b => ('a ~=> 'b)" |
13908 | 33 |
("_/'(_/\<mapsto>/_')" [900,0,0]900) |
5300 | 34 |
map_upds :: "('a ~=> 'b) => 'a list => 'b list => ('a ~=> 'b)" |
13908 | 35 |
("_/'(_/[\<mapsto>]/_')" [900,0,0]900) |
5300 | 36 |
|
37 |
translations |
|
13890 | 38 |
"empty" => "_K None" |
39 |
"empty" <= "%x. None" |
|
5300 | 40 |
|
41 |
"m(a|->b)" == "m(a:=Some b)" |
|
3981 | 42 |
|
43 |
defs |
|
13908 | 44 |
chg_map_def: "chg_map f a m == case m a of None => m | Some b => m(a|->f b)" |
3981 | 45 |
|
14025 | 46 |
map_add_def: "m1++m2 == %x. case m2 x of None => m1 x | Some y => Some y" |
47 |
||
48 |
map_upds_def: "m(xs [|->] ys) == m ++ map_of (rev(zip xs ys))" |
|
3981 | 49 |
|
13908 | 50 |
dom_def: "dom(m) == {a. m a ~= None}" |
14025 | 51 |
ran_def: "ran(m) == {b. EX a. m a = Some b}" |
3981 | 52 |
|
13910 | 53 |
map_le_def: "m1 \<subseteq>\<^sub>m m2 == ALL a : dom m1. m1 a = m2 a" |
54 |
||
5183 | 55 |
primrec |
56 |
"map_of [] = empty" |
|
5300 | 57 |
"map_of (p#ps) = (map_of ps)(fst p |-> snd p)" |
58 |
||
13908 | 59 |
|
13937 | 60 |
subsection {* empty *} |
13908 | 61 |
|
13910 | 62 |
lemma empty_upd_none[simp]: "empty(x := None) = empty" |
13908 | 63 |
apply (rule ext) |
64 |
apply (simp (no_asm)) |
|
65 |
done |
|
13910 | 66 |
|
13908 | 67 |
|
68 |
(* FIXME: what is this sum_case nonsense?? *) |
|
13910 | 69 |
lemma sum_case_empty_empty[simp]: "sum_case empty empty = empty" |
13908 | 70 |
apply (rule ext) |
71 |
apply (simp (no_asm) split add: sum.split) |
|
72 |
done |
|
73 |
||
13937 | 74 |
subsection {* map\_upd *} |
13908 | 75 |
|
76 |
lemma map_upd_triv: "t k = Some x ==> t(k|->x) = t" |
|
77 |
apply (rule ext) |
|
78 |
apply (simp (no_asm_simp)) |
|
79 |
done |
|
80 |
||
13910 | 81 |
lemma map_upd_nonempty[simp]: "t(k|->x) ~= empty" |
13908 | 82 |
apply safe |
83 |
apply (drule_tac x = "k" in fun_cong) |
|
84 |
apply (simp (no_asm_use)) |
|
85 |
done |
|
86 |
||
87 |
lemma finite_range_updI: "finite (range f) ==> finite (range (f(a|->b)))" |
|
88 |
apply (unfold image_def) |
|
89 |
apply (simp (no_asm_use) add: full_SetCompr_eq) |
|
90 |
apply (rule finite_subset) |
|
91 |
prefer 2 apply (assumption) |
|
92 |
apply auto |
|
93 |
done |
|
94 |
||
95 |
||
96 |
(* FIXME: what is this sum_case nonsense?? *) |
|
13937 | 97 |
subsection {* sum\_case and empty/map\_upd *} |
13908 | 98 |
|
13910 | 99 |
lemma sum_case_map_upd_empty[simp]: |
100 |
"sum_case (m(k|->y)) empty = (sum_case m empty)(Inl k|->y)" |
|
13908 | 101 |
apply (rule ext) |
102 |
apply (simp (no_asm) split add: sum.split) |
|
103 |
done |
|
104 |
||
13910 | 105 |
lemma sum_case_empty_map_upd[simp]: |
106 |
"sum_case empty (m(k|->y)) = (sum_case empty m)(Inr k|->y)" |
|
13908 | 107 |
apply (rule ext) |
108 |
apply (simp (no_asm) split add: sum.split) |
|
109 |
done |
|
110 |
||
13910 | 111 |
lemma sum_case_map_upd_map_upd[simp]: |
112 |
"sum_case (m1(k1|->y1)) (m2(k2|->y2)) = (sum_case (m1(k1|->y1)) m2)(Inr k2|->y2)" |
|
13908 | 113 |
apply (rule ext) |
114 |
apply (simp (no_asm) split add: sum.split) |
|
115 |
done |
|
116 |
||
117 |
||
13937 | 118 |
subsection {* chg\_map *} |
13908 | 119 |
|
13910 | 120 |
lemma chg_map_new[simp]: "m a = None ==> chg_map f a m = m" |
13908 | 121 |
apply (unfold chg_map_def) |
122 |
apply auto |
|
123 |
done |
|
124 |
||
13910 | 125 |
lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)" |
13908 | 126 |
apply (unfold chg_map_def) |
127 |
apply auto |
|
128 |
done |
|
129 |
||
130 |
||
13937 | 131 |
subsection {* map\_of *} |
13908 | 132 |
|
133 |
lemma map_of_SomeD [rule_format (no_asm)]: "map_of xs k = Some y --> (k,y):set xs" |
|
134 |
apply (induct_tac "xs") |
|
135 |
apply auto |
|
136 |
done |
|
137 |
||
138 |
lemma map_of_mapk_SomeI [rule_format (no_asm)]: "inj f ==> map_of t k = Some x --> |
|
139 |
map_of (map (split (%k. Pair (f k))) t) (f k) = Some x" |
|
140 |
apply (induct_tac "t") |
|
141 |
apply (auto simp add: inj_eq) |
|
142 |
done |
|
143 |
||
144 |
lemma weak_map_of_SomeI [rule_format (no_asm)]: "(k, x) : set l --> (? x. map_of l k = Some x)" |
|
145 |
apply (induct_tac "l") |
|
146 |
apply auto |
|
147 |
done |
|
148 |
||
149 |
lemma map_of_filter_in: |
|
150 |
"[| map_of xs k = Some z; P k z |] ==> map_of (filter (split P) xs) k = Some z" |
|
151 |
apply (rule mp) |
|
152 |
prefer 2 apply (assumption) |
|
153 |
apply (erule thin_rl) |
|
154 |
apply (induct_tac "xs") |
|
155 |
apply auto |
|
156 |
done |
|
157 |
||
158 |
lemma finite_range_map_of: "finite (range (map_of l))" |
|
159 |
apply (induct_tac "l") |
|
160 |
apply (simp_all (no_asm) add: image_constant) |
|
161 |
apply (rule finite_subset) |
|
162 |
prefer 2 apply (assumption) |
|
163 |
apply auto |
|
164 |
done |
|
165 |
||
166 |
lemma map_of_map: "map_of (map (%(a,b). (a,f b)) xs) x = option_map f (map_of xs x)" |
|
167 |
apply (induct_tac "xs") |
|
168 |
apply auto |
|
169 |
done |
|
170 |
||
171 |
||
13937 | 172 |
subsection {* option\_map related *} |
13908 | 173 |
|
13910 | 174 |
lemma option_map_o_empty[simp]: "option_map f o empty = empty" |
13908 | 175 |
apply (rule ext) |
176 |
apply (simp (no_asm)) |
|
177 |
done |
|
178 |
||
13910 | 179 |
lemma option_map_o_map_upd[simp]: |
180 |
"option_map f o m(a|->b) = (option_map f o m)(a|->f b)" |
|
13908 | 181 |
apply (rule ext) |
182 |
apply (simp (no_asm)) |
|
183 |
done |
|
184 |
||
185 |
||
13937 | 186 |
subsection {* ++ *} |
13908 | 187 |
|
14025 | 188 |
lemma map_add_empty[simp]: "m ++ empty = m" |
189 |
apply (unfold map_add_def) |
|
13908 | 190 |
apply (simp (no_asm)) |
191 |
done |
|
192 |
||
14025 | 193 |
lemma empty_map_add[simp]: "empty ++ m = m" |
194 |
apply (unfold map_add_def) |
|
13908 | 195 |
apply (rule ext) |
196 |
apply (simp split add: option.split) |
|
197 |
done |
|
198 |
||
14025 | 199 |
lemma map_add_assoc[simp]: "m1 ++ (m2 ++ m3) = (m1 ++ m2) ++ m3" |
200 |
apply(rule ext) |
|
201 |
apply(simp add: map_add_def split:option.split) |
|
202 |
done |
|
203 |
||
204 |
lemma map_add_Some_iff: |
|
13908 | 205 |
"((m ++ n) k = Some x) = (n k = Some x | n k = None & m k = Some x)" |
14025 | 206 |
apply (unfold map_add_def) |
13908 | 207 |
apply (simp (no_asm) split add: option.split) |
208 |
done |
|
209 |
||
14025 | 210 |
lemmas map_add_SomeD = map_add_Some_iff [THEN iffD1, standard] |
211 |
declare map_add_SomeD [dest!] |
|
13908 | 212 |
|
14025 | 213 |
lemma map_add_find_right[simp]: "!!xx. n k = Some xx ==> (m ++ n) k = Some xx" |
214 |
apply (subst map_add_Some_iff) |
|
13908 | 215 |
apply fast |
216 |
done |
|
217 |
||
14025 | 218 |
lemma map_add_None [iff]: "((m ++ n) k = None) = (n k = None & m k = None)" |
219 |
apply (unfold map_add_def) |
|
13908 | 220 |
apply (simp (no_asm) split add: option.split) |
221 |
done |
|
222 |
||
14025 | 223 |
lemma map_add_upd[simp]: "f ++ g(x|->y) = (f ++ g)(x|->y)" |
224 |
apply (unfold map_add_def) |
|
13908 | 225 |
apply (rule ext) |
226 |
apply auto |
|
227 |
done |
|
228 |
||
14025 | 229 |
lemma map_of_append[simp]: "map_of (xs@ys) = map_of ys ++ map_of xs" |
230 |
apply (unfold map_add_def) |
|
13908 | 231 |
apply (induct_tac "xs") |
232 |
apply (simp (no_asm)) |
|
233 |
apply (rule ext) |
|
234 |
apply (simp (no_asm_simp) split add: option.split) |
|
235 |
done |
|
236 |
||
237 |
declare fun_upd_apply [simp del] |
|
14025 | 238 |
lemma finite_range_map_of_map_add: |
239 |
"finite (range f) ==> finite (range (f ++ map_of l))" |
|
13908 | 240 |
apply (induct_tac "l") |
241 |
apply auto |
|
242 |
apply (erule finite_range_updI) |
|
243 |
done |
|
244 |
declare fun_upd_apply [simp] |
|
245 |
||
246 |
||
14025 | 247 |
subsection {* map\_upds *} |
248 |
||
249 |
lemma map_upds_Nil1[simp]: "m([] [|->] bs) = m" |
|
250 |
by(simp add:map_upds_def) |
|
251 |
||
252 |
lemma map_upds_Nil2[simp]: "m(as [|->] []) = m" |
|
253 |
by(simp add:map_upds_def) |
|
254 |
||
255 |
lemma map_upds_Cons[simp]: "m(a#as [|->] b#bs) = (m(a|->b))(as[|->]bs)" |
|
256 |
by(simp add:map_upds_def) |
|
257 |
||
258 |
||
259 |
lemma map_upd_upds_conv_if: "!!x y ys f. |
|
260 |
(f(x|->y))(xs [|->] ys) = |
|
261 |
(if x : set(take (length ys) xs) then f(xs [|->] ys) |
|
262 |
else (f(xs [|->] ys))(x|->y))" |
|
263 |
apply(induct xs) |
|
264 |
apply simp |
|
265 |
apply(case_tac ys) |
|
266 |
apply(auto split:split_if simp:fun_upd_twist) |
|
267 |
done |
|
268 |
||
269 |
lemma map_upds_twist [simp]: |
|
270 |
"a ~: set as ==> m(a|->b)(as[|->]bs) = m(as[|->]bs)(a|->b)" |
|
271 |
apply(insert set_take_subset) |
|
272 |
apply (fastsimp simp add: map_upd_upds_conv_if) |
|
273 |
done |
|
274 |
||
275 |
lemma map_upds_apply_nontin[simp]: |
|
276 |
"!!ys. x ~: set xs ==> (f(xs[|->]ys)) x = f x" |
|
277 |
apply(induct xs) |
|
278 |
apply simp |
|
279 |
apply(case_tac ys) |
|
280 |
apply(auto simp: map_upd_upds_conv_if) |
|
281 |
done |
|
282 |
||
13937 | 283 |
subsection {* dom *} |
13908 | 284 |
|
285 |
lemma domI: "m a = Some b ==> a : dom m" |
|
286 |
apply (unfold dom_def) |
|
287 |
apply auto |
|
288 |
done |
|
289 |
||
290 |
lemma domD: "a : dom m ==> ? b. m a = Some b" |
|
291 |
apply (unfold dom_def) |
|
292 |
apply auto |
|
293 |
done |
|
294 |
||
13910 | 295 |
lemma domIff[iff]: "(a : dom m) = (m a ~= None)" |
13908 | 296 |
apply (unfold dom_def) |
297 |
apply auto |
|
298 |
done |
|
299 |
declare domIff [simp del] |
|
300 |
||
13910 | 301 |
lemma dom_empty[simp]: "dom empty = {}" |
13908 | 302 |
apply (unfold dom_def) |
303 |
apply (simp (no_asm)) |
|
304 |
done |
|
305 |
||
13910 | 306 |
lemma dom_fun_upd[simp]: |
307 |
"dom(f(x := y)) = (if y=None then dom f - {x} else insert x (dom f))" |
|
308 |
by (simp add:dom_def) blast |
|
13908 | 309 |
|
13937 | 310 |
lemma dom_map_of: "dom(map_of xys) = {x. \<exists>y. (x,y) : set xys}" |
311 |
apply(induct xys) |
|
312 |
apply(auto simp del:fun_upd_apply) |
|
313 |
done |
|
314 |
||
13908 | 315 |
lemma finite_dom_map_of: "finite (dom (map_of l))" |
316 |
apply (unfold dom_def) |
|
317 |
apply (induct_tac "l") |
|
318 |
apply (auto simp add: insert_Collect [symmetric]) |
|
319 |
done |
|
320 |
||
14025 | 321 |
lemma dom_map_upds[simp]: |
322 |
"!!m ys. dom(m(xs[|->]ys)) = set(take (length ys) xs) Un dom m" |
|
323 |
apply(induct xs) |
|
324 |
apply simp |
|
325 |
apply(case_tac ys) |
|
326 |
apply auto |
|
327 |
done |
|
13910 | 328 |
|
14025 | 329 |
lemma dom_map_add[simp]: "dom(m++n) = dom n Un dom m" |
13908 | 330 |
apply (unfold dom_def) |
331 |
apply auto |
|
332 |
done |
|
13910 | 333 |
|
334 |
lemma dom_overwrite[simp]: |
|
335 |
"dom(f(g|A)) = (dom f - {a. a : A - dom g}) Un {a. a : A Int dom g}" |
|
336 |
by(auto simp add: dom_def overwrite_def) |
|
13908 | 337 |
|
14027 | 338 |
lemma map_add_comm: "dom m1 \<inter> dom m2 = {} \<Longrightarrow> m1++m2 = m2++m1" |
339 |
apply(rule ext) |
|
340 |
apply(fastsimp simp:map_add_def split:option.split) |
|
341 |
done |
|
342 |
||
13937 | 343 |
subsection {* ran *} |
13908 | 344 |
|
13910 | 345 |
lemma ran_empty[simp]: "ran empty = {}" |
13908 | 346 |
apply (unfold ran_def) |
347 |
apply (simp (no_asm)) |
|
348 |
done |
|
349 |
||
13910 | 350 |
lemma ran_map_upd[simp]: "m a = None ==> ran(m(a|->b)) = insert b (ran m)" |
13908 | 351 |
apply (unfold ran_def) |
352 |
apply auto |
|
353 |
apply (subgoal_tac "~ (aa = a) ") |
|
354 |
apply auto |
|
355 |
done |
|
13910 | 356 |
|
13937 | 357 |
subsection {* map\_le *} |
13910 | 358 |
|
13912 | 359 |
lemma map_le_empty [simp]: "empty \<subseteq>\<^sub>m g" |
13910 | 360 |
by(simp add:map_le_def) |
361 |
||
362 |
lemma map_le_upd[simp]: "f \<subseteq>\<^sub>m g ==> f(a := b) \<subseteq>\<^sub>m g(a := b)" |
|
363 |
by(fastsimp simp add:map_le_def) |
|
364 |
||
365 |
lemma map_le_upds[simp]: |
|
366 |
"!!f g bs. f \<subseteq>\<^sub>m g ==> f(as [|->] bs) \<subseteq>\<^sub>m g(as [|->] bs)" |
|
14025 | 367 |
apply(induct as) |
368 |
apply simp |
|
369 |
apply(case_tac bs) |
|
370 |
apply auto |
|
371 |
done |
|
13908 | 372 |
|
14033 | 373 |
lemma map_le_implies_dom_le: "(f \<subseteq>\<^sub>m g) \<Longrightarrow> (dom f \<subseteq> dom g)" |
374 |
by (fastsimp simp add: map_le_def dom_def) |
|
375 |
||
376 |
lemma map_le_refl [simp]: "f \<subseteq>\<^sub>m f" |
|
377 |
by (simp add: map_le_def) |
|
378 |
||
379 |
lemma map_le_trans: "\<lbrakk> f \<subseteq>\<^sub>m g; g \<subseteq>\<^sub>m h \<rbrakk> \<Longrightarrow> f \<subseteq>\<^sub>m h" |
|
380 |
apply (clarsimp simp add: map_le_def) |
|
381 |
apply (drule_tac x="a" in bspec, fastsimp)+ |
|
382 |
apply assumption |
|
383 |
done |
|
384 |
||
385 |
lemma map_le_antisym: "\<lbrakk> f \<subseteq>\<^sub>m g; g \<subseteq>\<^sub>m f \<rbrakk> \<Longrightarrow> f = g" |
|
386 |
apply (unfold map_le_def) |
|
387 |
apply (rule ext) |
|
388 |
apply (case_tac "x \<in> dom f") |
|
389 |
apply simp |
|
390 |
apply (case_tac "x \<in> dom g") |
|
391 |
apply simp |
|
392 |
apply fastsimp |
|
393 |
done |
|
394 |
||
395 |
lemma map_le_map_add [simp]: "f \<subseteq>\<^sub>m (g ++ f)" |
|
396 |
by (fastsimp simp add: map_le_def) |
|
397 |
||
3981 | 398 |
end |