src/HOL/Library/Mapping.thy
author wenzelm
Tue Sep 01 22:32:58 2015 +0200 (2015-09-01)
changeset 61076 bdc1e2f0a86a
parent 61068 6cb92c2a5ece
child 61585 a9599d3d7610
permissions -rw-r--r--
eliminated \<Colon>;
kuncar@49929
     1
(*  Title:      HOL/Library/Mapping.thy
kuncar@49929
     2
    Author:     Florian Haftmann and Ondrej Kuncar
kuncar@49929
     3
*)
haftmann@29708
     4
wenzelm@60500
     5
section \<open>An abstract view on maps for code generation.\<close>
haftmann@29708
     6
haftmann@29708
     7
theory Mapping
kuncar@53013
     8
imports Main
haftmann@29708
     9
begin
haftmann@29708
    10
wenzelm@60500
    11
subsection \<open>Parametricity transfer rules\<close>
kuncar@51379
    12
wenzelm@60500
    13
lemma map_of_foldr: -- \<open>FIXME move\<close>
haftmann@56529
    14
  "map_of xs = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) xs Map.empty"
haftmann@56529
    15
  using map_add_map_of_foldr [of Map.empty] by auto
haftmann@56529
    16
kuncar@53013
    17
context
kuncar@53013
    18
begin
haftmann@56528
    19
kuncar@53013
    20
interpretation lifting_syntax .
kuncar@53013
    21
haftmann@56529
    22
lemma empty_parametric:
haftmann@56528
    23
  "(A ===> rel_option B) Map.empty Map.empty"
haftmann@56528
    24
  by transfer_prover
kuncar@51379
    25
haftmann@56529
    26
lemma lookup_parametric: "((A ===> B) ===> A ===> B) (\<lambda>m k. m k) (\<lambda>m k. m k)"
haftmann@56528
    27
  by transfer_prover
kuncar@51379
    28
haftmann@56529
    29
lemma update_parametric:
kuncar@51379
    30
  assumes [transfer_rule]: "bi_unique A"
haftmann@56528
    31
  shows "(A ===> B ===> (A ===> rel_option B) ===> A ===> rel_option B)
haftmann@56528
    32
    (\<lambda>k v m. m(k \<mapsto> v)) (\<lambda>k v m. m(k \<mapsto> v))"
haftmann@56528
    33
  by transfer_prover
kuncar@51379
    34
haftmann@56529
    35
lemma delete_parametric:
kuncar@51379
    36
  assumes [transfer_rule]: "bi_unique A"
blanchet@55525
    37
  shows "(A ===> (A ===> rel_option B) ===> A ===> rel_option B) 
haftmann@56528
    38
    (\<lambda>k m. m(k := None)) (\<lambda>k m. m(k := None))"
haftmann@56528
    39
  by transfer_prover
kuncar@51379
    40
haftmann@56528
    41
lemma is_none_parametric [transfer_rule]:
haftmann@56528
    42
  "(rel_option A ===> HOL.eq) Option.is_none Option.is_none"
wenzelm@61068
    43
  by (auto simp add: Option.is_none_def rel_fun_def rel_option_iff split: option.split)
kuncar@51379
    44
haftmann@56529
    45
lemma dom_parametric:
kuncar@51379
    46
  assumes [transfer_rule]: "bi_total A"
blanchet@55938
    47
  shows "((A ===> rel_option B) ===> rel_set A) dom dom" 
wenzelm@61068
    48
  unfolding dom_def [abs_def] Option.is_none_def [symmetric] by transfer_prover
kuncar@51379
    49
haftmann@56529
    50
lemma map_of_parametric [transfer_rule]:
kuncar@51379
    51
  assumes [transfer_rule]: "bi_unique R1"
blanchet@55944
    52
  shows "(list_all2 (rel_prod R1 R2) ===> R1 ===> rel_option R2) map_of map_of"
haftmann@56528
    53
  unfolding map_of_def by transfer_prover
kuncar@51379
    54
haftmann@56529
    55
lemma map_entry_parametric [transfer_rule]:
haftmann@56529
    56
  assumes [transfer_rule]: "bi_unique A"
