src/HOL/Library/Mapping.thy
author haftmann
Sat Apr 12 11:27:36 2014 +0200 (2014-04-12)
changeset 56545 8f1e7596deb7
parent 56529 aff193f53a64
child 58881 b9556a055632
permissions -rw-r--r--
more operations and lemmas
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
haftmann@56529
    13
lemma map_of_foldr: -- {* FIXME move *}
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"
haftmann@56528
    43
  by (auto simp add: 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" 
haftmann@56528
    48
  unfolding dom_def [abs_def] 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
haftmann@29708
    95
subsection {* Type definition and primitive operations *}
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@56528
   101
setup_lifting (no_code) 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@56528
   109
lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   110
  is "\<lambda>k v m. m(k \<mapsto> v)" parametric update_parametric .
haftmann@37700
   111
haftmann@56528
   112
lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   113
  is "\<lambda>k m. m(k := None)" parametric delete_parametric .
haftmann@39380
   114
haftmann@56528
   115
lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set"
haftmann@56529
   116
  is dom parametric dom_parametric .
haftmann@29708
   117
haftmann@56528
   118
lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   119
  is "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" parametric tabulate_parametric .
haftmann@29708
   120
haftmann@56528
   121
lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping"
haftmann@56529
   122
  is "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" parametric bulkload_parametric .
haftmann@29708
   123
haftmann@56528
   124
lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping"
haftmann@56529
   125
  is "\<lambda>f g m. (map_option g \<circ> m \<circ> f)" parametric map_parametric .
haftmann@29708
   126
haftmann@51161
   127
haftmann@40605
   128
subsection {* Functorial structure *}
haftmann@40605
   129
blanchet@55467
   130
functor map: map
blanchet@55466
   131
  by (transfer, auto simp add: fun_eq_iff option.map_comp option.map_id)+
haftmann@40605
   132
haftmann@51161
   133
haftmann@29708
   134
subsection {* Derived operations *}
haftmann@29708
   135
haftmann@56528
   136
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list"
haftmann@56528
   137
where
haftmann@37052
   138
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
haftmann@35194
   139
haftmann@56528
   140
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool"
haftmann@56528
   141
where
haftmann@37052
   142
  "is_empty m \<longleftrightarrow> keys m = {}"
haftmann@35157
   143
haftmann@56528
   144
definition size :: "('a, 'b) mapping \<Rightarrow> nat"
haftmann@56528
   145
where
haftmann@37052
   146
  "size m = (if finite (keys m) then card (keys m) else 0)"
haftmann@35157
   147
haftmann@56528
   148
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   149
where
haftmann@37052
   150
  "replace k v m = (if k \<in> keys m then update k v m else m)"
haftmann@29814
   151
haftmann@56528
   152
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   153
where
haftmann@37052
   154
  "default k v m = (if k \<in> keys m then m else update k v m)"
haftmann@37026
   155
haftmann@56529
   156
text {* Manual derivation of transfer rule is non-trivial *}
haftmann@56529
   157
kuncar@49929
   158
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
kuncar@49929
   159
  "\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
   160
    | Some v \<Rightarrow> m (k \<mapsto> (f v)))" parametric map_entry_parametric .
kuncar@49929
   161
haftmann@56529
   162
lemma map_entry_code [code]:
haftmann@56529
   163
  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
huffman@49975
   164
    | Some v \<Rightarrow> update k (f v) m)"
huffman@49975
   165
  by transfer rule
haftmann@37026
   166
haftmann@56528
   167
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   168
where
haftmann@37026
   169
  "map_default k v f m = map_entry k f (default k v m)" 
haftmann@37026
   170
haftmann@56529
   171
definition of_alist :: "('k \<times> 'v) list \<Rightarrow> ('k, 'v) mapping"
haftmann@56529
   172
where
haftmann@54853
   173
  "of_alist xs = foldr (\<lambda>(k, v) m. update k v m) xs empty"
kuncar@51379
   174
haftmann@51161
   175
instantiation mapping :: (type, type) equal
haftmann@51161
   176
begin
haftmann@51161
   177
haftmann@51161
   178
definition
haftmann@51161
   179
  "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. lookup m1 k = lookup m2 k)"
haftmann@51161
   180
haftmann@51161
   181
instance proof
haftmann@51161
   182
