src/HOL/Library/Mapping.thy
changeset 35157 73cd6f78c86d
parent 33640 0d82107dc07a
child 35194 a6c573d13385
     1.1 --- a/src/HOL/Library/Mapping.thy	Wed Feb 17 09:48:52 2010 +0100
     1.2 +++ b/src/HOL/Library/Mapping.thy	Wed Feb 17 09:48:52 2010 +0100
     1.3 @@ -3,50 +3,58 @@
     1.4  header {* An abstract view on maps for code generation. *}
     1.5  
     1.6  theory Mapping
     1.7 -imports Map Main
     1.8 +imports Main
     1.9  begin
    1.10  
    1.11  subsection {* Type definition and primitive operations *}
    1.12  
    1.13 -datatype ('a, 'b) map = Map "'a \<rightharpoonup> 'b"
    1.14 +datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b"
    1.15  
    1.16 -definition empty :: "('a, 'b) map" where
    1.17 -  "empty = Map (\<lambda>_. None)"
    1.18 -
    1.19 -primrec lookup :: "('a, 'b) map \<Rightarrow> 'a \<rightharpoonup> 'b" where
    1.20 -  "lookup (Map f) = f"
    1.21 +definition empty :: "('a, 'b) mapping" where
    1.22 +  "empty = Mapping (\<lambda>_. None)"
    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 +primrec lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<rightharpoonup> 'b" where
    1.27 +  "lookup (Mapping f) = f"
    1.28  
    1.29 -primrec delete :: "'a \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    1.30 -  "delete k (Map f) = Map (f (k := None))"
    1.31 +primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    1.32 +  "update k v (Mapping f) = Mapping (f (k \<mapsto> v))"
    1.33  
    1.34 -primrec keys :: "('a, 'b) map \<Rightarrow> 'a set" where
    1.35 -  "keys (Map f) = dom f"
    1.36 +primrec delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    1.37 +  "delete k (Mapping f) = Mapping (f (k := None))"
    1.38  
    1.39  
    1.40  subsection {* Derived operations *}
    1.41  
    1.42 -definition size :: "('a, 'b) map \<Rightarrow> nat" where
    1.43 -  "size m = (if finite (keys m) then card (keys m) else 0)"
    1.44 +definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where
    1.45 +  "keys m = dom (lookup m)"
    1.46  
    1.47 -definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
    1.48 +definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
    1.49 +  "is_empty m \<longleftrightarrow> dom (lookup m) = {}"
    1.50 +
    1.51 +definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
    1.52 +  "size m = (if finite (dom (lookup m)) then card (dom (lookup m)) else 0)"
    1.53 +
    1.54 +definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    1.55    "replace k v m = (if lookup m k = None then m else update k v m)"
    1.56  
    1.57 -definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) map" where
    1.58 -  "tabulate ks f = Map (map_of (map (\<lambda>k. (k, f k)) ks))"
    1.59 +definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where
    1.60 +  "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))"
    1.61  
    1.62 -definition bulkload :: "'a list \<Rightarrow> (nat, 'a) map" where
    1.63 -  "bulkload xs = Map (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
    1.64 +definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
    1.65 +  "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
    1.66  
    1.67  
    1.68  subsection {* Properties *}
    1.69  
    1.70 -lemma lookup_inject:
    1.71 +lemma lookup_inject [simp]:
    1.72    "lookup m = lookup n \<longleftrightarrow> m = n"
    1.73    by (cases m, cases n) simp
    1.74  
    1.75 +lemma mapping_eqI:
    1.76 +  assumes "lookup m = lookup n"
    1.77 +  shows "m = n"
    1.78 +  using assms by simp
    1.79 +
    1.80  lemma lookup_empty [simp]:
    1.81    "lookup empty = Map.empty"
    1.82    by (simp add: empty_def)
    1.83 @@ -55,98 +63,82 @@
    1.84    "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
    1.85    by (cases m) simp
    1.86  
    1.87 -lemma lookup_delete:
    1.88 -  "lookup (delete k m) k = None"
    1.89 -  "k \<noteq> l \<Longrightarrow> lookup (delete k m) l = lookup m l"
    1.90 -  by (cases m, simp)+
    1.91 +lemma lookup_delete [simp]:
    1.92 +  "lookup (delete k m) = (lookup m) (k := None)"
    1.93 +  by (cases m) simp
    1.94  
    1.95 -lemma lookup_tabulate:
    1.96 +lemma lookup_tabulate [simp]:
    1.97    "lookup (tabulate ks f) = (Some o f) |` set ks"
    1.98    by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
    1.99  
   1.100 -lemma lookup_bulkload:
   1.101 +lemma lookup_bulkload [simp]:
   1.102    "lookup (bulkload xs) = (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
   1.103 -  unfolding bulkload_def by simp
   1.104 +  by (simp add: bulkload_def)
   1.105  
   1.106  lemma update_update:
   1.107    "update k v (update k w m) = update k v m"
   1.108    "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
   1.109 -  by (cases m, simp add: expand_fun_eq)+
   1.110 +  by (rule mapping_eqI, simp add: fun_upd_twist)+
   1.111  
   1.112 -lemma replace_update:
   1.113 -  "lookup m k = None \<Longrightarrow> replace k v m = m"
   1.114 -  "lookup m k \<noteq> None \<Longrightarrow> replace k v m = update k v m"
   1.115 -  by (auto simp add: replace_def)
   1.116 -
   1.117 -lemma delete_empty [simp]:
   1.118 -  "delete k empty = empty"
   1.119 -  by (simp add: empty_def)
   1.120 +lemma update_delete [simp]:
   1.121 +  "update k v (delete k m) = update k v m"
   1.122 +  by (rule mapping_eqI) simp
   1.123  
   1.124  lemma delete_update:
   1.125    "delete k (update k v m) = delete k m"
   1.126    "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
   1.127 -  by (cases m, simp add: expand_fun_eq)+
   1.128 -
   1.129 -lemma update_delete [simp]:
   1.130 -  "update k v (delete k m) = update k v m"
   1.131 -  by (cases m) simp
   1.132 -
   1.133 -lemma keys_empty [simp]:
   1.134 -  "keys empty = {}"
   1.135 -  unfolding empty_def by simp
   1.136 +  by (rule mapping_eqI, simp add: fun_upd_twist)+
   1.137  
   1.138 -lemma keys_update [simp]:
   1.139 -  "keys (update k v m) = insert k (keys m)"
   1.140 -  by (cases m) simp
   1.141 +lemma delete_empty [simp]:
   1.142 +  "delete k empty = empty"
   1.143 +  by (rule mapping_eqI) simp
   1.144  
   1.145 -lemma keys_delete [simp]:
   1.146 -  "keys (delete k m) = keys m - {k}"
   1.147 -  by (cases m) simp
   1.148 -
   1.149 -lemma keys_tabulate [simp]:
   1.150 -  "keys (tabulate ks f) = set ks"
   1.151 -  by (auto simp add: tabulate_def dest: map_of_SomeD intro!: weak_map_of_SomeI)
   1.152 +lemma replace_update:
   1.153 +  "k \<notin> dom (lookup m) \<Longrightarrow> replace k v m = m"
   1.154 +  "k \<in> dom (lookup m) \<Longrightarrow> replace k v m = update k v m"
   1.155 +  by (rule mapping_eqI, auto simp add: replace_def fun_upd_twist)+
   1.156  
   1.157  lemma size_empty [simp]:
   1.158    "size empty = 0"
   1.159 -  by (simp add: size_def keys_empty)
   1.160 +  by (simp add: size_def)
   1.161  
   1.162  lemma size_update:
   1.163 -  "finite (keys m) \<Longrightarrow> size (update k v m) =
   1.164 -    (if k \<in> keys m then size m else Suc (size m))"
   1.165 -  by (simp add: size_def keys_update)
   1.166 -    (auto simp only: card_insert card_Suc_Diff1)
   1.167 +  "finite (dom (lookup m)) \<Longrightarrow> size (update k v m) =
   1.168 +    (if k \<in> dom (lookup m) then size m else Suc (size m))"
   1.169 +  by (auto simp add: size_def insert_dom)
   1.170  
   1.171  lemma size_delete:
   1.172 -  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
   1.173 -  by (simp add: size_def keys_delete)
   1.174 +  "size (delete k m) = (if k \<in> dom (lookup m) then size m - 1 else size m)"
   1.175 +  by (simp add: size_def)
   1.176  
   1.177  lemma size_tabulate:
   1.178    "size (tabulate ks f) = length (remdups ks)"
   1.179 -  by (simp add: size_def keys_tabulate distinct_card [of "remdups ks", symmetric])
   1.180 +  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def)
   1.181  
   1.182  lemma bulkload_tabulate:
   1.183    "bulkload xs = tabulate [0..<length xs] (nth xs)"
   1.184 -  by (rule sym)
   1.185 -    (auto simp add: bulkload_def tabulate_def expand_fun_eq map_of_eq_None_iff comp_def)
   1.186 +  by (rule mapping_eqI) (simp add: expand_fun_eq)
   1.187  
   1.188  
   1.189  subsection {* Some technical code lemmas *}
   1.190  
   1.191  lemma [code]:
   1.192 -  "map_case f m = f (Mapping.lookup m)"
   1.193 +  "mapping_case f m = f (Mapping.lookup m)"
   1.194    by (cases m) simp
   1.195  
   1.196  lemma [code]:
   1.197 -  "map_rec f m = f (Mapping.lookup m)"
   1.198 +  "mapping_rec f m = f (Mapping.lookup m)"
   1.199    by (cases m) simp
   1.200  
   1.201  lemma [code]:
   1.202 -  "Nat.size (m :: (_, _) map) = 0"
   1.203 +  "Nat.size (m :: (_, _) mapping) = 0"
   1.204    by (cases m) simp
   1.205  
   1.206  lemma [code]:
   1.207 -  "map_size f g m = 0"
   1.208 +  "mapping_size f g m = 0"
   1.209    by (cases m) simp
   1.210  
   1.211 +
   1.212 +hide (open) const empty is_empty lookup update delete keys size replace tabulate bulkload
   1.213 +
   1.214  end
   1.215 \ No newline at end of file