src/HOL/Library/Finite_Map.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 64267 b9a1486e79be
child 66267 04b626416eae
permissions -rw-r--r--
executable domain membership checks
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@63885
   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@63885
   154
  
lars@63885
   155
end
lars@63885
   156
lars@63885
   157
lars@63885
   158
subsection \<open>Abstract characterisation\<close>
lars@63885
   159
lars@63885
   160
typedef ('a, 'b) fmap = "{m. finite (dom m)} :: ('a \<rightharpoonup> 'b) set"
lars@63885
   161
  morphisms fmlookup Abs_fmap
lars@63885
   162
proof
lars@63885
   163
  show "Map.empty \<in> {m. finite (dom m)}"
lars@63885
   164
    by auto
lars@63885
   165
qed
lars@63885
   166
lars@63885
   167
setup_lifting type_definition_fmap
lars@63885
   168
lars@63885
   169
lemma fmlookup_finite[intro, simp]: "finite (dom (fmlookup m))"
lars@63885
   170
using fmap.fmlookup by auto
lars@63885
   171
lars@63885
   172
lemma fmap_ext:
lars@63885
   173
  assumes "\<And>x. fmlookup m x = fmlookup n x"
lars@63885
   174
  shows "m = n"
lars@63885
   175
using assms
lars@63885
   176
by transfer' auto
lars@63885
   177
lars@63885
   178
lars@63885
   179
subsection \<open>Operations\<close>
lars@63885
   180
lars@63885
   181
context
lars@63885
   182
  includes fset.lifting
lars@63885
   183
begin
lars@63885
   184
lars@63885
   185
lift_definition fmran :: "('a, 'b) fmap \<Rightarrow> 'b fset"
lars@63885
   186
  is ran
lars@63885
   187
  parametric ran_transfer
lars@63885
   188
unfolding ran_alt_def by auto
lars@63885
   189
lars@63885
   190
lemma fmranI: "fmlookup m x = Some y \<Longrightarrow> y |\<in>| fmran m" by transfer' (auto simp: ran_def)
lars@63885
   191
lars@63885
   192
lift_definition fmdom :: "('a, 'b) fmap \<Rightarrow> 'a fset"
lars@63885
   193
  is dom
lars@63885
   194
  parametric dom_transfer
lars@63885
   195
.
lars@63885
   196
lars@63885
   197
lemma fmdom_notI: "fmlookup m x = None \<Longrightarrow> x |\<notin>| fmdom m" by transfer' auto
lars@63885
   198
lemma fmdomI: "fmlookup m x = Some y \<Longrightarrow> x |\<in>| fmdom m" by transfer' auto
lars@63900
   199
lemma fmdom_notD: "x |\<notin>| fmdom m \<Longrightarrow> fmlookup m x = None" by transfer' auto
lars@63885
   200
lars@63885
   201
lift_definition fmdom' :: "('a, 'b) fmap \<Rightarrow> 'a set"
lars@63885
   202
  is dom
lars@63885
   203
  parametric dom_transfer
lars@63885
   204
.
lars@63885
   205
lars@63885
   206
lemma fmdom'_notI: "fmlookup m x = None \<Longrightarrow> x \<notin> fmdom' m" by transfer' auto
lars@63885
   207
lemma fmdom'I: "fmlookup m x = Some y \<Longrightarrow> x \<in> fmdom' m" by transfer' auto
lars@63900
   208
lemma fmdom'_notD: "x \<notin> fmdom' m \<Longrightarrow> fmlookup m x = None" by transfer' auto
lars@63885
   209
lars@63885
   210
lemma fmdom'_alt_def: "fmdom' = fset \<circ> fmdom"
lars@63885
   211
by transfer' force
lars@63885
   212
lars@63885
   213
lemma fmlookup_dom_iff: "x |\<in>| fmdom m \<longleftrightarrow> (\<exists>a. fmlookup m x = Some a)"
lars@63885
   214
by transfer' auto
lars@63885
   215
lars@63885
   216
lift_definition fmempty :: "('a, 'b) fmap"
lars@63885
   217
  is Map.empty
lars@63885
   218
  parametric map_empty_transfer
lars@63885
   219
by simp
lars@63885
   220
lars@63885
   221
lemma fmempty_lookup[simp]: "fmlookup fmempty x = None"
lars@63885
   222
by transfer' simp
lars@63885
   223
lars@63885
   224
lemma fmdom_empty[simp]: "fmdom fmempty = {||}" by transfer' simp
lars@63885
   225
lemma fmdom'_empty[simp]: "fmdom' fmempty = {}" by transfer' simp
lars@63885
   226
lars@63885
   227
lift_definition fmupd :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   228
  is map_upd
lars@63885
   229
  parametric map_upd_transfer
lars@63885
   230
unfolding map_upd_def[abs_def]
lars@63885
   231
by simp
lars@63885
   232
lars@63885
   233
lemma fmupd_lookup[simp]: "fmlookup (fmupd a b m) a' = (if a = a' then Some b else fmlookup m a')"
lars@63885
   234
by transfer' (auto simp: map_upd_def)
lars@63885
   235
lars@63885
   236
lemma fmdom_fmupd[simp]: "fmdom (fmupd a b m) = finsert a (fmdom m)" by transfer (simp add: map_upd_def)
lars@63885
   237
