src/HOL/Library/Mapping.thy
author eberlm
Tue May 31 13:02:44 2016 +0200 (2016-05-31)
changeset 63194 0b7bdb75f451
parent 61585 a9599d3d7610
child 63195 f3f08c0d4aaf
permissions -rw-r--r--
Added code generation for PMFs
kuncar@49929
     1
(*  Title:      HOL/Library/Mapping.thy
kuncar@49929
     2
    Author:     Florian Haftmann and Ondrej Kuncar
kuncar@49929
     3
*)
haftmann@29708
     4
wenzelm@60500
     5
section \<open>An abstract view on maps for code generation.\<close>
haftmann@29708
     6
haftmann@29708
     7
theory Mapping
kuncar@53013
     8
imports Main
haftmann@29708
     9
begin
haftmann@29708
    10
wenzelm@60500
    11
subsection \<open>Parametricity transfer rules\<close>
kuncar@51379
    12
wenzelm@61585
    13
lemma map_of_foldr: \<comment> \<open>FIXME move\<close>
haftmann@56529
    14
  "map_of xs = foldr (\<lambda>(k, v) m. m(k \<mapsto> v)) xs Map.empty"
haftmann@56529
    15
  using map_add_map_of_foldr [of Map.empty] by auto
haftmann@56529
    16
kuncar@53013
    17
context
kuncar@53013
    18
begin
haftmann@56528
    19
kuncar@53013
    20
interpretation lifting_syntax .
kuncar@53013
    21
haftmann@56529
    22
lemma empty_parametric:
haftmann@56528
    23
  "(A ===> rel_option B) Map.empty Map.empty"
haftmann@56528
    24
  by transfer_prover
kuncar@51379
    25
haftmann@56529
    26
lemma lookup_parametric: "((A ===> B) ===> A ===> B) (\<lambda>m k. m k) (\<lambda>m k. m k)"
haftmann@56528
    27
  by transfer_prover
kuncar@51379
    28
haftmann@56529
    29
lemma update_parametric:
kuncar@51379
    30
  assumes [transfer_rule]: "bi_unique A"
haftmann@56528
    31
  shows "(A ===> B ===> (A ===> rel_option B) ===> A ===> rel_option B)
haftmann@56528
    32
    (\<lambda>k v m. m(k \<mapsto> v)) (\<lambda>k v m. m(k \<mapsto> v))"
haftmann@56528
    33
  by transfer_prover
kuncar@51379
    34
haftmann@56529
    35
lemma delete_parametric:
kuncar@51379
    36
  assumes [transfer_rule]: "bi_unique A"
blanchet@55525
    37
  shows "(A ===> (A ===> rel_option B) ===> A ===> rel_option B) 
haftmann@56528
    38
    (\<lambda>k m. m(k := None)) (\<lambda>k m. m(k := None))"
haftmann@56528
    39
  by transfer_prover
kuncar@51379
    40
haftmann@56528
    41
lemma is_none_parametric [transfer_rule]:
haftmann@56528
    42
  "(rel_option A ===> HOL.eq) Option.is_none Option.is_none"
wenzelm@61068
    43
  by (auto simp add: Option.is_none_def rel_fun_def rel_option_iff split: option.split)
kuncar@51379
    44
haftmann@56529
    45
lemma dom_parametric:
kuncar@51379
    46
  assumes [transfer_rule]: "bi_total A"
blanchet@55938
    47
  shows "((A ===> rel_option B) ===> rel_set A) dom dom" 
wenzelm@61068
    48
  unfolding dom_def [abs_def] Option.is_none_def [symmetric] by transfer_prover
kuncar@51379
    49
haftmann@56529
    50
lemma map_of_parametric [transfer_rule]:
kuncar@51379
    51
  assumes [transfer_rule]: "bi_unique R1"
blanchet@55944
    52
  shows "(list_all2 (rel_prod R1 R2) ===> R1 ===> rel_option R2) map_of map_of"
haftmann@56528
    53
  unfolding map_of_def by transfer_prover
kuncar@51379
    54
haftmann@56529
    55
lemma map_entry_parametric [transfer_rule]:
haftmann@56529
    56
  assumes [transfer_rule]: "bi_unique A"
haftmann@56529
    57
  shows "(A ===> (B ===> B) ===> (A ===> rel_option B) ===> A ===> rel_option B) 
haftmann@56529
    58
    (\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
    59
      | Some v \<Rightarrow> m (k \<mapsto> (f v)))) (\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
    60
      | Some v \<Rightarrow> m (k \<mapsto> (f v))))"
haftmann@56529
    61
  by transfer_prover
haftmann@56529
    62
haftmann@56529
    63
lemma tabulate_parametric: 
kuncar@51379
    64
  assumes [transfer_rule]: "bi_unique A"
blanchet@55525
    65
  shows "(list_all2 A ===> (A ===> B) ===> A ===> rel_option B) 
haftmann@56528
    66
    (\<lambda>ks f. (map_of (map (\<lambda>k. (k, f k)) ks))) (\<lambda>ks f. (map_of (map (\<lambda>k. (k, f k)) ks)))"
haftmann@56528
    67
  by transfer_prover
kuncar@51379
    68
haftmann@56529
    69
lemma bulkload_parametric: 
haftmann@56528
    70
  "(list_all2 A ===> HOL.eq ===> rel_option A) 
kuncar@51379
    71
    (\<lambda>xs k. if k < length xs then Some (xs ! k) else None) (\<lambda>xs k. if k < length xs then Some (xs ! k) else None)"
haftmann@56528
    72
proof
haftmann@56528
    73
  fix xs ys
haftmann@56528
    74
  assume "list_all2 A xs ys"
