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