src/HOL/Library/Finite_Map.thy
author nipkow
Wed Jan 10 15:25:09 2018 +0100 (18 months ago)
changeset 67399 eab6ce8368fa
parent 66398 4d2ce596f505
child 67485 89f5d876a656
permissions -rw-r--r--
ran isabelle update_op on all sources
lars@63885
     1
(*  Title:      HOL/Library/Finite_Map.thy
lars@63885
     2
    Author:     Lars Hupel, TU M√ľnchen
lars@63885
     3
*)
lars@63885
     4
lars@63885
     5
section \<open>Type of finite maps defined as a subtype of maps\<close>
lars@63885
     6
lars@63885
     7
theory Finite_Map
lars@63885
     8
  imports FSet AList
lars@63885
     9
begin
lars@63885
    10
lars@63885
    11
subsection \<open>Auxiliary constants and lemmas over @{type map}\<close>
lars@63885
    12
lars@63885
    13
context includes lifting_syntax begin
lars@63885
    14
lars@63885
    15
abbreviation rel_map :: "('b \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'c) \<Rightarrow> bool" where
nipkow@67399
    16
"rel_map f \<equiv> (=) ===> rel_option f"
lars@63885
    17
lars@63885
    18
lemma map_empty_transfer[transfer_rule]: "rel_map A Map.empty Map.empty"
lars@63885
    19
by transfer_prover
lars@63885
    20
lars@63885
    21
lemma ran_transfer[transfer_rule]: "(rel_map A ===> rel_set A) ran ran"
lars@63885
    22
proof
lars@63885
    23
  fix m n
lars@63885
    24
  assume "rel_map A m n"
lars@63885
    25
  show "rel_set A (ran m) (ran n)"
lars@63885
    26
    proof (rule rel_setI)
lars@63885
    27
      fix x
lars@63885
    28
      assume "x \<in> ran m"
lars@63885
    29
      then obtain a where "m a = Some x"
lars@63885
    30
        unfolding ran_def by auto
lars@63885
    31
lars@63885
    32
      have "rel_option A (m a) (n a)"
lars@63885
    33
        using \<open>rel_map A m n\<close>
lars@63885
    34
        by (auto dest: rel_funD)
lars@63885
    35
      then obtain y where "n a = Some y" "A x y"
lars@63885
    36
        unfolding \<open>m a = _\<close>
lars@63885
    37
        by cases auto
lars@64180
    38
      then show "\<exists>y \<in> ran n. A x y"
lars@63885
    39
        unfolding ran_def by blast
lars@63885
    40
    next
lars@63885
    41
      fix y
lars@63885
    42
      assume "y \<in> ran n"
lars@63885
    43
      then obtain a where "n a = Some y"
lars@63885
    44
        unfolding ran_def by auto
lars@63885
    45
lars@63885
    46
      have "rel_option A (m a) (n a)"
lars@63885
    47
        using \<open>rel_map A m n\<close>
lars@63885
    48
        by (auto dest: rel_funD)
lars@63885
    49
      then obtain x where "m a = Some x" "A x y"
lars@63885
    50
        unfolding \<open>n a = _\<close>
lars@63885
    51
        by cases auto
lars@64180
    52
      then show "\<exists>x \<in> ran m. A x y"
lars@63885
    53
        unfolding ran_def by blast
lars@63885
    54
    qed
lars@63885
    55
qed
lars@63885
    56
lars@63885
    57
lemma ran_alt_def: "ran m = (the \<circ> m) ` dom m"
lars@63885
    58
unfolding ran_def dom_def by force
lars@63885
    59
nipkow@67399
    60
lemma dom_transfer[transfer_rule]: "(rel_map A ===> (=)) dom dom"
lars@63885
    61
proof
lars@63885
    62
  fix m n
lars@63885
    63
  assume "rel_map A m n"
lars@63885
    64
  have "m a \<noteq> None \<longleftrightarrow> n a \<noteq> None" for a
lars@63885
    65
    proof -
lars@63885
    66
      from \<open>rel_map A m n\<close> have "rel_option A (m a) (n a)"
lars@63885
    67
        unfolding rel_fun_def by auto
lars@64180
    68
      then show ?thesis
lars@63885
    69
        by cases auto
lars@63885
    70
    qed
lars@64180
    71
  then show "dom m = dom n"
lars@63885
    72
    by auto
lars@63885
    73
qed
lars@63885
    74
lars@63885
    75
definition map_upd :: "'a \<Rightarrow> 'b \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)" where
lars@63885
    76
"map_upd k v m = m(k \<mapsto> v)"
lars@63885
    77
lars@63885
    78
lemma map_upd_transfer[transfer_rule]:
nipkow@67399
    79
  "((=) ===> A ===> rel_map A ===> rel_map A) map_upd map_upd"
lars@63885
    80
unfolding map_upd_def[abs_def]
lars@63885
    81
by transfer_prover
lars@63885
    82
lars@63885
    83
definition map_filter :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)" where
lars@63885
    84
"map_filter P m = (\<lambda>x. if P x then m x else None)"
lars@63885
    85
lars@63885
    86
lemma map_filter_map_of[simp]: "map_filter P (map_of m) = map_of [(k, _) \<leftarrow> m. P k]"
lars@63885
    87
proof
lars@63885
    88
  fix x
lars@63885
    89
  show "map_filter P (map_of m) x = map_of [(k, _) \<leftarrow> m. P k] x"
lars@63885
    90
    by (induct m) (auto simp: map_filter_def)
lars@63885
    91
qed
lars@63885
    92
lars@63885
    93
lemma map_filter_transfer[transfer_rule]:
nipkow@67399
    94
  "((=) ===> rel_map A ===> rel_map A) map_filter map_filter"
lars@63885
    95
unfolding map_filter_def[abs_def]
lars@63885
    96
by transfer_prover
lars@63885
    97
lars@63885
    98
lemma map_filter_finite[intro]:
lars@63885
    99
  assumes "finite (dom m)"
lars@63885
   100
  shows "finite (dom (map_filter P m))"
lars@63885
   101
proof -
lars@63885
   102
  have "dom (map_filter P m) = Set.filter P (dom m)"
lars@63885
   103
    unfolding map_filter_def Set.filter_def dom_def
lars@63885
   104
    by auto
lars@64180
   105
  then show ?thesis
lars@63885
   106
    using assms
lars@63885
   107
    by (simp add: Set.filter_def)
lars@63885
   108
qed
lars@63885
   109
lars@63885
   110
definition map_drop :: "'a \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)" where
lars@63885
   111
"map_drop a = map_filter (\<lambda>a'. a' \<noteq> a)"
lars@63885
   112
lars@63885
   113
lemma map_drop_transfer[transfer_rule]:
nipkow@67399
   114
  "((=) ===> rel_map A ===> rel_map A) map_drop map_drop"
lars@63885
   115
unfolding map_drop_def[abs_def]
lars@63885
   116
by transfer_prover
lars@63885
   117
lars@63885
   118
definition map_drop_set :: "'a set \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)" where
lars@63885
   119
"map_drop_set A = map_filter (\<lambda>a. a \<notin> A)"
lars@63885
   120
lars@63885
   121
lemma map_drop_set_transfer[transfer_rule]:
nipkow@67399
   122
  "((=) ===> rel_map A ===> rel_map A) map_drop_set map_drop_set"
lars@63885
   123
unfolding map_drop_set_def[abs_def]
lars@63885
   124
by transfer_prover
lars@63885
   125
lars@63885
   126
definition map_restrict_set :: "'a set \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)" where
lars@63885
   127
"map_restrict_set A = map_filter (\<lambda>a. a \<in> A)"
lars@63885
   128
lars@63885
   129
lemma map_restrict_set_transfer[transfer_rule]:
nipkow@67399
   130
  "((=) ===> rel_map A ===> rel_map A) map_restrict_set map_restrict_set"
lars@63885
   131
unfolding map_restrict_set_def[abs_def]
lars@63885
   132
by transfer_prover
lars@63885
   133
lars@63885
   134
lemma map_add_transfer[transfer_rule]:
nipkow@67399
   135
  "(rel_map A ===> rel_map A ===> rel_map A) (++) (++)"
lars@63885
   136
unfolding map_add_def[abs_def]
lars@63885
   137
by transfer_prover
lars@63885
   138
lars@63885
   139
definition map_pred :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> bool" where
lars@63885
   140
"map_pred P m \<longleftrightarrow> (\<forall>x. case m x of None \<Rightarrow> True | Some y \<Rightarrow> P x y)"
lars@63885
   141
lars@63885
   142
lemma map_pred_transfer[transfer_rule]:
nipkow@67399
   143
  "(((=) ===> A ===> (=)) ===> rel_map A ===> (=)) map_pred map_pred"
lars@63885
   144
unfolding map_pred_def[abs_def]
lars@63885
   145
by transfer_prover
lars@63885
   146
lars@63885
   147
definition rel_map_on_set :: "'a set \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'c) \<Rightarrow> bool" where
lars@63885
   148
"rel_map_on_set S P = eq_onp (\<lambda>x. x \<in> S) ===> rel_option P"
lars@66267
   149
lars@63885
   150
lemma map_of_transfer[transfer_rule]:
lars@63885
   151
  includes lifting_syntax
nipkow@67399
   152
  shows "(list_all2 (rel_prod (=) A) ===> rel_map A) map_of map_of"
lars@63885
   153
unfolding map_of_def by transfer_prover
lars@66267
   154
lars@66282
   155
definition set_of_map :: "('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<times> 'b) set" where
lars@66282
   156
"set_of_map m = {(k, v)|k v. m k = Some v}"
lars@66282
   157
lars@66282
   158
lemma set_of_map_alt_def: "set_of_map m = (\<lambda>k. (k, the (m k))) ` dom m"
lars@66282
   159
unfolding set_of_map_def dom_def
lars@66282
   160
by auto
lars@66282
   161
lars@66282
   162
lemma set_of_map_finite: "finite (dom m) \<Longrightarrow> finite (set_of_map m)"
lars@66282
   163
unfolding set_of_map_alt_def
lars@66282
   164
by auto
lars@66282
   165
lars@66282
   166
lemma set_of_map_inj: "inj set_of_map"
lars@66282
   167