haftmann@56528
    75
  then show "(HOL.eq ===> rel_option A)
haftmann@56528
    76
    (\<lambda>k. if k < length xs then Some (xs ! k) else None)
haftmann@56528
    77
    (\<lambda>k. if k < length ys then Some (ys ! k) else None)"
haftmann@56528
    78
    apply induct
haftmann@56528
    79
    apply auto
haftmann@56528
    80
    unfolding rel_fun_def
haftmann@56528
    81
    apply clarsimp 
haftmann@56528
    82
    apply (case_tac xa) 
haftmann@56528
    83
    apply (auto dest: list_all2_lengthD list_all2_nthD)
haftmann@56528
    84
    done
haftmann@56528
    85
qed
kuncar@51379
    86
haftmann@56529
    87
lemma map_parametric: 
blanchet@55525
    88
  "((A ===> B) ===> (C ===> D) ===> (B ===> rel_option C) ===> A ===> rel_option D) 
haftmann@56528
    89
     (\<lambda>f g m. (map_option g \<circ> m \<circ> f)) (\<lambda>f g m. (map_option g \<circ> m \<circ> f))"
haftmann@56528
    90
  by transfer_prover
eberlm@63194
    91
  
eberlm@63194
    92
lemma combine_with_key_parametric: 
eberlm@63194
    93
  shows "((A ===> B ===> B ===> B) ===> (A ===> rel_option B) ===> (A ===> rel_option B) ===>
eberlm@63194
    94
           (A ===> rel_option B)) (\<lambda>f m1 m2 x. combine_options (f x) (m1 x) (m2 x))
eberlm@63194
    95
           (\<lambda>f m1 m2 x. combine_options (f x) (m1 x) (m2 x))"
eberlm@63194
    96
  unfolding combine_options_def by transfer_prover
eberlm@63194
    97
  
eberlm@63194
    98
lemma combine_parametric: 
eberlm@63194
    99
  shows "((B ===> B ===> B) ===> (A ===> rel_option B) ===> (A ===> rel_option B) ===>
eberlm@63194
   100
           (A ===> rel_option B)) (\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x))
eberlm@63194
   101
           (\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x))"
eberlm@63194
   102
  unfolding combine_options_def by transfer_prover
kuncar@51379
   103
haftmann@56529
   104
end
kuncar@51379
   105
kuncar@53013
   106
wenzelm@60500
   107
subsection \<open>Type definition and primitive operations\<close>
haftmann@29708
   108
wenzelm@49834
   109
typedef ('a, 'b) mapping = "UNIV :: ('a \<rightharpoonup> 'b) set"
haftmann@56528
   110
  morphisms rep Mapping
haftmann@56528
   111
  ..
haftmann@37700
   112
haftmann@59485
   113
setup_lifting type_definition_mapping
haftmann@37700
   114
haftmann@56528
   115
lift_definition empty :: "('a, 'b) mapping"
haftmann@56529
   116
  is Map.empty parametric empty_parametric .
kuncar@49929
   117
haftmann@56528
   118
lift_definition lookup :: "('a, 'b) mapping \<Rightarrow> 'a \<Rightarrow> 'b option"
haftmann@56529
   119
  is "\<lambda>m k. m k" parametric lookup_parametric .
haftmann@56528
   120
eberlm@63194
   121
definition "lookup_default d m k = (case Mapping.lookup m k of None \<Rightarrow> d | Some v \<Rightarrow> v)"
eberlm@63194
   122
haftmann@59485
   123
declare [[code drop: Mapping.lookup]]
wenzelm@61585
   124
setup \<open>Code.add_default_eqn @{thm Mapping.lookup.abs_eq}\<close> \<comment> \<open>FIXME lifting\<close>
haftmann@59485
   125
haftmann@56528
   126
lift_definition update :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   127
  is "\<lambda>k v m. m(k \<mapsto> v)" parametric update_parametric .
haftmann@37700
   128
haftmann@56528
   129
lift_definition delete :: "'a \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   130
  is "\<lambda>k m. m(k := None)" parametric delete_parametric .
haftmann@39380
   131
eberlm@63194
   132
lift_definition filter :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
eberlm@63194
   133
  is "\<lambda>P m k. case m k of None \<Rightarrow> None | Some v \<Rightarrow> if P k v then Some v else None" . 
eberlm@63194
   134
haftmann@56528
   135
lift_definition keys :: "('a, 'b) mapping \<Rightarrow> 'a set"
haftmann@56529
   136
  is dom parametric dom_parametric .
haftmann@29708
   137
haftmann@56528
   138
lift_definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping"
haftmann@56529
   139
  is "\<lambda>ks f. (map_of (List.map (\<lambda>k. (k, f k)) ks))" parametric tabulate_parametric .
haftmann@29708
   140
haftmann@56528
   141
lift_definition bulkload :: "'a list \<Rightarrow> (nat, 'a) mapping"
haftmann@56529
   142
  is "\<lambda>xs k. if k < length xs then Some (xs ! k) else None" parametric bulkload_parametric .
haftmann@29708
   143
haftmann@56528
   144
lift_definition map :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('c, 'd) mapping"
haftmann@56529
   145
  is "\<lambda>f g m. (map_option g \<circ> m \<circ> f)" parametric map_parametric .
eberlm@63194
   146
  
eberlm@63194
   147
lift_definition map_values :: "('c \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> ('c, 'a) mapping \<Rightarrow> ('c, 'b) mapping"
eberlm@63194
   148
  is "\<lambda>f m x. map_option (f x) (m x)" . 
eberlm@63194
   149
eberlm@63194
   150
lift_definition combine_with_key :: 
eberlm@63194
   151
  "('a \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping"
