update RBT_Mapping, AList_Mapping and Mapping to use lifting/transfer
authorkuncar
Thu Oct 18 15:52:33 2012 +0200 (2012-10-18)
changeset 4992970300f1b6835
parent 49928 e3f0a92de280
child 49933 c897dc77e813
update RBT_Mapping, AList_Mapping and Mapping to use lifting/transfer
src/HOL/Library/AList_Mapping.thy
src/HOL/Library/Mapping.thy
src/HOL/Library/RBT_Mapping.thy
src/HOL/ex/Execute_Choice.thy
     1.1 --- a/src/HOL/Library/AList_Mapping.thy	Thu Oct 18 15:52:32 2012 +0200
     1.2 +++ b/src/HOL/Library/AList_Mapping.thy	Thu Oct 18 15:52:33 2012 +0200
     1.3 @@ -8,34 +8,33 @@
     1.4  imports AList Mapping
     1.5  begin
     1.6  
     1.7 -definition Mapping :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) mapping" where
     1.8 -  "Mapping xs = Mapping.Mapping (map_of xs)"
     1.9 +lift_definition Mapping :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) mapping" is map_of .
    1.10  
    1.11  code_datatype Mapping
    1.12  
    1.13  lemma lookup_Mapping [simp, code]:
    1.14    "Mapping.lookup (Mapping xs) = map_of xs"
    1.15 -  by (simp add: Mapping_def)
    1.16 +  by transfer rule
    1.17  
    1.18  lemma keys_Mapping [simp, code]:
    1.19 -  "Mapping.keys (Mapping xs) = set (map fst xs)"
    1.20 -  by (simp add: keys_def dom_map_of_conv_image_fst)
    1.21 +  "Mapping.keys (Mapping xs) = set (map fst xs)" 
    1.22 +  by transfer (simp add: dom_map_of_conv_image_fst)
    1.23  
    1.24  lemma empty_Mapping [code]:
    1.25    "Mapping.empty = Mapping []"
    1.26 -  by (rule mapping_eqI) simp
    1.27 +  by transfer simp
    1.28  
    1.29  lemma is_empty_Mapping [code]:
    1.30    "Mapping.is_empty (Mapping xs) \<longleftrightarrow> List.null xs"
    1.31 -  by (cases xs) (simp_all add: is_empty_def null_def)
    1.32 +  by (case_tac xs) (simp_all add: is_empty_def null_def)
    1.33  
    1.34  lemma update_Mapping [code]:
    1.35    "Mapping.update k v (Mapping xs) = Mapping (AList.update k v xs)"
    1.36 -  by (rule mapping_eqI) (simp add: update_conv')
    1.37 +  by transfer (simp add: update_conv')
    1.38  
    1.39  lemma delete_Mapping [code]:
    1.40    "Mapping.delete k (Mapping xs) = Mapping (AList.delete k xs)"
    1.41 -  by (rule mapping_eqI) (simp add: delete_conv')
    1.42 +  by transfer (simp add: delete_conv')
    1.43  
    1.44  lemma ordered_keys_Mapping [code]:
    1.45    "Mapping.ordered_keys (Mapping xs) = sort (remdups (map fst xs))"
    1.46 @@ -47,11 +46,11 @@
    1.47  
    1.48  lemma tabulate_Mapping [code]:
    1.49    "Mapping.tabulate ks f = Mapping (map (\<lambda>k. (k, f k)) ks)"
    1.50 -  by (rule mapping_eqI) (simp add: map_of_map_restrict)
    1.51 +  by transfer (simp add: map_of_map_restrict)
    1.52  
    1.53  lemma bulkload_Mapping [code]:
    1.54    "Mapping.bulkload vs = Mapping (map (\<lambda>n. (n, vs ! n)) [0..<length vs])"
    1.55 -  by (rule mapping_eqI) (simp add: map_of_map_restrict fun_eq_iff)
    1.56 +  by transfer (simp add: map_of_map_restrict fun_eq_iff)
    1.57  
    1.58  lemma equal_Mapping [code]:
    1.59    "HOL.equal (Mapping xs) (Mapping ys) \<longleftrightarrow>
    1.60 @@ -60,9 +59,8 @@
    1.61  proof -
    1.62    have aux: "\<And>a b xs. (a, b) \<in> set xs \<Longrightarrow> a \<in> fst ` set xs"
    1.63      by (auto simp add: image_def intro!: bexI)
    1.64 -  show ?thesis
    1.65 -    by (auto intro!: map_of_eqI simp add: Let_def equal Mapping_def)
    1.66 -      (auto dest!: map_of_eq_dom intro: aux)
    1.67 +  show ?thesis apply transfer 
    1.68 +  by (auto intro!: map_of_eqI) (auto dest!: map_of_eq_dom intro: aux)
    1.69  qed
    1.70  
    1.71  lemma [code nbe]:
     2.1 --- a/src/HOL/Library/Mapping.thy	Thu Oct 18 15:52:32 2012 +0200
     2.2 +++ b/src/HOL/Library/Mapping.thy	Thu Oct 18 15:52:33 2012 +0200
     2.3 @@ -1,64 +1,46 @@
     2.4 -(* Author: Florian Haftmann, TU Muenchen *)
     2.5 +(*  Title:      HOL/Library/Mapping.thy
     2.6 +    Author:     Florian Haftmann and Ondrej Kuncar
     2.7 +*)
     2.8  
     2.9  header {* An abstract view on maps for code generation. *}
    2.10  
    2.11  theory Mapping
    2.12 -imports Main
    2.13 +imports Main "~~/src/HOL/Library/Quotient_Option"
    2.14  begin
    2.15  
    2.16  subsection {* Type definition and primitive operations *}
    2.17  
    2.18  typedef ('a, 'b) mapping = "UNIV :: ('a \<rightharpoonup> 'b) set"
    2.19 -  morphisms lookup Mapping ..
    2.20 +  morphisms rep Mapping ..
    2.21  
    2.22 -lemma lookup_Mapping [simp]:
    2.23 -  "lookup (Mapping f) = f"
    2.24 -  by (rule Mapping_inverse) rule
    2.25 +setup_lifting(no_code) type_definition_mapping
    2.26  
    2.27 -lemma Mapping_lookup [simp]:
    2.28 -  "Mapping (lookup m) = m"
    2.29 -  by (fact lookup_inverse)
    2.30 +lift_definition empty :: "('a, 'b) mapping" is "(\<lambda>_. None)" .
    2.31  
    2.32 -lemma Mapping_inject [simp]:
    2.33 -  "Mapping f = Mapping g \<longleftrightarrow> f = g"
    2.34 -  by (simp add: Mapping_inject)
    2.35 +lift_definition lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<Rightarrow> 'b option" is "\<lambda>m k. m k" .
    2.36 +
    2.37 +lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is "\<lambda>k v m. m(k \<mapsto> v)" .
    2.38  
    2.39 -lemma mapping_eq_iff:
    2.40 -  "m = n \<longleftrightarrow> lookup m = lookup n"
    2.41 -  by (simp add: lookup_inject)
    2.42 +lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is "\<lambda>k m. m(k := None)" .
    2.43  
    2.44 -lemma mapping_eqI:
    2.45 -  "lookup m = lookup n \<Longrightarrow> m = n"
    2.46 -  by (simp add: mapping_eq_iff)
    2.47 +lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" is dom .
    2.48  
    2.49 -definition empty :: "('a, 'b) mapping" where
    2.50 -  "empty = Mapping (\<lambda>_. None)"
    2.51 +lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" is
    2.52 +  "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" .
    2.53  
    2.54 -definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    2.55 -  "update k v m = Mapping ((lookup m)(k \<mapsto> v))"
    2.56 +lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" is
    2.57 +  "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" .
    2.58  
    2.59 -definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    2.60 -  "delete k m = Mapping ((lookup m)(k := None))"
    2.61 -
    2.62 +lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping" is
    2.63 +  "\<lambda>f g m. (Option.map g \<circ> m \<circ> f)" .
    2.64  
    2.65  subsection {* Functorial structure *}
    2.66  
    2.67 -definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping" where
    2.68 -  "map f g m = Mapping (Option.map g \<circ> lookup m \<circ> f)"
    2.69 -
    2.70 -lemma lookup_map [simp]:
    2.71 -  "lookup (map f g m) = Option.map g \<circ> lookup m \<circ> f"
    2.72 -  by (simp add: map_def)
    2.73 -
    2.74  enriched_type map: map
    2.75 -  by (simp_all add: mapping_eq_iff fun_eq_iff Option.map.compositionality Option.map.id)
    2.76 -
    2.77 +  by (transfer, auto simp add: fun_eq_iff Option.map.compositionality Option.map.id)+
    2.78  
    2.79  subsection {* Derived operations *}
    2.80  
    2.81 -definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where
    2.82 -  "keys m = dom (lookup m)"
    2.83 -
    2.84  definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
    2.85    "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
    2.86  
    2.87 @@ -74,122 +56,94 @@
    2.88  definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    2.89    "default k v m = (if k \<in> keys m then m else update k v m)"
    2.90  
    2.91 -definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
    2.92 -  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
    2.93 -    | Some v \<Rightarrow> update k (f v) m)" 
    2.94 +lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
    2.95 +  "\<lambda>k f m. (case m k of None \<Rightarrow> m
    2.96 +    | Some v \<Rightarrow> m (k \<mapsto> (f v)))" .
    2.97 +
    2.98 +lemma map_entry_code [code]: "map_entry k f m = (case lookup m k of None \<Rightarrow> m
    2.99 +    | Some v \<Rightarrow> update k (f v) m)" by transfer rule
   2.100  
   2.101  definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
   2.102    "map_default k v f m = map_entry k f (default k v m)" 
   2.103  
   2.104 -definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where
   2.105 -  "tabulate ks f = Mapping (map_of (List.map (\<lambda>k. (k, f k)) ks))"
   2.106 -
   2.107 -definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
   2.108 -  "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
   2.109 -
   2.110 -
   2.111  subsection {* Properties *}
   2.112  
   2.113 -lemma keys_is_none_lookup [code_unfold]:
   2.114 +lemma keys_is_none_rep [code_unfold]:
   2.115    "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
   2.116 -  by (auto simp add: keys_def is_none_def)
   2.117 -
   2.118 -lemma lookup_empty [simp]:
   2.119 -  "lookup empty = Map.empty"
   2.120 -  by (simp add: empty_def)
   2.121 -
   2.122 -lemma lookup_update [simp]:
   2.123 -  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
   2.124 -  by (simp add: update_def)
   2.125 +  by transfer (auto simp add: is_none_def)
   2.126  
   2.127 -lemma lookup_delete [simp]:
   2.128 -  "lookup (delete k m) = (lookup m) (k := None)"
   2.129 -  by (simp add: delete_def)
   2.130 -
   2.131 -lemma lookup_map_entry [simp]:
   2.132 -  "lookup (map_entry k f m) = (lookup m) (k := Option.map f (lookup m k))"
   2.133 -  by (cases "lookup m k") (simp_all add: map_entry_def fun_eq_iff)
   2.134 -
   2.135 -lemma lookup_tabulate [simp]:
   2.136 -  "lookup (tabulate ks f) = (Some o f) |` set ks"
   2.137 -  by (induct ks) (auto simp add: tabulate_def restrict_map_def fun_eq_iff)
   2.138 -
   2.139 -lemma lookup_bulkload [simp]:
   2.140 -  "lookup (bulkload xs) = (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
   2.141 -  by (simp add: bulkload_def)
   2.142 +lemma tabulate_alt_def:
   2.143 +  "map_of (List.map (\<lambda>k. (k, f k)) ks) = (Some o f) |` set ks"
   2.144 +  by (induct ks) (auto simp add: tabulate_def restrict_map_def)
   2.145  
   2.146  lemma update_update:
   2.147    "update k v (update k w m) = update k v m"
   2.148    "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
   2.149 -  by (rule mapping_eqI, simp add: fun_upd_twist)+
   2.150 +  by (transfer, simp add: fun_upd_twist)+
   2.151  
   2.152  lemma update_delete [simp]:
   2.153    "update k v (delete k m) = update k v m"
   2.154 -  by (rule mapping_eqI) simp
   2.155 +  by transfer simp
   2.156  
   2.157  lemma delete_update:
   2.158    "delete k (update k v m) = delete k m"
   2.159    "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
   2.160 -  by (rule mapping_eqI, simp add: fun_upd_twist)+
   2.161 +  by (transfer, simp add: fun_upd_twist)+
   2.162  
   2.163  lemma delete_empty [simp]:
   2.164    "delete k empty = empty"
   2.165 -  by (rule mapping_eqI) simp
   2.166 +  by transfer simp
   2.167  
   2.168  lemma replace_update:
   2.169    "k \<notin> keys m \<Longrightarrow> replace k v m = m"
   2.170    "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
   2.171 -  by (rule mapping_eqI) (auto simp add: replace_def fun_upd_twist)+
   2.172 +  by (transfer, auto simp add: replace_def fun_upd_twist)+
   2.173  
   2.174  lemma size_empty [simp]:
   2.175    "size empty = 0"
   2.176 -  by (simp add: size_def keys_def)
   2.177 +  unfolding size_def by transfer simp
   2.178  
   2.179  lemma size_update:
   2.180    "finite (keys m) \<Longrightarrow> size (update k v m) =
   2.181      (if k \<in> keys m then size m else Suc (size m))"
   2.182 -  by (auto simp add: size_def insert_dom keys_def)
   2.183 +  unfolding size_def by transfer (auto simp add: insert_dom)
   2.184  
   2.185  lemma size_delete:
   2.186    "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
   2.187 -  by (simp add: size_def keys_def)
   2.188 +  unfolding size_def by transfer simp
   2.189  
   2.190  lemma size_tabulate [simp]:
   2.191    "size (tabulate ks f) = length (remdups ks)"
   2.192 -  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def keys_def)
   2.193 +  unfolding size_def by transfer (auto simp add: tabulate_alt_def card_set comp_def)
   2.194  
   2.195  lemma bulkload_tabulate:
   2.196    "bulkload xs = tabulate [0..<length xs] (nth xs)"
   2.197 -  by (rule mapping_eqI) (simp add: fun_eq_iff)
   2.198 +  by transfer (auto simp add: tabulate_alt_def)
   2.199  
   2.200 -lemma is_empty_empty: (*FIXME*)
   2.201 -  "is_empty m \<longleftrightarrow> m = Mapping Map.empty"
   2.202 -  by (cases m) (simp add: is_empty_def keys_def)
   2.203 -
   2.204 -lemma is_empty_empty' [simp]:
   2.205 +lemma is_empty_empty [simp]:
   2.206    "is_empty empty"
   2.207 -  by (simp add: is_empty_empty empty_def) 
   2.208 +  unfolding is_empty_def by transfer simp 
   2.209  
   2.210  lemma is_empty_update [simp]:
   2.211    "\<not> is_empty (update k v m)"
   2.212 -  by (simp add: is_empty_empty update_def)
   2.213 +  unfolding is_empty_def by transfer simp
   2.214  
   2.215  lemma is_empty_delete:
   2.216    "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
   2.217 -  by (auto simp add: delete_def is_empty_def keys_def simp del: dom_eq_empty_conv)
   2.218 +  unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
   2.219  
   2.220  lemma is_empty_replace [simp]:
   2.221    "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
   2.222 -  by (auto simp add: replace_def) (simp add: is_empty_def)
   2.223 +  unfolding is_empty_def replace_def by transfer auto
   2.224  
   2.225  lemma is_empty_default [simp]:
   2.226    "\<not> is_empty (default k v m)"
   2.227 -  by (auto simp add: default_def) (simp add: is_empty_def)
   2.228 +  unfolding is_empty_def default_def by transfer auto
   2.229  
   2.230  lemma is_empty_map_entry [simp]:
   2.231    "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
   2.232 -  by (cases "lookup m k")
   2.233 -    (auto simp add: map_entry_def, simp add: is_empty_empty)
   2.234 +  unfolding is_empty_def 
   2.235 +  apply transfer by (case_tac "m k") auto
   2.236  
   2.237  lemma is_empty_map_default [simp]:
   2.238    "\<not> is_empty (map_default k v f m)"
   2.239 @@ -197,27 +151,27 @@
   2.240  
   2.241  lemma keys_empty [simp]:
   2.242    "keys empty = {}"
   2.243 -  by (simp add: keys_def)
   2.244 +  by transfer simp
   2.245  
   2.246  lemma keys_update [simp]:
   2.247    "keys (update k v m) = insert k (keys m)"
   2.248 -  by (simp add: keys_def)
   2.249 +  by transfer simp
   2.250  
   2.251  lemma keys_delete [simp]:
   2.252    "keys (delete k m) = keys m - {k}"
   2.253 -  by (simp add: keys_def)
   2.254 +  by transfer simp
   2.255  
   2.256  lemma keys_replace [simp]:
   2.257    "keys (replace k v m) = keys m"
   2.258 -  by (auto simp add: keys_def replace_def)
   2.259 +  unfolding replace_def by transfer (simp add: insert_absorb)
   2.260  
   2.261  lemma keys_default [simp]:
   2.262    "keys (default k v m) = insert k (keys m)"
   2.263 -  by (auto simp add: keys_def default_def)
   2.264 +  unfolding default_def by transfer (simp add: insert_absorb)
   2.265  
   2.266  lemma keys_map_entry [simp]:
   2.267    "keys (map_entry k f m) = keys m"
   2.268 -  by (auto simp add: keys_def)
   2.269 +  apply transfer by (case_tac "m k") auto
   2.270  
   2.271  lemma keys_map_default [simp]:
   2.272    "keys (map_default k v f m) = insert k (keys m)"
   2.273 @@ -225,7 +179,7 @@
   2.274  
   2.275  lemma keys_tabulate [simp]:
   2.276    "keys (tabulate ks f) = set ks"
   2.277 -  by (simp add: tabulate_def keys_def map_of_map_restrict o_def)
   2.278 +  by transfer (simp add: map_of_map_restrict o_def)
   2.279  
   2.280  lemma keys_bulkload [simp]:
   2.281    "keys (bulkload xs) = {0..<length xs}"
   2.282 @@ -297,16 +251,15 @@
   2.283  instantiation mapping :: (type, type) equal
   2.284  begin
   2.285  
   2.286 -definition [code del]:
   2.287 -  "HOL.equal m n \<longleftrightarrow> lookup m = lookup n"
   2.288 +lift_definition equal_mapping :: "('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping \<Rightarrow> bool" is "op=" .
   2.289  
   2.290  instance proof
   2.291 -qed (simp add: equal_mapping_def mapping_eq_iff)
   2.292 +qed(transfer, rule)
   2.293  
   2.294  end
   2.295  
   2.296  
   2.297 -hide_const (open) empty is_empty lookup update delete ordered_keys keys size
   2.298 +hide_const (open) empty is_empty rep lookup update delete ordered_keys keys size
   2.299    replace default map_entry map_default tabulate bulkload map
   2.300  
   2.301  end
   2.302 \ No newline at end of file
     3.1 --- a/src/HOL/Library/RBT_Mapping.thy	Thu Oct 18 15:52:32 2012 +0200
     3.2 +++ b/src/HOL/Library/RBT_Mapping.thy	Thu Oct 18 15:52:33 2012 +0200
     3.3 @@ -1,4 +1,6 @@
     3.4 -(* Author: Florian Haftmann, TU Muenchen *)
     3.5 +(*  Title:      HOL/Library/RBT_Mapping.thy
     3.6 +    Author:     Florian Haftmann and Ondrej Kuncar
     3.7 +*)
     3.8  
     3.9  header {* Implementation of mappings with Red-Black Trees *}
    3.10  
    3.11 @@ -9,62 +11,69 @@
    3.12  
    3.13  subsection {* Implementation of mappings *}
    3.14  
    3.15 -definition Mapping :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) mapping" where
    3.16 -  "Mapping t = Mapping.Mapping (lookup t)"
    3.17 +lift_definition Mapping :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> ('a, 'b) mapping" is lookup .
    3.18  
    3.19  code_datatype Mapping
    3.20  
    3.21  lemma lookup_Mapping [simp, code]:
    3.22    "Mapping.lookup (Mapping t) = lookup t"
    3.23 -  by (simp add: Mapping_def)
    3.24 +   by (transfer fixing: t) rule
    3.25  
    3.26 -lemma empty_Mapping [code]:
    3.27 -  "Mapping.empty = Mapping empty"
    3.28 -  by (rule mapping_eqI) simp
    3.29 +lemma empty_Mapping [code]: "Mapping.empty = Mapping empty"
    3.30 +proof -
    3.31 +  note RBT.empty.transfer[transfer_rule del]
    3.32 +  show ?thesis by transfer simp
    3.33 +qed
    3.34  
    3.35  lemma is_empty_Mapping [code]:
    3.36    "Mapping.is_empty (Mapping t) \<longleftrightarrow> is_empty t"
    3.37 -  by (simp add: rbt_eq_iff Mapping.is_empty_empty Mapping_def)
    3.38 +  unfolding is_empty_def by (transfer fixing: t) simp
    3.39  
    3.40  lemma insert_Mapping [code]:
    3.41    "Mapping.update k v (Mapping t) = Mapping (insert k v t)"
    3.42 -  by (rule mapping_eqI) simp
    3.43 +  by (transfer fixing: t) simp
    3.44  
    3.45  lemma delete_Mapping [code]:
    3.46    "Mapping.delete k (Mapping t) = Mapping (delete k t)"
    3.47 -  by (rule mapping_eqI) simp
    3.48 +  by (transfer fixing: t) simp
    3.49  
    3.50  lemma map_entry_Mapping [code]:
    3.51    "Mapping.map_entry k f (Mapping t) = Mapping (map_entry k f t)"
    3.52 -  by (rule mapping_eqI) simp
    3.53 +  apply (transfer fixing: t) by (case_tac "lookup t k") auto
    3.54  
    3.55  lemma keys_Mapping [code]:
    3.56    "Mapping.keys (Mapping t) = set (keys t)"
    3.57 -  by (simp add: RBT.keys_def Mapping_def Mapping.keys_def lookup_def rbt_lookup_keys)
    3.58 +by (transfer fixing: t) (simp add: lookup_keys)
    3.59  
    3.60  lemma ordered_keys_Mapping [code]:
    3.61    "Mapping.ordered_keys (Mapping t) = keys t"
    3.62 -  by (rule sorted_distinct_set_unique) (simp_all add: ordered_keys_def keys_Mapping)
    3.63 +unfolding ordered_keys_def 
    3.64 +by (transfer fixing: t) (auto simp add: lookup_keys intro: sorted_distinct_set_unique)
    3.65  
    3.66  lemma Mapping_size_card_keys: (*FIXME*)
    3.67    "Mapping.size m = card (Mapping.keys m)"
    3.68 -  by (simp add: Mapping.size_def Mapping.keys_def)
    3.69 +unfolding size_def by transfer simp
    3.70  
    3.71  lemma size_Mapping [code]:
    3.72    "Mapping.size (Mapping t) = length (keys t)"
    3.73 -  by (simp add: Mapping_size_card_keys keys_Mapping distinct_card)
    3.74 +unfolding size_def
    3.75 +by (transfer fixing: t) (simp add: lookup_keys distinct_card)
    3.76  
    3.77 -lemma tabulate_Mapping [code]:
    3.78 -  "Mapping.tabulate ks f = Mapping (bulkload (List.map (\<lambda>k. (k, f k)) ks))"
    3.79 -  by (rule mapping_eqI) (simp add: map_of_map_restrict)
    3.80 -
    3.81 -lemma bulkload_Mapping [code]:
    3.82 -  "Mapping.bulkload vs = Mapping (bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
    3.83 -  by (rule mapping_eqI) (simp add: map_of_map_restrict fun_eq_iff)
    3.84 +context
    3.85 +  notes RBT.bulkload.transfer[transfer_rule del]
    3.86 +begin
    3.87 +  lemma tabulate_Mapping [code]:
    3.88 +    "Mapping.tabulate ks f = Mapping (bulkload (List.map (\<lambda>k. (k, f k)) ks))"
    3.89 +  by transfer (simp add: map_of_map_restrict)
    3.90 +  
    3.91 +  lemma bulkload_Mapping [code]:
    3.92 +    "Mapping.bulkload vs = Mapping (bulkload (List.map (\<lambda>n. (n, vs ! n)) [0..<length vs]))"
    3.93 +  by transfer (simp add: map_of_map_restrict fun_eq_iff)
    3.94 +end
    3.95  
    3.96  lemma equal_Mapping [code]:
    3.97    "HOL.equal (Mapping t1) (Mapping t2) \<longleftrightarrow> entries t1 = entries t2"
    3.98 -  by (simp add: equal Mapping_def entries_lookup)
    3.99 +by (transfer fixing: t1 t2) (simp add: entries_lookup)
   3.100  
   3.101  lemma [code nbe]:
   3.102    "HOL.equal (x :: (_, _) mapping) x \<longleftrightarrow> True"
     4.1 --- a/src/HOL/ex/Execute_Choice.thy	Thu Oct 18 15:52:32 2012 +0200
     4.2 +++ b/src/HOL/ex/Execute_Choice.thy	Thu Oct 18 15:52:33 2012 +0200
     4.3 @@ -26,7 +26,7 @@
     4.4    case True then show ?thesis by (simp add: is_empty_def keys_def valuesum_def)
     4.5  next
     4.6    case False
     4.7 -  then have l: "\<exists>l. l \<in> dom (Mapping.lookup m)" by (auto simp add: is_empty_def keys_def)
     4.8 +  then have l: "\<exists>l. l \<in> dom (Mapping.lookup m)" unfolding is_empty_def by transfer auto
     4.9    then have "(let l = SOME l. l \<in> dom (Mapping.lookup m) in
    4.10       the (Mapping.lookup m l) + (\<Sum>k \<in> dom (Mapping.lookup m) - {l}. the (Mapping.lookup m k))) =
    4.11         (\<Sum>k \<in> dom (Mapping.lookup m). the (Mapping.lookup m k))"
    4.12 @@ -41,7 +41,7 @@
    4.13          (\<Sum>k \<in> dom (Mapping.lookup m). the (Mapping.lookup m k))"
    4.14        by simp
    4.15     qed
    4.16 -  then show ?thesis by (simp add: keys_def valuesum_def is_empty_def)
    4.17 +  then show ?thesis unfolding is_empty_def valuesum_def by transfer simp
    4.18  qed
    4.19  
    4.20  text {*
    4.21 @@ -54,7 +54,7 @@
    4.22    "finite (Mapping.keys M) \<Longrightarrow> x \<in> Mapping.keys M \<Longrightarrow> y \<in> Mapping.keys M \<Longrightarrow>
    4.23      the (Mapping.lookup M x) + valuesum (Mapping.delete x M) =
    4.24      the (Mapping.lookup M y) + valuesum (Mapping.delete y M)"
    4.25 -  by (simp add: valuesum_def keys_def setsum_diff)
    4.26 +  unfolding valuesum_def  by transfer (simp add: setsum_diff)
    4.27  
    4.28  text {*
    4.29    Given @{text valuesum_rec} as initial description, we stepwise refine it to something executable;