haftmann@56529
    57
  shows "(A ===> (B ===> B) ===> (A ===> rel_option B) ===> A ===> rel_option B) 
haftmann@56529
    58
    (\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
    59
      | Some v \<Rightarrow> m (k \<mapsto> (f v)))) (\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
    60
      | Some v \<Rightarrow> m (k \<mapsto> (f v))))"
haftmann@56529
    61
  by transfer_prover
haftmann@56529
    62
haftmann@56529
    63
lemma tabulate_parametric: 
kuncar@51379
    64
  assumes [transfer_rule]: "bi_unique A"
blanchet@55525
    65
  shows "(list_all2 A ===> (A ===> B) ===> A ===> rel_option B) 
haftmann@56528
    66
    (\<lambda>ks f. (map_of (map (\<lambda>k. (k, f k)) ks))) (\<lambda>ks f. (map_of (map (\<lambda>k. (k, f k)) ks)))"
haftmann@56528
    67
  by transfer_prover
kuncar@51379
    68
haftmann@56529
    69
lemma bulkload_parametric: 
haftmann@56528
    70
  "(list_all2 A ===> HOL.eq ===> rel_option A) 
kuncar@51379
    71
    (\<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)"
haftmann@56528
    72
proof
haftmann@56528
    73
  fix xs ys
haftmann@56528
    74
  assume "list_all2 A xs ys"
haftmann@56528
    75
  then show "(HOL.eq ===> rel_option A)
haftmann@56528
    76
    (\<lambda>k. if k < length xs then Some (xs ! k) else None)
haftmann@56528
    77
    (\<lambda>k. if k < length ys then Some (ys ! k) else None)"
haftmann@56528
    78
    apply induct
haftmann@56528
    79
    apply auto
haftmann@56528
    80
    unfolding rel_fun_def
haftmann@56528
    81
    apply clarsimp 
haftmann@56528
    82
    apply (case_tac xa) 
haftmann@56528
    83
    apply (auto dest: list_all2_lengthD list_all2_nthD)
haftmann@56528
    84
    done
haftmann@56528
    85
qed
kuncar@51379
    86
haftmann@56529
    87
lemma map_parametric: 
blanchet@55525
    88
  "((A ===> B) ===> (C ===> D) ===> (B ===> rel_option C) ===> A ===> rel_option D) 
haftmann@56528
    89
     (\<lambda>f g m. (map_option g \<circ> m \<circ> f)) (\<lambda>f g m. (map_option g \<circ> m \<circ> f))"
haftmann@56528
    90
  by transfer_prover
kuncar@51379
    91
haftmann@56529
    92
end
kuncar@51379
    93
kuncar@53013
    94
wenzelm@60500
    95
subsection \<open>Type definition and primitive operations\<close>
haftmann@29708
    96
wenzelm@49834
    97
typedef ('a, 'b) mapping = "UNIV :: ('a \<rightharpoonup> 'b) set"
haftmann@56528
    98
  morphisms rep Mapping
haftmann@56528
    99
  ..
haftmann@37700
   100
haftmann@59485
   101
setup_lifting type_definition_mapping
haftmann@37700
   102
haftmann@56528
   103
lift_definition empty :: "('a, 'b) mapping"
haftmann@56529
   104
  is Map.empty parametric empty_parametric .
kuncar@49929
   105
haftmann@56528
   106
lift_definition lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<Rightarrow> 'b option"
haftmann@56529
   107
  is "\<lambda>m k. m k" parametric lookup_parametric .
haftmann@56528
   108
haftmann@59485
   109
declare [[code drop: Mapping.lookup]]
haftmann@59485
   110
setup \<open>Code.add_default_eqn @{thm Mapping.lookup.abs_eq}\<close> -- \<open>FIXME lifting\<close>
haftmann@59485
   111
haftmann@56528
   112
lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   113
  is "\<lambda>k v m. m(k \<mapsto> v)" parametric update_parametric .
haftmann@37700
   114
haftmann@56528
   115
lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   116
  is "\<lambda>k m. m(k := None)" parametric delete_parametric .
haftmann@39380
   117
haftmann@56528
   118
lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set"
haftmann@56529
   119
  is dom parametric dom_parametric .