eberlm@63194
   152
  is "\<lambda>f m1 m2 x. combine_options (f x) (m1 x) (m2 x)" parametric combine_with_key_parametric .
eberlm@63194
   153
eberlm@63194
   154
lift_definition combine :: 
eberlm@63194
   155
  "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping \<Rightarrow> ('a,'b) mapping"
eberlm@63194
   156
  is "\<lambda>f m1 m2 x. combine_options f (m1 x) (m2 x)" parametric combine_parametric .
eberlm@63194
   157
eberlm@63194
   158
definition All_mapping where
eberlm@63194
   159
  "All_mapping m P \<longleftrightarrow> (\<forall>x. case Mapping.lookup m x of None \<Rightarrow> True | Some y \<Rightarrow> P x y)"
haftmann@29708
   160
haftmann@59485
   161
declare [[code drop: map]]
haftmann@59485
   162
haftmann@51161
   163
wenzelm@60500
   164
subsection \<open>Functorial structure\<close>
haftmann@40605
   165
blanchet@55467
   166
functor map: map
blanchet@55466
   167
  by (transfer, auto simp add: fun_eq_iff option.map_comp option.map_id)+
haftmann@40605
   168
haftmann@51161
   169
wenzelm@60500
   170
subsection \<open>Derived operations\<close>
haftmann@29708
   171
wenzelm@61076
   172
definition ordered_keys :: "('a::linorder, 'b) mapping \<Rightarrow> 'a list"
haftmann@56528
   173
where
haftmann@37052
   174
  "ordered_keys m = (if finite (keys m) then sorted_list_of_set (keys m) else [])"
haftmann@35194
   175
haftmann@56528
   176
definition is_empty :: "('a, 'b) mapping \<Rightarrow> bool"
haftmann@56528
   177
where
haftmann@37052
   178
  "is_empty m \<longleftrightarrow> keys m = {}"
haftmann@35157
   179
haftmann@56528
   180
definition size :: "('a, 'b) mapping \<Rightarrow> nat"
haftmann@56528
   181
where
haftmann@37052
   182
  "size m = (if finite (keys m) then card (keys m) else 0)"
haftmann@35157
   183
haftmann@56528
   184
definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   185
where
haftmann@37052
   186
  "replace k v m = (if k \<in> keys m then update k v m else m)"
haftmann@29814
   187
haftmann@56528
   188
definition default :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   189
where
haftmann@37052
   190
  "default k v m = (if k \<in> keys m then m else update k v m)"
haftmann@37026
   191
wenzelm@60500
   192
text \<open>Manual derivation of transfer rule is non-trivial\<close>
haftmann@56529
   193
kuncar@49929
   194
lift_definition map_entry :: "'a \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping" is
kuncar@49929
   195
  "\<lambda>k f m. (case m k of None \<Rightarrow> m
haftmann@56529
   196
    | Some v \<Rightarrow> m (k \<mapsto> (f v)))" parametric map_entry_parametric .
kuncar@49929
   197
haftmann@56529
   198
lemma map_entry_code [code]:
haftmann@56529
   199
  "map_entry k f m = (case lookup m k of None \<Rightarrow> m
huffman@49975
   200
    | Some v \<Rightarrow> update k (f v) m)"
huffman@49975
   201
  by transfer rule
haftmann@37026
   202
haftmann@56528
   203
definition map_default :: "'a \<Rightarrow> 'b \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) mapping \<Rightarrow> ('a, 'b) mapping"
haftmann@56528
   204
where
haftmann@37026
   205
  "map_default k v f m = map_entry k f (default k v m)" 
haftmann@37026
   206
haftmann@56529
   207
definition of_alist :: "('k \<times> 'v) list \<Rightarrow> ('k, 'v) mapping"
haftmann@56529
   208
where
haftmann@54853
   209
  "of_alist xs = foldr (\<lambda>(k, v) m. update k v m) xs empty"
kuncar@51379
   210
haftmann@51161
   211
instantiation mapping :: (type, type) equal
haftmann@51161
   212
begin
haftmann@51161
   213
haftmann@51161
   214
definition
haftmann@51161
   215
  "HOL.equal m1 m2 \<longleftrightarrow> (\<forall>k. lookup m1 k = lookup m2 k)"
haftmann@51161
   216
wenzelm@60679
   217
instance
wenzelm@60679
   218
  by standard (unfold equal_mapping_def, transfer, auto)
haftmann@51161
   219
haftmann@51161
   220
end
haftmann@51161
   221
kuncar@53013
   222
context
kuncar@53013
   223
begin
haftmann@56528
   224
kuncar@53013
   225
interpretation lifting_syntax .
kuncar@53013
   226
haftmann@51161
   227
lemma [transfer_rule]:
kuncar@51379
   228
  assumes [transfer_rule]: "bi_total A"
kuncar@51379
   229
  assumes [transfer_rule]: "bi_unique B"
haftmann@56528
   230
  shows "(pcr_mapping A B ===> pcr_mapping A B ===> op=) HOL.eq HOL.equal"
haftmann@56528
   231
  by (unfold equal) transfer_prover
haftmann@51161
   232
haftmann@56529
   233
lemma of_alist_transfer [transfer_rule]:
haftmann@56529
   234
  assumes [transfer_rule]: "bi_unique R1"
haftmann@56529
   235
  shows "(list_all2 (rel_prod R1 R2) ===> pcr_mapping R1 R2) map_of of_alist"
haftmann@56529
   236
  unfolding of_alist_def [abs_def] map_of_foldr [abs_def] by transfer_prover
haftmann@56529
   237
kuncar@53013
   238
end
haftmann@51161
   239
haftmann@56528
   240
wenzelm@60500
   241