lemma fmdom'_fmupd[simp]: "fmdom' (fmupd a b m) = insert a (fmdom' m)" by transfer (simp add: map_upd_def)
lars@63885
   238
lars@63885
   239
lift_definition fmfilter :: "('a \<Rightarrow> bool) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   240
  is map_filter
lars@63885
   241
  parametric map_filter_transfer
lars@63885
   242
by auto
lars@63885
   243
lars@63885
   244
lemma fmdom_filter[simp]: "fmdom (fmfilter P m) = ffilter P (fmdom m)"
lars@63885
   245
by transfer' (auto simp: map_filter_def Set.filter_def split: if_splits)
lars@63885
   246
lars@63885
   247
lemma fmdom'_filter[simp]: "fmdom' (fmfilter P m) = Set.filter P (fmdom' m)"
lars@63885
   248
by transfer' (auto simp: map_filter_def Set.filter_def split: if_splits)
lars@63885
   249
lars@63885
   250
lemma fmlookup_filter[simp]: "fmlookup (fmfilter P m) x = (if P x then fmlookup m x else None)"
lars@63885
   251
by transfer' (auto simp: map_filter_def)
lars@63885
   252
lars@63885
   253
lemma fmfilter_empty[simp]: "fmfilter P fmempty = fmempty"
lars@63885
   254
by transfer' (auto simp: map_filter_def)
lars@63885
   255
lars@63900
   256
lemma fmfilter_true[simp]:
lars@63900
   257
  assumes "\<And>x. x |\<in>| fmdom m \<Longrightarrow> P x"
lars@63900
   258
  shows "fmfilter P m = m"
lars@63900
   259
proof (rule fmap_ext)
lars@63900
   260
  fix x
lars@63900
   261
  have "fmlookup m x = None" if "\<not> P x"
lars@63900
   262
    using that assms
lars@63900
   263
    unfolding fmlookup_dom_iff by fastforce
lars@64180
   264
  then show "fmlookup (fmfilter P m) x = fmlookup m x"
lars@63900
   265
    by simp
lars@63900
   266
qed
lars@63885
   267
lars@63885
   268
lemma fmfilter_false[simp]: "(\<And>x. x |\<in>| fmdom m \<Longrightarrow> \<not> P x) \<Longrightarrow> fmfilter P m = fmempty"
lars@63900
   269
by transfer' (auto simp: map_filter_def fun_eq_iff)
lars@63885
   270
lars@63885
   271
lemma fmfilter_comp[simp]: "fmfilter P (fmfilter Q m) = fmfilter (\<lambda>x. P x \<and> Q x) m"
lars@63885
   272
by transfer' (auto simp: map_filter_def)
lars@63885
   273
lars@63885
   274
lemma fmfilter_comm: "fmfilter P (fmfilter Q m) = fmfilter Q (fmfilter P m)"
lars@63885
   275
unfolding fmfilter_comp by meson
lars@63885
   276
lars@63885
   277
lemma fmfilter_cong[cong]:
lars@63885
   278
  assumes "\<And>x. x |\<in>| fmdom m \<Longrightarrow> P x = Q x"
lars@63885
   279
  shows "fmfilter P m = fmfilter Q m"
lars@63900
   280
proof (rule fmap_ext)
lars@63900
   281
  fix x
lars@63900
   282
  have "fmlookup m x = None" if "P x \<noteq> Q x"
lars@63900
   283
    using that assms
lars@63900
   284
    unfolding fmlookup_dom_iff by fastforce
lars@64180
   285
  then show "fmlookup (fmfilter P m) x = fmlookup (fmfilter Q m) x"
lars@63900
   286
    by auto
lars@63900
   287
qed
lars@63885
   288
lars@63885
   289
lemma fmfilter_cong'[fundef_cong]:
lars@63885
   290
  assumes "\<And>x. x \<in> fmdom' m \<Longrightarrow> P x = Q x"
lars@63885
   291
  shows "fmfilter P m = fmfilter Q m"
lars@63900
   292
proof (rule fmfilter_cong)
lars@63900
   293
  fix x
lars@63900
   294
  assume "x |\<in>| fmdom m"
lars@64180
   295
  then show "P x = Q x"
lars@63900
   296
    using assms
lars@63900
   297
    unfolding fmdom'_alt_def fmember.rep_eq
lars@63900
   298
    by auto
lars@63900
   299
qed
lars@63885
   300
lars@63900
   301
lemma fmfilter_upd[simp]:
lars@63900
   302
  "fmfilter P (fmupd x y m) = (if P x then fmupd x y (fmfilter P m) else fmfilter P m)"
lars@63885
   303
by transfer' (auto simp: map_upd_def map_filter_def)
lars@63885
   304
lars@63885
   305
lift_definition fmdrop :: "'a \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   306
  is map_drop
lars@63885
   307
  parametric map_drop_transfer
lars@63885
   308
unfolding map_drop_def by auto
lars@63885
   309
lars@63885
   310
lemma fmdrop_lookup[simp]: "fmlookup (fmdrop a m) a = None"
lars@63885
   311
by transfer' (auto simp: map_drop_def map_filter_def)
lars@63885
   312
lars@63885
   313
lift_definition fmdrop_set :: "'a set \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   314
  is map_drop_set
lars@63885
   315
  parametric map_drop_set_transfer
lars@63885
   316
unfolding map_drop_set_def by auto
lars@63885
   317
lars@63885
   318
lift_definition fmdrop_fset :: "'a fset \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   319
  is map_drop_set
lars@63885
   320
  parametric map_drop_set_transfer
lars@63885
   321
unfolding map_drop_set_def by auto
lars@63885
   322
lars@63885
   323
lift_definition fmrestrict_set :: "'a set \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   324
  is map_restrict_set
lars@63885
   325
  parametric map_restrict_set_transfer
lars@63885
   326
unfolding map_restrict_set_def by auto
lars@63885
   327
lars@63885
   328
lift_definition fmrestrict_fset :: "'a fset \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap"
lars@63885
   329
  is map_restrict_set
lars@63885
   330
  parametric map_restrict_set_transfer
lars@63885
   331
unfolding map_restrict_set_def by auto
lars@63885
   332
lars@63885
   333
lemma fmfilter_alt_defs:
lars@63885
   334
  "fmdrop a = fmfilter (\<lambda>a'. a' \<noteq> a)"
lars@63885
   335
  "fmdrop_set A = fmfilter (\<lambda>a. a \<notin> A)"
lars@63885
   336
  "fmdrop_fset B = fmfilter (\<lambda>a. a |\<notin>| B)"
lars@63885
   337
  "fmrestrict_set A = fmfilter (\<lambda>a. a \<in> A)"
lars@63885
   338
  "fmrestrict_fset B = fmfilter (\<lambda>a. a |\<in>| B)"
lars@63885
   339
by (transfer'; simp add: map_drop_def map_drop_set_def map_restrict_set_def)+
lars@63885
   340
lars@63885
   341
lemma fmdom_drop[simp]: "fmdom (fmdrop a m) = fmdom m - {|a|}" unfolding fmfilter_alt_defs by auto
lars@63885
   342
lemma fmdom'_drop[simp]: "fmdom' (fmdrop a m) = fmdom' m - {a}" unfolding fmfilter_alt_defs by auto
lars@63885
   343
lemma fmdom'_drop_set[simp]: "fmdom' (fmdrop_set A m) = fmdom' m - A" unfolding fmfilter_alt_defs by auto
lars@63885
   344
lemma fmdom_drop_fset[simp]: "fmdom (fmdrop_fset A m) = fmdom m - A" unfolding fmfilter_alt_defs by auto
lars@63885
   345
lemma fmdom'_restrict_set: "fmdom' (fmrestrict_set A m) \<subseteq> A" unfolding fmfilter_alt_defs by auto
lars@63885
   346
lemma fmdom_restrict_fset: "fmdom (fmrestrict_fset A m) |\<subseteq>| A" unfolding fmfilter_alt_defs by auto
lars@63885
   347
lars@63885
   348
lemma fmdom'_drop_fset[simp]: "fmdom' (fmdrop_fset A m) = fmdom' m - fset A"
lars@63885
   349
unfolding fmfilter_alt_defs by transfer' (auto simp: map_filter_def split: if_splits)
lars@63885
   350
lars@63885
   351
lemma fmdom'_restrict_fset: "fmdom' (fmrestrict_fset A m) \<subseteq> fset A"
lars@63885
   352
unfolding fmfilter_alt_defs by transfer' (auto simp: map_filter_def)
lars@63885
   353
lars@63885
   354
lemma fmlookup_drop[simp]:
lars@63885
   355
  "fmlookup (fmdrop a m) x = (if x \<noteq> a then fmlookup m x else None)"
lars@63885
   356
unfolding fmfilter_alt_defs by simp
lars@63885
   357
lars@63885
   358
lemma fmlookup_drop_set[simp]:
lars@63885
   359
  "fmlookup (fmdrop_set A m) x = (if x \<notin> A then fmlookup m x else None)"
lars@63885
   360
unfolding fmfilter_alt_defs by simp
lars@63885
   361
lars@63885
   362
lemma fmlookup_drop_fset[simp]:
lars@63885
   363
  "fmlookup (fmdrop_fset A m) x = (if x |\<notin>| A then fmlookup m x else None)"
lars@63885
   364
unfolding fmfilter_alt_defs by simp
lars@63885
   365
lars@63885
   366
lemma fmlookup_restrict_set[simp]:
lars@63885
   367
  "fmlookup (fmrestrict_set A m) x = (if x \<in> A then fmlookup m x else None)"
lars@63885
   368
unfolding fmfilter_alt_defs by simp
lars@63885
   369
lars@63885
   370
lemma fmlookup_restrict_fset[simp]:
lars@63885
   371
  "fmlookup (fmrestrict_fset A m) x = (if x |\<in>| A then fmlookup m x else None)"
lars@63885
   372
unfolding fmfilter_alt_defs by simp
lars@63885
   373
lars@63900
   374
lemma fmrestrict_set_dom[simp]: "fmrestrict_set (fmdom' m) m = m"
lars@63900
   375
  by (rule fmap_ext) (auto dest: fmdom'_notD)
lars@63900
   376
lars@63900
   377
lemma fmrestrict_fset_dom[simp]: "fmrestrict_fset (fmdom m) m = m"
lars@63900
   378
  by (rule fmap_ext) (auto dest: fmdom_notD)
lars@63900
   379
lars@63885
   380
lemma fmdrop_empty[simp]: "fmdrop a fmempty = fmempty"
lars@63885
   381
  unfolding fmfilter_alt_defs by simp
lars@63885
   382
lars@63885
   383
lemma fmdrop_set_empty[simp]: "fmdrop_set A fmempty = fmempty"
lars@63885
   384
  unfolding fmfilter_alt_defs by simp
lars@63885
   385
lars@63885
   386
lemma fmdrop_fset_empty[simp]: "fmdrop_fset A fmempty = fmempty"
lars@63885
   387
  unfolding fmfilter_alt_defs by simp
lars@63885
   388
lars@63885
   389
lemma fmrestrict_set_empty[simp]: "fmrestrict_set A fmempty = fmempty"
lars@63885
   390
  unfolding fmfilter_alt_defs by simp
lars@63885
   391
lars@63885
   392
lemma fmrestrict_fset_empty[simp]: "fmrestrict_fset A fmempty = fmempty"
lars@63885
   393
  unfolding fmfilter_alt_defs by simp
lars@63885
   394
lars@63885
   395
lemma fmdrop_set_single[simp]: "fmdrop_set {a} m = fmdrop a m"
lars@63885
   396
  unfolding fmfilter_alt_defs by simp
lars@63885
   397
lars@63885
   398
lemma fmdrop_fset_single[simp]: "fmdrop_fset {|a|} m = fmdrop a m"
lars@63885
   399
  unfolding fmfilter_alt_defs by simp
lars@63885
   400
lars@63885
   401
lemma fmrestrict_set_null[simp]: "fmrestrict_set {} m = fmempty"
lars@63885
   402
  unfolding fmfilter_alt_defs by simp
lars@63885
   403
lars@63885
   404
lemma fmrestrict_fset_null[simp]: "fmrestrict_fset {||} m = fmempty"
lars@63885
   405
  unfolding fmfilter_alt_defs by simp
lars@63885
   406
lars@63885
   407
lemma fmdrop_comm: "fmdrop a (fmdrop b m) = fmdrop b (fmdrop a m)"
lars@63885
   408
unfolding fmfilter_alt_defs by (rule fmfilter_comm)
lars@63885
   409
lars@63885
   410
lift_definition fmadd :: "('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap" (infixl "++\<^sub>f" 100)
lars@63885
   411
  is map_add
lars@63885
   412
  parametric map_add_transfer
lars@63885
   413
by simp
lars@63885
   414
lars@63900
   415
lemma fmlookup_add[simp]:
lars@63900
   416
  "fmlookup (m ++\<^sub>f n) x = (if x |\<in>| fmdom n then fmlookup n x else fmlookup m x)"
lars@63900
   417
  by transfer' (auto simp: map_add_def split: option.splits)
lars@63900
   418
lars@63885
   419
lemma fmdom_add[simp]: "fmdom (m ++\<^sub>f n) = fmdom m |\<union>| fmdom n" by transfer' auto
lars@63885
   420
lemma fmdom'_add[simp]: "fmdom' (m ++\<^sub>f n) = fmdom' m \<union> fmdom' n" by transfer' auto
lars@63885
   421
lars@63885
   422
lemma fmadd_drop_left_dom: "fmdrop_fset (fmdom n) m ++\<^sub>f n = m ++\<^sub>f n"
lars@63900
   423
  by (rule fmap_ext) auto
lars@63885
   424
lars@63885
   425
lemma fmadd_restrict_right_dom: "fmrestrict_fset (fmdom n) (m ++\<^sub>f n) = n"
lars@63900
   426
  by (rule fmap_ext) (auto dest: fmdom_notD)
lars@63885
   427
lars@63885
   428
lemma fmfilter_add_distrib[simp]: "fmfilter P (m ++\<^sub>f n) = fmfilter P m ++\<^sub>f fmfilter P n"
lars@63885
   429
by transfer' (auto simp: map_filter_def map_add_def)
lars@63885
   430
lars@63885
   431
lemma fmdrop_add_distrib[simp]: "fmdrop a (m ++\<^sub>f n) = fmdrop a m ++\<^sub>f fmdrop a n"
lars@63885
   432
  unfolding fmfilter_alt_defs by simp
lars@63885
   433
lars@63885
   434
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
   435
  unfolding fmfilter_alt_defs by simp
lars@63885
   436
lars@63885
   437
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
   438
  unfolding fmfilter_alt_defs by simp
lars@63885
   439
lars@63885
   440
lemma fmrestrict_set_add_distrib[simp]:
lars@63885
   441
  "fmrestrict_set A (m ++\<^sub>f n) = fmrestrict_set A m ++\<^sub>f fmrestrict_set A n"
lars@63885
   442
  unfolding fmfilter_alt_defs by simp
lars@63885
   443
lars@63885
   444
lemma fmrestrict_fset_add_distrib[simp]:
lars@63885
   445
  "fmrestrict_fset A (m ++\<^sub>f n) = fmrestrict_fset A m ++\<^sub>f fmrestrict_fset A n"
lars@63885
   446
  unfolding fmfilter_alt_defs by simp
lars@63885
   447
lars@63885
   448
lemma fmadd_empty[simp]: "fmempty ++\<^sub>f m = m" "m ++\<^sub>f fmempty = m"
lars@63885
   449
by (transfer'; auto)+
lars@63885
   450
lars@63885
   451
lemma fmadd_idempotent[simp]: "m ++\<^sub>f m = m"
lars@63885
   452
by transfer' (auto simp: map_add_def split: option.splits)
lars@63885
   453
lars@63885
   454
lemma fmadd_assoc[simp]: "m ++\<^sub>f (n ++\<^sub>f p) = m ++\<^sub>f n ++\<^sub>f p"
lars@63885
   455
by transfer' simp
lars@63885
   456
lars@63885
   457
lift_definition fmpred :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> bool"
lars@63885
   458
  is map_pred
lars@63885
   459
  parametric map_pred_transfer
lars@63885
   460
.
lars@63885
   461
lars@63885
   462
lemma fmpredI[intro]:
lars@63885
   463
  assumes "\<And>x y. fmlookup m x = Some y \<Longrightarrow> P x y"
lars@63885
   464
  shows "fmpred P m"
lars@63885
   465
using assms
lars@63885
   466
by transfer' (auto simp: map_pred_def split: option.splits)
lars@63885
   467
lars@63885
   468
lemma fmpredD[dest]: "fmpred P m \<Longrightarrow> fmlookup m x = Some y \<Longrightarrow> P x y" 
lars@63885
   469
by transfer' (auto simp: map_pred_def split: option.split_asm)
lars@63885
   470
lars@63885
   471
lemma fmpred_iff: "fmpred P m \<longleftrightarrow> (\<forall>x y. fmlookup m x = Some y \<longrightarrow> P x y)"
lars@63885
   472
by auto
lars@63885
   473
lars@63885
   474
lemma fmpred_alt_def: "fmpred P m \<longleftrightarrow> fBall (fmdom m) (\<lambda>x. P x (the (fmlookup m x)))"
lars@63885
   475
unfolding fmpred_iff
lars@63885
   476
apply auto
lars@63885
   477
apply (subst (asm) fmlookup_dom_iff)
lars@63885
   478
apply simp
lars@63900
   479
apply (rename_tac x y)
lars@63885
   480
apply (erule_tac x = x in fBallE)
lars@63885
   481
apply simp
lars@63885
   482
by (simp add: fmlookup_dom_iff)
lars@63885
   483
lars@63885
   484
lemma fmpred_empty[intro!, simp]: "fmpred P fmempty"
lars@63885
   485
by auto
lars@63885
   486
lars@63885
   487
lemma fmpred_upd[intro]: "fmpred P m \<Longrightarrow> P x y \<Longrightarrow> fmpred P (fmupd x y m)"
lars@63885
   488
by transfer' (auto simp: map_pred_def map_upd_def)
lars@63885
   489
lars@63885
   490
lemma fmpred_updD[dest]: "fmpred P (fmupd x y m) \<Longrightarrow> P x y"
lars@63885
   491
by auto
lars@63885
   492
lars@63885
   493
lemma fmpred_add[intro]: "fmpred P m \<Longrightarrow> fmpred P n \<Longrightarrow> fmpred P (m ++\<^sub>f n)"
lars@63885
   494
by transfer' (auto simp: map_pred_def map_add_def split: option.splits)
lars@63885
   495
lars@63885
   496
lemma fmpred_filter[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmfilter Q m)"
lars@63885
   497
by transfer' (auto simp: map_pred_def map_filter_def)
lars@63885
   498
lars@63885
   499
lemma fmpred_drop[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmdrop a m)"
lars@63885
   500
  by (auto simp: fmfilter_alt_defs)
lars@63885
   501
lars@63885
   502
lemma fmpred_drop_set[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmdrop_set A m)"
lars@63885
   503
  by (auto simp: fmfilter_alt_defs)
lars@63885
   504
lars@63885
   505
lemma fmpred_drop_fset[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmdrop_fset A m)"
lars@63885
   506
  by (auto simp: fmfilter_alt_defs)
lars@63885
   507
lars@63885
   508
lemma fmpred_restrict_set[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmrestrict_set A m)"
lars@63885
   509
  by (auto simp: fmfilter_alt_defs)
lars@63885
   510
lars@63885
   511
lemma fmpred_restrict_fset[intro]: "fmpred P m \<Longrightarrow> fmpred P (fmrestrict_fset A m)"
lars@63885
   512
  by (auto simp: fmfilter_alt_defs)
lars@63885
   513
lars@63885
   514
lemma fmpred_cases[consumes 1]:
lars@63885
   515
  assumes "fmpred P m"
lars@63885
   516
  obtains (none) "fmlookup m x = None" | (some) y where "fmlookup m x = Some y" "P x y"
lars@63885
   517
using assms by auto
lars@63885
   518
lars@63885
   519
lift_definition fmsubset :: "('a, 'b) fmap \<Rightarrow> ('a, 'b) fmap \<Rightarrow> bool" (infix "\<subseteq>\<^sub>f" 50)
lars@63885
   520
  is map_le
lars@63885
   521
.
lars@63885
   522
lars@63885
   523
lemma fmsubset_alt_def: "m \<subseteq>\<^sub>f n \<longleftrightarrow> fmpred (\<lambda>k v. fmlookup n k = Some v) m"
lars@63885
   524
by transfer' (auto simp: map_pred_def map_le_def dom_def split: option.splits)
lars@63885
   525
lars@63885
   526
lemma fmsubset_pred: "fmpred P m \<Longrightarrow> n \<subseteq>\<^sub>f m \<Longrightarrow> fmpred P n"
lars@63885
   527
unfolding fmsubset_alt_def fmpred_iff
lars@63885
   528
by auto
lars@63885
   529
lars@63885
   530
lemma fmsubset_filter_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmfilter P m \<subseteq>\<^sub>f fmfilter P n"
lars@63885
   531
unfolding fmsubset_alt_def fmpred_iff
lars@63885
   532
by auto
lars@63885
   533
lars@63885
   534
lemma fmsubset_drop_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmdrop a m \<subseteq>\<^sub>f fmdrop a n"
lars@63885
   535
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   536
lars@63885
   537
lemma fmsubset_drop_set_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmdrop_set A m \<subseteq>\<^sub>f fmdrop_set A n"
lars@63885
   538
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   539
lars@63885
   540
lemma fmsubset_drop_fset_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmdrop_fset A m \<subseteq>\<^sub>f fmdrop_fset A n"
lars@63885
   541
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   542
lars@63885
   543
lemma fmsubset_restrict_set_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmrestrict_set A m \<subseteq>\<^sub>f fmrestrict_set A n"
lars@63885
   544
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   545
lars@63885
   546
lemma fmsubset_restrict_fset_mono: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmrestrict_fset A m \<subseteq>\<^sub>f fmrestrict_fset A n"
lars@63885
   547
unfolding fmfilter_alt_defs by (rule fmsubset_filter_mono)
lars@63885
   548
lars@63885
   549
lift_definition fmap_of_list :: "('a \<times> 'b) list \<Rightarrow> ('a, 'b) fmap"
lars@63885
   550
  is map_of
lars@63885
   551
  parametric map_of_transfer
lars@63885
   552
by (rule finite_dom_map_of)
lars@63885
   553
lars@63885
   554
lemma fmap_of_list_simps[simp]:
lars@63885
   555
  "fmap_of_list [] = fmempty"
lars@63885
   556
  "fmap_of_list ((k, v) # kvs) = fmupd k v (fmap_of_list kvs)"
lars@63885
   557
by (transfer, simp add: map_upd_def)+
lars@63885
   558
lars@63885
   559
lemma fmap_of_list_app[simp]: "fmap_of_list (xs @ ys) = fmap_of_list ys ++\<^sub>f fmap_of_list xs"
lars@63885
   560
by transfer' simp
lars@63885
   561
lars@63885
   562
lemma fmupd_alt_def: "fmupd k v m = m ++\<^sub>f fmap_of_list [(k, v)]"
lars@63885
   563
by transfer' (auto simp: map_upd_def)
lars@63885
   564
lars@63885
   565
lemma fmpred_of_list[intro]:
lars@63885
   566
  assumes "\<And>k v. (k, v) \<in> set xs \<Longrightarrow> P k v"
lars@63885
   567
  shows "fmpred P (fmap_of_list xs)"
lars@63885
   568
using assms
lars@63885
   569
by (induction xs) (transfer'; auto simp: map_pred_def)+
lars@63885
   570
lars@63885
   571
lemma fmap_of_list_SomeD: "fmlookup (fmap_of_list xs) k = Some v \<Longrightarrow> (k, v) \<in> set xs"
lars@63885
   572
by transfer' (auto dest: map_of_SomeD)
lars@63885
   573
lars@63885
   574
lift_definition fmrel_on_set :: "'a set \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('a, 'b) fmap \<Rightarrow> ('a, 'c) fmap \<Rightarrow> bool"
lars@63885
   575
  is rel_map_on_set
lars@63885
   576
.
lars@63885
   577
lars@63885
   578
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
   579
  is rel_map_on_set
lars@63885
   580
.
lars@63885
   581
lars@63885
   582
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
   583
by transfer' (auto simp: rel_map_on_set_def eq_onp_def rel_fun_def)
lars@63885
   584
lars@64181
   585
lemma fmrel_on_fsetI[intro]:
lars@63885
   586
  assumes "\<And>x. x |\<in>| S \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   587
  shows "fmrel_on_fset S P m n"
lars@63885
   588
using assms
lars@63885
   589
unfolding fmrel_on_fset_alt_def by auto
lars@63885
   590
lars@63885
   591
lemma fmrel_on_fset_mono[mono]: "R \<le> Q \<Longrightarrow> fmrel_on_fset S R \<le> fmrel_on_fset S Q"
lars@63885
   592
unfolding fmrel_on_fset_alt_def[abs_def]
lars@63885
   593
apply (intro le_funI fBall_mono)
lars@63885
   594
using option.rel_mono by auto
lars@63885
   595
lars@63885
   596
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
   597
unfolding fmrel_on_fset_alt_def
lars@63885
   598
by auto
lars@63885
   599
lars@63885
   600
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
   601
unfolding fmrel_on_fset_alt_def
lars@63885
   602
by auto
lars@63885
   603
lars@63885
   604
end
lars@63885
   605
lars@63885
   606
lars@63885
   607
subsection \<open>BNF setup\<close>
lars@63885
   608
lars@63885
   609
lift_bnf ('a, fmran': 'b) fmap [wits: Map.empty]
lars@63885
   610
  for map: fmmap
lars@63885
   611
      rel: fmrel
lars@63885
   612
  by auto
lars@63885
   613
lars@64180
   614
text \<open>
lars@64180
   615
  Unfortunately, @{command lift_bnf} doesn't register the definitional theorems. We're doing it
lars@64180
   616
  manually below.
lars@64180
   617
\<close>
lars@64180
   618
lars@64180
   619
local_setup \<open>fn lthy =>
lars@64180
   620
  let
lars@64180
   621
    val SOME bnf = BNF_Def.bnf_of lthy @{type_name fmap}
lars@64180
   622
  in
lars@64180
   623
    lthy
lars@64180
   624
    |> Local_Theory.note ((@{binding fmmap_def}, []), [BNF_Def.map_def_of_bnf bnf]) |> #2
lars@64180
   625
    |> Local_Theory.note ((@{binding fmran'_def}, []), BNF_Def.set_defs_of_bnf bnf) |> #2
lars@64180
   626
    |> Local_Theory.note ((@{binding fmrel_def}, []), [BNF_Def.rel_def_of_bnf bnf]) |> #2
lars@64180
   627
  end
lars@64180
   628
\<close>
lars@64180
   629
lars@63885
   630
context includes lifting_syntax begin
lars@63885
   631
lars@63885
   632
lemma fmmap_transfer[transfer_rule]:
lars@63885
   633
  "(op = ===> pcr_fmap op = op = ===> pcr_fmap op = op =) (\<lambda>f. op \<circ> (map_option f)) fmmap"
lars@64180
   634
  unfolding fmmap_def
lars@64180
   635
  by (rule rel_funI ext)+ (auto simp: fmap.Abs_fmap_inverse fmap.pcr_cr_eq cr_fmap_def)
lars@63885
   636
lars@63885
   637
lemma fmran'_transfer[transfer_rule]:
lars@63885
   638
  "(pcr_fmap op = op = ===> op =) (\<lambda>x. UNION (range x) set_option) fmran'"
lars@64180
   639
  unfolding fmran'_def fmap.pcr_cr_eq cr_fmap_def by fastforce
lars@63885
   640
lars@63885
   641
lemma fmrel_transfer[transfer_rule]:
lars@63885
   642
  "(op = ===> pcr_fmap op = op = ===> pcr_fmap op = op = ===> op =) rel_map fmrel"
lars@64180
   643
  unfolding fmrel_def fmap.pcr_cr_eq cr_fmap_def vimage2p_def by fastforce
lars@63885
   644
lars@63885
   645
end
lars@63885
   646
lars@63885
   647
lars@63885
   648
lemma fmran'_alt_def: "fmran' = fset \<circ> fmran"
lars@63885
   649
including fset.lifting
lars@63885
   650
by transfer' (auto simp: ran_def fun_eq_iff)
lars@63885
   651
lars@63885
   652
lemma fmran'I: "fmlookup m x = Some y \<Longrightarrow> y \<in> fmran' m"
lars@63885
   653
by transfer' auto
lars@63885
   654
lars@63885
   655
lemma fmrel_iff: "fmrel R m n \<longleftrightarrow> (\<forall>x. rel_option R (fmlookup m x) (fmlookup n x))"
lars@63885
   656
by transfer' (auto simp: rel_fun_def)
lars@63885
   657
lars@63885
   658
lemma fmrelI[intro]:
lars@63885
   659
  assumes "\<And>x. rel_option R (fmlookup m x) (fmlookup n x)"
lars@63885
   660
  shows "fmrel R m n"
lars@63885
   661
using assms
lars@63885
   662
by transfer' auto
lars@63885
   663
lars@63885
   664
lemma fmempty_transfer[simp, intro, transfer_rule]: "fmrel P fmempty fmempty"
lars@63885
   665
by transfer auto
lars@63885
   666
lars@63885
   667
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
   668
by transfer' (auto simp: map_upd_def rel_fun_def)
lars@63885
   669
lars@63885
   670
lemma fmrelD[dest]: "fmrel P m n \<Longrightarrow> rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   671
by transfer' (auto simp: rel_fun_def)
lars@63885
   672
lars@63885
   673
lemma fmrel_addI[intro]:
lars@63885
   674
  assumes "fmrel P m n" "fmrel P a b"
lars@63885
   675
  shows "fmrel P (m ++\<^sub>f a) (n ++\<^sub>f b)"
lars@63885
   676
using assms
lars@63885
   677
apply transfer'
lars@63885
   678
apply (auto simp: rel_fun_def map_add_def)
lars@63885
   679
by (metis option.case_eq_if option.collapse option.rel_sel)
lars@63885
   680
lars@63885
   681
lemma fmrel_cases[consumes 1]:
lars@63885
   682
  assumes "fmrel P m n"
lars@63885
   683
  obtains (none) "fmlookup m x = None" "fmlookup n x = None"
lars@63885
   684
        | (some) a b where "fmlookup m x = Some a" "fmlookup n x = Some b" "P a b"
lars@63885
   685
proof -
lars@63885
   686
  from assms have "rel_option P (fmlookup m x) (fmlookup n x)"
lars@63885
   687
    by auto
lars@64180
   688
  then show thesis
lars@63885
   689
    using none some
lars@63885
   690
    by (cases rule: option.rel_cases) auto
lars@63885
   691
qed
lars@63885
   692
lars@63885
   693
lemma fmrel_filter[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmfilter Q m) (fmfilter Q n)"
lars@63885
   694
unfolding fmrel_iff by auto
lars@63885
   695
lars@63885
   696
lemma fmrel_drop[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop a m) (fmdrop a n)"
lars@63885
   697
  unfolding fmfilter_alt_defs by blast
lars@63885
   698
lars@63885
   699
lemma fmrel_drop_set[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop_set A m) (fmdrop_set A n)"
lars@63885
   700
  unfolding fmfilter_alt_defs by blast
lars@63885
   701
lars@63885
   702
lemma fmrel_drop_fset[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmdrop_fset A m) (fmdrop_fset A n)"
lars@63885
   703
  unfolding fmfilter_alt_defs by blast
lars@63885
   704
lars@63885
   705
lemma fmrel_restrict_set[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmrestrict_set A m) (fmrestrict_set A n)"
lars@63885
   706
  unfolding fmfilter_alt_defs by blast
lars@63885
   707
lars@63885
   708
lemma fmrel_restrict_fset[intro]: "fmrel P m n \<Longrightarrow> fmrel P (fmrestrict_fset A m) (fmrestrict_fset A n)"
lars@63885
   709
  unfolding fmfilter_alt_defs by blast
lars@63885
   710
lars@63885
   711
lemma pred_fmap_fmpred[simp]: "pred_fmap P = fmpred (\<lambda>_. P)"
lars@63885
   712
unfolding fmap.pred_set fmran'_alt_def
lars@63885
   713
including fset.lifting
lars@63885
   714
apply transfer'
lars@63885
   715
apply (rule ext)
lars@63885
   716
apply (auto simp: map_pred_def ran_def split: option.splits dest: )
lars@63885
   717
done
lars@63885
   718
lars@63885
   719
lemma pred_fmap_id[simp]: "pred_fmap id (fmmap f m) \<longleftrightarrow> pred_fmap f m"
lars@63885
   720
unfolding fmap.pred_set fmap.set_map
lars@63885
   721
by simp
lars@63885
   722
lars@63885
   723
lemma fmlookup_map[simp]: "fmlookup (fmmap f m) x = map_option f (fmlookup m x)"
lars@64180
   724
by transfer' auto
lars@63885
   725
lars@63885
   726
lemma fmpred_map[simp]: "fmpred P (fmmap f m) \<longleftrightarrow> fmpred (\<lambda>k v. P k (f v)) m"
lars@63885
   727
unfolding fmpred_iff pred_fmap_def fmap.set_map
lars@63885
   728
by auto
lars@63885
   729
lars@63885
   730
lemma fmpred_id[simp]: "fmpred (\<lambda>_. id) (fmmap f m) \<longleftrightarrow> fmpred (\<lambda>_. f) m"
lars@63885
   731
by simp
lars@63885
   732
lars@63885
   733
lemma fmmap_add[simp]: "fmmap f (m ++\<^sub>f n) = fmmap f m ++\<^sub>f fmmap f n"
lars@63885
   734
by transfer' (auto simp: map_add_def fun_eq_iff split: option.splits)
lars@63885
   735
lars@63885
   736
lemma fmmap_empty[simp]: "fmmap f fmempty = fmempty"
lars@63885
   737
by transfer auto
lars@63885
   738
lars@63885
   739
lemma fmdom_map[simp]: "fmdom (fmmap f m) = fmdom m"
lars@63885
   740
including fset.lifting
lars@63885
   741
by transfer' simp
lars@63885
   742
lars@63885
   743
lemma fmdom'_map[simp]: "fmdom' (fmmap f m) = fmdom' m"
lars@63885
   744
by transfer' simp
lars@63885
   745
lars@63885
   746
lemma fmfilter_fmmap[simp]: "fmfilter P (fmmap f m) = fmmap f (fmfilter P m)"
lars@63885
   747
by transfer' (auto simp: map_filter_def)
lars@63885
   748
lars@63885
   749
lemma fmdrop_fmmap[simp]: "fmdrop a (fmmap f m) = fmmap f (fmdrop a m)" unfolding fmfilter_alt_defs by simp
lars@63885
   750
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
   751
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
   752
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
   753
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
   754
lars@63885
   755
lemma fmmap_subset[intro]: "m \<subseteq>\<^sub>f n \<Longrightarrow> fmmap f m \<subseteq>\<^sub>f fmmap f n"
lars@63885
   756
by transfer' (auto simp: map_le_def)
lars@63885
   757
lars@63885
   758
lars@63885
   759
subsection \<open>Code setup\<close>
lars@63885
   760
lars@63885
   761
instantiation fmap :: (type, equal) equal begin
lars@63885
   762
lars@63885
   763
definition "equal_fmap \<equiv> fmrel HOL.equal"
lars@63885
   764
lars@63885
   765
instance proof
lars@63885
   766
  fix m n :: "('a, 'b) fmap"
lars@63885
   767
  have "fmrel op = m n \<longleftrightarrow> (m = n)"
lars@63885
   768
    by transfer' (simp add: option.rel_eq rel_fun_eq)
lars@64180
   769
  then show "equal_class.equal m n \<longleftrightarrow> (m = n)"
lars@63885
   770
    unfolding equal_fmap_def
lars@63885
   771
    by (simp add: equal_eq[abs_def])
lars@63885
   772
qed
lars@63885
   773
lars@63885
   774
end
lars@63885
   775
lars@63885
   776
lemma fBall_alt_def: "fBall S P \<longleftrightarrow> (\<forall>x. x |\<in>| S \<longrightarrow> P x)"
lars@63885
   777
by force
lars@63885
   778
lars@63885
   779
lemma fmrel_code:
lars@63885
   780
  "fmrel R m n \<longleftrightarrow>
lars@63885
   781
    fBall (fmdom m) (\<lambda>x. rel_option R (fmlookup m x) (fmlookup n x)) \<and>
lars@63885
   782
    fBall (fmdom n) (\<lambda>x. rel_option R (fmlookup m x) (fmlookup n x))"
lars@63885
   783
unfolding fmrel_iff fmlookup_dom_iff fBall_alt_def
lars@63885
   784
by (metis option.collapse option.rel_sel)
lars@63885
   785
lars@63885
   786
lemmas fmap_generic_code =
lars@63885
   787
  fmrel_code
lars@63885
   788
  fmran'_alt_def
lars@63885
   789
  fmdom'_alt_def
lars@63885
   790
  fmfilter_alt_defs
lars@63885
   791
  pred_fmap_fmpred
lars@63885
   792
  fmsubset_alt_def
lars@63885
   793
  fmupd_alt_def
lars@63885
   794
  fmrel_on_fset_alt_def
lars@63885
   795
  fmpred_alt_def
lars@63885
   796
lars@63885
   797
lars@63885
   798
code_datatype fmap_of_list
lars@63885
   799
quickcheck_generator fmap constructors: fmap_of_list
lars@63885
   800
lars@63885
   801
context includes fset.lifting begin
lars@63885
   802
lars@63885
   803
lemma [code]: "fmlookup (fmap_of_list m) = map_of m"
lars@63885
   804
by transfer simp
lars@63885
   805
lars@63885
   806
lemma [code]: "fmempty = fmap_of_list []"
lars@63885
   807
by transfer simp
lars@63885
   808
lars@63885
   809
lemma [code]: "fmran (fmap_of_list m) = snd |`| fset_of_list (AList.clearjunk m)"
lars@63885
   810
by transfer (auto simp: ran_map_of)
lars@63885
   811
lars@63885
   812
lemma [code]: "fmdom (fmap_of_list m) = fst |`| fset_of_list m"
lars@63885
   813
by transfer (auto simp: dom_map_of_conv_image_fst)
lars@63885
   814
lars@63885
   815
lemma [code]: "fmfilter P (fmap_of_list m) = fmap_of_list (filter (\<lambda>(k, _). P k) m)"
lars@63885
   816
by transfer' auto
lars@63885
   817
lars@63885
   818
lemma [code]: "fmap_of_list m ++\<^sub>f fmap_of_list n = fmap_of_list (AList.merge m n)"
lars@63885
   819
by transfer (simp add: merge_conv')
lars@63885
   820
lars@63885
   821
lemma [code]: "fmmap f (fmap_of_list m) = fmap_of_list (map (apsnd f) m)"
lars@63885
   822
apply transfer
lars@63885
   823
apply (subst map_of_map[symmetric])
lars@63885
   824
apply (auto simp: apsnd_def map_prod_def)
lars@63885
   825
done
lars@63885
   826
lars@63885
   827
end
lars@63885
   828
lars@63885
   829
declare fmap_generic_code[code]
lars@63885
   830
lars@63885
   831
lifting_update fmap.lifting
lars@63885
   832
lifting_forget fmap.lifting
lars@63885
   833
nipkow@64267
   834
end