19234
|
1 |
(* Title: HOL/Library/Library.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Norbert Schirmer, Tobias Nipkow, Martin Wildmoser
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Map operations implemented on association lists*}
|
|
7 |
|
|
8 |
theory AssocList
|
|
9 |
imports Map
|
|
10 |
|
|
11 |
begin
|
|
12 |
|
|
13 |
text {* The operations preserve distinctness of keys and
|
|
14 |
function @{term "clearjunk"} distributes over them.*}
|
|
15 |
consts
|
|
16 |
delete :: "'key \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
17 |
update :: "'key \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
18 |
updates :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
19 |
merge :: "('key * 'val)list \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
20 |
compose :: "('key * 'a)list \<Rightarrow> ('a * 'b)list \<Rightarrow> ('key * 'b)list"
|
|
21 |
restrict :: "('key set) \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
22 |
|
|
23 |
clearjunk :: "('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
24 |
|
19323
|
25 |
(* a bit special
|
|
26 |
substitute :: "'val \<Rightarrow> 'val \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val)list"
|
|
27 |
map_at :: "('val \<Rightarrow> 'val) \<Rightarrow> 'key \<Rightarrow> ('key * 'val)list \<Rightarrow> ('key * 'val) list"
|
|
28 |
*)
|
|
29 |
|
19234
|
30 |
defs
|
|
31 |
delete_def: "delete k \<equiv> filter (\<lambda>p. fst p \<noteq> k)"
|
|
32 |
|
|
33 |
primrec
|
|
34 |
"update k v [] = [(k,v)]"
|
|
35 |
"update k v (p#ps) = (if fst p = k then (k,v)#ps else p # update k v ps)"
|
|
36 |
primrec
|
|
37 |
"updates [] vs al = al"
|
|
38 |
"updates (k#ks) vs al = (case vs of [] \<Rightarrow> al
|
|
39 |
| (v#vs') \<Rightarrow> updates ks vs' (update k v al))"
|
|
40 |
primrec
|
19323
|
41 |
"merge xs [] = xs"
|
|
42 |
"merge xs (p#ps) = update (fst p) (snd p) (merge xs ps)"
|
|
43 |
|
|
44 |
(*
|
|
45 |
primrec
|
19234
|
46 |
"substitute v v' [] = []"
|
|
47 |
"substitute v v' (p#ps) = (if snd p = v then (fst p,v')#substitute v v' ps
|
|
48 |
else p#substitute v v' ps)"
|
|
49 |
primrec
|
|
50 |
"map_at f k [] = []"
|
|
51 |
"map_at f k (p#ps) = (if fst p = k then (k,f (snd p))#ps else p # map_at f k ps)"
|
19323
|
52 |
*)
|
19234
|
53 |
|
|
54 |
lemma length_delete_le: "length (delete k al) \<le> length al"
|
|
55 |
proof (induct al)
|
|
56 |
case Nil thus ?case by (simp add: delete_def)
|
|
57 |
next
|
|
58 |
case (Cons a al)
|
|
59 |
note length_filter_le [of "\<lambda>p. fst p \<noteq> fst a" al]
|
|
60 |
also have "\<And>n. n \<le> Suc n"
|
|
61 |
by simp
|
|
62 |
finally have "length [p\<in>al . fst p \<noteq> fst a] \<le> Suc (length al)" .
|
|
63 |
with Cons show ?case
|
|
64 |
by (auto simp add: delete_def)
|
|
65 |
qed
|
|
66 |
|
|
67 |
lemma compose_hint: "length (delete k al) < Suc (length al)"
|
|
68 |
proof -
|
|
69 |
note length_delete_le
|
|
70 |
also have "\<And>n. n < Suc n"
|
|
71 |
by simp
|
|
72 |
finally show ?thesis .
|
|
73 |
qed
|
|
74 |
|
|
75 |
recdef compose "measure size"
|
|
76 |
"compose [] = (\<lambda>ys. [])"
|
|
77 |
"compose (x#xs) = (\<lambda>ys. (case (map_of ys (snd x)) of
|
|
78 |
None \<Rightarrow> compose (delete (fst x) xs) ys
|
|
79 |
| Some v \<Rightarrow> (fst x,v)#compose xs ys))"
|
|
80 |
(hints intro: compose_hint)
|
|
81 |
|
|
82 |
defs
|
|
83 |
restrict_def: "restrict A \<equiv> filter (\<lambda>(k,v). k \<in> A)"
|
|
84 |
|
|
85 |
recdef clearjunk "measure size"
|
|
86 |
"clearjunk [] = []"
|
|
87 |
"clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)"
|
|
88 |
(hints intro: compose_hint)
|
|
89 |
|
|
90 |
|
|
91 |
(* ******************************************************************************** *)
|
|
92 |
subsection {* Lookup *}
|
|
93 |
(* ******************************************************************************** *)
|
|
94 |
|
|
95 |
lemma lookup_simps:
|
|
96 |
"map_of [] k = None"
|
|
97 |
"map_of (p#ps) k = (if fst p = k then Some (snd p) else map_of ps k)"
|
|
98 |
by simp_all
|
|
99 |
|
|
100 |
(* ******************************************************************************** *)
|
|
101 |
subsection {* @{const delete} *}
|
|
102 |
(* ******************************************************************************** *)
|
|
103 |
|
|
104 |
lemma delete_simps [simp]:
|
|
105 |
"delete k [] = []"
|
|
106 |
"delete k (p#ps) = (if fst p = k then delete k ps else p # delete k ps)"
|
|
107 |
by (simp_all add: delete_def)
|
|
108 |
|
|
109 |
lemma delete_id[simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al"
|
|
110 |
by(induct al, auto)
|
|
111 |
|
|
112 |
lemma delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'"
|
|
113 |
by (induct al) auto
|
|
114 |
|
|
115 |
lemma delete_conv': "map_of (delete k al) = ((map_of al)(k := None))"
|
|
116 |
by (rule ext) (rule delete_conv)
|
|
117 |
|
|
118 |
lemma delete_idem: "delete k (delete k al) = delete k al"
|
|
119 |
by (induct al) auto
|
|
120 |
|
|
121 |
lemma map_of_delete[simp]:
|
|
122 |
"k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'"
|
|
123 |
by(induct al, auto)
|
|
124 |
|
|
125 |
lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)"
|
|
126 |
by (induct al) auto
|
|
127 |
|
|
128 |
lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al"
|
|
129 |
by (induct al) auto
|
|
130 |
|
|
131 |
lemma distinct_delete:
|
|
132 |
assumes "distinct (map fst al)"
|
|
133 |
shows "distinct (map fst (delete k al))"
|
|
134 |
using prems
|
|
135 |
proof (induct al)
|
|
136 |
case Nil thus ?case by simp
|
|
137 |
next
|
|
138 |
case (Cons a al)
|
|
139 |
from Cons.prems obtain
|
|
140 |
a_notin_al: "fst a \<notin> fst ` set al" and
|
|
141 |
dist_al: "distinct (map fst al)"
|
|
142 |
by auto
|
|
143 |
show ?case
|
|
144 |
proof (cases "fst a = k")
|
|
145 |
case True
|
|
146 |
from True dist_al show ?thesis by simp
|
|
147 |
next
|
|
148 |
case False
|
|
149 |
from dist_al
|
|
150 |
have "distinct (map fst (delete k al))"
|
|
151 |
by (rule Cons.hyps)
|
|
152 |
moreover from a_notin_al dom_delete_subset [of k al]
|
|
153 |
have "fst a \<notin> fst ` set (delete k al)"
|
|
154 |
by blast
|
|
155 |
ultimately show ?thesis using False by simp
|
|
156 |
qed
|
|
157 |
qed
|
|
158 |
|
|
159 |
lemma delete_twist: "delete x (delete y al) = delete y (delete x al)"
|
|
160 |
by (induct al) auto
|
|
161 |
|
|
162 |
lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)"
|
|
163 |
by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist)
|
|
164 |
|
|
165 |
(* ******************************************************************************** *)
|
|
166 |
subsection {* @{const clearjunk} *}
|
|
167 |
(* ******************************************************************************** *)
|
|
168 |
|
|
169 |
lemma insert_fst_filter:
|
|
170 |
"insert a(fst ` {x \<in> set ps. fst x \<noteq> a}) = insert a (fst ` set ps)"
|
|
171 |
by (induct ps) auto
|
|
172 |
|
|
173 |
lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al"
|
|
174 |
by (induct al rule: clearjunk.induct) (simp_all add: insert_fst_filter delete_def)
|
|
175 |
|
|
176 |
lemma notin_filter_fst: "a \<notin> fst ` {x \<in> set ps. fst x \<noteq> a}"
|
|
177 |
by (induct ps) auto
|
|
178 |
|
|
179 |
lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))"
|
|
180 |
by (induct al rule: clearjunk.induct)
|
|
181 |
(simp_all add: dom_clearjunk notin_filter_fst delete_def)
|
|
182 |
|
|
183 |
lemma map_of_filter: "k \<noteq> a \<Longrightarrow> map_of [q\<in>ps . fst q \<noteq> a] k = map_of ps k"
|
|
184 |
by (induct ps) auto
|
|
185 |
|
|
186 |
lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al"
|
|
187 |
apply (rule ext)
|
|
188 |
apply (induct al rule: clearjunk.induct)
|
|
189 |
apply simp
|
|
190 |
apply (simp add: map_of_filter)
|
|
191 |
done
|
|
192 |
|
|
193 |
lemma length_clearjunk: "length (clearjunk al) \<le> length al"
|
|
194 |
proof (induct al rule: clearjunk.induct [case_names Nil Cons])
|
|
195 |
case Nil thus ?case by simp
|
|
196 |
next
|
|
197 |
case (Cons k v ps)
|
|
198 |
from Cons have "length (clearjunk [q\<in>ps . fst q \<noteq> k]) \<le> length [q\<in>ps . fst q \<noteq> k]"
|
|
199 |
by (simp add: delete_def)
|
|
200 |
also have "\<dots> \<le> length ps"
|
|
201 |
by simp
|
|
202 |
finally show ?case
|
|
203 |
by (simp add: delete_def)
|
|
204 |
qed
|
|
205 |
|
|
206 |
lemma notin_fst_filter: "a \<notin> fst ` set ps \<Longrightarrow> [q\<in>ps . fst q \<noteq> a] = ps"
|
|
207 |
by (induct ps) auto
|
|
208 |
|
|
209 |
lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al"
|
|
210 |
by (induct al rule: clearjunk.induct) (auto simp add: notin_fst_filter)
|
|
211 |
|
|
212 |
lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al"
|
|
213 |
by simp
|
|
214 |
|
|
215 |
(* ******************************************************************************** *)
|
|
216 |
subsection {* @{const dom} and @{term "ran"} *}
|
|
217 |
(* ******************************************************************************** *)
|
|
218 |
|
|
219 |
lemma dom_map_of': "fst ` set al = dom (map_of al)"
|
|
220 |
by (induct al) auto
|
|
221 |
|
|
222 |
lemmas dom_map_of = dom_map_of' [symmetric]
|
|
223 |
|
|
224 |
lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)"
|
|
225 |
by (simp add: map_of_clearjunk)
|
|
226 |
|
|
227 |
lemma ran_distinct:
|
|
228 |
assumes dist: "distinct (map fst al)"
|
|
229 |
shows "ran (map_of al) = snd ` set al"
|
|
230 |
using dist
|
|
231 |
proof (induct al)
|
|
232 |
case Nil
|
|
233 |
thus ?case by simp
|
|
234 |
next
|
|
235 |
case (Cons a al)
|
|
236 |
hence hyp: "snd ` set al = ran (map_of al)"
|
|
237 |
by simp
|
|
238 |
|
|
239 |
have "ran (map_of (a # al)) = {snd a} \<union> ran (map_of al)"
|
|
240 |
proof
|
|
241 |
show "ran (map_of (a # al)) \<subseteq> {snd a} \<union> ran (map_of al)"
|
|
242 |
proof
|
|
243 |
fix v
|
|
244 |
assume "v \<in> ran (map_of (a#al))"
|
|
245 |
then obtain x where "map_of (a#al) x = Some v"
|
|
246 |
by (auto simp add: ran_def)
|
|
247 |
then show "v \<in> {snd a} \<union> ran (map_of al)"
|
|
248 |
by (auto split: split_if_asm simp add: ran_def)
|
|
249 |
qed
|
|
250 |
next
|
|
251 |
show "{snd a} \<union> ran (map_of al) \<subseteq> ran (map_of (a # al))"
|
|
252 |
proof
|
|
253 |
fix v
|
|
254 |
assume v_in: "v \<in> {snd a} \<union> ran (map_of al)"
|
|
255 |
show "v \<in> ran (map_of (a#al))"
|
|
256 |
proof (cases "v=snd a")
|
|
257 |
case True
|
|
258 |
with v_in show ?thesis
|
|
259 |
by (auto simp add: ran_def)
|
|
260 |
next
|
|
261 |
case False
|
|
262 |
with v_in have "v \<in> ran (map_of al)" by auto
|
|
263 |
then obtain x where al_x: "map_of al x = Some v"
|
|
264 |
by (auto simp add: ran_def)
|
|
265 |
from map_of_SomeD [OF this]
|
|
266 |
have "x \<in> fst ` set al"
|
|
267 |
by (force simp add: image_def)
|
|
268 |
with Cons.prems have "x\<noteq>fst a"
|
|
269 |
by - (rule ccontr,simp)
|
|
270 |
with al_x
|
|
271 |
show ?thesis
|
|
272 |
by (auto simp add: ran_def)
|
|
273 |
qed
|
|
274 |
qed
|
|
275 |
qed
|
|
276 |
with hyp show ?case
|
|
277 |
by (simp only:) auto
|
|
278 |
qed
|
|
279 |
|
|
280 |
lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)"
|
|
281 |
proof -
|
|
282 |
have "ran (map_of al) = ran (map_of (clearjunk al))"
|
|
283 |
by (simp add: ran_clearjunk)
|
|
284 |
also have "\<dots> = snd ` set (clearjunk al)"
|
|
285 |
by (simp add: ran_distinct)
|
|
286 |
finally show ?thesis .
|
|
287 |
qed
|
|
288 |
|
|
289 |
(* ******************************************************************************** *)
|
|
290 |
subsection {* @{const update} *}
|
|
291 |
(* ******************************************************************************** *)
|
|
292 |
|
|
293 |
lemma update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'"
|
|
294 |
by (induct al) auto
|
|
295 |
|
|
296 |
lemma update_conv': "map_of (update k v al) = ((map_of al)(k\<mapsto>v))"
|
|
297 |
by (rule ext) (rule update_conv)
|
|
298 |
|
|
299 |
lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al"
|
|
300 |
by (induct al) auto
|
|
301 |
|
|
302 |
lemma distinct_update:
|
|
303 |
assumes "distinct (map fst al)"
|
|
304 |
shows "distinct (map fst (update k v al))"
|
|
305 |
using prems
|
|
306 |
proof (induct al)
|
|
307 |
case Nil thus ?case by simp
|
|
308 |
next
|
|
309 |
case (Cons a al)
|
|
310 |
from Cons.prems obtain
|
|
311 |
a_notin_al: "fst a \<notin> fst ` set al" and
|
|
312 |
dist_al: "distinct (map fst al)"
|
|
313 |
by auto
|
|
314 |
show ?case
|
|
315 |
proof (cases "fst a = k")
|
|
316 |
case True
|
|
317 |
from True dist_al a_notin_al show ?thesis by simp
|
|
318 |
next
|
|
319 |
case False
|
|
320 |
from dist_al
|
|
321 |
have "distinct (map fst (update k v al))"
|
|
322 |
by (rule Cons.hyps)
|
|
323 |
with False a_notin_al show ?thesis by (simp add: dom_update)
|
|
324 |
qed
|
|
325 |
qed
|
|
326 |
|
|
327 |
lemma update_filter:
|
|
328 |
"a\<noteq>k \<Longrightarrow> update k v [q\<in>ps . fst q \<noteq> a] = [q\<in>update k v ps . fst q \<noteq> a]"
|
|
329 |
by (induct ps) auto
|
|
330 |
|
|
331 |
lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)"
|
|
332 |
by (induct al rule: clearjunk.induct) (auto simp add: update_filter delete_def)
|
|
333 |
|
|
334 |
lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al"
|
|
335 |
by (induct al) auto
|
|
336 |
|
|
337 |
lemma update_nonempty [simp]: "update k v al \<noteq> []"
|
|
338 |
by (induct al) auto
|
|
339 |
|
|
340 |
lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v=v'"
|
|
341 |
proof (induct al fixing: al')
|
|
342 |
case Nil thus ?case
|
|
343 |
by (cases al') (auto split: split_if_asm)
|
|
344 |
next
|
|
345 |
case Cons thus ?case
|
|
346 |
by (cases al') (auto split: split_if_asm)
|
|
347 |
qed
|
|
348 |
|
|
349 |
lemma update_last [simp]: "update k v (update k v' al) = update k v al"
|
|
350 |
by (induct al) auto
|
|
351 |
|
|
352 |
text {* Note that the lists are not necessarily the same:
|
|
353 |
@{term "update k v (update k' v' []) = [(k',v'),(k,v)]"} and
|
|
354 |
@{term "update k' v' (update k v []) = [(k,v),(k',v')]"}.*}
|
|
355 |
lemma update_swap: "k\<noteq>k'
|
|
356 |
\<Longrightarrow> map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))"
|
|
357 |
by (auto simp add: update_conv' intro: ext)
|
|
358 |
|
|
359 |
lemma update_Some_unfold:
|
|
360 |
"(map_of (update k v al) x = Some y) =
|
|
361 |
(x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y)"
|
|
362 |
by (simp add: update_conv' map_upd_Some_unfold)
|
|
363 |
|
|
364 |
lemma image_update[simp]: "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A"
|
|
365 |
by (simp add: update_conv' image_map_upd)
|
|
366 |
|
|
367 |
|
|
368 |
(* ******************************************************************************** *)
|
|
369 |
subsection {* @{const updates} *}
|
|
370 |
(* ******************************************************************************** *)
|
|
371 |
|
|
372 |
lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k"
|
|
373 |
proof (induct ks fixing: vs al)
|
|
374 |
case Nil
|
|
375 |
thus ?case by simp
|
|
376 |
next
|
|
377 |
case (Cons k ks)
|
|
378 |
show ?case
|
|
379 |
proof (cases vs)
|
|
380 |
case Nil
|
|
381 |
with Cons show ?thesis by simp
|
|
382 |
next
|
|
383 |
case (Cons k ks')
|
|
384 |
with Cons.hyps show ?thesis
|
|
385 |
by (simp add: update_conv fun_upd_def)
|
|
386 |
qed
|
|
387 |
qed
|
|
388 |
|
|
389 |
lemma updates_conv': "map_of (updates ks vs al) = ((map_of al)(ks[\<mapsto>]vs))"
|
|
390 |
by (rule ext) (rule updates_conv)
|
|
391 |
|
|
392 |
lemma distinct_updates:
|
|
393 |
assumes "distinct (map fst al)"
|
|
394 |
shows "distinct (map fst (updates ks vs al))"
|
|
395 |
using prems
|
|
396 |
by (induct ks fixing: vs al) (auto simp add: distinct_update split: list.splits)
|
|
397 |
|
|
398 |
lemma clearjunk_updates:
|
|
399 |
"clearjunk (updates ks vs al) = updates ks vs (clearjunk al)"
|
|
400 |
by (induct ks fixing: vs al) (auto simp add: clearjunk_update split: list.splits)
|
|
401 |
|
|
402 |
lemma updates_empty[simp]: "updates vs [] al = al"
|
|
403 |
by (induct vs) auto
|
|
404 |
|
|
405 |
lemma updates_Cons: "updates (k#ks) (v#vs) al = updates ks vs (update k v al)"
|
|
406 |
by simp
|
|
407 |
|
|
408 |
lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow>
|
|
409 |
updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)"
|
|
410 |
by (induct ks fixing: vs al) (auto split: list.splits)
|
|
411 |
|
|
412 |
lemma updates_list_update_drop[simp]:
|
|
413 |
"\<lbrakk>size ks \<le> i; i < size vs\<rbrakk>
|
|
414 |
\<Longrightarrow> updates ks (vs[i:=v]) al = updates ks vs al"
|
|
415 |
by (induct ks fixing: al vs i) (auto split:list.splits nat.splits)
|
|
416 |
|
|
417 |
lemma update_updates_conv_if: "
|
|
418 |
map_of (updates xs ys (update x y al)) =
|
|
419 |
map_of (if x \<in> set(take (length ys) xs) then updates xs ys al
|
|
420 |
else (update x y (updates xs ys al)))"
|
|
421 |
by (simp add: updates_conv' update_conv' map_upd_upds_conv_if)
|
|
422 |
|
|
423 |
lemma updates_twist [simp]:
|
|
424 |
"k \<notin> set ks \<Longrightarrow>
|
|
425 |
map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))"
|
|
426 |
by (simp add: updates_conv' update_conv' map_upds_twist)
|
|
427 |
|
|
428 |
lemma updates_apply_notin[simp]:
|
|
429 |
"k \<notin> set ks ==> map_of (updates ks vs al) k = map_of al k"
|
|
430 |
by (simp add: updates_conv)
|
|
431 |
|
|
432 |
lemma updates_append_drop[simp]:
|
|
433 |
"size xs = size ys \<Longrightarrow> updates (xs@zs) ys al = updates xs ys al"
|
|
434 |
by (induct xs fixing: ys al) (auto split: list.splits)
|
|
435 |
|
|
436 |
lemma updates_append2_drop[simp]:
|
|
437 |
"size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al"
|
|
438 |
by (induct xs fixing: ys al) (auto split: list.splits)
|
|
439 |
|
19323
|
440 |
(*
|
19234
|
441 |
(* ******************************************************************************** *)
|
|
442 |
subsection {* @{const substitute} *}
|
|
443 |
(* ******************************************************************************** *)
|
|
444 |
|
|
445 |
lemma substitute_conv: "map_of (substitute v v' al) k = ((map_of al)(v ~> v')) k"
|
|
446 |
by (induct al) auto
|
|
447 |
|
|
448 |
lemma substitute_conv': "map_of (substitute v v' al) = ((map_of al)(v ~> v'))"
|
|
449 |
by (rule ext) (rule substitute_conv)
|
|
450 |
|
|
451 |
lemma dom_substitute: "fst ` set (substitute v v' al) = fst ` set al"
|
|
452 |
by (induct al) auto
|
|
453 |
|
|
454 |
lemma distinct_substitute:
|
|
455 |
"distinct (map fst al) \<Longrightarrow> distinct (map fst (substitute v v' al))"
|
|
456 |
by (induct al) (auto simp add: dom_substitute)
|
|
457 |
|
|
458 |
lemma substitute_filter:
|
|
459 |
"(substitute v v' [q\<in>ps . fst q \<noteq> a]) = [q\<in>substitute v v' ps . fst q \<noteq> a]"
|
|
460 |
by (induct ps) auto
|
|
461 |
|
|
462 |
lemma clearjunk_substitute:
|
|
463 |
"clearjunk (substitute v v' al) = substitute v v' (clearjunk al)"
|
|
464 |
by (induct al rule: clearjunk.induct) (auto simp add: substitute_filter delete_def)
|
19323
|
465 |
*)
|
|
466 |
(*
|
19234
|
467 |
(* ******************************************************************************** *)
|
|
468 |
subsection {* @{const map_at} *}
|
|
469 |
(* ******************************************************************************** *)
|
|
470 |
|
|
471 |
lemma map_at_conv: "map_of (map_at f k al) k' = (chg_map f k (map_of al)) k'"
|
|
472 |
by (induct al) (auto simp add: chg_map_def split: option.splits)
|
|
473 |
|
|
474 |
lemma map_at_conv': "map_of (map_at f k al) = (chg_map f k (map_of al))"
|
|
475 |
by (rule ext) (rule map_at_conv)
|
|
476 |
|
|
477 |
lemma dom_map_at: "fst ` set (map_at f k al) = fst ` set al"
|
|
478 |
by (induct al) auto
|
|
479 |
|
|
480 |
lemma distinct_map_at:
|
|
481 |
assumes "distinct (map fst al)"
|
|
482 |
shows "distinct (map fst (map_at f k al))"
|
|
483 |
using prems by (induct al) (auto simp add: dom_map_at)
|
|
484 |
|
|
485 |
lemma map_at_notin_filter:
|
|
486 |
"a \<noteq> k \<Longrightarrow> (map_at f k [q\<in>ps . fst q \<noteq> a]) = [q\<in>map_at f k ps . fst q \<noteq> a]"
|
|
487 |
by (induct ps) auto
|
|
488 |
|
|
489 |
lemma clearjunk_map_at:
|
|
490 |
"clearjunk (map_at f k al) = map_at f k (clearjunk al)"
|
|
491 |
by (induct al rule: clearjunk.induct) (auto simp add: map_at_notin_filter delete_def)
|
|
492 |
|
|
493 |
lemma map_at_new[simp]: "map_of al k = None \<Longrightarrow> map_at f k al = al"
|
|
494 |
by (induct al) auto
|
|
495 |
|
|
496 |
lemma map_at_update: "map_of al k = Some v \<Longrightarrow> map_at f k al = update k (f v) al"
|
|
497 |
by (induct al) auto
|
|
498 |
|
|
499 |
lemma map_at_other [simp]: "a \<noteq> b \<Longrightarrow> map_of (map_at f a al) b = map_of al b"
|
|
500 |
by (simp add: map_at_conv')
|
19323
|
501 |
*)
|
19234
|
502 |
(* ******************************************************************************** *)
|
|
503 |
subsection {* @{const merge} *}
|
|
504 |
(* ******************************************************************************** *)
|
|
505 |
|
|
506 |
lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
|
|
507 |
by (induct ys fixing: xs) (auto simp add: dom_update)
|
|
508 |
|
|
509 |
lemma distinct_merge:
|
|
510 |
assumes "distinct (map fst xs)"
|
|
511 |
shows "distinct (map fst (merge xs ys))"
|
|
512 |
using prems
|
|
513 |
by (induct ys fixing: xs) (auto simp add: dom_merge distinct_update)
|
|
514 |
|
|
515 |
lemma clearjunk_merge:
|
|
516 |
"clearjunk (merge xs ys) = merge (clearjunk xs) ys"
|
|
517 |
by (induct ys) (auto simp add: clearjunk_update)
|
|
518 |
|
|
519 |
lemma merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k"
|
|
520 |
proof (induct ys)
|
|
521 |
case Nil thus ?case by simp
|
|
522 |
next
|
|
523 |
case (Cons y ys)
|
|
524 |
show ?case
|
|
525 |
proof (cases "k = fst y")
|
|
526 |
case True
|
|
527 |
from True show ?thesis
|
|
528 |
by (simp add: update_conv)
|
|
529 |
next
|
|
530 |
case False
|
|
531 |
from False show ?thesis
|
|
532 |
by (auto simp add: update_conv Cons.hyps map_add_def)
|
|
533 |
qed
|
|
534 |
qed
|
|
535 |
|
|
536 |
lemma merge_conv': "map_of (merge xs ys) = (map_of xs ++ map_of ys)"
|
|
537 |
by (rule ext) (rule merge_conv)
|
|
538 |
|
|
539 |
lemma merge_emty: "map_of (merge [] ys) = map_of ys"
|
|
540 |
by (simp add: merge_conv')
|
|
541 |
|
|
542 |
lemma merge_assoc[simp]: "map_of (merge m1 (merge m2 m3)) =
|
|
543 |
map_of (merge (merge m1 m2) m3)"
|
|
544 |
by (simp add: merge_conv')
|
|
545 |
|
|
546 |
lemma merge_Some_iff:
|
|
547 |
"(map_of (merge m n) k = Some x) =
|
|
548 |
(map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x)"
|
|
549 |
by (simp add: merge_conv' map_add_Some_iff)
|
|
550 |
|
|
551 |
lemmas merge_SomeD = merge_Some_iff [THEN iffD1, standard]
|
|
552 |
declare merge_SomeD [dest!]
|
|
553 |
|
|
554 |
lemma merge_find_right[simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v"
|
|
555 |
by (simp add: merge_conv')
|
|
556 |
|
|
557 |
lemma merge_None [iff]:
|
|
558 |
"(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)"
|
|
559 |
by (simp add: merge_conv')
|
|
560 |
|
|
561 |
lemma merge_upd[simp]:
|
|
562 |
"map_of (merge m (update k v n)) = map_of (update k v (merge m n))"
|
|
563 |
by (simp add: update_conv' merge_conv')
|
|
564 |
|
|
565 |
lemma merge_updatess[simp]:
|
|
566 |
"map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))"
|
|
567 |
by (simp add: updates_conv' merge_conv')
|
|
568 |
|
|
569 |
lemma merge_append: "map_of (xs@ys) = map_of (merge ys xs)"
|
|
570 |
by (simp add: merge_conv')
|
|
571 |
|
|
572 |
(* ******************************************************************************** *)
|
|
573 |
subsection {* @{const compose} *}
|
|
574 |
(* ******************************************************************************** *)
|
|
575 |
|
|
576 |
lemma compose_induct [case_names Nil Cons]:
|
|
577 |
assumes Nil: "P [] ys"
|
|
578 |
assumes Cons: "\<And>x xs.
|
|
579 |
\<lbrakk>\<And>v. map_of ys (snd x) = Some v \<Longrightarrow> P xs ys;
|
|
580 |
map_of ys (snd x) = None \<Longrightarrow> P (delete (fst x) xs) ys\<rbrakk>
|
|
581 |
\<Longrightarrow> P (x # xs) ys"
|
|
582 |
shows "P xs ys"
|
|
583 |
apply (rule compose.induct [where ?P="\<lambda>xs. P xs ys"])
|
|
584 |
apply (rule Nil)
|
|
585 |
apply (rule Cons)
|
|
586 |
apply (erule allE, erule allE, erule impE, assumption,assumption)
|
|
587 |
apply (erule allE, erule impE,assumption,assumption)
|
|
588 |
done
|
|
589 |
|
|
590 |
lemma compose_first_None [simp]:
|
|
591 |
assumes "map_of xs k = None"
|
|
592 |
shows "map_of (compose xs ys) k = None"
|
|
593 |
using prems
|
|
594 |
by (induct xs ys rule: compose_induct) (auto split: option.splits split_if_asm)
|
|
595 |
|
|
596 |
|
|
597 |
lemma compose_conv:
|
|
598 |
shows "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
|
|
599 |
proof (induct xs ys rule: compose_induct )
|
|
600 |
case Nil thus ?case by simp
|
|
601 |
next
|
|
602 |
case (Cons x xs)
|
|
603 |
show ?case
|
|
604 |
proof (cases "map_of ys (snd x)")
|
|
605 |
case None
|
|
606 |
with Cons
|
|
607 |
have hyp: "map_of (compose (delete (fst x) xs) ys) k =
|
|
608 |
(map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k"
|
|
609 |
by simp
|
|
610 |
show ?thesis
|
|
611 |
proof (cases "fst x = k")
|
|
612 |
case True
|
|
613 |
from True delete_notin_dom [of k xs]
|
|
614 |
have "map_of (delete (fst x) xs) k = None"
|
|
615 |
by (simp add: map_of_eq_None_iff)
|
|
616 |
with hyp show ?thesis
|
|
617 |
using True None
|
|
618 |
by simp
|
|
619 |
next
|
|
620 |
case False
|
|
621 |
from False have "map_of (delete (fst x) xs) k = map_of xs k"
|
|
622 |
by simp
|
|
623 |
with hyp show ?thesis
|
|
624 |
using False None
|
|
625 |
by (simp add: map_comp_def)
|
|
626 |
qed
|
|
627 |
next
|
|
628 |
case (Some v)
|
|
629 |
with Cons
|
|
630 |
have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
|
|
631 |
by simp
|
|
632 |
with Some show ?thesis
|
|
633 |
by (auto simp add: map_comp_def)
|
|
634 |
qed
|
|
635 |
qed
|
|
636 |
|
|
637 |
lemma compose_conv':
|
|
638 |
shows "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)"
|
|
639 |
by (rule ext) (rule compose_conv)
|
|
640 |
|
|
641 |
lemma compose_first_Some [simp]:
|
|
642 |
assumes "map_of xs k = Some v"
|
|
643 |
shows "map_of (compose xs ys) k = map_of ys v"
|
|
644 |
using prems by (simp add: compose_conv)
|
|
645 |
|
|
646 |
lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
|
|
647 |
proof (induct xs ys rule: compose_induct )
|
|
648 |
case Nil thus ?case by simp
|
|
649 |
next
|
|
650 |
case (Cons x xs)
|
|
651 |
show ?case
|
|
652 |
proof (cases "map_of ys (snd x)")
|
|
653 |
case None
|
|
654 |
with Cons.hyps
|
|
655 |
have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)"
|
|
656 |
by simp
|
|
657 |
also
|
|
658 |
have "\<dots> \<subseteq> fst ` set xs"
|
|
659 |
by (rule dom_delete_subset)
|
|
660 |
finally show ?thesis
|
|
661 |
using None
|
|
662 |
by auto
|
|
663 |
next
|
|
664 |
case (Some v)
|
|
665 |
with Cons.hyps
|
|
666 |
have "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
|
|
667 |
by simp
|
|
668 |
with Some show ?thesis
|
|
669 |
by auto
|
|
670 |
qed
|
|
671 |
qed
|
|
672 |
|
|
673 |
lemma distinct_compose:
|
|
674 |
assumes "distinct (map fst xs)"
|
|
675 |
shows "distinct (map fst (compose xs ys))"
|
|
676 |
using prems
|
|
677 |
proof (induct xs ys rule: compose_induct)
|
|
678 |
case Nil thus ?case by simp
|
|
679 |
next
|
|
680 |
case (Cons x xs)
|
|
681 |
show ?case
|
|
682 |
proof (cases "map_of ys (snd x)")
|
|
683 |
case None
|
|
684 |
with Cons show ?thesis by simp
|
|
685 |
next
|
|
686 |
case (Some v)
|
|
687 |
with Cons dom_compose [of xs ys] show ?thesis
|
|
688 |
by (auto)
|
|
689 |
qed
|
|
690 |
qed
|
|
691 |
|
|
692 |
lemma compose_delete_twist: "(compose (delete k xs) ys) = delete k (compose xs ys)"
|
|
693 |
proof (induct xs ys rule: compose_induct)
|
|
694 |
case Nil thus ?case by simp
|
|
695 |
next
|
|
696 |
case (Cons x xs)
|
|
697 |
show ?case
|
|
698 |
proof (cases "map_of ys (snd x)")
|
|
699 |
case None
|
|
700 |
with Cons have
|
|
701 |
hyp: "compose (delete k (delete (fst x) xs)) ys =
|
|
702 |
delete k (compose (delete (fst x) xs) ys)"
|
|
703 |
by simp
|
|
704 |
show ?thesis
|
|
705 |
proof (cases "fst x = k")
|
|
706 |
case True
|
|
707 |
with None hyp
|
|
708 |
show ?thesis
|
|
709 |
by (simp add: delete_idem)
|
|
710 |
next
|
|
711 |
case False
|
|
712 |
from None False hyp
|
|
713 |
show ?thesis
|
|
714 |
by (simp add: delete_twist)
|
|
715 |
qed
|
|
716 |
next
|
|
717 |
case (Some v)
|
|
718 |
with Cons have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" by simp
|
|
719 |
with Some show ?thesis
|
|
720 |
by simp
|
|
721 |
qed
|
|
722 |
qed
|
|
723 |
|
|
724 |
lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys"
|
|
725 |
by (induct xs ys rule: compose_induct)
|
|
726 |
(auto simp add: map_of_clearjunk split: option.splits)
|
|
727 |
|
|
728 |
lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys"
|
|
729 |
by (induct xs rule: clearjunk.induct)
|
|
730 |
(auto split: option.splits simp add: clearjunk_delete delete_idem
|
|
731 |
compose_delete_twist)
|
|
732 |
|
|
733 |
lemma compose_empty [simp]:
|
|
734 |
"compose xs [] = []"
|
|
735 |
by (induct xs rule: compose_induct [where ys="[]"]) auto
|
|
736 |
|
|
737 |
|
|
738 |
lemma compose_Some_iff:
|
|
739 |
"(map_of (compose xs ys) k = Some v) =
|
|
740 |
(\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)"
|
|
741 |
by (simp add: compose_conv map_comp_Some_iff)
|
|
742 |
|
|
743 |
lemma map_comp_None_iff:
|
|
744 |
"(map_of (compose xs ys) k = None) =
|
|
745 |
(map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None)) "
|
|
746 |
by (simp add: compose_conv map_comp_None_iff)
|
|
747 |
|
|
748 |
|
|
749 |
(* ******************************************************************************** *)
|
|
750 |
subsection {* @{const restrict} *}
|
|
751 |
(* ******************************************************************************** *)
|
|
752 |
|
|
753 |
lemma restrict_simps [simp]:
|
|
754 |
"restrict A [] = []"
|
|
755 |
"restrict A (p#ps) = (if fst p \<in> A then p#restrict A ps else restrict A ps)"
|
|
756 |
by (auto simp add: restrict_def)
|
|
757 |
|
|
758 |
lemma distinct_restr: "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))"
|
|
759 |
by (induct al) (auto simp add: restrict_def)
|
|
760 |
|
|
761 |
lemma restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k"
|
|
762 |
apply (induct al)
|
|
763 |
apply (simp add: restrict_def)
|
|
764 |
apply (cases "k\<in>A")
|
|
765 |
apply (auto simp add: restrict_def)
|
|
766 |
done
|
|
767 |
|
|
768 |
lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)"
|
|
769 |
by (rule ext) (rule restr_conv)
|
|
770 |
|
|
771 |
lemma restr_empty [simp]:
|
|
772 |
"restrict {} al = []"
|
|
773 |
"restrict A [] = []"
|
|
774 |
by (induct al) (auto simp add: restrict_def)
|
|
775 |
|
|
776 |
lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x"
|
|
777 |
by (simp add: restr_conv')
|
|
778 |
|
|
779 |
lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None"
|
|
780 |
by (simp add: restr_conv')
|
|
781 |
|
|
782 |
lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A"
|
|
783 |
by (induct al) (auto simp add: restrict_def)
|
|
784 |
|
|
785 |
lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al"
|
|
786 |
by (induct al) (auto simp add: restrict_def)
|
|
787 |
|
|
788 |
lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al"
|
|
789 |
by (induct al) (auto simp add: restrict_def)
|
|
790 |
|
|
791 |
lemma restr_update[simp]:
|
|
792 |
"map_of (restrict D (update x y al)) =
|
|
793 |
map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))"
|
|
794 |
by (simp add: restr_conv' update_conv')
|
|
795 |
|
|
796 |
lemma restr_delete [simp]:
|
|
797 |
"(delete x (restrict D al)) =
|
|
798 |
(if x\<in> D then restrict (D - {x}) al else restrict D al)"
|
|
799 |
proof (induct al)
|
|
800 |
case Nil thus ?case by simp
|
|
801 |
next
|
|
802 |
case (Cons a al)
|
|
803 |
show ?case
|
|
804 |
proof (cases "x \<in> D")
|
|
805 |
case True
|
|
806 |
note x_D = this
|
|
807 |
with Cons have hyp: "delete x (restrict D al) = restrict (D - {x}) al"
|
|
808 |
by simp
|
|
809 |
show ?thesis
|
|
810 |
proof (cases "fst a = x")
|
|
811 |
case True
|
|
812 |
from Cons.hyps
|
|
813 |
show ?thesis
|
|
814 |
using x_D True
|
|
815 |
by simp
|
|
816 |
next
|
|
817 |
case False
|
|
818 |
note not_fst_a_x = this
|
|
819 |
show ?thesis
|
|
820 |
proof (cases "fst a \<in> D")
|
|
821 |
case True
|
|
822 |
with not_fst_a_x
|
|
823 |
have "delete x (restrict D (a#al)) = a#(delete x (restrict D al))"
|
|
824 |
by (cases a) (simp add: restrict_def)
|
|
825 |
also from not_fst_a_x True hyp have "\<dots> = restrict (D - {x}) (a # al)"
|
|
826 |
by (cases a) (simp add: restrict_def)
|
|
827 |
finally show ?thesis
|
|
828 |
using x_D by simp
|
|
829 |
next
|
|
830 |
case False
|
|
831 |
hence "delete x (restrict D (a#al)) = delete x (restrict D al)"
|
|
832 |
by (cases a) (simp add: restrict_def)
|
|
833 |
moreover from False not_fst_a_x
|
|
834 |
have "restrict (D - {x}) (a # al) = restrict (D - {x}) al"
|
|
835 |
by (cases a) (simp add: restrict_def)
|
|
836 |
ultimately
|
|
837 |
show ?thesis using x_D hyp by simp
|
|
838 |
qed
|
|
839 |
qed
|
|
840 |
next
|
|
841 |
case False
|
|
842 |
from False Cons show ?thesis
|
|
843 |
by simp
|
|
844 |
qed
|
|
845 |
qed
|
|
846 |
|
|
847 |
lemma update_restr:
|
|
848 |
"map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
|
|
849 |
by (simp add: update_conv' restr_conv') (rule fun_upd_restrict)
|
|
850 |
|
|
851 |
lemma upate_restr_conv[simp]:
|
|
852 |
"x \<in> D \<Longrightarrow>
|
|
853 |
map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))"
|
|
854 |
by (simp add: update_conv' restr_conv')
|
|
855 |
|
|
856 |
lemma restr_updates[simp]: "
|
|
857 |
\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk>
|
|
858 |
\<Longrightarrow> map_of (restrict D (updates xs ys al)) =
|
|
859 |
map_of (updates xs ys (restrict (D - set xs) al))"
|
|
860 |
by (simp add: updates_conv' restr_conv')
|
|
861 |
|
|
862 |
lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)"
|
|
863 |
by (induct ps) auto
|
|
864 |
|
|
865 |
lemma clearjunk_restrict:
|
|
866 |
"clearjunk (restrict A al) = restrict A (clearjunk al)"
|
|
867 |
by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist)
|
|
868 |
|
|
869 |
end
|