subsection \<open>Properties\<close>
haftmann@29708
   242
haftmann@56528
   243
lemma lookup_update:
haftmann@56528
   244
  "lookup (update k v m) k = Some v" 
kuncar@49973
   245
  by transfer simp
kuncar@49973
   246
haftmann@56528
   247
lemma lookup_update_neq:
haftmann@56528
   248
  "k \<noteq> k' \<Longrightarrow> lookup (update k v m) k' = lookup m k'" 
kuncar@49973
   249
  by transfer simp
kuncar@49973
   250
eberlm@63194
   251
lemma lookup_update': 
eberlm@63194
   252
  "Mapping.lookup (update k v m) k' = (if k = k' then Some v else lookup m k')"
eberlm@63194
   253
  by (auto simp: lookup_update lookup_update_neq)
eberlm@63194
   254
haftmann@56528
   255
lemma lookup_empty:
haftmann@56528
   256
  "lookup empty k = None" 
kuncar@49973
   257
  by transfer simp
kuncar@49973
   258
eberlm@63194
   259
lemma lookup_filter:
eberlm@63194
   260
  "lookup (filter P m) k = 
eberlm@63194
   261
     (case lookup m k of None \<Rightarrow> None | Some v \<Rightarrow> if P k v then Some v else None)"
eberlm@63194
   262
  by transfer simp_all
eberlm@63194
   263
eberlm@63194
   264
lemma lookup_map_values:
eberlm@63194
   265
  "lookup (map_values f m) k = map_option (f k) (lookup m k)"
eberlm@63194
   266
  by transfer simp_all
eberlm@63194
   267
eberlm@63194
   268
lemma lookup_default_empty: "lookup_default d empty k = d"
eberlm@63194
   269
  by (simp add: lookup_default_def lookup_empty)
eberlm@63194
   270
eberlm@63194
   271
lemma lookup_default_update:
eberlm@63194
   272
  "lookup_default d (update k v m) k = v" 
eberlm@63194
   273
  by (simp add: lookup_default_def lookup_update)
eberlm@63194
   274
eberlm@63194
   275
lemma lookup_default_update_neq:
eberlm@63194
   276
  "k \<noteq> k' \<Longrightarrow> lookup_default d (update k v m) k' = lookup_default d m k'" 
eberlm@63194
   277
  by (simp add: lookup_default_def lookup_update_neq)
eberlm@63194
   278
eberlm@63194
   279
lemma lookup_default_update': 
eberlm@63194
   280
  "lookup_default d (update k v m) k' = (if k = k' then v else lookup_default d m k')"
eberlm@63194
   281
  by (auto simp: lookup_default_update lookup_default_update_neq)
eberlm@63194
   282
eberlm@63194
   283
lemma lookup_default_filter:
eberlm@63194
   284
  "lookup_default d (filter P m) k =  
eberlm@63194
   285
     (if P k (lookup_default d m k) then lookup_default d m k else d)"
eberlm@63194
   286
  by (simp add: lookup_default_def lookup_filter split: option.splits)
eberlm@63194
   287
eberlm@63194
   288
lemma lookup_default_map_values:
eberlm@63194
   289
  "lookup_default (f k d) (map_values f m) k = f k (lookup_default d m k)"
eberlm@63194
   290
  by (simp add: lookup_default_def lookup_map_values split: option.splits)  
eberlm@63194
   291
eberlm@63194
   292
lemma lookup_combine_with_key:
eberlm@63194
   293
  "Mapping.lookup (combine_with_key f m1 m2) x = 
eberlm@63194
   294
     combine_options (f x) (Mapping.lookup m1 x) (Mapping.lookup m2 x)"
eberlm@63194
   295
  by transfer (auto split: option.splits)
eberlm@63194
   296
  
eberlm@63194
   297
lemma combine_altdef: "combine f m1 m2 = combine_with_key (\<lambda>_. f) m1 m2"
eberlm@63194
   298
  by transfer' (rule refl)
eberlm@63194
   299
eberlm@63194
   300
lemma lookup_combine:
eberlm@63194
   301
  "Mapping.lookup (combine f m1 m2) x = 
eberlm@63194
   302
     combine_options f (Mapping.lookup m1 x) (Mapping.lookup m2 x)"
eberlm@63194
   303
  by transfer (auto split: option.splits)
eberlm@63194
   304
  
eberlm@63194
   305
lemma lookup_default_neutral_combine_with_key: 
eberlm@63194
   306
  assumes "\<And>x. f k d x = x" "\<And>x. f k x d = x"
eberlm@63194
   307
  shows   "Mapping.lookup_default d (combine_with_key f m1 m2) k = 
eberlm@63194
   308
             f k (Mapping.lookup_default d m1 k) (Mapping.lookup_default d m2 k)"
eberlm@63194
   309
  by (auto simp: lookup_default_def lookup_combine_with_key assms split: option.splits)
eberlm@63194
   310
  
eberlm@63194
   311
lemma lookup_default_neutral_combine: 
eberlm@63194
   312
  assumes "\<And>x. f d x = x" "\<And>x. f x d = x"
eberlm@63194
   313
  shows   "Mapping.lookup_default d (combine f m1 m2) x = 
eberlm@63194
   314
             f (Mapping.lookup_default d m1 x) (Mapping.lookup_default d m2 x)"
eberlm@63194
   315
  by (auto simp: lookup_default_def lookup_combine assms split: option.splits)
eberlm@63194
   316
eberlm@63194
   317
lemma lookup_tabulate: 
eberlm@63194
   318
  assumes "distinct xs"
eberlm@63194
   319
  shows   "Mapping.lookup (Mapping.tabulate xs f) x = (if x \<in> set xs then Some (f x) else None)"
