added Mapping.thy to Library
authorhaftmann
Mon, 02 Feb 2009 13:56:22 +0100
changeset 29708 e40b70d38909
parent 29707 01cae7ad8576
child 29709 cf8476cc440d
added Mapping.thy to Library
src/HOL/IsaMakefile
src/HOL/Library/Library.thy
src/HOL/Library/Mapping.thy
--- a/src/HOL/IsaMakefile	Mon Feb 02 13:56:22 2009 +0100
+++ b/src/HOL/IsaMakefile	Mon Feb 02 13:56:22 2009 +0100
@@ -331,7 +331,7 @@
   Library/Binomial.thy Library/Eval_Witness.thy	\
   Library/Code_Index.thy Library/Code_Char.thy				\
   Library/Code_Char_chr.thy Library/Code_Integer.thy			\
-  Library/Numeral_Type.thy	Library/Reflection.thy		\
+  Library/Mapping.thy	Library/Numeral_Type.thy	Library/Reflection.thy		\
   Library/Boolean_Algebra.thy Library/Countable.thy	\
   Library/RBT.thy	Library/Univ_Poly.thy	\
   Library/Enum.thy Library/Float.thy $(SRC)/Tools/float.ML $(SRC)/HOL/Tools/float_arith.ML \
--- a/src/HOL/Library/Library.thy	Mon Feb 02 13:56:22 2009 +0100
+++ b/src/HOL/Library/Library.thy	Mon Feb 02 13:56:22 2009 +0100
@@ -24,6 +24,7 @@
   FuncSet
   Infinite_Set
   ListVector
+  Mapping
   Multiset
   Nat_Infinity
   Nested_Environment
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Mapping.thy	Mon Feb 02 13:56:22 2009 +0100
@@ -0,0 +1,115 @@
+(*  Title:      HOL/Library/Mapping.thy
+    Author:     Florian Haftmann, TU Muenchen
+*)
+
+header {* An abstract view on maps for code generation. *}
+
+theory Mapping
+imports Map
+begin
+
+subsection {* Type definition and primitive operations *}
+
+datatype ('a, 'b) map = Map "'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"
+
+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 delete :: "'a \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
+  "delete k (Map f) = Map (f (k := None))"
+
+primrec keys :: "('a, 'b) map \<Rightarrow> 'a set" where
+  "keys (Map f) = dom f"
+
+
+subsection {* Derived operations *}
+
+definition size :: "('a, 'b) map \<Rightarrow> nat" where
+  "size m = (if finite (keys m) then card (keys m) else 0)"
+
+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))"
+
+
+subsection {* Properties *}
+
+lemma lookup_inject:
+  "lookup m = lookup n \<longleftrightarrow> m = n"
+  by (cases m, cases n) simp
+
+lemma lookup_empty [simp]:
+  "lookup empty = Map.empty"
+  by (simp add: empty_def)
+
+lemma lookup_update [simp]:
+  "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_tabulate:
+  "lookup (tabulate ks f) = (Some o f) |` set ks"
+  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
+
+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)+
+
+lemma delete_empty [simp]:
+  "delete k empty = empty"
+  by (simp add: empty_def)
+
+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
+
+lemma keys_update [simp]:
+  "keys (update k v m) = insert k (keys m)"
+  by (cases m) 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 size_empty [simp]:
+  "size empty = 0"
+  by (simp add: size_def keys_empty)
+
+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)
+
+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)
+
+lemma size_tabulate:
+  "size (tabulate ks f) = length (remdups ks)"
+  by (simp add: size_def keys_tabulate distinct_card [of "remdups ks", symmetric])
+
+end
\ No newline at end of file