haftmann@29708
   120
haftmann@56528
   121
lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   122
  is "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" parametric tabulate_parametric .
haftmann@29708
   123
haftmann@56528
   124
lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping"
haftmann@56529
   125
  is "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" parametric bulkload_parametric .
haftmann@29708
   126
haftmann@56528
   127
lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping"
haftmann@56529
   128
  is "\<lambda>f g m. (map_option g \<circ> m \<circ> f)" parametric map_parametric .
haftmann@29708
   129
haftmann@59485
   130
declare [[code drop: map]]
haftmann@59485
   131
haftmann@51161
   132
wenzelm@60500
   133
subsection \<open>Functorial structure\<close>
haftmann@40605
   134
blanchet@55467
   135
functor map: map
blanchet@55466
   136
  by (transfer, auto simp add: fun_eq_iff option.map_comp option.map_id)+
haftmann@40605
   137
haftmann@51161
   138
wenzelm@60500
   139
subsection \<open>Derived operations\<close>
haftmann@29708
   140
wenzelm@61076
   141
definition ordered_keys :: "('a::linorder, 'b) mapping \<Rightarrow> 'a list"
haftmann@56528
   142
where
haftmann@37052
   143
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
haftmann@35194
   144
haftmann@56528
   145
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool"
haftmann@56528
   146
where
haftmann@37052
   147
  "is_empty m \<longleftrightarrow> keys m = {}"
haftmann@35157
   148
haftmann@56528
   149
definition size :: "('a, 'b) mapping \<Rightarrow> nat"
haftmann@56528
   150
where
haftmann@37052
   151
  "size m = (if finite (keys m) then card (keys m) else 0)"
haftmann@35157
   152
haftmann@56528
   153
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   154
where
haftmann@37052
   155
  "replace k v m = (if k \<in> keys m then update k v m else m)"
haftmann@29814
   156
haftmann@56528
   157
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   158
where
haftmann@37052
   159
  "default k v m = (if k \<in> keys m then m else update k v m)"
haftmann@37026
   160
wenzelm@60500
   161
text \<open>Manual derivation of transfer rule is non-trivial\<close>
haftmann@56529
   162
kuncar@49929
   163
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
kuncar@49929
   164
  "\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
   165
    | Some v \<Rightarrow> m (k \<mapsto> (f v)))" parametric map_entry_parametric .
kuncar@49929
   166
haftmann@56529
   167
lemma map_entry_code [code]:
haftmann@56529
   168
  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
huffman@49975
   169
    | Some v \<Rightarrow> update k (f v) m)"
huffman@49975
   170
  by transfer rule
haftmann@37026
   171
haftmann@56528
   172
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   173
where
haftmann@37026
   174
  "map_default k v f m = map_entry k f (default k v m)" 
haftmann@37026
   175
haftmann@56529
   176
definition of_alist :: "('k \<times> 'v) list \<Rightarrow> ('k, 'v) mapping"
haftmann@56529
   177
where
haftmann@54853
   178
  "of_alist xs = foldr (\<lambda>(k, v) m. update k v m) xs empty"
kuncar@51379
   179
haftmann@51161
   180
instantiation mapping :: (type, type) equal
haftmann@51161
   181
begin
haftmann@51161
   182
haftmann@51161
   183
definition
haftmann@51161
   184
  "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. lookup m1 k = lookup m2 k)"
haftmann@51161
   185
wenzelm@60679
   186
instance
wenzelm@60679
   187
  by standard (unfold equal_mapping_def, transfer, auto)
haftmann@51161
   188
haftmann@51161
   189
end
haftmann@51161
   190
kuncar@53013
   191
context
kuncar@53013
   192
begin
haftmann@56528
   193
kuncar@53013
   194
interpretation lifting_syntax .
kuncar@53013
   195
haftmann@51161
   196
lemma [transfer_rule]:
kuncar@51379
   197
  assumes [transfer_rule]: "bi_total A"
kuncar@51379
   198
  assumes [transfer_rule]: "bi_unique B"
haftmann@56528
   199
  shows "(pcr_mapping A B ===> pcr_mapping A B ===> op=) HOL.eq HOL.equal"
