src/HOL/Library/Mapping.thy
author kuncar
Thu Oct 18 15:52:33 2012 +0200 (2012-10-18)
changeset 49929 70300f1b6835
parent 49834 b27bbb021df1
child 49939 eb8b434158c8
permissions -rw-r--r--
update RBT_Mapping, AList_Mapping and Mapping to use lifting/transfer
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@49929
     8
imports Main "~~/src/HOL/Library/Quotient_Option"
haftmann@29708
     9
begin
haftmann@29708
    10
haftmann@29708
    11
subsection {* Type definition and primitive operations *}
haftmann@29708
    12
wenzelm@49834
    13
typedef ('a, 'b) mapping = "UNIV :: ('a \<rightharpoonup> 'b) set"
kuncar@49929
    14
  morphisms rep Mapping ..
haftmann@37700
    15
kuncar@49929
    16
setup_lifting(no_code) type_definition_mapping
haftmann@37700
    17
kuncar@49929
    18
lift_definition empty :: "('a, 'b) mapping" is "(\<lambda>_. None)" .
haftmann@37700
    19
kuncar@49929
    20
lift_definition lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<Rightarrow> 'b option" is "\<lambda>m k. m k" .
kuncar@49929
    21
kuncar@49929
    22
lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is "\<lambda>k v m. m(k \<mapsto> v)" .
haftmann@37700
    23
kuncar@49929
    24
lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is "\<lambda>k m. m(k := None)" .
haftmann@39380
    25
kuncar@49929
    26
lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set" is dom .
haftmann@29708
    27
kuncar@49929
    28
lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping" is
kuncar@49929
    29
  "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" .
haftmann@29708
    30
kuncar@49929
    31
lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping" is
kuncar@49929
    32
  "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" .
haftmann@29708
    33
kuncar@49929
    34
lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping" is
kuncar@49929
    35
  "\<lambda>f g m. (Option.map g \<circ> m \<circ> f)" .
haftmann@29708
    36
haftmann@40605
    37
subsection {* Functorial structure *}
haftmann@40605
    38
haftmann@41505
    39
enriched_type map: map
kuncar@49929
    40
  by (transfer, auto simp add: fun_eq_iff Option.map.compositionality Option.map.id)+
haftmann@40605
    41
haftmann@29708
    42
subsection {* Derived operations *}
haftmann@29708
    43
haftmann@35194
    44
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
haftmann@37052
    45
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
haftmann@35194
    46
haftmann@35157
    47
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
haftmann@37052
    48
  "is_empty m \<longleftrightarrow> keys m = {}"
haftmann@35157
    49
haftmann@35157
    50
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
haftmann@37052
    51
  "size m = (if finite (keys m) then card (keys m) else 0)"
haftmann@35157
    52
haftmann@35157
    53
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37052
    54
  "replace k v m = (if k \<in> keys m then update k v m else m)"
haftmann@29814
    55
haftmann@37026
    56
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37052
    57
  "default k v m = (if k \<in> keys m then m else update k v m)"
haftmann@37026
    58
kuncar@49929
    59
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
kuncar@49929
    60
  "\<lambda>k f m. (case m k of None \<Rightarrow> m
kuncar@49929
    61
    | Some v \<Rightarrow> m (k \<mapsto> (f v)))" .
kuncar@49929
    62
kuncar@49929
    63
lemma map_entry_code [code]: "map_entry k f m = (case lookup m k of None \<Rightarrow> m
kuncar@49929
    64
    | Some v \<Rightarrow> update k (f v) m)" by transfer rule
haftmann@37026
    65
haftmann@37026
    66
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37026
    67
  "map_default k v f m = map_entry k f (default k v m)" 
haftmann@37026
    68
haftmann@29708
    69
subsection {* Properties *}
haftmann@29708
    70
kuncar@49929
    71
lemma keys_is_none_rep [code_unfold]:
haftmann@37052
    72
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
kuncar@49929
    73
  by transfer (auto simp add: is_none_def)
haftmann@29708
    74
kuncar@49929
    75
lemma tabulate_alt_def:
kuncar@49929
    76
  "map_of (List.map (\<lambda>k. (k, f k)) ks) = (Some o f) |` set ks"
kuncar@49929
    77
  by (induct ks) (auto simp add: tabulate_def restrict_map_def)
haftmann@29826
    78
haftmann@29708
    79
lemma update_update:
haftmann@29708
    80
  "update k v (update k w m) = update k v m"
haftmann@29708
    81
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
kuncar@49929
    82
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
    83
haftmann@35157
    84
lemma update_delete [simp]:
haftmann@35157
    85
  "update k v (delete k m) = update k v m"
kuncar@49929
    86
  by transfer simp
haftmann@29708
    87
haftmann@29708
    88
