src/HOL/Library/Mapping.thy
author wenzelm
Wed Jun 22 10:09:20 2016 +0200 (2016-06-22)
changeset 63343 fb5d8a50c641
parent 63239 d562c9948dee
child 63462 c1fe30f2bc32
permissions -rw-r--r--
bundle lifting_syntax;
     1 (*  Title:      HOL/Library/Mapping.thy
     2     Author:     Florian Haftmann and Ondrej Kuncar
     3 *)
     4 
     5 section \<open>An abstract view on maps for code generation.\<close>
     6 
     7 theory Mapping
     8 imports Main
     9 begin
    10 
    11 subsection \<open>Parametricity transfer rules\<close>
    12 
    13 lemma map_of_foldr: \<comment> \<open>FIXME move\<close>
    14   "map_of xs = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) xs Map.empty"
    15   using map_add_map_of_foldr [of Map.empty] by auto
    16 
    17 context includes lifting_syntax
    18 begin
    19 
    20 lemma empty_parametric:
    21   "(A ===> rel_option B) Map.empty Map.empty"
    22   by transfer_prover
    23 
    24 lemma lookup_parametric: "((A ===> B) ===> A ===> B) (\<lambda>m k. m k) (\<lambda>m k. m k)"
    25   by transfer_prover
    26 
    27 lemma update_parametric:
    28   assumes [transfer_rule]: "bi_unique A"
    29   shows "(A ===> B ===> (A ===> rel_option B) ===> A ===> rel_option B)
    30     (\<lambda>k v m. m(k \<mapsto> v)) (\<lambda>k v m. m(k \<mapsto> v))"
    31   by transfer_prover
    32 
    33 lemma delete_parametric:
    34   assumes [transfer_rule]: "bi_unique A"
    35   shows "(A ===> (A ===> rel_option B) ===> A ===> rel_option B) 
    36     (\<lambda>k m. m(k := None)) (\<lambda>k m. m(k := None))"
    37   by transfer_prover
    38 
    39 lemma is_none_parametric [transfer_rule]:
    40   "(rel_option A ===> HOL.eq) Option.is_none Option.is_none"
    41   by (auto simp add: Option.is_none_def rel_fun_def rel_option_iff split: option.split)
    42 
    43 lemma dom_parametric:
    44   assumes [transfer_rule]: "bi_total A"
    45   shows "((A ===> rel_option B) ===> rel_set A) dom dom" 
    46   unfolding dom_def [abs_def] Option.is_none_def [symmetric] by transfer_prover
    47 
    48 lemma map_of_parametric [transfer_rule]:
    49   assumes [transfer_rule]: "bi_unique R1"
    50   shows "(list_all2 (rel_prod R1 R2) ===> R1 ===> rel_option R2) map_of map_of"
    51   unfolding map_of_def by transfer_prover
    52 
    53 lemma map_entry_parametric [transfer_rule]:
    54   assumes [transfer_rule]: "bi_unique A"
    55   shows "(A ===> (B ===> B) ===> (A ===> rel_option B) ===> A ===> rel_option B) 
    56     (\<lambda>k f m. (case m k of None \<Rightarrow> m
    57       | Some v \<Rightarrow> m (k \<mapsto> (f v)))) (\<lambda>k f m. (case m k of None \<Rightarrow> m
    58       | Some v \<Rightarrow> m (k \<mapsto> (f v))))"
    59   by transfer_prover
    60 
    61 lemma tabulate_parametric: 
    62   assumes [transfer_rule]: "bi_unique A"
    63   shows "(list_all2 A ===> (A ===> B) ===> A ===> rel_option B) 
    64     (\<lambda>ks f. (map_of (map (\<lambda>k. (k, f k)) ks))) (\<lambda>ks f. (map_of (map (\<lambda>k. (k, f k)) ks)))"
    65   by transfer_prover
    66 
    67 lemma bulkload_parametric: 
    68   "(list_all2 A ===> HOL.eq ===> rel_option A) 
    69     (\<lambda>xs k. if k < length xs then Some (xs ! k) else None) (\<lambda>xs k. if k < length xs then Some (xs ! k) else None)"
    70 proof
    71   fix xs ys
    72   assume "list_all2 A xs ys"
    73   then show "(HOL.eq ===> rel_option A)
    74     (\<lambda>k. if k < length xs then Some (xs ! k) else None)
    75     (\<lambda>k. if k < length ys then Some (ys ! k) else None)"
    76     apply induct
    77     apply auto
    78     unfolding rel_fun_def
    79     apply clarsimp 
    80     apply (case_tac xa) 
    81     apply (auto dest: list_all2_lengthD list_all2_nthD)
    82     done
    83 qed
    84 
    85 lemma map_parametric: 
    86   "((A ===> B) ===> (C ===> D) ===> (B ===> rel_option C) ===> A ===> rel_option D) 
    87      (\<lambda>f g m. (map_option g \<circ> m \<circ> f)) (\<lambda>f g m. (map_option g \<circ> m \<circ> f))"
    88   by transfer_prover
    89   
    90 lemma combine_with_key_parametric: 
    91   shows "((A ===> B ===> B ===> B) ===> (A ===> rel_option B) ===> (A ===> rel_option B) ===>
    92            (A ===> rel_option B)) (\<lambda>f m1 m2 x. combine_options (f x) (m1 x) (m2 x))
    93            (\<lambda>f m1 m2 x. combine_options (f x) (m1 x) (m2 x))"
    94   unfolding combine_options_def by transfer_prover
    95   
    96 lemma combine_parametric: 
    97   shows "((B ===> B ===> B) ===> (A ===> rel_option B) ===> (A ===> rel_option B) ===>
    98            (A ===> rel_option B)) (\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x))
    99            (\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x))"
   100   unfolding combine_options_def by transfer_prover
   101 
   102 end
   103 
   104 
   105 subsection \<open>Type definition and primitive operations\<close>
   106 
   107 typedef ('a, 'b) mapping = "UNIV :: ('a \<rightharpoonup> 'b) set"
   108   morphisms rep Mapping
   109   ..
   110 
   111 setup_lifting type_definition_mapping
   112 
   113 lift_definition empty :: "('a, 'b) mapping"
   114   is Map.empty parametric empty_parametric .
   115 
   116 lift_definition lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<Rightarrow> 'b option"
   117   is "\<lambda>m k. m k" parametric lookup_parametric .
   118 
   119 definition "lookup_default d m k = (case Mapping.lookup m k of None \<Rightarrow> d | Some v \<Rightarrow> v)"
   120 
   121 declare [[code drop: Mapping.lookup]]
   122 setup \<open>Code.add_eqn (Code.Equation, true) @{thm Mapping.lookup.abs_eq}\<close> \<comment> \<open>FIXME lifting\<close>
   123 
   124 lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
   125   is "\<lambda>k v m. m(k \<mapsto> v)" parametric update_parametric .
   126 
   127 lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
   128   is "\<lambda>k m. m(k := None)" parametric delete_parametric .
   129 
   130 lift_definition filter :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
   131   is "\<lambda>P m k. case m k of None \<Rightarrow> None | Some v \<Rightarrow> if P k v then Some v else None" . 
   132 
   133 lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set"
   134   is dom parametric dom_parametric .
   135 
   136 lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping"
   137   is "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" parametric tabulate_parametric .
   138 
   139 lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping"
   140   is "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" parametric bulkload_parametric .
   141 
   142 lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping"
   143   is "\<lambda>f g m. (map_option g \<circ> m \<circ> f)" parametric map_parametric .
   144   
   145 lift_definition map_values :: "('c \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> ('c, 'a) mapping \<Rightarrow> ('c, 'b) mapping"
   146   is "\<lambda>f m x. map_option (f x) (m x)" . 
   147 
   148 lift_definition combine_with_key :: 
   149   "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping"
   150   is "\<lambda>f m1 m2 x. combine_options (f x) (m1 x) (m2 x)" parametric combine_with_key_parametric .
   151 
   152 lift_definition combine :: 
   153   "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping"
   154   is "\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x)" parametric combine_parametric .
   155 
   156 definition All_mapping where
   157   "All_mapping m P \<longleftrightarrow> (\<forall>x. case Mapping.lookup m x of None \<Rightarrow> True | Some y \<Rightarrow> P x y)"
   158 
   159 declare [[code drop: map]]
   160 
   161 
   162 subsection \<open>Functorial structure\<close>
   163 
   164 functor map: map
   165   by (transfer, auto simp add: fun_eq_iff option.map_comp option.map_id)+
   166 
   167 
   168 subsection \<open>Derived operations\<close>
   169 
   170 definition ordered_keys :: "('a::linorder, 'b) mapping \<Rightarrow> 'a list"
   171 where
   172   "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
   173 
   174 definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool"
   175 where
   176   "is_empty m \<longleftrightarrow> keys m = {}"
   177 
   178 definition size :: "('a, 'b) mapping \<Rightarrow> nat"
   179 where
   180   "size m = (if finite (keys m) then card (keys m) else 0)"
   181 
   182 definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
   183 where
   184   "replace k v m = (if k \<in> keys m then update k v m else m)"
   185 
   186 definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
   187 where
   188   "default k v m = (if k \<in> keys m then m else update k v m)"
   189 
   190 text \<open>Manual derivation of transfer rule is non-trivial\<close>
   191 
   192 lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
   193   "\<lambda>k f m. (case m k of None \<Rightarrow> m
   194     | Some v \<Rightarrow> m (k \<mapsto> (f v)))" parametric map_entry_parametric .
   195 
   196 lemma map_entry_code [code]:
   197   "map_entry k f m = (case lookup m k of None \<Rightarrow> m
   198     | Some v \<Rightarrow> update k (f v) m)"
   199   by transfer rule
   200 
   201 definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
   202 where
   203   "map_default k v f m = map_entry k f (default k v m)" 
   204 
   205 definition of_alist :: "('k \<times> 'v) list \<Rightarrow> ('k, 'v) mapping"
   206 where
   207   "of_alist xs = foldr (\<lambda>(k, v) m. update k v m) xs empty"
   208 
   209 instantiation mapping :: (type, type) equal
   210 begin
   211 
   212 definition
   213   "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. lookup m1 k = lookup m2 k)"
   214 
   215 instance
   216   by standard (unfold equal_mapping_def, transfer, auto)
   217 
   218 end
   219 
   220 context includes lifting_syntax
   221 begin
   222 
   223 lemma [transfer_rule]:
   224   assumes [transfer_rule]: "bi_total A"
   225   assumes [transfer_rule]: "bi_unique B"
   226   shows "(pcr_mapping A B ===> pcr_mapping A B ===> op=) HOL.eq HOL.equal"
   227   by (unfold equal) transfer_prover
   228 
   229 lemma of_alist_transfer [transfer_rule]:
   230   assumes [transfer_rule]: "bi_unique R1"
   231   shows "(list_all2 (rel_prod R1 R2) ===> pcr_mapping R1 R2) map_of of_alist"
   232   unfolding of_alist_def [abs_def] map_of_foldr [abs_def] by transfer_prover
   233 
   234 end
   235 
   236 
   237 subsection \<open>Properties\<close>
   238 
   239 lemma mapping_eqI:
   240   "(\<And>x. lookup m x = lookup m' x) \<Longrightarrow> m = m'"
   241   by transfer (simp add: fun_eq_iff)
   242 
   243 lemma mapping_eqI': 
   244   assumes "\<And>x. x \<in> Mapping.keys m \<Longrightarrow> Mapping.lookup_default d m x = Mapping.lookup_default d m' x" 
   245       and "Mapping.keys m = Mapping.keys m'"
   246   shows   "m = m'"
   247 proof (intro mapping_eqI)
   248   fix x
   249   show "Mapping.lookup m x = Mapping.lookup m' x"
   250   proof (cases "Mapping.lookup m x")
   251     case None
   252     hence "x \<notin> Mapping.keys m" by transfer (simp add: dom_def)
   253     hence "x \<notin> Mapping.keys m'" by (simp add: assms)
   254     hence "Mapping.lookup m' x = None" by transfer (simp add: dom_def)
   255     with None show ?thesis by simp
   256   next
   257     case (Some y)
   258     hence A: "x \<in> Mapping.keys m" by transfer (simp add: dom_def)
   259     hence "x \<in> Mapping.keys m'" by (simp add: assms)
   260     hence "\<exists>y'. Mapping.lookup m' x = Some y'" by transfer (simp add: dom_def)
   261     with Some assms(1)[OF A] show ?thesis by (auto simp add: lookup_default_def)
   262   qed
   263 qed
   264 
   265 lemma lookup_update:
   266   "lookup (update k v m) k = Some v" 
   267   by transfer simp
   268 
   269 lemma lookup_update_neq:
   270   "k \<noteq> k' \<Longrightarrow> lookup (update k v m) k' = lookup m k'" 
   271   by transfer simp
   272 
   273 lemma lookup_update': 
   274   "Mapping.lookup (update k v m) k' = (if k = k' then Some v else lookup m k')"
   275   by (auto simp: lookup_update lookup_update_neq)
   276 
   277 lemma lookup_empty:
   278   "lookup empty k = None" 
   279   by transfer simp
   280 
   281 lemma lookup_filter:
   282   "lookup (filter P m) k = 
   283      (case lookup m k of None \<Rightarrow> None | Some v \<Rightarrow> if P k v then Some v else None)"
   284   by transfer simp_all
   285 
   286 lemma lookup_map_values:
   287   "lookup (map_values f m) k = map_option (f k) (lookup m k)"
   288   by transfer simp_all
   289 
   290 lemma lookup_default_empty: "lookup_default d empty k = d"
   291   by (simp add: lookup_default_def lookup_empty)
   292 
   293 lemma lookup_default_update:
   294   "lookup_default d (update k v m) k = v" 
   295   by (simp add: lookup_default_def lookup_update)
   296 
   297 lemma lookup_default_update_neq:
   298   "k \<noteq> k' \<Longrightarrow> lookup_default d (update k v m) k' = lookup_default d m k'" 
   299   by (simp add: lookup_default_def lookup_update_neq)
   300 
   301 lemma lookup_default_update': 
   302   "lookup_default d (update k v m) k' = (if k = k' then v else lookup_default d m k')"
   303   by (auto simp: lookup_default_update lookup_default_update_neq)
   304 
   305 lemma lookup_default_filter:
   306   "lookup_default d (filter P m) k =  
   307      (if P k (lookup_default d m k) then lookup_default d m k else d)"
   308   by (simp add: lookup_default_def lookup_filter split: option.splits)
   309 
   310 lemma lookup_default_map_values:
   311   "lookup_default (f k d) (map_values f m) k = f k (lookup_default d m k)"
   312   by (simp add: lookup_default_def lookup_map_values split: option.splits)  
   313 
   314 lemma lookup_combine_with_key:
   315   "Mapping.lookup (combine_with_key f m1 m2) x = 
   316      combine_options (f x) (Mapping.lookup m1 x) (Mapping.lookup m2 x)"
   317   by transfer (auto split: option.splits)
   318   
   319 lemma combine_altdef: "combine f m1 m2 = combine_with_key (\<lambda>_. f) m1 m2"
   320   by transfer' (rule refl)
   321 
   322 lemma lookup_combine:
   323   "Mapping.lookup (combine f m1 m2) x = 
   324      combine_options f (Mapping.lookup m1 x) (Mapping.lookup m2 x)"
   325   by transfer (auto split: option.splits)
   326   
   327 lemma lookup_default_neutral_combine_with_key: 
   328   assumes "\<And>x. f k d x = x" "\<And>x. f k x d = x"
   329   shows   "Mapping.lookup_default d (combine_with_key f m1 m2) k = 
   330              f k (Mapping.lookup_default d m1 k) (Mapping.lookup_default d m2 k)"
   331   by (auto simp: lookup_default_def lookup_combine_with_key assms split: option.splits)
   332   
   333 lemma lookup_default_neutral_combine: 
   334   assumes "\<And>x. f d x = x" "\<And>x. f x d = x"
   335   shows   "Mapping.lookup_default d (combine f m1 m2) x = 
   336              f (Mapping.lookup_default d m1 x) (Mapping.lookup_default d m2 x)"
   337   by (auto simp: lookup_default_def lookup_combine assms split: option.splits)
   338 
   339 lemma lookup_map_entry:
   340   "lookup (map_entry x f m) x = map_option f (lookup m x)"
   341   by transfer (auto split: option.splits)
   342 
   343 lemma lookup_map_entry_neq:
   344   "x \<noteq> y \<Longrightarrow> lookup (map_entry x f m) y = lookup m y"
   345   by transfer (auto split: option.splits)
   346 
   347 lemma lookup_map_entry':
   348   "lookup (map_entry x f m) y = 
   349      (if x = y then map_option f (lookup m y) else lookup m y)"
   350   by transfer (auto split: option.splits)
   351   
   352 lemma lookup_default:
   353   "lookup (default x d m) x = Some (lookup_default d m x)"
   354     unfolding lookup_default_def default_def
   355     by transfer (auto split: option.splits)
   356 
   357 lemma lookup_default_neq:
   358   "x \<noteq> y \<Longrightarrow> lookup (default x d m) y = lookup m y"
   359     unfolding lookup_default_def default_def
   360     by transfer (auto split: option.splits)
   361 
   362 lemma lookup_default':
   363   "lookup (default x d m) y = 
   364      (if x = y then Some (lookup_default d m x) else lookup m y)"
   365   unfolding lookup_default_def default_def
   366   by transfer (auto split: option.splits)
   367   
   368 lemma lookup_map_default:
   369   "lookup (map_default x d f m) x = Some (f (lookup_default d m x))"
   370     unfolding lookup_default_def default_def
   371     by (simp add: map_default_def lookup_map_entry lookup_default lookup_default_def)
   372 
   373 lemma lookup_map_default_neq:
   374   "x \<noteq> y \<Longrightarrow> lookup (map_default x d f m) y = lookup m y"
   375     unfolding lookup_default_def default_def
   376     by (simp add: map_default_def lookup_map_entry_neq lookup_default_neq) 
   377 
   378 lemma lookup_map_default':
   379   "lookup (map_default x d f m) y = 
   380      (if x = y then Some (f (lookup_default d m x)) else lookup m y)"
   381     unfolding lookup_default_def default_def
   382     by (simp add: map_default_def lookup_map_entry' lookup_default' lookup_default_def)  
   383 
   384 lemma lookup_tabulate: 
   385   assumes "distinct xs"
   386   shows   "Mapping.lookup (Mapping.tabulate xs f) x = (if x \<in> set xs then Some (f x) else None)"
   387   using assms by transfer (auto simp: map_of_eq_None_iff o_def dest!: map_of_SomeD)
   388 
   389 lemma lookup_of_alist: "Mapping.lookup (Mapping.of_alist xs) k = map_of xs k"
   390   by transfer simp_all
   391 
   392 lemma keys_is_none_rep [code_unfold]:
   393   "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
   394   by transfer (auto simp add: Option.is_none_def)
   395 
   396 lemma update_update:
   397   "update k v (update k w m) = update k v m"
   398   "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
   399   by (transfer, simp add: fun_upd_twist)+
   400 
   401 lemma update_delete [simp]:
   402   "update k v (delete k m) = update k v m"
   403   by transfer simp
   404 
   405 lemma delete_update:
   406   "delete k (update k v m) = delete k m"
   407   "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
   408   by (transfer, simp add: fun_upd_twist)+
   409 
   410 lemma delete_empty [simp]:
   411   "delete k empty = empty"
   412   by transfer simp
   413 
   414 lemma replace_update:
   415   "k \<notin> keys m \<Longrightarrow> replace k v m = m"
   416   "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
   417   by (transfer, auto simp add: replace_def fun_upd_twist)+
   418   
   419 lemma map_values_update: "map_values f (update k v m) = update k (f k v) (map_values f m)"
   420   by transfer (simp_all add: fun_eq_iff)
   421   
   422 lemma size_mono:
   423   "finite (keys m') \<Longrightarrow> keys m \<subseteq> keys m' \<Longrightarrow> size m \<le> size m'"
   424   unfolding size_def by (auto intro: card_mono)
   425 
   426 lemma size_empty [simp]:
   427   "size empty = 0"
   428   unfolding size_def by transfer simp
   429 
   430 lemma size_update:
   431   "finite (keys m) \<Longrightarrow> size (update k v m) =
   432     (if k \<in> keys m then size m else Suc (size m))"
   433   unfolding size_def by transfer (auto simp add: insert_dom)
   434 
   435 lemma size_delete:
   436   "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
   437   unfolding size_def by transfer simp
   438 
   439 lemma size_tabulate [simp]:
   440   "size (tabulate ks f) = length (remdups ks)"
   441   unfolding size_def by transfer (auto simp add: map_of_map_restrict  card_set comp_def)
   442 
   443 lemma keys_filter: "keys (filter P m) \<subseteq> keys m"
   444   by transfer (auto split: option.splits)
   445 
   446 lemma size_filter: "finite (keys m) \<Longrightarrow> size (filter P m) \<le> size m"
   447   by (intro size_mono keys_filter)
   448 
   449 
   450 lemma bulkload_tabulate:
   451   "bulkload xs = tabulate [0..<length xs] (nth xs)"
   452   by transfer (auto simp add: map_of_map_restrict)
   453 
   454 lemma is_empty_empty [simp]:
   455   "is_empty empty"
   456   unfolding is_empty_def by transfer simp 
   457 
   458 lemma is_empty_update [simp]:
   459   "\<not> is_empty (update k v m)"
   460   unfolding is_empty_def by transfer simp
   461 
   462 lemma is_empty_delete:
   463   "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
   464   unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
   465 
   466 lemma is_empty_replace [simp]:
   467   "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
   468   unfolding is_empty_def replace_def by transfer auto
   469 
   470 lemma is_empty_default [simp]:
   471   "\<not> is_empty (default k v m)"
   472   unfolding is_empty_def default_def by transfer auto
   473 
   474 lemma is_empty_map_entry [simp]:
   475   "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
   476   unfolding is_empty_def by transfer (auto split: option.split)
   477 
   478 lemma is_empty_map_values [simp]:
   479   "is_empty (map_values f m) \<longleftrightarrow> is_empty m"
   480   unfolding is_empty_def by transfer (auto simp: fun_eq_iff)
   481 
   482 lemma is_empty_map_default [simp]:
   483   "\<not> is_empty (map_default k v f m)"
   484   by (simp add: map_default_def)
   485 
   486 lemma keys_dom_lookup:
   487   "keys m = dom (Mapping.lookup m)"
   488   by transfer rule
   489 
   490 lemma keys_empty [simp]:
   491   "keys empty = {}"
   492   by transfer simp
   493 
   494 lemma keys_update [simp]:
   495   "keys (update k v m) = insert k (keys m)"
   496   by transfer simp
   497 
   498 lemma keys_delete [simp]:
   499   "keys (delete k m) = keys m - {k}"
   500   by transfer simp
   501 
   502 lemma keys_replace [simp]:
   503   "keys (replace k v m) = keys m"
   504   unfolding replace_def by transfer (simp add: insert_absorb)
   505 
   506 lemma keys_default [simp]:
   507   "keys (default k v m) = insert k (keys m)"
   508   unfolding default_def by transfer (simp add: insert_absorb)
   509 
   510 lemma keys_map_entry [simp]:
   511   "keys (map_entry k f m) = keys m"
   512   by transfer (auto split: option.split)
   513 
   514 lemma keys_map_default [simp]:
   515   "keys (map_default k v f m) = insert k (keys m)"
   516   by (simp add: map_default_def)
   517 
   518 lemma keys_map_values [simp]:
   519   "keys (map_values f m) = keys m"
   520   by transfer (simp_all add: dom_def)
   521 
   522 lemma keys_combine_with_key [simp]: 
   523   "Mapping.keys (combine_with_key f m1 m2) = Mapping.keys m1 \<union> Mapping.keys m2"
   524   by transfer (auto simp: dom_def combine_options_def split: option.splits)  
   525 
   526 lemma keys_combine [simp]: "Mapping.keys (combine f m1 m2) = Mapping.keys m1 \<union> Mapping.keys m2"
   527   by (simp add: combine_altdef)
   528 
   529 lemma keys_tabulate [simp]:
   530   "keys (tabulate ks f) = set ks"
   531   by transfer (simp add: map_of_map_restrict o_def)
   532 
   533 lemma keys_of_alist [simp]: "keys (of_alist xs) = set (List.map fst xs)"
   534   by transfer (simp_all add: dom_map_of_conv_image_fst)
   535 
   536 lemma keys_bulkload [simp]:
   537   "keys (bulkload xs) = {0..<length xs}"
   538   by (simp add: bulkload_tabulate)
   539 
   540 lemma distinct_ordered_keys [simp]:
   541   "distinct (ordered_keys m)"
   542   by (simp add: ordered_keys_def)
   543 
   544 lemma ordered_keys_infinite [simp]:
   545   "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
   546   by (simp add: ordered_keys_def)
   547 
   548 lemma ordered_keys_empty [simp]:
   549   "ordered_keys empty = []"
   550   by (simp add: ordered_keys_def)
   551 
   552 lemma ordered_keys_update [simp]:
   553   "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
   554   "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
   555   by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
   556 
   557 lemma ordered_keys_delete [simp]:
   558   "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
   559 proof (cases "finite (keys m)")
   560   case False then show ?thesis by simp
   561 next
   562   case True note fin = True
   563   show ?thesis
   564   proof (cases "k \<in> keys m")
   565     case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
   566     with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
   567   next
   568     case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
   569   qed
   570 qed
   571 
   572 lemma ordered_keys_replace [simp]:
   573   "ordered_keys (replace k v m) = ordered_keys m"
   574   by (simp add: replace_def)
   575 
   576 lemma ordered_keys_default [simp]:
   577   "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
   578   "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
   579   by (simp_all add: default_def)
   580 
   581 lemma ordered_keys_map_entry [simp]:
   582   "ordered_keys (map_entry k f m) = ordered_keys m"
   583   by (simp add: ordered_keys_def)
   584 
   585 lemma ordered_keys_map_default [simp]:
   586   "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
   587   "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
   588   by (simp_all add: map_default_def)
   589 
   590 lemma ordered_keys_tabulate [simp]:
   591   "ordered_keys (tabulate ks f) = sort (remdups ks)"
   592   by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
   593 
   594 lemma ordered_keys_bulkload [simp]:
   595   "ordered_keys (bulkload ks) = [0..<length ks]"
   596   by (simp add: ordered_keys_def)
   597 
   598 lemma tabulate_fold:
   599   "tabulate xs f = fold (\<lambda>k m. update k (f k) m) xs empty"
   600 proof transfer
   601   fix f :: "'a \<Rightarrow> 'b" and xs
   602   have "map_of (List.map (\<lambda>k. (k, f k)) xs) = foldr (\<lambda>k m. m(k \<mapsto> f k)) xs Map.empty"
   603     by (simp add: foldr_map comp_def map_of_foldr)
   604   also have "foldr (\<lambda>k m. m(k \<mapsto> f k)) xs = fold (\<lambda>k m. m(k \<mapsto> f k)) xs"
   605     by (rule foldr_fold) (simp add: fun_eq_iff)
   606   ultimately show "map_of (List.map (\<lambda>k. (k, f k)) xs) = fold (\<lambda>k m. m(k \<mapsto> f k)) xs Map.empty"
   607     by simp
   608 qed
   609 
   610 lemma All_mapping_mono:
   611   "(\<And>k v. k \<in> keys m \<Longrightarrow> P k v \<Longrightarrow> Q k v) \<Longrightarrow> All_mapping m P \<Longrightarrow> All_mapping m Q"
   612   unfolding All_mapping_def by transfer (auto simp: All_mapping_def dom_def split: option.splits)
   613 
   614 lemma All_mapping_empty [simp]: "All_mapping Mapping.empty P"
   615   by (auto simp: All_mapping_def lookup_empty)
   616   
   617 lemma All_mapping_update_iff: 
   618   "All_mapping (Mapping.update k v m) P \<longleftrightarrow> P k v \<and> All_mapping m (\<lambda>k' v'. k = k' \<or> P k' v')"
   619   unfolding All_mapping_def 
   620 proof safe
   621   assume "\<forall>x. case Mapping.lookup (Mapping.update k v m) x of None \<Rightarrow> True | Some y \<Rightarrow> P x y"
   622   hence A: "case Mapping.lookup (Mapping.update k v m) x of None \<Rightarrow> True | Some y \<Rightarrow> P x y" for x
   623     by blast
   624   from A[of k] show "P k v" by (simp add: lookup_update)
   625   show "case Mapping.lookup m x of None \<Rightarrow> True | Some v' \<Rightarrow> k = x \<or> P x v'" for x
   626     using A[of x] by (auto simp add: lookup_update' split: if_splits option.splits)
   627 next
   628   assume "P k v"
   629   assume "\<forall>x. case Mapping.lookup m x of None \<Rightarrow> True | Some v' \<Rightarrow> k = x \<or> P x v'"
   630   hence A: "case Mapping.lookup m x of None \<Rightarrow> True | Some v' \<Rightarrow> k = x \<or> P x v'" for x by blast
   631   show "case Mapping.lookup (Mapping.update k v m) x of None \<Rightarrow> True | Some xa \<Rightarrow> P x xa" for x
   632     using \<open>P k v\<close> A[of x] by (auto simp: lookup_update' split: option.splits)
   633 qed
   634 
   635 lemma All_mapping_update:
   636   "P k v \<Longrightarrow> All_mapping m (\<lambda>k' v'. k = k' \<or> P k' v') \<Longrightarrow> All_mapping (Mapping.update k v m) P"
   637   by (simp add: All_mapping_update_iff)
   638 
   639 lemma All_mapping_filter_iff:
   640   "All_mapping (filter P m) Q \<longleftrightarrow> All_mapping m (\<lambda>k v. P k v \<longrightarrow> Q k v)"
   641   by (auto simp: All_mapping_def lookup_filter split: option.splits)
   642 
   643 lemma All_mapping_filter:
   644   "All_mapping m Q \<Longrightarrow> All_mapping (filter P m) Q"
   645   by (auto simp: All_mapping_filter_iff intro: All_mapping_mono)
   646 
   647 lemma All_mapping_map_values:
   648   "All_mapping (map_values f m) P \<longleftrightarrow> All_mapping m (\<lambda>k v. P k (f k v))"
   649   by (auto simp: All_mapping_def lookup_map_values split: option.splits)
   650 
   651 lemma All_mapping_tabulate: 
   652   "(\<forall>x\<in>set xs. P x (f x)) \<Longrightarrow> All_mapping (Mapping.tabulate xs f) P"
   653   unfolding All_mapping_def 
   654   by (intro allI,  transfer) (auto split: option.split dest!: map_of_SomeD)
   655 
   656 lemma All_mapping_alist:
   657   "(\<And>k v. (k, v) \<in> set xs \<Longrightarrow> P k v) \<Longrightarrow> All_mapping (Mapping.of_alist xs) P"
   658   by (auto simp: All_mapping_def lookup_of_alist dest!: map_of_SomeD split: option.splits)
   659 
   660 
   661 lemma combine_empty [simp]:
   662   "combine f Mapping.empty y = y" "combine f y Mapping.empty = y"
   663   by (transfer, force)+
   664 
   665 lemma (in abel_semigroup) comm_monoid_set_combine: "comm_monoid_set (combine f) Mapping.empty"
   666   by standard (transfer fixing: f, simp add: combine_options_ac[of f] ac_simps)+
   667 
   668 locale combine_mapping_abel_semigroup = abel_semigroup
   669 begin
   670 
   671 sublocale combine: comm_monoid_set "combine f" Mapping.empty
   672   by (rule comm_monoid_set_combine)
   673 
   674 lemma fold_combine_code:
   675   "combine.F g (set xs) = foldr (\<lambda>x. combine f (g x)) (remdups xs) Mapping.empty"
   676 proof -
   677   have "combine.F g (set xs) = foldr (\<lambda>x. combine f (g x)) xs Mapping.empty"
   678     if "distinct xs" for xs
   679     using that by (induction xs) simp_all
   680   from this[of "remdups xs"] show ?thesis by simp
   681 qed
   682   
   683 lemma keys_fold_combine:
   684   assumes "finite A"
   685   shows   "Mapping.keys (combine.F g A) = (\<Union>x\<in>A. Mapping.keys (g x))"
   686   using assms by (induction A rule: finite_induct) simp_all
   687 
   688 end
   689 
   690   
   691 subsection \<open>Code generator setup\<close>
   692 
   693 hide_const (open) empty is_empty rep lookup lookup_default filter update delete ordered_keys
   694   keys size replace default map_entry map_default tabulate bulkload map map_values combine of_alist
   695 
   696 end
   697