haftmann@56528
   200
  by (unfold equal) transfer_prover
haftmann@51161
   201
haftmann@56529
   202
lemma of_alist_transfer [transfer_rule]:
haftmann@56529
   203
  assumes [transfer_rule]: "bi_unique R1"
haftmann@56529
   204
  shows "(list_all2 (rel_prod R1 R2) ===> pcr_mapping R1 R2) map_of of_alist"
haftmann@56529
   205
  unfolding of_alist_def [abs_def] map_of_foldr [abs_def] by transfer_prover
haftmann@56529
   206
kuncar@53013
   207
end
haftmann@51161
   208
haftmann@56528
   209
wenzelm@60500
   210
subsection \<open>Properties\<close>
haftmann@29708
   211
haftmann@56528
   212
lemma lookup_update:
haftmann@56528
   213
  "lookup (update k v m) k = Some v" 
kuncar@49973
   214
  by transfer simp
kuncar@49973
   215
haftmann@56528
   216
lemma lookup_update_neq:
haftmann@56528
   217
  "k \<noteq> k' \<Longrightarrow> lookup (update k v m) k' = lookup m k'" 
kuncar@49973
   218
  by transfer simp
kuncar@49973
   219
haftmann@56528
   220
lemma lookup_empty:
haftmann@56528
   221
  "lookup empty k = None" 
kuncar@49973
   222
  by transfer simp
kuncar@49973
   223
kuncar@49929
   224
lemma keys_is_none_rep [code_unfold]:
haftmann@37052
   225
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
wenzelm@61068
   226
  by transfer (auto simp add: Option.is_none_def)
haftmann@29708
   227
haftmann@29708
   228
lemma update_update:
haftmann@29708
   229
  "update k v (update k w m) = update k v m"
haftmann@29708
   230
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
kuncar@49929
   231
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   232
haftmann@35157
   233
lemma update_delete [simp]:
haftmann@35157
   234
  "update k v (delete k m) = update k v m"
kuncar@49929
   235
  by transfer simp
haftmann@29708
   236
haftmann@29708
   237
lemma delete_update:
haftmann@29708
   238
  "delete k (update k v m) = delete k m"
haftmann@29708
   239
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
kuncar@49929
   240
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   241
haftmann@35157
   242
lemma delete_empty [simp]:
haftmann@35157
   243
  "delete k empty = empty"
kuncar@49929
   244
  by transfer simp
haftmann@29708
   245
haftmann@35157
   246
lemma replace_update:
haftmann@37052
   247
  "k \<notin> keys m \<Longrightarrow> replace k v m = m"
haftmann@37052
   248
  "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
kuncar@49929
   249
  by (transfer, auto simp add: replace_def fun_upd_twist)+
haftmann@29708
   250
haftmann@29708
   251
lemma size_empty [simp]:
haftmann@29708
   252
  "size empty = 0"
kuncar@49929
   253
  unfolding size_def by transfer simp
haftmann@29708
   254
haftmann@29708
   255
lemma size_update:
haftmann@37052
   256
  "finite (keys m) \<Longrightarrow> size (update k v m) =
haftmann@37052
   257
    (if k \<in> keys m then size m else Suc (size m))"
kuncar@49929
   258
  unfolding size_def by transfer (auto simp add: insert_dom)
haftmann@29708
   259
haftmann@29708
   260
lemma size_delete:
haftmann@37052
   261
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
kuncar@49929
   262
  unfolding size_def by transfer simp
haftmann@29708
   263
haftmann@37052
   264
lemma size_tabulate [simp]:
haftmann@29708
   265
  "size (tabulate ks f) = length (remdups ks)"
haftmann@56528
   266
  unfolding size_def by transfer (auto simp add: map_of_map_restrict  card_set comp_def)
haftmann@29708
   267
haftmann@29831
   268
lemma bulkload_tabulate:
haftmann@29826
   269
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
haftmann@56528
   270
  by transfer (auto simp add: map_of_map_restrict)
haftmann@29826
   271
kuncar@49929
   272
lemma is_empty_empty [simp]:
haftmann@37052
   273
  "is_empty empty"