lemma delete_update:
haftmann@29708
    89
  "delete k (update k v m) = delete k m"
haftmann@29708
    90
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
kuncar@49929
    91
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
    92
haftmann@35157
    93
lemma delete_empty [simp]:
haftmann@35157
    94
  "delete k empty = empty"
kuncar@49929
    95
  by transfer simp
haftmann@29708
    96
haftmann@35157
    97
lemma replace_update:
haftmann@37052
    98
  "k \<notin> keys m \<Longrightarrow> replace k v m = m"
haftmann@37052
    99
  "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
kuncar@49929
   100
  by (transfer, auto simp add: replace_def fun_upd_twist)+
haftmann@29708
   101
haftmann@29708
   102
lemma size_empty [simp]:
haftmann@29708
   103
  "size empty = 0"
kuncar@49929
   104
  unfolding size_def by transfer simp
haftmann@29708
   105
haftmann@29708
   106
lemma size_update:
haftmann@37052
   107
  "finite (keys m) \<Longrightarrow> size (update k v m) =
haftmann@37052
   108
    (if k \<in> keys m then size m else Suc (size m))"
kuncar@49929
   109
  unfolding size_def by transfer (auto simp add: insert_dom)
haftmann@29708
   110
haftmann@29708
   111
lemma size_delete:
haftmann@37052
   112
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
kuncar@49929
   113
  unfolding size_def by transfer simp
haftmann@29708
   114
haftmann@37052
   115
lemma size_tabulate [simp]:
haftmann@29708
   116
  "size (tabulate ks f) = length (remdups ks)"
kuncar@49929
   117
  unfolding size_def by transfer (auto simp add: tabulate_alt_def card_set comp_def)
haftmann@29708
   118
haftmann@29831
   119
lemma bulkload_tabulate:
haftmann@29826
   120
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
kuncar@49929
   121
  by transfer (auto simp add: tabulate_alt_def)
haftmann@29826
   122
kuncar@49929
   123
lemma is_empty_empty [simp]:
haftmann@37052
   124
  "is_empty empty"
kuncar@49929
   125
  unfolding is_empty_def by transfer simp 
haftmann@37052
   126
haftmann@37052
   127
lemma is_empty_update [simp]:
haftmann@37052
   128
  "\<not> is_empty (update k v m)"
kuncar@49929
   129
  unfolding is_empty_def by transfer simp
haftmann@37052
   130
haftmann@37052
   131
lemma is_empty_delete:
haftmann@37052
   132
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
kuncar@49929
   133
  unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
haftmann@37052
   134
haftmann@37052
   135
lemma is_empty_replace [simp]:
haftmann@37052
   136
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
kuncar@49929
   137
  unfolding is_empty_def replace_def by transfer auto
haftmann@37052
   138
haftmann@37052
   139
lemma is_empty_default [simp]:
haftmann@37052
   140
  "\<not> is_empty (default k v m)"
kuncar@49929
   141
  unfolding is_empty_def default_def by transfer auto
haftmann@37052
   142
haftmann@37052
   143
lemma is_empty_map_entry [simp]:
haftmann@37052
   144
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
kuncar@49929
   145
  unfolding is_empty_def 
kuncar@49929
   146
  apply transfer by (case_tac "m k") auto
haftmann@37052
   147
haftmann@37052
   148
lemma is_empty_map_default [simp]:
haftmann@37052
   149
  "\<not> is_empty (map_default k v f m)"
haftmann@37052
   150
  by (simp add: map_default_def)
haftmann@37052
   151
haftmann@37052
   152
lemma keys_empty [simp]:
haftmann@37052
   153
  "keys empty = {}"
kuncar@49929
   154
  by transfer simp
haftmann@37052
   155
haftmann@37052
   156
lemma keys_update [simp]:
haftmann@37052
   157
  "keys (update k v m) = insert k (keys m)"
kuncar@49929
   158
  by transfer simp
haftmann@37052
   159
haftmann@37052
   160
lemma keys_delete [simp]:
haftmann@37052
   161
  "keys (delete k m) = keys m - {k}"
kuncar@49929
   162
  by transfer simp
haftmann@37052
   163
haftmann@37052
   164
lemma keys_replace [simp]:
haftmann@37052
   165
  "keys (replace k v m) = keys m"
kuncar@49929
   166
  unfolding replace_def by transfer (simp add: insert_absorb)
haftmann@37052
   167
haftmann@37052
   168
lemma keys_default [simp]:
haftmann@37052
   169
  "keys (default k v m) = insert k (keys m)"
kuncar@49929
   170
  unfolding default_def by transfer (simp add: insert_absorb)
haftmann@37052
   171
haftmann@37052
   172
lemma keys_map_entry [simp]:
haftmann@37052
   173
  "keys (map_entry k f m) = keys m"