eberlm@63194
   320
  using assms by transfer (auto simp: map_of_eq_None_iff o_def dest!: map_of_SomeD)
eberlm@63194
   321
eberlm@63194
   322
lemma lookup_of_alist: "Mapping.lookup (Mapping.of_alist xs) k = map_of xs k"
eberlm@63194
   323
  by transfer simp_all
eberlm@63194
   324
kuncar@49929
   325
lemma keys_is_none_rep [code_unfold]:
haftmann@37052
   326
  "k \<in> keys m \<longleftrightarrow> \<not> (Option.is_none (lookup m k))"
wenzelm@61068
   327
  by transfer (auto simp add: Option.is_none_def)
haftmann@29708
   328
haftmann@29708
   329
lemma update_update:
haftmann@29708
   330
  "update k v (update k w m) = update k v m"
haftmann@29708
   331
  "k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
kuncar@49929
   332
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   333
haftmann@35157
   334
lemma update_delete [simp]:
haftmann@35157
   335
  "update k v (delete k m) = update k v m"
kuncar@49929
   336
  by transfer simp
haftmann@29708
   337
haftmann@29708
   338
lemma delete_update:
haftmann@29708
   339
  "delete k (update k v m) = delete k m"
haftmann@29708
   340
  "k \<noteq> l \<Longrightarrow> delete k (update l v m) = update l v (delete k m)"
kuncar@49929
   341
  by (transfer, simp add: fun_upd_twist)+
haftmann@29708
   342
haftmann@35157
   343
lemma delete_empty [simp]:
haftmann@35157
   344
  "delete k empty = empty"
kuncar@49929
   345
  by transfer simp
haftmann@29708
   346
haftmann@35157
   347
lemma replace_update:
haftmann@37052
   348
  "k \<notin> keys m \<Longrightarrow> replace k v m = m"
haftmann@37052
   349
  "k \<in> keys m \<Longrightarrow> replace k v m = update k v m"
kuncar@49929
   350
  by (transfer, auto simp add: replace_def fun_upd_twist)+
eberlm@63194
   351
  
eberlm@63194
   352
lemma map_values_update: "map_values f (update k v m) = update k (f k v) (map_values f m)"
eberlm@63194
   353
  by transfer (simp_all add: fun_eq_iff)
eberlm@63194
   354
  
eberlm@63194
   355
lemma size_mono:
eberlm@63194
   356
  "finite (keys m') \<Longrightarrow> keys m \<subseteq> keys m' \<Longrightarrow> size m \<le> size m'"
eberlm@63194
   357
  unfolding size_def by (auto intro: card_mono)
haftmann@29708
   358
haftmann@29708
   359
lemma size_empty [simp]:
haftmann@29708
   360
  "size empty = 0"
kuncar@49929
   361
  unfolding size_def by transfer simp
haftmann@29708
   362
haftmann@29708
   363
lemma size_update:
haftmann@37052
   364
  "finite (keys m) \<Longrightarrow> size (update k v m) =
haftmann@37052
   365
    (if k \<in> keys m then size m else Suc (size m))"
kuncar@49929
   366
  unfolding size_def by transfer (auto simp add: insert_dom)
haftmann@29708
   367
haftmann@29708
   368
lemma size_delete:
haftmann@37052
   369
  "size (delete k m) = (if k \<in> keys m then size m - 1 else size m)"
kuncar@49929
   370
  unfolding size_def by transfer simp
haftmann@29708
   371
haftmann@37052
   372
lemma size_tabulate [simp]:
haftmann@29708
   373
  "size (tabulate ks f) = length (remdups ks)"
haftmann@56528
   374
  unfolding size_def by transfer (auto simp add: map_of_map_restrict  card_set comp_def)
haftmann@29708
   375
eberlm@63194
   376
lemma keys_filter: "keys (filter P m) \<subseteq> keys m"
eberlm@63194
   377
  by transfer (auto split: option.splits)
eberlm@63194
   378
eberlm@63194
   379
lemma size_filter: "finite (keys m) \<Longrightarrow> size (filter P m) \<le> size m"
eberlm@63194
   380
  by (intro size_mono keys_filter)
eberlm@63194
   381
eberlm@63194
   382
haftmann@29831
   383
lemma bulkload_tabulate:
haftmann@29826
   384
  "bulkload xs = tabulate [0..<length xs] (nth xs)"
haftmann@56528
   385
  by transfer (auto simp add: map_of_map_restrict)
haftmann@29826
   386
kuncar@49929
   387
lemma is_empty_empty [simp]:
haftmann@37052
   388
  "is_empty empty"
kuncar@49929
   389
  unfolding is_empty_def by transfer simp 
haftmann@37052
   390
haftmann@37052
   391
lemma is_empty_update [simp]:
haftmann@37052
   392
  "\<not> is_empty (update k v m)"
kuncar@49929
   393
  unfolding is_empty_def by transfer simp
haftmann@37052
   394
haftmann@37052
   395
lemma is_empty_delete:
haftmann@37052
   396
  "is_empty (delete k m) \<longleftrightarrow> is_empty m \<or> keys m = {k}"
kuncar@49929
   397
  unfolding is_empty_def by transfer (auto simp del: dom_eq_empty_conv)
haftmann@37052
   398
haftmann@37052
   399
lemma is_empty_replace [simp]:
haftmann@37052
   400
  "is_empty (replace k v m) \<longleftrightarrow> is_empty m"
kuncar@49929
   401
  unfolding is_empty_def replace_def by transfer auto
haftmann@37052
   402
haftmann@37052
   403
lemma is_empty_default [simp]:
haftmann@37052
   404
  "\<not> is_empty (default k v m)"
