author | nipkow |
Wed, 06 Jun 2018 11:12:37 +0200 | |
changeset 68386 | 98cf1c823c48 |
parent 68249 | 949d93804740 |
child 69593 | 3dda49e08b9d |
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 |
16 |
function @{term "clearjunk"} distributes over them. Since |
|
22740 | 17 |
@{term clearjunk} enforces distinctness of keys it can be used |
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: |
56327 | 72 |
@{term "update k v (update k' v' []) = [(k', v'), (k, v)]"} and |
60500 | 73 |
@{term "update k' v' (update k v []) = [(k, v), (k', v')]"}.\<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" |
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
|
85 |
by (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> |
60043
177d740a0d48
moved _aux functions from AFP/Collections to AList
nipkow
parents:
59990
diff
changeset
|
226 |
The above @{term "delete"} traverses all the list even if it has found the key. |
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 |
|
56327 | 426 |
lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)" |
427 |
by (induct al rule: clearjunk.induct) (simp_all add: delete_update) |
|
19234 | 428 |
|
56327 | 429 |
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
|
430 |
proof - |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
431 |
have "clearjunk \<circ> fold (case_prod update) (zip ks vs) = |
63462 | 432 |
fold (case_prod update) (zip ks vs) \<circ> clearjunk" |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
433 |
by (rule fold_commute) (simp add: clearjunk_update case_prod_beta o_def) |
56327 | 434 |
then show ?thesis |
435 |
by (simp add: updates_def fun_eq_iff) |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
436 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
437 |
|
56327 | 438 |
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
|
439 |
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
|
440 |
|
56327 | 441 |
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
|
442 |
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
|
443 |
|
56327 | 444 |
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
|
445 |
by (induct al rule: clearjunk.induct) auto |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
446 |
|
56327 | 447 |
lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
448 |
by simp |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
449 |
|
56327 | 450 |
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
|
451 |
proof (induct al rule: clearjunk.induct [case_names Nil Cons]) |
56327 | 452 |
case Nil |
453 |
then show ?case by simp |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
454 |
next |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
455 |
case (Cons kv al) |
56327 | 456 |
moreover have "length (delete (fst kv) al) \<le> length al" |
457 |
by (fact length_delete_le) |
|
458 |
ultimately have "length (clearjunk (delete (fst kv) al)) \<le> length al" |
|
459 |
by (rule order_trans) |
|
460 |
then show ?case |
|
461 |
by simp |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
462 |
qed |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
463 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
464 |
lemma delete_map: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
465 |
assumes "\<And>kv. fst (f kv) = fst kv" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
466 |
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
|
467 |
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
|
468 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
469 |
lemma clearjunk_map: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
470 |
assumes "\<And>kv. fst (f kv) = fst kv" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
471 |
shows "clearjunk (map f ps) = map f (clearjunk ps)" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
472 |
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
|
473 |
(simp_all add: clearjunk_delete delete_map assms) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
474 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
475 |
|
61585 | 476 |
subsection \<open>\<open>map_ran\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
477 |
|
56327 | 478 |
definition map_ran :: "('key \<Rightarrow> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
479 |
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
|
480 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
481 |
lemma map_ran_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
482 |
"map_ran f [] = []" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
483 |
"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
|
484 |
by (simp_all add: map_ran_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
485 |
|
56327 | 486 |
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
|
487 |
by (simp add: map_ran_def image_image split_def) |
56327 | 488 |
|
489 |
lemma map_ran_conv: "map_of (map_ran f al) k = map_option (f k) (map_of al k)" |
|
19234 | 490 |
by (induct al) auto |
491 |
||
56327 | 492 |
lemma distinct_map_ran: "distinct (map fst al) \<Longrightarrow> distinct (map fst (map_ran f al))" |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
493 |
by (simp add: map_ran_def split_def comp_def) |
19234 | 494 |
|
68386 | 495 |
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
|
496 |
by (simp add: map_ran_def filter_map split_def comp_def) |
19234 | 497 |
|
56327 | 498 |
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
|
499 |
by (simp add: map_ran_def split_def clearjunk_map) |
19234 | 500 |
|
23373 | 501 |
|
61585 | 502 |
subsection \<open>\<open>merge\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
503 |
|
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
|
504 |
qualified definition merge :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
56327 | 505 |
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
|
506 |
|
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
507 |
lemma merge_simps [simp]: |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
508 |
"merge qs [] = qs" |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
509 |
"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
|
510 |
by (simp_all add: merge_def split_def) |
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
511 |
|
56327 | 512 |
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
|
513 |
by (simp add: merge_def updates_def foldr_conv_fold zip_rev zip_map_fst_snd) |
19234 | 514 |
|
515 |
lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys" |
|
20503 | 516 |
by (induct ys arbitrary: xs) (auto simp add: dom_update) |
19234 | 517 |
|
63462 | 518 |
lemma distinct_merge: "distinct (map fst xs) \<Longrightarrow> distinct (map fst (merge xs ys))" |
519 |
by (simp add: merge_updates distinct_updates) |
|
19234 | 520 |
|
56327 | 521 |
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
|
522 |
by (simp add: merge_updates clearjunk_updates) |
19234 | 523 |
|
56327 | 524 |
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
|
525 |
proof - |
55414
eab03e9cee8a
renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents:
47397
diff
changeset
|
526 |
have "map_of \<circ> fold (case_prod update) (rev ys) = |
56327 | 527 |
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
|
528 |
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
|
529 |
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
|
530 |
by (simp add: merge_def map_add_map_of_foldr foldr_conv_fold fun_eq_iff) |
19234 | 531 |
qed |
532 |
||
56327 | 533 |
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
|
534 |
by (simp add: merge_conv') |
19234 | 535 |
|
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
536 |
lemma merge_empty: "map_of (merge [] ys) = map_of ys" |
19234 | 537 |
by (simp add: merge_conv') |
538 |
||
56327 | 539 |
lemma merge_assoc [simp]: "map_of (merge m1 (merge m2 m3)) = map_of (merge (merge m1 m2) m3)" |
19234 | 540 |
by (simp add: merge_conv') |
541 |
||
56327 | 542 |
lemma merge_Some_iff: |
543 |
"map_of (merge m n) k = Some x \<longleftrightarrow> |
|
544 |
map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x" |
|
19234 | 545 |
by (simp add: merge_conv' map_add_Some_iff) |
546 |
||
45605 | 547 |
lemmas merge_SomeD [dest!] = merge_Some_iff [THEN iffD1] |
19234 | 548 |
|
56327 | 549 |
lemma merge_find_right [simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v" |
19234 | 550 |
by (simp add: merge_conv') |
551 |
||
63462 | 552 |
lemma merge_None [iff]: "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)" |
19234 | 553 |
by (simp add: merge_conv') |
554 |
||
63462 | 555 |
lemma merge_upd [simp]: "map_of (merge m (update k v n)) = map_of (update k v (merge m n))" |
19234 | 556 |
by (simp add: update_conv' merge_conv') |
557 |
||
56327 | 558 |
lemma merge_updatess [simp]: |
19234 | 559 |
"map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))" |
560 |
by (simp add: updates_conv' merge_conv') |
|
561 |
||
56327 | 562 |
lemma merge_append: "map_of (xs @ ys) = map_of (merge ys xs)" |
19234 | 563 |
by (simp add: merge_conv') |
564 |
||
23373 | 565 |
|
61585 | 566 |
subsection \<open>\<open>compose\<close>\<close> |
34975
f099b0b20646
more correspondence lemmas between related operations; tuned some proofs
haftmann
parents:
32960
diff
changeset
|
567 |
|
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
|
568 |
qualified function compose :: "('key \<times> 'a) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('key \<times> 'b) list" |
63462 | 569 |
where |
570 |
"compose [] ys = []" |
|
571 |
| "compose (x # xs) ys = |
|
572 |
(case map_of ys (snd x) of |
|
573 |
None \<Rightarrow> compose (delete (fst x) xs) ys |
|
574 |
| 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
|
575 |
by pat_completeness auto |
56327 | 576 |
termination |
577 |
by (relation "measure (length \<circ> fst)") (simp_all add: less_Suc_eq_le length_delete_le) |
|
19234 | 578 |
|
63462 | 579 |
lemma compose_first_None [simp]: "map_of xs k = None \<Longrightarrow> map_of (compose xs ys) k = None" |
580 |
by (induct xs ys rule: compose.induct) (auto split: option.splits if_split_asm) |
|
19234 | 581 |
|
56327 | 582 |
lemma compose_conv: "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k" |
22916 | 583 |
proof (induct xs ys rule: compose.induct) |
56327 | 584 |
case 1 |
585 |
then show ?case by simp |
|
19234 | 586 |
next |
56327 | 587 |
case (2 x xs ys) |
588 |
show ?case |
|
19234 | 589 |
proof (cases "map_of ys (snd x)") |
56327 | 590 |
case None |
591 |
with 2 have hyp: "map_of (compose (delete (fst x) xs) ys) k = |
|
592 |
(map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k" |
|
19234 | 593 |
by simp |
594 |
show ?thesis |
|
595 |
proof (cases "fst x = k") |
|
596 |
case True |
|
597 |
from True delete_notin_dom [of k xs] |
|
598 |
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
|
599 |
by (simp add: map_of_eq_None_iff) |
19234 | 600 |
with hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
601 |
using True None |
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
602 |
by simp |
19234 | 603 |
next |
604 |
case False |
|
605 |
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
|
606 |
by simp |
19234 | 607 |
with hyp show ?thesis |
56327 | 608 |
using False None by (simp add: map_comp_def) |
19234 | 609 |
qed |
610 |
next |
|
611 |
case (Some v) |
|
22916 | 612 |
with 2 |
19234 | 613 |
have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k" |
614 |
by simp |
|
615 |
with Some show ?thesis |
|
616 |
by (auto simp add: map_comp_def) |
|
617 |
qed |
|
618 |
qed |
|
56327 | 619 |
|
620 |
lemma compose_conv': "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)" |
|
19234 | 621 |
by (rule ext) (rule compose_conv) |
622 |
||
63462 | 623 |
lemma compose_first_Some [simp]: "map_of xs k = Some v \<Longrightarrow> map_of (compose xs ys) k = map_of ys v" |
624 |
by (simp add: compose_conv) |
|
19234 | 625 |
|
626 |
lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs" |
|
22916 | 627 |
proof (induct xs ys rule: compose.induct) |
56327 | 628 |
case 1 |
629 |
then show ?case by simp |
|
19234 | 630 |
next |
22916 | 631 |
case (2 x xs ys) |
19234 | 632 |
show ?case |
633 |
proof (cases "map_of ys (snd x)") |
|
634 |
case None |
|
63462 | 635 |
with "2.hyps" have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)" |
19234 | 636 |
by simp |
63462 | 637 |
also have "\<dots> \<subseteq> fst ` set xs" |
19234 | 638 |
by (rule dom_delete_subset) |
639 |
finally show ?thesis |
|
63462 | 640 |
using None by auto |
19234 | 641 |
next |
642 |
case (Some v) |
|
63462 | 643 |
with "2.hyps" have "fst ` set (compose xs ys) \<subseteq> fst ` set xs" |
19234 | 644 |
by simp |
645 |
with Some show ?thesis |
|
646 |
by auto |
|
647 |
qed |
|
648 |
qed |
|
649 |
||
650 |
lemma distinct_compose: |
|
56327 | 651 |
assumes "distinct (map fst xs)" |
652 |
shows "distinct (map fst (compose xs ys))" |
|
653 |
using assms |
|
22916 | 654 |
proof (induct xs ys rule: compose.induct) |
56327 | 655 |
case 1 |
656 |
then show ?case by simp |
|
19234 | 657 |
next |
22916 | 658 |
case (2 x xs ys) |
19234 | 659 |
show ?case |
660 |
proof (cases "map_of ys (snd x)") |
|
661 |
case None |
|
22916 | 662 |
with 2 show ?thesis by simp |
19234 | 663 |
next |
664 |
case (Some v) |
|
56327 | 665 |
with 2 dom_compose [of xs ys] show ?thesis |
666 |
by auto |
|
19234 | 667 |
qed |
668 |
qed |
|
669 |
||
56327 | 670 |
lemma compose_delete_twist: "compose (delete k xs) ys = delete k (compose xs ys)" |
22916 | 671 |
proof (induct xs ys rule: compose.induct) |
56327 | 672 |
case 1 |
673 |
then show ?case by simp |
|
19234 | 674 |
next |
22916 | 675 |
case (2 x xs ys) |
19234 | 676 |
show ?case |
677 |
proof (cases "map_of ys (snd x)") |
|
678 |
case None |
|
56327 | 679 |
with 2 have hyp: "compose (delete k (delete (fst x) xs)) ys = |
680 |
delete k (compose (delete (fst x) xs) ys)" |
|
19234 | 681 |
by simp |
682 |
show ?thesis |
|
683 |
proof (cases "fst x = k") |
|
684 |
case True |
|
56327 | 685 |
with None hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
686 |
by (simp add: delete_idem) |
19234 | 687 |
next |
688 |
case False |
|
56327 | 689 |
from None False hyp show ?thesis |
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
30663
diff
changeset
|
690 |
by (simp add: delete_twist) |
19234 | 691 |
qed |
692 |
next |
|
693 |
case (Some v) |
|
56327 | 694 |
with 2 have hyp: "compose (delete k xs) ys = delete k (compose xs ys)" |
695 |
by simp |
|
19234 | 696 |
with Some show ?thesis |
697 |
by simp |
|
698 |
qed |
|
699 |
qed |
|
700 |
||
701 |
lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys" |
|
56327 | 702 |
by (induct xs ys rule: compose.induct) |
703 |
(auto simp add: map_of_clearjunk split: option.splits) |
|
704 |
||
19234 | 705 |
lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys" |
706 |
by (induct xs rule: clearjunk.induct) |
|
56327 | 707 |
(auto split: option.splits simp add: clearjunk_delete delete_idem compose_delete_twist) |
708 |
||
709 |
lemma compose_empty [simp]: "compose xs [] = []" |
|
22916 | 710 |
by (induct xs) (auto simp add: compose_delete_twist) |
19234 | 711 |
|
712 |
lemma compose_Some_iff: |
|
56327 | 713 |
"(map_of (compose xs ys) k = Some v) \<longleftrightarrow> |
714 |
(\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)" |
|
19234 | 715 |
by (simp add: compose_conv map_comp_Some_iff) |
716 |
||
717 |
lemma map_comp_None_iff: |
|
56327 | 718 |
"map_of (compose xs ys) k = None \<longleftrightarrow> |
719 |
(map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None))" |
|
19234 | 720 |
by (simp add: compose_conv map_comp_None_iff) |
721 |
||
56327 | 722 |
|
61585 | 723 |
subsection \<open>\<open>map_entry\<close>\<close> |
45869 | 724 |
|
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
|
725 |
qualified fun map_entry :: "'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
63462 | 726 |
where |
727 |
"map_entry k f [] = []" |
|
728 |
| "map_entry k f (p # ps) = |
|
729 |
(if fst p = k then (k, f (snd p)) # ps else p # map_entry k f ps)" |
|
45869 | 730 |
|
731 |
lemma map_of_map_entry: |
|
56327 | 732 |
"map_of (map_entry k f xs) = |
733 |
(map_of xs)(k := case map_of xs k of None \<Rightarrow> None | Some v' \<Rightarrow> Some (f v'))" |
|
734 |
by (induct xs) auto |
|
45869 | 735 |
|
56327 | 736 |
lemma dom_map_entry: "fst ` set (map_entry k f xs) = fst ` set xs" |
737 |
by (induct xs) auto |
|
45869 | 738 |
|
739 |
lemma distinct_map_entry: |
|
740 |
assumes "distinct (map fst xs)" |
|
741 |
shows "distinct (map fst (map_entry k f xs))" |
|
56327 | 742 |
using assms by (induct xs) (auto simp add: dom_map_entry) |
743 |
||
45869 | 744 |
|
61585 | 745 |
subsection \<open>\<open>map_default\<close>\<close> |
45868 | 746 |
|
747 |
fun map_default :: "'key \<Rightarrow> 'val \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list" |
|
63462 | 748 |
where |
749 |
"map_default k v f [] = [(k, v)]" |
|
750 |
| "map_default k v f (p # ps) = |
|
751 |
(if fst p = k then (k, f (snd p)) # ps else p # map_default k v f ps)" |
|
45868 | 752 |
|
753 |
lemma map_of_map_default: |
|
56327 | 754 |
"map_of (map_default k v f xs) = |
755 |
(map_of xs)(k := case map_of xs k of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f v'))" |
|
756 |
by (induct xs) auto |
|
45868 | 757 |
|
56327 | 758 |
lemma dom_map_default: "fst ` set (map_default k v f xs) = insert k (fst ` set xs)" |
759 |
by (induct xs) auto |
|
45868 | 760 |
|
761 |
lemma distinct_map_default: |
|
762 |
assumes "distinct (map fst xs)" |
|
763 |
shows "distinct (map fst (map_default k v f xs))" |
|
56327 | 764 |
using assms by (induct xs) (auto simp add: dom_map_default) |
45868 | 765 |
|
59943
e83ecf0a0ee1
more qualified names -- eliminated hide_const (open);
wenzelm
parents:
58881
diff
changeset
|
766 |
end |
45884 | 767 |
|
19234 | 768 |
end |