src/HOL/Library/Mapping.thy
author kuncar
Fri Mar 08 13:21:06 2013 +0100 (2013-03-08)
changeset 51375 d9e62d9c98de
parent 51161 6ed12ae3b3e1
child 51379 6dd83e007f56
permissions -rw-r--r--
patch Isabelle ditribution to conform to changes regarding the parametricity
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@51375
     8
imports Main Quotient_Option Quotient_List
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@51161
    37
haftmann@40605
    38
subsection {* Functorial structure *}
haftmann@40605
    39
haftmann@41505
    40
enriched_type map: map
kuncar@49929
    41
  by (transfer, auto simp add: fun_eq_iff Option.map.compositionality Option.map.id)+
haftmann@40605
    42
haftmann@51161
    43
haftmann@29708
    44
subsection {* Derived operations *}
haftmann@29708
    45
haftmann@35194
    46
definition ordered_keys :: "('a\<Colon>linorder, 'b) mapping \<Rightarrow> 'a list" where
haftmann@37052
    47
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
haftmann@35194
    48
haftmann@35157
    49
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool" where
haftmann@37052
    50
  "is_empty m \<longleftrightarrow> keys m = {}"
haftmann@35157
    51
haftmann@35157
    52
definition size :: "('a, 'b) mapping \<Rightarrow> nat" where
haftmann@37052
    53
  "size m = (if finite (keys m) then card (keys m) else 0)"
haftmann@35157
    54
haftmann@35157
    55
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37052
    56
  "replace k v m = (if k \<in> keys m then update k v m else m)"
haftmann@29814
    57
haftmann@37026
    58
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37052
    59
  "default k v m = (if k \<in> keys m then m else update k v m)"
haftmann@37026
    60
kuncar@49929
    61
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
kuncar@49929
    62
  "\<lambda>k f m. (case m k of None \<Rightarrow> m
kuncar@49929
    63
    | Some v \<Rightarrow> m (k \<mapsto> (f v)))" .
kuncar@49929
    64
kuncar@49929
    65
lemma map_entry_code [code]: "map_entry k f m = (case lookup m k of None \<Rightarrow> m
huffman@49975
    66
    | Some v \<Rightarrow> update k (f v) m)"
huffman@49975
    67
  by transfer rule
haftmann@37026
    68
haftmann@37026
    69
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" where
haftmann@37026
    70
  "map_default k v f m = map_entry k f (default k v m)" 
haftmann@37026
    71
haftmann@51161
    72
instantiation mapping :: (type, type) equal
haftmann@51161
    73
begin
haftmann@51161
    74
haftmann@51161
    75
definition
haftmann@51161
    76
  "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. lookup m1 k = lookup m2 k)"
haftmann@51161
    77
haftmann@51161
    78
instance proof
haftmann@51161
    79
qed (unfold equal_mapping_def, transfer, auto)
haftmann@51161
    80
haftmann@51161
    81
end
haftmann@51161
    82
haftmann@51161
    83
lemma [transfer_rule]:
kuncar@51375
    84
  "fun_rel (pcr_mapping op= op=) (fun_rel (pcr_mapping op= op=) HOL.iff) HOL.eq HOL.equal"
haftmann@51161
    85
  by (unfold equal) transfer_prover
haftmann@51161
    86
haftmann@51161
    87
haftmann@29708
    88
subsection {* Properties *}
haftmann@29708
    89
kuncar@49973
    90
lemma lookup_update: "lookup (update k v m) k = Some v" 
kuncar@49973
    91
  by transfer simp
kuncar@49973
    92
kuncar@49973
    93
lemma lookup_update_neq: "k \<noteq> k' \<Longrightarrow> lookup (update k v m) k' = lookup m k'" 
kuncar@49973
    94
  by transfer simp
kuncar@49973
    95
kuncar@49973
    96
lemma lookup_empty: "lookup empty k = None" 
kuncar@49973
    97
  by transfer simp
kuncar@49973
    98
kuncar@49929
    99
lemma keys_is_none_rep [code_unfold]:
haftmann@37052
   100
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
kuncar@49929
   101
  by transfer (auto simp add: is_none_def)
haftmann@29708
   102
kuncar@49929
   103
lemma tabulate_alt_def:
kuncar@49929
   104
  "map_of (List.map (\<lambda>k. (k, f k)) ks) = (Some o f) |` set ks"