qed (unfold equal_mapping_def, transfer, auto)
haftmann@51161
   183
haftmann@51161
   184
end
haftmann@51161
   185
kuncar@53013
   186
context
kuncar@53013
   187
begin
haftmann@56528
   188
kuncar@53013
   189
interpretation lifting_syntax .
kuncar@53013
   190
haftmann@51161
   191
lemma [transfer_rule]:
kuncar@51379
   192
  assumes [transfer_rule]: "bi_total A"
kuncar@51379
   193
  assumes [transfer_rule]: "bi_unique B"
haftmann@56528
   194
  shows "(pcr_mapping A B ===> pcr_mapping A B ===> op=) HOL.eq HOL.equal"
haftmann@56528
   195
  by (unfold equal) transfer_prover
haftmann@51161
   196
haftmann@56529
   197
lemma of_alist_transfer [transfer_rule]:
haftmann@56529
   198
  assumes [transfer_rule]: "bi_unique R1"
haftmann@56529
   199
  shows "(list_all2 (rel_prod R1 R2) ===> pcr_mapping R1 R2) map_of of_alist"
haftmann@56529
   200
  unfolding of_alist_def [abs_def] map_of_foldr [abs_def] by transfer_prover
haftmann@56529
   201
kuncar@53013
   202
end
haftmann@51161
   203
haftmann@56528
   204
haftmann@29708
   205
subsection {* Properties *}
haftmann@29708
   206
haftmann@56528
   207
lemma lookup_update:
haftmann@56528
   208
  "lookup (update k v m) k = Some v" 
kuncar@49973
   209
  by transfer simp
kuncar@49973
   210
haftmann@56528
   211
lemma lookup_update_neq:
haftmann@56528
   212
  "k \<noteq> k' \<Longrightarrow> lookup (update k v m) k' = lookup m k'" 
kuncar@49973
   213
  by transfer simp
kuncar@49973
   214
haftmann@56528
   215
lemma lookup_empty:
haftmann@56528
   216
  "lookup empty k = None" 
kuncar@49973
   217
  by transfer simp
kuncar@49973
   218
kuncar@49929
   219
lemma keys_is_none_rep [code_unfold]:
haftmann@37052
   220
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
kuncar@49929
   221
  by transfer (auto simp add: is_none_def)
haftmann@29708
   222
haftmann@29708
   223
lemma update_update:
haftmann@29708
   224
  "update k v (update k w m) = update k v m"
haftmann@29708
   225
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
kuncar@49929
   226
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   227
haftmann@35157
   228
lemma update_delete [simp]:
haftmann@35157
   229
  "update k v (delete k m) = update k v m"
kuncar@49929
   230
  by transfer simp
haftmann@29708
   231
haftmann@29708
   232
lemma delete_update:
haftmann@29708
   233
  "delete k (update k v m) = delete k m"
haftmann@29708
   234
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
kuncar@49929
   235
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   236
haftmann@35157
   237
lemma delete_empty [simp]:
haftmann@35157
   238
  "delete k empty = empty"
kuncar@49929
   239
  by transfer simp
haftmann@29708
   240
haftmann@35157
   241
lemma replace_update:
haftmann@37052
   242
  "k \<notin> keys m \<Longrightarrow> replace k v m = m"
haftmann@37052
   243
  "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
kuncar@49929
   244
  by (transfer, auto simp add: replace_def fun_upd_twist)+
haftmann@29708
   245
haftmann@29708
   246
lemma size_empty [simp]:
haftmann@29708
   247
  "size empty = 0"
kuncar@49929
   248
  unfolding size_def by transfer simp
haftmann@29708
   249
haftmann@29708
   250
lemma size_update:
haftmann@37052
   251
  "finite (keys m) \<Longrightarrow> size (update k v m) =
haftmann@37052
   252
    (if k \<in> keys m then size m else Suc (size m))"
kuncar@49929
   253
  unfolding size_def by transfer (auto simp add: insert_dom)
haftmann@29708
   254
haftmann@29708
   255
lemma size_delete:
haftmann@37052
   256
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
kuncar@49929
   257
  unfolding size_def by transfer simp
haftmann@29708
   258
haftmann@37052
   259
lemma size_tabulate [simp]:
haftmann@29708
   260
  "size (tabulate ks f) = length (remdups ks)"
