src/HOL/Library/Mapping.thy
author haftmann
Fri May 21 15:22:37 2010 +0200 (2010-05-21)
changeset 37052 80dd92673fca
parent 37026 7e8979a155ae
child 37107 1535aa1c943a
permissions -rw-r--r--
more lemmas about mappings, in particular keys
haftmann@31459
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@29708
     2
haftmann@29708
     3
header {* An abstract view on maps for code generation. *}
haftmann@29708
     4
haftmann@29708
     5
theory Mapping
haftmann@35157
     6
imports Main
haftmann@29708
     7
begin
haftmann@29708
     8
haftmann@37052
     9
lemma remove1_idem: (*FIXME move to List.thy*)
haftmann@37052
    10
  assumes "x \<notin> set xs"
haftmann@37052
    11
  shows "remove1 x xs = xs"
haftmann@37052
    12
  using assms by (induct xs) simp_all
haftmann@37052
    13
haftmann@37052
    14
lemma remove1_insort [simp]:
haftmann@37052
    15
  "remove1 x (insort x xs) = xs"
haftmann@37052
    16
  by (induct xs) simp_all
haftmann@37052
    17
haftmann@37052
    18
lemma sorted_list_of_set_remove:
haftmann@37052
    19
  assumes "finite A"
haftmann@37052
    20
  shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
haftmann@37052
    21
proof (cases "x \<in> A")
haftmann@37052
    22
  case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp
haftmann@37052
    23
  with False show ?thesis by (simp add: remove1_idem)
haftmann@37052
    24
next
haftmann@37052
    25
  case True then obtain B where A: "A = insert x B" by (rule Set.set_insert)
haftmann@37052
    26
  with assms show ?thesis by simp
haftmann@37052
    27
qed
haftmann@37052
    28
haftmann@37052
    29
lemma sorted_list_of_set_range [simp]:
haftmann@37052
    30
  "sorted_list_of_set {m..<n} = [m..<n]"
haftmann@37052
    31
  by (rule sorted_distinct_set_unique) simp_all
haftmann@37052
    32
haftmann@29708
    33
subsection {* Type definition and primitive operations *}
haftmann@29708
    34
haftmann@35157
    35
datatype ('a, 'b) mapping = Mapping "'a \<rightharpoonup> 'b"
haftmann@29708
    36
haftmann@35157
    37
definition empty :: "('a, 'b) mapping" where
haftmann@35157
    38
  "empty = Mapping (\<lambda>_. None)"
haftmann@29708
    39
haftmann@35157
    40
primrec lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<rightharpoonup> 'b" where
haftmann@35157
    41
  "lookup (Mapping f) = f"
haftmann@29708
    42
haftmann@35157
    43
primrec update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@35157
    44
  "update k v (Mapping f) = Mapping (f (k \<mapsto> v))"
haftmann@29708
    45
haftmann@35157
    46
primrec delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@35157
    47
  "delete k (Mapping f) = Mapping (f (k := None))"
haftmann@29708
    48
haftmann@29708
    49
haftmann@29708
    50
subsection {* Derived operations *}
haftmann@29708
    51
haftmann@35157
    52
definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" where
haftmann@35157
    53
  "keys m = dom (lookup m)"
haftmann@29708
    54
haftmann@35194
    55
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
haftmann@37052
    56
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
haftmann@35194
    57
haftmann@35157
    58
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
haftmann@37052
    59
  "is_empty m \<longleftrightarrow> keys m = {}"
haftmann@35157
    60
haftmann@35157
    61
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
haftmann@37052
    62
  "size m = (if finite (keys m) then card (keys m) else 0)"
haftmann@35157
    63
haftmann@35157
    64
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37052
    65
  "replace k v m = (if k \<in> keys m then update k v m else m)"
haftmann@29814
    66
haftmann@37026
    67
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37052
    68
  "default k v m = (if k \<in> keys m then m else update k v m)"
haftmann@37026
    69
haftmann@37026
    70
definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37026
    71
  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
haftmann@37026
    72
    | Some v \<Rightarrow> update k (f v) m)" 
haftmann@37026
    73
haftmann@37026
    74
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37026
    75
  "map_default k v f m = map_entry k f (default k v m)" 
haftmann@37026
    76
haftmann@35157
    77
definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" where
haftmann@35157
    78
  "tabulate ks f = Mapping (map_of (map (\<lambda>k. (k, f k)) ks))"
haftmann@29708
    79
haftmann@35157
    80
definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" where
haftmann@35157
    81
  "bulkload xs = Mapping (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