kuncar@49929
   405
  unfolding is_empty_def default_def by transfer auto
haftmann@37052
   406
haftmann@37052
   407
lemma is_empty_map_entry [simp]:
haftmann@37052
   408
  "is_empty (map_entry k f m) \<longleftrightarrow> is_empty m"
haftmann@56528
   409
  unfolding is_empty_def by transfer (auto split: option.split)
haftmann@37052
   410
eberlm@63194
   411
lemma is_empty_map_values [simp]:
eberlm@63194
   412
  "is_empty (map_values f m) \<longleftrightarrow> is_empty m"
eberlm@63194
   413
  unfolding is_empty_def by transfer (auto simp: fun_eq_iff)
eberlm@63194
   414
haftmann@37052
   415
lemma is_empty_map_default [simp]:
haftmann@37052
   416
  "\<not> is_empty (map_default k v f m)"
haftmann@37052
   417
  by (simp add: map_default_def)
haftmann@37052
   418
haftmann@56545
   419
lemma keys_dom_lookup:
haftmann@56545
   420
  "keys m = dom (Mapping.lookup m)"
haftmann@56545
   421
  by transfer rule
haftmann@56545
   422
haftmann@37052
   423
lemma keys_empty [simp]:
haftmann@37052
   424
  "keys empty = {}"
kuncar@49929
   425
  by transfer simp
haftmann@37052
   426
haftmann@37052
   427
lemma keys_update [simp]:
haftmann@37052
   428
  "keys (update k v m) = insert k (keys m)"
kuncar@49929
   429
  by transfer simp
haftmann@37052
   430
haftmann@37052
   431
lemma keys_delete [simp]:
haftmann@37052
   432
  "keys (delete k m) = keys m - {k}"
kuncar@49929
   433
  by transfer simp
haftmann@37052
   434
haftmann@37052
   435
lemma keys_replace [simp]:
haftmann@37052
   436
  "keys (replace k v m) = keys m"
kuncar@49929
   437
  unfolding replace_def by transfer (simp add: insert_absorb)
haftmann@37052
   438
haftmann@37052
   439
lemma keys_default [simp]:
haftmann@37052
   440
  "keys (default k v m) = insert k (keys m)"
kuncar@49929
   441
  unfolding default_def by transfer (simp add: insert_absorb)
haftmann@37052
   442
haftmann@37052
   443
lemma keys_map_entry [simp]:
haftmann@37052
   444
  "keys (map_entry k f m) = keys m"
haftmann@56528
   445
  by transfer (auto split: option.split)
haftmann@37052
   446
haftmann@37052
   447
lemma keys_map_default [simp]:
haftmann@37052
   448
  "keys (map_default k v f m) = insert k (keys m)"
haftmann@37052
   449
  by (simp add: map_default_def)
haftmann@37052
   450
eberlm@63194
   451
lemma keys_map_values [simp]:
eberlm@63194
   452
  "keys (map_values f m) = keys m"
eberlm@63194
   453
  by transfer (simp_all add: dom_def)
eberlm@63194
   454
eberlm@63194
   455
lemma keys_combine_with_key [simp]: 
eberlm@63194
   456
  "Mapping.keys (combine_with_key f m1 m2) = Mapping.keys m1 \<union> Mapping.keys m2"
eberlm@63194
   457
  by transfer (auto simp: dom_def combine_options_def split: option.splits)  
eberlm@63194
   458
eberlm@63194
   459
lemma keys_combine [simp]: "Mapping.keys (combine f m1 m2) = Mapping.keys m1 \<union> Mapping.keys m2"
eberlm@63194
   460
  by (simp add: combine_altdef)
eberlm@63194
   461
haftmann@37052
   462
lemma keys_tabulate [simp]:
haftmann@37026
   463
  "keys (tabulate ks f) = set ks"
kuncar@49929
   464
  by transfer (simp add: map_of_map_restrict o_def)
haftmann@37026
   465
eberlm@63194
   466
lemma keys_of_alist [simp]: "keys (of_alist xs) = set (List.map fst xs)"
eberlm@63194
   467
  by transfer (simp_all add: dom_map_of_conv_image_fst)
eberlm@63194
   468
haftmann@37052
   469
lemma keys_bulkload [simp]:
haftmann@37026
   470
  "keys (bulkload xs) = {0..<length xs}"
haftmann@56528
   471
  by (simp add: bulkload_tabulate)
haftmann@37026
   472
haftmann@37052
   473
lemma distinct_ordered_keys [simp]:
haftmann@37052
   474
  "distinct (ordered_keys m)"
haftmann@37052
   475
  by (simp add: ordered_keys_def)
haftmann@37052
   476
haftmann@37052
   477
lemma ordered_keys_infinite [simp]:
haftmann@37052
   478
  "\<not> finite (keys m) \<Longrightarrow> ordered_keys m = []"
haftmann@37052
   479
  by (simp add: ordered_keys_def)
haftmann@37052
   480
haftmann@37052
   481
lemma ordered_keys_empty [simp]:
haftmann@37052
   482
  "ordered_keys empty = []"
haftmann@37052
   483
  by (simp add: ordered_keys_def)
haftmann@37052
   484
haftmann@37052
   485
lemma ordered_keys_update [simp]:
haftmann@37052
   486
  "k \<in> keys m \<Longrightarrow> ordered_keys (update k v m) = ordered_keys m"
haftmann@37052
   487
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (update k v m) = insort k (ordered_keys m)"
haftmann@37052
   488
  by (simp_all add: ordered_keys_def) (auto simp only: sorted_list_of_set_insert [symmetric] insert_absorb)
haftmann@37052
   489
haftmann@37052
   490