kuncar@49929
   105
  by (induct ks) (auto simp add: tabulate_def restrict_map_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)"
kuncar@49929
   110
  by (transfer, 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"
kuncar@49929
   114
  by transfer 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)"
kuncar@49929
   119
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   120
haftmann@35157
   121
lemma delete_empty [simp]:
haftmann@35157
   122
  "delete k empty = empty"
kuncar@49929
   123
  by transfer 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"
kuncar@49929
   128
  by (transfer, auto simp add: replace_def fun_upd_twist)+
haftmann@29708
   129
haftmann@29708
   130
lemma size_empty [simp]:
haftmann@29708
   131
  "size empty = 0"
kuncar@49929
   132
  unfolding size_def by transfer simp
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))"
kuncar@49929
   137
  unfolding size_def by transfer (auto simp add: insert_dom)
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)"
kuncar@49929
   141
  unfolding size_def by transfer simp
haftmann@29708
   142
haftmann@37052
   143
lemma size_tabulate [simp]:
haftmann@29708
   144
  "size (tabulate ks f) = length (remdups ks)"
kuncar@49929
   145
  unfolding size_def by transfer (auto simp add: tabulate_alt_def card_set comp_def)
haftmann@29708
   146
haftmann@29831
   147
lemma bulkload_tabulate:
haftmann@29826
   148
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
kuncar@49929
   149
  by transfer (auto simp add: tabulate_alt_def)
haftmann@29826
   150
kuncar@49929
   151
lemma is_empty_empty [simp]:
haftmann@37052
   152
  "is_empty empty"
kuncar@49929
   153
  unfolding is_empty_def by transfer simp 
haftmann@37052
   154
haftmann@37052
   155
lemma is_empty_update [simp]:
haftmann@37052
   156
  "\<not> is_empty (update k v m)"
kuncar@49929
   157
  unfolding is_empty_def by transfer simp
haftmann@37052
   158
haftmann@37052
   159
lemma is_empty_delete:
haftmann@37052
   160
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
kuncar@49929
   161
  unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
haftmann@37052
   162
haftmann@37052
   163
lemma is_empty_replace [simp]:
haftmann@37052
   164
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
kuncar@49929
   165
  unfolding is_empty_def replace_def by transfer auto
haftmann@37052
   166
haftmann@37052
   167
lemma is_empty_default [simp]:
haftmann@37052
   168
  "\<not> is_empty (default k v m)"
kuncar@49929
   169
  unfolding is_empty_def default_def by transfer auto
haftmann@37052
   170
haftmann@37052
   171
lemma is_empty_map_entry [simp]:
haftmann@37052
   172
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
kuncar@49929
   173
  unfolding is_empty_def 
kuncar@49929
   174
  apply transfer by (case_tac "m k") auto
haftmann@37052
   175
haftmann@37052
   176
lemma is_empty_map_default [simp]:
haftmann@37052
   177
  "\<not> is_empty (map_default k v f m)"
haftmann@37052
   178
  by (simp add: map_default_def)
haftmann@37052
   179
haftmann@37052
   180
lemma keys_empty [simp]:
haftmann@37052
   181
  "keys empty = {}"
kuncar@49929
   182
  by transfer simp
haftmann@37052
   183
haftmann@37052
   184
lemma keys_update [simp]:
haftmann@37052
   185
  "keys (update k v m) = insert k (keys m)"
kuncar@49929
   186
  by transfer simp
haftmann@37052
   187
haftmann@37052
   188
lemma keys_delete [simp]:
haftmann@37052
   189
  "keys (delete k m) = keys m - {k}"
kuncar@49929
   190
  by transfer simp
haftmann@37052
   191
haftmann@37052
   192
lemma keys_replace [simp]:
haftmann@37052
   193
  "keys (replace k v m) = keys m"
kuncar@49929
   194
  unfolding replace_def by transfer (simp add: insert_absorb)
haftmann@37052
   195
haftmann@37052
   196
lemma keys_default [simp]:
haftmann@37052
   197
  "keys (default k v m) = insert k (keys m)"
kuncar@49929
   198
  unfolding default_def by transfer (simp add: insert_absorb)
haftmann@37052
   199
haftmann@37052
   200
lemma keys_map_entry [simp]:
haftmann@37052
   201
  "keys (map_entry k f m) = keys m"
