src/HOL/Library/Mapping.thy
changeset 29708 e40b70d38909
child 29814 15344c0899e1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Mapping.thy	Mon Feb 02 13:56:22 2009 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +(*  Title:      HOL/Library/Mapping.thy
     1.5 +    Author:     Florian Haftmann, TU Muenchen
     1.6 +*)
     1.7 +
     1.8 +header {* An abstract view on maps for code generation. *}
     1.9 +
    1.10 +theory Mapping
    1.11 +imports Map
    1.12 +begin
    1.13 +
    1.14 +subsection {* Type definition and primitive operations *}
    1.15 +
    1.16 +datatype ('a, 'b) map = Map "'a \<rightharpoonup> 'b"
    1.17 +
    1.18 +definition empty :: "('a, 'b) map" where
    1.19 +  "empty = Map (\<lambda>_. None)"
    1.20 +
    1.21 +primrec lookup :: "('a, 'b) map \<Rightarrow> 'a \<rightharpoonup> 'b" where
    1.22 +  "lookup (Map f) = f"
    1.23 +
    1.24 +primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    1.25 +  "update k v (Map f) = Map (f (k \<mapsto> v))"
    1.26 +
    1.27 +primrec delete :: "'a \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    1.28 +  "delete k (Map f) = Map (f (k := None))"
    1.29 +
    1.30 +primrec keys :: "('a, 'b) map \<Rightarrow> 'a set" where
    1.31 +  "keys (Map f) = dom f"
    1.32 +
    1.33 +
    1.34 +subsection {* Derived operations *}
    1.35 +
    1.36 +definition size :: "('a, 'b) map \<Rightarrow> nat" where
    1.37 +  "size m = (if finite (keys m) then card (keys m) else 0)"
    1.38 +
    1.39 +definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) map" where
    1.40 +  "tabulate ks f = Map (map_of (map (\<lambda>k. (k, f k)) ks))"
    1.41 +
    1.42 +
    1.43 +subsection {* Properties *}
    1.44 +
    1.45 +lemma lookup_inject:
    1.46 +  "lookup m = lookup n \<longleftrightarrow> m = n"
    1.47 +  by (cases m, cases n) simp
    1.48 +
    1.49 +lemma lookup_empty [simp]:
    1.50 +  "lookup empty = Map.empty"
    1.51 +  by (simp add: empty_def)
    1.52 +
    1.53 +lemma lookup_update [simp]:
    1.54 +  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
    1.55 +  by (cases m) simp
    1.56 +
    1.57 +lemma lookup_delete:
    1.58 +  "lookup (delete k m) k = None"
    1.59 +  "k \<noteq> l \<Longrightarrow> lookup (delete k m) l = lookup m l"
    1.60 +  by (cases m, simp)+
    1.61 +
    1.62 +lemma lookup_tabulate:
    1.63 +  "lookup (tabulate ks f) = (Some o f) |` set ks"
    1.64 +  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
    1.65 +
    1.66 +lemma update_update:
    1.67 +  "update k v (update k w m) = update k v m"
    1.68 +  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
    1.69 +  by (cases m, simp add: expand_fun_eq)+
    1.70 +
    1.71 +lemma delete_empty [simp]:
    1.72 +  "delete k empty = empty"
    1.73 +  by (simp add: empty_def)
    1.74 +
    1.75 +lemma delete_update:
    1.76 +  "delete k (update k v m) = delete k m"
    1.77 +  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
    1.78 +  by (cases m, simp add: expand_fun_eq)+
    1.79 +
    1.80 +lemma update_delete [simp]:
    1.81 +  "update k v (delete k m) = update k v m"
    1.82 +  by (cases m) simp
    1.83 +
    1.84 +lemma keys_empty [simp]:
    1.85 +  "keys empty = {}"
    1.86 +  unfolding empty_def by simp
    1.87 +
    1.88 +lemma keys_update [simp]:
    1.89 +  "keys (update k v m) = insert k (keys m)"
    1.90 +  by (cases m) simp
    1.91 +
    1.92 +lemma keys_delete [simp]:
    1.93 +  "keys (delete k m) = keys m - {k}"
    1.94 +  by (cases m) simp
    1.95 +
    1.96 +lemma keys_tabulate [simp]:
    1.97 +  "keys (tabulate ks f) = set ks"
    1.98 +  by (auto simp add: tabulate_def dest: map_of_SomeD intro!: weak_map_of_SomeI)
    1.99 +
   1.100 +lemma size_empty [simp]:
   1.101 +  "size empty = 0"
   1.102 +  by (simp add: size_def keys_empty)
   1.103 +
   1.104 +lemma size_update:
   1.105 +  "finite (keys m) \<Longrightarrow> size (update k v m) =
   1.106 +    (if k \<in> keys m then size m else Suc (size m))"
   1.107 +  by (simp add: size_def keys_update)
   1.108 +    (auto simp only: card_insert card_Suc_Diff1)
   1.109 +
   1.110 +lemma size_delete:
   1.111 +  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
   1.112 +  by (simp add: size_def keys_delete)
   1.113 +
   1.114 +lemma size_tabulate:
   1.115 +  "size (tabulate ks f) = length (remdups ks)"
   1.116 +  by (simp add: size_def keys_tabulate distinct_card [of "remdups ks", symmetric])
   1.117 +
   1.118 +end
   1.119 \ No newline at end of file