haftmann@29826
    82
haftmann@29708
    83
haftmann@29708
    84
subsection {* Properties *}
haftmann@29708
    85
haftmann@35157
    86
lemma lookup_inject [simp]:
haftmann@29708
    87
  "lookup m = lookup n \<longleftrightarrow> m = n"
haftmann@29708
    88
  by (cases m, cases n) simp
haftmann@29708
    89
haftmann@35157
    90
lemma mapping_eqI:
haftmann@35157
    91
  assumes "lookup m = lookup n"
haftmann@35157
    92
  shows "m = n"
haftmann@35157
    93
  using assms by simp
haftmann@35157
    94
haftmann@37052
    95
lemma keys_is_none_lookup [code_inline]:
haftmann@37052
    96
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
haftmann@37052
    97
  by (auto simp add: keys_def is_none_def)
haftmann@37052
    98
haftmann@29708
    99
lemma lookup_empty [simp]:
haftmann@29708
   100
  "lookup empty = Map.empty"
haftmann@29708
   101
  by (simp add: empty_def)
haftmann@29708
   102
haftmann@29708
   103
lemma lookup_update [simp]:
haftmann@29708
   104
  "lookup (update k v m) = (lookup m) (k \<mapsto> v)"
haftmann@29708
   105
  by (cases m) simp
haftmann@29708
   106
haftmann@35157
   107
lemma lookup_delete [simp]:
haftmann@35157
   108
  "lookup (delete k m) = (lookup m) (k := None)"
haftmann@35157
   109
  by (cases m) simp
haftmann@29708
   110
haftmann@37026
   111
lemma lookup_map_entry [simp]:
haftmann@37026
   112
  "lookup (map_entry k f m) = (lookup m) (k := Option.map f (lookup m k))"
haftmann@37026
   113
  by (cases "lookup m k") (simp_all add: map_entry_def expand_fun_eq)
haftmann@37026
   114
haftmann@35157
   115
lemma lookup_tabulate [simp]:
haftmann@29708
   116
  "lookup (tabulate ks f) = (Some o f) |` set ks"
haftmann@29708
   117
  by (induct ks) (auto simp add: tabulate_def restrict_map_def expand_fun_eq)
haftmann@29708
   118
haftmann@35157
   119
lemma lookup_bulkload [simp]:
haftmann@29826
   120
  "lookup (bulkload xs) = (\<lambda>k. if k < length xs then Some (xs ! k) else None)"
haftmann@35157
   121
  by (simp add: bulkload_def)
haftmann@29826
   122
haftmann@29708
   123
lemma update_update:
haftmann@29708
   124
  "update k v (update k w m) = update k v m"
haftmann@29708
   125
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
haftmann@35157
   126
  by (rule mapping_eqI, simp add: fun_upd_twist)+
haftmann@29708
   127
haftmann@35157
   128
lemma update_delete [simp]:
haftmann@35157
   129
  "update k v (delete k m) = update k v m"
haftmann@35157
   130
  by (rule mapping_eqI) simp
haftmann@29708
   131
haftmann@29708
   132
lemma delete_update:
haftmann@29708
   133
  "delete k (update k v m) = delete k m"
haftmann@29708
   134
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
haftmann@35157
   135
  by (rule mapping_eqI, simp add: fun_upd_twist)+
haftmann@29708
   136
haftmann@35157
   137
lemma delete_empty [simp]:
haftmann@35157
   138
  "delete k empty = empty"
haftmann@35157
   139
  by (rule mapping_eqI) simp
haftmann@29708
   140
haftmann@35157
   141
lemma replace_update:
haftmann@37052
   142
  "k \<notin> keys m \<Longrightarrow> replace k v m = m"
haftmann@37052
   143
  "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
haftmann@37052
   144
  by (rule mapping_eqI) (auto simp add: replace_def fun_upd_twist)+
haftmann@29708
   145
haftmann@29708
   146
lemma size_empty [simp]:
haftmann@29708
   147
  "size empty = 0"
haftmann@37052
   148
  by (simp add: size_def keys_def)
haftmann@29708
   149
haftmann@29708
   150
lemma size_update:
haftmann@37052
   151
  "finite (keys m) \<Longrightarrow> size (update k v m) =
haftmann@37052
   152
    (if k \<in> keys m then size m else Suc (size m))"
haftmann@37052
   153
  by (auto simp add: size_def insert_dom keys_def)
haftmann@29708
   154
haftmann@29708
   155
