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