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