lemma size_delete:
haftmann@37052
   156
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
haftmann@37052
   157
  by (simp add: size_def keys_def)
haftmann@29708
   158
haftmann@37052
   159
lemma size_tabulate [simp]:
haftmann@29708
   160
  "size (tabulate ks f) = length (remdups ks)"
haftmann@37052
   161
  by (simp add: size_def distinct_card [of "remdups ks", symmetric] comp_def keys_def)
haftmann@29708
   162
haftmann@29831
   163
lemma bulkload_tabulate:
haftmann@29826
   164
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
haftmann@35157
   165
  by (rule mapping_eqI) (simp add: expand_fun_eq)
haftmann@29826
   166
haftmann@37052
   167
lemma is_empty_empty: (*FIXME*)
haftmann@37052
   168
  "is_empty m \<longleftrightarrow> m = Mapping Map.empty"
haftmann@37052
   169
  by (cases m) (simp add: is_empty_def keys_def)
haftmann@37052
   170
haftmann@37052
   171
lemma is_empty_empty' [simp]:
haftmann@37052
   172
  "is_empty empty"
haftmann@37052
   173
  by (simp add: is_empty_empty empty_def) 
haftmann@37052
   174
haftmann@37052
   175
lemma is_empty_update [simp]:
haftmann@37052
   176
  "\<not> is_empty (update k v m)"
haftmann@37052
   177
  by (cases m) (simp add: is_empty_empty)
haftmann@37052
   178
haftmann@37052
   179
lemma is_empty_delete:
haftmann@37052
   180
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
haftmann@37052
   181
  by (cases m) (auto simp add: is_empty_empty keys_def dom_eq_empty_conv [symmetric] simp del: dom_eq_empty_conv)
haftmann@37052
   182
haftmann@37052
   183
lemma is_empty_replace [simp]:
haftmann@37052
   184
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
haftmann@37052
   185
  by (auto simp add: replace_def) (simp add: is_empty_def)
haftmann@37052
   186
haftmann@37052
   187
lemma is_empty_default [simp]:
haftmann@37052
   188
  "\<not> is_empty (default k v m)"
haftmann@37052
   189
  by (auto simp add: default_def) (simp add: is_empty_def)
haftmann@37052
   190
haftmann@37052
   191
lemma is_empty_map_entry [simp]:
haftmann@37052
   192
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
haftmann@37052
   193
  by (cases "lookup m k")
haftmann@37052
   194
    (auto simp add: map_entry_def, simp add: is_empty_empty)
haftmann@37052
   195
haftmann@37052
   196
lemma is_empty_map_default [simp]:
haftmann@37052
   197
  "\<not> is_empty (map_default k v f m)"
haftmann@37052
   198
  by (simp add: map_default_def)
haftmann@37052
   199
haftmann@37052
   200
lemma keys_empty [simp]:
haftmann@37052
   201
  "keys empty = {}"
haftmann@37052
   202
  by (simp add: keys_def)
haftmann@37052
   203
haftmann@37052
   204
lemma keys_update [simp]:
haftmann@37052
   205
  "keys (update k v m) = insert k (keys m)"
haftmann@37052
   206
  by (simp add: keys_def)
haftmann@37052
   207
haftmann@37052
   208
lemma keys_delete [simp]:
haftmann@37052
   209
  "keys (delete k m) = keys m - {k}"
haftmann@37052
   210
  by (simp add: keys_def)
haftmann@37052
   211
haftmann@37052
   212
lemma keys_replace [simp]:
haftmann@37052
   213
  "keys (replace k v m) = keys m"
haftmann@37052
   214
  by (auto simp add: keys_def replace_def)
haftmann@37052
   215
haftmann@37052
   216
lemma keys_default [simp]:
haftmann@37052
   217
  "keys (default k v m) = insert k (keys m)"
haftmann@37052
   218
  by (auto simp add: keys_def default_def)
haftmann@37052
   219
haftmann@37052
   220
lemma keys_map_entry [simp]:
haftmann@37052
   221
  "keys (map_entry k f m) = keys m"
haftmann@37052
   222
  by (auto simp add: keys_def)
haftmann@37052
   223
haftmann@37052
   224
lemma keys_map_default [simp]:
haftmann@37052
   225
  "keys (map_default k v f m) = insert k (keys m)"
haftmann@37052
   226
  by (simp add: map_default_def)
haftmann@37052
   227
haftmann@37052
   228
lemma keys_tabulate [simp]:
haftmann@37026
   229
  "keys (tabulate ks f) = set ks"
