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