
bulwahn@46238: (*  Title:      HOL/Library/AList.thy
haftmann@34975:     Author:     Norbert Schirmer, Tobias Nipkow, Martin Wildmoser, TU Muenchen
schirmer@19234: *)
schirmer@19234: 
bulwahn@44897: header {* Implementation of Association Lists *}
schirmer@19234: 
bulwahn@46238: theory AList
haftmann@45990: imports Main
schirmer@19234: begin
schirmer@19234: 
haftmann@22740: text {*
wenzelm@56327:   The operations preserve distinctness of keys and
wenzelm@56327:   function @{term "clearjunk"} distributes over them. Since
haftmann@22740:   @{term clearjunk} enforces distinctness of keys it can be used
haftmann@22740:   to establish the invariant, e.g. for inductive proofs.
haftmann@22740: *}
schirmer@19234: 
haftmann@34975: subsection {* @{text update} and @{text updates} *}
nipkow@19323: 
wenzelm@56327: primrec update :: "'key \<Rightarrow> 'val \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327: where
wenzelm@56327:   "update k v [] = [(k, v)]"
wenzelm@56327: | "update k v (p # ps) = (if fst p = k then (k, v) # ps else p # update k v ps)"
schirmer@19234: 
haftmann@34975: lemma update_conv': "map_of (update k v al)  = (map_of al)(k\<mapsto>v)"
nipkow@39302:   by (induct al) (auto simp add: fun_eq_iff)
wenzelm@23373: 
haftmann@34975: corollary update_conv: "map_of (update k v al) k' = ((map_of al)(k\<mapsto>v)) k'"
haftmann@34975:   by (simp add: update_conv')
schirmer@19234: 
schirmer@19234: lemma dom_update: "fst ` set (update k v al) = {k} \<union> fst ` set al"
schirmer@19234:   by (induct al) auto
schirmer@19234: 
haftmann@34975: lemma update_keys:
haftmann@34975:   "map fst (update k v al) =
haftmann@34975:     (if k \<in> set (map fst al) then map fst al else map fst al @ [k])"
haftmann@34975:   by (induct al) simp_all
haftmann@34975: 
schirmer@19234: lemma distinct_update:
wenzelm@56327:   assumes "distinct (map fst al)"
schirmer@19234:   shows "distinct (map fst (update k v al))"
haftmann@34975:   using assms by (simp add: update_keys)
schirmer@19234: 
wenzelm@56327: lemma update_filter:
wenzelm@56327:   "a \<noteq> k \<Longrightarrow> update k v [q\<leftarrow>ps. fst q \<noteq> a] = [q\<leftarrow>update k v ps. fst q \<noteq> a]"
schirmer@19234:   by (induct ps) auto
schirmer@19234: 
schirmer@19234: lemma update_triv: "map_of al k = Some v \<Longrightarrow> update k v al = al"
schirmer@19234:   by (induct al) auto
schirmer@19234: 
schirmer@19234: lemma update_nonempty [simp]: "update k v al \<noteq> []"
schirmer@19234:   by (induct al) auto
schirmer@19234: 
haftmann@34975: lemma update_eqD: "update k v al = update k v' al' \<Longrightarrow> v = v'"
wenzelm@56327: proof (induct al arbitrary: al')
wenzelm@56327:   case Nil
wenzelm@56327:   then show ?case
schirmer@19234:     by (cases al') (auto split: split_if_asm)
schirmer@19234: next
wenzelm@56327:   case Cons
wenzelm@56327:   then show ?case
schirmer@19234:     by (cases al') (auto split: split_if_asm)
schirmer@19234: qed
schirmer@19234: 
schirmer@19234: lemma update_last [simp]: "update k v (update k v' al) = update k v al"
schirmer@19234:   by (induct al) auto
schirmer@19234: 
schirmer@19234: text {* Note that the lists are not necessarily the same:
wenzelm@56327:         @{term "update k v (update k' v' []) = [(k', v'), (k, v)]"} and
haftmann@34975:         @{term "update k' v' (update k v []) = [(k, v), (k', v')]"}.*}
wenzelm@56327: 
wenzelm@56327: lemma update_swap:
wenzelm@56327:   "k \<noteq> k' \<Longrightarrow>
wenzelm@56327:     map_of (update k v (update k' v' al)) = map_of (update k' v' (update k v al))"
nipkow@39302:   by (simp add: update_conv' fun_eq_iff)
schirmer@19234: 
wenzelm@56327: lemma update_Some_unfold:
haftmann@34975:   "map_of (update k v al) x = Some y \<longleftrightarrow>
haftmann@34975:     x = k \<and> v = y \<or> x \<noteq> k \<and> map_of al x = Some y"
schirmer@19234:   by (simp add: update_conv' map_upd_Some_unfold)
schirmer@19234: 
haftmann@34975: lemma image_update [simp]:
haftmann@34975:   "x \<notin> A \<Longrightarrow> map_of (update x y al) ` A = map_of al ` A"
haftmann@46133:   by (simp add: update_conv')
schirmer@19234: 
wenzelm@56327: definition updates :: "'key list \<Rightarrow> 'val list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327:   where "updates ks vs = fold (case_prod update) (zip ks vs)"
schirmer@19234: 
haftmann@34975: lemma updates_simps [simp]:
haftmann@34975:   "updates [] vs ps = ps"
haftmann@34975:   "updates ks [] ps = ps"
haftmann@34975:   "updates (k#ks) (v#vs) ps = updates ks vs (update k v ps)"
haftmann@34975:   by (simp_all add: updates_def)
haftmann@34975: 
haftmann@34975: lemma updates_key_simp [simp]:
haftmann@34975:   "updates (k # ks) vs ps =
haftmann@34975:     (case vs of [] \<Rightarrow> ps | v # vs \<Rightarrow> updates ks vs (update k v ps))"
haftmann@34975:   by (cases vs) simp_all
haftmann@34975: 
haftmann@34975: lemma updates_conv': "map_of (updates ks vs al) = (map_of al)(ks[\<mapsto>]vs)"
haftmann@34975: proof -
blanchet@55414:   have "map_of \<circ> fold (case_prod update) (zip ks vs) =
wenzelm@56327:       fold (\<lambda>(k, v) f. f(k \<mapsto> v)) (zip ks vs) \<circ> map_of"
haftmann@39921:     by (rule fold_commute) (auto simp add: fun_eq_iff update_conv')
wenzelm@56327:   then show ?thesis
wenzelm@56327:     by (auto simp add: updates_def fun_eq_iff map_upds_fold_map_upd foldl_conv_fold split_def)
haftmann@34975: qed
schirmer@19234: 
schirmer@19234: lemma updates_conv: "map_of (updates ks vs al) k = ((map_of al)(ks[\<mapsto>]vs)) k"
haftmann@34975:   by (simp add: updates_conv')
schirmer@19234: 
schirmer@19234: lemma distinct_updates:
schirmer@19234:   assumes "distinct (map fst al)"
schirmer@19234:   shows "distinct (map fst (updates ks vs al))"
haftmann@34975: proof -
haftmann@46133:   have "distinct (fold
haftmann@37458:        (\<lambda>(k, v) al. if k \<in> set al then al else al @ [k])
haftmann@37458:        (zip ks vs) (map fst al))"
haftmann@37458:     by (rule fold_invariant [of "zip ks vs" "\<lambda>_. True"]) (auto intro: assms)
blanchet@55414:   moreover have "map fst \<circ> fold (case_prod update) (zip ks vs) =
wenzelm@56327:       fold (\<lambda>(k, v) al. if k \<in> set al then al else al @ [k]) (zip ks vs) \<circ> map fst"
blanchet@55414:     by (rule fold_commute) (simp add: update_keys split_def case_prod_beta comp_def)
wenzelm@56327:   ultimately show ?thesis
wenzelm@56327:     by (simp add: updates_def fun_eq_iff)
haftmann@34975: qed
schirmer@19234: 
schirmer@19234: lemma updates_append1[simp]: "size ks < size vs \<Longrightarrow>
wenzelm@56327:     updates (ks@[k]) vs al = update k (vs!size ks) (updates ks vs al)"
wenzelm@20503:   by (induct ks arbitrary: vs al) (auto split: list.splits)
schirmer@19234: 
schirmer@19234: lemma updates_list_update_drop[simp]:
wenzelm@56327:   "size ks \<le> i \<Longrightarrow> i < size vs \<Longrightarrow>
wenzelm@56327:     updates ks (vs[i:=v]) al = updates ks vs al"
wenzelm@56327:   by (induct ks arbitrary: al vs i) (auto split: list.splits nat.splits)
schirmer@19234: 
wenzelm@56327: lemma update_updates_conv_if:
wenzelm@56327:   "map_of (updates xs ys (update x y al)) =
wenzelm@56327:     map_of
wenzelm@56327:      (if x \<in> set (take (length ys) xs)
wenzelm@56327:       then updates xs ys al
wenzelm@56327:       else (update x y (updates xs ys al)))"
schirmer@19234:   by (simp add: updates_conv' update_conv' map_upd_upds_conv_if)
schirmer@19234: 
schirmer@19234: lemma updates_twist [simp]:
wenzelm@56327:   "k \<notin> set ks \<Longrightarrow>
wenzelm@56327:     map_of (updates ks vs (update k v al)) = map_of (update k v (updates ks vs al))"
wenzelm@46507:   by (simp add: updates_conv' update_conv')
schirmer@19234: 
wenzelm@56327: lemma updates_apply_notin [simp]:
wenzelm@56327:   "k \<notin> set ks \<Longrightarrow> map_of (updates ks vs al) k = map_of al k"
schirmer@19234:   by (simp add: updates_conv)
schirmer@19234: 
wenzelm@56327: lemma updates_append_drop [simp]:
wenzelm@56327:   "size xs = size ys \<Longrightarrow> updates (xs @ zs) ys al = updates xs ys al"
wenzelm@20503:   by (induct xs arbitrary: ys al) (auto split: list.splits)
schirmer@19234: 
wenzelm@56327: lemma updates_append2_drop [simp]:
wenzelm@56327:   "size xs = size ys \<Longrightarrow> updates xs (ys @ zs) al = updates xs ys al"
wenzelm@20503:   by (induct xs arbitrary: ys al) (auto split: list.splits)
schirmer@19234: 
wenzelm@23373: 
haftmann@34975: subsection {* @{text delete} *}
haftmann@34975: 
wenzelm@56327: definition delete :: "'key \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327:   where delete_eq: "delete k = filter (\<lambda>(k', _). k \<noteq> k')"
haftmann@34975: 
haftmann@34975: lemma delete_simps [simp]:
haftmann@34975:   "delete k [] = []"
wenzelm@56327:   "delete k (p # ps) = (if fst p = k then delete k ps else p # delete k ps)"
haftmann@34975:   by (auto simp add: delete_eq)
haftmann@34975: 
haftmann@34975: lemma delete_conv': "map_of (delete k al) = (map_of al)(k := None)"
nipkow@39302:   by (induct al) (auto simp add: fun_eq_iff)
haftmann@34975: 
haftmann@34975: corollary delete_conv: "map_of (delete k al) k' = ((map_of al)(k := None)) k'"
haftmann@34975:   by (simp add: delete_conv')
haftmann@34975: 
wenzelm@56327: lemma delete_keys: "map fst (delete k al) = removeAll k (map fst al)"
haftmann@34975:   by (simp add: delete_eq removeAll_filter_not_eq filter_map split_def comp_def)
haftmann@34975: 
haftmann@34975: lemma distinct_delete:
wenzelm@56327:   assumes "distinct (map fst al)"
haftmann@34975:   shows "distinct (map fst (delete k al))"
haftmann@34975:   using assms by (simp add: delete_keys distinct_removeAll)
haftmann@34975: 
haftmann@34975: lemma delete_id [simp]: "k \<notin> fst ` set al \<Longrightarrow> delete k al = al"
haftmann@34975:   by (auto simp add: image_iff delete_eq filter_id_conv)
haftmann@34975: 
haftmann@34975: lemma delete_idem: "delete k (delete k al) = delete k al"
haftmann@34975:   by (simp add: delete_eq)
haftmann@34975: 
wenzelm@56327: lemma map_of_delete [simp]: "k' \<noteq> k \<Longrightarrow> map_of (delete k al) k' = map_of al k'"
haftmann@34975:   by (simp add: delete_conv')
haftmann@34975: 
haftmann@34975: lemma delete_notin_dom: "k \<notin> fst ` set (delete k al)"
haftmann@34975:   by (auto simp add: delete_eq)
haftmann@34975: 
haftmann@34975: lemma dom_delete_subset: "fst ` set (delete k al) \<subseteq> fst ` set al"
haftmann@34975:   by (auto simp add: delete_eq)
haftmann@34975: 
wenzelm@56327: lemma delete_update_same: "delete k (update k v al) = delete k al"
haftmann@34975:   by (induct al) simp_all
haftmann@34975: 
wenzelm@56327: lemma delete_update: "k \<noteq> l \<Longrightarrow> delete l (update k v al) = update k v (delete l al)"
haftmann@34975:   by (induct al) simp_all
haftmann@34975: 
haftmann@34975: lemma delete_twist: "delete x (delete y al) = delete y (delete x al)"
haftmann@34975:   by (simp add: delete_eq conj_commute)
haftmann@34975: 
haftmann@34975: lemma length_delete_le: "length (delete k al) \<le> length al"
haftmann@34975:   by (simp add: delete_eq)
haftmann@34975: 
haftmann@34975: 
haftmann@34975: subsection {* @{text restrict} *}
haftmann@34975: 
wenzelm@56327: definition restrict :: "'key set \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327:   where restrict_eq: "restrict A = filter (\<lambda>(k, v). k \<in> A)"
haftmann@34975: 
haftmann@34975: lemma restr_simps [simp]:
haftmann@34975:   "restrict A [] = []"
haftmann@34975:   "restrict A (p#ps) = (if fst p \<in> A then p # restrict A ps else restrict A ps)"
haftmann@34975:   by (auto simp add: restrict_eq)
haftmann@34975: 
haftmann@34975: lemma restr_conv': "map_of (restrict A al) = ((map_of al)|` A)"
haftmann@34975: proof
haftmann@34975:   fix k
haftmann@34975:   show "map_of (restrict A al) k = ((map_of al)|` A) k"
haftmann@34975:     by (induct al) (simp, cases "k \<in> A", auto)
haftmann@34975: qed
haftmann@34975: 
haftmann@34975: corollary restr_conv: "map_of (restrict A al) k = ((map_of al)|` A) k"
haftmann@34975:   by (simp add: restr_conv')
haftmann@34975: 
haftmann@34975: lemma distinct_restr:
haftmann@34975:   "distinct (map fst al) \<Longrightarrow> distinct (map fst (restrict A al))"
haftmann@34975:   by (induct al) (auto simp add: restrict_eq)
haftmann@34975: 
wenzelm@56327: lemma restr_empty [simp]:
wenzelm@56327:   "restrict {} al = []"
haftmann@34975:   "restrict A [] = []"
haftmann@34975:   by (induct al) (auto simp add: restrict_eq)
haftmann@34975: 
haftmann@34975: lemma restr_in [simp]: "x \<in> A \<Longrightarrow> map_of (restrict A al) x = map_of al x"
haftmann@34975:   by (simp add: restr_conv')
haftmann@34975: 
haftmann@34975: lemma restr_out [simp]: "x \<notin> A \<Longrightarrow> map_of (restrict A al) x = None"
haftmann@34975:   by (simp add: restr_conv')
haftmann@34975: 
haftmann@34975: lemma dom_restr [simp]: "fst ` set (restrict A al) = fst ` set al \<inter> A"
haftmann@34975:   by (induct al) (auto simp add: restrict_eq)
haftmann@34975: 
haftmann@34975: lemma restr_upd_same [simp]: "restrict (-{x}) (update x y al) = restrict (-{x}) al"
haftmann@34975:   by (induct al) (auto simp add: restrict_eq)
haftmann@34975: 
haftmann@34975: lemma restr_restr [simp]: "restrict A (restrict B al) = restrict (A\<inter>B) al"
haftmann@34975:   by (induct al) (auto simp add: restrict_eq)
haftmann@34975: 
haftmann@34975: lemma restr_update[simp]:
wenzelm@56327:  "map_of (restrict D (update x y al)) =
haftmann@34975:   map_of ((if x \<in> D then (update x y (restrict (D-{x}) al)) else restrict D al))"
haftmann@34975:   by (simp add: restr_conv' update_conv')
haftmann@34975: 
haftmann@34975: lemma restr_delete [simp]:
wenzelm@56327:   "delete x (restrict D al) = (if x \<in> D then restrict (D - {x}) al else restrict D al)"
wenzelm@56327:   apply (simp add: delete_eq restrict_eq)
wenzelm@56327:   apply (auto simp add: split_def)
haftmann@34975: proof -
wenzelm@56327:   have "\<And>y. y \<noteq> x \<longleftrightarrow> x \<noteq> y"
wenzelm@56327:     by auto
haftmann@34975:   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]"
haftmann@34975:     by simp
haftmann@34975:   assume "x \<notin> D"
wenzelm@56327:   then have "\<And>y. y \<in> D \<longleftrightarrow> y \<in> D \<and> x \<noteq> y"
wenzelm@56327:     by auto
haftmann@34975:   then show "[p \<leftarrow> al . fst p \<in> D \<and> x \<noteq> fst p] = [p \<leftarrow> al . fst p \<in> D]"
haftmann@34975:     by simp
haftmann@34975: qed
haftmann@34975: 
haftmann@34975: lemma update_restr:
wenzelm@56327:   "map_of (update x y (restrict D al)) = map_of (update x y (restrict (D - {x}) al))"
haftmann@34975:   by (simp add: update_conv' restr_conv') (rule fun_upd_restrict)
schirmer@19234: 
bulwahn@45867: lemma update_restr_conv [simp]:
wenzelm@56327:   "x \<in> D \<Longrightarrow>
wenzelm@56327:     map_of (update x y (restrict D al)) = map_of (update x y (restrict (D - {x}) al))"
haftmann@34975:   by (simp add: update_conv' restr_conv')
haftmann@34975: 
wenzelm@56327: lemma restr_updates [simp]:
wenzelm@56327:   "length xs = length ys \<Longrightarrow> set xs \<subseteq> D \<Longrightarrow>
wenzelm@56327:     map_of (restrict D (updates xs ys al)) =
wenzelm@56327:       map_of (updates xs ys (restrict (D - set xs) al))"
haftmann@34975:   by (simp add: updates_conv' restr_conv')
haftmann@34975: 
haftmann@34975: lemma restr_delete_twist: "(restrict A (delete a ps)) = delete a (restrict A ps)"
haftmann@34975:   by (induct ps) auto
haftmann@34975: 
haftmann@34975: 
haftmann@34975: subsection {* @{text clearjunk} *}
haftmann@34975: 
wenzelm@56327: function clearjunk  :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327: where
wenzelm@56327:   "clearjunk [] = []"
wenzelm@56327: | "clearjunk (p#ps) = p # clearjunk (delete (fst p) ps)"
haftmann@34975:   by pat_completeness auto
wenzelm@56327: termination
wenzelm@56327:   by (relation "measure length") (simp_all add: less_Suc_eq_le length_delete_le)
haftmann@34975: 
wenzelm@56327: lemma map_of_clearjunk: "map_of (clearjunk al) = map_of al"
wenzelm@56327:   by (induct al rule: clearjunk.induct) (simp_all add: fun_eq_iff)
haftmann@34975: 
wenzelm@56327: lemma clearjunk_keys_set: "set (map fst (clearjunk al)) = set (map fst al)"
wenzelm@56327:   by (induct al rule: clearjunk.induct) (simp_all add: delete_keys)
haftmann@34975: 
wenzelm@56327: lemma dom_clearjunk: "fst ` set (clearjunk al) = fst ` set al"
haftmann@34975:   using clearjunk_keys_set by simp
haftmann@34975: 
wenzelm@56327: lemma distinct_clearjunk [simp]: "distinct (map fst (clearjunk al))"
wenzelm@56327:   by (induct al rule: clearjunk.induct) (simp_all del: set_map add: clearjunk_keys_set delete_keys)
haftmann@34975: 
wenzelm@56327: lemma ran_clearjunk: "ran (map_of (clearjunk al)) = ran (map_of al)"
haftmann@34975:   by (simp add: map_of_clearjunk)
haftmann@34975: 
wenzelm@56327: lemma ran_map_of: "ran (map_of al) = snd ` set (clearjunk al)"
haftmann@34975: proof -
haftmann@34975:   have "ran (map_of al) = ran (map_of (clearjunk al))"
haftmann@34975:     by (simp add: ran_clearjunk)
haftmann@34975:   also have "\<dots> = snd ` set (clearjunk al)"
haftmann@34975:     by (simp add: ran_distinct)
haftmann@34975:   finally show ?thesis .
haftmann@34975: qed
haftmann@34975: 
wenzelm@56327: lemma clearjunk_update: "clearjunk (update k v al) = update k v (clearjunk al)"
wenzelm@56327:   by (induct al rule: clearjunk.induct) (simp_all add: delete_update)
schirmer@19234: 
wenzelm@56327: lemma clearjunk_updates: "clearjunk (updates ks vs al) = updates ks vs (clearjunk al)"
haftmann@34975: proof -
blanchet@55414:   have "clearjunk \<circ> fold (case_prod update) (zip ks vs) =
blanchet@55414:     fold (case_prod update) (zip ks vs) \<circ> clearjunk"
blanchet@55414:     by (rule fold_commute) (simp add: clearjunk_update case_prod_beta o_def)
wenzelm@56327:   then show ?thesis
wenzelm@56327:     by (simp add: updates_def fun_eq_iff)
haftmann@34975: qed
haftmann@34975: 
wenzelm@56327: lemma clearjunk_delete: "clearjunk (delete x al) = delete x (clearjunk al)"
haftmann@34975:   by (induct al rule: clearjunk.induct) (auto simp add: delete_idem delete_twist)
haftmann@34975: 
wenzelm@56327: lemma clearjunk_restrict: "clearjunk (restrict A al) = restrict A (clearjunk al)"
haftmann@34975:   by (induct al rule: clearjunk.induct) (auto simp add: restr_delete_twist)
haftmann@34975: 
wenzelm@56327: lemma distinct_clearjunk_id [simp]: "distinct (map fst al) \<Longrightarrow> clearjunk al = al"
haftmann@34975:   by (induct al rule: clearjunk.induct) auto
haftmann@34975: 
wenzelm@56327: lemma clearjunk_idem: "clearjunk (clearjunk al) = clearjunk al"
haftmann@34975:   by simp
haftmann@34975: 
wenzelm@56327: lemma length_clearjunk: "length (clearjunk al) \<le> length al"
haftmann@34975: proof (induct al rule: clearjunk.induct [case_names Nil Cons])
wenzelm@56327:   case Nil
wenzelm@56327:   then show ?case by simp
haftmann@34975: next
haftmann@34975:   case (Cons kv al)
wenzelm@56327:   moreover have "length (delete (fst kv) al) \<le> length al"
wenzelm@56327:     by (fact length_delete_le)
wenzelm@56327:   ultimately have "length (clearjunk (delete (fst kv) al)) \<le> length al"
wenzelm@56327:     by (rule order_trans)
wenzelm@56327:   then show ?case
wenzelm@56327:     by simp
haftmann@34975: qed
haftmann@34975: 
haftmann@34975: lemma delete_map:
haftmann@34975:   assumes "\<And>kv. fst (f kv) = fst kv"
haftmann@34975:   shows "delete k (map f ps) = map f (delete k ps)"
haftmann@34975:   by (simp add: delete_eq filter_map comp_def split_def assms)
haftmann@34975: 
haftmann@34975: lemma clearjunk_map:
haftmann@34975:   assumes "\<And>kv. fst (f kv) = fst kv"
haftmann@34975:   shows "clearjunk (map f ps) = map f (clearjunk ps)"
haftmann@34975:   by (induct ps rule: clearjunk.induct [case_names Nil Cons])
haftmann@34975:     (simp_all add: clearjunk_delete delete_map assms)
haftmann@34975: 
haftmann@34975: 
haftmann@34975: subsection {* @{text map_ran} *}
haftmann@34975: 
wenzelm@56327: definition map_ran :: "('key \<Rightarrow> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327:   where "map_ran f = map (\<lambda>(k, v). (k, f k v))"
haftmann@34975: 
haftmann@34975: lemma map_ran_simps [simp]:
haftmann@34975:   "map_ran f [] = []"
haftmann@34975:   "map_ran f ((k, v) # ps) = (k, f k v) # map_ran f ps"
haftmann@34975:   by (simp_all add: map_ran_def)
haftmann@34975: 
wenzelm@56327: lemma dom_map_ran: "fst ` set (map_ran f al) = fst ` set al"
haftmann@34975:   by (simp add: map_ran_def image_image split_def)
wenzelm@56327: 
wenzelm@56327: lemma map_ran_conv: "map_of (map_ran f al) k = map_option (f k) (map_of al k)"
schirmer@19234:   by (induct al) auto
schirmer@19234: 
wenzelm@56327: lemma distinct_map_ran: "distinct (map fst al) \<Longrightarrow> distinct (map fst (map_ran f al))"
haftmann@34975:   by (simp add: map_ran_def split_def comp_def)
schirmer@19234: 
wenzelm@56327: lemma map_ran_filter: "map_ran f [p\<leftarrow>ps. fst p \<noteq> a] = [p\<leftarrow>map_ran f ps. fst p \<noteq> a]"
haftmann@34975:   by (simp add: map_ran_def filter_map split_def comp_def)
schirmer@19234: 
wenzelm@56327: lemma clearjunk_map_ran: "clearjunk (map_ran f al) = map_ran f (clearjunk al)"
haftmann@34975:   by (simp add: map_ran_def split_def clearjunk_map)
schirmer@19234: 
wenzelm@23373: 
haftmann@34975: subsection {* @{text merge} *}
haftmann@34975: 
wenzelm@56327: definition merge :: "('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@56327:   where "merge qs ps = foldr (\<lambda>(k, v). update k v) ps qs"
haftmann@34975: 
haftmann@34975: lemma merge_simps [simp]:
haftmann@34975:   "merge qs [] = qs"
haftmann@34975:   "merge qs (p#ps) = update (fst p) (snd p) (merge qs ps)"
haftmann@34975:   by (simp_all add: merge_def split_def)
haftmann@34975: 
wenzelm@56327: lemma merge_updates: "merge qs ps = updates (rev (map fst ps)) (rev (map snd ps)) qs"
haftmann@47397:   by (simp add: merge_def updates_def foldr_conv_fold zip_rev zip_map_fst_snd)
schirmer@19234: 
schirmer@19234: lemma dom_merge: "fst ` set (merge xs ys) = fst ` set xs \<union> fst ` set ys"
wenzelm@20503:   by (induct ys arbitrary: xs) (auto simp add: dom_update)
schirmer@19234: 
schirmer@19234: lemma distinct_merge:
schirmer@19234:   assumes "distinct (map fst xs)"
schirmer@19234:   shows "distinct (map fst (merge xs ys))"
wenzelm@56327:   using assms by (simp add: merge_updates distinct_updates)
schirmer@19234: 
wenzelm@56327: lemma clearjunk_merge: "clearjunk (merge xs ys) = merge (clearjunk xs) ys"
haftmann@34975:   by (simp add: merge_updates clearjunk_updates)
schirmer@19234: 
wenzelm@56327: lemma merge_conv': "map_of (merge xs ys) = map_of xs ++ map_of ys"
haftmann@34975: proof -
blanchet@55414:   have "map_of \<circ> fold (case_prod update) (rev ys) =
wenzelm@56327:       fold (\<lambda>(k, v) m. m(k \<mapsto> v)) (rev ys) \<circ> map_of"
blanchet@55414:     by (rule fold_commute) (simp add: update_conv' case_prod_beta split_def fun_eq_iff)
haftmann@34975:   then show ?thesis
haftmann@47397:     by (simp add: merge_def map_add_map_of_foldr foldr_conv_fold fun_eq_iff)
schirmer@19234: qed
schirmer@19234: 
wenzelm@56327: corollary merge_conv: "map_of (merge xs ys) k = (map_of xs ++ map_of ys) k"
haftmann@34975:   by (simp add: merge_conv')
schirmer@19234: 
haftmann@34975: lemma merge_empty: "map_of (merge [] ys) = map_of ys"
schirmer@19234:   by (simp add: merge_conv')
schirmer@19234: 
wenzelm@56327: lemma merge_assoc [simp]: "map_of (merge m1 (merge m2 m3)) = map_of (merge (merge m1 m2) m3)"
schirmer@19234:   by (simp add: merge_conv')
schirmer@19234: 
wenzelm@56327: lemma merge_Some_iff:
wenzelm@56327:   "map_of (merge m n) k = Some x \<longleftrightarrow>
wenzelm@56327:     map_of n k = Some x \<or> map_of n k = None \<and> map_of m k = Some x"
schirmer@19234:   by (simp add: merge_conv' map_add_Some_iff)
schirmer@19234: 
wenzelm@45605: lemmas merge_SomeD [dest!] = merge_Some_iff [THEN iffD1]
schirmer@19234: 
wenzelm@56327: lemma merge_find_right [simp]: "map_of n k = Some v \<Longrightarrow> map_of (merge m n) k = Some v"
schirmer@19234:   by (simp add: merge_conv')
schirmer@19234: 
wenzelm@56327: lemma merge_None [iff]:
schirmer@19234:   "(map_of (merge m n) k = None) = (map_of n k = None \<and> map_of m k = None)"
schirmer@19234:   by (simp add: merge_conv')
schirmer@19234: 
wenzelm@56327: lemma merge_upd [simp]:
schirmer@19234:   "map_of (merge m (update k v n)) = map_of (update k v (merge m n))"
schirmer@19234:   by (simp add: update_conv' merge_conv')
schirmer@19234: 
wenzelm@56327: lemma merge_updatess [simp]:
schirmer@19234:   "map_of (merge m (updates xs ys n)) = map_of (updates xs ys (merge m n))"
schirmer@19234:   by (simp add: updates_conv' merge_conv')
schirmer@19234: 
wenzelm@56327: lemma merge_append: "map_of (xs @ ys) = map_of (merge ys xs)"
schirmer@19234:   by (simp add: merge_conv')
schirmer@19234: 
wenzelm@23373: 
haftmann@34975: subsection {* @{text compose} *}
haftmann@34975: 
wenzelm@56327: function compose :: "('key \<times> 'a) list \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> ('key \<times> 'b) list"
wenzelm@56327: where
wenzelm@56327:   "compose [] ys = []"
wenzelm@56327: | "compose (x # xs) ys =
wenzelm@56327:     (case map_of ys (snd x) of
wenzelm@56327:       None \<Rightarrow> compose (delete (fst x) xs) ys
wenzelm@56327:     | Some v \<Rightarrow> (fst x, v) # compose xs ys)"
haftmann@34975:   by pat_completeness auto
wenzelm@56327: termination
wenzelm@56327:   by (relation "measure (length \<circ> fst)") (simp_all add: less_Suc_eq_le length_delete_le)
schirmer@19234: 
wenzelm@56327: lemma compose_first_None [simp]:
wenzelm@56327:   assumes "map_of xs k = None"
schirmer@19234:   shows "map_of (compose xs ys) k = None"
wenzelm@56327:   using assms by (induct xs ys rule: compose.induct) (auto split: option.splits split_if_asm)
schirmer@19234: 
wenzelm@56327: lemma compose_conv: "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
haftmann@22916: proof (induct xs ys rule: compose.induct)
wenzelm@56327:   case 1
wenzelm@56327:   then show ?case by simp
schirmer@19234: next
wenzelm@56327:   case (2 x xs ys)
wenzelm@56327:   show ?case
schirmer@19234:   proof (cases "map_of ys (snd x)")
wenzelm@56327:     case None
wenzelm@56327:     with 2 have hyp: "map_of (compose (delete (fst x) xs) ys) k =
wenzelm@56327:         (map_of ys \<circ>\<^sub>m map_of (delete (fst x) xs)) k"
schirmer@19234:       by simp
schirmer@19234:     show ?thesis
schirmer@19234:     proof (cases "fst x = k")
schirmer@19234:       case True
schirmer@19234:       from True delete_notin_dom [of k xs]
schirmer@19234:       have "map_of (delete (fst x) xs) k = None"
wenzelm@32960:         by (simp add: map_of_eq_None_iff)
schirmer@19234:       with hyp show ?thesis
wenzelm@32960:         using True None
wenzelm@32960:         by simp
schirmer@19234:     next
schirmer@19234:       case False
schirmer@19234:       from False have "map_of (delete (fst x) xs) k = map_of xs k"
wenzelm@32960:         by simp
schirmer@19234:       with hyp show ?thesis
wenzelm@56327:         using False None by (simp add: map_comp_def)
schirmer@19234:     qed
schirmer@19234:   next
schirmer@19234:     case (Some v)
haftmann@22916:     with 2
schirmer@19234:     have "map_of (compose xs ys) k = (map_of ys \<circ>\<^sub>m map_of xs) k"
schirmer@19234:       by simp
schirmer@19234:     with Some show ?thesis
schirmer@19234:       by (auto simp add: map_comp_def)
schirmer@19234:   qed
schirmer@19234: qed
wenzelm@56327: 
wenzelm@56327: lemma compose_conv': "map_of (compose xs ys) = (map_of ys \<circ>\<^sub>m map_of xs)"
schirmer@19234:   by (rule ext) (rule compose_conv)
schirmer@19234: 
schirmer@19234: lemma compose_first_Some [simp]:
wenzelm@56327:   assumes "map_of xs k = Some v"
schirmer@19234:   shows "map_of (compose xs ys) k = map_of ys v"
wenzelm@56327:   using assms by (simp add: compose_conv)
schirmer@19234: 
schirmer@19234: lemma dom_compose: "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
haftmann@22916: proof (induct xs ys rule: compose.induct)
wenzelm@56327:   case 1
wenzelm@56327:   then show ?case by simp
schirmer@19234: next
haftmann@22916:   case (2 x xs ys)
schirmer@19234:   show ?case
schirmer@19234:   proof (cases "map_of ys (snd x)")
schirmer@19234:     case None
haftmann@22916:     with "2.hyps"
schirmer@19234:     have "fst ` set (compose (delete (fst x) xs) ys) \<subseteq> fst ` set (delete (fst x) xs)"
schirmer@19234:       by simp
schirmer@19234:     also
schirmer@19234:     have "\<dots> \<subseteq> fst ` set xs"
schirmer@19234:       by (rule dom_delete_subset)
schirmer@19234:     finally show ?thesis
schirmer@19234:       using None
schirmer@19234:       by auto
schirmer@19234:   next
schirmer@19234:     case (Some v)
haftmann@22916:     with "2.hyps"
schirmer@19234:     have "fst ` set (compose xs ys) \<subseteq> fst ` set xs"
schirmer@19234:       by simp
schirmer@19234:     with Some show ?thesis
schirmer@19234:       by auto
schirmer@19234:   qed
schirmer@19234: qed
schirmer@19234: 
schirmer@19234: lemma distinct_compose:
wenzelm@56327:   assumes "distinct (map fst xs)"
wenzelm@56327:   shows "distinct (map fst (compose xs ys))"
wenzelm@56327:   using assms
haftmann@22916: proof (induct xs ys rule: compose.induct)
wenzelm@56327:   case 1
wenzelm@56327:   then show ?case by simp
schirmer@19234: next
haftmann@22916:   case (2 x xs ys)
schirmer@19234:   show ?case
schirmer@19234:   proof (cases "map_of ys (snd x)")
schirmer@19234:     case None
haftmann@22916:     with 2 show ?thesis by simp
schirmer@19234:   next
schirmer@19234:     case (Some v)
wenzelm@56327:     with 2 dom_compose [of xs ys] show ?thesis
wenzelm@56327:       by auto
schirmer@19234:   qed
schirmer@19234: qed
schirmer@19234: 
wenzelm@56327: lemma compose_delete_twist: "compose (delete k xs) ys = delete k (compose xs ys)"
haftmann@22916: proof (induct xs ys rule: compose.induct)
wenzelm@56327:   case 1
wenzelm@56327:   then show ?case by simp
schirmer@19234: next
haftmann@22916:   case (2 x xs ys)
schirmer@19234:   show ?case
schirmer@19234:   proof (cases "map_of ys (snd x)")
schirmer@19234:     case None
wenzelm@56327:     with 2 have hyp: "compose (delete k (delete (fst x) xs)) ys =
wenzelm@56327:         delete k (compose (delete (fst x) xs) ys)"
schirmer@19234:       by simp
schirmer@19234:     show ?thesis
schirmer@19234:     proof (cases "fst x = k")
schirmer@19234:       case True
wenzelm@56327:       with None hyp show ?thesis
wenzelm@32960:         by (simp add: delete_idem)
schirmer@19234:     next
schirmer@19234:       case False
wenzelm@56327:       from None False hyp show ?thesis
wenzelm@32960:         by (simp add: delete_twist)
schirmer@19234:     qed
schirmer@19234:   next
schirmer@19234:     case (Some v)
wenzelm@56327:     with 2 have hyp: "compose (delete k xs) ys = delete k (compose xs ys)"
wenzelm@56327:       by simp
schirmer@19234:     with Some show ?thesis
schirmer@19234:       by simp
schirmer@19234:   qed
schirmer@19234: qed
schirmer@19234: 
schirmer@19234: lemma compose_clearjunk: "compose xs (clearjunk ys) = compose xs ys"
wenzelm@56327:   by (induct xs ys rule: compose.induct)
wenzelm@56327:     (auto simp add: map_of_clearjunk split: option.splits)
wenzelm@56327: 
schirmer@19234: lemma clearjunk_compose: "clearjunk (compose xs ys) = compose (clearjunk xs) ys"
schirmer@19234:   by (induct xs rule: clearjunk.induct)
wenzelm@56327:     (auto split: option.splits simp add: clearjunk_delete delete_idem compose_delete_twist)
wenzelm@56327: 
wenzelm@56327: lemma compose_empty [simp]: "compose xs [] = []"
haftmann@22916:   by (induct xs) (auto simp add: compose_delete_twist)
schirmer@19234: 
schirmer@19234: lemma compose_Some_iff:
wenzelm@56327:   "(map_of (compose xs ys) k = Some v) \<longleftrightarrow>
wenzelm@56327:     (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = Some v)"
schirmer@19234:   by (simp add: compose_conv map_comp_Some_iff)
schirmer@19234: 
schirmer@19234: lemma map_comp_None_iff:
wenzelm@56327:   "map_of (compose xs ys) k = None \<longleftrightarrow>
wenzelm@56327:     (map_of xs k = None \<or> (\<exists>k'. map_of xs k = Some k' \<and> map_of ys k' = None))"
schirmer@19234:   by (simp add: compose_conv map_comp_None_iff)
schirmer@19234: 
wenzelm@56327: 
bulwahn@45869: subsection {* @{text map_entry} *}
bulwahn@45869: 
bulwahn@45869: fun map_entry :: "'key \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bulwahn@45869: where
bulwahn@45869:   "map_entry k f [] = []"
wenzelm@56327: | "map_entry k f (p # ps) =
wenzelm@56327:     (if fst p = k then (k, f (snd p)) # ps else p # map_entry k f ps)"
bulwahn@45869: 
bulwahn@45869: lemma map_of_map_entry:
wenzelm@56327:   "map_of (map_entry k f xs) =
wenzelm@56327:     (map_of xs)(k := case map_of xs k of None \<Rightarrow> None | Some v' \<Rightarrow> Some (f v'))"
wenzelm@56327:   by (induct xs) auto
bulwahn@45869: 
wenzelm@56327: lemma dom_map_entry: "fst ` set (map_entry k f xs) = fst ` set xs"
wenzelm@56327:   by (induct xs) auto
bulwahn@45869: 
bulwahn@45869: lemma distinct_map_entry:
bulwahn@45869:   assumes "distinct (map fst xs)"
bulwahn@45869:   shows "distinct (map fst (map_entry k f xs))"
wenzelm@56327:   using assms by (induct xs) (auto simp add: dom_map_entry)
wenzelm@56327: 
bulwahn@45869: 
bulwahn@45868: subsection {* @{text map_default} *}
bulwahn@45868: 
bulwahn@45868: fun map_default :: "'key \<Rightarrow> 'val \<Rightarrow> ('val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bulwahn@45868: where
bulwahn@45868:   "map_default k v f [] = [(k, v)]"
wenzelm@56327: | "map_default k v f (p # ps) =
wenzelm@56327:     (if fst p = k then (k, f (snd p)) # ps else p # map_default k v f ps)"
bulwahn@45868: 
bulwahn@45868: lemma map_of_map_default:
wenzelm@56327:   "map_of (map_default k v f xs) =
wenzelm@56327:     (map_of xs)(k := case map_of xs k of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f v'))"
wenzelm@56327:   by (induct xs) auto
bulwahn@45868: 
wenzelm@56327: lemma dom_map_default: "fst ` set (map_default k v f xs) = insert k (fst ` set xs)"
wenzelm@56327:   by (induct xs) auto
bulwahn@45868: 
bulwahn@45868: lemma distinct_map_default:
bulwahn@45868:   assumes "distinct (map fst xs)"
bulwahn@45868:   shows "distinct (map fst (map_default k v f xs))"
wenzelm@56327:   using assms by (induct xs) (auto simp add: dom_map_default)
bulwahn@45868: 
bulwahn@46171: hide_const (open) update updates delete restrict clearjunk merge compose map_entry
bulwahn@45884: 
schirmer@19234: end




