author | desharna |
Tue, 11 Jun 2024 10:27:35 +0200 | |
changeset 80345 | 7d4cd57cd955 |
parent 74157 | 8e2355ddce1b |
child 81306 | 42b9bd119d2b |
permissions | -rw-r--r-- |
46238
9ace9e5b79be
renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents:
46171
diff
changeset
|
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 |
||
60500 | 5 |
section \<open>Implementation of Association Lists\<close> |
19234 | 6 |
|
46238
9ace9e5b79be
renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents:
46171
diff
changeset
|
7 |
theory AList |
63462 | 8 |
imports Main |
19234 | 9 |
begin |
10 |
||
59943
e83ecf0a0ee1
more qualified names -- eliminated hide_const (open);
wenzelm
parents:
58881
diff
changeset
|
11 |
context |
e83ecf0a0ee1
more qualified names -- eliminated hide_const (open);
wenzelm
parents:
58881
diff
changeset
|
12 |
begin |
e83ecf0a0ee1
more qualified names -- eliminated hide_const (open);
wenzelm
parents:
58881
diff
changeset
|
13 |
|
60500 | 14 |
text \<open> |
56327 | 15 |
The operations preserve distinctness of keys and |
69593 | 16 |
function \<^term>\<open>clearjunk\<close> distributes over them. Since |
17 |
\<^term>\<open>clearjunk\<close> enforces distinctness of keys it can be used |
|
22740 | 18 |
to establish the invariant, e.g. for inductive proofs. |
60500 | 19 |
\<close> |
19234 | 20 |
|
61585 | 21 |
subsection \<open>\<open>update\<close> and \<open>updates\<close>\<close> |
19323 | 22 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
23 |
qualified primrec update :: "'key \<Rightarrow> 'val \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
63462 | 24 |
where |
25 |
"update k v [] = [(k, v)]" |
|
26 |
| "update k v (p # ps) = (if fst p = k then (k, v) # ps else p # update k v ps)" |
|
19234 | 27 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
28 |
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
|
29 |
by (induct al) (auto simp add: fun_eq_iff) |
23373 | 30 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
31 |
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
|
32 |
by (simp add: update_conv') |
19234 | 33 |
|
34 |
lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al" |
|
35 |
by (induct al) auto |
|
36 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
37 |
lemma update_keys: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
38 |
"map fst (update k v al) = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
39 |
(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
|
40 |
by (induct al) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
41 |
|
19234 | 42 |
lemma distinct_update: |
56327 | 43 |
assumes "distinct (map fst al)" |
19234 | 44 |
shows "distinct (map fst (update k v al))" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
45 |
using assms by (simp add: update_keys) |
19234 | 46 |
|
56327 | 47 |
lemma update_filter: |
68386 | 48 |
"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 | 49 |
by (induct ps) auto |
50 |
||
51 |
lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al" |
|
52 |
by (induct al) auto |
|
53 |
||
54 |
lemma update_nonempty [simp]: "update k v al \<noteq> []" |
|
55 |
by (induct al) auto |
|
56 |
||
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
57 |
lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v = v'" |
56327 | 58 |
proof (induct al arbitrary: al') |
59 |
case Nil |
|
60 |
then show ?case |
|
62390 | 61 |
by (cases al') (auto split: if_split_asm) |
19234 | 62 |
next |
56327 | 63 |
case Cons |
64 |
then show ?case |
|
62390 | 65 |
by (cases al') (auto split: if_split_asm) |
19234 | 66 |
qed |
67 |
||
68 |
lemma update_last [simp]: "update k v (update k v' al) = update k v al" |
|
69 |
by (induct al) auto |
|
70 |
||
60500 | 71 |
text \<open>Note that the lists are not necessarily the same: |
69593 | 72 |
\<^term>\<open>update k v (update k' v' []) = [(k', v'), (k, v)]\<close> and |
73 |
\<^term>\<open>update k' v' (update k v []) = [(k, v), (k', v')]\<close>.\<close> |
|
56327 | 74 |
|
75 |
lemma update_swap: |
|
63476 | 76 |
"k \<noteq> k' \<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
|
77 |
by (simp add: update_conv' fun_eq_iff) |
19234 | 78 |
|
56327 | 79 |
lemma update_Some_unfold: |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
80 |
"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
|
81 |
x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y" |
19234 | 82 |
by (simp add: update_conv' map_upd_Some_unfold) |
83 |
||
63462 | 84 |
lemma image_update [simp]: "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A" |
69661 | 85 |
by (auto simp add: update_conv') |
19234 | 86 |
|
63476 | 87 |
qualified definition updates :: |
88 |
"'key list \<Rightarrow> 'val list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
56327 | 89 |
where "updates ks vs = fold (case_prod update) (zip ks vs)" |
19234 | 90 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
91 |
lemma updates_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
92 |
"updates [] vs ps = ps" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
93 |
"updates ks [] ps = ps" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
94 |
"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
|
95 |
by (simp_all add: updates_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
96 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
97 |
lemma updates_key_simp [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
98 |
"updates (k # ks) vs ps = |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
99 |
(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
|
100 |
by (cases vs) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
101 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
102 |
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
|
103 |
proof - |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
104 |
have "map_of \<circ> fold (case_prod update) (zip ks vs) = |
56327 | 105 |
fold (\<lambda>(k, v) f. f(k \<mapsto> v)) (zip ks vs) \<circ> map_of" |
39921 | 106 |
by (rule fold_commute) (auto simp add: fun_eq_iff update_conv') |
56327 | 107 |
then show ?thesis |
108 |
by (auto simp add: updates_def fun_eq_iff map_upds_fold_map_upd foldl_conv_fold split_def) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
109 |
qed |
19234 | 110 |
|
111 |
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
|
112 |
by (simp add: updates_conv') |
19234 | 113 |
|
114 |
lemma distinct_updates: |
|
115 |
assumes "distinct (map fst al)" |
|
116 |
shows "distinct (map fst (updates ks vs al))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
117 |
proof - |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
45990
diff
changeset
|
118 |
have "distinct (fold |
37458 | 119 |
(\<lambda>(k, v) al. if k \<in> set al then al else al @ [k]) |
120 |
(zip ks vs) (map fst al))" |
|
121 |
by (rule fold_invariant [of "zip ks vs" "\<lambda>_. True"]) (auto intro: assms) |
|
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
122 |
moreover have "map fst \<circ> fold (case_prod update) (zip ks vs) = |
56327 | 123 |
fold (\<lambda>(k, v) al. if k \<in> set al then al else al @ [k]) (zip ks vs) \<circ> map fst" |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
124 |
by (rule fold_commute) (simp add: update_keys split_def case_prod_beta comp_def) |
56327 | 125 |
ultimately show ?thesis |
126 |
by (simp add: updates_def fun_eq_iff) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
127 |
qed |
19234 | 128 |
|
129 |
lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow> |
|
56327 | 130 |
updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)" |
20503 | 131 |
by (induct ks arbitrary: vs al) (auto split: list.splits) |
19234 | 132 |
|
133 |
lemma updates_list_update_drop[simp]: |
|
56327 | 134 |
"size ks \<le> i \<Longrightarrow> i < size vs \<Longrightarrow> |
135 |
updates ks (vs[i:=v]) al = updates ks vs al" |
|
136 |
by (induct ks arbitrary: al vs i) (auto split: list.splits nat.splits) |
|
19234 | 137 |
|
56327 | 138 |
lemma update_updates_conv_if: |
139 |
"map_of (updates xs ys (update x y al)) = |
|
140 |
map_of |
|
141 |
(if x \<in> set (take (length ys) xs) |
|
142 |
then updates xs ys al |
|
143 |
else (update x y (updates xs ys al)))" |
|
19234 | 144 |
by (simp add: updates_conv' update_conv' map_upd_upds_conv_if) |
145 |
||
146 |
lemma updates_twist [simp]: |
|
56327 | 147 |
"k \<notin> set ks \<Longrightarrow> |
148 |
map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))" |
|
46507 | 149 |
by (simp add: updates_conv' update_conv') |
19234 | 150 |
|
56327 | 151 |
lemma updates_apply_notin [simp]: |
152 |
"k \<notin> set ks \<Longrightarrow> map_of (updates ks vs al) k = map_of al k" |
|
19234 | 153 |
by (simp add: updates_conv) |
154 |
||
56327 | 155 |
lemma updates_append_drop [simp]: |
156 |
"size xs = size ys \<Longrightarrow> updates (xs @ zs) ys al = updates xs ys al" |
|
20503 | 157 |
by (induct xs arbitrary: ys al) (auto split: list.splits) |
19234 | 158 |
|
56327 | 159 |
lemma updates_append2_drop [simp]: |
160 |
"size xs = size ys \<Longrightarrow> updates xs (ys @ zs) al = updates xs ys al" |
|
20503 | 161 |
by (induct xs arbitrary: ys al) (auto split: list.splits) |
19234 | 162 |
|
23373 | 163 |
|
61585 | 164 |
subsection \<open>\<open>delete\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
165 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
166 |
qualified definition delete :: "'key \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
56327 | 167 |
where delete_eq: "delete k = filter (\<lambda>(k', _). k \<noteq> k')" |
34975
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_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
170 |
"delete k [] = []" |
56327 | 171 |
"delete k (p # ps) = (if fst p = k then delete k ps else p # delete k ps)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
172 |
by (auto simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
173 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
174 |
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
|
175 |
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
|
176 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
177 |
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
|
178 |
by (simp add: delete_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
179 |
|
56327 | 180 |
lemma delete_keys: "map fst (delete k al) = removeAll k (map fst al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
181 |
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
|
182 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
183 |
lemma distinct_delete: |
56327 | 184 |
assumes "distinct (map fst al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
185 |
shows "distinct (map fst (delete k al))" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
186 |
using assms by (simp add: delete_keys distinct_removeAll) |
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_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
|
189 |
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
|
190 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
191 |
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
|
192 |
by (simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
193 |
|
56327 | 194 |
lemma map_of_delete [simp]: "k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
195 |
by (simp add: delete_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
196 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
197 |
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
|
198 |
by (auto simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
199 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
200 |
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
|
201 |
by (auto simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
202 |
|
56327 | 203 |
lemma delete_update_same: "delete k (update k v al) = delete k al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
204 |
by (induct al) simp_all |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
205 |
|
56327 | 206 |
lemma delete_update: "k \<noteq> l \<Longrightarrow> delete l (update k v al) = update k v (delete l al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
207 |
by (induct al) simp_all |
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 |
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
|
210 |
by (simp add: delete_eq conj_commute) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
211 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
212 |
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
|
213 |
by (simp add: delete_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
214 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
215 |
|
61585 | 216 |
subsection \<open>\<open>update_with_aux\<close> and \<open>delete_aux\<close>\<close> |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
217 |
|
63476 | 218 |
qualified primrec update_with_aux :: |
219 |
"'val \<Rightarrow> 'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
63462 | 220 |
where |
221 |
"update_with_aux v k f [] = [(k, f v)]" |
|
222 |
| "update_with_aux v k f (p # ps) = |
|
223 |
(if (fst p = k) then (k, f (snd p)) # ps else p # update_with_aux v k f ps)" |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
224 |
|
60500 | 225 |
text \<open> |
69593 | 226 |
The above \<^term>\<open>delete\<close> traverses all the list even if it has found the key. |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
227 |
This one does not have to keep going because is assumes the invariant that keys are distinct. |
60500 | 228 |
\<close> |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
229 |
qualified fun delete_aux :: "'key \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
63462 | 230 |
where |
231 |
"delete_aux k [] = []" |
|
232 |
| "delete_aux k ((k', v) # xs) = (if k = k' then xs else (k', v) # delete_aux k xs)" |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
233 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
234 |
lemma map_of_update_with_aux': |
63462 | 235 |
"map_of (update_with_aux v k f ps) k' = |
236 |
((map_of ps)(k \<mapsto> (case map_of ps k of None \<Rightarrow> f v | Some v \<Rightarrow> f v))) k'" |
|
237 |
by (induct ps) auto |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
238 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
239 |
lemma map_of_update_with_aux: |
63462 | 240 |
"map_of (update_with_aux v k f ps) = |
241 |
(map_of ps)(k \<mapsto> (case map_of ps k of None \<Rightarrow> f v | Some v \<Rightarrow> f v))" |
|
242 |
by (simp add: fun_eq_iff map_of_update_with_aux') |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
243 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
244 |
lemma dom_update_with_aux: "fst ` set (update_with_aux v k f ps) = {k} \<union> fst ` set ps" |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
245 |
by (induct ps) auto |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
246 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
247 |
lemma distinct_update_with_aux [simp]: |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
248 |
"distinct (map fst (update_with_aux v k f ps)) = distinct (map fst ps)" |
63462 | 249 |
by (induct ps) (auto simp add: dom_update_with_aux) |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
250 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
251 |
lemma set_update_with_aux: |
63462 | 252 |
"distinct (map fst xs) \<Longrightarrow> |
253 |
set (update_with_aux v k f xs) = |
|
254 |
(set xs - {k} \<times> UNIV \<union> {(k, f (case map_of xs k of None \<Rightarrow> v | Some v \<Rightarrow> v))})" |
|
255 |
by (induct xs) (auto intro: rev_image_eqI) |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
256 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
257 |
lemma set_delete_aux: "distinct (map fst xs) \<Longrightarrow> set (delete_aux k xs) = set xs - {k} \<times> UNIV" |
63462 | 258 |
apply (induct xs) |
63476 | 259 |
apply simp_all |
63462 | 260 |
apply clarsimp |
261 |
apply (fastforce intro: rev_image_eqI) |
|
262 |
done |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
263 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
264 |
lemma dom_delete_aux: "distinct (map fst ps) \<Longrightarrow> fst ` set (delete_aux k ps) = fst ` set ps - {k}" |
63462 | 265 |
by (auto simp add: set_delete_aux) |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
266 |
|
63462 | 267 |
lemma distinct_delete_aux [simp]: "distinct (map fst ps) \<Longrightarrow> distinct (map fst (delete_aux k ps))" |
268 |
proof (induct ps) |
|
269 |
case Nil |
|
270 |
then show ?case by simp |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
271 |
next |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
272 |
case (Cons a ps) |
63462 | 273 |
obtain k' v where a: "a = (k', v)" |
274 |
by (cases a) |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
275 |
show ?case |
63462 | 276 |
proof (cases "k' = k") |
277 |
case True |
|
278 |
with Cons a show ?thesis by simp |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
279 |
next |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
280 |
case False |
63462 | 281 |
with Cons a have "k' \<notin> fst ` set ps" "distinct (map fst ps)" |
282 |
by simp_all |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
283 |
with False a have "k' \<notin> fst ` set (delete_aux k ps)" |
63462 | 284 |
by (auto dest!: dom_delete_aux[where k=k]) |
285 |
with Cons a show ?thesis |
|
286 |
by simp |
|
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
287 |
qed |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
288 |
qed |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
289 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
290 |
lemma map_of_delete_aux': |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
291 |
"distinct (map fst xs) \<Longrightarrow> map_of (delete_aux k xs) = (map_of xs)(k := None)" |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
292 |
apply (induct xs) |
63476 | 293 |
apply (fastforce simp add: map_of_eq_None_iff fun_upd_twist) |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
294 |
apply (auto intro!: ext) |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
295 |
apply (simp add: map_of_eq_None_iff) |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
296 |
done |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
297 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
298 |
lemma map_of_delete_aux: |
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
299 |
"distinct (map fst xs) \<Longrightarrow> map_of (delete_aux k xs) k' = ((map_of xs)(k := None)) k'" |
63462 | 300 |
by (simp add: map_of_delete_aux') |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
301 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
302 |
lemma delete_aux_eq_Nil_conv: "delete_aux k ts = [] \<longleftrightarrow> ts = [] \<or> (\<exists>v. ts = [(k, v)])" |
63462 | 303 |
by (cases ts) (auto split: if_split_asm) |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
304 |
|
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
305 |
|
61585 | 306 |
subsection \<open>\<open>restrict\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
307 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
308 |
qualified definition restrict :: "'key set \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
56327 | 309 |
where restrict_eq: "restrict A = filter (\<lambda>(k, v). k \<in> A)" |
34975
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 restr_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
312 |
"restrict A [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
313 |
"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
|
314 |
by (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
315 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
316 |
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
|
317 |
proof |
63462 | 318 |
show "map_of (restrict A al) k = ((map_of al)|` A) k" for k |
319 |
apply (induct al) |
|
63476 | 320 |
apply simp |
63462 | 321 |
apply (cases "k \<in> A") |
63476 | 322 |
apply auto |
63462 | 323 |
done |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
324 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
325 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
326 |
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
|
327 |
by (simp add: restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
328 |
|
63462 | 329 |
lemma distinct_restr: "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
330 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
331 |
|
56327 | 332 |
lemma restr_empty [simp]: |
333 |
"restrict {} al = []" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
334 |
"restrict A [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
335 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
336 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
337 |
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
|
338 |
by (simp add: restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
339 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
340 |
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
|
341 |
by (simp add: restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
342 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
343 |
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
|
344 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
345 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
346 |
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
|
347 |
by (induct al) (auto simp add: restrict_eq) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
348 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
349 |
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
|
350 |
by (induct al) (auto simp add: restrict_eq) |
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 restr_update[simp]: |
63462 | 353 |
"map_of (restrict D (update x y al)) = |
354 |
map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
355 |
by (simp add: restr_conv' update_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
356 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
357 |
lemma restr_delete [simp]: |
56327 | 358 |
"delete x (restrict D al) = (if x \<in> D then restrict (D - {x}) al else restrict D al)" |
359 |
apply (simp add: delete_eq restrict_eq) |
|
360 |
apply (auto simp add: split_def) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
361 |
proof - |
63462 | 362 |
have "y \<noteq> x \<longleftrightarrow> x \<noteq> y" for y |
56327 | 363 |
by auto |
68386 | 364 |
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]" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
365 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
366 |
assume "x \<notin> D" |
63462 | 367 |
then have "y \<in> D \<longleftrightarrow> y \<in> D \<and> x \<noteq> y" for y |
56327 | 368 |
by auto |
68386 | 369 |
then show "[p \<leftarrow> al . fst p \<in> D \<and> x \<noteq> fst p] = [p \<leftarrow> al . fst p \<in> D]" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
370 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
371 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
372 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
373 |
lemma update_restr: |
56327 | 374 |
"map_of (update x y (restrict D al)) = map_of (update x y (restrict (D - {x}) al))" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
375 |
by (simp add: update_conv' restr_conv') (rule fun_upd_restrict) |
19234 | 376 |
|
45867 | 377 |
lemma update_restr_conv [simp]: |
56327 | 378 |
"x \<in> D \<Longrightarrow> |
379 |
map_of (update x y (restrict D al)) = map_of (update x y (restrict (D - {x}) al))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
380 |
by (simp add: update_conv' restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
381 |
|
56327 | 382 |
lemma restr_updates [simp]: |
383 |
"length xs = length ys \<Longrightarrow> set xs \<subseteq> D \<Longrightarrow> |
|
384 |
map_of (restrict D (updates xs ys al)) = |
|
385 |
map_of (updates xs ys (restrict (D - set xs) al))" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
386 |
by (simp add: updates_conv' restr_conv') |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
387 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
388 |
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
|
389 |
by (induct ps) auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
390 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
391 |
|
61585 | 392 |
subsection \<open>\<open>clearjunk\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
393 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
394 |
qualified function clearjunk :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
63462 | 395 |
where |
396 |
"clearjunk [] = []" |
|
397 |
| "clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
398 |
by pat_completeness auto |
56327 | 399 |
termination |
400 |
by (relation "measure length") (simp_all add: less_Suc_eq_le length_delete_le) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
401 |
|
56327 | 402 |
lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al" |
403 |
by (induct al rule: clearjunk.induct) (simp_all add: fun_eq_iff) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
404 |
|
56327 | 405 |
lemma clearjunk_keys_set: "set (map fst (clearjunk al)) = set (map fst al)" |
406 |
by (induct al rule: clearjunk.induct) (simp_all add: delete_keys) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
407 |
|
56327 | 408 |
lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
409 |
using clearjunk_keys_set by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
410 |
|
56327 | 411 |
lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))" |
412 |
by (induct al rule: clearjunk.induct) (simp_all del: set_map add: clearjunk_keys_set delete_keys) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
413 |
|
56327 | 414 |
lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
415 |
by (simp add: map_of_clearjunk) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
416 |
|
56327 | 417 |
lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
418 |
proof - |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
419 |
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
|
420 |
by (simp add: ran_clearjunk) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
421 |
also have "\<dots> = snd ` set (clearjunk al)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
422 |
by (simp add: ran_distinct) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
423 |
finally show ?thesis . |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
424 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
425 |
|
73832 | 426 |
lemma graph_map_of: "Map.graph (map_of al) = set (clearjunk al)" |
74157
8e2355ddce1b
add/rename some theorems about Map(pings)
Lukas Stevens <mail@lukas-stevens.de>
parents:
73832
diff
changeset
|
427 |
by (metis distinct_clearjunk graph_map_of_if_distinct_dom map_of_clearjunk) |
73832 | 428 |
|
56327 | 429 |
lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)" |
430 |
by (induct al rule: clearjunk.induct) (simp_all add: delete_update) |
|
19234 | 431 |
|
56327 | 432 |
lemma clearjunk_updates: "clearjunk (updates ks vs al) = updates ks vs (clearjunk al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
433 |
proof - |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
434 |
have "clearjunk \<circ> fold (case_prod update) (zip ks vs) = |
63462 | 435 |
fold (case_prod update) (zip ks vs) \<circ> clearjunk" |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
436 |
by (rule fold_commute) (simp add: clearjunk_update case_prod_beta o_def) |
56327 | 437 |
then show ?thesis |
438 |
by (simp add: updates_def fun_eq_iff) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
439 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
440 |
|
56327 | 441 |
lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
442 |
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
|
443 |
|
56327 | 444 |
lemma clearjunk_restrict: "clearjunk (restrict A al) = restrict A (clearjunk al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
445 |
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
|
446 |
|
56327 | 447 |
lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
448 |
by (induct al rule: clearjunk.induct) auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
449 |
|
56327 | 450 |
lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
451 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
452 |
|
56327 | 453 |
lemma length_clearjunk: "length (clearjunk al) \<le> length al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
454 |
proof (induct al rule: clearjunk.induct [case_names Nil Cons]) |
56327 | 455 |
case Nil |
456 |
then show ?case by simp |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
457 |
next |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
458 |
case (Cons kv al) |
56327 | 459 |
moreover have "length (delete (fst kv) al) \<le> length al" |
460 |
by (fact length_delete_le) |
|
461 |
ultimately have "length (clearjunk (delete (fst kv) al)) \<le> length al" |
|
462 |
by (rule order_trans) |
|
463 |
then show ?case |
|
464 |
by simp |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
465 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
466 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
467 |
lemma delete_map: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
468 |
assumes "\<And>kv. fst (f kv) = fst kv" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
469 |
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
|
470 |
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
|
471 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
472 |
lemma clearjunk_map: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
473 |
assumes "\<And>kv. fst (f kv) = fst kv" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
474 |
shows "clearjunk (map f ps) = map f (clearjunk ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
475 |
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
|
476 |
(simp_all add: clearjunk_delete delete_map assms) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
477 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
478 |
|
61585 | 479 |
subsection \<open>\<open>map_ran\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
480 |
|
73678 | 481 |
definition map_ran :: "('key \<Rightarrow> 'val1 \<Rightarrow> 'val2) \<Rightarrow> ('key \<times> 'val1) list \<Rightarrow> ('key \<times> 'val2) list" |
56327 | 482 |
where "map_ran f = map (\<lambda>(k, v). (k, f k v))" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
483 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
484 |
lemma map_ran_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
485 |
"map_ran f [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
486 |
"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
|
487 |
by (simp_all add: map_ran_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
488 |
|
73680
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
489 |
lemma map_ran_Cons_sel: "map_ran f (p # ps) = (fst p, f (fst p) (snd p)) # map_ran f ps" |
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
490 |
by (simp add: map_ran_def case_prod_beta) |
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
491 |
|
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
492 |
lemma length_map_ran[simp]: "length (map_ran f al) = length al" |
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
493 |
by (simp add: map_ran_def) |
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
494 |
|
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
495 |
lemma map_fst_map_ran[simp]: "map fst (map_ran f al) = map fst al" |
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
496 |
by (simp add: map_ran_def case_prod_beta) |
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
497 |
|
56327 | 498 |
lemma dom_map_ran: "fst ` set (map_ran f al) = fst ` set al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
499 |
by (simp add: map_ran_def image_image split_def) |
56327 | 500 |
|
501 |
lemma map_ran_conv: "map_of (map_ran f al) k = map_option (f k) (map_of al k)" |
|
19234 | 502 |
by (induct al) auto |
503 |
||
56327 | 504 |
lemma distinct_map_ran: "distinct (map fst al) \<Longrightarrow> distinct (map fst (map_ran f al))" |
73680
50437744eb1c
added lemmas map_ran_Cons_sel and (length|map_fst)_map_ran
desharna
parents:
73678
diff
changeset
|
505 |
by simp |
19234 | 506 |
|
68386 | 507 |
lemma map_ran_filter: "map_ran f [p\<leftarrow>ps. fst p \<noteq> a] = [p\<leftarrow>map_ran f ps. fst p \<noteq> a]" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
508 |
by (simp add: map_ran_def filter_map split_def comp_def) |
19234 | 509 |
|
56327 | 510 |
lemma clearjunk_map_ran: "clearjunk (map_ran f al) = map_ran f (clearjunk al)" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
511 |
by (simp add: map_ran_def split_def clearjunk_map) |
19234 | 512 |
|
23373 | 513 |
|
61585 | 514 |
subsection \<open>\<open>merge\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
515 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
516 |
qualified definition merge :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
56327 | 517 |
where "merge qs ps = foldr (\<lambda>(k, v). update k v) ps qs" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
518 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
519 |
lemma merge_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
520 |
"merge qs [] = qs" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
521 |
"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
|
522 |
by (simp_all add: merge_def split_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
523 |
|
56327 | 524 |
lemma merge_updates: "merge qs ps = updates (rev (map fst ps)) (rev (map snd ps)) qs" |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
46507
diff
changeset
|
525 |
by (simp add: merge_def updates_def foldr_conv_fold zip_rev zip_map_fst_snd) |
19234 | 526 |
|
527 |
lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys" |
|
20503 | 528 |
by (induct ys arbitrary: xs) (auto simp add: dom_update) |
19234 | 529 |
|
63462 | 530 |
lemma distinct_merge: "distinct (map fst xs) \<Longrightarrow> distinct (map fst (merge xs ys))" |
531 |
by (simp add: merge_updates distinct_updates) |
|
19234 | 532 |
|
56327 | 533 |
lemma clearjunk_merge: "clearjunk (merge xs ys) = merge (clearjunk xs) ys" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
534 |
by (simp add: merge_updates clearjunk_updates) |
19234 | 535 |
|
56327 | 536 |
lemma merge_conv': "map_of (merge xs ys) = map_of xs ++ map_of ys" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
537 |
proof - |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
538 |
have "map_of \<circ> fold (case_prod update) (rev ys) = |
56327 | 539 |
fold (\<lambda>(k, v) m. m(k \<mapsto> v)) (rev ys) \<circ> map_of" |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
540 |
by (rule fold_commute) (simp add: update_conv' case_prod_beta split_def fun_eq_iff) |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
541 |
then show ?thesis |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
46507
diff
changeset
|
542 |
by (simp add: merge_def map_add_map_of_foldr foldr_conv_fold fun_eq_iff) |
19234 | 543 |
qed |
544 |
||
56327 | 545 |
corollary merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
546 |
by (simp add: merge_conv') |
19234 | 547 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
548 |
lemma merge_empty: "map_of (merge [] ys) = map_of ys" |
19234 | 549 |
by (simp add: merge_conv') |
550 |
||
56327 | 551 |
lemma merge_assoc [simp]: "map_of (merge m1 (merge m2 m3)) = map_of (merge (merge m1 m2) m3)" |
19234 | 552 |
by (simp add: merge_conv') |
553 |
||
56327 | 554 |
lemma merge_Some_iff: |
555 |
"map_of (merge m n) k = Some x \<longleftrightarrow> |
|
556 |
map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x" |
|
19234 | 557 |
by (simp add: merge_conv' map_add_Some_iff) |
558 |
||
45605 | 559 |
lemmas merge_SomeD [dest!] = merge_Some_iff [THEN iffD1] |
19234 | 560 |
|
56327 | 561 |
lemma merge_find_right [simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v" |
19234 | 562 |
by (simp add: merge_conv') |
563 |
||
63462 | 564 |
lemma merge_None [iff]: "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)" |
19234 | 565 |
by (simp add: merge_conv') |
566 |
||
63462 | 567 |
lemma merge_upd [simp]: "map_of (merge m (update k v n)) = map_of (update k v (merge m n))" |
19234 | 568 |
by (simp add: update_conv' merge_conv') |
569 |
||
56327 | 570 |
lemma merge_updatess [simp]: |
19234 | 571 |
"map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))" |
572 |
by (simp add: updates_conv' merge_conv') |
|
573 |
||
56327 | 574 |
lemma merge_append: "map_of (xs @ ys) = map_of (merge ys xs)" |
19234 | 575 |
by (simp add: merge_conv') |
576 |
||
23373 | 577 |
|
61585 | 578 |
subsection \<open>\<open>compose\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
579 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
580 |
qualified function compose :: "('key \<times> 'a) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('key \<times> 'b) list" |
63462 | 581 |
where |
582 |
"compose [] ys = []" |
|
583 |
| "compose (x # xs) ys = |
|
584 |
(case map_of ys (snd x) of |
|
585 |
None \<Rightarrow> compose (delete (fst x) xs) ys |
|
586 |
| Some v \<Rightarrow> (fst x, v) # compose xs ys)" |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
587 |
by pat_completeness auto |
56327 | 588 |
termination |
589 |
by (relation "measure (length \<circ> fst)") (simp_all add: less_Suc_eq_le length_delete_le) |
|
19234 | 590 |
|
63462 | 591 |
lemma compose_first_None [simp]: "map_of xs k = None \<Longrightarrow> map_of (compose xs ys) k = None" |
592 |
by (induct xs ys rule: compose.induct) (auto split: option.splits if_split_asm) |
|
19234 | 593 |
|
56327 | 594 |
lemma compose_conv: "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k" |
22916 | 595 |
proof (induct xs ys rule: compose.induct) |
56327 | 596 |
case 1 |
597 |
then show ?case by simp |
|
19234 | 598 |
next |
56327 | 599 |
case (2 x xs ys) |
600 |
show ?case |
|
19234 | 601 |
proof (cases "map_of ys (snd x)") |
56327 | 602 |
case None |
603 |
with 2 have hyp: "map_of (compose (delete (fst x) xs) ys) k = |
|
604 |
(map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k" |
|
19234 | 605 |
by simp |
606 |
show ?thesis |
|
607 |
proof (cases "fst x = k") |
|
608 |
case True |
|
609 |
from True delete_notin_dom [of k xs] |
|
610 |
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
|
611 |
by (simp add: map_of_eq_None_iff) |
19234 | 612 |
with hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
613 |
using True None |
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
614 |
by simp |
19234 | 615 |
next |
616 |
case False |
|
617 |
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
|
618 |
by simp |
19234 | 619 |
with hyp show ?thesis |
56327 | 620 |
using False None by (simp add: map_comp_def) |
19234 | 621 |
qed |
622 |
next |
|
623 |
case (Some v) |
|
22916 | 624 |
with 2 |
19234 | 625 |
have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k" |
626 |
by simp |
|
627 |
with Some show ?thesis |
|
628 |
by (auto simp add: map_comp_def) |
|
629 |
qed |
|
630 |
qed |
|
56327 | 631 |
|
632 |
lemma compose_conv': "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)" |
|
19234 | 633 |
by (rule ext) (rule compose_conv) |
634 |
||
63462 | 635 |
lemma compose_first_Some [simp]: "map_of xs k = Some v \<Longrightarrow> map_of (compose xs ys) k = map_of ys v" |
636 |
by (simp add: compose_conv) |
|
19234 | 637 |
|
638 |
lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs" |
|
22916 | 639 |
proof (induct xs ys rule: compose.induct) |
56327 | 640 |
case 1 |
641 |
then show ?case by simp |
|
19234 | 642 |
next |
22916 | 643 |
case (2 x xs ys) |
19234 | 644 |
show ?case |
645 |
proof (cases "map_of ys (snd x)") |
|
646 |
case None |
|
63462 | 647 |
with "2.hyps" have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)" |
19234 | 648 |
by simp |
63462 | 649 |
also have "\<dots> \<subseteq> fst ` set xs" |
19234 | 650 |
by (rule dom_delete_subset) |
651 |
finally show ?thesis |
|
63462 | 652 |
using None by auto |
19234 | 653 |
next |
654 |
case (Some v) |
|
63462 | 655 |
with "2.hyps" have "fst ` set (compose xs ys) \<subseteq> fst ` set xs" |
19234 | 656 |
by simp |
657 |
with Some show ?thesis |
|
658 |
by auto |
|
659 |
qed |
|
660 |
qed |
|
661 |
||
662 |
lemma distinct_compose: |
|
56327 | 663 |
assumes "distinct (map fst xs)" |
664 |
shows "distinct (map fst (compose xs ys))" |
|
665 |
using assms |
|
22916 | 666 |
proof (induct xs ys rule: compose.induct) |
56327 | 667 |
case 1 |
668 |
then show ?case by simp |
|
19234 | 669 |
next |
22916 | 670 |
case (2 x xs ys) |
19234 | 671 |
show ?case |
672 |
proof (cases "map_of ys (snd x)") |
|
673 |
case None |
|
22916 | 674 |
with 2 show ?thesis by simp |
19234 | 675 |
next |
676 |
case (Some v) |
|
56327 | 677 |
with 2 dom_compose [of xs ys] show ?thesis |
678 |
by auto |
|
19234 | 679 |
qed |
680 |
qed |
|
681 |
||
56327 | 682 |
lemma compose_delete_twist: "compose (delete k xs) ys = delete k (compose xs ys)" |
22916 | 683 |
proof (induct xs ys rule: compose.induct) |
56327 | 684 |
case 1 |
685 |
then show ?case by simp |
|
19234 | 686 |
next |
22916 | 687 |
case (2 x xs ys) |
19234 | 688 |
show ?case |
689 |
proof (cases "map_of ys (snd x)") |
|
690 |
case None |
|
56327 | 691 |
with 2 have hyp: "compose (delete k (delete (fst x) xs)) ys = |
692 |
delete k (compose (delete (fst x) xs) ys)" |
|
19234 | 693 |
by simp |
694 |
show ?thesis |
|
695 |
proof (cases "fst x = k") |
|
696 |
case True |
|
56327 | 697 |
with None hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
698 |
by (simp add: delete_idem) |
19234 | 699 |
next |
700 |
case False |
|
56327 | 701 |
from None False hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
702 |
by (simp add: delete_twist) |
19234 | 703 |
qed |
704 |
next |
|
705 |
case (Some v) |
|
56327 | 706 |
with 2 have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" |
707 |
by simp |
|
19234 | 708 |
with Some show ?thesis |
709 |
by simp |
|
710 |
qed |
|
711 |
qed |
|
712 |
||
713 |
lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys" |
|
56327 | 714 |
by (induct xs ys rule: compose.induct) |
715 |
(auto simp add: map_of_clearjunk split: option.splits) |
|
716 |
||
19234 | 717 |
lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys" |
718 |
by (induct xs rule: clearjunk.induct) |
|
56327 | 719 |
(auto split: option.splits simp add: clearjunk_delete delete_idem compose_delete_twist) |
720 |
||
721 |
lemma compose_empty [simp]: "compose xs [] = []" |
|
22916 | 722 |
by (induct xs) (auto simp add: compose_delete_twist) |
19234 | 723 |
|
724 |
lemma compose_Some_iff: |
|
56327 | 725 |
"(map_of (compose xs ys) k = Some v) \<longleftrightarrow> |
726 |
(\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)" |
|
19234 | 727 |
by (simp add: compose_conv map_comp_Some_iff) |
728 |
||
729 |
lemma map_comp_None_iff: |
|
56327 | 730 |
"map_of (compose xs ys) k = None \<longleftrightarrow> |
731 |
(map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None))" |
|
19234 | 732 |
by (simp add: compose_conv map_comp_None_iff) |
733 |
||
56327 | 734 |
|
61585 | 735 |
subsection \<open>\<open>map_entry\<close>\<close> |
45869 | 736 |
|
59990
a81dc82ecba3
clarified keyword 'qualified' in accordance to a similar keyword from Haskell (despite unrelated Binding.qualified in Isabelle/ML);
wenzelm
parents:
59943
diff
changeset
|
737 |
qualified fun map_entry :: "'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
63462 | 738 |
where |
739 |
"map_entry k f [] = []" |
|
740 |
| "map_entry k f (p # ps) = |
|
741 |
(if fst p = k then (k, f (snd p)) # ps else p # map_entry k f ps)" |
|
45869 | 742 |
|
743 |
lemma map_of_map_entry: |
|
56327 | 744 |
"map_of (map_entry k f xs) = |
745 |
(map_of xs)(k := case map_of xs k of None \<Rightarrow> None | Some v' \<Rightarrow> Some (f v'))" |
|
746 |
by (induct xs) auto |
|
45869 | 747 |
|
56327 | 748 |
lemma dom_map_entry: "fst ` set (map_entry k f xs) = fst ` set xs" |
749 |
by (induct xs) auto |
|
45869 | 750 |
|
751 |
lemma distinct_map_entry: |
|
752 |
assumes "distinct (map fst xs)" |
|
753 |
shows "distinct (map fst (map_entry k f xs))" |
|
56327 | 754 |
using assms by (induct xs) (auto simp add: dom_map_entry) |
755 |
||
45869 | 756 |
|
61585 | 757 |
subsection \<open>\<open>map_default\<close>\<close> |
45868 | 758 |
|
759 |
fun map_default :: "'key \<Rightarrow> 'val \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
63462 | 760 |
where |
761 |
"map_default k v f [] = [(k, v)]" |
|
762 |
| "map_default k v f (p # ps) = |
|
763 |
(if fst p = k then (k, f (snd p)) # ps else p # map_default k v f ps)" |
|
45868 | 764 |
|
765 |
lemma map_of_map_default: |
|
56327 | 766 |
"map_of (map_default k v f xs) = |
767 |
(map_of xs)(k := case map_of xs k of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f v'))" |
|
768 |
by (induct xs) auto |
|
45868 | 769 |
|
56327 | 770 |
lemma dom_map_default: "fst ` set (map_default k v f xs) = insert k (fst ` set xs)" |
771 |
by (induct xs) auto |
|
45868 | 772 |
|
773 |
lemma distinct_map_default: |
|
774 |
assumes "distinct (map fst xs)" |
|
775 |
shows "distinct (map fst (map_default k v f xs))" |
|
56327 | 776 |
using assms by (induct xs) (auto simp add: dom_map_default) |
45868 | 777 |
|
59943
e83ecf0a0ee1
more qualified names -- eliminated hide_const (open);
wenzelm
parents:
58881
diff
changeset
|
778 |
end |
45884 | 779 |
|
19234 | 780 |
end |