proof
lars@66282
   168
  fix x y
lars@66282
   169
  assume "set_of_map x = set_of_map y"
lars@66282
   170
  hence "(x a = Some b) = (y a = Some b)" for a b
lars@66282
   171
    unfolding set_of_map_def by auto
lars@66282
   172
  hence "x k = y k" for k
lars@66282
   173
    by (metis not_None_eq)
lars@66282
   174
  thus "x = y" ..
lars@66282
   175
qed
lars@66282
   176
lars@63885
   177
end
lars@63885
   178
lars@63885
   179
lars@63885
   180
subsection \<open>Abstract characterisation\<close>
lars@63885
   181
lars@63885
   182
typedef ('a, 'b) fmap = "{m. finite (dom m)} :: ('a \<rightharpoonup> 'b) set"
lars@63885
   183
  morphisms fmlookup Abs_fmap
lars@63885
   184
proof
lars@63885
   185
  show "Map.empty \<in> {m. finite (dom m)}"
lars@63885
   186
    by auto
lars@63885
   187
qed
lars@63885
   188
lars@63885
   189
setup_lifting type_definition_fmap
lars@63885
   190
lars@63885
   191
lemma fmlookup_finite[intro, simp]: "finite (dom (fmlookup m))"
lars@63885
   192
using fmap.fmlookup by auto
lars@63885
   193
lars@63885
   194
lemma fmap_ext:
lars@63885
   195
  assumes "\<And>x. fmlookup m x = fmlookup n x"
lars@63885
   196
  shows "m = n"
lars@63885
   197
using assms
lars@63885
   198
by transfer' auto
lars@63885
   199
lars@63885
   200
lars@63885
   201
subsection \<open>Operations\<close>
lars@63885
   202
lars@63885
   203
context
lars@63885
   204
  includes fset.lifting
lars@63885
   205
begin
lars@63885
   206
lars@63885
   207
lift_definition fmran :: "('a, 'b) fmap \<Rightarrow> 'b fset"
lars@63885
   208
  is ran
lars@63885
   209
  parametric ran_transfer
lars@63885
   210
unfolding ran_alt_def by auto
lars@63885
   211
lars@66268
   212
lemma fmlookup_ran_iff: "y |\<in>| fmran m \<longleftrightarrow> (\<exists>x. fmlookup m x = Some y)"
lars@66268
   213
by transfer' (auto simp: ran_def)
lars@66268
   214
lars@66268
   215
lemma fmranI: "fmlookup m x = Some y \<Longrightarrow> y |\<in>| fmran m" by (auto simp: fmlookup_ran_iff)
lars@66268
   216
lars@66268
   217
lemma fmranE[elim]:
lars@66268
   218
  assumes "y |\<in>| fmran m"
lars@66268
   219
  obtains x where "fmlookup m x = Some y"
lars@66268
   220
using assms by (auto simp: fmlookup_ran_iff)
lars@63885
   221
lars@63885
   222
lift_definition fmdom :: "('a, 'b) fmap \<Rightarrow> 'a fset"
lars@63885
   223
  is dom
lars@63885
   224
  parametric dom_transfer
lars@63885
   225
.
lars@63885
   226
lars@66268
   227
lemma fmlookup_dom_iff: "x |\<in>| fmdom m \<longleftrightarrow> (\<exists>a. fmlookup m x = Some a)"
lars@66268
   228
by transfer' auto
lars@66268
   229
lars@66268
   230
lemma fmdom_notI: "fmlookup m x = None \<Longrightarrow> x |\<notin>| fmdom m" by (simp add: fmlookup_dom_iff)
lars@66268
   231
lemma fmdomI: "fmlookup m x = Some y \<Longrightarrow> x |\<in>| fmdom m" by (simp add: fmlookup_dom_iff)
lars@66268
   232
lemma fmdom_notD[dest]: "x |\<notin>| fmdom m \<Longrightarrow> fmlookup m x = None" by (simp add: fmlookup_dom_iff)
lars@66268
   233
lars@66268
   234
lemma fmdomE[elim]:
lars@66268
   235
  assumes "x |\<in>| fmdom m"
lars@66268
   236
  obtains y where "fmlookup m x = Some y"
lars@66268
   237
using assms by (auto simp: fmlookup_dom_iff)
lars@63885
   238
lars@63885
   239
lift_definition fmdom' :: "('a, 'b) fmap \<Rightarrow> 'a set"
lars@63885
   240
  is dom
lars@63885
   241
  parametric dom_transfer
lars@63885
   242
.
lars@63885
   243
lars@66268
   244
lemma fmlookup_dom'_iff: "x \<in> fmdom' m \<longleftrightarrow> (\<exists>a. fmlookup m x = Some a)"
lars@66268
   245
by transfer' auto
lars@66268
   246
lars@66268
   247
lemma fmdom'_notI: "fmlookup m x = None \<Longrightarrow> x \<notin> fmdom' m" by (simp add: fmlookup_dom'_iff)
lars@66268
   248
lemma fmdom'I: "fmlookup m x = Some y \<Longrightarrow> x \<in> fmdom' m" by (simp add: fmlookup_dom'_iff)
lars@66268
   249
lemma fmdom'_notD[dest]: "x \<notin> fmdom' m \<Longrightarrow> fmlookup m x = None" by (simp add: fmlookup_dom'_iff)
lars@63885
   250
lars@66268
   251
lemma fmdom'E[elim]:
lars@66268
   252
  assumes "x \<in> fmdom' m"
lars@66268
   253
  obtains x y where "fmlookup m x = Some y"
lars@66268
   254