haftmann@37026
   230
  by (simp add: tabulate_def keys_def map_of_map_restrict o_def)
haftmann@37026
   231
haftmann@37052
   232
lemma keys_bulkload [simp]:
haftmann@37026
   233
  "keys (bulkload xs) = {0..<length xs}"
haftmann@37026
   234
  by (simp add: keys_tabulate bulkload_tabulate)
haftmann@37026
   235
haftmann@37052
   236
lemma distinct_ordered_keys [simp]:
haftmann@37052
   237
  "distinct (ordered_keys m)"
haftmann@37052
   238
  by (simp add: ordered_keys_def)
haftmann@37052
   239
haftmann@37052
   240
lemma ordered_keys_infinite [simp]:
haftmann@37052
   241
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
haftmann@37052
   242
  by (simp add: ordered_keys_def)
haftmann@37052
   243
haftmann@37052
   244
lemma ordered_keys_empty [simp]:
haftmann@37052
   245
  "ordered_keys empty = []"
haftmann@37052
   246
  by (simp add: ordered_keys_def)
haftmann@37052
   247
haftmann@37052
   248
lemma ordered_keys_update [simp]:
haftmann@37052
   249
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
haftmann@37052
   250
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
haftmann@37052
   251
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
haftmann@37052
   252
haftmann@37052
   253
lemma ordered_keys_delete [simp]:
haftmann@37052
   254
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
haftmann@37052
   255
proof (cases "finite (keys m)")
haftmann@37052
   256
  case False then show ?thesis by simp
haftmann@37052
   257
next
haftmann@37052
   258
  case True note fin = True
haftmann@37052
   259
  show ?thesis
haftmann@37052
   260
  proof (cases "k \<in> keys m")
haftmann@37052
   261
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
haftmann@37052
   262
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
haftmann@37052
   263
  next
haftmann@37052
   264
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
haftmann@37052
   265
  qed
haftmann@37052
   266
qed
haftmann@37052
   267
haftmann@37052
   268
lemma ordered_keys_replace [simp]:
haftmann@37052
   269
  "ordered_keys (replace k v m) = ordered_keys m"
haftmann@37052
   270
  by (simp add: replace_def)
haftmann@37052
   271
haftmann@37052
   272
lemma ordered_keys_default [simp]:
haftmann@37052
   273
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
haftmann@37052
   274
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
haftmann@37052
   275
  by (simp_all add: default_def)
haftmann@37052
   276
haftmann@37052
   277
lemma ordered_keys_map_entry [simp]:
haftmann@37052
   278
  "ordered_keys (map_entry k f m) = ordered_keys m"
haftmann@37052
   279
  by (simp add: ordered_keys_def)
haftmann@37052
   280
haftmann@37052
   281
lemma ordered_keys_map_default [simp]:
haftmann@37052
   282
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
haftmann@37052
   283
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
haftmann@37052
   284
  by (simp_all add: map_default_def)
haftmann@37052
   285
haftmann@37052
   286
lemma ordered_keys_tabulate [simp]:
haftmann@37052
   287
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
haftmann@37052
   288
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
haftmann@37052
   289
haftmann@37052
   290
lemma ordered_keys_bulkload [simp]:
haftmann@37052
   291
  "ordered_keys (bulkload ks) = [0..<length ks]"
haftmann@37052
   292
  by (simp add: ordered_keys_def)
haftmann@36110
   293
haftmann@31459
   294
haftmann@31459
   295
subsection {* Some technical code lemmas *}
haftmann@31459
   296
haftmann@31459
   297
lemma [code]:
haftmann@35157
   298
  "mapping_case f m = f (Mapping.lookup m)"
haftmann@31459
   299
  by (cases m) simp
haftmann@31459
   300
haftmann@31459
   301
lemma [code]:
haftmann@35157
   302
  "mapping_rec f m = f (Mapping.lookup m)"
haftmann@31459
   303
  by (cases m) simp
haftmann@31459
   304
haftmann@31459
   305
lemma [code]:
haftmann@35157
   306
  "Nat.size (m :: (_, _) mapping) = 0"
haftmann@31459
   307
  by (cases m) simp
haftmann@31459
   308
haftmann@31459
   309
lemma [code]:
haftmann@35157
   310
  "mapping_size f g m = 0"
haftmann@31459
   311
  by (cases m) simp
haftmann@31459
   312
haftmann@35157
   313
wenzelm@36176
   314
hide_const (open) empty is_empty lookup update delete ordered_keys keys size replace tabulate bulkload
haftmann@35157
   315
haftmann@29708
   316
end