haftmann@56528
   261
  unfolding size_def by transfer (auto simp add: map_of_map_restrict  card_set comp_def)
haftmann@29708
   262
haftmann@29831
   263
lemma bulkload_tabulate:
haftmann@29826
   264
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
haftmann@56528
   265
  by transfer (auto simp add: map_of_map_restrict)
haftmann@29826
   266
kuncar@49929
   267
lemma is_empty_empty [simp]:
haftmann@37052
   268
  "is_empty empty"
kuncar@49929
   269
  unfolding is_empty_def by transfer simp 
haftmann@37052
   270
haftmann@37052
   271
lemma is_empty_update [simp]:
haftmann@37052
   272
  "\<not> is_empty (update k v m)"
kuncar@49929
   273
  unfolding is_empty_def by transfer simp
haftmann@37052
   274
haftmann@37052
   275
lemma is_empty_delete:
haftmann@37052
   276
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
kuncar@49929
   277
  unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
haftmann@37052
   278
haftmann@37052
   279
lemma is_empty_replace [simp]:
haftmann@37052
   280
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
kuncar@49929
   281
  unfolding is_empty_def replace_def by transfer auto
haftmann@37052
   282
haftmann@37052
   283
lemma is_empty_default [simp]:
haftmann@37052
   284
  "\<not> is_empty (default k v m)"
kuncar@49929
   285
  unfolding is_empty_def default_def by transfer auto
haftmann@37052
   286
haftmann@37052
   287
lemma is_empty_map_entry [simp]:
haftmann@37052
   288
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
haftmann@56528
   289
  unfolding is_empty_def by transfer (auto split: option.split)
haftmann@37052
   290
haftmann@37052
   291
lemma is_empty_map_default [simp]:
haftmann@37052
   292
  "\<not> is_empty (map_default k v f m)"
haftmann@37052
   293
  by (simp add: map_default_def)
haftmann@37052
   294
haftmann@56545
   295
lemma keys_dom_lookup:
haftmann@56545
   296
  "keys m = dom (Mapping.lookup m)"
haftmann@56545
   297
  by transfer rule
haftmann@56545
   298
haftmann@37052
   299
lemma keys_empty [simp]:
haftmann@37052
   300
  "keys empty = {}"
kuncar@49929
   301
  by transfer simp
haftmann@37052
   302
haftmann@37052
   303
lemma keys_update [simp]:
haftmann@37052
   304
  "keys (update k v m) = insert k (keys m)"
kuncar@49929
   305
  by transfer simp
haftmann@37052
   306
haftmann@37052
   307
lemma keys_delete [simp]:
haftmann@37052
   308
  "keys (delete k m) = keys m - {k}"
kuncar@49929
   309
  by transfer simp
haftmann@37052
   310
haftmann@37052
   311
lemma keys_replace [simp]:
haftmann@37052
   312
  "keys (replace k v m) = keys m"
kuncar@49929
   313
  unfolding replace_def by transfer (simp add: insert_absorb)
haftmann@37052
   314
haftmann@37052
   315
lemma keys_default [simp]:
haftmann@37052
   316
  "keys (default k v m) = insert k (keys m)"
kuncar@49929
   317
  unfolding default_def by transfer (simp add: insert_absorb)
haftmann@37052
   318
haftmann@37052
   319
lemma keys_map_entry [simp]:
haftmann@37052
   320
  "keys (map_entry k f m) = keys m"
haftmann@56528
   321
  by transfer (auto split: option.split)
haftmann@37052
   322
haftmann@37052
   323
lemma keys_map_default [simp]:
haftmann@37052
   324
  "keys (map_default k v f m) = insert k (keys m)"
haftmann@37052
   325
  by (simp add: map_default_def)
haftmann@37052
   326
haftmann@37052
   327
lemma keys_tabulate [simp]:
haftmann@37026
   328
  "keys (tabulate ks f) = set ks"
kuncar@49929
   329
  by transfer (simp add: map_of_map_restrict o_def)
haftmann@37026
   330
haftmann@37052
   331
lemma keys_bulkload [simp]:
haftmann@37026
   332
  "keys (bulkload xs) = {0..<length xs}"
haftmann@56528
   333
  by (simp add: bulkload_tabulate)