kuncar@49929
   274
  unfolding is_empty_def by transfer simp 
haftmann@37052
   275
haftmann@37052
   276
lemma is_empty_update [simp]:
haftmann@37052
   277
  "\<not> is_empty (update k v m)"
kuncar@49929
   278
  unfolding is_empty_def by transfer simp
haftmann@37052
   279
haftmann@37052
   280
lemma is_empty_delete:
haftmann@37052
   281
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
kuncar@49929
   282
  unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
haftmann@37052
   283
haftmann@37052
   284
lemma is_empty_replace [simp]:
haftmann@37052
   285
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
kuncar@49929
   286
  unfolding is_empty_def replace_def by transfer auto
haftmann@37052
   287
haftmann@37052
   288
lemma is_empty_default [simp]:
haftmann@37052
   289
  "\<not> is_empty (default k v m)"
kuncar@49929
   290
  unfolding is_empty_def default_def by transfer auto
haftmann@37052
   291
haftmann@37052
   292
lemma is_empty_map_entry [simp]:
haftmann@37052
   293
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
haftmann@56528
   294
  unfolding is_empty_def by transfer (auto split: option.split)
haftmann@37052
   295
haftmann@37052
   296
lemma is_empty_map_default [simp]:
haftmann@37052
   297
  "\<not> is_empty (map_default k v f m)"
haftmann@37052
   298
  by (simp add: map_default_def)
haftmann@37052
   299
haftmann@56545
   300
lemma keys_dom_lookup:
haftmann@56545
   301
  "keys m = dom (Mapping.lookup m)"
haftmann@56545
   302
  by transfer rule
haftmann@56545
   303
haftmann@37052
   304
lemma keys_empty [simp]:
haftmann@37052
   305
  "keys empty = {}"
kuncar@49929
   306
  by transfer simp
haftmann@37052
   307
haftmann@37052
   308
lemma keys_update [simp]:
haftmann@37052
   309
  "keys (update k v m) = insert k (keys m)"
kuncar@49929
   310
  by transfer simp
haftmann@37052
   311
haftmann@37052
   312
lemma keys_delete [simp]:
haftmann@37052
   313
  "keys (delete k m) = keys m - {k}"
kuncar@49929
   314
  by transfer simp
haftmann@37052
   315
haftmann@37052
   316
lemma keys_replace [simp]:
haftmann@37052
   317
  "keys (replace k v m) = keys m"
kuncar@49929
   318
  unfolding replace_def by transfer (simp add: insert_absorb)
haftmann@37052
   319
haftmann@37052
   320
lemma keys_default [simp]:
haftmann@37052
   321
  "keys (default k v m) = insert k (keys m)"
kuncar@49929
   322
  unfolding default_def by transfer (simp add: insert_absorb)
haftmann@37052
   323
haftmann@37052
   324
lemma keys_map_entry [simp]:
haftmann@37052
   325
  "keys (map_entry k f m) = keys m"
haftmann@56528
   326
  by transfer (auto split: option.split)
haftmann@37052
   327
haftmann@37052
   328
lemma keys_map_default [simp]:
haftmann@37052
   329
  "keys (map_default k v f m) = insert k (keys m)"
haftmann@37052
   330
  by (simp add: map_default_def)
haftmann@37052
   331
haftmann@37052
   332
lemma keys_tabulate [simp]:
haftmann@37026
   333
  "keys (tabulate ks f) = set ks"
kuncar@49929
   334
  by transfer (simp add: map_of_map_restrict o_def)
haftmann@37026
   335
haftmann@37052
   336
lemma keys_bulkload [simp]:
haftmann@37026
   337
  "keys (bulkload xs) = {0..<length xs}"
haftmann@56528
   338
  by (simp add: bulkload_tabulate)
haftmann@37026
   339
haftmann@37052
   340
lemma distinct_ordered_keys [simp]:
haftmann@37052
   341
  "distinct (ordered_keys m)"
haftmann@37052
   342
  by (simp add: ordered_keys_def)
haftmann@37052
   343
haftmann@37052
   344
lemma ordered_keys_infinite [simp]:
haftmann@37052
   345
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
haftmann@37052
   346
  by (simp add: ordered_keys_def)
