src/HOL/Library/Finite_Map.thy
author Lars Hupel <lars.hupel@mytum.de>
Sun Jul 16 23:47:21 2017 +0200 (2017-07-16)
changeset 66282 e5ba49a72ab9
parent 66274 be8d3819c21c
child 66291 f32968e099d5
permissions -rw-r--r--
fmap is finite
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
lars@63885
    16
"rel_map f \<equiv> op = ===> 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
lars@63885
    60
lemma dom_transfer[transfer_rule]: "(rel_map A ===> op =) 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]:
lars@63885
    79
  "(op = ===> 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]:
lars@63885
    94
  "(op = ===> 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]:
lars@63885
   114
  "(op = ===> 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]:
lars@63885
   122
  "(op = ===> 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]:
lars@63885
   130
  "(op = ===> 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]:
lars@63885
   135
  "(rel_map A ===> rel_map A ===> rel_map A) op ++ op ++"
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]:
lars@63885
   143
  "((op = ===> A ===> op =) ===> rel_map A ===> op =) 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
lars@63885
   152
  shows "(list_all2 (rel_prod op = 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@63885
   608
lift_definition fmap_of_list :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) fmap"
lars@63885
   609
  is map_of
lars@63885
   610
  parametric map_of_transfer
lars@63885
   611
by (rule finite_dom_map_of)
lars@63885
   612
lars@63885
   613
lemma fmap_of_list_simps[simp]:
lars@63885
   614
  "fmap_of_list [] = fmempty"
lars@63885
   615
  "fmap_of_list ((k, v) # kvs) = fmupd k v (fmap_of_list kvs)"
lars@63885
   616
by (transfer, simp add: map_upd_def)+
lars@63885
   617
lars@63885
   618
lemma fmap_of_list_app[simp]: "fmap_of_list (xs @ ys) = fmap_of_list ys ++\<^sub>f fmap_of_list xs"
lars@63885
   619
by transfer' simp
lars@63885
   620
lars@63885
   621
lemma fmupd_alt_def: "fmupd k v m = m ++\<^sub>f fmap_of_list [(k, v)]"
lars@63885
   622
by transfer' (auto simp: map_upd_def)
lars@63885
   623
lars@63885
   624
lemma fmpred_of_list[intro]:
lars@63885
   625
  assumes "\<And>k v. (k, v) \<in> set xs \<Longrightarrow> P k v"
lars@63885
   626
  shows "fmpred P (fmap_of_list xs)"
lars@63885
   627
using assms
lars@63885
   628
