author | bulwahn |
Wed, 14 Dec 2011 15:56:37 +0100 | |
changeset 45869 | bd5ec56d2a0c |
parent 45868 | 397116757273 |
permissions | -rw-r--r-- |
44913 | 1 |
(* Title: HOL/Library/AList.thy |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
2 |
Author: Norbert Schirmer, Tobias Nipkow, Martin Wildmoser, TU Muenchen |
19234 | 3 |
*) |
4 |
||
44897
787983a08bfb
moving connection of association lists to Mappings into a separate theory
bulwahn
parents:
39921
diff
changeset
|
5 |
header {* Implementation of Association Lists *} |
19234 | 6 |
|
44913 | 7 |
theory AList |
44897
787983a08bfb
moving connection of association lists to Mappings into a separate theory
bulwahn
parents:
39921
diff
changeset
|
8 |
imports Main More_List |
19234 | 9 |
begin |
10 |
||
22740 | 11 |
text {* |
12 |
The operations preserve distinctness of keys and |
|
13 |
function @{term "clearjunk"} distributes over them. Since |
|
14 |
@{term clearjunk} enforces distinctness of keys it can be used |
|
15 |
to establish the invariant, e.g. for inductive proofs. |
|
16 |
*} |
|
19234 | 17 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
18 |
subsection {* @{text update} and @{text updates} *} |
19323 | 19 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
20 |
primrec update :: "'key \<Rightarrow> 'val \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
22740 | 21 |
"update k v [] = [(k, v)]" |
22 |
| "update k v (p#ps) = (if fst p = k then (k, v) # ps else p # update k v ps)" |
|
19234 | 23 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
24 |
lemma update_conv': "map_of (update k v al) = (map_of al)(k\<mapsto>v)" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
25 |
by (induct al) (auto simp add: fun_eq_iff) |
23373 | 26 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
27 |
corollary update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
28 |
by (simp add: update_conv') |
19234 | 29 |
|
30 |
lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al" |
|
31 |
by (induct al) auto |
|
32 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
33 |
lemma update_keys: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
34 |
"map fst (update k v al) = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
35 |
(if k \<in> set (map fst al) then map fst al else map fst al @ [k])" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
36 |
by (induct al) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
37 |
|
19234 | 38 |
lemma distinct_update: |
39 |
assumes "distinct (map fst al)" |
|
40 |
shows "distinct (map fst (update k v al))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
41 |
using assms by (simp add: update_keys) |
19234 | 42 |
|
43 |
lemma update_filter: |
|
23281 | 44 |
"a\<noteq>k \<Longrightarrow> update k v [q\<leftarrow>ps . fst q \<noteq> a] = [q\<leftarrow>update k v ps . fst q \<noteq> a]" |
19234 | 45 |
by (induct ps) auto |
46 |
||
47 |
lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al" |
|
48 |
by (induct al) auto |
|
49 |
||
50 |
lemma update_nonempty [simp]: "update k v al \<noteq> []" |
|
51 |
by (induct al) auto |
|
52 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
53 |
lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v = v'" |
20503 | 54 |
proof (induct al arbitrary: al') |
19234 | 55 |
case Nil thus ?case |
56 |
by (cases al') (auto split: split_if_asm) |
|
57 |
next |
|
58 |
case Cons thus ?case |
|
59 |
by (cases al') (auto split: split_if_asm) |
|
60 |
qed |
|
61 |
||
62 |
lemma update_last [simp]: "update k v (update k v' al) = update k v al" |
|
63 |
by (induct al) auto |
|
64 |
||
65 |
text {* Note that the lists are not necessarily the same: |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
66 |
@{term "update k v (update k' v' []) = [(k', v'), (k, v)]"} and |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
67 |
@{term "update k' v' (update k v []) = [(k, v), (k', v')]"}.*} |
19234 | 68 |
lemma update_swap: "k\<noteq>k' |
69 |
\<Longrightarrow> map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))" |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
70 |
by (simp add: update_conv' fun_eq_iff) |
19234 | 71 |
|
72 |
lemma update_Some_unfold: |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
73 |
"map_of (update k v al) x = Some y \<longleftrightarrow> |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
74 |
x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y" |
19234 | 75 |
by (simp add: update_conv' map_upd_Some_unfold) |
76 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
77 |
lemma image_update [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
78 |
"x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A" |
19234 | 79 |
by (simp add: update_conv' image_map_upd) |
80 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
81 |
definition updates :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
37458 | 82 |
"updates ks vs = More_List.fold (prod_case update) (zip ks vs)" |
19234 | 83 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
84 |
lemma updates_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
85 |
"updates [] vs ps = ps" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
86 |
"updates ks [] ps = ps" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
87 |
"updates (k#ks) (v#vs) ps = updates ks vs (update k v ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
88 |
by (simp_all add: updates_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
89 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
90 |
lemma updates_key_simp [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
91 |
"updates (k # ks) vs ps = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
92 |
(case vs of [] \<Rightarrow> ps | v # vs \<Rightarrow> updates ks vs (update k v ps))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
93 |
by (cases vs) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
94 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
95 |
lemma updates_conv': "map_of (updates ks vs al) = (map_of al)(ks[\<mapsto>]vs)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
96 |
proof - |
37458 | 97 |
have "map_of \<circ> More_List.fold (prod_case update) (zip ks vs) = |
98 |
More_List.fold (\<lambda>(k, v) f. f(k \<mapsto> v)) (zip ks vs) \<circ> map_of" |
|
39921 | 99 |
by (rule fold_commute) (auto simp add: fun_eq_iff update_conv') |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
100 |
then show ?thesis by (auto simp add: updates_def fun_eq_iff map_upds_fold_map_upd foldl_fold split_def) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
101 |
qed |
19234 | 102 |
|
103 |
lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
104 |
by (simp add: updates_conv') |
19234 | 105 |
|
106 |
lemma distinct_updates: |
|
107 |
assumes "distinct (map fst al)" |
|
108 |
shows "distinct (map fst (updates ks vs al))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
109 |
proof - |
37458 | 110 |
have "distinct (More_List.fold |
111 |
(\<lambda>(k, v) al. if k \<in> set al then al else al @ [k]) |
|
112 |
(zip ks vs) (map fst al))" |
|
113 |
by (rule fold_invariant [of "zip ks vs" "\<lambda>_. True"]) (auto intro: assms) |
|
114 |
moreover have "map fst \<circ> More_List.fold (prod_case update) (zip ks vs) = |
|
115 |
More_List.fold (\<lambda>(k, v) al. if k \<in> set al then al else al @ [k]) (zip ks vs) \<circ> map fst" |
|
39921 | 116 |
by (rule fold_commute) (simp add: update_keys split_def prod_case_beta comp_def) |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
117 |
ultimately show ?thesis by (simp add: updates_def fun_eq_iff) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
118 |
qed |
19234 | 119 |
|
120 |
lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow> |
|
121 |
updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)" |
|
20503 | 122 |
by (induct ks arbitrary: vs al) (auto split: list.splits) |
19234 | 123 |
|
124 |
lemma updates_list_update_drop[simp]: |
|
125 |
"\<lbrakk>size ks \<le> i; i < size vs\<rbrakk> |
|
126 |
\<Longrightarrow> updates ks (vs[i:=v]) al = updates ks vs al" |
|
20503 | 127 |
by (induct ks arbitrary: al vs i) (auto split:list.splits nat.splits) |
19234 | 128 |
|
129 |
lemma update_updates_conv_if: " |
|
130 |
map_of (updates xs ys (update x y al)) = |
|
131 |
map_of (if x \<in> set(take (length ys) xs) then updates xs ys al |
|
132 |
else (update x y (updates xs ys al)))" |
|
133 |
by (simp add: updates_conv' update_conv' map_upd_upds_conv_if) |
|
134 |
||
135 |
lemma updates_twist [simp]: |
|
136 |
"k \<notin> set ks \<Longrightarrow> |
|
137 |
map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))" |
|
138 |
by (simp add: updates_conv' update_conv' map_upds_twist) |
|
139 |
||
140 |
lemma updates_apply_notin[simp]: |
|
141 |
"k \<notin> set ks ==> map_of (updates ks vs al) k = map_of al k" |
|
142 |
by (simp add: updates_conv) |
|
143 |
||
144 |
lemma updates_append_drop[simp]: |
|
145 |
"size xs = size ys \<Longrightarrow> updates (xs@zs) ys al = updates xs ys al" |
|
20503 | 146 |
by (induct xs arbitrary: ys al) (auto split: list.splits) |
19234 | 147 |
|
148 |
lemma updates_append2_drop[simp]: |
|
149 |
"size xs = size ys \<Longrightarrow> updates xs (ys@zs) al = updates xs ys al" |
|
20503 | 150 |
by (induct xs arbitrary: ys al) (auto split: list.splits) |
19234 | 151 |
|
23373 | 152 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
153 |
subsection {* @{text delete} *} |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
154 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
155 |
definition delete :: "'key \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
156 |
delete_eq: "delete k = filter (\<lambda>(k', _). k \<noteq> k')" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
157 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
158 |
lemma delete_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
159 |
"delete k [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
160 |
"delete k (p#ps) = (if fst p = k then delete k ps else p # delete k ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
161 |
by (auto simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
162 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
163 |
lemma delete_conv': "map_of (delete k al) = (map_of al)(k := None)" |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
164 |
by (induct al) (auto simp add: fun_eq_iff) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
165 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
166 |
corollary delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
167 |
by (simp add: delete_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
168 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
169 |
lemma delete_keys: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
170 |
"map fst (delete k al) = removeAll k (map fst al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
171 |
by (simp add: delete_eq removeAll_filter_not_eq filter_map split_def comp_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
172 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
173 |
lemma distinct_delete: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
174 |
assumes "distinct (map fst al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
175 |
shows "distinct (map fst (delete k al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
176 |
using assms by (simp add: delete_keys distinct_removeAll) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
177 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
178 |
lemma delete_id [simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
179 |
by (auto simp add: image_iff delete_eq filter_id_conv) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
180 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
181 |
lemma delete_idem: "delete k (delete k al) = delete k al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
182 |
by (simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
183 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
184 |
lemma map_of_delete [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
185 |
"k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
186 |
by (simp add: delete_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
187 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
188 |
lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
189 |
by (auto simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
190 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
191 |
lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
192 |
by (auto simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
193 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
194 |
lemma delete_update_same: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
195 |
"delete k (update k v al) = delete k al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
196 |
by (induct al) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
197 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
198 |
lemma delete_update: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
199 |
"k \<noteq> l \<Longrightarrow> delete l (update k v al) = update k v (delete l al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
200 |
by (induct al) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
201 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
202 |
lemma delete_twist: "delete x (delete y al) = delete y (delete x al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
203 |
by (simp add: delete_eq conj_commute) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
204 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
205 |
lemma length_delete_le: "length (delete k al) \<le> length al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
206 |
by (simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
207 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
208 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
209 |
subsection {* @{text restrict} *} |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
210 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
211 |
definition restrict :: "'key set \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
212 |
restrict_eq: "restrict A = filter (\<lambda>(k, v). k \<in> A)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
213 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
214 |
lemma restr_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
215 |
"restrict A [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
216 |
"restrict A (p#ps) = (if fst p \<in> A then p # restrict A ps else restrict A ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
217 |
by (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
218 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
219 |
lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
220 |
proof |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
221 |
fix k |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
222 |
show "map_of (restrict A al) k = ((map_of al)|` A) k" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
223 |
by (induct al) (simp, cases "k \<in> A", auto) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
224 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
225 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
226 |
corollary restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
227 |
by (simp add: restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
228 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
229 |
lemma distinct_restr: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
230 |
"distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
231 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
232 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
233 |
lemma restr_empty [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
234 |
"restrict {} al = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
235 |
"restrict A [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
236 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
237 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
238 |
lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
239 |
by (simp add: restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
240 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
241 |
lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
242 |
by (simp add: restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
243 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
244 |
lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
245 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
246 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
247 |
lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
248 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
249 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
250 |
lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
251 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
252 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
253 |
lemma restr_update[simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
254 |
"map_of (restrict D (update x y al)) = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
255 |
map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
256 |
by (simp add: restr_conv' update_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
257 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
258 |
lemma restr_delete [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
259 |
"(delete x (restrict D al)) = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
260 |
(if x \<in> D then restrict (D - {x}) al else restrict D al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
261 |
apply (simp add: delete_eq restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
262 |
apply (auto simp add: split_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
263 |
proof - |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
264 |
have "\<And>y. y \<noteq> x \<longleftrightarrow> x \<noteq> y" by auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
265 |
then show "[p \<leftarrow> al. fst p \<in> D \<and> x \<noteq> fst p] = [p \<leftarrow> al. fst p \<in> D \<and> fst p \<noteq> x]" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
266 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
267 |
assume "x \<notin> D" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
268 |
then have "\<And>y. y \<in> D \<longleftrightarrow> y \<in> D \<and> x \<noteq> y" by auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
269 |
then show "[p \<leftarrow> al . fst p \<in> D \<and> x \<noteq> fst p] = [p \<leftarrow> al . fst p \<in> D]" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
270 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
271 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
272 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
273 |
lemma update_restr: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
274 |
"map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
275 |
by (simp add: update_conv' restr_conv') (rule fun_upd_restrict) |
19234 | 276 |
|
45867 | 277 |
lemma update_restr_conv [simp]: |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
278 |
"x \<in> D \<Longrightarrow> |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
279 |
map_of (update x y (restrict D al)) = map_of (update x y (restrict (D-{x}) al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
280 |
by (simp add: update_conv' restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
281 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
282 |
lemma restr_updates [simp]: " |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
283 |
\<lbrakk> length xs = length ys; set xs \<subseteq> D \<rbrakk> |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
284 |
\<Longrightarrow> map_of (restrict D (updates xs ys al)) = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
285 |
map_of (updates xs ys (restrict (D - set xs) al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
286 |
by (simp add: updates_conv' restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
287 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
288 |
lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
289 |
by (induct ps) auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
290 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
291 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
292 |
subsection {* @{text clearjunk} *} |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
293 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
294 |
function clearjunk :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
295 |
"clearjunk [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
296 |
| "clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
297 |
by pat_completeness auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
298 |
termination by (relation "measure length") |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
299 |
(simp_all add: less_Suc_eq_le length_delete_le) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
300 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
301 |
lemma map_of_clearjunk: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
302 |
"map_of (clearjunk al) = map_of al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
303 |
by (induct al rule: clearjunk.induct) |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
304 |
(simp_all add: fun_eq_iff) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
305 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
306 |
lemma clearjunk_keys_set: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
307 |
"set (map fst (clearjunk al)) = set (map fst al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
308 |
by (induct al rule: clearjunk.induct) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
309 |
(simp_all add: delete_keys) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
310 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
311 |
lemma dom_clearjunk: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
312 |
"fst ` set (clearjunk al) = fst ` set al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
313 |
using clearjunk_keys_set by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
314 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
315 |
lemma distinct_clearjunk [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
316 |
"distinct (map fst (clearjunk al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
317 |
by (induct al rule: clearjunk.induct) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
318 |
(simp_all del: set_map add: clearjunk_keys_set delete_keys) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
319 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
320 |
lemma ran_clearjunk: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
321 |
"ran (map_of (clearjunk al)) = ran (map_of al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
322 |
by (simp add: map_of_clearjunk) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
323 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
324 |
lemma ran_map_of: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
325 |
"ran (map_of al) = snd ` set (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
326 |
proof - |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
327 |
have "ran (map_of al) = ran (map_of (clearjunk al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
328 |
by (simp add: ran_clearjunk) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
329 |
also have "\<dots> = snd ` set (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
330 |
by (simp add: ran_distinct) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
331 |
finally show ?thesis . |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
332 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
333 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
334 |
lemma clearjunk_update: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
335 |
"clearjunk (update k v al) = update k v (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
336 |
by (induct al rule: clearjunk.induct) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
337 |
(simp_all add: delete_update) |
19234 | 338 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
339 |
lemma clearjunk_updates: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
340 |
"clearjunk (updates ks vs al) = updates ks vs (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
341 |
proof - |
37458 | 342 |
have "clearjunk \<circ> More_List.fold (prod_case update) (zip ks vs) = |
343 |
More_List.fold (prod_case update) (zip ks vs) \<circ> clearjunk" |
|
39921 | 344 |
by (rule fold_commute) (simp add: clearjunk_update prod_case_beta o_def) |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
345 |
then show ?thesis by (simp add: updates_def fun_eq_iff) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
346 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
347 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
348 |
lemma clearjunk_delete: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
349 |
"clearjunk (delete x al) = delete x (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
350 |
by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
351 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
352 |
lemma clearjunk_restrict: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
353 |
"clearjunk (restrict A al) = restrict A (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
354 |
by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
355 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
356 |
lemma distinct_clearjunk_id [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
357 |
"distinct (map fst al) \<Longrightarrow> clearjunk al = al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
358 |
by (induct al rule: clearjunk.induct) auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
359 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
360 |
lemma clearjunk_idem: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
361 |
"clearjunk (clearjunk al) = clearjunk al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
362 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
363 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
364 |
lemma length_clearjunk: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
365 |
"length (clearjunk al) \<le> length al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
366 |
proof (induct al rule: clearjunk.induct [case_names Nil Cons]) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
367 |
case Nil then show ?case by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
368 |
next |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
369 |
case (Cons kv al) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
370 |
moreover have "length (delete (fst kv) al) \<le> length al" by (fact length_delete_le) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
371 |
ultimately have "length (clearjunk (delete (fst kv) al)) \<le> length al" by (rule order_trans) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
372 |
then show ?case by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
373 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
374 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
375 |
lemma delete_map: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
376 |
assumes "\<And>kv. fst (f kv) = fst kv" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
377 |
shows "delete k (map f ps) = map f (delete k ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
378 |
by (simp add: delete_eq filter_map comp_def split_def assms) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
379 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
380 |
lemma clearjunk_map: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
381 |
assumes "\<And>kv. fst (f kv) = fst kv" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
382 |
shows "clearjunk (map f ps) = map f (clearjunk ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
383 |
by (induct ps rule: clearjunk.induct [case_names Nil Cons]) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
384 |
(simp_all add: clearjunk_delete delete_map assms) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
385 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
386 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
387 |
subsection {* @{text map_ran} *} |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
388 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
389 |
definition map_ran :: "('key \<Rightarrow> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
390 |
"map_ran f = map (\<lambda>(k, v). (k, f k v))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
391 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
392 |
lemma map_ran_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
393 |
"map_ran f [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
394 |
"map_ran f ((k, v) # ps) = (k, f k v) # map_ran f ps" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
395 |
by (simp_all add: map_ran_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
396 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
397 |
lemma dom_map_ran: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
398 |
"fst ` set (map_ran f al) = fst ` set al" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
399 |
by (simp add: map_ran_def image_image split_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
400 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
401 |
lemma map_ran_conv: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
402 |
"map_of (map_ran f al) k = Option.map (f k) (map_of al k)" |
19234 | 403 |
by (induct al) auto |
404 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
405 |
lemma distinct_map_ran: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
406 |
"distinct (map fst al) \<Longrightarrow> distinct (map fst (map_ran f al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
407 |
by (simp add: map_ran_def split_def comp_def) |
19234 | 408 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
409 |
lemma map_ran_filter: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
410 |
"map_ran f [p\<leftarrow>ps. fst p \<noteq> a] = [p\<leftarrow>map_ran f ps. fst p \<noteq> a]" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
411 |
by (simp add: map_ran_def filter_map split_def comp_def) |
19234 | 412 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
413 |
lemma clearjunk_map_ran: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
414 |
"clearjunk (map_ran f al) = map_ran f (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
415 |
by (simp add: map_ran_def split_def clearjunk_map) |
19234 | 416 |
|
23373 | 417 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
418 |
subsection {* @{text merge} *} |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
419 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
420 |
definition merge :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" where |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
421 |
"merge qs ps = foldr (\<lambda>(k, v). update k v) ps qs" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
422 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
423 |
lemma merge_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
424 |
"merge qs [] = qs" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
425 |
"merge qs (p#ps) = update (fst p) (snd p) (merge qs ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
426 |
by (simp_all add: merge_def split_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
427 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
428 |
lemma merge_updates: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
429 |
"merge qs ps = updates (rev (map fst ps)) (rev (map snd ps)) qs" |
37591 | 430 |
by (simp add: merge_def updates_def foldr_fold_rev zip_rev zip_map_fst_snd) |
19234 | 431 |
|
432 |
lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys" |
|
20503 | 433 |
by (induct ys arbitrary: xs) (auto simp add: dom_update) |
19234 | 434 |
|
435 |
lemma distinct_merge: |
|
436 |
assumes "distinct (map fst xs)" |
|
437 |
shows "distinct (map fst (merge xs ys))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
438 |
using assms by (simp add: merge_updates distinct_updates) |
19234 | 439 |
|
440 |
lemma clearjunk_merge: |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
441 |
"clearjunk (merge xs ys) = merge (clearjunk xs) ys" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
442 |
by (simp add: merge_updates clearjunk_updates) |
19234 | 443 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
444 |
lemma merge_conv': |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
445 |
"map_of (merge xs ys) = map_of xs ++ map_of ys" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
446 |
proof - |
37458 | 447 |
have "map_of \<circ> More_List.fold (prod_case update) (rev ys) = |
448 |
More_List.fold (\<lambda>(k, v) m. m(k \<mapsto> v)) (rev ys) \<circ> map_of" |
|
39921 | 449 |
by (rule fold_commute) (simp add: update_conv' prod_case_beta split_def fun_eq_iff) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
450 |
then show ?thesis |
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
451 |
by (simp add: merge_def map_add_map_of_foldr foldr_fold_rev fun_eq_iff) |
19234 | 452 |
qed |
453 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
454 |
corollary merge_conv: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
455 |
"map_of (merge xs ys) k = (map_of xs ++ map_of ys) k" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
456 |
by (simp add: merge_conv') |
19234 | 457 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
458 |
lemma merge_empty: "map_of (merge [] ys) = map_of ys" |
19234 | 459 |
by (simp add: merge_conv') |
460 |
||
461 |
lemma merge_assoc[simp]: "map_of (merge m1 (merge m2 m3)) = |
|
462 |
map_of (merge (merge m1 m2) m3)" |
|
463 |
by (simp add: merge_conv') |
|
464 |
||
465 |
lemma merge_Some_iff: |
|
466 |
"(map_of (merge m n) k = Some x) = |
|
467 |
(map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x)" |
|
468 |
by (simp add: merge_conv' map_add_Some_iff) |
|
469 |
||
45605 | 470 |
lemmas merge_SomeD [dest!] = merge_Some_iff [THEN iffD1] |
19234 | 471 |
|
472 |
lemma merge_find_right[simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v" |
|
473 |
by (simp add: merge_conv') |
|
474 |
||
475 |
lemma merge_None [iff]: |
|
476 |
"(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)" |
|
477 |
by (simp add: merge_conv') |
|
478 |
||
479 |
lemma merge_upd[simp]: |
|
480 |
"map_of (merge m (update k v n)) = map_of (update k v (merge m n))" |
|
481 |
by (simp add: update_conv' merge_conv') |
|
482 |
||
483 |
lemma merge_updatess[simp]: |
|
484 |
"map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))" |
|
485 |
by (simp add: updates_conv' merge_conv') |
|
486 |
||
487 |
lemma merge_append: "map_of (xs@ys) = map_of (merge ys xs)" |
|
488 |
by (simp add: merge_conv') |
|
489 |
||
23373 | 490 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
491 |
subsection {* @{text compose} *} |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
492 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
493 |
function compose :: "('key \<times> 'a) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('key \<times> 'b) list" where |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
494 |
"compose [] ys = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
495 |
| "compose (x#xs) ys = (case map_of ys (snd x) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
496 |
of None \<Rightarrow> compose (delete (fst x) xs) ys |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
497 |
| Some v \<Rightarrow> (fst x, v) # compose xs ys)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
498 |
by pat_completeness auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
499 |
termination by (relation "measure (length \<circ> fst)") |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
500 |
(simp_all add: less_Suc_eq_le length_delete_le) |
19234 | 501 |
|
502 |
lemma compose_first_None [simp]: |
|
503 |
assumes "map_of xs k = None" |
|
504 |
shows "map_of (compose xs ys) k = None" |
|
23373 | 505 |
using assms by (induct xs ys rule: compose.induct) |
22916 | 506 |
(auto split: option.splits split_if_asm) |
19234 | 507 |
|
508 |
lemma compose_conv: |
|
509 |
shows "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k" |
|
22916 | 510 |
proof (induct xs ys rule: compose.induct) |
511 |
case 1 then show ?case by simp |
|
19234 | 512 |
next |
22916 | 513 |
case (2 x xs ys) show ?case |
19234 | 514 |
proof (cases "map_of ys (snd x)") |
22916 | 515 |
case None with 2 |
19234 | 516 |
have hyp: "map_of (compose (delete (fst x) xs) ys) k = |
517 |
(map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k" |
|
518 |
by simp |
|
519 |
show ?thesis |
|
520 |
proof (cases "fst x = k") |
|
521 |
case True |
|
522 |
from True delete_notin_dom [of k xs] |
|
523 |
have "map_of (delete (fst x) xs) k = None" |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
524 |
by (simp add: map_of_eq_None_iff) |
19234 | 525 |
with hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
526 |
using True None |
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
527 |
by simp |
19234 | 528 |
next |
529 |
case False |
|
530 |
from False have "map_of (delete (fst x) xs) k = map_of xs k" |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
531 |
by simp |
19234 | 532 |
with hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
533 |
using False None |
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
534 |
by (simp add: map_comp_def) |
19234 | 535 |
qed |
536 |
next |
|
537 |
case (Some v) |
|
22916 | 538 |
with 2 |
19234 | 539 |
have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k" |
540 |
by simp |
|
541 |
with Some show ?thesis |
|
542 |
by (auto simp add: map_comp_def) |
|
543 |
qed |
|
544 |
qed |
|
545 |
||
546 |
lemma compose_conv': |
|
547 |
shows "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)" |
|
548 |
by (rule ext) (rule compose_conv) |
|
549 |
||
550 |
lemma compose_first_Some [simp]: |
|
551 |
assumes "map_of xs k = Some v" |
|
552 |
shows "map_of (compose xs ys) k = map_of ys v" |
|
23373 | 553 |
using assms by (simp add: compose_conv) |
19234 | 554 |
|
555 |
lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs" |
|
22916 | 556 |
proof (induct xs ys rule: compose.induct) |
557 |
case 1 thus ?case by simp |
|
19234 | 558 |
next |
22916 | 559 |
case (2 x xs ys) |
19234 | 560 |
show ?case |
561 |
proof (cases "map_of ys (snd x)") |
|
562 |
case None |
|
22916 | 563 |
with "2.hyps" |
19234 | 564 |
have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)" |
565 |
by simp |
|
566 |
also |
|
567 |
have "\<dots> \<subseteq> fst ` set xs" |
|
568 |
by (rule dom_delete_subset) |
|
569 |
finally show ?thesis |
|
570 |
using None |
|
571 |
by auto |
|
572 |
next |
|
573 |
case (Some v) |
|
22916 | 574 |
with "2.hyps" |
19234 | 575 |
have "fst ` set (compose xs ys) \<subseteq> fst ` set xs" |
576 |
by simp |
|
577 |
with Some show ?thesis |
|
578 |
by auto |
|
579 |
qed |
|
580 |
qed |
|
581 |
||
582 |
lemma distinct_compose: |
|
583 |
assumes "distinct (map fst xs)" |
|
584 |
shows "distinct (map fst (compose xs ys))" |
|
23373 | 585 |
using assms |
22916 | 586 |
proof (induct xs ys rule: compose.induct) |
587 |
case 1 thus ?case by simp |
|
19234 | 588 |
next |
22916 | 589 |
case (2 x xs ys) |
19234 | 590 |
show ?case |
591 |
proof (cases "map_of ys (snd x)") |
|
592 |
case None |
|
22916 | 593 |
with 2 show ?thesis by simp |
19234 | 594 |
next |
595 |
case (Some v) |
|
22916 | 596 |
with 2 dom_compose [of xs ys] show ?thesis |
19234 | 597 |
by (auto) |
598 |
qed |
|
599 |
qed |
|
600 |
||
601 |
lemma compose_delete_twist: "(compose (delete k xs) ys) = delete k (compose xs ys)" |
|
22916 | 602 |
proof (induct xs ys rule: compose.induct) |
603 |
case 1 thus ?case by simp |
|
19234 | 604 |
next |
22916 | 605 |
case (2 x xs ys) |
19234 | 606 |
show ?case |
607 |
proof (cases "map_of ys (snd x)") |
|
608 |
case None |
|
22916 | 609 |
with 2 have |
19234 | 610 |
hyp: "compose (delete k (delete (fst x) xs)) ys = |
611 |
delete k (compose (delete (fst x) xs) ys)" |
|
612 |
by simp |
|
613 |
show ?thesis |
|
614 |
proof (cases "fst x = k") |
|
615 |
case True |
|
616 |
with None hyp |
|
617 |
show ?thesis |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
618 |
by (simp add: delete_idem) |
19234 | 619 |
next |
620 |
case False |
|
621 |
from None False hyp |
|
622 |
show ?thesis |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
623 |
by (simp add: delete_twist) |
19234 | 624 |
qed |
625 |
next |
|
626 |
case (Some v) |
|
22916 | 627 |
with 2 have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" by simp |
19234 | 628 |
with Some show ?thesis |
629 |
by simp |
|
630 |
qed |
|
631 |
qed |
|
632 |
||
633 |
lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys" |
|
22916 | 634 |
by (induct xs ys rule: compose.induct) |
19234 | 635 |
(auto simp add: map_of_clearjunk split: option.splits) |
636 |
||
637 |
lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys" |
|
638 |
by (induct xs rule: clearjunk.induct) |
|
639 |
(auto split: option.splits simp add: clearjunk_delete delete_idem |
|
640 |
compose_delete_twist) |
|
641 |
||
642 |
lemma compose_empty [simp]: |
|
643 |
"compose xs [] = []" |
|
22916 | 644 |
by (induct xs) (auto simp add: compose_delete_twist) |
19234 | 645 |
|
646 |
lemma compose_Some_iff: |
|
647 |
"(map_of (compose xs ys) k = Some v) = |
|
648 |
(\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)" |
|
649 |
by (simp add: compose_conv map_comp_Some_iff) |
|
650 |
||
651 |
lemma map_comp_None_iff: |
|
652 |
"(map_of (compose xs ys) k = None) = |
|
653 |
(map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None)) " |
|
654 |
by (simp add: compose_conv map_comp_None_iff) |
|
655 |
||
45869 | 656 |
subsection {* @{text map_entry} *} |
657 |
||
658 |
fun map_entry :: "'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
659 |
where |
|
660 |
"map_entry k f [] = []" |
|
661 |
| "map_entry k f (p # ps) = (if fst p = k then (k, f (snd p)) # ps else p # map_entry k f ps)" |
|
662 |
||
663 |
lemma map_of_map_entry: |
|
664 |
"map_of (map_entry k f xs) = (map_of xs)(k := case map_of xs k of None => None | Some v' => Some (f v'))" |
|
665 |
by (induct xs) auto |
|
666 |
||
667 |
lemma dom_map_entry: |
|
668 |
"fst ` set (map_entry k f xs) = fst ` set xs" |
|
669 |
by (induct xs) auto |
|
670 |
||
671 |
lemma distinct_map_entry: |
|
672 |
assumes "distinct (map fst xs)" |
|
673 |
shows "distinct (map fst (map_entry k f xs))" |
|
674 |
using assms by (induct xs) (auto simp add: dom_map_entry) |
|
675 |
||
45868 | 676 |
subsection {* @{text map_default} *} |
677 |
||
678 |
fun map_default :: "'key \<Rightarrow> 'val \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
679 |
where |
|
680 |
"map_default k v f [] = [(k, v)]" |
|
681 |
| "map_default k v f (p # ps) = (if fst p = k then (k, f (snd p)) # ps else p # map_default k v f ps)" |
|
682 |
||
683 |
lemma map_of_map_default: |
|
684 |
"map_of (map_default k v f xs) = (map_of xs)(k := case map_of xs k of None => Some v | Some v' => Some (f v'))" |
|
685 |
by (induct xs) auto |
|
686 |
||
687 |
lemma dom_map_default: |
|
688 |
"fst ` set (map_default k v f xs) = insert k (fst ` set xs)" |
|
689 |
by (induct xs) auto |
|
690 |
||
691 |
lemma distinct_map_default: |
|
692 |
assumes "distinct (map fst xs)" |
|
693 |
shows "distinct (map fst (map_default k v f xs))" |
|
694 |
using assms by (induct xs) (auto simp add: dom_map_default) |
|
695 |
||
19234 | 696 |
end |