haftmann@37052
   347
haftmann@37052
   348
lemma ordered_keys_empty [simp]:
haftmann@37052
   349
  "ordered_keys empty = []"
haftmann@37052
   350
  by (simp add: ordered_keys_def)
haftmann@37052
   351
haftmann@37052
   352
lemma ordered_keys_update [simp]:
haftmann@37052
   353
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
haftmann@37052
   354
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
haftmann@37052
   355
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
haftmann@37052
   356
haftmann@37052
   357
lemma ordered_keys_delete [simp]:
haftmann@37052
   358
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
haftmann@37052
   359
proof (cases "finite (keys m)")
haftmann@37052
   360
  case False then show ?thesis by simp
haftmann@37052
   361
next
haftmann@37052
   362
  case True note fin = True
haftmann@37052
   363
  show ?thesis
haftmann@37052
   364
  proof (cases "k \<in> keys m")
haftmann@37052
   365
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
haftmann@37052
   366
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
haftmann@37052
   367
  next
haftmann@37052
   368
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
haftmann@37052
   369
  qed
haftmann@37052
   370
qed
haftmann@37052
   371
haftmann@37052
   372
lemma ordered_keys_replace [simp]:
haftmann@37052
   373
  "ordered_keys (replace k v m) = ordered_keys m"
haftmann@37052
   374
  by (simp add: replace_def)
haftmann@37052
   375
haftmann@37052
   376
lemma ordered_keys_default [simp]:
haftmann@37052
   377
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
haftmann@37052
   378
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
haftmann@37052
   379
  by (simp_all add: default_def)
haftmann@37052
   380
haftmann@37052
   381
lemma ordered_keys_map_entry [simp]:
haftmann@37052
   382
  "ordered_keys (map_entry k f m) = ordered_keys m"
haftmann@37052
   383
  by (simp add: ordered_keys_def)
haftmann@37052
   384
haftmann@37052
   385
lemma ordered_keys_map_default [simp]:
haftmann@37052
   386
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
haftmann@37052
   387
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
haftmann@37052
   388
  by (simp_all add: map_default_def)
haftmann@37052
   389
haftmann@37052
   390
lemma ordered_keys_tabulate [simp]:
haftmann@37052
   391
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
haftmann@37052
   392
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
haftmann@37052
   393
haftmann@37052
   394
lemma ordered_keys_bulkload [simp]:
haftmann@37052
   395
  "ordered_keys (bulkload ks) = [0..<length ks]"
haftmann@37052
   396
  by (simp add: ordered_keys_def)
haftmann@36110
   397
haftmann@56528
   398
lemma tabulate_fold:
haftmann@56528
   399
  "tabulate xs f = fold (\<lambda>k m. update k (f k) m) xs empty"
haftmann@56528
   400
proof transfer
haftmann@56528
   401
  fix f :: "'a \<Rightarrow> 'b" and xs
haftmann@56529
   402
  have "map_of (List.map (\<lambda>k. (k, f k)) xs) = foldr (\<lambda>k m. m(k \<mapsto> f k)) xs Map.empty"
haftmann@56529
   403
    by (simp add: foldr_map comp_def map_of_foldr)
haftmann@56528
   404
  also have "foldr (\<lambda>k m. m(k \<mapsto> f k)) xs = fold (\<lambda>k m. m(k \<mapsto> f k)) xs"
haftmann@56528
   405
    by (rule foldr_fold) (simp add: fun_eq_iff)
haftmann@56528
   406
  ultimately show "map_of (List.map (\<lambda>k. (k, f k)) xs) = fold (\<lambda>k m. m(k \<mapsto> f k)) xs Map.empty"
haftmann@56528
   407
    by simp
haftmann@56528
   408
qed
haftmann@56528
   409
haftmann@31459
   410
wenzelm@60500
   411
subsection \<open>Code generator setup\<close>
haftmann@31459
   412
kuncar@49929
   413
hide_const (open) empty is_empty rep lookup update delete ordered_keys keys size
haftmann@54853
   414
  replace default map_entry map_default tabulate bulkload map of_alist
haftmann@35157
   415
huffman@49975
   416
end
haftmann@59485
   417