using assms by (auto simp: fmlookup_dom'_iff)
lars@63885
   255
lars@66268
   256
lemma fmdom'_alt_def: "fmdom' m = fset (fmdom m)"
lars@66268
   257
by transfer' force
lars@63885
   258
lars@63885
   259
lift_definition fmempty :: "('a, 'b) fmap"
lars@63885
   260
  is Map.empty
lars@63885
   261
  parametric map_empty_transfer
lars@63885
   262
by simp
lars@63885
   263
lars@63885
   264
lemma fmempty_lookup[simp]: "fmlookup fmempty x = None"
lars@63885
   265
by transfer' simp
lars@63885
   266
lars@63885
   267
lemma fmdom_empty[simp]: "fmdom fmempty = {||}" by transfer' simp
lars@63885
   268
lemma fmdom'_empty[simp]: "fmdom' fmempty = {}" by transfer' simp
lars@66269
   269
lemma fmran_empty[simp]: "fmran fmempty = fempty" by transfer' (auto simp: ran_def map_filter_def)
lars@63885
   270
lars@63885
   271
lift_definition fmupd :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   272
  is map_upd
lars@63885
   273
  parametric map_upd_transfer
lars@63885
   274
unfolding map_upd_def[abs_def]
lars@63885
   275
by simp
lars@63885
   276
lars@63885
   277
lemma fmupd_lookup[simp]: "fmlookup (fmupd a b m) a' = (if a = a' then Some b else fmlookup m a')"
lars@63885
   278
by transfer' (auto simp: map_upd_def)
lars@63885
   279
lars@63885
   280
lemma fmdom_fmupd[simp]: "fmdom (fmupd a b m) = finsert a (fmdom m)" by transfer (simp add: map_upd_def)
lars@63885
   281
lemma fmdom'_fmupd[simp]: "fmdom' (fmupd a b m) = insert a (fmdom' m)" by transfer (simp add: map_upd_def)
lars@63885
   282
lars@63885
   283
lift_definition fmfilter :: "('a \<Rightarrow> bool) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   284
  is map_filter
lars@63885
   285
  parametric map_filter_transfer
lars@63885
   286
by auto
lars@63885
   287
lars@63885
   288
lemma fmdom_filter[simp]: "fmdom (fmfilter P m) = ffilter P (fmdom m)"
lars@63885
   289
by transfer' (auto simp: map_filter_def Set.filter_def split: if_splits)
lars@63885
   290
lars@63885
   291
lemma fmdom'_filter[simp]: "fmdom' (fmfilter P m) = Set.filter P (fmdom' m)"
lars@63885
   292
by transfer' (auto simp: map_filter_def Set.filter_def split: if_splits)
lars@63885
   293
lars@63885
   294
lemma fmlookup_filter[simp]: "fmlookup (fmfilter P m) x = (if P x then fmlookup m x else None)"
lars@63885
   295
by transfer' (auto simp: map_filter_def)
lars@63885
   296
lars@63885
   297
lemma fmfilter_empty[simp]: "fmfilter P fmempty = fmempty"
lars@63885
   298
by transfer' (auto simp: map_filter_def)
lars@63885
   299
lars@63900
   300
lemma fmfilter_true[simp]:
lars@66268
   301
  assumes "\<And>x y. fmlookup m x = Some y \<Longrightarrow> P x"
lars@63900
   302
  shows "fmfilter P m = m"
lars@63900
   303
proof (rule fmap_ext)
lars@63900
   304
  fix x
lars@63900
   305
  have "fmlookup m x = None" if "\<not> P x"
lars@66268
   306
    using that assms by fastforce
lars@64180
   307
  then show "fmlookup (fmfilter P m) x = fmlookup m x"
lars@63900
   308
    by simp
lars@63900
   309
qed
lars@63885
   310
lars@66268
   311
lemma fmfilter_false[simp]:
lars@66268
   312
  assumes "\<And>x y. fmlookup m x = Some y \<Longrightarrow> \<not> P x"
lars@66268
   313
  shows "fmfilter P m = fmempty"
lars@66268
   314
using assms by transfer' (fastforce simp: map_filter_def)
lars@63885
   315
lars@63885
   316
lemma fmfilter_comp[simp]: "fmfilter P (fmfilter Q m) = fmfilter (\<lambda>x. P x \<and> Q x) m"
lars@63885
   317
by transfer' (auto simp: map_filter_def)
lars@63885
   318
lars@63885
   319
lemma fmfilter_comm: "fmfilter P (fmfilter Q m) = fmfilter Q (fmfilter P m)"
lars@63885
   320
unfolding fmfilter_comp by meson
lars@63885
   321
lars@63885
   322
lemma fmfilter_cong[cong]:
lars@66268
   323
  assumes "\<And>x y. fmlookup m x = Some y \<Longrightarrow> P x = Q x"
lars@63885
   324
  shows "fmfilter P m = fmfilter Q m"
lars@63900
   325
proof (rule fmap_ext)
lars@63900
   326
  fix x
lars@63900
   327
  have "fmlookup m x = None" if "P x \<noteq> Q x"
lars@66268
   328
    using that assms by fastforce
lars@64180
   329
  then show "fmlookup (fmfilter P m) x = fmlookup (fmfilter Q m) x"
lars@63900
   330
    by auto
lars@63900
   331
qed
lars@63885
   332
lars@63885
   333
lemma fmfilter_cong'[fundef_cong]:
lars@63885
   334
  assumes "\<And>x. x \<in> fmdom' m \<Longrightarrow> P x = Q x"
lars@63885
   335
  shows "fmfilter P m = fmfilter Q m"
lars@66268
   336
using assms
lars@66268
   337
by (rule fmfilter_cong) (metis fmdom'I)
lars@63885
   338
lars@63900
   339
lemma fmfilter_upd[simp]:
lars@63900
   340
  "fmfilter P (fmupd x y m) = (if P x then fmupd x y (fmfilter P m) else fmfilter P m)"
lars@63885
   341
by transfer' (auto simp: map_upd_def map_filter_def)
lars@63885
   342
lars@63885
   343
lift_definition fmdrop :: "'a \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   344
  is map_drop
lars@63885
   345
  parametric map_drop_transfer
lars@63885
   346
unfolding map_drop_def by auto
lars@63885
   347
lars@63885
   348
lemma fmdrop_lookup[simp]: "fmlookup (fmdrop a m) a = None"
lars@63885
   349
by transfer' (auto simp: map_drop_def map_filter_def)
lars@63885
   350
lars@63885
   351
lift_definition fmdrop_set :: "'a set \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   352
  is map_drop_set
lars@63885
   353
  parametric map_drop_set_transfer
lars@63885
   354
unfolding map_drop_set_def by auto
lars@63885
   355
lars@63885
   356
lift_definition fmdrop_fset :: "'a fset \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   357
  is map_drop_set
lars@63885
   358
  parametric map_drop_set_transfer
lars@63885
   359
unfolding map_drop_set_def by auto
lars@63885
   360
lars@63885
   361
lift_definition fmrestrict_set :: "'a set \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   362
  is map_restrict_set
lars@63885
   363
  parametric map_restrict_set_transfer
lars@63885
   364
unfolding map_restrict_set_def by auto
lars@63885
   365
lars@63885
   366
lift_definition fmrestrict_fset :: "'a fset \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   367
  is map_restrict_set
lars@63885
   368
  parametric map_restrict_set_transfer
lars@63885
   369
unfolding map_restrict_set_def by auto
lars@63885
   370
lars@63885
   371
lemma fmfilter_alt_defs:
lars@63885
   372
  "fmdrop a = fmfilter (\<lambda>a'. a' \<noteq> a)"
lars@63885
   373
  "fmdrop_set A = fmfilter (\<lambda>a. a \<notin> A)"
lars@63885
   374
  "fmdrop_fset B = fmfilter (\<lambda>a. a |\<notin>| B)"
lars@63885
   375
  "fmrestrict_set A = fmfilter (\<lambda>a. a \<in> A)"
lars@63885
   376
  "fmrestrict_fset B = fmfilter (\<lambda>a. a |\<in>| B)"
lars@63885
   377
by (transfer'; simp add: map_drop_def map_drop_set_def map_restrict_set_def)+
lars@63885
   378
lars@63885
   379
lemma fmdom_drop[simp]: "fmdom (fmdrop a m) = fmdom m - {|a|}" unfolding fmfilter_alt_defs by auto
lars@63885
   380
lemma fmdom'_drop[simp]: "fmdom' (fmdrop a m) = fmdom' m - {a}" unfolding fmfilter_alt_defs by auto
lars@63885
   381
lemma fmdom'_drop_set[simp]: "fmdom' (fmdrop_set A m) = fmdom' m - A" unfolding fmfilter_alt_defs by auto
lars@63885
   382
lemma fmdom_drop_fset[simp]: "fmdom (fmdrop_fset A m) = fmdom m - A" unfolding fmfilter_alt_defs by auto
lars@63885
   383
lemma fmdom'_restrict_set: "fmdom' (fmrestrict_set A m) \<subseteq> A" unfolding fmfilter_alt_defs by auto
lars@63885
   384
lemma fmdom_restrict_fset: "fmdom (fmrestrict_fset A m) |\<subseteq>| A" unfolding fmfilter_alt_defs by auto
lars@63885
   385
lars@63885
   386
lemma fmdom'_drop_fset[simp]: "fmdom' (fmdrop_fset A m) = fmdom' m - fset A"
lars@63885
   387
unfolding fmfilter_alt_defs by transfer' (auto simp: map_filter_def split: if_splits)
lars@63885
   388
lars@63885
   389
lemma fmdom'_restrict_fset: "fmdom' (fmrestrict_fset A m) \<subseteq> fset A"
lars@63885
   390
unfolding fmfilter_alt_defs by transfer' (auto simp: map_filter_def)
lars@63885
   391
lars@63885
   392
lemma fmlookup_drop[simp]:
lars@63885
   393
  "fmlookup (fmdrop a m) x = (if x \<noteq> a then fmlookup m x else None)"
lars@63885
   394
unfolding fmfilter_alt_defs by simp
lars@63885
   395
lars@63885
   396
lemma fmlookup_drop_set[simp]:
lars@63885
   397
  "fmlookup (fmdrop_set A m) x = (if x \<notin> A then fmlookup m x else None)"
lars@63885
   398
unfolding fmfilter_alt_defs by simp
lars@63885
   399
lars@63885
   400
lemma fmlookup_drop_fset[simp]:
lars@63885
   401
  "fmlookup (fmdrop_fset A m) x = (if x |\<notin>| A then fmlookup m x else None)"
lars@63885
   402
unfolding fmfilter_alt_defs by simp
lars@63885
   403
lars@63885
   404
lemma fmlookup_restrict_set[simp]:
lars@63885
   405
  "fmlookup (fmrestrict_set A m) x = (if x \<in> A then fmlookup m x else None)"
lars@63885
   406
unfolding fmfilter_alt_defs by simp
lars@63885
   407
lars@63885
   408
lemma fmlookup_restrict_fset[simp]:
lars@63885
   409
  "fmlookup (fmrestrict_fset A m) x = (if x |\<in>| A then fmlookup m x else None)"
lars@63885
   410
unfolding fmfilter_alt_defs by simp
lars@63885
   411
lars@63900
   412
lemma fmrestrict_set_dom[simp]: "fmrestrict_set (fmdom' m) m = m"
lars@66268
   413
  by (rule fmap_ext) auto
lars@63900
   414
lars@63900
   415
lemma fmrestrict_fset_dom[simp]: "fmrestrict_fset (fmdom m) m = m"
lars@66268
   416
  by (rule fmap_ext) auto
lars@63900
   417
lars@63885
   418
lemma fmdrop_empty[simp]: "fmdrop a fmempty = fmempty"
lars@63885
   419
  unfolding fmfilter_alt_defs by simp
lars@63885
   420
lars@63885
   421
lemma fmdrop_set_empty[simp]: "fmdrop_set A fmempty = fmempty"
lars@63885
   422
  unfolding fmfilter_alt_defs by simp
lars@63885
   423
lars@63885
   424
lemma fmdrop_fset_empty[simp]: "fmdrop_fset A fmempty = fmempty"
lars@63885
   425
  unfolding fmfilter_alt_defs by simp
lars@63885
   426
lars@63885
   427
lemma fmrestrict_set_empty[simp]: "fmrestrict_set A fmempty = fmempty"
lars@63885
   428
  unfolding fmfilter_alt_defs by simp
lars@63885
   429
lars@63885
   430
lemma fmrestrict_fset_empty[simp]: "fmrestrict_fset A fmempty = fmempty"
lars@63885
   431
  unfolding fmfilter_alt_defs by simp
lars@63885
   432
lars@66269
   433
lemma fmdrop_set_null[simp]: "fmdrop_set {} m = m"
lars@66269
   434
  by (rule fmap_ext) auto
lars@66269
   435
lars@66269
   436
lemma fmdrop_fset_null[simp]: "fmdrop_fset {||} m = m"
lars@66269
   437
  by (rule fmap_ext) auto
lars@66269
   438
lars@63885
   439
lemma fmdrop_set_single[simp]: "fmdrop_set {a} m = fmdrop a m"
lars@63885
   440
  unfolding fmfilter_alt_defs by simp
lars@63885
   441
lars@63885
   442
lemma fmdrop_fset_single[simp]: "fmdrop_fset {|a|} m = fmdrop a m"
lars@63885
   443
  unfolding fmfilter_alt_defs by simp
lars@63885
   444
lars@63885
   445
lemma fmrestrict_set_null[simp]: "fmrestrict_set {} m = fmempty"
lars@63885
   446
  unfolding fmfilter_alt_defs by simp
lars@63885
   447
lars@63885
   448
lemma fmrestrict_fset_null[simp]: "fmrestrict_fset {||} m = fmempty"
lars@63885
   449
  unfolding fmfilter_alt_defs by simp
lars@63885
   450
lars@63885
   451
lemma fmdrop_comm: "fmdrop a (fmdrop b m) = fmdrop b (fmdrop a m)"
lars@63885
   452
unfolding fmfilter_alt_defs by (rule fmfilter_comm)
lars@63885
   453
lars@66269
   454
lemma fmdrop_set_insert[simp]: "fmdrop_set (insert x S) m = fmdrop x (fmdrop_set S m)"
lars@66269
   455
by (rule fmap_ext) auto
lars@66269
   456
lars@66269
   457
lemma fmdrop_fset_insert[simp]: "fmdrop_fset (finsert x S) m = fmdrop x (fmdrop_fset S m)"
lars@66269
   458
by (rule fmap_ext) auto
lars@66269
   459
lars@63885
   460
lift_definition fmadd :: "('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap" (infixl "++\<^sub>f" 100)
lars@63885
   461
  is map_add
lars@63885
   462
  parametric map_add_transfer
lars@63885
   463
by simp
lars@63885
   464
lars@63900
   465
lemma fmlookup_add[simp]:
lars@63900
   466
  "fmlookup (m ++\<^sub>f n) x = (if x |\<in>| fmdom n then fmlookup n x else fmlookup m x)"
lars@63900
   467
  by transfer' (auto simp: map_add_def split: option.splits)
lars@63900
   468
lars@63885
   469
lemma fmdom_add[simp]: "fmdom (m ++\<^sub>f n) = fmdom m |\<union>| fmdom n" by transfer' auto
lars@63885
   470
lemma fmdom'_add[simp]: "fmdom' (m ++\<^sub>f n) = fmdom' m \<union> fmdom' n" by transfer' auto
lars@63885
   471
lars@63885
   472
lemma fmadd_drop_left_dom: "fmdrop_fset (fmdom n) m ++\<^sub>f n = m ++\<^sub>f n"
lars@63900
   473
  by (rule fmap_ext) auto
lars@63885
   474
lars@63885
   475
lemma fmadd_restrict_right_dom: "fmrestrict_fset (fmdom n) (m ++\<^sub>f n) = n"
lars@66268
   476
  by (rule fmap_ext) auto
lars@63885
   477
lars@63885
   478
lemma fmfilter_add_distrib[simp]: "fmfilter P (m ++\<^sub>f n) = fmfilter P m ++\<^sub>f fmfilter P n"
lars@63885
   479
by transfer' (auto simp: map_filter_def map_add_def)
lars@63885
   480
lars@63885
   481
lemma fmdrop_add_distrib[simp]: "fmdrop a (m ++\<^sub>f n) = fmdrop a m ++\<^sub>f fmdrop a n"
lars@63885
   482
  unfolding fmfilter_alt_defs by simp
lars@63885
   483
lars@63885
   484
lemma fmdrop_set_add_distrib[simp]: "fmdrop_set A (m ++\<^sub>f n) = fmdrop_set A m ++\<^sub>f fmdrop_set A n"
lars@63885
   485
  unfolding fmfilter_alt_defs by simp
lars@63885
   486
lars@63885
   487
lemma fmdrop_fset_add_distrib[simp]: "fmdrop_fset A (m ++\<^sub>f n) = fmdrop_fset A m ++\<^sub>f fmdrop_fset A n"
lars@63885
   488
  unfolding fmfilter_alt_defs by simp
lars@63885
   489
lars@63885
   490
lemma fmrestrict_set_add_distrib[simp]:
lars@63885
   491
  "fmrestrict_set A (m ++\<^sub>f n) = fmrestrict_set A m ++\<^sub>f fmrestrict_set A n"
lars@63885
   492
  unfolding fmfilter_alt_defs by simp
lars@63885
   493
lars@63885
   494
lemma fmrestrict_fset_add_distrib[simp]:
lars@63885
   495
  "fmrestrict_fset A (m ++\<^sub>f n) = fmrestrict_fset A m ++\<^sub>f fmrestrict_fset A n"
lars@63885
   496
  unfolding fmfilter_alt_defs by simp
lars@63885
   497
lars@63885
   498
lemma fmadd_empty[simp]: "fmempty ++\<^sub>f m = m" "m ++\<^sub>f fmempty = m"
lars@63885
   499
by (transfer'; auto)+
lars@63885
   500
lars@63885
   501
lemma fmadd_idempotent[simp]: "m ++\<^sub>f m = m"
lars@63885
   502
by transfer' (auto simp: map_add_def split: option.splits)
lars@63885
   503
lars@63885
   504
lemma fmadd_assoc[simp]: "m ++\<^sub>f (n ++\<^sub>f p) = m ++\<^sub>f n ++\<^sub>f p"
lars@63885
   505
by transfer' simp
lars@63885
   506
lars@66269
   507
lemma fmadd_fmupd[simp]: "m ++\<^sub>f fmupd a b n = fmupd a b (m ++\<^sub>f n)"
lars@66269
   508
by (rule fmap_ext) simp
lars@66269
   509
lars@63885
   510
lift_definition fmpred :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> bool"
lars@63885
   511
  is map_pred
lars@63885
   512
  parametric map_pred_transfer
lars@63885
   513
.
lars@63885
   514
lars@63885
   515
lemma fmpredI[intro]:
lars@63885
   516
  assumes "\<And>x y. fmlookup m x = Some y \<Longrightarrow> P x y"
lars@63885
   517
  shows "fmpred P m"
lars@63885
   518
using assms
lars@63885
   519
by transfer' (auto simp: map_pred_def split: option.splits)
lars@63885
   520
lars@66267
   521
lemma fmpredD[dest]: "fmpred P m \<Longrightarrow> fmlookup m x = Some y \<Longrightarrow> P x y"
lars@63885
   522
by transfer' (auto simp: map_pred_def split: option.split_asm)
lars@63885
   523
lars@63885
   524
lemma fmpred_iff: "fmpred P m \<longleftrightarrow> (\<forall>x y. fmlookup m x = Some y \<longrightarrow> P x y)"
lars@63885
   525
by auto
lars@63885
   526
lars@63885
   527
lemma fmpred_alt_def: "fmpred P m \<longleftrightarrow> fBall (fmdom m) (\<lambda>x. P x (the (fmlookup m x)))"
lars@63885
   528
unfolding fmpred_iff
lars@63885
   529
apply auto
lars@63900
   530
apply (rename_tac x y)
lars@63885
   531
apply (erule_tac x = x in fBallE)
lars@63885
   532
apply simp
lars@63885
   533
by (simp add: fmlookup_dom_iff)
lars@63885
   534
lars@63885
   535
lemma fmpred_empty[intro!, simp]: "fmpred P fmempty"
lars@63885
   536
by auto
lars@63885
   537
lars@63885
   538
lemma fmpred_upd[intro]: "fmpred P m \<Longrightarrow> P x y \<Longrightarrow> fmpred P (fmupd x y m)"
lars@63885
   539
by transfer' (auto simp: map_pred_def map_upd_def)
lars@63885
   540
lars@63885
   541
lemma fmpred_updD[dest]: "fmpred P (fmupd x y m) \<Longrightarrow> P x y"
lars@63885
   542
by auto
lars@63885
   543
lars@63885
   544
lemma fmpred_add[intro]: "fmpred P m \<Longrightarrow> fmpred P n \<Longrightarrow> fmpred P (m ++\<^sub>f n)"
lars@63885
   545
by transfer' (auto simp: map_pred_def map_add_def split: option.splits)
lars@63885
   546
lars@63885
   547
lemma fmpred_filter[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmfilter Q m)"
lars@63885
   548
by transfer' (auto simp: map_pred_def map_filter_def)
lars@63885
   549
lars@63885
   550
lemma fmpred_drop[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmdrop a m)"
lars@63885
   551
  by (auto simp: fmfilter_alt_defs)
lars@63885
   552
lars@63885
   553
lemma fmpred_drop_set[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmdrop_set A m)"
lars@63885
   554
  by (auto simp: fmfilter_alt_defs)
lars@63885
   555
lars@63885
   556
lemma fmpred_drop_fset[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmdrop_fset A m)"
lars@63885
   557
  by (auto simp: fmfilter_alt_defs)
lars@63885
   558
lars@63885
   559
lemma fmpred_restrict_set[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmrestrict_set A m)"
lars@63885
   560
  by (auto simp: fmfilter_alt_defs)
lars@63885
   561
lars@63885
   562
lemma fmpred_restrict_fset[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmrestrict_fset A m)"
lars@63885
   563
  by (auto simp: fmfilter_alt_defs)
lars@63885
   564
lars@63885
   565
lemma fmpred_cases[consumes 1]:
lars@63885
   566
  assumes "fmpred P m"
lars@63885
   567
  obtains (none) "fmlookup m x = None" | (some) y where "fmlookup m x = Some y" "P x y"
lars@63885
   568
using assms by auto
lars@63885
   569
lars@63885
   570
lift_definition fmsubset :: "('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap \<Rightarrow> bool" (infix "\<subseteq>\<^sub>f" 50)
lars@63885
   571
  is map_le
lars@63885
   572
.
lars@63885
   573
lars@63885
   574
lemma fmsubset_alt_def: "m \<subseteq>\<^sub>f n \<longleftrightarrow> fmpred (\<lambda>k v. fmlookup n k = Some v) m"
lars@63885
   575
by transfer' (auto simp: map_pred_def map_le_def dom_def split: option.splits)
lars@63885
   576
lars@63885
   577
lemma fmsubset_pred: "fmpred P m \<Longrightarrow> n \<subseteq>\<^sub>f m \<Longrightarrow> fmpred P n"
lars@63885
   578
unfolding fmsubset_alt_def fmpred_iff
lars@63885
   579
by auto
lars@63885
   580
lars@63885
   581
lemma fmsubset_filter_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmfilter P m \<subseteq>\<^sub>f fmfilter P n"
lars@63885
   582
unfolding fmsubset_alt_def fmpred_iff
lars@63885
   583
by auto
lars@63885
   584
lars@63885
   585
lemma fmsubset_drop_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmdrop a m \<subseteq>\<^sub>f fmdrop a n"
lars@63885
   586
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   587
lars@63885
   588
lemma fmsubset_drop_set_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmdrop_set A m \<subseteq>\<^sub>f fmdrop_set A n"
lars@63885
   589
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   590
lars@63885
   591
lemma fmsubset_drop_fset_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmdrop_fset A m \<subseteq>\<^sub>f fmdrop_fset A n"
lars@63885
   592
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   593
lars@63885
   594
lemma fmsubset_restrict_set_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmrestrict_set A m \<subseteq>\<^sub>f fmrestrict_set A n"
lars@63885
   595
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   596
lars@63885
   597
lemma fmsubset_restrict_fset_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmrestrict_fset A m \<subseteq>\<^sub>f fmrestrict_fset A n"
lars@63885
   598
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   599
lars@66282
   600
lift_definition fset_of_fmap :: "('a, 'b) fmap \<Rightarrow> ('a \<times> 'b) fset" is set_of_map
lars@66282
   601
by (rule set_of_map_finite)
lars@66282
   602
lars@66282
   603
lemma fset_of_fmap_inj[intro, simp]: "inj fset_of_fmap"
lars@66282
   604
apply rule
lars@66282
   605
apply transfer'
lars@66282
   606
using set_of_map_inj unfolding inj_def by auto
lars@66282
   607
lars@66398
   608
lemma fset_of_fmap_iff[simp]: "(a, b) |\<in>| fset_of_fmap m \<longleftrightarrow> fmlookup m a = Some b"
lars@66398
   609
by transfer' (auto simp: set_of_map_def)
lars@66398
   610
lars@66398
   611
lemma fset_of_fmap_iff'[simp]: "(a, b) \<in> fset (fset_of_fmap m) \<longleftrightarrow> fmlookup m a = Some b"
lars@66398
   612
by transfer' (auto simp: set_of_map_def)
lars@66398
   613
lars@66398
   614
lars@63885
   615
lift_definition fmap_of_list :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) fmap"
lars@63885
   616
  is map_of
lars@63885
   617
  parametric map_of_transfer
lars@63885
   618
by (rule finite_dom_map_of)
lars@63885
   619
lars@63885
   620
lemma fmap_of_list_simps[simp]:
lars@63885
   621
  "fmap_of_list [] = fmempty"
lars@63885
   622
  "fmap_of_list ((k, v) # kvs) = fmupd k v (fmap_of_list kvs)"
lars@63885
   623
by (transfer, simp add: map_upd_def)+
lars@63885
   624
lars@63885
   625
lemma fmap_of_list_app[simp]: "fmap_of_list (xs @ ys) = fmap_of_list ys ++\<^sub>f fmap_of_list xs"
lars@63885
   626
by transfer' simp
lars@63885
   627
lars@63885
   628
lemma fmupd_alt_def: "fmupd k v m = m ++\<^sub>f fmap_of_list [(k, v)]"
lars@63885
   629
by transfer' (auto simp: map_upd_def)
lars@63885
   630
lars@63885
   631
lemma fmpred_of_list[intro]:
lars@63885
   632
  assumes "\<And>k v. (k, v) \<in> set xs \<Longrightarrow> P k v"
lars@63885
   633
  shows "fmpred P (fmap_of_list xs)"
lars@63885
   634
using assms
lars@63885
   635
by (induction xs) (transfer'; auto simp: map_pred_def)+
lars@63885
   636
lars@63885
   637
lemma fmap_of_list_SomeD: "fmlookup (fmap_of_list xs) k = Some v \<Longrightarrow> (k, v) \<in> set xs"
lars@63885
   638
by transfer' (auto dest: map_of_SomeD)
lars@63885
   639
lars@66269
   640
lemma fmdom_fmap_of_list[simp]: "fmdom (fmap_of_list xs) = fset_of_list (map fst xs)"
lars@66269
   641
apply transfer'
lars@66269
   642
apply (subst dom_map_of_conv_image_fst)
lars@66269
   643
apply auto
lars@66269
   644
done
lars@66269
   645
lars@63885
   646
lift_definition fmrel_on_fset :: "'a fset \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'c) fmap \<Rightarrow> bool"
lars@63885
   647
  is rel_map_on_set
lars@63885
   648
.
lars@63885
   649
lars@63885
   650
lemma fmrel_on_fset_alt_def: "fmrel_on_fset S P m n \<longleftrightarrow> fBall S (\<lambda>x. rel_option P (fmlookup m x) (fmlookup n x))"
lars@63885
   651
by transfer' (auto simp: rel_map_on_set_def eq_onp_def rel_fun_def)
lars@63885
   652
lars@64181
   653
lemma fmrel_on_fsetI[intro]:
lars@63885
   654
  assumes "\<And>x. x |\<in>| S \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   655
  shows "fmrel_on_fset S P m n"
lars@63885
   656
using assms
lars@63885
   657
unfolding fmrel_on_fset_alt_def by auto
lars@63885
   658
lars@63885
   659
lemma fmrel_on_fset_mono[mono]: "R \<le> Q \<Longrightarrow> fmrel_on_fset S R \<le> fmrel_on_fset S Q"
lars@63885
   660
unfolding fmrel_on_fset_alt_def[abs_def]
lars@63885
   661
apply (intro le_funI fBall_mono)
lars@63885
   662
using option.rel_mono by auto
lars@63885
   663
lars@63885
   664
lemma fmrel_on_fsetD: "x |\<in>| S \<Longrightarrow> fmrel_on_fset S P m n \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   665
unfolding fmrel_on_fset_alt_def
lars@63885
   666
by auto
lars@63885
   667
lars@63885
   668
lemma fmrel_on_fsubset: "fmrel_on_fset S R m n \<Longrightarrow> T |\<subseteq>| S \<Longrightarrow> fmrel_on_fset T R m n"
lars@63885
   669
unfolding fmrel_on_fset_alt_def
lars@63885
   670
by auto
lars@63885
   671
lars@66274
   672
lemma fmrel_on_fset_unionI:
lars@66274
   673
  "fmrel_on_fset A R m n \<Longrightarrow> fmrel_on_fset B R m n \<Longrightarrow> fmrel_on_fset (A |\<union>| B) R m n"
lars@66274
   674
unfolding fmrel_on_fset_alt_def
lars@66274
   675
by auto
lars@66274
   676
lars@66274
   677
lemma fmrel_on_fset_updateI:
lars@66274
   678
  assumes "fmrel_on_fset S P m n" "P v\<^sub>1 v\<^sub>2"
lars@66274
   679
  shows "fmrel_on_fset (finsert k S) P (fmupd k v\<^sub>1 m) (fmupd k v\<^sub>2 n)"
lars@66274
   680
using assms
lars@66274
   681
unfolding fmrel_on_fset_alt_def
lars@66274
   682
by auto
lars@66274
   683
lars@63885
   684
end
lars@63885
   685
lars@63885
   686
lars@63885
   687
subsection \<open>BNF setup\<close>
lars@63885
   688
lars@63885
   689
lift_bnf ('a, fmran': 'b) fmap [wits: Map.empty]
lars@63885
   690
  for map: fmmap
lars@63885
   691
      rel: fmrel
lars@63885
   692
  by auto
lars@63885
   693
lars@66269
   694
declare fmap.pred_mono[mono]
lars@66268
   695
lars@63885
   696
context includes lifting_syntax begin
lars@63885
   697
lars@63885
   698
lemma fmmap_transfer[transfer_rule]:
nipkow@67399
   699
  "((=) ===> pcr_fmap (=) (=) ===> pcr_fmap (=) (=)) (\<lambda>f. (\<circ>) (map_option f)) fmmap"
lars@64180
   700
  unfolding fmmap_def
lars@64180
   701
  by (rule rel_funI ext)+ (auto simp: fmap.Abs_fmap_inverse fmap.pcr_cr_eq cr_fmap_def)
lars@63885
   702
lars@63885
   703
lemma fmran'_transfer[transfer_rule]:
nipkow@67399
   704
  "(pcr_fmap (=) (=) ===> (=)) (\<lambda>x. UNION (range x) set_option) fmran'"
lars@64180
   705
  unfolding fmran'_def fmap.pcr_cr_eq cr_fmap_def by fastforce
lars@63885
   706
lars@63885
   707
lemma fmrel_transfer[transfer_rule]:
nipkow@67399
   708
  "((=) ===> pcr_fmap (=) (=) ===> pcr_fmap (=) (=) ===> (=)) rel_map fmrel"
lars@64180
   709
  unfolding fmrel_def fmap.pcr_cr_eq cr_fmap_def vimage2p_def by fastforce
lars@63885
   710
lars@63885
   711
end
lars@63885
   712
lars@63885
   713
lars@66268
   714
lemma fmran'_alt_def: "fmran' m = fset (fmran m)"
lars@63885
   715
including fset.lifting
lars@63885
   716
by transfer' (auto simp: ran_def fun_eq_iff)
lars@63885
   717
lars@66268
   718
lemma fmlookup_ran'_iff: "y \<in> fmran' m \<longleftrightarrow> (\<exists>x. fmlookup m x = Some y)"
lars@66268
   719
by transfer' (auto simp: ran_def)
lars@66268
   720
lars@66268
   721
lemma fmran'I: "fmlookup m x = Some y \<Longrightarrow> y \<in> fmran' m" by (auto simp: fmlookup_ran'_iff)
lars@66268
   722
lars@66268
   723
lemma fmran'E[elim]:
lars@66268
   724
  assumes "y \<in> fmran' m"
lars@66268
   725
  obtains x where "fmlookup m x = Some y"
lars@66268
   726
using assms by (auto simp: fmlookup_ran'_iff)
lars@63885
   727
lars@63885
   728
lemma fmrel_iff: "fmrel R m n \<longleftrightarrow> (\<forall>x. rel_option R (fmlookup m x) (fmlookup n x))"
lars@63885
   729
by transfer' (auto simp: rel_fun_def)
lars@63885
   730
lars@63885
   731
lemma fmrelI[intro]:
lars@63885
   732
  assumes "\<And>x. rel_option R (fmlookup m x) (fmlookup n x)"
lars@63885
   733
  shows "fmrel R m n"
lars@63885
   734
using assms
lars@63885
   735
by transfer' auto
lars@63885
   736
lars@63885
   737
lemma fmrel_upd[intro]: "fmrel P m n \<Longrightarrow> P x y \<Longrightarrow> fmrel P (fmupd k x m) (fmupd k y n)"
lars@63885
   738
by transfer' (auto simp: map_upd_def rel_fun_def)
lars@63885
   739
lars@63885
   740
lemma fmrelD[dest]: "fmrel P m n \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   741
by transfer' (auto simp: rel_fun_def)
lars@63885
   742
lars@63885
   743
lemma fmrel_addI[intro]:
lars@63885
   744
  assumes "fmrel P m n" "fmrel P a b"
lars@63885
   745
  shows "fmrel P (m ++\<^sub>f a) (n ++\<^sub>f b)"
lars@63885
   746
using assms
lars@63885
   747
apply transfer'
lars@63885
   748
apply (auto simp: rel_fun_def map_add_def)
lars@63885
   749
by (metis option.case_eq_if option.collapse option.rel_sel)
lars@63885
   750
lars@63885
   751
lemma fmrel_cases[consumes 1]:
lars@63885
   752
  assumes "fmrel P m n"
lars@63885
   753
  obtains (none) "fmlookup m x = None" "fmlookup n x = None"
lars@63885
   754
        | (some) a b where "fmlookup m x = Some a" "fmlookup n x = Some b" "P a b"
lars@63885
   755
proof -
lars@63885
   756
  from assms have "rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   757
    by auto
lars@64180
   758
  then show thesis
lars@63885
   759
    using none some
lars@63885
   760
    by (cases rule: option.rel_cases) auto
lars@63885
   761
qed
lars@63885
   762
lars@63885
   763
lemma fmrel_filter[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmfilter Q m) (fmfilter Q n)"
lars@63885
   764
unfolding fmrel_iff by auto
lars@63885
   765
lars@63885
   766
lemma fmrel_drop[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop a m) (fmdrop a n)"
lars@63885
   767
  unfolding fmfilter_alt_defs by blast
lars@63885
   768
lars@63885
   769
lemma fmrel_drop_set[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop_set A m) (fmdrop_set A n)"
lars@63885
   770
  unfolding fmfilter_alt_defs by blast
lars@63885
   771
lars@63885
   772
lemma fmrel_drop_fset[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop_fset A m) (fmdrop_fset A n)"
lars@63885
   773
  unfolding fmfilter_alt_defs by blast
lars@63885
   774
lars@63885
   775
lemma fmrel_restrict_set[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmrestrict_set A m) (fmrestrict_set A n)"
lars@63885
   776
  unfolding fmfilter_alt_defs by blast
lars@63885
   777
lars@63885
   778
lemma fmrel_restrict_fset[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmrestrict_fset A m) (fmrestrict_fset A n)"
lars@63885
   779
  unfolding fmfilter_alt_defs by blast
lars@63885
   780
lars@66274
   781
lemma fmrel_on_fset_fmrel_restrict:
lars@66274
   782
  "fmrel_on_fset S P m n \<longleftrightarrow> fmrel P (fmrestrict_fset S m) (fmrestrict_fset S n)"
lars@66274
   783
unfolding fmrel_on_fset_alt_def fmrel_iff
lars@66274
   784
by auto
lars@66274
   785
lars@66274
   786
lemma fmrel_on_fset_refl_strong:
lars@66274
   787
  assumes "\<And>x y. x |\<in>| S \<Longrightarrow> fmlookup m x = Some y \<Longrightarrow> P y y"
lars@66274
   788
  shows "fmrel_on_fset S P m m"
lars@66274
   789
unfolding fmrel_on_fset_fmrel_restrict fmrel_iff
lars@66274
   790
using assms
lars@66274
   791
by (simp add: option.rel_sel)
lars@66274
   792
lars@66274
   793
lemma fmrel_on_fset_addI:
lars@66274
   794
  assumes "fmrel_on_fset S P m n" "fmrel_on_fset S P a b"
lars@66274
   795
  shows "fmrel_on_fset S P (m ++\<^sub>f a) (n ++\<^sub>f b)"
lars@66274
   796
using assms
lars@66274
   797
unfolding fmrel_on_fset_fmrel_restrict
lars@66274
   798
by auto
lars@66274
   799
lars@66274
   800
lemma fmrel_fmdom_eq:
lars@66274
   801
  assumes "fmrel P x y"
lars@66274
   802
  shows "fmdom x = fmdom y"
lars@66274
   803
proof -
lars@66274
   804
  have "a |\<in>| fmdom x \<longleftrightarrow> a |\<in>| fmdom y" for a
lars@66274
   805
    proof -
lars@66274
   806
      have "rel_option P (fmlookup x a) (fmlookup y a)"
lars@66274
   807
        using assms by (simp add: fmrel_iff)
lars@66274
   808
      thus ?thesis
lars@66274
   809
        by cases (auto intro: fmdomI)
lars@66274
   810
    qed
lars@66274
   811
  thus ?thesis
lars@66274
   812
    by auto
lars@66274
   813
qed
lars@66274
   814
lars@66274
   815
lemma fmrel_fmdom'_eq: "fmrel P x y \<Longrightarrow> fmdom' x = fmdom' y"
lars@66274
   816
unfolding fmdom'_alt_def
lars@66274
   817
by (metis fmrel_fmdom_eq)
lars@66274
   818
lars@66274
   819
lemma fmrel_rel_fmran:
lars@66274
   820
  assumes "fmrel P x y"
lars@66274
   821
  shows "rel_fset P (fmran x) (fmran y)"
lars@66274
   822
proof -
lars@66274
   823
  {
lars@66274
   824
    fix b
lars@66274
   825
    assume "b |\<in>| fmran x"
lars@66274
   826
    then obtain a where "fmlookup x a = Some b"
lars@66274
   827
      by auto
lars@66274
   828
    moreover have "rel_option P (fmlookup x a) (fmlookup y a)"
lars@66274
   829
      using assms by auto
lars@66274
   830
    ultimately have "\<exists>b'. b' |\<in>| fmran y \<and> P b b'"
lars@66274
   831
      by (metis option_rel_Some1 fmranI)
lars@66274
   832
  }
lars@66274
   833
  moreover
lars@66274
   834
  {
lars@66274
   835
    fix b
lars@66274
   836
    assume "b |\<in>| fmran y"
lars@66274
   837
    then obtain a where "fmlookup y a = Some b"
lars@66274
   838
      by auto
lars@66274
   839
    moreover have "rel_option P (fmlookup x a) (fmlookup y a)"
lars@66274
   840
      using assms by auto
lars@66274
   841
    ultimately have "\<exists>b'. b' |\<in>| fmran x \<and> P b' b"
lars@66274
   842
      by (metis option_rel_Some2 fmranI)
lars@66274
   843
  }
lars@66274
   844
  ultimately show ?thesis
lars@66274
   845
    unfolding rel_fset_alt_def
lars@66274
   846
    by auto
lars@66274
   847
qed
lars@66274
   848
lars@66274
   849
lemma fmrel_rel_fmran': "fmrel P x y \<Longrightarrow> rel_set P (fmran' x) (fmran' y)"
lars@66274
   850
unfolding fmran'_alt_def
lars@66274
   851
by (metis fmrel_rel_fmran rel_fset_fset)
lars@66274
   852
lars@63885
   853
lemma pred_fmap_fmpred[simp]: "pred_fmap P = fmpred (\<lambda>_. P)"
lars@63885
   854
unfolding fmap.pred_set fmran'_alt_def
lars@63885
   855
including fset.lifting
lars@63885
   856
apply transfer'
lars@63885
   857
apply (rule ext)
lars@63885
   858
apply (auto simp: map_pred_def ran_def split: option.splits dest: )
lars@63885
   859
done
lars@63885
   860
lars@63885
   861
lemma pred_fmap_id[simp]: "pred_fmap id (fmmap f m) \<longleftrightarrow> pred_fmap f m"
lars@63885
   862
unfolding fmap.pred_set fmap.set_map
lars@63885
   863
by simp
lars@63885
   864
lars@66274
   865
lemma pred_fmapD: "pred_fmap P m \<Longrightarrow> x |\<in>| fmran m \<Longrightarrow> P x"
lars@66274
   866
by auto
lars@66274
   867
lars@63885
   868
lemma fmlookup_map[simp]: "fmlookup (fmmap f m) x = map_option f (fmlookup m x)"
lars@64180
   869
by transfer' auto
lars@63885
   870
lars@63885
   871
lemma fmpred_map[simp]: "fmpred P (fmmap f m) \<longleftrightarrow> fmpred (\<lambda>k v. P k (f v)) m"
lars@63885
   872
unfolding fmpred_iff pred_fmap_def fmap.set_map
lars@63885
   873
by auto
lars@63885
   874
lars@63885
   875
lemma fmpred_id[simp]: "fmpred (\<lambda>_. id) (fmmap f m) \<longleftrightarrow> fmpred (\<lambda>_. f) m"
lars@63885
   876
by simp
lars@63885
   877
lars@63885
   878
lemma fmmap_add[simp]: "fmmap f (m ++\<^sub>f n) = fmmap f m ++\<^sub>f fmmap f n"
lars@63885
   879
by transfer' (auto simp: map_add_def fun_eq_iff split: option.splits)
lars@63885
   880
lars@63885
   881
lemma fmmap_empty[simp]: "fmmap f fmempty = fmempty"
lars@63885
   882
by transfer auto
lars@63885
   883
lars@63885
   884
lemma fmdom_map[simp]: "fmdom (fmmap f m) = fmdom m"
lars@63885
   885
including fset.lifting
lars@63885
   886
by transfer' simp
lars@63885
   887
lars@63885
   888
lemma fmdom'_map[simp]: "fmdom' (fmmap f m) = fmdom' m"
lars@63885
   889
by transfer' simp
lars@63885
   890
lars@66269
   891
lemma fmran_fmmap[simp]: "fmran (fmmap f m) = f |`| fmran m"
lars@66269
   892
including fset.lifting
lars@66269
   893
by transfer' (auto simp: ran_def)
lars@66269
   894
lars@66269
   895
lemma fmran'_fmmap[simp]: "fmran' (fmmap f m) = f ` fmran' m"
lars@66269
   896
by transfer' (auto simp: ran_def)
lars@66269
   897
lars@63885
   898
lemma fmfilter_fmmap[simp]: "fmfilter P (fmmap f m) = fmmap f (fmfilter P m)"
lars@63885
   899
by transfer' (auto simp: map_filter_def)
lars@63885
   900
lars@63885
   901
lemma fmdrop_fmmap[simp]: "fmdrop a (fmmap f m) = fmmap f (fmdrop a m)" unfolding fmfilter_alt_defs by simp
lars@63885
   902
lemma fmdrop_set_fmmap[simp]: "fmdrop_set A (fmmap f m) = fmmap f (fmdrop_set A m)" unfolding fmfilter_alt_defs by simp
lars@63885
   903
lemma fmdrop_fset_fmmap[simp]: "fmdrop_fset A (fmmap f m) = fmmap f (fmdrop_fset A m)" unfolding fmfilter_alt_defs by simp
lars@63885
   904
lemma fmrestrict_set_fmmap[simp]: "fmrestrict_set A (fmmap f m) = fmmap f (fmrestrict_set A m)" unfolding fmfilter_alt_defs by simp
lars@63885
   905
lemma fmrestrict_fset_fmmap[simp]: "fmrestrict_fset A (fmmap f m) = fmmap f (fmrestrict_fset A m)" unfolding fmfilter_alt_defs by simp
lars@63885
   906
lars@63885
   907
lemma fmmap_subset[intro]: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmmap f m \<subseteq>\<^sub>f fmmap f n"
lars@63885
   908
by transfer' (auto simp: map_le_def)
lars@63885
   909
lars@66398
   910
lemma fmmap_fset_of_fmap: "fset_of_fmap (fmmap f m) = (\<lambda>(k, v). (k, f v)) |`| fset_of_fmap m"
lars@66398
   911
including fset.lifting
lars@66398
   912
by transfer' (auto simp: set_of_map_def)
lars@66398
   913
lars@66398
   914
lars@66398
   915
subsection \<open>@{const size} setup\<close>
lars@66398
   916
lars@66398
   917
definition size_fmap :: "('a \<Rightarrow> nat) \<Rightarrow> ('b \<Rightarrow> nat) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> nat" where
lars@66398
   918
[simp]: "size_fmap f g m = size_fset (\<lambda>(a, b). f a + g b) (fset_of_fmap m)"
lars@66398
   919
lars@66398
   920
instantiation fmap :: (type, type) size begin
lars@66398
   921
lars@66398
   922
definition size_fmap where
lars@66398
   923
size_fmap_overloaded_def: "size_fmap = Finite_Map.size_fmap (\<lambda>_. 0) (\<lambda>_. 0)"
lars@66398
   924
lars@66398
   925
instance ..
lars@66398
   926
lars@66398
   927
end
lars@66398
   928
lars@66398
   929
lemma size_fmap_overloaded_simps[simp]: "size x = size (fset_of_fmap x)"
lars@66398
   930
unfolding size_fmap_overloaded_def
lars@66398
   931
by simp
lars@66398
   932
lars@66398
   933
lemma fmap_size_o_map: "inj h \<Longrightarrow> size_fmap f g \<circ> fmmap h = size_fmap f (g \<circ> h)"
lars@66398
   934
  unfolding size_fmap_def
lars@66398
   935
  apply (auto simp: fun_eq_iff fmmap_fset_of_fmap)
lars@66398
   936
  apply (subst sum.reindex)
lars@66398
   937
  subgoal for m
lars@66398
   938
    using prod.inj_map[unfolded map_prod_def, of "\<lambda>x. x" h]
lars@66398
   939
    unfolding inj_on_def
lars@66398
   940
    by auto
lars@66398
   941
  subgoal
lars@66398
   942
    by (rule sum.cong) (auto split: prod.splits)
lars@66398
   943
  done
lars@66398
   944
lars@66398
   945
setup \<open>
lars@66398
   946
BNF_LFP_Size.register_size_global @{type_name fmap} @{const_name size_fmap}
lars@66398
   947
  @{thm size_fmap_overloaded_def} @{thms size_fmap_def size_fmap_overloaded_simps}
lars@66398
   948
  @{thms fmap_size_o_map}
lars@66398
   949
\<close>
lars@66398
   950
lars@63885
   951
lars@66269
   952
subsection \<open>Additional operations\<close>
lars@66269
   953
lars@66269
   954
lift_definition fmmap_keys :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'c) fmap" is
lars@66269
   955
  "\<lambda>f m a. map_option (f a) (m a)"
lars@66269
   956
unfolding dom_def
lars@66269
   957
by simp
lars@66269
   958
lars@66269
   959
lemma fmpred_fmmap_keys[simp]: "fmpred P (fmmap_keys f m) = fmpred (\<lambda>a b. P a (f a b)) m"
lars@66269
   960
by transfer' (auto simp: map_pred_def split: option.splits)
lars@66269
   961
lars@66269
   962
lemma fmdom_fmmap_keys[simp]: "fmdom (fmmap_keys f m) = fmdom m"
lars@66269
   963
including fset.lifting
lars@66269
   964
by transfer' auto
lars@66269
   965
lars@66269
   966
lemma fmlookup_fmmap_keys[simp]: "fmlookup (fmmap_keys f m) x = map_option (f x) (fmlookup m x)"
lars@66269
   967
by transfer' simp
lars@66269
   968
lars@66269
   969
lemma fmfilter_fmmap_keys[simp]: "fmfilter P (fmmap_keys f m) = fmmap_keys f (fmfilter P m)"
lars@66269
   970
by transfer' (auto simp: map_filter_def)
lars@66269
   971
lars@66269
   972
lemma fmdrop_fmmap_keys[simp]: "fmdrop a (fmmap_keys f m) = fmmap_keys f (fmdrop a m)"
lars@66269
   973
unfolding fmfilter_alt_defs by simp
lars@66269
   974
lars@66269
   975
lemma fmdrop_set_fmmap_keys[simp]: "fmdrop_set A (fmmap_keys f m) = fmmap_keys f (fmdrop_set A m)"
lars@66269
   976
unfolding fmfilter_alt_defs by simp
lars@66269
   977
lars@66269
   978
lemma fmdrop_fset_fmmap_keys[simp]: "fmdrop_fset A (fmmap_keys f m) = fmmap_keys f (fmdrop_fset A m)"
lars@66269
   979
unfolding fmfilter_alt_defs by simp
lars@66269
   980
lars@66269
   981
lemma fmrestrict_set_fmmap_keys[simp]: "fmrestrict_set A (fmmap_keys f m) = fmmap_keys f (fmrestrict_set A m)"
lars@66269
   982
unfolding fmfilter_alt_defs by simp
lars@66269
   983
lars@66269
   984
lemma fmrestrict_fset_fmmap_keys[simp]: "fmrestrict_fset A (fmmap_keys f m) = fmmap_keys f (fmrestrict_fset A m)"
lars@66269
   985
unfolding fmfilter_alt_defs by simp
lars@66269
   986
lars@66269
   987
lemma fmmap_keys_subset[intro]: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmmap_keys f m \<subseteq>\<^sub>f fmmap_keys f n"
lars@66269
   988
by transfer' (auto simp: map_le_def dom_def)
lars@66269
   989
lars@66269
   990
lars@66269
   991
subsection \<open>Lifting/transfer setup\<close>
lars@66269
   992
lars@66269
   993
context includes lifting_syntax begin
lars@66269
   994
lars@66269
   995
lemma fmempty_transfer[simp, intro, transfer_rule]: "fmrel P fmempty fmempty"
lars@66269
   996
by transfer auto
lars@66269
   997
lars@66269
   998
lemma fmadd_transfer[transfer_rule]:
lars@66269
   999
  "(fmrel P ===> fmrel P ===> fmrel P) fmadd fmadd"
lars@66269
  1000
  by (intro fmrel_addI rel_funI)
lars@66269
  1001
lars@66269
  1002
lemma fmupd_transfer[transfer_rule]:
nipkow@67399
  1003
  "((=) ===> P ===> fmrel P ===> fmrel P) fmupd fmupd"
lars@66269
  1004
  by auto
lars@66269
  1005
lars@66269
  1006
end
lars@66269
  1007
lars@66274
  1008
lars@66274
  1009
subsection \<open>View as datatype\<close>
lars@66274
  1010
lars@66274
  1011
lemma fmap_distinct[simp]:
lars@66274
  1012
  "fmempty \<noteq> fmupd k v m"
lars@66274
  1013
  "fmupd k v m \<noteq> fmempty"
lars@66274
  1014
by (transfer'; auto simp: map_upd_def fun_eq_iff)+
lars@66274
  1015
lars@66274
  1016
lifting_update fmap.lifting
lars@66274
  1017
lars@66274
  1018
lemma fmap_exhaust[case_names fmempty fmupd, cases type: fmap]:
lars@66274
  1019
  assumes fmempty: "m = fmempty \<Longrightarrow> P"
lars@66274
  1020
  assumes fmupd: "\<And>x y m'. m = fmupd x y m' \<Longrightarrow> x |\<notin>| fmdom m' \<Longrightarrow> P"
lars@66274
  1021
  shows "P"
lars@66274
  1022
using assms including fmap.lifting fset.lifting
lars@66274
  1023
proof transfer
lars@66274
  1024
  fix m P
lars@66274
  1025
  assume "finite (dom m)"
lars@66274
  1026
  assume empty: P if "m = Map.empty"
lars@66274
  1027
  assume map_upd: P if "finite (dom m')" "m = map_upd x y m'" "x \<notin> dom m'" for x y m'
lars@66274
  1028
lars@66274
  1029
  show P
lars@66274
  1030
    proof (cases "m = Map.empty")
lars@66274
  1031
      case True thus ?thesis using empty by simp
lars@66274
  1032
    next
lars@66274
  1033
      case False
lars@66274
  1034
      hence "dom m \<noteq> {}" by simp
lars@66274
  1035
      then obtain x where "x \<in> dom m" by blast
lars@66274
  1036
lars@66274
  1037
      let ?m' = "map_drop x m"
lars@66274
  1038
lars@66274
  1039
      show ?thesis
lars@66274
  1040
        proof (rule map_upd)
lars@66274
  1041
          show "finite (dom ?m')"
lars@66274
  1042
            using \<open>finite (dom m)\<close>
lars@66274
  1043
            unfolding map_drop_def
lars@66274
  1044
            by auto
lars@66274
  1045
        next
lars@66274
  1046
          show "m = map_upd x (the (m x)) ?m'"
lars@66274
  1047
            using \<open>x \<in> dom m\<close> unfolding map_drop_def map_filter_def map_upd_def
lars@66274
  1048
            by auto
lars@66274
  1049
        next
lars@66274
  1050
          show "x \<notin> dom ?m'"
lars@66274
  1051
            unfolding map_drop_def map_filter_def
lars@66274
  1052
            by auto
lars@66274
  1053
        qed
lars@66274
  1054
    qed
lars@66274
  1055
qed
lars@66274
  1056
lars@66274
  1057
lemma fmap_induct[case_names fmempty fmupd, induct type: fmap]:
lars@66274
  1058
  assumes "P fmempty"
lars@66274
  1059
  assumes "(\<And>x y m. P m \<Longrightarrow> fmlookup m x = None \<Longrightarrow> P (fmupd x y m))"
lars@66274
  1060
  shows "P m"
lars@66274
  1061
proof (induction "fmdom m" arbitrary: m rule: fset_induct_stronger)
lars@66274
  1062
  case empty
lars@66274
  1063
  hence "m = fmempty"
lars@66274
  1064
    by (metis fmrestrict_fset_dom fmrestrict_fset_null)
lars@66274
  1065
  with assms show ?case
lars@66274
  1066
    by simp
lars@66274
  1067
next
lars@66274
  1068
  case (insert x S)
lars@66274
  1069
  hence "S = fmdom (fmdrop x m)"
lars@66274
  1070
    by auto
lars@66274
  1071
  with insert have "P (fmdrop x m)"
lars@66274
  1072
    by auto
lars@66274
  1073
lars@66274
  1074
  have "x |\<in>| fmdom m"
lars@66274
  1075
    using insert by auto
lars@66274
  1076
  then obtain y where "fmlookup m x = Some y"
lars@66274
  1077
    by auto
lars@66274
  1078
  hence "m = fmupd x y (fmdrop x m)"
lars@66274
  1079
    by (auto intro: fmap_ext)
lars@66274
  1080
lars@66274
  1081
  show ?case
lars@66274
  1082
    apply (subst \<open>m = _\<close>)
lars@66274
  1083
    apply (rule assms)
lars@66274
  1084
    apply fact
lars@66274
  1085
    apply simp
lars@66274
  1086
    done
lars@66274
  1087
qed
lars@66274
  1088
lars@66274
  1089
lars@63885
  1090
subsection \<open>Code setup\<close>
lars@63885
  1091
lars@63885
  1092
instantiation fmap :: (type, equal) equal begin
lars@63885
  1093
lars@63885
  1094
definition "equal_fmap \<equiv> fmrel HOL.equal"
lars@63885
  1095
lars@63885
  1096
instance proof
lars@63885
  1097
  fix m n :: "('a, 'b) fmap"
nipkow@67399
  1098
  have "fmrel (=) m n \<longleftrightarrow> (m = n)"
lars@63885
  1099
    by transfer' (simp add: option.rel_eq rel_fun_eq)
lars@64180
  1100
  then show "equal_class.equal m n \<longleftrightarrow> (m = n)"
lars@63885
  1101
    unfolding equal_fmap_def
lars@63885
  1102
    by (simp add: equal_eq[abs_def])
lars@63885
  1103
qed
lars@63885
  1104
lars@63885
  1105
end
lars@63885
  1106
lars@63885
  1107
lemma fBall_alt_def: "fBall S P \<longleftrightarrow> (\<forall>x. x |\<in>| S \<longrightarrow> P x)"
lars@63885
  1108
by force
lars@63885
  1109
lars@63885
  1110
lemma fmrel_code:
lars@63885
  1111
  "fmrel R m n \<longleftrightarrow>
lars@63885
  1112
    fBall (fmdom m) (\<lambda>x. rel_option R (fmlookup m x) (fmlookup n x)) \<and>
lars@63885
  1113
    fBall (fmdom n) (\<lambda>x. rel_option R (fmlookup m x) (fmlookup n x))"
lars@63885
  1114
unfolding fmrel_iff fmlookup_dom_iff fBall_alt_def
lars@63885
  1115
by (metis option.collapse option.rel_sel)
lars@63885
  1116
lars@66291
  1117
lemmas [code] =
lars@63885
  1118
  fmrel_code
lars@63885
  1119
  fmran'_alt_def
lars@63885
  1120
  fmdom'_alt_def
lars@63885
  1121
  fmfilter_alt_defs
lars@63885
  1122
  pred_fmap_fmpred
lars@63885
  1123
  fmsubset_alt_def
lars@63885
  1124
  fmupd_alt_def
lars@63885
  1125
  fmrel_on_fset_alt_def
lars@63885
  1126
  fmpred_alt_def
lars@63885
  1127
lars@63885
  1128
lars@63885
  1129
code_datatype fmap_of_list
lars@63885
  1130
quickcheck_generator fmap constructors: fmap_of_list
lars@63885
  1131
lars@63885
  1132
context includes fset.lifting begin
lars@63885
  1133
lars@66269
  1134
lemma fmlookup_of_list[code]: "fmlookup (fmap_of_list m) = map_of m"
lars@63885
  1135
by transfer simp
lars@63885
  1136
lars@66269
  1137
lemma fmempty_of_list[code]: "fmempty = fmap_of_list []"
lars@63885
  1138
by transfer simp
lars@63885
  1139
lars@66269
  1140
lemma fmran_of_list[code]: "fmran (fmap_of_list m) = snd |`| fset_of_list (AList.clearjunk m)"
lars@63885
  1141
by transfer (auto simp: ran_map_of)
lars@63885
  1142
lars@66269
  1143
lemma fmdom_of_list[code]: "fmdom (fmap_of_list m) = fst |`| fset_of_list m"
lars@63885
  1144
by transfer (auto simp: dom_map_of_conv_image_fst)
lars@63885
  1145
lars@66269
  1146
lemma fmfilter_of_list[code]: "fmfilter P (fmap_of_list m) = fmap_of_list (filter (\<lambda>(k, _). P k) m)"
lars@63885
  1147
by transfer' auto
lars@63885
  1148
lars@66269
  1149
lemma fmadd_of_list[code]: "fmap_of_list m ++\<^sub>f fmap_of_list n = fmap_of_list (AList.merge m n)"
lars@63885
  1150
by transfer (simp add: merge_conv')
lars@63885
  1151
lars@66269
  1152
lemma fmmap_of_list[code]: "fmmap f (fmap_of_list m) = fmap_of_list (map (apsnd f) m)"
lars@63885
  1153
apply transfer
lars@63885
  1154
apply (subst map_of_map[symmetric])
lars@63885
  1155
apply (auto simp: apsnd_def map_prod_def)
lars@63885
  1156
done
lars@63885
  1157
lars@66269
  1158
lemma fmmap_keys_of_list[code]: "fmmap_keys f (fmap_of_list m) = fmap_of_list (map (\<lambda>(a, b). (a, f a b)) m)"
lars@66269
  1159
apply transfer
lars@66269
  1160
subgoal for f m by (induction m) (auto simp: apsnd_def map_prod_def fun_eq_iff)
lars@66269
  1161
done
lars@66269
  1162
lars@63885
  1163
end
lars@63885
  1164
lars@66267
  1165
lars@66267
  1166
subsection \<open>Instances\<close>
lars@66267
  1167
lars@66267
  1168
lemma exists_map_of:
lars@66267
  1169
  assumes "finite (dom m)" shows "\<exists>xs. map_of xs = m"
lars@66267
  1170
  using assms
lars@66267
  1171
proof (induction "dom m" arbitrary: m)
lars@66267
  1172
  case empty
lars@66267
  1173
  hence "m = Map.empty"
lars@66267
  1174
    by auto
lars@66267
  1175
  moreover have "map_of [] = Map.empty"
lars@66267
  1176
    by simp
lars@66267
  1177
  ultimately show ?case
lars@66267
  1178
    by blast
lars@66267
  1179
next
lars@66267
  1180
  case (insert x F)
lars@66267
  1181
  hence "F = dom (map_drop x m)"
lars@66267
  1182
    unfolding map_drop_def map_filter_def dom_def by auto
lars@66267
  1183
  with insert have "\<exists>xs'. map_of xs' = map_drop x m"
lars@66267
  1184
    by auto
lars@66267
  1185
  then obtain xs' where "map_of xs' = map_drop x m"
lars@66267
  1186
    ..
lars@66267
  1187
  moreover obtain y where "m x = Some y"
lars@66267
  1188
    using insert unfolding dom_def by blast
lars@66267
  1189
  ultimately have "map_of ((x, y) # xs') = m"
lars@66267
  1190
    using \<open>insert x F = dom m\<close>
lars@66267
  1191
    unfolding map_drop_def map_filter_def
lars@66267
  1192
    by auto
lars@66267
  1193
  thus ?case
lars@66267
  1194
    ..
lars@66267
  1195
qed
lars@66267
  1196
lars@66267
  1197
lemma exists_fmap_of_list: "\<exists>xs. fmap_of_list xs = m"
lars@66267
  1198
by transfer (rule exists_map_of)
lars@66267
  1199
lars@66267
  1200
lemma fmap_of_list_surj[simp, intro]: "surj fmap_of_list"
lars@66267
  1201
proof -
lars@66267
  1202
  have "x \<in> range fmap_of_list" for x :: "('a, 'b) fmap"
lars@66267
  1203
    unfolding image_iff
lars@66267
  1204
    using exists_fmap_of_list by (metis UNIV_I)
lars@66267
  1205
  thus ?thesis by auto
lars@66267
  1206
qed
lars@66267
  1207
lars@66267
  1208
instance fmap :: (countable, countable) countable
lars@66267
  1209
proof
lars@66267
  1210
  obtain to_nat :: "('a \<times> 'b) list \<Rightarrow> nat" where "inj to_nat"
lars@66267
  1211
    by (metis ex_inj)
lars@66267
  1212
  moreover have "inj (inv fmap_of_list)"
lars@66267
  1213
    using fmap_of_list_surj by (rule surj_imp_inj_inv)
lars@66267
  1214
  ultimately have "inj (to_nat \<circ> inv fmap_of_list)"
lars@66267
  1215
    by (rule inj_comp)
lars@66267
  1216
  thus "\<exists>to_nat::('a, 'b) fmap \<Rightarrow> nat. inj to_nat"
lars@66267
  1217
    by auto
lars@66267
  1218
qed
lars@66267
  1219
lars@66282
  1220
instance fmap :: (finite, finite) finite
lars@66282
  1221
proof
lars@66282
  1222
  show "finite (UNIV :: ('a, 'b) fmap set)"
lars@66282
  1223
    by (rule finite_imageD) auto
lars@66282
  1224
qed
lars@66282
  1225
lars@63885
  1226
lifting_update fmap.lifting
lars@63885
  1227
lifting_forget fmap.lifting
lars@63885
  1228
nipkow@67399
  1229
end