by (induction xs) (transfer'; auto simp: map_pred_def)+
lars@63885
   629
lars@63885
   630
lemma fmap_of_list_SomeD: "fmlookup (fmap_of_list xs) k = Some v \<Longrightarrow> (k, v) \<in> set xs"
lars@63885
   631
by transfer' (auto dest: map_of_SomeD)
lars@63885
   632
lars@66269
   633
lemma fmdom_fmap_of_list[simp]: "fmdom (fmap_of_list xs) = fset_of_list (map fst xs)"
lars@66269
   634
apply transfer'
lars@66269
   635
apply (subst dom_map_of_conv_image_fst)
lars@66269
   636
apply auto
lars@66269
   637
done
lars@66269
   638
lars@63885
   639
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
   640
  is rel_map_on_set
lars@63885
   641
.
lars@63885
   642
lars@63885
   643
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
   644
by transfer' (auto simp: rel_map_on_set_def eq_onp_def rel_fun_def)
lars@63885
   645
lars@64181
   646
lemma fmrel_on_fsetI[intro]:
lars@63885
   647
  assumes "\<And>x. x |\<in>| S \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   648
  shows "fmrel_on_fset S P m n"
lars@63885
   649
using assms
lars@63885
   650
unfolding fmrel_on_fset_alt_def by auto
lars@63885
   651
lars@63885
   652
lemma fmrel_on_fset_mono[mono]: "R \<le> Q \<Longrightarrow> fmrel_on_fset S R \<le> fmrel_on_fset S Q"
lars@63885
   653
unfolding fmrel_on_fset_alt_def[abs_def]
lars@63885
   654
apply (intro le_funI fBall_mono)
lars@63885
   655
using option.rel_mono by auto
lars@63885
   656
lars@63885
   657
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
   658
unfolding fmrel_on_fset_alt_def
lars@63885
   659
by auto
lars@63885
   660
lars@63885
   661
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
   662
unfolding fmrel_on_fset_alt_def
lars@63885
   663
by auto
lars@63885
   664
lars@66274
   665
lemma fmrel_on_fset_unionI:
lars@66274
   666
  "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
   667
unfolding fmrel_on_fset_alt_def
lars@66274
   668
by auto
lars@66274
   669
lars@66274
   670
lemma fmrel_on_fset_updateI:
lars@66274
   671
  assumes "fmrel_on_fset S P m n" "P v\<^sub>1 v\<^sub>2"
lars@66274
   672
  shows "fmrel_on_fset (finsert k S) P (fmupd k v\<^sub>1 m) (fmupd k v\<^sub>2 n)"
lars@66274
   673
using assms
lars@66274
   674
unfolding fmrel_on_fset_alt_def
lars@66274
   675
by auto
lars@66274
   676
lars@63885
   677
end
lars@63885
   678
lars@63885
   679
lars@63885
   680
subsection \<open>BNF setup\<close>
lars@63885
   681
lars@63885
   682
lift_bnf ('a, fmran': 'b) fmap [wits: Map.empty]
lars@63885
   683
  for map: fmmap
lars@63885
   684
      rel: fmrel
lars@63885
   685
  by auto
lars@63885
   686
lars@66269
   687
declare fmap.pred_mono[mono]
lars@66268
   688
lars@63885
   689
context includes lifting_syntax begin
lars@63885
   690
lars@63885
   691
lemma fmmap_transfer[transfer_rule]:
lars@63885
   692
  "(op = ===> pcr_fmap op = op = ===> pcr_fmap op = op =) (\<lambda>f. op \<circ> (map_option f)) fmmap"
lars@64180
   693
  unfolding fmmap_def
lars@64180
   694
  by (rule rel_funI ext)+ (auto simp: fmap.Abs_fmap_inverse fmap.pcr_cr_eq cr_fmap_def)
lars@63885
   695
lars@63885
   696
lemma fmran'_transfer[transfer_rule]:
lars@63885
   697
  "(pcr_fmap op = op = ===> op =) (\<lambda>x. UNION (range x) set_option) fmran'"
lars@64180
   698
  unfolding fmran'_def fmap.pcr_cr_eq cr_fmap_def by fastforce
lars@63885
   699
lars@63885
   700
lemma fmrel_transfer[transfer_rule]:
lars@63885
   701
  "(op = ===> pcr_fmap op = op = ===> pcr_fmap op = op = ===> op =) rel_map fmrel"
lars@64180
   702
  unfolding fmrel_def fmap.pcr_cr_eq cr_fmap_def vimage2p_def by fastforce
lars@63885
   703
lars@63885
   704
end
lars@63885
   705
lars@63885
   706
lars@66268
   707
lemma fmran'_alt_def: "fmran' m = fset (fmran m)"
lars@63885
   708
including fset.lifting
lars@63885
   709
by transfer' (auto simp: ran_def fun_eq_iff)
lars@63885
   710
lars@66268
   711
lemma fmlookup_ran'_iff: "y \<in> fmran' m \<longleftrightarrow> (\<exists>x. fmlookup m x = Some y)"
lars@66268
   712
by transfer' (auto simp: ran_def)
lars@66268
   713
lars@66268
   714
lemma fmran'I: "fmlookup m x = Some y \<Longrightarrow> y \<in> fmran' m" by (auto simp: fmlookup_ran'_iff)
lars@66268
   715
lars@66268
   716
lemma fmran'E[elim]:
lars@66268
   717
  assumes "y \<in> fmran' m"
lars@66268
   718
  obtains x where "fmlookup m x = Some y"
lars@66268
   719
using assms by (auto simp: fmlookup_ran'_iff)
lars@63885
   720
lars@63885
   721
lemma fmrel_iff: "fmrel R m n \<longleftrightarrow> (\<forall>x. rel_option R (fmlookup m x) (fmlookup n x))"
lars@63885
   722
by transfer' (auto simp: rel_fun_def)
lars@63885
   723
lars@63885
   724
lemma fmrelI[intro]:
lars@63885
   725
  assumes "\<And>x. rel_option R (fmlookup m x) (fmlookup n x)"
lars@63885
   726
  shows "fmrel R m n"
lars@63885
   727
using assms
lars@63885
   728
by transfer' auto
lars@63885
   729
lars@63885
   730
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
   731
by transfer' (auto simp: map_upd_def rel_fun_def)
lars@63885
   732
lars@63885
   733
lemma fmrelD[dest]: "fmrel P m n \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   734
by transfer' (auto simp: rel_fun_def)
lars@63885
   735
lars@63885
   736
lemma fmrel_addI[intro]:
lars@63885
   737
  assumes "fmrel P m n" "fmrel P a b"
lars@63885
   738
  shows "fmrel P (m ++\<^sub>f a) (n ++\<^sub>f b)"
lars@63885
   739
using assms
lars@63885
   740
apply transfer'
lars@63885
   741
apply (auto simp: rel_fun_def map_add_def)
lars@63885
   742
by (metis option.case_eq_if option.collapse option.rel_sel)
lars@63885
   743
lars@63885
   744
lemma fmrel_cases[consumes 1]:
lars@63885
   745
  assumes "fmrel P m n"
lars@63885
   746
  obtains (none) "fmlookup m x = None" "fmlookup n x = None"
lars@63885
   747
        | (some) a b where "fmlookup m x = Some a" "fmlookup n x = Some b" "P a b"
lars@63885
   748
proof -
lars@63885
   749
  from assms have "rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   750
    by auto
lars@64180
   751
  then show thesis
lars@63885
   752
    using none some
lars@63885
   753
    by (cases rule: option.rel_cases) auto
lars@63885
   754
qed
lars@63885
   755
lars@63885
   756
lemma fmrel_filter[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmfilter Q m) (fmfilter Q n)"
lars@63885
   757
unfolding fmrel_iff by auto
lars@63885
   758
lars@63885
   759
lemma fmrel_drop[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop a m) (fmdrop a n)"
lars@63885
   760
  unfolding fmfilter_alt_defs by blast
lars@63885
   761
lars@63885
   762
lemma fmrel_drop_set[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop_set A m) (fmdrop_set A n)"
lars@63885
   763
  unfolding fmfilter_alt_defs by blast
lars@63885
   764
lars@63885
   765
lemma fmrel_drop_fset[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop_fset A m) (fmdrop_fset A n)"
lars@63885
   766
  unfolding fmfilter_alt_defs by blast
lars@63885
   767
lars@63885
   768
lemma fmrel_restrict_set[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmrestrict_set A m) (fmrestrict_set A n)"
lars@63885
   769
  unfolding fmfilter_alt_defs by blast
lars@63885
   770
lars@63885
   771
lemma fmrel_restrict_fset[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmrestrict_fset A m) (fmrestrict_fset A n)"
lars@63885
   772
  unfolding fmfilter_alt_defs by blast
lars@63885
   773
lars@66274
   774
lemma fmrel_on_fset_fmrel_restrict:
lars@66274
   775
  "fmrel_on_fset S P m n \<longleftrightarrow> fmrel P (fmrestrict_fset S m) (fmrestrict_fset S n)"
lars@66274
   776
unfolding fmrel_on_fset_alt_def fmrel_iff
lars@66274
   777
by auto
lars@66274
   778
lars@66274
   779
lemma fmrel_on_fset_refl_strong:
lars@66274
   780
  assumes "\<And>x y. x |\<in>| S \<Longrightarrow> fmlookup m x = Some y \<Longrightarrow> P y y"
lars@66274
   781
  shows "fmrel_on_fset S P m m"
lars@66274
   782
unfolding fmrel_on_fset_fmrel_restrict fmrel_iff
lars@66274
   783
using assms
lars@66274
   784
by (simp add: option.rel_sel)
lars@66274
   785
lars@66274
   786
lemma fmrel_on_fset_addI:
lars@66274
   787
  assumes "fmrel_on_fset S P m n" "fmrel_on_fset S P a b"
lars@66274
   788
  shows "fmrel_on_fset S P (m ++\<^sub>f a) (n ++\<^sub>f b)"
lars@66274
   789
using assms
lars@66274
   790
unfolding fmrel_on_fset_fmrel_restrict
lars@66274
   791
by auto
lars@66274
   792
lars@66274
   793
lemma fmrel_fmdom_eq:
lars@66274
   794
  assumes "fmrel P x y"
lars@66274
   795
  shows "fmdom x = fmdom y"
lars@66274
   796
proof -
lars@66274
   797
  have "a |\<in>| fmdom x \<longleftrightarrow> a |\<in>| fmdom y" for a
lars@66274
   798
    proof -
lars@66274
   799
      have "rel_option P (fmlookup x a) (fmlookup y a)"
lars@66274
   800
        using assms by (simp add: fmrel_iff)
lars@66274
   801
      thus ?thesis
lars@66274
   802
        by cases (auto intro: fmdomI)
lars@66274
   803
    qed
lars@66274
   804
  thus ?thesis
lars@66274
   805
    by auto
lars@66274
   806
qed
lars@66274
   807
lars@66274
   808
lemma fmrel_fmdom'_eq: "fmrel P x y \<Longrightarrow> fmdom' x = fmdom' y"
lars@66274
   809
unfolding fmdom'_alt_def
lars@66274
   810
by (metis fmrel_fmdom_eq)
lars@66274
   811
lars@66274
   812
lemma fmrel_rel_fmran:
lars@66274
   813
  assumes "fmrel P x y"
lars@66274
   814
  shows "rel_fset P (fmran x) (fmran y)"
lars@66274
   815
proof -
lars@66274
   816
  {
lars@66274
   817
    fix b
lars@66274
   818
    assume "b |\<in>| fmran x"
lars@66274
   819
    then obtain a where "fmlookup x a = Some b"
lars@66274
   820
      by auto
lars@66274
   821
    moreover have "rel_option P (fmlookup x a) (fmlookup y a)"
lars@66274
   822
      using assms by auto
lars@66274
   823
    ultimately have "\<exists>b'. b' |\<in>| fmran y \<and> P b b'"
lars@66274
   824
      by (metis option_rel_Some1 fmranI)
lars@66274
   825
  }
lars@66274
   826
  moreover
lars@66274
   827
  {
lars@66274
   828
    fix b
lars@66274
   829
    assume "b |\<in>| fmran y"
lars@66274
   830
    then obtain a where "fmlookup y a = Some b"
lars@66274
   831
      by auto
lars@66274
   832
    moreover have "rel_option P (fmlookup x a) (fmlookup y a)"
lars@66274
   833
      using assms by auto
lars@66274
   834
    ultimately have "\<exists>b'. b' |\<in>| fmran x \<and> P b' b"
lars@66274
   835
      by (metis option_rel_Some2 fmranI)
lars@66274
   836
  }
lars@66274
   837
  ultimately show ?thesis
lars@66274
   838
    unfolding rel_fset_alt_def
lars@66274
   839
    by auto
lars@66274
   840
qed
lars@66274
   841
lars@66274
   842
lemma fmrel_rel_fmran': "fmrel P x y \<Longrightarrow> rel_set P (fmran' x) (fmran' y)"
lars@66274
   843
unfolding fmran'_alt_def
lars@66274
   844
by (metis fmrel_rel_fmran rel_fset_fset)
lars@66274
   845
lars@63885
   846
lemma pred_fmap_fmpred[simp]: "pred_fmap P = fmpred (\<lambda>_. P)"
lars@63885
   847
unfolding fmap.pred_set fmran'_alt_def
lars@63885
   848
including fset.lifting
lars@63885
   849
apply transfer'
lars@63885
   850
apply (rule ext)
lars@63885
   851
apply (auto simp: map_pred_def ran_def split: option.splits dest: )
lars@63885
   852
done
lars@63885
   853
lars@63885
   854
lemma pred_fmap_id[simp]: "pred_fmap id (fmmap f m) \<longleftrightarrow> pred_fmap f m"
lars@63885
   855
unfolding fmap.pred_set fmap.set_map
lars@63885
   856
by simp
lars@63885
   857
lars@66274
   858
lemma pred_fmapD: "pred_fmap P m \<Longrightarrow> x |\<in>| fmran m \<Longrightarrow> P x"
lars@66274
   859
by auto
lars@66274
   860
lars@63885
   861
lemma fmlookup_map[simp]: "fmlookup (fmmap f m) x = map_option f (fmlookup m x)"
lars@64180
   862
by transfer' auto
lars@63885
   863
lars@63885
   864
lemma fmpred_map[simp]: "fmpred P (fmmap f m) \<longleftrightarrow> fmpred (\<lambda>k v. P k (f v)) m"
lars@63885
   865
unfolding fmpred_iff pred_fmap_def fmap.set_map
lars@63885
   866
by auto
lars@63885
   867
lars@63885
   868
lemma fmpred_id[simp]: "fmpred (\<lambda>_. id) (fmmap f m) \<longleftrightarrow> fmpred (\<lambda>_. f) m"
lars@63885
   869
by simp
lars@63885
   870
lars@63885
   871
lemma fmmap_add[simp]: "fmmap f (m ++\<^sub>f n) = fmmap f m ++\<^sub>f fmmap f n"
lars@63885
   872
by transfer' (auto simp: map_add_def fun_eq_iff split: option.splits)
lars@63885
   873
lars@63885
   874
lemma fmmap_empty[simp]: "fmmap f fmempty = fmempty"
lars@63885
   875
by transfer auto
lars@63885
   876
lars@63885
   877
lemma fmdom_map[simp]: "fmdom (fmmap f m) = fmdom m"
lars@63885
   878
including fset.lifting
lars@63885
   879
by transfer' simp
lars@63885
   880
lars@63885
   881
lemma fmdom'_map[simp]: "fmdom' (fmmap f m) = fmdom' m"
lars@63885
   882
by transfer' simp
lars@63885
   883
lars@66269
   884
lemma fmran_fmmap[simp]: "fmran (fmmap f m) = f |`| fmran m"
lars@66269
   885
including fset.lifting
lars@66269
   886
by transfer' (auto simp: ran_def)
lars@66269
   887
lars@66269
   888
lemma fmran'_fmmap[simp]: "fmran' (fmmap f m) = f ` fmran' m"
lars@66269
   889
by transfer' (auto simp: ran_def)
lars@66269
   890
lars@63885
   891
lemma fmfilter_fmmap[simp]: "fmfilter P (fmmap f m) = fmmap f (fmfilter P m)"
lars@63885
   892
by transfer' (auto simp: map_filter_def)
lars@63885
   893
lars@63885
   894
lemma fmdrop_fmmap[simp]: "fmdrop a (fmmap f m) = fmmap f (fmdrop a m)" unfolding fmfilter_alt_defs by simp
lars@63885
   895
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
   896
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
   897
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
   898
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
   899
lars@63885
   900
lemma fmmap_subset[intro]: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmmap f m \<subseteq>\<^sub>f fmmap f n"
lars@63885
   901
by transfer' (auto simp: map_le_def)
lars@63885
   902
lars@63885
   903
lars@66269
   904
subsection \<open>Additional operations\<close>
lars@66269
   905
lars@66269
   906
lift_definition fmmap_keys :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'c) fmap" is
lars@66269
   907
  "\<lambda>f m a. map_option (f a) (m a)"
lars@66269
   908
unfolding dom_def
lars@66269
   909
by simp
lars@66269
   910
lars@66269
   911
lemma fmpred_fmmap_keys[simp]: "fmpred P (fmmap_keys f m) = fmpred (\<lambda>a b. P a (f a b)) m"
lars@66269
   912
by transfer' (auto simp: map_pred_def split: option.splits)
lars@66269
   913
lars@66269
   914
lemma fmdom_fmmap_keys[simp]: "fmdom (fmmap_keys f m) = fmdom m"
lars@66269
   915
including fset.lifting
lars@66269
   916
by transfer' auto
lars@66269
   917
lars@66269
   918
lemma fmlookup_fmmap_keys[simp]: "fmlookup (fmmap_keys f m) x = map_option (f x) (fmlookup m x)"
lars@66269
   919
by transfer' simp
lars@66269
   920
lars@66269
   921
lemma fmfilter_fmmap_keys[simp]: "fmfilter P (fmmap_keys f m) = fmmap_keys f (fmfilter P m)"
lars@66269
   922
by transfer' (auto simp: map_filter_def)
lars@66269
   923
lars@66269
   924
lemma fmdrop_fmmap_keys[simp]: "fmdrop a (fmmap_keys f m) = fmmap_keys f (fmdrop a m)"
lars@66269
   925
unfolding fmfilter_alt_defs by simp
lars@66269
   926
lars@66269
   927
lemma fmdrop_set_fmmap_keys[simp]: "fmdrop_set A (fmmap_keys f m) = fmmap_keys f (fmdrop_set A m)"
lars@66269
   928
unfolding fmfilter_alt_defs by simp
lars@66269
   929
lars@66269
   930
lemma fmdrop_fset_fmmap_keys[simp]: "fmdrop_fset A (fmmap_keys f m) = fmmap_keys f (fmdrop_fset A m)"
lars@66269
   931
unfolding fmfilter_alt_defs by simp
lars@66269
   932
lars@66269
   933
lemma fmrestrict_set_fmmap_keys[simp]: "fmrestrict_set A (fmmap_keys f m) = fmmap_keys f (fmrestrict_set A m)"
lars@66269
   934
unfolding fmfilter_alt_defs by simp
lars@66269
   935
lars@66269
   936
lemma fmrestrict_fset_fmmap_keys[simp]: "fmrestrict_fset A (fmmap_keys f m) = fmmap_keys f (fmrestrict_fset A m)"
lars@66269
   937
unfolding fmfilter_alt_defs by simp
lars@66269
   938
lars@66269
   939
lemma fmmap_keys_subset[intro]: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmmap_keys f m \<subseteq>\<^sub>f fmmap_keys f n"
lars@66269
   940
by transfer' (auto simp: map_le_def dom_def)
lars@66269
   941
lars@66269
   942
lars@66269
   943
subsection \<open>Lifting/transfer setup\<close>
lars@66269
   944
lars@66269
   945
context includes lifting_syntax begin
lars@66269
   946
lars@66269
   947
lemma fmempty_transfer[simp, intro, transfer_rule]: "fmrel P fmempty fmempty"
lars@66269
   948
by transfer auto
lars@66269
   949
lars@66269
   950
lemma fmadd_transfer[transfer_rule]:
lars@66269
   951
  "(fmrel P ===> fmrel P ===> fmrel P) fmadd fmadd"
lars@66269
   952
  by (intro fmrel_addI rel_funI)
lars@66269
   953
lars@66269
   954
lemma fmupd_transfer[transfer_rule]:
lars@66269
   955
  "(op = ===> P ===> fmrel P ===> fmrel P) fmupd fmupd"
lars@66269
   956
  by auto
lars@66269
   957
lars@66269
   958
end
lars@66269
   959
lars@66274
   960
lars@66274
   961
subsection \<open>View as datatype\<close>
lars@66274
   962
lars@66274
   963
lemma fmap_distinct[simp]:
lars@66274
   964
  "fmempty \<noteq> fmupd k v m"
lars@66274
   965
  "fmupd k v m \<noteq> fmempty"
lars@66274
   966
by (transfer'; auto simp: map_upd_def fun_eq_iff)+
lars@66274
   967
lars@66274
   968
lifting_update fmap.lifting
lars@66274
   969
lars@66274
   970
lemma fmap_exhaust[case_names fmempty fmupd, cases type: fmap]:
lars@66274
   971
  assumes fmempty: "m = fmempty \<Longrightarrow> P"
lars@66274
   972
  assumes fmupd: "\<And>x y m'. m = fmupd x y m' \<Longrightarrow> x |\<notin>| fmdom m' \<Longrightarrow> P"
lars@66274
   973
  shows "P"
lars@66274
   974
using assms including fmap.lifting fset.lifting
lars@66274
   975
proof transfer
lars@66274
   976
  fix m P
lars@66274
   977
  assume "finite (dom m)"
lars@66274
   978
  assume empty: P if "m = Map.empty"
lars@66274
   979
  assume map_upd: P if "finite (dom m')" "m = map_upd x y m'" "x \<notin> dom m'" for x y m'
lars@66274
   980
lars@66274
   981
  show P
lars@66274
   982
    proof (cases "m = Map.empty")
lars@66274
   983
      case True thus ?thesis using empty by simp
lars@66274
   984
    next
lars@66274
   985
      case False
lars@66274
   986
      hence "dom m \<noteq> {}" by simp
lars@66274
   987
      then obtain x where "x \<in> dom m" by blast
lars@66274
   988
lars@66274
   989
      let ?m' = "map_drop x m"
lars@66274
   990
lars@66274
   991
      show ?thesis
lars@66274
   992
        proof (rule map_upd)
lars@66274
   993
          show "finite (dom ?m')"
lars@66274
   994
            using \<open>finite (dom m)\<close>
lars@66274
   995
            unfolding map_drop_def
lars@66274
   996
            by auto
lars@66274
   997
        next
lars@66274
   998
          show "m = map_upd x (the (m x)) ?m'"
lars@66274
   999
            using \<open>x \<in> dom m\<close> unfolding map_drop_def map_filter_def map_upd_def
lars@66274
  1000
            by auto
lars@66274
  1001
        next
lars@66274
  1002
          show "x \<notin> dom ?m'"
lars@66274
  1003
            unfolding map_drop_def map_filter_def
lars@66274
  1004
            by auto
lars@66274
  1005
        qed
lars@66274
  1006
    qed
lars@66274
  1007
qed
lars@66274
  1008
lars@66274
  1009
lemma fmap_induct[case_names fmempty fmupd, induct type: fmap]:
lars@66274
  1010
  assumes "P fmempty"
lars@66274
  1011
  assumes "(\<And>x y m. P m \<Longrightarrow> fmlookup m x = None \<Longrightarrow> P (fmupd x y m))"
lars@66274
  1012
  shows "P m"
lars@66274
  1013
proof (induction "fmdom m" arbitrary: m rule: fset_induct_stronger)
lars@66274
  1014
  case empty
lars@66274
  1015
  hence "m = fmempty"
lars@66274
  1016
    by (metis fmrestrict_fset_dom fmrestrict_fset_null)
lars@66274
  1017
  with assms show ?case
lars@66274
  1018
    by simp
lars@66274
  1019
next
lars@66274
  1020
  case (insert x S)
lars@66274
  1021
  hence "S = fmdom (fmdrop x m)"
lars@66274
  1022
    by auto
lars@66274
  1023
  with insert have "P (fmdrop x m)"
lars@66274
  1024
    by auto
lars@66274
  1025
lars@66274
  1026
  have "x |\<in>| fmdom m"
lars@66274
  1027
    using insert by auto
lars@66274
  1028
  then obtain y where "fmlookup m x = Some y"
lars@66274
  1029
    by auto
lars@66274
  1030
  hence "m = fmupd x y (fmdrop x m)"
lars@66274
  1031
    by (auto intro: fmap_ext)
lars@66274
  1032
lars@66274
  1033
  show ?case
lars@66274
  1034
    apply (subst \<open>m = _\<close>)
lars@66274
  1035
    apply (rule assms)
lars@66274
  1036
    apply fact
lars@66274
  1037
    apply simp
lars@66274
  1038
    done
lars@66274
  1039
qed
lars@66274
  1040
lars@66274
  1041
lars@63885
  1042
subsection \<open>Code setup\<close>
lars@63885
  1043
lars@63885
  1044
instantiation fmap :: (type, equal) equal begin
lars@63885
  1045
lars@63885
  1046
definition "equal_fmap \<equiv> fmrel HOL.equal"
lars@63885
  1047
lars@63885
  1048
instance proof
lars@63885
  1049
  fix m n :: "('a, 'b) fmap"
lars@63885
  1050
  have "fmrel op = m n \<longleftrightarrow> (m = n)"
lars@63885
  1051
    by transfer' (simp add: option.rel_eq rel_fun_eq)
lars@64180
  1052
  then show "equal_class.equal m n \<longleftrightarrow> (m = n)"
lars@63885
  1053
    unfolding equal_fmap_def
lars@63885
  1054
    by (simp add: equal_eq[abs_def])
lars@63885
  1055
qed
lars@63885
  1056
lars@63885
  1057
end
lars@63885
  1058
lars@63885
  1059
lemma fBall_alt_def: "fBall S P \<longleftrightarrow> (\<forall>x. x |\<in>| S \<longrightarrow> P x)"
lars@63885
  1060
by force
lars@63885
  1061
lars@63885
  1062
lemma fmrel_code:
lars@63885
  1063
  "fmrel R m n \<longleftrightarrow>
lars@63885
  1064
    fBall (fmdom m) (\<lambda>x. rel_option R (fmlookup m x) (fmlookup n x)) \<and>
lars@63885
  1065
    fBall (fmdom n) (\<lambda>x. rel_option R (fmlookup m x) (fmlookup n x))"
lars@63885
  1066
unfolding fmrel_iff fmlookup_dom_iff fBall_alt_def
lars@63885
  1067
by (metis option.collapse option.rel_sel)
lars@63885
  1068
lars@63885
  1069
lemmas fmap_generic_code =
lars@63885
  1070
  fmrel_code
lars@63885
  1071
  fmran'_alt_def
lars@63885
  1072
  fmdom'_alt_def
lars@63885
  1073
  fmfilter_alt_defs
lars@63885
  1074
  pred_fmap_fmpred
lars@63885
  1075
  fmsubset_alt_def
lars@63885
  1076
  fmupd_alt_def
lars@63885
  1077
  fmrel_on_fset_alt_def
lars@63885
  1078
  fmpred_alt_def
lars@63885
  1079
lars@63885
  1080
lars@63885
  1081
code_datatype fmap_of_list
lars@63885
  1082
quickcheck_generator fmap constructors: fmap_of_list
lars@63885
  1083
lars@63885
  1084
context includes fset.lifting begin
lars@63885
  1085
lars@66269
  1086
lemma fmlookup_of_list[code]: "fmlookup (fmap_of_list m) = map_of m"
lars@63885
  1087
by transfer simp
lars@63885
  1088
lars@66269
  1089
lemma fmempty_of_list[code]: "fmempty = fmap_of_list []"
lars@63885
  1090
by transfer simp
lars@63885
  1091
lars@66269
  1092
lemma fmran_of_list[code]: "fmran (fmap_of_list m) = snd |`| fset_of_list (AList.clearjunk m)"
lars@63885
  1093
by transfer (auto simp: ran_map_of)
lars@63885
  1094
lars@66269
  1095
lemma fmdom_of_list[code]: "fmdom (fmap_of_list m) = fst |`| fset_of_list m"
lars@63885
  1096
by transfer (auto simp: dom_map_of_conv_image_fst)
lars@63885
  1097
lars@66269
  1098
lemma fmfilter_of_list[code]: "fmfilter P (fmap_of_list m) = fmap_of_list (filter (\<lambda>(k, _). P k) m)"
lars@63885
  1099
by transfer' auto
lars@63885
  1100
lars@66269
  1101
lemma fmadd_of_list[code]: "fmap_of_list m ++\<^sub>f fmap_of_list n = fmap_of_list (AList.merge m n)"
lars@63885
  1102
by transfer (simp add: merge_conv')
lars@63885
  1103
lars@66269
  1104
lemma fmmap_of_list[code]: "fmmap f (fmap_of_list m) = fmap_of_list (map (apsnd f) m)"
lars@63885
  1105
apply transfer
lars@63885
  1106
apply (subst map_of_map[symmetric])
lars@63885
  1107
apply (auto simp: apsnd_def map_prod_def)
lars@63885
  1108
done
lars@63885
  1109
lars@66269
  1110
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
  1111
apply transfer
lars@66269
  1112
subgoal for f m by (induction m) (auto simp: apsnd_def map_prod_def fun_eq_iff)
lars@66269
  1113
done
lars@66269
  1114
lars@63885
  1115
end
lars@63885
  1116
lars@63885
  1117
declare fmap_generic_code[code]
lars@63885
  1118
lars@66267
  1119
lars@66267
  1120
subsection \<open>Instances\<close>
lars@66267
  1121
lars@66267
  1122
lemma exists_map_of:
lars@66267
  1123
  assumes "finite (dom m)" shows "\<exists>xs. map_of xs = m"
lars@66267
  1124
  using assms
lars@66267
  1125
proof (induction "dom m" arbitrary: m)
lars@66267
  1126
  case empty
lars@66267
  1127
  hence "m = Map.empty"
lars@66267
  1128
    by auto
lars@66267
  1129
  moreover have "map_of [] = Map.empty"
lars@66267
  1130
    by simp
lars@66267
  1131
  ultimately show ?case
lars@66267
  1132
    by blast
lars@66267
  1133
next
lars@66267
  1134
  case (insert x F)
lars@66267
  1135
  hence "F = dom (map_drop x m)"
lars@66267
  1136
    unfolding map_drop_def map_filter_def dom_def by auto
lars@66267
  1137
  with insert have "\<exists>xs'. map_of xs' = map_drop x m"
lars@66267
  1138
    by auto
lars@66267
  1139
  then obtain xs' where "map_of xs' = map_drop x m"
lars@66267
  1140
    ..
lars@66267
  1141
  moreover obtain y where "m x = Some y"
lars@66267
  1142
    using insert unfolding dom_def by blast
lars@66267
  1143
  ultimately have "map_of ((x, y) # xs') = m"
lars@66267
  1144
    using \<open>insert x F = dom m\<close>
lars@66267
  1145
    unfolding map_drop_def map_filter_def
lars@66267
  1146
    by auto
lars@66267
  1147
  thus ?case
lars@66267
  1148
    ..
lars@66267
  1149
qed
lars@66267
  1150
lars@66267
  1151
lemma exists_fmap_of_list: "\<exists>xs. fmap_of_list xs = m"
lars@66267
  1152
by transfer (rule exists_map_of)
lars@66267
  1153
lars@66267
  1154
lemma fmap_of_list_surj[simp, intro]: "surj fmap_of_list"
lars@66267
  1155
proof -
lars@66267
  1156
  have "x \<in> range fmap_of_list" for x :: "('a, 'b) fmap"
lars@66267
  1157
    unfolding image_iff
lars@66267
  1158
    using exists_fmap_of_list by (metis UNIV_I)
lars@66267
  1159
  thus ?thesis by auto
lars@66267
  1160
qed
lars@66267
  1161
lars@66267
  1162
instance fmap :: (countable, countable) countable
lars@66267
  1163
proof
lars@66267
  1164
  obtain to_nat :: "('a \<times> 'b) list \<Rightarrow> nat" where "inj to_nat"
lars@66267
  1165
    by (metis ex_inj)
lars@66267
  1166
  moreover have "inj (inv fmap_of_list)"
lars@66267
  1167
    using fmap_of_list_surj by (rule surj_imp_inj_inv)
lars@66267
  1168
  ultimately have "inj (to_nat \<circ> inv fmap_of_list)"
lars@66267
  1169
    by (rule inj_comp)
lars@66267
  1170
  thus "\<exists>to_nat::('a, 'b) fmap \<Rightarrow> nat. inj to_nat"
lars@66267
  1171
    by auto
lars@66267
  1172
qed
lars@66267
  1173
lars@66282
  1174
instance fmap :: (finite, finite) finite
lars@66282
  1175
proof
lars@66282
  1176
  show "finite (UNIV :: ('a, 'b) fmap set)"
lars@66282
  1177
    by (rule finite_imageD) auto
lars@66282
  1178
qed
lars@66282
  1179
lars@63885
  1180
lifting_update fmap.lifting
lars@63885
  1181
lifting_forget fmap.lifting
lars@63885
  1182
lars@66267
  1183
end