kuncar@49929
   202
  apply transfer by (case_tac "m k") auto
haftmann@37052
   203
haftmann@37052
   204
lemma keys_map_default [simp]:
haftmann@37052
   205
  "keys (map_default k v f m) = insert k (keys m)"
haftmann@37052
   206
  by (simp add: map_default_def)
haftmann@37052
   207
haftmann@37052
   208
lemma keys_tabulate [simp]:
haftmann@37026
   209
  "keys (tabulate ks f) = set ks"
kuncar@49929
   210
  by transfer (simp add: map_of_map_restrict o_def)
haftmann@37026
   211
haftmann@37052
   212
lemma keys_bulkload [simp]:
haftmann@37026
   213
  "keys (bulkload xs) = {0..<length xs}"
haftmann@37026
   214
  by (simp add: keys_tabulate bulkload_tabulate)
haftmann@37026
   215
haftmann@37052
   216
lemma distinct_ordered_keys [simp]:
haftmann@37052
   217
  "distinct (ordered_keys m)"
haftmann@37052
   218
  by (simp add: ordered_keys_def)
haftmann@37052
   219
haftmann@37052
   220
lemma ordered_keys_infinite [simp]:
haftmann@37052
   221
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
haftmann@37052
   222
  by (simp add: ordered_keys_def)
haftmann@37052
   223
haftmann@37052
   224
lemma ordered_keys_empty [simp]:
haftmann@37052
   225
  "ordered_keys empty = []"
haftmann@37052
   226
  by (simp add: ordered_keys_def)
haftmann@37052
   227
haftmann@37052
   228
lemma ordered_keys_update [simp]:
haftmann@37052
   229
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
haftmann@37052
   230
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
haftmann@37052
   231
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
haftmann@37052
   232
haftmann@37052
   233
lemma ordered_keys_delete [simp]:
haftmann@37052
   234
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
haftmann@37052
   235
proof (cases "finite (keys m)")
haftmann@37052
   236
  case False then show ?thesis by simp
haftmann@37052
   237
next
haftmann@37052
   238
  case True note fin = True
haftmann@37052
   239
  show ?thesis
haftmann@37052
   240
  proof (cases "k \<in> keys m")
haftmann@37052
   241
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
haftmann@37052
   242
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
haftmann@37052
   243
  next
haftmann@37052
   244
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
haftmann@37052
   245
  qed
haftmann@37052
   246
qed
haftmann@37052
   247
haftmann@37052
   248
lemma ordered_keys_replace [simp]:
haftmann@37052
   249
  "ordered_keys (replace k v m) = ordered_keys m"
haftmann@37052
   250
  by (simp add: replace_def)
haftmann@37052
   251
haftmann@37052
   252
lemma ordered_keys_default [simp]:
haftmann@37052
   253
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
haftmann@37052
   254
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
haftmann@37052
   255
  by (simp_all add: default_def)
haftmann@37052
   256
haftmann@37052
   257
lemma ordered_keys_map_entry [simp]:
haftmann@37052
   258
  "ordered_keys (map_entry k f m) = ordered_keys m"
haftmann@37052
   259
  by (simp add: ordered_keys_def)
haftmann@37052
   260
haftmann@37052
   261
lemma ordered_keys_map_default [simp]:
haftmann@37052
   262
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
haftmann@37052
   263
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
haftmann@37052
   264
  by (simp_all add: map_default_def)
haftmann@37052
   265
haftmann@37052
   266
lemma ordered_keys_tabulate [simp]:
haftmann@37052
   267
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
haftmann@37052
   268
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
haftmann@37052
   269
haftmann@37052
   270
lemma ordered_keys_bulkload [simp]:
haftmann@37052
   271
  "ordered_keys (bulkload ks) = [0..<length ks]"
haftmann@37052
   272
  by (simp add: ordered_keys_def)
haftmann@36110
   273
haftmann@31459
   274
haftmann@37700
   275
subsection {* Code generator setup *}
haftmann@31459
   276
haftmann@37701
   277
code_datatype empty update
haftmann@37701
   278
kuncar@49929
   279
hide_const (open) empty is_empty rep lookup update delete ordered_keys keys size
haftmann@40605
   280
  replace default map_entry map_default tabulate bulkload map
haftmann@35157
   281
huffman@49975
   282
end
haftmann@51161
   283