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