lemma ordered_keys_delete [simp]:
haftmann@37052
   491
  "ordered_keys (delete k m) = remove1 k (ordered_keys m)"
haftmann@37052
   492
proof (cases "finite (keys m)")
haftmann@37052
   493
  case False then show ?thesis by simp
haftmann@37052
   494
next
haftmann@37052
   495
  case True note fin = True
haftmann@37052
   496
  show ?thesis
haftmann@37052
   497
  proof (cases "k \<in> keys m")
haftmann@37052
   498
    case False with fin have "k \<notin> set (sorted_list_of_set (keys m))" by simp
haftmann@37052
   499
    with False show ?thesis by (simp add: ordered_keys_def remove1_idem)
haftmann@37052
   500
  next
haftmann@37052
   501
    case True with fin show ?thesis by (simp add: ordered_keys_def sorted_list_of_set_remove)
haftmann@37052
   502
  qed
haftmann@37052
   503
qed
haftmann@37052
   504
haftmann@37052
   505
lemma ordered_keys_replace [simp]:
haftmann@37052
   506
  "ordered_keys (replace k v m) = ordered_keys m"
haftmann@37052
   507
  by (simp add: replace_def)
haftmann@37052
   508
haftmann@37052
   509
lemma ordered_keys_default [simp]:
haftmann@37052
   510
  "k \<in> keys m \<Longrightarrow> ordered_keys (default k v m) = ordered_keys m"
haftmann@37052
   511
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (default k v m) = insort k (ordered_keys m)"
haftmann@37052
   512
  by (simp_all add: default_def)
haftmann@37052
   513
haftmann@37052
   514
lemma ordered_keys_map_entry [simp]:
haftmann@37052
   515
  "ordered_keys (map_entry k f m) = ordered_keys m"
haftmann@37052
   516
  by (simp add: ordered_keys_def)
haftmann@37052
   517
haftmann@37052
   518
lemma ordered_keys_map_default [simp]:
haftmann@37052
   519
  "k \<in> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = ordered_keys m"
haftmann@37052
   520
  "finite (keys m) \<Longrightarrow> k \<notin> keys m \<Longrightarrow> ordered_keys (map_default k v f m) = insort k (ordered_keys m)"
haftmann@37052
   521
  by (simp_all add: map_default_def)
haftmann@37052
   522
haftmann@37052
   523
lemma ordered_keys_tabulate [simp]:
haftmann@37052
   524
  "ordered_keys (tabulate ks f) = sort (remdups ks)"
haftmann@37052
   525
  by (simp add: ordered_keys_def sorted_list_of_set_sort_remdups)
haftmann@37052
   526
haftmann@37052
   527
lemma ordered_keys_bulkload [simp]:
haftmann@37052
   528
  "ordered_keys (bulkload ks) = [0..<length ks]"
haftmann@37052
   529
  by (simp add: ordered_keys_def)
haftmann@36110
   530
haftmann@56528
   531
lemma tabulate_fold:
haftmann@56528
   532
  "tabulate xs f = fold (\<lambda>k m. update k (f k) m) xs empty"
haftmann@56528
   533
proof transfer
haftmann@56528
   534
  fix f :: "'a \<Rightarrow> 'b" and xs
haftmann@56529
   535
  have "map_of (List.map (\<lambda>k. (k, f k)) xs) = foldr (\<lambda>k m. m(k \<mapsto> f k)) xs Map.empty"
haftmann@56529
   536
    by (simp add: foldr_map comp_def map_of_foldr)
haftmann@56528
   537
  also have "foldr (\<lambda>k m. m(k \<mapsto> f k)) xs = fold (\<lambda>k m. m(k \<mapsto> f k)) xs"
haftmann@56528
   538
    by (rule foldr_fold) (simp add: fun_eq_iff)
haftmann@56528
   539
  ultimately show "map_of (List.map (\<lambda>k. (k, f k)) xs) = fold (\<lambda>k m. m(k \<mapsto> f k)) xs Map.empty"
haftmann@56528
   540
    by simp
haftmann@56528
   541
qed
haftmann@56528
   542
eberlm@63194
   543
lemma All_mapping_mono:
eberlm@63194
   544
  "(\<And>k v. k \<in> keys m \<Longrightarrow> P k v \<Longrightarrow> Q k v) \<Longrightarrow> All_mapping m P \<Longrightarrow> All_mapping m Q"
eberlm@63194
   545
  unfolding All_mapping_def by transfer (auto simp: All_mapping_def dom_def split: option.splits)
haftmann@31459
   546
eberlm@63194
   547
lemma All_mapping_empty [simp]: "All_mapping Mapping.empty P"
eberlm@63194
   548
  by (auto simp: All_mapping_def lookup_empty)
eberlm@63194
   549
  
eberlm@63194
   550
lemma All_mapping_update_iff: 
eberlm@63194
   551
  "All_mapping (Mapping.update k v m) P \<longleftrightarrow> P k v \<and> All_mapping m (\<lambda>k' v'. k = k' \<or> P k' v')"
eberlm@63194
   552
  unfolding All_mapping_def 
eberlm@63194
   553
proof safe
eberlm@63194
   554
  assume "\<forall>x. case Mapping.lookup (Mapping.update k v m) x of None \<Rightarrow> True | Some y \<Rightarrow> P x y"
eberlm@63194
   555
  hence A: "case Mapping.lookup (Mapping.update k v m) x of None \<Rightarrow> True | Some y \<Rightarrow> P x y" for x
eberlm@63194
   556
    by blast
eberlm@63194
   557
  from A[of k] show "P k v" by (simp add: lookup_update)
eberlm@63194
   558
  show "case Mapping.lookup m x of None \<Rightarrow> True | Some v' \<Rightarrow> k = x \<or> P x v'" for x
