
bulwahn@44897: (* Title: HOL/Library/AList_Mapping.thy
bulwahn@44897:    Author: Florian Haftmann, TU Muenchen
bulwahn@44897: *)
bulwahn@44897: 
bulwahn@44897: header {* Implementation of mappings with Association Lists *}
bulwahn@44897: 
bulwahn@44897: theory AList_Mapping
bulwahn@46238: imports AList Mapping
bulwahn@44897: begin
bulwahn@44897: 
kuncar@49929: lift_definition Mapping :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) mapping" is map_of .
bulwahn@44897: 
bulwahn@44897: code_datatype Mapping
bulwahn@44897: 
bulwahn@44897: lemma lookup_Mapping [simp, code]:
bulwahn@44897:   "Mapping.lookup (Mapping xs) = map_of xs"
kuncar@49929:   by transfer rule
bulwahn@44897: 
bulwahn@44897: lemma keys_Mapping [simp, code]:
kuncar@49929:   "Mapping.keys (Mapping xs) = set (map fst xs)" 
kuncar@49929:   by transfer (simp add: dom_map_of_conv_image_fst)
bulwahn@44897: 
bulwahn@44897: lemma empty_Mapping [code]:
bulwahn@44897:   "Mapping.empty = Mapping []"
kuncar@49929:   by transfer simp
bulwahn@44897: 
bulwahn@44897: lemma is_empty_Mapping [code]:
bulwahn@44897:   "Mapping.is_empty (Mapping xs) \<longleftrightarrow> List.null xs"
kuncar@49929:   by (case_tac xs) (simp_all add: is_empty_def null_def)
bulwahn@44897: 
bulwahn@44897: lemma update_Mapping [code]:
bulwahn@46238:   "Mapping.update k v (Mapping xs) = Mapping (AList.update k v xs)"
kuncar@49929:   by transfer (simp add: update_conv')
bulwahn@44897: 
bulwahn@44897: lemma delete_Mapping [code]:
bulwahn@46238:   "Mapping.delete k (Mapping xs) = Mapping (AList.delete k xs)"
kuncar@49929:   by transfer (simp add: delete_conv')
bulwahn@44897: 
bulwahn@44897: lemma ordered_keys_Mapping [code]:
bulwahn@44897:   "Mapping.ordered_keys (Mapping xs) = sort (remdups (map fst xs))"
bulwahn@44897:   by (simp only: ordered_keys_def keys_Mapping sorted_list_of_set_sort_remdups) simp
bulwahn@44897: 
bulwahn@44897: lemma size_Mapping [code]:
bulwahn@44897:   "Mapping.size (Mapping xs) = length (remdups (map fst xs))"
bulwahn@44897:   by (simp add: size_def length_remdups_card_conv dom_map_of_conv_image_fst)
bulwahn@44897: 
bulwahn@44897: lemma tabulate_Mapping [code]:
bulwahn@44897:   "Mapping.tabulate ks f = Mapping (map (\<lambda>k. (k, f k)) ks)"
kuncar@49929:   by transfer (simp add: map_of_map_restrict)
bulwahn@44897: 
bulwahn@44897: lemma bulkload_Mapping [code]:
bulwahn@44897:   "Mapping.bulkload vs = Mapping (map (\<lambda>n. (n, vs ! n)) [0..<length vs])"
kuncar@49929:   by transfer (simp add: map_of_map_restrict fun_eq_iff)
bulwahn@44897: 
bulwahn@44897: lemma equal_Mapping [code]:
bulwahn@44897:   "HOL.equal (Mapping xs) (Mapping ys) \<longleftrightarrow>
bulwahn@44897:     (let ks = map fst xs; ls = map fst ys
bulwahn@44897:     in (\<forall>l\<in>set ls. l \<in> set ks) \<and> (\<forall>k\<in>set ks. k \<in> set ls \<and> map_of xs k = map_of ys k))"
bulwahn@44897: proof -
bulwahn@44897:   have aux: "\<And>a b xs. (a, b) \<in> set xs \<Longrightarrow> a \<in> fst ` set xs"
bulwahn@44897:     by (auto simp add: image_def intro!: bexI)
kuncar@49929:   show ?thesis apply transfer 
kuncar@49929:   by (auto intro!: map_of_eqI) (auto dest!: map_of_eq_dom intro: aux)
bulwahn@44897: qed
bulwahn@44897: 
bulwahn@44897: lemma [code nbe]:
bulwahn@44897:   "HOL.equal (x :: ('a, 'b) mapping) x \<longleftrightarrow> True"
bulwahn@44897:   by (fact equal_refl)
bulwahn@44897:   
bulwahn@44913: end