kuncar@49929
   174
  apply transfer by (case_tac "m k") auto
haftmann@37052
   175
haftmann@37052
   176
lemma keys_map_default [simp]:
haftmann@37052
   177
  "keys (map_default k v f m) = insert k (keys m)"
haftmann@37052
   178
  by (simp add: map_default_def)
haftmann@37052
   179
haftmann@37052
   180
lemma keys_tabulate [simp]:
haftmann@37026
   181
  "keys (tabulate ks f) = set ks"
kuncar@49929
   182
  by transfer (simp add: map_of_map_restrict o_def)
haftmann@37026
   183
haftmann@37052
   184
lemma keys_bulkload [simp]:
haftmann@37026
   185
  "keys (bulkload xs) = {0..<length xs}"
haftmann@37026
   186
  by (simp add: keys_tabulate bulkload_tabulate)
haftmann@37026
   187
haftmann@37052
   188
lemma distinct_ordered_keys [simp]:
haftmann@37052
   189
  "distinct (ordered_keys m)"
haftmann@37052
   190
  by (simp add: ordered_keys_def)
haftmann@37052
   191
haftmann@37052
   192
lemma ordered_keys_infinite [simp]:
haftmann@37052
   193
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
haftmann@37052
   194
  by (simp add: ordered_keys_def)
haftmann@37052
   195
haftmann@37052
   196
lemma ordered_keys_empty [simp]:
haftmann@37052
   197
  "ordered_keys empty = []"
haftmann@37052
   198
  by (simp add: ordered_keys_def)
haftmann@37052
   199
haftmann@37052
   200
lemma ordered_keys_update [simp]:
haftmann@37052
   201
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
haftmann@37052
   202
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
haftmann@37052
   203
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
haftmann@37052
   204
haftmann@37052
   205
lemma ordered_keys_delete [simp]:
haftmann@37052
   206
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
haftmann@37052
   207
proof (cases "finite (keys m)")
haftmann@37052
   208
  case False then show ?thesis by simp
haftmann@37052
   209
next
haftmann@37052
   210
  case True note fin = True
haftmann@37052
   211
  show ?thesis
haftmann@37052
   212
  proof (cases "k \<in> keys m")
haftmann@37052
   213
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
haftmann@37052
   214
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
haftmann@37052
   215
  next
haftmann@37052
   216
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
haftmann@37052
   217
  qed
haftmann@37052
   218
qed
haftmann@37052
   219
haftmann@37052
   220
lemma ordered_keys_replace [simp]:
haftmann@37052
   221
  "ordered_keys (replace k v m) = ordered_keys m"
haftmann@37052
   222
  by (simp add: replace_def)
haftmann@37052
   223
haftmann@37052
   224
lemma ordered_keys_default [simp]:
haftmann@37052
   225
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
haftmann@37052
   226
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
haftmann@37052
   227
  by (simp_all add: default_def)
haftmann@37052
   228
haftmann@37052
   229
lemma ordered_keys_map_entry [simp]:
haftmann@37052
   230
  "ordered_keys (map_entry k f m) = ordered_keys m"
haftmann@37052
   231
  by (simp add: ordered_keys_def)
haftmann@37052
   232
haftmann@37052
   233
lemma ordered_keys_map_default [simp]:
haftmann@37052
   234
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
haftmann@37052
   235
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
haftmann@37052
   236
  by (simp_all add: map_default_def)
haftmann@37052
   237
haftmann@37052
   238
lemma ordered_keys_tabulate [simp]:
haftmann@37052
   239
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
haftmann@37052
   240
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
haftmann@37052
   241
haftmann@37052
   242
lemma ordered_keys_bulkload [simp]:
haftmann@37052
   243
  "ordered_keys (bulkload ks) = [0..<length ks]"
haftmann@37052
   244
  by (simp add: ordered_keys_def)
haftmann@36110
   245
haftmann@31459
   246
haftmann@37700
   247
subsection {* Code generator setup *}
haftmann@31459
   248
haftmann@37701
   249
code_datatype empty update
haftmann@37701
   250
haftmann@38857
   251
instantiation mapping :: (type, type) equal
haftmann@37700
   252
begin
haftmann@31459
   253
kuncar@49929
   254
lift_definition equal_mapping :: "('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping \<Rightarrow> bool" is "op=" .
haftmann@31459
   255
haftmann@37700
   256
instance proof
kuncar@49929
   257
qed(transfer, rule)
haftmann@31459
   258
haftmann@37700
   259
end
haftmann@31459
   260
haftmann@35157
   261
kuncar@49929
   262
hide_const (open) empty is_empty rep lookup update delete ordered_keys keys size
haftmann@40605
   263
  replace default map_entry map_default tabulate bulkload map
haftmann@35157
   264
haftmann@29708
   265
end