--- a/src/HOL/Library/Mapping.thy Wed Feb 17 09:48:52 2010 +0100
+++ b/src/HOL/Library/Mapping.thy Wed Feb 17 09:48:52 2010 +0100
@@ -3,50 +3,58 @@
header {* An abstract view on maps for code generation. *}
theory Mapping
-imports Map Main
+imports Main
begin
subsection {* Type definition and primitive operations *}
-datatype ('a, 'b) map = Map "'a \<rightharpoonup> 'b"
+datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b"
-definition empty :: "('a, 'b) map" where
- "empty = Map (\<lambda>_. None)"
-
-primrec lookup :: "('a, 'b) map \<Rightarrow> 'a \<rightharpoonup> 'b" where
- "lookup (Map f) = f"
+definition empty :: "('a, 'b) mapping" where
+ "empty = Mapping (\<lambda>_. None)"
-primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
- "update k v (Map f) = Map (f (k \<mapsto> v))"
+primrec lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<rightharpoonup> 'b" where
+ "lookup (Mapping f) = f"
-primrec delete :: "'a \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
- "delete k (Map f) = Map (f (k := None))"
+primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
+ "update k v (Mapping f) = Mapping (f (k \<mapsto> v))"
-primrec keys :: "('a, 'b) map \<Rightarrow> 'a set" where
- "keys (Map f) = dom f"
+primrec delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
+ "delete k (Mapping f) = Mapping (f (k := None))"
subsection {* Derived operations *}
-definition size :: "('a, 'b) map \<Rightarrow> nat" where
- "size m = (if finite (keys m) then card (keys m) else 0)"
+definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where
+ "keys m = dom (lookup m)"
-definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
+definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
+ "is_empty m \<longleftrightarrow> dom (lookup m) = {}"
+
+definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
+ "size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)"
+
+definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
"replace k v m = (if lookup m k = None then m else update k v m)"
-definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) map" where
- "tabulate ks f = Map (map_of (map (\<lambda>k. (k, f k)) ks))"
+definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where
+ "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))"
-definition bulkload :: "'a list \<Rightarrow> (nat, 'a) map" where
- "bulkload xs = Map (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
+definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
+ "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
subsection {* Properties *}
-lemma lookup_inject:
+lemma lookup_inject [simp]:
"lookup m = lookup n \<longleftrightarrow> m = n"
by (cases m, cases n) simp
+lemma mapping_eqI:
+ assumes "lookup m = lookup n"
+ shows "m = n"
+ using assms by simp
+
lemma lookup_empty [simp]:
"lookup empty = Map.empty"
by (simp add: empty_def)
@@ -55,98 +63,82 @@
"lookup (update k v m) = (lookup m) (k \<mapsto> v)"
by (cases m) simp
-lemma lookup_delete:
- "lookup (delete k m) k = None"
- "k \<noteq> l \<Longrightarrow> lookup (delete k m) l = lookup m l"
- by (cases m, simp)+
+lemma lookup_delete [simp]:
+ "lookup (delete k m) = (lookup m) (k := None)"
+ by (cases m) simp
-lemma lookup_tabulate:
+lemma lookup_tabulate [simp]:
"lookup (tabulate ks f) = (Some o f) |` set ks"
by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
-lemma lookup_bulkload:
+lemma lookup_bulkload [simp]:
"lookup (bulkload xs) = (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
- unfolding bulkload_def by simp
+ by (simp add: bulkload_def)
lemma update_update:
"update k v (update k w m) = update k v m"
"k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
- by (cases m, simp add: expand_fun_eq)+
+ by (rule mapping_eqI, simp add: fun_upd_twist)+
-lemma replace_update:
- "lookup m k = None \<Longrightarrow> replace k v m = m"
- "lookup m k \<noteq> None \<Longrightarrow> replace k v m = update k v m"
- by (auto simp add: replace_def)
-
-lemma delete_empty [simp]:
- "delete k empty = empty"
- by (simp add: empty_def)
+lemma update_delete [simp]:
+ "update k v (delete k m) = update k v m"
+ by (rule mapping_eqI) simp
lemma delete_update:
"delete k (update k v m) = delete k m"
"k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
- by (cases m, simp add: expand_fun_eq)+
-
-lemma update_delete [simp]:
- "update k v (delete k m) = update k v m"
- by (cases m) simp
-
-lemma keys_empty [simp]:
- "keys empty = {}"
- unfolding empty_def by simp
+ by (rule mapping_eqI, simp add: fun_upd_twist)+
-lemma keys_update [simp]:
- "keys (update k v m) = insert k (keys m)"
- by (cases m) simp
+lemma delete_empty [simp]:
+ "delete k empty = empty"
+ by (rule mapping_eqI) simp
-lemma keys_delete [simp]:
- "keys (delete k m) = keys m - {k}"
- by (cases m) simp
-
-lemma keys_tabulate [simp]:
- "keys (tabulate ks f) = set ks"
- by (auto simp add: tabulate_def dest: map_of_SomeD intro!: weak_map_of_SomeI)
+lemma replace_update:
+ "k \<notin> dom (lookup m) \<Longrightarrow> replace k v m = m"
+ "k \<in> dom (lookup m) \<Longrightarrow> replace k v m = update k v m"
+ by (rule mapping_eqI, auto simp add: replace_def fun_upd_twist)+
lemma size_empty [simp]:
"size empty = 0"
- by (simp add: size_def keys_empty)
+ by (simp add: size_def)
lemma size_update:
- "finite (keys m) \<Longrightarrow> size (update k v m) =
- (if k \<in> keys m then size m else Suc (size m))"
- by (simp add: size_def keys_update)
- (auto simp only: card_insert card_Suc_Diff1)
+ "finite (dom (lookup m)) \<Longrightarrow> size (update k v m) =
+ (if k \<in> dom (lookup m) then size m else Suc (size m))"
+ by (auto simp add: size_def insert_dom)
lemma size_delete:
- "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
- by (simp add: size_def keys_delete)
+ "size (delete k m) = (if k \<in> dom (lookup m) then size m - 1 else size m)"
+ by (simp add: size_def)
lemma size_tabulate:
"size (tabulate ks f) = length (remdups ks)"
- by (simp add: size_def keys_tabulate distinct_card [of "remdups ks", symmetric])
+ by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def)
lemma bulkload_tabulate:
"bulkload xs = tabulate [0..<length xs] (nth xs)"
- by (rule sym)
- (auto simp add: bulkload_def tabulate_def expand_fun_eq map_of_eq_None_iff comp_def)
+ by (rule mapping_eqI) (simp add: expand_fun_eq)
subsection {* Some technical code lemmas *}
lemma [code]:
- "map_case f m = f (Mapping.lookup m)"
+ "mapping_case f m = f (Mapping.lookup m)"
by (cases m) simp
lemma [code]:
- "map_rec f m = f (Mapping.lookup m)"
+ "mapping_rec f m = f (Mapping.lookup m)"
by (cases m) simp
lemma [code]:
- "Nat.size (m :: (_, _) map) = 0"
+ "Nat.size (m :: (_, _) mapping) = 0"
by (cases m) simp
lemma [code]:
- "map_size f g m = 0"
+ "mapping_size f g m = 0"
by (cases m) simp
+
+hide (open) const empty is_empty lookup update delete keys size replace tabulate bulkload
+
end
\ No newline at end of file