haftmann@37026
   334
haftmann@37052
   335
lemma distinct_ordered_keys [simp]:
haftmann@37052
   336
  "distinct (ordered_keys m)"
haftmann@37052
   337
  by (simp add: ordered_keys_def)
haftmann@37052
   338
haftmann@37052
   339
lemma ordered_keys_infinite [simp]:
haftmann@37052
   340
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
haftmann@37052
   341
  by (simp add: ordered_keys_def)
haftmann@37052
   342
haftmann@37052
   343
lemma ordered_keys_empty [simp]:
haftmann@37052
   344
  "ordered_keys empty = []"
haftmann@37052
   345
  by (simp add: ordered_keys_def)
haftmann@37052
   346
haftmann@37052
   347
lemma ordered_keys_update [simp]:
haftmann@37052
   348
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
haftmann@37052
   349
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
haftmann@37052
   350
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
haftmann@37052
   351
haftmann@37052
   352
lemma ordered_keys_delete [simp]:
haftmann@37052
   353
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
haftmann@37052
   354
proof (cases "finite (keys m)")
haftmann@37052
   355
  case False then show ?thesis by simp
haftmann@37052
   356
next
haftmann@37052
   357
  case True note fin = True
haftmann@37052
   358
  show ?thesis
haftmann@37052
   359
  proof (cases "k \<in> keys m")
haftmann@37052
   360
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
haftmann@37052
   361
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
haftmann@37052
   362
  next
haftmann@37052
   363
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
haftmann@37052
   364
  qed
haftmann@37052
   365
qed
haftmann@37052
   366
haftmann@37052
   367
lemma ordered_keys_replace [simp]:
haftmann@37052
   368
  "ordered_keys (replace k v m) = ordered_keys m"
haftmann@37052
   369
  by (simp add: replace_def)
haftmann@37052
   370
haftmann@37052
   371
lemma ordered_keys_default [simp]:
haftmann@37052
   372
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
haftmann@37052
   373
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
haftmann@37052
   374
  by (simp_all add: default_def)
haftmann@37052
   375
haftmann@37052
   376
lemma ordered_keys_map_entry [simp]:
haftmann@37052
   377
  "ordered_keys (map_entry k f m) = ordered_keys m"
haftmann@37052
   378
  by (simp add: ordered_keys_def)
haftmann@37052
   379
haftmann@37052
   380
lemma ordered_keys_map_default [simp]:
haftmann@37052
   381
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
haftmann@37052
   382
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
haftmann@37052
   383
  by (simp_all add: map_default_def)
haftmann@37052
   384
haftmann@37052
   385
lemma ordered_keys_tabulate [simp]:
haftmann@37052
   386
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
haftmann@37052
   387
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
haftmann@37052
   388
haftmann@37052
   389
lemma ordered_keys_bulkload [simp]:
haftmann@37052
   390
  "ordered_keys (bulkload ks) = [0..<length ks]"
haftmann@37052
   391
  by (simp add: ordered_keys_def)
haftmann@36110
   392
haftmann@56528
   393
lemma tabulate_fold:
haftmann@56528
   394
  "tabulate xs f = fold (\<lambda>k m. update k (f k) m) xs empty"
haftmann@56528
   395
proof transfer
haftmann@56528
   396
  fix f :: "'a \<Rightarrow> 'b" and xs
haftmann@56529
   397
  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
   398
    by (simp add: foldr_map comp_def map_of_foldr)
haftmann@56528
   399
  also have "foldr (\<lambda>k m. m(k \<mapsto> f k)) xs = fold (\<lambda>k m. m(k \<mapsto> f k)) xs"
haftmann@56528
   400
    by (rule foldr_fold) (simp add: fun_eq_iff)
haftmann@56528
   401
  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
   402
    by simp
haftmann@56528
   403
qed
haftmann@56528
   404
haftmann@31459
   405
haftmann@37700
   406
subsection {* Code generator setup *}
haftmann@31459
   407
haftmann@37701
   408
code_datatype empty update
haftmann@37701
   409
kuncar@49929
   410
hide_const (open) empty is_empty rep lookup update delete ordered_keys keys size
haftmann@54853
   411
  replace default map_entry map_default tabulate bulkload map of_alist
haftmann@35157
   412
huffman@49975
   413
end