eberlm@63194
   559
    using A[of x] by (auto simp add: lookup_update' split: if_splits option.splits)
eberlm@63194
   560
next
eberlm@63194
   561
  assume "P k v"
eberlm@63194
   562
  assume "\<forall>x. case Mapping.lookup m x of None \<Rightarrow> True | Some v' \<Rightarrow> k = x \<or> P x v'"
eberlm@63194
   563
  hence A: "case Mapping.lookup m x of None \<Rightarrow> True | Some v' \<Rightarrow> k = x \<or> P x v'" for x by blast
eberlm@63194
   564
  show "case Mapping.lookup (Mapping.update k v m) x of None \<Rightarrow> True | Some xa \<Rightarrow> P x xa" for x
eberlm@63194
   565
    using \<open>P k v\<close> A[of x] by (auto simp: lookup_update' split: option.splits)
eberlm@63194
   566
qed
eberlm@63194
   567
eberlm@63194
   568
lemma All_mapping_update:
eberlm@63194
   569
  "P k v \<Longrightarrow> All_mapping m (\<lambda>k' v'. k = k' \<or> P k' v') \<Longrightarrow> All_mapping (Mapping.update k v m) P"
eberlm@63194
   570
  by (simp add: All_mapping_update_iff)
eberlm@63194
   571
eberlm@63194
   572
lemma All_mapping_filter_iff:
eberlm@63194
   573
  "All_mapping (filter P m) Q \<longleftrightarrow> All_mapping m (\<lambda>k v. P k v \<longrightarrow> Q k v)"
eberlm@63194
   574
  by (auto simp: All_mapping_def lookup_filter split: option.splits)
eberlm@63194
   575
eberlm@63194
   576
lemma All_mapping_filter:
eberlm@63194
   577
  "All_mapping m Q \<Longrightarrow> All_mapping (filter P m) Q"
eberlm@63194
   578
  by (auto simp: All_mapping_filter_iff intro: All_mapping_mono)
haftmann@31459
   579
eberlm@63194
   580
lemma All_mapping_map_values:
eberlm@63194
   581
  "All_mapping (map_values f m) P \<longleftrightarrow> All_mapping m (\<lambda>k v. P k (f k v))"
eberlm@63194
   582
  by (auto simp: All_mapping_def lookup_map_values split: option.splits)
eberlm@63194
   583
eberlm@63194
   584
lemma All_mapping_tabulate: 
eberlm@63194
   585
  "(\<forall>x\<in>set xs. P x (f x)) \<Longrightarrow> All_mapping (Mapping.tabulate xs f) P"
eberlm@63194
   586
  unfolding All_mapping_def 
eberlm@63194
   587
  by (intro allI,  transfer) (auto split: option.split dest!: map_of_SomeD)
eberlm@63194
   588
eberlm@63194
   589
lemma All_mapping_alist:
eberlm@63194
   590
  "(\<And>k v. (k, v) \<in> set xs \<Longrightarrow> P k v) \<Longrightarrow> All_mapping (Mapping.of_alist xs) P"
eberlm@63194
   591
  by (auto simp: All_mapping_def lookup_of_alist dest!: map_of_SomeD split: option.splits)
eberlm@63194
   592
eberlm@63194
   593
eberlm@63194
   594
lemma combine_empty [simp]:
eberlm@63194
   595
  "combine f Mapping.empty y = y" "combine f y Mapping.empty = y"
eberlm@63194
   596
  by (transfer, force)+
eberlm@63194
   597
eberlm@63194
   598
lemma (in abel_semigroup) comm_monoid_set_combine: "comm_monoid_set (combine f) Mapping.empty"
eberlm@63194
   599
  by standard (transfer fixing: f, simp add: combine_options_ac[of f] ac_simps)+
eberlm@63194
   600
eberlm@63194
   601
locale combine_mapping_abel_semigroup = abel_semigroup
eberlm@63194
   602
begin
eberlm@63194
   603
eberlm@63194
   604
sublocale combine: comm_monoid_set "combine f" Mapping.empty
eberlm@63194
   605
  by (rule comm_monoid_set_combine)
eberlm@63194
   606
eberlm@63194
   607
lemma fold_combine_code:
eberlm@63194
   608
  "combine.F g (set xs) = foldr (\<lambda>x. combine f (g x)) (remdups xs) Mapping.empty"
eberlm@63194
   609
proof -
eberlm@63194
   610
  have "combine.F g (set xs) = foldr (\<lambda>x. combine f (g x)) xs Mapping.empty"
eberlm@63194
   611
    if "distinct xs" for xs
eberlm@63194
   612
    using that by (induction xs) simp_all
eberlm@63194
   613
  from this[of "remdups xs"] show ?thesis by simp
eberlm@63194
   614
qed
eberlm@63194
   615
  
eberlm@63194
   616
lemma keys_fold_combine:
eberlm@63194
   617
  assumes "finite A"
eberlm@63194
   618
  shows   "Mapping.keys (combine.F g A) = (\<Union>x\<in>A. Mapping.keys (g x))"
eberlm@63194
   619
  using assms by (induction A rule: finite_induct) simp_all
haftmann@35157
   620
huffman@49975
   621
end
haftmann@59485
   622
eberlm@63194
   623
  
eberlm@63194
   624
subsection \<open>Code generator setup\<close>
eberlm@63194
   625
eberlm@63194
   626
hide_const (open) empty is_empty rep lookup lookup_default filter update delete ordered_keys
eberlm@63194
   627
  keys size replace default map_entry map_default tabulate bulkload map map_values combine of_alist
eberlm@63194
   628
eberlm@63194
   629
end
eberlm@63194
   630