src/HOL/Library/Multiset.thy
author fleury <Mathias.Fleury@mpi-inf.mpg.de>
Wed Jul 20 13:51:38 2016 +0200 (2016-07-20)
changeset 63524 4ec755485732
parent 63410 9789ccc2a477
child 63534 523b488b15c9
child 63539 70d4d9e5707b
permissions -rw-r--r--
adding mset_map to the simp rules
wenzelm@10249
     1
(*  Title:      HOL/Library/Multiset.thy
paulson@15072
     2
    Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
blanchet@55129
     3
    Author:     Andrei Popescu, TU Muenchen
blanchet@59813
     4
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
blanchet@59813
     5
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@59813
     6
    Author:     Mathias Fleury, MPII
wenzelm@10249
     7
*)
wenzelm@10249
     8
wenzelm@60500
     9
section \<open>(Finite) multisets\<close>
wenzelm@10249
    10
nipkow@15131
    11
theory Multiset
haftmann@51599
    12
imports Main
nipkow@15131
    13
begin
wenzelm@10249
    14
wenzelm@60500
    15
subsection \<open>The type of multisets\<close>
wenzelm@10249
    16
wenzelm@60606
    17
definition "multiset = {f :: 'a \<Rightarrow> nat. finite {x. f x > 0}}"
wenzelm@60606
    18
wenzelm@60606
    19
typedef 'a multiset = "multiset :: ('a \<Rightarrow> nat) set"
haftmann@34943
    20
  morphisms count Abs_multiset
wenzelm@45694
    21
  unfolding multiset_def
wenzelm@10249
    22
proof
wenzelm@45694
    23
  show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
wenzelm@10249
    24
qed
wenzelm@10249
    25
bulwahn@47429
    26
setup_lifting type_definition_multiset
wenzelm@19086
    27
wenzelm@60606
    28
lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
nipkow@39302
    29
  by (simp only: count_inject [symmetric] fun_eq_iff)
haftmann@34943
    30
wenzelm@60606
    31
lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
nipkow@39302
    32
  using multiset_eq_iff by auto
haftmann@34943
    33
wenzelm@60606
    34
text \<open>Preservation of the representing set @{term multiset}.\<close>
wenzelm@60606
    35
wenzelm@60606
    36
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset"
haftmann@34943
    37
  by (simp add: multiset_def)
haftmann@34943
    38
wenzelm@60606
    39
lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset"
haftmann@34943
    40
  by (simp add: multiset_def)
haftmann@34943
    41
wenzelm@60606
    42
lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
haftmann@34943
    43
  by (simp add: multiset_def)
haftmann@34943
    44
haftmann@34943
    45
lemma diff_preserves_multiset:
haftmann@34943
    46
  assumes "M \<in> multiset"
haftmann@34943
    47
  shows "(\<lambda>a. M a - N a) \<in> multiset"
haftmann@34943
    48
proof -
haftmann@34943
    49
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    50
    by auto
haftmann@34943
    51
  with assms show ?thesis
haftmann@34943
    52
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    53
qed
haftmann@34943
    54
haftmann@41069
    55
lemma filter_preserves_multiset:
haftmann@34943
    56
  assumes "M \<in> multiset"
haftmann@34943
    57
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
haftmann@34943
    58
proof -
haftmann@34943
    59
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    60
    by auto
haftmann@34943
    61
  with assms show ?thesis
haftmann@34943
    62
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    63
qed
haftmann@34943
    64
haftmann@34943
    65
lemmas in_multiset = const0_in_multiset only1_in_multiset
haftmann@41069
    66
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
haftmann@34943
    67
haftmann@34943
    68
wenzelm@60500
    69
subsection \<open>Representing multisets\<close>
wenzelm@60500
    70
wenzelm@60500
    71
text \<open>Multiset enumeration\<close>
haftmann@34943
    72
huffman@48008
    73
instantiation multiset :: (type) cancel_comm_monoid_add
haftmann@25571
    74
begin
haftmann@25571
    75
bulwahn@47429
    76
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
bulwahn@47429
    77
by (rule const0_in_multiset)
haftmann@25571
    78
haftmann@34943
    79
abbreviation Mempty :: "'a multiset" ("{#}") where
haftmann@34943
    80
  "Mempty \<equiv> 0"
haftmann@25571
    81
wenzelm@60606
    82
lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
bulwahn@47429
    83
by (rule union_preserves_multiset)
haftmann@25571
    84
wenzelm@60606
    85
lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
haftmann@59815
    86
by (rule diff_preserves_multiset)
haftmann@59815
    87
huffman@48008
    88
instance
wenzelm@60678
    89
  by (standard; transfer; simp add: fun_eq_iff)
haftmann@25571
    90
haftmann@25571
    91
end
wenzelm@10249
    92
eberlm@63195
    93
context
eberlm@63195
    94
begin
eberlm@63195
    95
eberlm@63195
    96
qualified definition is_empty :: "'a multiset \<Rightarrow> bool" where
eberlm@63195
    97
  [code_abbrev]: "is_empty A \<longleftrightarrow> A = {#}"
eberlm@63195
    98
eberlm@63195
    99
end
eberlm@63195
   100
eberlm@63195
   101
wenzelm@60606
   102
lift_definition single :: "'a \<Rightarrow> 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
bulwahn@47429
   103
by (rule only1_in_multiset)
kleing@15869
   104
wenzelm@26145
   105
syntax
wenzelm@60606
   106
  "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
nipkow@25507
   107
translations
nipkow@25507
   108
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
   109
  "{#x#}" == "CONST single x"
nipkow@25507
   110
haftmann@34943
   111
lemma count_empty [simp]: "count {#} a = 0"
bulwahn@47429
   112
  by (simp add: zero_multiset.rep_eq)
wenzelm@10249
   113
haftmann@34943
   114
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
bulwahn@47429
   115
  by (simp add: single.rep_eq)
nipkow@29901
   116
wenzelm@10249
   117
wenzelm@60500
   118
subsection \<open>Basic operations\<close>
wenzelm@60500
   119
haftmann@62430
   120
subsubsection \<open>Conversion to set and membership\<close>
haftmann@62430
   121
haftmann@62430
   122
definition set_mset :: "'a multiset \<Rightarrow> 'a set"
haftmann@62430
   123
  where "set_mset M = {x. count M x > 0}"
haftmann@62430
   124
haftmann@62537
   125
abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
haftmann@62537
   126
  where "Melem a M \<equiv> a \<in> set_mset M"
haftmann@62537
   127
haftmann@62537
   128
notation
haftmann@62537
   129
  Melem  ("op \<in>#") and
haftmann@62537
   130
  Melem  ("(_/ \<in># _)" [51, 51] 50)
haftmann@62537
   131
haftmann@62537
   132
notation  (ASCII)
haftmann@62537
   133
  Melem  ("op :#") and
haftmann@62537
   134
  Melem  ("(_/ :# _)" [51, 51] 50)
haftmann@62537
   135
haftmann@62537
   136
abbreviation not_Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"
haftmann@62537
   137
  where "not_Melem a M \<equiv> a \<notin> set_mset M"
haftmann@62537
   138
haftmann@62537
   139
notation
haftmann@62537
   140
  not_Melem  ("op \<notin>#") and
haftmann@62537
   141
  not_Melem  ("(_/ \<notin># _)" [51, 51] 50)
haftmann@62537
   142
haftmann@62537
   143
notation  (ASCII)
haftmann@62537
   144
  not_Melem  ("op ~:#") and
haftmann@62537
   145
  not_Melem  ("(_/ ~:# _)" [51, 51] 50)
haftmann@62430
   146
haftmann@62430
   147
context
haftmann@62430
   148
begin
haftmann@62430
   149
haftmann@62430
   150
qualified abbreviation Ball :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@62430
   151
  where "Ball M \<equiv> Set.Ball (set_mset M)"
haftmann@62430
   152
haftmann@62430
   153
qualified abbreviation Bex :: "'a multiset \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@62430
   154
  where "Bex M \<equiv> Set.Bex (set_mset M)"
haftmann@62430
   155
haftmann@62430
   156
end
haftmann@62430
   157
haftmann@62430
   158
syntax
haftmann@62430
   159
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>#_./ _)" [0, 0, 10] 10)
haftmann@62430
   160
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>#_./ _)" [0, 0, 10] 10)
haftmann@62430
   161
haftmann@62537
   162
syntax  (ASCII)
haftmann@62537
   163
  "_MBall"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_:#_./ _)" [0, 0, 10] 10)
haftmann@62537
   164
  "_MBex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_:#_./ _)" [0, 0, 10] 10)
haftmann@62537
   165
haftmann@62430
   166
translations
haftmann@62430
   167
  "\<forall>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Ball A (\<lambda>x. P)"
haftmann@62430
   168
  "\<exists>x\<in>#A. P" \<rightleftharpoons> "CONST Multiset.Bex A (\<lambda>x. P)"
haftmann@62430
   169
haftmann@62430
   170
lemma count_eq_zero_iff:
haftmann@62430
   171
  "count M x = 0 \<longleftrightarrow> x \<notin># M"
haftmann@62430
   172
  by (auto simp add: set_mset_def)
haftmann@62430
   173
haftmann@62430
   174
lemma not_in_iff:
haftmann@62430
   175
  "x \<notin># M \<longleftrightarrow> count M x = 0"
haftmann@62430
   176
  by (auto simp add: count_eq_zero_iff)
haftmann@62430
   177
haftmann@62430
   178
lemma count_greater_zero_iff [simp]:
haftmann@62430
   179
  "count M x > 0 \<longleftrightarrow> x \<in># M"
haftmann@62430
   180
  by (auto simp add: set_mset_def)
haftmann@62430
   181
haftmann@62430
   182
lemma count_inI:
haftmann@62430
   183
  assumes "count M x = 0 \<Longrightarrow> False"
haftmann@62430
   184
  shows "x \<in># M"
haftmann@62430
   185
proof (rule ccontr)
haftmann@62430
   186
  assume "x \<notin># M"
haftmann@62430
   187
  with assms show False by (simp add: not_in_iff)
haftmann@62430
   188
qed
haftmann@62430
   189
haftmann@62430
   190
lemma in_countE:
haftmann@62430
   191
  assumes "x \<in># M"
haftmann@62430
   192
  obtains n where "count M x = Suc n"
haftmann@62430
   193
proof -
haftmann@62430
   194
  from assms have "count M x > 0" by simp
haftmann@62430
   195
  then obtain n where "count M x = Suc n"
haftmann@62430
   196
    using gr0_conv_Suc by blast
haftmann@62430
   197
  with that show thesis .
haftmann@62430
   198
qed
haftmann@62430
   199
haftmann@62430
   200
lemma count_greater_eq_Suc_zero_iff [simp]:
haftmann@62430
   201
  "count M x \<ge> Suc 0 \<longleftrightarrow> x \<in># M"
haftmann@62430
   202
  by (simp add: Suc_le_eq)
haftmann@62430
   203
haftmann@62430
   204
lemma count_greater_eq_one_iff [simp]:
haftmann@62430
   205
  "count M x \<ge> 1 \<longleftrightarrow> x \<in># M"
haftmann@62430
   206
  by simp
haftmann@62430
   207
haftmann@62430
   208
lemma set_mset_empty [simp]:
haftmann@62430
   209
  "set_mset {#} = {}"
haftmann@62430
   210
  by (simp add: set_mset_def)
haftmann@62430
   211
haftmann@62430
   212
lemma set_mset_single [simp]:
haftmann@62430
   213
  "set_mset {#b#} = {b}"
haftmann@62430
   214
  by (simp add: set_mset_def)
haftmann@62430
   215
haftmann@62430
   216
lemma set_mset_eq_empty_iff [simp]:
haftmann@62430
   217
  "set_mset M = {} \<longleftrightarrow> M = {#}"
haftmann@62430
   218
  by (auto simp add: multiset_eq_iff count_eq_zero_iff)
haftmann@62430
   219
haftmann@62430
   220
lemma finite_set_mset [iff]:
haftmann@62430
   221
  "finite (set_mset M)"
haftmann@62430
   222
  using count [of M] by (simp add: multiset_def)
haftmann@62430
   223
haftmann@62430
   224
wenzelm@60500
   225
subsubsection \<open>Union\<close>
wenzelm@10249
   226
haftmann@62430
   227
lemma count_union [simp]:
haftmann@62430
   228
  "count (M + N) a = count M a + count N a"
bulwahn@47429
   229
  by (simp add: plus_multiset.rep_eq)
wenzelm@10249
   230
haftmann@62430
   231
lemma set_mset_union [simp]:
haftmann@62430
   232
  "set_mset (M + N) = set_mset M \<union> set_mset N"
haftmann@62430
   233
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_union) simp
haftmann@62430
   234
wenzelm@10249
   235
wenzelm@60500
   236
subsubsection \<open>Difference\<close>
wenzelm@10249
   237
haftmann@62430
   238
instance multiset :: (type) comm_monoid_diff
haftmann@62430
   239
  by standard (transfer; simp add: fun_eq_iff)
haftmann@62430
   240
haftmann@62430
   241
lemma count_diff [simp]:
haftmann@62430
   242
  "count (M - N) a = count M a - count N a"
bulwahn@47429
   243
  by (simp add: minus_multiset.rep_eq)
haftmann@34943
   244
haftmann@62430
   245
lemma in_diff_count:
haftmann@62430
   246
  "a \<in># M - N \<longleftrightarrow> count N a < count M a"
haftmann@62430
   247
  by (simp add: set_mset_def)
haftmann@62430
   248
haftmann@62430
   249
lemma count_in_diffI:
haftmann@62430
   250
  assumes "\<And>n. count N x = n + count M x \<Longrightarrow> False"
haftmann@62430
   251
  shows "x \<in># M - N"
haftmann@62430
   252
proof (rule ccontr)
haftmann@62430
   253
  assume "x \<notin># M - N"
haftmann@62430
   254
  then have "count N x = (count N x - count M x) + count M x"
haftmann@62430
   255
    by (simp add: in_diff_count not_less)
haftmann@62430
   256
  with assms show False by auto
haftmann@62430
   257
qed
haftmann@62430
   258
haftmann@62430
   259
lemma in_diff_countE:
haftmann@62430
   260
  assumes "x \<in># M - N"
haftmann@62430
   261
  obtains n where "count M x = Suc n + count N x"
haftmann@62430
   262
proof -
haftmann@62430
   263
  from assms have "count M x - count N x > 0" by (simp add: in_diff_count)
haftmann@62430
   264
  then have "count M x > count N x" by simp
haftmann@62430
   265
  then obtain n where "count M x = Suc n + count N x"
haftmann@62430
   266
    using less_iff_Suc_add by auto
haftmann@62430
   267
  with that show thesis .
haftmann@62430
   268
qed
haftmann@62430
   269
haftmann@62430
   270
lemma in_diffD:
haftmann@62430
   271
  assumes "a \<in># M - N"
haftmann@62430
   272
  shows "a \<in># M"
haftmann@62430
   273
proof -
haftmann@62430
   274
  have "0 \<le> count N a" by simp
haftmann@62430
   275
  also from assms have "count N a < count M a"
haftmann@62430
   276
    by (simp add: in_diff_count)
haftmann@62430
   277
  finally show ?thesis by simp
haftmann@62430
   278
qed
haftmann@62430
   279
haftmann@62430
   280
lemma set_mset_diff:
haftmann@62430
   281
  "set_mset (M - N) = {a. count N a < count M a}"
haftmann@62430
   282
  by (simp add: set_mset_def)
haftmann@62430
   283
wenzelm@17161
   284
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
haftmann@52289
   285
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
nipkow@36903
   286
haftmann@62430
   287
lemma diff_cancel [simp]: "A - A = {#}"
haftmann@52289
   288
  by (fact Groups.diff_cancel)
wenzelm@10249
   289
nipkow@36903
   290
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
haftmann@52289
   291
  by (fact add_diff_cancel_right')
wenzelm@10249
   292
nipkow@36903
   293
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
haftmann@52289
   294
  by (fact add_diff_cancel_left')
haftmann@34943
   295
haftmann@52289
   296
lemma diff_right_commute:
wenzelm@60606
   297
  fixes M N Q :: "'a multiset"
wenzelm@60606
   298
  shows "M - N - Q = M - Q - N"
haftmann@52289
   299
  by (fact diff_right_commute)
haftmann@52289
   300
haftmann@52289
   301
lemma diff_add:
wenzelm@60606
   302
  fixes M N Q :: "'a multiset"
wenzelm@60606
   303
  shows "M - (N + Q) = M - N - Q"
haftmann@52289
   304
  by (rule sym) (fact diff_diff_add)
blanchet@58425
   305
wenzelm@60606
   306
lemma insert_DiffM: "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
nipkow@39302
   307
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   308
wenzelm@60606
   309
lemma insert_DiffM2 [simp]: "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
nipkow@39302
   310
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   311
wenzelm@60606
   312
lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
nipkow@39302
   313
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   314
haftmann@62430
   315
lemma diff_union_single_conv:
haftmann@62430
   316
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
haftmann@62430
   317
  by (simp add: multiset_eq_iff Suc_le_eq)
haftmann@62430
   318
haftmann@62430
   319
lemma mset_add [elim?]:
haftmann@62430
   320
  assumes "a \<in># A"
haftmann@62430
   321
  obtains B where "A = B + {#a#}"
haftmann@62430
   322
proof -
haftmann@62430
   323
  from assms have "A = (A - {#a#}) + {#a#}"
haftmann@62430
   324
    by simp
haftmann@62430
   325
  with that show thesis .
haftmann@62430
   326
qed
haftmann@62430
   327
haftmann@62430
   328
lemma union_iff:
haftmann@62430
   329
  "a \<in># A + B \<longleftrightarrow> a \<in># A \<or> a \<in># B"
haftmann@62430
   330
  by auto
bulwahn@26143
   331
wenzelm@10249
   332
wenzelm@60500
   333
subsubsection \<open>Equality of multisets\<close>
haftmann@34943
   334
haftmann@34943
   335
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
nipkow@39302
   336
  by (simp add: multiset_eq_iff)
haftmann@34943
   337
haftmann@34943
   338
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
nipkow@39302
   339
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   340
haftmann@34943
   341
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   342
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   343
haftmann@34943
   344
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   345
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   346
haftmann@34943
   347
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
nipkow@39302
   348
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   349
wenzelm@60606
   350
lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
haftmann@62430
   351
  by (auto simp add: multiset_eq_iff not_in_iff)
haftmann@34943
   352
wenzelm@60606
   353
lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
haftmann@34943
   354
  by auto
haftmann@34943
   355
wenzelm@60606
   356
lemma union_single_eq_diff: "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
haftmann@34943
   357
  by (auto dest: sym)
haftmann@34943
   358
wenzelm@60606
   359
lemma union_single_eq_member: "M + {#x#} = N \<Longrightarrow> x \<in># N"
haftmann@34943
   360
  by auto
haftmann@34943
   361
haftmann@62430
   362
lemma union_is_single:
haftmann@62430
   363
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N = {#} \<or> M = {#} \<and> N = {#a#}"
wenzelm@60606
   364
  (is "?lhs = ?rhs")
wenzelm@46730
   365
proof
wenzelm@60606
   366
  show ?lhs if ?rhs using that by auto
wenzelm@60606
   367
  show ?rhs if ?lhs
haftmann@62430
   368
    by (metis Multiset.diff_cancel add.commute add_diff_cancel_left' diff_add_zero diff_single_trivial insert_DiffM that)
haftmann@34943
   369
qed
haftmann@34943
   370
wenzelm@60606
   371
lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
haftmann@34943
   372
  by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
haftmann@34943
   373
haftmann@34943
   374
lemma add_eq_conv_diff:
wenzelm@60606
   375
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"
wenzelm@60606
   376
  (is "?lhs \<longleftrightarrow> ?rhs")
nipkow@44890
   377
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
haftmann@34943
   378
proof
wenzelm@60606
   379
  show ?lhs if ?rhs
wenzelm@60606
   380
    using that
wenzelm@60606
   381
    by (auto simp add: add.assoc add.commute [of "{#b#}"])
wenzelm@60606
   382
      (drule sym, simp add: add.assoc [symmetric])
wenzelm@60606
   383
  show ?rhs if ?lhs
haftmann@34943
   384
  proof (cases "a = b")
wenzelm@60500
   385
    case True with \<open>?lhs\<close> show ?thesis by simp
haftmann@34943
   386
  next
haftmann@34943
   387
    case False
wenzelm@60500
   388
    from \<open>?lhs\<close> have "a \<in># N + {#b#}" by (rule union_single_eq_member)
haftmann@34943
   389
    with False have "a \<in># N" by auto
wenzelm@60500
   390
    moreover from \<open>?lhs\<close> have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
haftmann@34943
   391
    moreover note False
haftmann@34943
   392
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
haftmann@34943
   393
  qed
haftmann@34943
   394
qed
haftmann@34943
   395
blanchet@58425
   396
lemma insert_noteq_member:
haftmann@34943
   397
  assumes BC: "B + {#b#} = C + {#c#}"
haftmann@34943
   398
   and bnotc: "b \<noteq> c"
haftmann@34943
   399
  shows "c \<in># B"
haftmann@34943
   400
proof -
haftmann@34943
   401
  have "c \<in># C + {#c#}" by simp
haftmann@34943
   402
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
haftmann@34943
   403
  then have "c \<in># B + {#b#}" using BC by simp
haftmann@34943
   404
  then show "c \<in># B" using nc by simp
haftmann@34943
   405
qed
haftmann@34943
   406
haftmann@34943
   407
lemma add_eq_conv_ex:
haftmann@34943
   408
  "(M + {#a#} = N + {#b#}) =
haftmann@34943
   409
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
haftmann@34943
   410
  by (auto simp add: add_eq_conv_diff)
haftmann@34943
   411
wenzelm@60606
   412
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
wenzelm@60678
   413
  by (rule exI [where x = "M - {#x#}"]) simp
haftmann@51600
   414
blanchet@58425
   415
lemma multiset_add_sub_el_shuffle:
wenzelm@60606
   416
  assumes "c \<in># B"
wenzelm@60606
   417
    and "b \<noteq> c"
haftmann@58098
   418
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
haftmann@58098
   419
proof -
wenzelm@60500
   420
  from \<open>c \<in># B\<close> obtain A where B: "B = A + {#c#}"
haftmann@58098
   421
    by (blast dest: multi_member_split)
haftmann@58098
   422
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
blanchet@58425
   423
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
haftmann@58098
   424
    by (simp add: ac_simps)
haftmann@58098
   425
  then show ?thesis using B by simp
haftmann@58098
   426
qed
haftmann@58098
   427
haftmann@34943
   428
wenzelm@60500
   429
subsubsection \<open>Pointwise ordering induced by count\<close>
haftmann@34943
   430
wenzelm@61955
   431
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<subseteq>#" 50)
wenzelm@61955
   432
  where "A \<subseteq># B = (\<forall>a. count A a \<le> count B a)"
wenzelm@61955
   433
wenzelm@61955
   434
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "\<subset>#" 50)
wenzelm@61955
   435
  where "A \<subset># B = (A \<subseteq># B \<and> A \<noteq> B)"
wenzelm@61955
   436
haftmann@62430
   437
abbreviation (input) supseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supseteq>#" 50)
haftmann@62430
   438
  where "supseteq_mset A B \<equiv> B \<subseteq># A"
haftmann@62430
   439
haftmann@62430
   440
abbreviation (input) supset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool"  (infix "\<supset>#" 50)
haftmann@62430
   441
  where "supset_mset A B \<equiv> B \<subset># A"
blanchet@62208
   442
wenzelm@61955
   443
notation (input)
blanchet@62208
   444
  subseteq_mset  (infix "\<le>#" 50) and
haftmann@62430
   445
  supseteq_mset  (infix "\<ge>#" 50)
wenzelm@61955
   446
wenzelm@61955
   447
notation (ASCII)
wenzelm@61955
   448
  subseteq_mset  (infix "<=#" 50) and
blanchet@62208
   449
  subset_mset  (infix "<#" 50) and
blanchet@62208
   450
  supseteq_mset  (infix ">=#" 50) and
blanchet@62208
   451
  supset_mset  (infix ">#" 50)
Mathias@60397
   452
wenzelm@60606
   453
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#"
wenzelm@60678
   454
  by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym)
wenzelm@62837
   455
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
haftmann@62430
   456
Mathias@63310
   457
lemma mset_subset_eqI:
haftmann@62430
   458
  "(\<And>a. count A a \<le> count B a) \<Longrightarrow> A \<subseteq># B"
Mathias@60397
   459
  by (simp add: subseteq_mset_def)
haftmann@34943
   460
Mathias@63310
   461
lemma mset_subset_eq_count:
haftmann@62430
   462
  "A \<subseteq># B \<Longrightarrow> count A a \<le> count B a"
haftmann@62430
   463
  by (simp add: subseteq_mset_def)
haftmann@62430
   464
Mathias@63310
   465
lemma mset_subset_eq_exists_conv: "(A::'a multiset) \<subseteq># B \<longleftrightarrow> (\<exists>C. B = A + C)"
wenzelm@60678
   466
  unfolding subseteq_mset_def
wenzelm@60678
   467
  apply (rule iffI)
wenzelm@60678
   468
   apply (rule exI [where x = "B - A"])
wenzelm@60678
   469
   apply (auto intro: multiset_eq_iff [THEN iffD2])
wenzelm@60678
   470
  done
haftmann@34943
   471
hoelzl@62376
   472
interpretation subset_mset: ordered_cancel_comm_monoid_diff  "op +" 0 "op \<le>#" "op <#" "op -"
Mathias@63310
   473
  by standard (simp, fact mset_subset_eq_exists_conv)
Mathias@63310
   474
Mathias@63310
   475
lemma mset_subset_eq_mono_add_right_cancel [simp]: "(A::'a multiset) + C \<subseteq># B + C \<longleftrightarrow> A \<subseteq># B"
haftmann@62430
   476
   by (fact subset_mset.add_le_cancel_right)
haftmann@62430
   477
 
Mathias@63310
   478
lemma mset_subset_eq_mono_add_left_cancel [simp]: "C + (A::'a multiset) \<subseteq># C + B \<longleftrightarrow> A \<subseteq># B"
haftmann@62430
   479
   by (fact subset_mset.add_le_cancel_left)
haftmann@62430
   480
 
Mathias@63310
   481
lemma mset_subset_eq_mono_add: "(A::'a multiset) \<subseteq># B \<Longrightarrow> C \<subseteq># D \<Longrightarrow> A + C \<subseteq># B + D"
haftmann@62430
   482
   by (fact subset_mset.add_mono)
haftmann@62430
   483
 
Mathias@63310
   484
lemma mset_subset_eq_add_left [simp]: "(A::'a multiset) \<subseteq># A + B"
haftmann@62430
   485
   unfolding subseteq_mset_def by auto
haftmann@62430
   486
 
Mathias@63310
   487
lemma mset_subset_eq_add_right [simp]: "B \<subseteq># (A::'a multiset) + B"
haftmann@62430
   488
   unfolding subseteq_mset_def by auto
haftmann@62430
   489
 
haftmann@62430
   490
lemma single_subset_iff [simp]:
haftmann@62430
   491
  "{#a#} \<subseteq># M \<longleftrightarrow> a \<in># M"
haftmann@62430
   492
  by (auto simp add: subseteq_mset_def Suc_le_eq)
haftmann@62430
   493
Mathias@63310
   494
lemma mset_subset_eq_single: "a \<in># B \<Longrightarrow> {#a#} \<subseteq># B"
haftmann@62430
   495
  by (simp add: subseteq_mset_def Suc_le_eq)
haftmann@62430
   496
 
haftmann@35268
   497
lemma multiset_diff_union_assoc:
wenzelm@60606
   498
  fixes A B C D :: "'a multiset"
haftmann@62430
   499
  shows "C \<subseteq># B \<Longrightarrow> A + B - C = A + (B - C)"
haftmann@62430
   500
  by (fact subset_mset.diff_add_assoc)
haftmann@62430
   501
 
Mathias@63310
   502
lemma mset_subset_eq_multiset_union_diff_commute:
wenzelm@60606
   503
  fixes A B C D :: "'a multiset"
haftmann@62430
   504
  shows "B \<subseteq># A \<Longrightarrow> A - B + C = A + C - B"
haftmann@62430
   505
  by (fact subset_mset.add_diff_assoc2)
haftmann@62430
   506
Mathias@63310
   507
lemma diff_subset_eq_self[simp]:
haftmann@62430
   508
  "(M::'a multiset) - N \<subseteq># M"
haftmann@62430
   509
  by (simp add: subseteq_mset_def)
haftmann@62430
   510
Mathias@63310
   511
lemma mset_subset_eqD:
haftmann@62430
   512
  assumes "A \<subseteq># B" and "x \<in># A"
haftmann@62430
   513
  shows "x \<in># B"
haftmann@62430
   514
proof -
haftmann@62430
   515
  from \<open>x \<in># A\<close> have "count A x > 0" by simp
haftmann@62430
   516
  also from \<open>A \<subseteq># B\<close> have "count A x \<le> count B x"
haftmann@62430
   517
    by (simp add: subseteq_mset_def)
haftmann@62430
   518
  finally show ?thesis by simp
haftmann@62430
   519
qed
haftmann@62430
   520
  
Mathias@63310
   521
lemma mset_subsetD:
haftmann@62430
   522
  "A \<subset># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
Mathias@63310
   523
  by (auto intro: mset_subset_eqD [of A])
haftmann@62430
   524
haftmann@62430
   525
lemma set_mset_mono:
haftmann@62430
   526
  "A \<subseteq># B \<Longrightarrow> set_mset A \<subseteq> set_mset B"
Mathias@63310
   527
  by (metis mset_subset_eqD subsetI)
Mathias@63310
   528
Mathias@63310
   529
lemma mset_subset_eq_insertD:
haftmann@62430
   530
  "A + {#x#} \<subseteq># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
haftmann@34943
   531
apply (rule conjI)
Mathias@63310
   532
 apply (simp add: mset_subset_eqD)
haftmann@62430
   533
 apply (clarsimp simp: subset_mset_def subseteq_mset_def)
haftmann@62430
   534
 apply safe
haftmann@62430
   535
  apply (erule_tac x = a in allE)
haftmann@62430
   536
  apply (auto split: if_split_asm)
haftmann@34943
   537
done
haftmann@34943
   538
Mathias@63310
   539
lemma mset_subset_insertD:
haftmann@62430
   540
  "A + {#x#} \<subset># B \<Longrightarrow> x \<in># B \<and> A \<subset># B"
Mathias@63310
   541
  by (rule mset_subset_eq_insertD) simp
Mathias@63310
   542
Mathias@63310
   543
lemma mset_subset_of_empty[simp]: "A \<subset># {#} \<longleftrightarrow> False"
Mathias@60397
   544
  by (auto simp add: subseteq_mset_def subset_mset_def multiset_eq_iff)
Mathias@60397
   545
haftmann@62430
   546
lemma empty_le [simp]: "{#} \<subseteq># A"
Mathias@63310
   547
  unfolding mset_subset_eq_exists_conv by auto
haftmann@62430
   548
 
haftmann@62430
   549
lemma insert_subset_eq_iff:
haftmann@62430
   550
  "{#a#} + A \<subseteq># B \<longleftrightarrow> a \<in># B \<and> A \<subseteq># B - {#a#}"
haftmann@62430
   551
  using le_diff_conv2 [of "Suc 0" "count B a" "count A a"]
haftmann@62430
   552
  apply (auto simp add: subseteq_mset_def not_in_iff Suc_le_eq)
haftmann@62430
   553
  apply (rule ccontr)
haftmann@62430
   554
  apply (auto simp add: not_in_iff)
haftmann@62430
   555
  done
haftmann@62430
   556
haftmann@62430
   557
lemma insert_union_subset_iff:
haftmann@62430
   558
  "{#a#} + A \<subset># B \<longleftrightarrow> a \<in># B \<and> A \<subset># B - {#a#}"
haftmann@62430
   559
  by (auto simp add: insert_subset_eq_iff subset_mset_def insert_DiffM)
haftmann@62430
   560
haftmann@62430
   561
lemma subset_eq_diff_conv:
haftmann@62430
   562
  "A - C \<subseteq># B \<longleftrightarrow> A \<subseteq># B + C"
haftmann@62430
   563
  by (simp add: subseteq_mset_def le_diff_conv)
haftmann@62430
   564
Mathias@63310
   565
lemma subset_eq_empty [simp]: "M \<subseteq># {#} \<longleftrightarrow> M = {#}"
Mathias@63310
   566
  unfolding mset_subset_eq_exists_conv by auto
haftmann@62430
   567
haftmann@62430
   568
lemma multi_psub_of_add_self[simp]: "A \<subset># A + {#x#}"
Mathias@60397
   569
  by (auto simp: subset_mset_def subseteq_mset_def)
Mathias@60397
   570
haftmann@62430
   571
lemma multi_psub_self[simp]: "(A::'a multiset) \<subset># A = False"
haftmann@35268
   572
  by simp
haftmann@34943
   573
Mathias@63310
   574
lemma mset_subset_add_bothsides: "N + {#x#} \<subset># M + {#x#} \<Longrightarrow> N \<subset># M"
Mathias@60397
   575
  by (fact subset_mset.add_less_imp_less_right)
haftmann@35268
   576
Mathias@63310
   577
lemma mset_subset_empty_nonempty: "{#} \<subset># S \<longleftrightarrow> S \<noteq> {#}"
hoelzl@62378
   578
  by (fact subset_mset.zero_less_iff_neq_zero)
haftmann@35268
   579
Mathias@63310
   580
lemma mset_subset_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} \<subset># B"
haftmann@62430
   581
  by (auto simp: subset_mset_def elim: mset_add)
haftmann@35268
   582
haftmann@35268
   583
wenzelm@60500
   584
subsubsection \<open>Intersection\<close>
haftmann@35268
   585
Mathias@60397
   586
definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
Mathias@60397
   587
  multiset_inter_def: "inf_subset_mset A B = A - (A - B)"
Mathias@60397
   588
haftmann@62430
   589
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<subseteq>#" "op \<subset>#"
wenzelm@46921
   590
proof -
wenzelm@60678
   591
  have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat
wenzelm@60678
   592
    by arith
haftmann@62430
   593
  show "class.semilattice_inf op #\<inter> op \<subseteq># op \<subset>#"
wenzelm@60678
   594
    by standard (auto simp add: multiset_inter_def subseteq_mset_def)
haftmann@35268
   595
qed
wenzelm@62837
   596
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
haftmann@34943
   597
haftmann@41069
   598
lemma multiset_inter_count [simp]:
wenzelm@60606
   599
  fixes A B :: "'a multiset"
wenzelm@60606
   600
  shows "count (A #\<inter> B) x = min (count A x) (count B x)"
bulwahn@47429
   601
  by (simp add: multiset_inter_def)
haftmann@35268
   602
haftmann@62430
   603
lemma set_mset_inter [simp]:
haftmann@62430
   604
  "set_mset (A #\<inter> B) = set_mset A \<inter> set_mset B"
haftmann@62430
   605
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] multiset_inter_count) simp
haftmann@62430
   606
haftmann@62430
   607
lemma diff_intersect_left_idem [simp]:
haftmann@62430
   608
  "M - M #\<inter> N = M - N"
haftmann@62430
   609
  by (simp add: multiset_eq_iff min_def)
haftmann@62430
   610
haftmann@62430
   611
lemma diff_intersect_right_idem [simp]:
haftmann@62430
   612
  "M - N #\<inter> M = M - N"
haftmann@62430
   613
  by (simp add: multiset_eq_iff min_def)
haftmann@62430
   614
haftmann@35268
   615
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
wenzelm@46730
   616
  by (rule multiset_eqI) auto
haftmann@34943
   617
haftmann@35268
   618
lemma multiset_union_diff_commute:
haftmann@35268
   619
  assumes "B #\<inter> C = {#}"
haftmann@35268
   620
  shows "A + B - C = A - C + B"
nipkow@39302
   621
proof (rule multiset_eqI)
haftmann@35268
   622
  fix x
haftmann@35268
   623
  from assms have "min (count B x) (count C x) = 0"
wenzelm@46730
   624
    by (auto simp add: multiset_eq_iff)
haftmann@35268
   625
  then have "count B x = 0 \<or> count C x = 0"
haftmann@62430
   626
    unfolding min_def by (auto split: if_splits)
haftmann@35268
   627
  then show "count (A + B - C) x = count (A - C + B) x"
haftmann@35268
   628
    by auto
haftmann@35268
   629
qed
haftmann@35268
   630
haftmann@62430
   631
lemma disjunct_not_in:
haftmann@62430
   632
  "A #\<inter> B = {#} \<longleftrightarrow> (\<forall>a. a \<notin># A \<or> a \<notin># B)" (is "?P \<longleftrightarrow> ?Q")
haftmann@62430
   633
proof
haftmann@62430
   634
  assume ?P
haftmann@62430
   635
  show ?Q
haftmann@62430
   636
  proof
haftmann@62430
   637
    fix a
haftmann@62430
   638
    from \<open>?P\<close> have "min (count A a) (count B a) = 0"
haftmann@62430
   639
      by (simp add: multiset_eq_iff)
haftmann@62430
   640
    then have "count A a = 0 \<or> count B a = 0"
haftmann@62430
   641
      by (cases "count A a \<le> count B a") (simp_all add: min_def)
haftmann@62430
   642
    then show "a \<notin># A \<or> a \<notin># B"
haftmann@62430
   643
      by (simp add: not_in_iff)
haftmann@62430
   644
  qed
haftmann@62430
   645
next
haftmann@62430
   646
  assume ?Q
haftmann@62430
   647
  show ?P
haftmann@62430
   648
  proof (rule multiset_eqI)
haftmann@62430
   649
    fix a
haftmann@62430
   650
    from \<open>?Q\<close> have "count A a = 0 \<or> count B a = 0"
haftmann@62430
   651
      by (auto simp add: not_in_iff)
haftmann@62430
   652
    then show "count (A #\<inter> B) a = count {#} a"
haftmann@62430
   653
      by auto
haftmann@62430
   654
  qed
haftmann@62430
   655
qed
haftmann@62430
   656
wenzelm@60606
   657
lemma empty_inter [simp]: "{#} #\<inter> M = {#}"
haftmann@51600
   658
  by (simp add: multiset_eq_iff)
haftmann@51600
   659
wenzelm@60606
   660
lemma inter_empty [simp]: "M #\<inter> {#} = {#}"
haftmann@51600
   661
  by (simp add: multiset_eq_iff)
haftmann@51600
   662
wenzelm@60606
   663
lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
haftmann@62430
   664
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@51600
   665
wenzelm@60606
   666
lemma inter_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
haftmann@62430
   667
  by (auto simp add: multiset_eq_iff elim: mset_add)
haftmann@51600
   668
wenzelm@60606
   669
lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
haftmann@62430
   670
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@51600
   671
wenzelm@60606
   672
lemma inter_add_right2: "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
haftmann@62430
   673
  by (auto simp add: multiset_eq_iff elim: mset_add)
haftmann@62430
   674
haftmann@62430
   675
lemma disjunct_set_mset_diff:
haftmann@62430
   676
  assumes "M #\<inter> N = {#}"
haftmann@62430
   677
  shows "set_mset (M - N) = set_mset M"
haftmann@62430
   678
proof (rule set_eqI)
haftmann@62430
   679
  fix a
haftmann@62430
   680
  from assms have "a \<notin># M \<or> a \<notin># N"
haftmann@62430
   681
    by (simp add: disjunct_not_in)
haftmann@62430
   682
  then show "a \<in># M - N \<longleftrightarrow> a \<in># M"
haftmann@62430
   683
    by (auto dest: in_diffD) (simp add: in_diff_count not_in_iff)
haftmann@62430
   684
qed
haftmann@62430
   685
haftmann@62430
   686
lemma at_most_one_mset_mset_diff:
haftmann@62430
   687
  assumes "a \<notin># M - {#a#}"
haftmann@62430
   688
  shows "set_mset (M - {#a#}) = set_mset M - {a}"
haftmann@62430
   689
  using assms by (auto simp add: not_in_iff in_diff_count set_eq_iff)
haftmann@62430
   690
haftmann@62430
   691
lemma more_than_one_mset_mset_diff:
haftmann@62430
   692
  assumes "a \<in># M - {#a#}"
haftmann@62430
   693
  shows "set_mset (M - {#a#}) = set_mset M"
haftmann@62430
   694
proof (rule set_eqI)
haftmann@62430
   695
  fix b
haftmann@62430
   696
  have "Suc 0 < count M b \<Longrightarrow> count M b > 0" by arith
haftmann@62430
   697
  then show "b \<in># M - {#a#} \<longleftrightarrow> b \<in># M"
haftmann@62430
   698
    using assms by (auto simp add: in_diff_count)
haftmann@62430
   699
qed
haftmann@62430
   700
haftmann@62430
   701
lemma inter_iff:
haftmann@62430
   702
  "a \<in># A #\<inter> B \<longleftrightarrow> a \<in># A \<and> a \<in># B"
haftmann@62430
   703
  by simp
haftmann@62430
   704
haftmann@62430
   705
lemma inter_union_distrib_left:
haftmann@62430
   706
  "A #\<inter> B + C = (A + C) #\<inter> (B + C)"
haftmann@62430
   707
  by (simp add: multiset_eq_iff min_add_distrib_left)
haftmann@62430
   708
haftmann@62430
   709
lemma inter_union_distrib_right:
haftmann@62430
   710
  "C + A #\<inter> B = (C + A) #\<inter> (C + B)"
haftmann@62430
   711
  using inter_union_distrib_left [of A B C] by (simp add: ac_simps)
haftmann@62430
   712
haftmann@62430
   713
lemma inter_subset_eq_union:
haftmann@62430
   714
  "A #\<inter> B \<subseteq># A + B"
haftmann@62430
   715
  by (auto simp add: subseteq_mset_def)
haftmann@51600
   716
haftmann@35268
   717
wenzelm@60500
   718
subsubsection \<open>Bounded union\<close>
wenzelm@60678
   719
wenzelm@60678
   720
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "#\<union>" 70)
wenzelm@62837
   721
  where "sup_subset_mset A B = A + (B - A)" \<comment> \<open>FIXME irregular fact name\<close>
haftmann@62430
   722
haftmann@62430
   723
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<subseteq>#" "op \<subset>#"
haftmann@51623
   724
proof -
wenzelm@60678
   725
  have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat
wenzelm@60678
   726
    by arith
haftmann@62430
   727
  show "class.semilattice_sup op #\<union> op \<subseteq># op \<subset>#"
wenzelm@60678
   728
    by standard (auto simp add: sup_subset_mset_def subseteq_mset_def)
haftmann@51623
   729
qed
wenzelm@62837
   730
  \<comment> \<open>FIXME: avoid junk stemming from type class interpretation\<close>
wenzelm@62837
   731
wenzelm@62837
   732
lemma sup_subset_mset_count [simp]: \<comment> \<open>FIXME irregular fact name\<close>
haftmann@62430
   733
  "count (A #\<union> B) x = max (count A x) (count B x)"
Mathias@60397
   734
  by (simp add: sup_subset_mset_def)
haftmann@51623
   735
haftmann@62430
   736
lemma set_mset_sup [simp]:
haftmann@62430
   737
  "set_mset (A #\<union> B) = set_mset A \<union> set_mset B"
haftmann@62430
   738
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] sup_subset_mset_count)
haftmann@62430
   739
    (auto simp add: not_in_iff elim: mset_add)
haftmann@62430
   740
wenzelm@60606
   741
lemma empty_sup [simp]: "{#} #\<union> M = M"
haftmann@51623
   742
  by (simp add: multiset_eq_iff)
haftmann@51623
   743
wenzelm@60606
   744
lemma sup_empty [simp]: "M #\<union> {#} = M"
haftmann@51623
   745
  by (simp add: multiset_eq_iff)
haftmann@51623
   746
haftmann@62430
   747
lemma sup_union_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
haftmann@62430
   748
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@62430
   749
haftmann@62430
   750
lemma sup_union_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
haftmann@51623
   751
  by (simp add: multiset_eq_iff)
haftmann@51623
   752
haftmann@62430
   753
lemma sup_union_right1: "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
haftmann@62430
   754
  by (simp add: multiset_eq_iff not_in_iff)
haftmann@62430
   755
haftmann@62430
   756
lemma sup_union_right2: "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
haftmann@51623
   757
  by (simp add: multiset_eq_iff)
haftmann@51623
   758
haftmann@62430
   759
lemma sup_union_distrib_left:
haftmann@62430
   760
  "A #\<union> B + C = (A + C) #\<union> (B + C)"
haftmann@62430
   761
  by (simp add: multiset_eq_iff max_add_distrib_left)
haftmann@62430
   762
haftmann@62430
   763
lemma union_sup_distrib_right:
haftmann@62430
   764
  "C + A #\<union> B = (C + A) #\<union> (C + B)"
haftmann@62430
   765
  using sup_union_distrib_left [of A B C] by (simp add: ac_simps)
haftmann@62430
   766
haftmann@62430
   767
lemma union_diff_inter_eq_sup:
haftmann@62430
   768
  "A + B - A #\<inter> B = A #\<union> B"
haftmann@62430
   769
  by (auto simp add: multiset_eq_iff)
haftmann@62430
   770
haftmann@62430
   771
lemma union_diff_sup_eq_inter:
haftmann@62430
   772
  "A + B - A #\<union> B = A #\<inter> B"
haftmann@62430
   773
  by (auto simp add: multiset_eq_iff)
haftmann@62430
   774
haftmann@51623
   775
wenzelm@60500
   776
subsubsection \<open>Subset is an order\<close>
haftmann@62430
   777
Mathias@60397
   778
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto
haftmann@51623
   779
blanchet@63409
   780
eberlm@63358
   781
subsubsection \<open>Conditionally complete lattice\<close>
eberlm@63358
   782
eberlm@63358
   783
instantiation multiset :: (type) Inf
eberlm@63358
   784
begin
eberlm@63358
   785
eberlm@63358
   786
lift_definition Inf_multiset :: "'a multiset set \<Rightarrow> 'a multiset" is
eberlm@63358
   787
  "\<lambda>A i. if A = {} then 0 else Inf ((\<lambda>f. f i) ` A)"
eberlm@63358
   788
proof -
eberlm@63358
   789
  fix A :: "('a \<Rightarrow> nat) set" assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<in> multiset"
eberlm@63358
   790
  have "finite {i. (if A = {} then 0 else Inf ((\<lambda>f. f i) ` A)) > 0}" unfolding multiset_def
eberlm@63358
   791
  proof (cases "A = {}")
eberlm@63358
   792
    case False
eberlm@63358
   793
    then obtain f where "f \<in> A" by blast
eberlm@63358
   794
    hence "{i. Inf ((\<lambda>f. f i) ` A) > 0} \<subseteq> {i. f i > 0}"
eberlm@63358
   795
      by (auto intro: less_le_trans[OF _ cInf_lower])
eberlm@63358
   796
    moreover from \<open>f \<in> A\<close> * have "finite \<dots>" by (simp add: multiset_def)
eberlm@63358
   797
    ultimately have "finite {i. Inf ((\<lambda>f. f i) ` A) > 0}" by (rule finite_subset)
eberlm@63358
   798
    with False show ?thesis by simp
eberlm@63358
   799
  qed simp_all
eberlm@63358
   800
  thus "(\<lambda>i. if A = {} then 0 else INF f:A. f i) \<in> multiset" by (simp add: multiset_def)
eberlm@63358
   801
qed
eberlm@63358
   802
eberlm@63358
   803
instance ..
eberlm@63358
   804
eberlm@63358
   805
end
eberlm@63358
   806
eberlm@63358
   807
lemma Inf_multiset_empty: "Inf {} = {#}"
eberlm@63358
   808
  by transfer simp_all
eberlm@63358
   809
eberlm@63358
   810
lemma count_Inf_multiset_nonempty: "A \<noteq> {} \<Longrightarrow> count (Inf A) x = Inf ((\<lambda>X. count X x) ` A)"
eberlm@63358
   811
  by transfer simp_all
eberlm@63358
   812
eberlm@63358
   813
eberlm@63358
   814
instantiation multiset :: (type) Sup
eberlm@63358
   815
begin
eberlm@63358
   816
eberlm@63360
   817
definition Sup_multiset :: "'a multiset set \<Rightarrow> 'a multiset" where
eberlm@63360
   818
  "Sup_multiset A = (if A \<noteq> {} \<and> subset_mset.bdd_above A then
eberlm@63360
   819
           Abs_multiset (\<lambda>i. Sup ((\<lambda>X. count X i) ` A)) else {#})"
eberlm@63360
   820
eberlm@63360
   821
lemma Sup_multiset_empty: "Sup {} = {#}"
eberlm@63360
   822
  by (simp add: Sup_multiset_def)
eberlm@63360
   823
eberlm@63360
   824
lemma Sup_multiset_unbounded: "\<not>subset_mset.bdd_above A \<Longrightarrow> Sup A = {#}"
eberlm@63360
   825
  by (simp add: Sup_multiset_def)
eberlm@63358
   826
eberlm@63358
   827
instance ..
eberlm@63358
   828
eberlm@63358
   829
end
eberlm@63358
   830
eberlm@63358
   831
lemma bdd_below_multiset [simp]: "subset_mset.bdd_below A"
eberlm@63358
   832
  by (intro subset_mset.bdd_belowI[of _ "{#}"]) simp_all
eberlm@63358
   833
eberlm@63358
   834
lemma bdd_above_multiset_imp_bdd_above_count:
eberlm@63358
   835
  assumes "subset_mset.bdd_above (A :: 'a multiset set)"
eberlm@63358
   836
  shows   "bdd_above ((\<lambda>X. count X x) ` A)"
eberlm@63358
   837
proof -
eberlm@63358
   838
  from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y"
eberlm@63358
   839
    by (auto simp: subset_mset.bdd_above_def)
eberlm@63358
   840
  hence "count X x \<le> count Y x" if "X \<in> A" for X
eberlm@63358
   841
    using that by (auto intro: mset_subset_eq_count)
eberlm@63358
   842
  thus ?thesis by (intro bdd_aboveI[of _ "count Y x"]) auto
eberlm@63358
   843
qed
eberlm@63358
   844
eberlm@63358
   845
lemma bdd_above_multiset_imp_finite_support:
eberlm@63358
   846
  assumes "A \<noteq> {}" "subset_mset.bdd_above (A :: 'a multiset set)"
eberlm@63358
   847
  shows   "finite (\<Union>X\<in>A. {x. count X x > 0})"
eberlm@63358
   848
proof -
eberlm@63358
   849
  from assms obtain Y where Y: "\<forall>X\<in>A. X \<subseteq># Y"
eberlm@63358
   850
    by (auto simp: subset_mset.bdd_above_def)
eberlm@63358
   851
  hence "count X x \<le> count Y x" if "X \<in> A" for X x
eberlm@63358
   852
    using that by (auto intro: mset_subset_eq_count)
eberlm@63358
   853
  hence "(\<Union>X\<in>A. {x. count X x > 0}) \<subseteq> {x. count Y x > 0}"
eberlm@63358
   854
    by safe (erule less_le_trans)
eberlm@63358
   855
  moreover have "finite \<dots>" by simp
eberlm@63358
   856
  ultimately show ?thesis by (rule finite_subset)
eberlm@63358
   857
qed
eberlm@63358
   858
eberlm@63360
   859
lemma Sup_multiset_in_multiset:
eberlm@63360
   860
  assumes "A \<noteq> {}" "subset_mset.bdd_above A"
eberlm@63360
   861
  shows   "(\<lambda>i. SUP X:A. count X i) \<in> multiset"
eberlm@63360
   862
  unfolding multiset_def
eberlm@63360
   863
proof
eberlm@63360
   864
  have "{i. Sup ((\<lambda>X. count X i) ` A) > 0} \<subseteq> (\<Union>X\<in>A. {i. 0 < count X i})"
eberlm@63360
   865
  proof safe
eberlm@63360
   866
    fix i assume pos: "(SUP X:A. count X i) > 0"
eberlm@63360
   867
    show "i \<in> (\<Union>X\<in>A. {i. 0 < count X i})"
eberlm@63360
   868
    proof (rule ccontr)
eberlm@63360
   869
      assume "i \<notin> (\<Union>X\<in>A. {i. 0 < count X i})"
eberlm@63360
   870
      hence "\<forall>X\<in>A. count X i \<le> 0" by (auto simp: count_eq_zero_iff)
eberlm@63360
   871
      with assms have "(SUP X:A. count X i) \<le> 0"
eberlm@63360
   872
        by (intro cSup_least bdd_above_multiset_imp_bdd_above_count) auto
eberlm@63360
   873
      with pos show False by simp
eberlm@63360
   874
    qed
eberlm@63360
   875
  qed
eberlm@63360
   876
  moreover from assms have "finite \<dots>" by (rule bdd_above_multiset_imp_finite_support)
eberlm@63360
   877
  ultimately show "finite {i. Sup ((\<lambda>X. count X i) ` A) > 0}" by (rule finite_subset)
eberlm@63360
   878
qed
eberlm@63360
   879
eberlm@63358
   880
lemma count_Sup_multiset_nonempty:
eberlm@63358
   881
  assumes "A \<noteq> {}" "subset_mset.bdd_above A"
eberlm@63358
   882
  shows   "count (Sup A) x = (SUP X:A. count X x)"
eberlm@63360
   883
  using assms by (simp add: Sup_multiset_def Abs_multiset_inverse Sup_multiset_in_multiset)
eberlm@63358
   884
eberlm@63358
   885
eberlm@63358
   886
interpretation subset_mset: conditionally_complete_lattice Inf Sup "op #\<inter>" "op \<subseteq>#" "op \<subset>#" "op #\<union>"
eberlm@63358
   887
proof
eberlm@63358
   888
  fix X :: "'a multiset" and A
eberlm@63358
   889
  assume "X \<in> A"
eberlm@63358
   890
  show "Inf A \<subseteq># X"
eberlm@63358
   891
  proof (rule mset_subset_eqI)
eberlm@63358
   892
    fix x
eberlm@63358
   893
    from \<open>X \<in> A\<close> have "A \<noteq> {}" by auto
eberlm@63358
   894
    hence "count (Inf A) x = (INF X:A. count X x)"
eberlm@63358
   895
      by (simp add: count_Inf_multiset_nonempty)
eberlm@63358
   896
    also from \<open>X \<in> A\<close> have "\<dots> \<le> count X x"
eberlm@63358
   897
      by (intro cInf_lower) simp_all
eberlm@63358
   898
    finally show "count (Inf A) x \<le> count X x" .
eberlm@63358
   899
  qed
eberlm@63358
   900
next
eberlm@63358
   901
  fix X :: "'a multiset" and A
eberlm@63358
   902
  assume nonempty: "A \<noteq> {}" and le: "\<And>Y. Y \<in> A \<Longrightarrow> X \<subseteq># Y"
eberlm@63358
   903
  show "X \<subseteq># Inf A"
eberlm@63358
   904
  proof (rule mset_subset_eqI)
eberlm@63358
   905
    fix x
eberlm@63358
   906
    from nonempty have "count X x \<le> (INF X:A. count X x)"
eberlm@63358
   907
      by (intro cInf_greatest) (auto intro: mset_subset_eq_count le)
eberlm@63358
   908
    also from nonempty have "\<dots> = count (Inf A) x" by (simp add: count_Inf_multiset_nonempty)
eberlm@63358
   909
    finally show "count X x \<le> count (Inf A) x" .
eberlm@63358
   910
  qed
eberlm@63358
   911
next
eberlm@63358
   912
  fix X :: "'a multiset" and A
eberlm@63358
   913
  assume X: "X \<in> A" and bdd: "subset_mset.bdd_above A"
eberlm@63358
   914
  show "X \<subseteq># Sup A"
eberlm@63358
   915
  proof (rule mset_subset_eqI)
eberlm@63358
   916
    fix x
eberlm@63358
   917
    from X have "A \<noteq> {}" by auto
eberlm@63358
   918
    have "count X x \<le> (SUP X:A. count X x)"
eberlm@63358
   919
      by (intro cSUP_upper X bdd_above_multiset_imp_bdd_above_count bdd)
eberlm@63358
   920
    also from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
eberlm@63358
   921
      have "(SUP X:A. count X x) = count (Sup A) x" by simp
eberlm@63358
   922
    finally show "count X x \<le> count (Sup A) x" .
eberlm@63358
   923
  qed
eberlm@63358
   924
next
eberlm@63358
   925
  fix X :: "'a multiset" and A
eberlm@63358
   926
  assume nonempty: "A \<noteq> {}" and ge: "\<And>Y. Y \<in> A \<Longrightarrow> Y \<subseteq># X"
eberlm@63358
   927
  from ge have bdd: "subset_mset.bdd_above A" by (rule subset_mset.bdd_aboveI[of _ X])
eberlm@63358
   928
  show "Sup A \<subseteq># X"
eberlm@63358
   929
  proof (rule mset_subset_eqI)
eberlm@63358
   930
    fix x
eberlm@63358
   931
    from count_Sup_multiset_nonempty[OF \<open>A \<noteq> {}\<close> bdd]
eberlm@63358
   932
      have "count (Sup A) x = (SUP X:A. count X x)" .
eberlm@63358
   933
    also from nonempty have "\<dots> \<le> count X x"
eberlm@63358
   934
      by (intro cSup_least) (auto intro: mset_subset_eq_count ge)
eberlm@63358
   935
    finally show "count (Sup A) x \<le> count X x" .
eberlm@63358
   936
  qed
eberlm@63358
   937
qed
eberlm@63358
   938
eberlm@63358
   939
lemma set_mset_Inf:
eberlm@63358
   940
  assumes "A \<noteq> {}"
eberlm@63358
   941
  shows   "set_mset (Inf A) = (\<Inter>X\<in>A. set_mset X)"
eberlm@63358
   942
proof safe
eberlm@63358
   943
  fix x X assume "x \<in># Inf A" "X \<in> A"
eberlm@63358
   944
  hence nonempty: "A \<noteq> {}" by (auto simp: Inf_multiset_empty)
eberlm@63358
   945
  from \<open>x \<in># Inf A\<close> have "{#x#} \<subseteq># Inf A" by auto
eberlm@63358
   946
  also from \<open>X \<in> A\<close> have "\<dots> \<subseteq># X" by (rule subset_mset.cInf_lower) simp_all
eberlm@63358
   947
  finally show "x \<in># X" by simp
eberlm@63358
   948
next
eberlm@63358
   949
  fix x assume x: "x \<in> (\<Inter>X\<in>A. set_mset X)"
eberlm@63358
   950
  hence "{#x#} \<subseteq># X" if "X \<in> A" for X using that by auto
eberlm@63358
   951
  from assms and this have "{#x#} \<subseteq># Inf A" by (rule subset_mset.cInf_greatest)
eberlm@63358
   952
  thus "x \<in># Inf A" by simp
eberlm@63358
   953
qed
eberlm@63358
   954
eberlm@63358
   955
lemma in_Inf_multiset_iff:
eberlm@63358
   956
  assumes "A \<noteq> {}"
eberlm@63358
   957
  shows   "x \<in># Inf A \<longleftrightarrow> (\<forall>X\<in>A. x \<in># X)"
eberlm@63358
   958
proof -
eberlm@63358
   959
  from assms have "set_mset (Inf A) = (\<Inter>X\<in>A. set_mset X)" by (rule set_mset_Inf)
eberlm@63358
   960
  also have "x \<in> \<dots> \<longleftrightarrow> (\<forall>X\<in>A. x \<in># X)" by simp
eberlm@63358
   961
  finally show ?thesis .
eberlm@63358
   962
qed
eberlm@63358
   963
eberlm@63360
   964
lemma in_Inf_multisetD: "x \<in># Inf A \<Longrightarrow> X \<in> A \<Longrightarrow> x \<in># X"
eberlm@63360
   965
  by (subst (asm) in_Inf_multiset_iff) auto
eberlm@63360
   966
eberlm@63358
   967
lemma set_mset_Sup:
eberlm@63358
   968
  assumes "subset_mset.bdd_above A"
eberlm@63358
   969
  shows   "set_mset (Sup A) = (\<Union>X\<in>A. set_mset X)"
eberlm@63358
   970
proof safe
eberlm@63358
   971
  fix x assume "x \<in># Sup A"
eberlm@63358
   972
  hence nonempty: "A \<noteq> {}" by (auto simp: Sup_multiset_empty)
eberlm@63358
   973
  show "x \<in> (\<Union>X\<in>A. set_mset X)"
eberlm@63358
   974
  proof (rule ccontr)
eberlm@63358
   975
    assume x: "x \<notin> (\<Union>X\<in>A. set_mset X)"
eberlm@63358
   976
    have "count X x \<le> count (Sup A) x" if "X \<in> A" for X x
eberlm@63358
   977
      using that by (intro mset_subset_eq_count subset_mset.cSup_upper assms)
eberlm@63358
   978
    with x have "X \<subseteq># Sup A - {#x#}" if "X \<in> A" for X
eberlm@63358
   979
      using that by (auto simp: subseteq_mset_def algebra_simps not_in_iff)
eberlm@63358
   980
    hence "Sup A \<subseteq># Sup A - {#x#}" by (intro subset_mset.cSup_least nonempty)
eberlm@63358
   981
    with \<open>x \<in># Sup A\<close> show False
eberlm@63358
   982
      by (auto simp: subseteq_mset_def count_greater_zero_iff [symmetric]
eberlm@63358
   983
               simp del: count_greater_zero_iff dest!: spec[of _ x])
eberlm@63358
   984
  qed
eberlm@63358
   985
next
eberlm@63358
   986
  fix x X assume "x \<in> set_mset X" "X \<in> A"
eberlm@63358
   987
  hence "{#x#} \<subseteq># X" by auto
eberlm@63358
   988
  also have "X \<subseteq># Sup A" by (intro subset_mset.cSup_upper \<open>X \<in> A\<close> assms)
eberlm@63358
   989
  finally show "x \<in> set_mset (Sup A)" by simp
eberlm@63358
   990
qed
eberlm@63358
   991
eberlm@63358
   992
lemma in_Sup_multiset_iff:
eberlm@63358
   993
  assumes "subset_mset.bdd_above A"
eberlm@63358
   994
  shows   "x \<in># Sup A \<longleftrightarrow> (\<exists>X\<in>A. x \<in># X)"
eberlm@63358
   995
proof -
eberlm@63358
   996
  from assms have "set_mset (Sup A) = (\<Union>X\<in>A. set_mset X)" by (rule set_mset_Sup)
eberlm@63358
   997
  also have "x \<in> \<dots> \<longleftrightarrow> (\<exists>X\<in>A. x \<in># X)" by simp
eberlm@63358
   998
  finally show ?thesis .
eberlm@63358
   999
qed
eberlm@63358
  1000
eberlm@63360
  1001
lemma in_Sup_multisetD: 
eberlm@63360
  1002
  assumes "x \<in># Sup A"
eberlm@63360
  1003
  shows   "\<exists>X\<in>A. x \<in># X"
eberlm@63360
  1004
proof -
eberlm@63360
  1005
  have "subset_mset.bdd_above A"
eberlm@63360
  1006
    by (rule ccontr) (insert assms, simp_all add: Sup_multiset_unbounded)
eberlm@63360
  1007
  with assms show ?thesis by (simp add: in_Sup_multiset_iff)
eberlm@63360
  1008
qed    
eberlm@63360
  1009
haftmann@62430
  1010
wenzelm@60500
  1011
subsubsection \<open>Filter (with comprehension syntax)\<close>
wenzelm@60500
  1012
wenzelm@60500
  1013
text \<open>Multiset comprehension\<close>
haftmann@41069
  1014
nipkow@59998
  1015
lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
nipkow@59998
  1016
is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
bulwahn@47429
  1017
by (rule filter_preserves_multiset)
haftmann@35268
  1018
haftmann@62430
  1019
syntax (ASCII)
haftmann@62430
  1020
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
haftmann@62430
  1021
syntax
haftmann@62430
  1022
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
haftmann@62430
  1023
translations
haftmann@62430
  1024
  "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
haftmann@62430
  1025
haftmann@62430
  1026
lemma count_filter_mset [simp]:
haftmann@62430
  1027
  "count (filter_mset P M) a = (if P a then count M a else 0)"
nipkow@59998
  1028
  by (simp add: filter_mset.rep_eq)
nipkow@59998
  1029
haftmann@62430
  1030
lemma set_mset_filter [simp]:
haftmann@62430
  1031
  "set_mset (filter_mset P M) = {a \<in> set_mset M. P a}"
haftmann@62430
  1032
  by (simp only: set_eq_iff count_greater_zero_iff [symmetric] count_filter_mset) simp
haftmann@62430
  1033
wenzelm@60606
  1034
lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
nipkow@59998
  1035
  by (rule multiset_eqI) simp
nipkow@59998
  1036
wenzelm@60606
  1037
lemma filter_single_mset [simp]: "filter_mset P {#x#} = (if P x then {#x#} else {#})"
nipkow@39302
  1038
  by (rule multiset_eqI) simp
haftmann@35268
  1039
wenzelm@60606
  1040
lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N"
haftmann@41069
  1041
  by (rule multiset_eqI) simp
haftmann@41069
  1042
wenzelm@60606
  1043
lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N"
nipkow@39302
  1044
  by (rule multiset_eqI) simp
haftmann@35268
  1045
wenzelm@60606
  1046
lemma filter_inter_mset [simp]: "filter_mset P (M #\<inter> N) = filter_mset P M #\<inter> filter_mset P N"
haftmann@41069
  1047
  by (rule multiset_eqI) simp
haftmann@41069
  1048
haftmann@62430
  1049
lemma multiset_filter_subset[simp]: "filter_mset f M \<subseteq># M"
Mathias@63310
  1050
  by (simp add: mset_subset_eqI)
Mathias@60397
  1051
wenzelm@60606
  1052
lemma multiset_filter_mono:
haftmann@62430
  1053
  assumes "A \<subseteq># B"
haftmann@62430
  1054
  shows "filter_mset f A \<subseteq># filter_mset f B"
blanchet@58035
  1055
proof -
Mathias@63310
  1056
  from assms[unfolded mset_subset_eq_exists_conv]
blanchet@58035
  1057
  obtain C where B: "B = A + C" by auto
blanchet@58035
  1058
  show ?thesis unfolding B by auto
blanchet@58035
  1059
qed
blanchet@58035
  1060
haftmann@62430
  1061
lemma filter_mset_eq_conv:
haftmann@62430
  1062
  "filter_mset P M = N \<longleftrightarrow> N \<subseteq># M \<and> (\<forall>b\<in>#N. P b) \<and> (\<forall>a\<in>#M - N. \<not> P a)" (is "?P \<longleftrightarrow> ?Q")
haftmann@62430
  1063
proof
haftmann@62430
  1064
  assume ?P then show ?Q by auto (simp add: multiset_eq_iff in_diff_count)
haftmann@62430
  1065
next
haftmann@62430
  1066
  assume ?Q
haftmann@62430
  1067
  then obtain Q where M: "M = N + Q"
Mathias@63310
  1068
    by (auto simp add: mset_subset_eq_exists_conv)
haftmann@62430
  1069
  then have MN: "M - N = Q" by simp
haftmann@62430
  1070
  show ?P
haftmann@62430
  1071
  proof (rule multiset_eqI)
haftmann@62430
  1072
    fix a
haftmann@62430
  1073
    from \<open>?Q\<close> MN have *: "\<not> P a \<Longrightarrow> a \<notin># N" "P a \<Longrightarrow> a \<notin># Q"
haftmann@62430
  1074
      by auto
haftmann@62430
  1075
    show "count (filter_mset P M) a = count N a"
haftmann@62430
  1076
    proof (cases "a \<in># M")
haftmann@62430
  1077
      case True
haftmann@62430
  1078
      with * show ?thesis
haftmann@62430
  1079
        by (simp add: not_in_iff M)
haftmann@62430
  1080
    next
haftmann@62430
  1081
      case False then have "count M a = 0"
haftmann@62430
  1082
        by (simp add: not_in_iff)
haftmann@62430
  1083
      with M show ?thesis by simp
haftmann@62430
  1084
    qed 
haftmann@62430
  1085
  qed
haftmann@62430
  1086
qed
blanchet@59813
  1087
blanchet@59813
  1088
wenzelm@60500
  1089
subsubsection \<open>Size\<close>
wenzelm@10249
  1090
blanchet@56656
  1091
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
blanchet@56656
  1092
blanchet@56656
  1093
lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a"
blanchet@56656
  1094
  by (auto simp: wcount_def add_mult_distrib)
blanchet@56656
  1095
blanchet@56656
  1096
definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
nipkow@60495
  1097
  "size_multiset f M = setsum (wcount f M) (set_mset M)"
blanchet@56656
  1098
blanchet@56656
  1099
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
blanchet@56656
  1100
wenzelm@60606
  1101
instantiation multiset :: (type) size
wenzelm@60606
  1102
begin
wenzelm@60606
  1103
blanchet@56656
  1104
definition size_multiset where
blanchet@56656
  1105
  size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)"
haftmann@34943
  1106
instance ..
wenzelm@60606
  1107
haftmann@34943
  1108
end
haftmann@34943
  1109
blanchet@56656
  1110
lemmas size_multiset_overloaded_eq =
blanchet@56656
  1111
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
blanchet@56656
  1112
blanchet@56656
  1113
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
blanchet@56656
  1114
by (simp add: size_multiset_def)
blanchet@56656
  1115
haftmann@28708
  1116
lemma size_empty [simp]: "size {#} = 0"
blanchet@56656
  1117
by (simp add: size_multiset_overloaded_def)
blanchet@56656
  1118
blanchet@56656
  1119
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"
blanchet@56656
  1120
by (simp add: size_multiset_eq)
wenzelm@10249
  1121
haftmann@28708
  1122
lemma size_single [simp]: "size {#b#} = 1"
blanchet@56656
  1123
by (simp add: size_multiset_overloaded_def)
blanchet@56656
  1124
blanchet@56656
  1125
lemma setsum_wcount_Int:
nipkow@60495
  1126
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A"
haftmann@62430
  1127
  by (induct rule: finite_induct)
haftmann@62430
  1128
    (simp_all add: Int_insert_left wcount_def count_eq_zero_iff)
blanchet@56656
  1129
blanchet@56656
  1130
lemma size_multiset_union [simp]:
blanchet@56656
  1131
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
haftmann@57418
  1132
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union)
blanchet@56656
  1133
apply (subst Int_commute)
blanchet@56656
  1134
apply (simp add: setsum_wcount_Int)
nipkow@26178
  1135
done
wenzelm@10249
  1136
haftmann@28708
  1137
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
blanchet@56656
  1138
by (auto simp add: size_multiset_overloaded_def)
blanchet@56656
  1139
haftmann@62430
  1140
lemma size_multiset_eq_0_iff_empty [iff]:
haftmann@62430
  1141
  "size_multiset f M = 0 \<longleftrightarrow> M = {#}"
haftmann@62430
  1142
  by (auto simp add: size_multiset_eq count_eq_zero_iff)
wenzelm@10249
  1143
wenzelm@17161
  1144
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
blanchet@56656
  1145
by (auto simp add: size_multiset_overloaded_def)
nipkow@26016
  1146
nipkow@26016
  1147
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
nipkow@26178
  1148
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
wenzelm@10249
  1149
wenzelm@60607
  1150
lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M"
blanchet@56656
  1151
apply (unfold size_multiset_overloaded_eq)
nipkow@26178
  1152
apply (drule setsum_SucD)
nipkow@26178
  1153
apply auto
nipkow@26178
  1154
done
wenzelm@10249
  1155
haftmann@34943
  1156
lemma size_eq_Suc_imp_eq_union:
haftmann@34943
  1157
  assumes "size M = Suc n"
haftmann@34943
  1158
  shows "\<exists>a N. M = N + {#a#}"
haftmann@34943
  1159
proof -
haftmann@34943
  1160
  from assms obtain a where "a \<in># M"
haftmann@34943
  1161
    by (erule size_eq_Suc_imp_elem [THEN exE])
haftmann@34943
  1162
  then have "M = M - {#a#} + {#a#}" by simp
haftmann@34943
  1163
  then show ?thesis by blast
nipkow@23611
  1164
qed
kleing@15869
  1165
wenzelm@60606
  1166
lemma size_mset_mono:
wenzelm@60606
  1167
  fixes A B :: "'a multiset"
haftmann@62430
  1168
  assumes "A \<subseteq># B"
wenzelm@60606
  1169
  shows "size A \<le> size B"
nipkow@59949
  1170
proof -
Mathias@63310
  1171
  from assms[unfolded mset_subset_eq_exists_conv]
nipkow@59949
  1172
  obtain C where B: "B = A + C" by auto
wenzelm@60606
  1173
  show ?thesis unfolding B by (induct C) auto
nipkow@59949
  1174
qed
nipkow@59949
  1175
nipkow@59998
  1176
lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M"
nipkow@59949
  1177
by (rule size_mset_mono[OF multiset_filter_subset])
nipkow@59949
  1178
nipkow@59949
  1179
lemma size_Diff_submset:
haftmann@62430
  1180
  "M \<subseteq># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)"
Mathias@63310
  1181
by (metis add_diff_cancel_left' size_union mset_subset_eq_exists_conv)
nipkow@26016
  1182
haftmann@62430
  1183
wenzelm@60500
  1184
subsection \<open>Induction and case splits\<close>
wenzelm@10249
  1185
wenzelm@18258
  1186
theorem multiset_induct [case_names empty add, induct type: multiset]:
huffman@48009
  1187
  assumes empty: "P {#}"
huffman@48009
  1188
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
huffman@48009
  1189
  shows "P M"
huffman@48009
  1190
proof (induct n \<equiv> "size M" arbitrary: M)
huffman@48009
  1191
  case 0 thus "P M" by (simp add: empty)
huffman@48009
  1192
next
huffman@48009
  1193
  case (Suc k)
huffman@48009
  1194
  obtain N x where "M = N + {#x#}"
wenzelm@60500
  1195
    using \<open>Suc k = size M\<close> [symmetric]
huffman@48009
  1196
    using size_eq_Suc_imp_eq_union by fast
huffman@48009
  1197
  with Suc add show "P M" by simp
wenzelm@10249
  1198
qed
wenzelm@10249
  1199
kleing@25610
  1200
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
nipkow@26178
  1201
by (induct M) auto
kleing@25610
  1202
wenzelm@55913
  1203
lemma multiset_cases [cases type]:
wenzelm@55913
  1204
  obtains (empty) "M = {#}"
wenzelm@55913
  1205
    | (add) N x where "M = N + {#x#}"
wenzelm@63092
  1206
  by (induct M) simp_all
kleing@25610
  1207
haftmann@34943
  1208
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
haftmann@34943
  1209
by (cases "B = {#}") (auto dest: multi_member_split)
haftmann@34943
  1210
wenzelm@60607
  1211
lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}"
nipkow@39302
  1212
apply (subst multiset_eq_iff)
nipkow@26178
  1213
apply auto
nipkow@26178
  1214
done
wenzelm@10249
  1215
Mathias@63310
  1216
lemma mset_subset_size: "(A::'a multiset) \<subset># B \<Longrightarrow> size A < size B"
haftmann@34943
  1217
proof (induct A arbitrary: B)
haftmann@34943
  1218
  case (empty M)
Mathias@63310
  1219
  then have "M \<noteq> {#}" by (simp add: mset_subset_empty_nonempty)
blanchet@58425
  1220
  then obtain M' x where "M = M' + {#x#}"
haftmann@34943
  1221
    by (blast dest: multi_nonempty_split)
haftmann@34943
  1222
  then show ?case by simp
haftmann@34943
  1223
next
haftmann@34943
  1224
  case (add S x T)
haftmann@62430
  1225
  have IH: "\<And>B. S \<subset># B \<Longrightarrow> size S < size B" by fact
haftmann@62430
  1226
  have SxsubT: "S + {#x#} \<subset># T" by fact
haftmann@62430
  1227
  then have "x \<in># T" and "S \<subset># T"
Mathias@63310
  1228
    by (auto dest: mset_subset_insertD)
blanchet@58425
  1229
  then obtain T' where T: "T = T' + {#x#}"
haftmann@34943
  1230
    by (blast dest: multi_member_split)
haftmann@62430
  1231
  then have "S \<subset># T'" using SxsubT
Mathias@63310
  1232
    by (blast intro: mset_subset_add_bothsides)
haftmann@34943
  1233
  then have "size S < size T'" using IH by simp
haftmann@34943
  1234
  then show ?case using T by simp
haftmann@34943
  1235
qed
haftmann@34943
  1236
nipkow@59949
  1237
lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}"
nipkow@59949
  1238
by (cases M) auto
nipkow@59949
  1239
haftmann@62430
  1240
wenzelm@60500
  1241
subsubsection \<open>Strong induction and subset induction for multisets\<close>
wenzelm@60500
  1242
wenzelm@60500
  1243
text \<open>Well-foundedness of strict subset relation\<close>
haftmann@58098
  1244
Mathias@63310
  1245
lemma wf_subset_mset_rel: "wf {(M, N :: 'a multiset). M \<subset># N}"
haftmann@34943
  1246
apply (rule wf_measure [THEN wf_subset, where f1=size])
Mathias@63310
  1247
apply (clarsimp simp: measure_def inv_image_def mset_subset_size)
haftmann@34943
  1248
done
haftmann@34943
  1249
haftmann@34943
  1250
lemma full_multiset_induct [case_names less]:
haftmann@62430
  1251
assumes ih: "\<And>B. \<forall>(A::'a multiset). A \<subset># B \<longrightarrow> P A \<Longrightarrow> P B"
haftmann@34943
  1252
shows "P B"
Mathias@63310
  1253
apply (rule wf_subset_mset_rel [THEN wf_induct])
haftmann@58098
  1254
apply (rule ih, auto)
haftmann@34943
  1255
done
haftmann@34943
  1256
haftmann@34943
  1257
lemma multi_subset_induct [consumes 2, case_names empty add]:
haftmann@62430
  1258
  assumes "F \<subseteq># A"
wenzelm@60606
  1259
    and empty: "P {#}"
wenzelm@60606
  1260
    and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
wenzelm@60606
  1261
  shows "P F"
haftmann@34943
  1262
proof -
haftmann@62430
  1263
  from \<open>F \<subseteq># A\<close>
haftmann@34943
  1264
  show ?thesis
haftmann@34943
  1265
  proof (induct F)
haftmann@34943
  1266
    show "P {#}" by fact
haftmann@34943
  1267
  next
haftmann@34943
  1268
    fix x F
haftmann@62430
  1269
    assume P: "F \<subseteq># A \<Longrightarrow> P F" and i: "F + {#x#} \<subseteq># A"
haftmann@34943
  1270
    show "P (F + {#x#})"
haftmann@34943
  1271
    proof (rule insert)
Mathias@63310
  1272
      from i show "x \<in># A" by (auto dest: mset_subset_eq_insertD)
Mathias@63310
  1273
      from i have "F \<subseteq># A" by (auto dest: mset_subset_eq_insertD)
haftmann@34943
  1274
      with P show "P F" .
haftmann@34943
  1275
    qed
haftmann@34943
  1276
  qed
haftmann@34943
  1277
qed
wenzelm@26145
  1278
wenzelm@17161
  1279
wenzelm@60500
  1280
subsection \<open>The fold combinator\<close>
huffman@48023
  1281
nipkow@59998
  1282
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
huffman@48023
  1283
where
nipkow@60495
  1284
  "fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)"
huffman@48023
  1285
wenzelm@60606
  1286
lemma fold_mset_empty [simp]: "fold_mset f s {#} = s"
nipkow@59998
  1287
  by (simp add: fold_mset_def)
huffman@48023
  1288
huffman@48023
  1289
context comp_fun_commute
huffman@48023
  1290
begin
huffman@48023
  1291
wenzelm@60606
  1292
lemma fold_mset_insert: "fold_mset f s (M + {#x#}) = f x (fold_mset f s M)"
haftmann@49822
  1293
proof -
haftmann@49822
  1294
  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
haftmann@49822
  1295
    by (fact comp_fun_commute_funpow)
haftmann@49822
  1296
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
haftmann@49822
  1297
    by (fact comp_fun_commute_funpow)
haftmann@49822
  1298
  show ?thesis
nipkow@60495
  1299
  proof (cases "x \<in> set_mset M")
haftmann@49822
  1300
    case False
haftmann@62430
  1301
    then have *: "count (M + {#x#}) x = 1"
haftmann@62430
  1302
      by (simp add: not_in_iff)
nipkow@60495
  1303
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_mset M) =
nipkow@60495
  1304
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)"
haftmann@49822
  1305
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
  1306
    with False * show ?thesis
nipkow@59998
  1307
      by (simp add: fold_mset_def del: count_union)
huffman@48023
  1308
  next
haftmann@49822
  1309
    case True
wenzelm@63040
  1310
    define N where "N = set_mset M - {x}"
nipkow@60495
  1311
    from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto
haftmann@49822
  1312
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
haftmann@49822
  1313
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
haftmann@49822
  1314
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
nipkow@59998
  1315
    with * show ?thesis by (simp add: fold_mset_def del: count_union) simp
huffman@48023
  1316
  qed
huffman@48023
  1317
qed
huffman@48023
  1318
wenzelm@60606
  1319
corollary fold_mset_single [simp]: "fold_mset f s {#x#} = f x s"
haftmann@49822
  1320
proof -
nipkow@59998
  1321
  have "fold_mset f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
haftmann@49822
  1322
  then show ?thesis by simp
haftmann@49822
  1323
qed
huffman@48023
  1324
wenzelm@60606
  1325
lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M"
haftmann@49822
  1326
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
huffman@48023
  1327
wenzelm@60606
  1328
lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N"
haftmann@49822
  1329
proof (induct M)
huffman@48023
  1330
  case empty then show ?case by simp
huffman@48023
  1331
next
haftmann@49822
  1332
  case (add M x)
haftmann@49822
  1333
  have "M + {#x#} + N = (M + N) + {#x#}"
haftmann@57514
  1334
    by (simp add: ac_simps)
haftmann@51548
  1335
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
huffman@48023
  1336
qed
huffman@48023
  1337
huffman@48023
  1338
lemma fold_mset_fusion:
huffman@48023
  1339
  assumes "comp_fun_commute g"
wenzelm@60606
  1340
    and *: "\<And>x y. h (g x y) = f x (h y)"
wenzelm@60606
  1341
  shows "h (fold_mset g w A) = fold_mset f (h w) A"
huffman@48023
  1342
proof -
huffman@48023
  1343
  interpret comp_fun_commute g by (fact assms)
wenzelm@60606
  1344
  from * show ?thesis by (induct A) auto
huffman@48023
  1345
qed
huffman@48023
  1346
huffman@48023
  1347
end
huffman@48023
  1348
wenzelm@60500
  1349
text \<open>
huffman@48023
  1350
  A note on code generation: When defining some function containing a
nipkow@59998
  1351
  subterm @{term "fold_mset F"}, code generation is not automatic. When
wenzelm@61585
  1352
  interpreting locale \<open>left_commutative\<close> with \<open>F\<close>, the
nipkow@59998
  1353
  would be code thms for @{const fold_mset} become thms like
wenzelm@61585
  1354
  @{term "fold_mset F z {#} = z"} where \<open>F\<close> is not a pattern but
huffman@48023
  1355
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
wenzelm@61585
  1356
  constant with its own code thms needs to be introduced for \<open>F\<close>. See the image operator below.
wenzelm@60500
  1357
\<close>
wenzelm@60500
  1358
wenzelm@60500
  1359
wenzelm@60500
  1360
subsection \<open>Image\<close>
huffman@48023
  1361
huffman@48023
  1362
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
wenzelm@60607
  1363
  "image_mset f = fold_mset (plus \<circ> single \<circ> f) {#}"
wenzelm@60607
  1364
wenzelm@60607
  1365
lemma comp_fun_commute_mset_image: "comp_fun_commute (plus \<circ> single \<circ> f)"
haftmann@49823
  1366
proof
haftmann@57514
  1367
qed (simp add: ac_simps fun_eq_iff)
huffman@48023
  1368
huffman@48023
  1369
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
haftmann@49823
  1370
  by (simp add: image_mset_def)
huffman@48023
  1371
huffman@48023
  1372
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
haftmann@49823
  1373
proof -
wenzelm@60607
  1374
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
haftmann@49823
  1375
    by (fact comp_fun_commute_mset_image)
haftmann@49823
  1376
  show ?thesis by (simp add: image_mset_def)
haftmann@49823
  1377
qed
huffman@48023
  1378
wenzelm@60606
  1379
lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N"
haftmann@49823
  1380
proof -
wenzelm@60607
  1381
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
haftmann@49823
  1382
    by (fact comp_fun_commute_mset_image)
haftmann@57514
  1383
  show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps)
haftmann@49823
  1384
qed
haftmann@49823
  1385
wenzelm@60606
  1386
corollary image_mset_insert: "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
haftmann@49823
  1387
  by simp
huffman@48023
  1388
wenzelm@60606
  1389
lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)"
haftmann@49823
  1390
  by (induct M) simp_all
huffman@48040
  1391
wenzelm@60606
  1392
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
haftmann@49823
  1393
  by (induct M) simp_all
huffman@48023
  1394
wenzelm@60606
  1395
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
haftmann@49823
  1396
  by (cases M) auto
huffman@48023
  1397
eberlm@63099
  1398
lemma image_mset_If:
eberlm@63099
  1399
  "image_mset (\<lambda>x. if P x then f x else g x) A = 
eberlm@63099
  1400
     image_mset f (filter_mset P A) + image_mset g (filter_mset (\<lambda>x. \<not>P x) A)"
eberlm@63099
  1401
  by (induction A) (auto simp: add_ac)
eberlm@63099
  1402
eberlm@63099
  1403
lemma image_mset_Diff: 
eberlm@63099
  1404
  assumes "B \<subseteq># A"
eberlm@63099
  1405
  shows   "image_mset f (A - B) = image_mset f A - image_mset f B"
eberlm@63099
  1406
proof -
eberlm@63099
  1407
  have "image_mset f (A - B + B) = image_mset f (A - B) + image_mset f B"
eberlm@63099
  1408
    by simp
eberlm@63099
  1409
  also from assms have "A - B + B = A"
eberlm@63099
  1410
    by (simp add: subset_mset.diff_add) 
eberlm@63099
  1411
  finally show ?thesis by simp
eberlm@63099
  1412
qed
eberlm@63099
  1413
eberlm@63099
  1414
lemma count_image_mset: 
eberlm@63099
  1415
  "count (image_mset f A) x = (\<Sum>y\<in>f -` {x} \<inter> set_mset A. count A y)"
eberlm@63099
  1416
  by (induction A)
eberlm@63099
  1417
     (auto simp: setsum.distrib setsum.delta' intro!: setsum.mono_neutral_left)
eberlm@63099
  1418
eberlm@63099
  1419
wenzelm@61955
  1420
syntax (ASCII)
wenzelm@61955
  1421
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ :# _#})")
huffman@48023
  1422
syntax
wenzelm@61955
  1423
  "_comprehension_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"  ("({#_/. _ \<in># _#})")
blanchet@59813
  1424
translations
wenzelm@61955
  1425
  "{#e. x \<in># M#}" \<rightleftharpoons> "CONST image_mset (\<lambda>x. e) M"
wenzelm@61955
  1426
wenzelm@61955
  1427
syntax (ASCII)
wenzelm@61955
  1428
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ :# _./ _#})")
huffman@48023
  1429
syntax
wenzelm@61955
  1430
  "_comprehension_mset'" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"  ("({#_/ | _ \<in># _./ _#})")
blanchet@59813
  1431
translations
wenzelm@60606
  1432
  "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
blanchet@59813
  1433
wenzelm@60500
  1434
text \<open>
wenzelm@60607
  1435
  This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"}
wenzelm@60607
  1436
  but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source]
wenzelm@60607
  1437
  "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
wenzelm@60607
  1438
  @{term "{#x+x|x\<in>#M. x<c#}"}.
wenzelm@60500
  1439
\<close>
huffman@48023
  1440
nipkow@60495
  1441
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
haftmann@62430
  1442
by (metis set_image_mset)
blanchet@59813
  1443
blanchet@55467
  1444
functor image_mset: image_mset
huffman@48023
  1445
proof -
huffman@48023
  1446
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
huffman@48023
  1447
  proof
huffman@48023
  1448
    fix A
huffman@48023
  1449
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
huffman@48023
  1450
      by (induct A) simp_all
huffman@48023
  1451
  qed
huffman@48023
  1452
  show "image_mset id = id"
huffman@48023
  1453
  proof
huffman@48023
  1454
    fix A
huffman@48023
  1455
    show "image_mset id A = id A"
huffman@48023
  1456
      by (induct A) simp_all
huffman@48023
  1457
  qed
huffman@48023
  1458
qed
huffman@48023
  1459
blanchet@59813
  1460
declare
blanchet@59813
  1461
  image_mset.id [simp]
blanchet@59813
  1462
  image_mset.identity [simp]
blanchet@59813
  1463
blanchet@59813
  1464
lemma image_mset_id[simp]: "image_mset id x = x"
blanchet@59813
  1465
  unfolding id_def by auto
blanchet@59813
  1466
blanchet@59813
  1467
lemma image_mset_cong: "(\<And>x. x \<in># M \<Longrightarrow> f x = g x) \<Longrightarrow> {#f x. x \<in># M#} = {#g x. x \<in># M#}"
blanchet@59813
  1468
  by (induct M) auto
blanchet@59813
  1469
blanchet@59813
  1470
lemma image_mset_cong_pair:
blanchet@59813
  1471
  "(\<forall>x y. (x, y) \<in># M \<longrightarrow> f x y = g x y) \<Longrightarrow> {#f x y. (x, y) \<in># M#} = {#g x y. (x, y) \<in># M#}"
blanchet@59813
  1472
  by (metis image_mset_cong split_cong)
haftmann@49717
  1473
huffman@48023
  1474
wenzelm@60500
  1475
subsection \<open>Further conversions\<close>
haftmann@34943
  1476
nipkow@60515
  1477
primrec mset :: "'a list \<Rightarrow> 'a multiset" where
nipkow@60515
  1478
  "mset [] = {#}" |
nipkow@60515
  1479
  "mset (a # x) = mset x + {# a #}"
haftmann@34943
  1480
haftmann@37107
  1481
lemma in_multiset_in_set:
nipkow@60515
  1482
  "x \<in># mset xs \<longleftrightarrow> x \<in> set xs"
haftmann@37107
  1483
  by (induct xs) simp_all
haftmann@37107
  1484
nipkow@60515
  1485
lemma count_mset:
nipkow@60515
  1486
  "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@37107
  1487
  by (induct xs) simp_all
haftmann@37107
  1488
nipkow@60515
  1489
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
blanchet@59813
  1490
  by (induct x) auto
haftmann@34943
  1491
nipkow@60515
  1492
lemma mset_zero_iff_right[simp]: "({#} = mset x) = (x = [])"
haftmann@34943
  1493
by (induct x) auto
haftmann@34943
  1494
nipkow@60515
  1495
lemma set_mset_mset[simp]: "set_mset (mset x) = set x"
haftmann@34943
  1496
by (induct x) auto
haftmann@34943
  1497
haftmann@62430
  1498
lemma set_mset_comp_mset [simp]: "set_mset \<circ> mset = set"
haftmann@62430
  1499
  by (simp add: fun_eq_iff)
haftmann@34943
  1500
nipkow@60515
  1501
lemma size_mset [simp]: "size (mset xs) = length xs"
huffman@48012
  1502
  by (induct xs) simp_all
huffman@48012
  1503
wenzelm@60606
  1504
lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys"
haftmann@57514
  1505
  by (induct xs arbitrary: ys) (auto simp: ac_simps)
haftmann@34943
  1506
wenzelm@60607
  1507
lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
haftmann@40303
  1508
  by (induct xs) simp_all
haftmann@40303
  1509
nipkow@60515
  1510
lemma mset_rev [simp]:
nipkow@60515
  1511
  "mset (rev xs) = mset xs"
haftmann@40950
  1512
  by (induct xs) simp_all
haftmann@40950
  1513
nipkow@60515
  1514
lemma surj_mset: "surj mset"
haftmann@34943
  1515
apply (unfold surj_def)
haftmann@34943
  1516
apply (rule allI)
haftmann@34943
  1517
apply (rule_tac M = y in multiset_induct)
haftmann@34943
  1518
 apply auto
haftmann@34943
  1519
apply (rule_tac x = "x # xa" in exI)
haftmann@34943
  1520
apply auto
haftmann@34943
  1521
done
haftmann@34943
  1522
haftmann@34943
  1523
lemma distinct_count_atmost_1:
wenzelm@60606
  1524
  "distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))"
haftmann@62430
  1525
proof (induct x)
haftmann@62430
  1526
  case Nil then show ?case by simp
haftmann@62430
  1527
next
haftmann@62430
  1528
  case (Cons x xs) show ?case (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@62430
  1529
  proof
haftmann@62430
  1530
    assume ?lhs then show ?rhs using Cons by simp
haftmann@62430
  1531
  next
haftmann@62430
  1532
    assume ?rhs then have "x \<notin> set xs"
haftmann@62430
  1533
      by (simp split: if_splits)
haftmann@62430
  1534
    moreover from \<open>?rhs\<close> have "(\<forall>a. count (mset xs) a =
haftmann@62430
  1535
       (if a \<in> set xs then 1 else 0))"
haftmann@62430
  1536
      by (auto split: if_splits simp add: count_eq_zero_iff)
haftmann@62430
  1537
    ultimately show ?lhs using Cons by simp
haftmann@62430
  1538
  qed
haftmann@62430
  1539
qed
haftmann@62430
  1540
haftmann@62430
  1541
lemma mset_eq_setD:
haftmann@62430
  1542
  assumes "mset xs = mset ys"
haftmann@62430
  1543
  shows "set xs = set ys"
haftmann@62430
  1544
proof -
haftmann@62430
  1545
  from assms have "set_mset (mset xs) = set_mset (mset ys)"
haftmann@62430
  1546
    by simp
haftmann@62430
  1547
  then show ?thesis by simp
haftmann@62430
  1548
qed
haftmann@34943
  1549
nipkow@60515
  1550
lemma set_eq_iff_mset_eq_distinct:
haftmann@34943
  1551
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
nipkow@60515
  1552
    (set x = set y) = (mset x = mset y)"
nipkow@39302
  1553
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
haftmann@34943
  1554
nipkow@60515
  1555
lemma set_eq_iff_mset_remdups_eq:
nipkow@60515
  1556
   "(set x = set y) = (mset (remdups x) = mset (remdups y))"
haftmann@34943
  1557
apply (rule iffI)
nipkow@60515
  1558
apply (simp add: set_eq_iff_mset_eq_distinct[THEN iffD1])
haftmann@34943
  1559
apply (drule distinct_remdups [THEN distinct_remdups
nipkow@60515
  1560
      [THEN set_eq_iff_mset_eq_distinct [THEN iffD2]]])
haftmann@34943
  1561
apply simp
haftmann@34943
  1562
done
haftmann@34943
  1563
wenzelm@60606
  1564
lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs"
haftmann@57514
  1565
  by (induct xs) (auto simp: ac_simps)
haftmann@34943
  1566
wenzelm@60607
  1567
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls"
wenzelm@60678
  1568
proof (induct ls arbitrary: i)
wenzelm@60678
  1569
  case Nil
wenzelm@60678
  1570
  then show ?case by simp
wenzelm@60678
  1571
next
wenzelm@60678
  1572
  case Cons
wenzelm@60678
  1573
  then show ?case by (cases i) auto
wenzelm@60678
  1574
qed
haftmann@34943
  1575
wenzelm@60606
  1576
lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
wenzelm@60678
  1577
  by (induct xs) (auto simp add: multiset_eq_iff)
haftmann@34943
  1578
nipkow@60515
  1579
lemma mset_eq_length:
nipkow@60515
  1580
  assumes "mset xs = mset ys"
haftmann@37107
  1581
  shows "length xs = length ys"
nipkow@60515
  1582
  using assms by (metis size_mset)
nipkow@60515
  1583
nipkow@60515
  1584
lemma mset_eq_length_filter:
nipkow@60515
  1585
  assumes "mset xs = mset ys"
haftmann@39533
  1586
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
nipkow@60515
  1587
  using assms by (metis count_mset)
haftmann@39533
  1588
haftmann@45989
  1589
lemma fold_multiset_equiv:
haftmann@45989
  1590
  assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
nipkow@60515
  1591
    and equiv: "mset xs = mset ys"
haftmann@49822
  1592
  shows "List.fold f xs = List.fold f ys"
wenzelm@60606
  1593
  using f equiv [symmetric]
wenzelm@46921
  1594
proof (induct xs arbitrary: ys)
wenzelm@60678
  1595
  case Nil
wenzelm@60678
  1596
  then show ?case by simp
haftmann@45989
  1597
next
haftmann@45989
  1598
  case (Cons x xs)
wenzelm@60678
  1599
  then have *: "set ys = set (x # xs)"
wenzelm@60678
  1600
    by (blast dest: mset_eq_setD)
blanchet@58425
  1601
  have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
haftmann@45989
  1602
    by (rule Cons.prems(1)) (simp_all add: *)
wenzelm@60678
  1603
  moreover from * have "x \<in> set ys"
wenzelm@60678
  1604
    by simp
wenzelm@60678
  1605
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x"
wenzelm@60678
  1606
    by (fact fold_remove1_split)
wenzelm@60678
  1607
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)"
wenzelm@60678
  1608
    by (auto intro: Cons.hyps)
haftmann@45989
  1609
  ultimately show ?case by simp
haftmann@45989
  1610
qed
haftmann@45989
  1611
wenzelm@60606
  1612
lemma mset_insort [simp]: "mset (insort x xs) = mset xs + {#x#}"
haftmann@51548
  1613
  by (induct xs) (simp_all add: ac_simps)
haftmann@51548
  1614
Mathias@63524
  1615
lemma mset_map[simp]: "mset (map f xs) = image_mset f (mset xs)"
haftmann@51600
  1616
  by (induct xs) simp_all
haftmann@51600
  1617
haftmann@61890
  1618
global_interpretation mset_set: folding "\<lambda>x M. {#x#} + M" "{#}"
haftmann@61832
  1619
  defines mset_set = "folding.F (\<lambda>x M. {#x#} + M) {#}"
haftmann@61832
  1620
  by standard (simp add: fun_eq_iff ac_simps)
haftmann@51548
  1621
nipkow@60513
  1622
lemma count_mset_set [simp]:
nipkow@60513
  1623
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P")
nipkow@60513
  1624
  "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
nipkow@60513
  1625
  "x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R")
haftmann@51600
  1626
proof -
wenzelm@60606
  1627
  have *: "count (mset_set A) x = 0" if "x \<notin> A" for A
wenzelm@60606
  1628
  proof (cases "finite A")
wenzelm@60606
  1629
    case False then show ?thesis by simp
wenzelm@60606
  1630
  next
wenzelm@60606
  1631
    case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
wenzelm@60606
  1632
  qed
haftmann@51600
  1633
  then show "PROP ?P" "PROP ?Q" "PROP ?R"
haftmann@51600
  1634
  by (auto elim!: Set.set_insert)
wenzelm@61585
  1635
qed \<comment> \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
nipkow@60513
  1636
nipkow@60513
  1637
lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A"
blanchet@59813
  1638
  by (induct A rule: finite_induct) simp_all
blanchet@59813
  1639
eberlm@63099
  1640
lemma mset_set_Union: 
eberlm@63099
  1641
  "finite A \<Longrightarrow> finite B \<Longrightarrow> A \<inter> B = {} \<Longrightarrow> mset_set (A \<union> B) = mset_set A + mset_set B"
eberlm@63099
  1642
  by (induction A rule: finite_induct) (auto simp: add_ac)
eberlm@63099
  1643
eberlm@63099
  1644
lemma filter_mset_mset_set [simp]:
eberlm@63099
  1645
  "finite A \<Longrightarrow> filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
eberlm@63099
  1646
proof (induction A rule: finite_induct)
eberlm@63099
  1647
  case (insert x A)
eberlm@63099
  1648
  from insert.hyps have "filter_mset P (mset_set (insert x A)) = 
eberlm@63099
  1649
      filter_mset P (mset_set A) + mset_set (if P x then {x} else {})"
eberlm@63099
  1650
    by (simp add: add_ac)
eberlm@63099
  1651
  also have "filter_mset P (mset_set A) = mset_set {x\<in>A. P x}"
eberlm@63099
  1652
    by (rule insert.IH)
eberlm@63099
  1653
  also from insert.hyps 
eberlm@63099
  1654
    have "\<dots> + mset_set (if P x then {x} else {}) =
eberlm@63099
  1655
            mset_set ({x \<in> A. P x} \<union> (if P x then {x} else {}))" (is "_ = mset_set ?A")
eberlm@63099
  1656
     by (intro mset_set_Union [symmetric]) simp_all
eberlm@63099
  1657
  also from insert.hyps have "?A = {y\<in>insert x A. P y}" by auto
eberlm@63099
  1658
  finally show ?case .
eberlm@63099
  1659
qed simp_all
eberlm@63099
  1660
eberlm@63099
  1661
lemma mset_set_Diff:
eberlm@63099
  1662
  assumes "finite A" "B \<subseteq> A"
eberlm@63099
  1663
  shows  "mset_set (A - B) = mset_set A - mset_set B"
eberlm@63099
  1664
proof -
eberlm@63099
  1665
  from assms have "mset_set ((A - B) \<union> B) = mset_set (A - B) + mset_set B"
eberlm@63099
  1666
    by (intro mset_set_Union) (auto dest: finite_subset)
eberlm@63099
  1667
  also from assms have "A - B \<union> B = A" by blast
eberlm@63099
  1668
  finally show ?thesis by simp
eberlm@63099
  1669
qed
eberlm@63099
  1670
eberlm@63099
  1671
lemma mset_set_set: "distinct xs \<Longrightarrow> mset_set (set xs) = mset xs"
eberlm@63099
  1672
  by (induction xs) (simp_all add: add_ac)
eberlm@63099
  1673
haftmann@51548
  1674
context linorder
haftmann@51548
  1675
begin
haftmann@51548
  1676
haftmann@51548
  1677
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
haftmann@51548
  1678
where
nipkow@59998
  1679
  "sorted_list_of_multiset M = fold_mset insort [] M"
haftmann@51548
  1680
haftmann@51548
  1681
lemma sorted_list_of_multiset_empty [simp]:
haftmann@51548
  1682
  "sorted_list_of_multiset {#} = []"
haftmann@51548
  1683
  by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1684
haftmann@51548
  1685
lemma sorted_list_of_multiset_singleton [simp]:
haftmann@51548
  1686
  "sorted_list_of_multiset {#x#} = [x]"
haftmann@51548
  1687
proof -
haftmann@51548
  1688
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1689
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1690
qed
haftmann@51548
  1691
haftmann@51548
  1692
lemma sorted_list_of_multiset_insert [simp]:
haftmann@51548
  1693
  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
haftmann@51548
  1694
proof -
haftmann@51548
  1695
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1696
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1697
qed
haftmann@51548
  1698
haftmann@51548
  1699
end
haftmann@51548
  1700
nipkow@60515
  1701
lemma mset_sorted_list_of_multiset [simp]:
nipkow@60515
  1702
  "mset (sorted_list_of_multiset M) = M"
nipkow@60513
  1703
by (induct M) simp_all
haftmann@51548
  1704
nipkow@60515
  1705
lemma sorted_list_of_multiset_mset [simp]:
nipkow@60515
  1706
  "sorted_list_of_multiset (mset xs) = sort xs"
nipkow@60513
  1707
by (induct xs) simp_all
nipkow@60513
  1708
nipkow@60513
  1709
lemma finite_set_mset_mset_set[simp]:
nipkow@60513
  1710
  "finite A \<Longrightarrow> set_mset (mset_set A) = A"
nipkow@60513
  1711
by (induct A rule: finite_induct) simp_all
nipkow@60513
  1712
eberlm@63099
  1713
lemma mset_set_empty_iff: "mset_set A = {#} \<longleftrightarrow> A = {} \<or> infinite A"
eberlm@63099
  1714
  using finite_set_mset_mset_set by fastforce
eberlm@63099
  1715
nipkow@60513
  1716
lemma infinite_set_mset_mset_set:
nipkow@60513
  1717
  "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
nipkow@60513
  1718
by simp
haftmann@51548
  1719
haftmann@51548
  1720
lemma set_sorted_list_of_multiset [simp]:
nipkow@60495
  1721
  "set (sorted_list_of_multiset M) = set_mset M"
nipkow@60513
  1722
by (induct M) (simp_all add: set_insort)
nipkow@60513
  1723
nipkow@60513
  1724
lemma sorted_list_of_mset_set [simp]:
nipkow@60513
  1725
  "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
nipkow@60513
  1726
by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
haftmann@51548
  1727
eberlm@63099
  1728
lemma mset_upt [simp]: "mset [m..<n] = mset_set {m..<n}"
eberlm@63099
  1729
  by (induction n) (simp_all add: atLeastLessThanSuc add_ac)
eberlm@63099
  1730
eberlm@63099
  1731
lemma image_mset_map_of: 
eberlm@63099
  1732
  "distinct (map fst xs) \<Longrightarrow> {#the (map_of xs i). i \<in># mset (map fst xs)#} = mset (map snd xs)"
eberlm@63099
  1733
proof (induction xs)
eberlm@63099
  1734
  case (Cons x xs)
eberlm@63099
  1735
  have "{#the (map_of (x # xs) i). i \<in># mset (map fst (x # xs))#} = 
eberlm@63099
  1736
          {#the (if i = fst x then Some (snd x) else map_of xs i). 
eberlm@63099
  1737
             i \<in># mset (map fst xs)#} + {#snd x#}" (is "_ = ?A + _") by simp
eberlm@63099
  1738
  also from Cons.prems have "?A = {#the (map_of xs i). i :# mset (map fst xs)#}"
eberlm@63099
  1739
    by (cases x, intro image_mset_cong) (auto simp: in_multiset_in_set)
eberlm@63099
  1740
  also from Cons.prems have "\<dots> = mset (map snd xs)" by (intro Cons.IH) simp_all
eberlm@63099
  1741
  finally show ?case by simp
eberlm@63099
  1742
qed simp_all  
eberlm@63099
  1743
haftmann@51548
  1744
haftmann@60804
  1745
subsection \<open>Replicate operation\<close>
haftmann@60804
  1746
haftmann@60804
  1747
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where
haftmann@60804
  1748
  "replicate_mset n x = ((op + {#x#}) ^^ n) {#}"
haftmann@60804
  1749
haftmann@60804
  1750
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
haftmann@60804
  1751
  unfolding replicate_mset_def by simp
haftmann@60804
  1752
haftmann@60804
  1753
lemma replicate_mset_Suc[simp]: "replicate_mset (Suc n) x = replicate_mset n x + {#x#}"
haftmann@60804
  1754
  unfolding replicate_mset_def by (induct n) (auto intro: add.commute)
haftmann@60804
  1755
haftmann@60804
  1756
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y"
haftmann@62430
  1757
  unfolding replicate_mset_def by (induct n) auto
haftmann@60804
  1758
haftmann@60804
  1759
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)"
haftmann@60804
  1760
  unfolding replicate_mset_def by (induct n) simp_all
haftmann@60804
  1761
haftmann@60804
  1762
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
haftmann@60804
  1763
  by (auto split: if_splits)
haftmann@60804
  1764
haftmann@60804
  1765
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n"
haftmann@60804
  1766
  by (induct n, simp_all)
haftmann@60804
  1767
Mathias@63310
  1768
lemma count_le_replicate_mset_subset_eq: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<subseteq># M"
Mathias@63310
  1769
  by (auto simp add: mset_subset_eqI) (metis count_replicate_mset subseteq_mset_def)
haftmann@60804
  1770
haftmann@60804
  1771
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
haftmann@60804
  1772
  by (induct D) simp_all
haftmann@60804
  1773
haftmann@61031
  1774
lemma replicate_count_mset_eq_filter_eq:
haftmann@61031
  1775
  "replicate (count (mset xs) k) k = filter (HOL.eq k) xs"
haftmann@61031
  1776
  by (induct xs) auto
haftmann@61031
  1777
haftmann@62366
  1778
lemma replicate_mset_eq_empty_iff [simp]:
haftmann@62366
  1779
  "replicate_mset n a = {#} \<longleftrightarrow> n = 0"
haftmann@62366
  1780
  by (induct n) simp_all
haftmann@62366
  1781
haftmann@62366
  1782
lemma replicate_mset_eq_iff:
haftmann@62366
  1783
  "replicate_mset m a = replicate_mset n b \<longleftrightarrow>
haftmann@62366
  1784
    m = 0 \<and> n = 0 \<or> m = n \<and> a = b"
haftmann@62366
  1785
  by (auto simp add: multiset_eq_iff)
haftmann@62366
  1786
haftmann@60804
  1787
wenzelm@60500
  1788
subsection \<open>Big operators\<close>
haftmann@51548
  1789
haftmann@51548
  1790
locale comm_monoid_mset = comm_monoid
haftmann@51548
  1791
begin
haftmann@51548
  1792
haftmann@51548
  1793
definition F :: "'a multiset \<Rightarrow> 'a"
haftmann@63290
  1794
  where eq_fold: "F M = fold_mset f \<^bold>1 M"
haftmann@63290
  1795
haftmann@63290
  1796
lemma empty [simp]: "F {#} = \<^bold>1"
haftmann@51548
  1797
  by (simp add: eq_fold)
haftmann@51548
  1798
wenzelm@60678
  1799
lemma singleton [simp]: "F {#x#} = x"
haftmann@51548
  1800
proof -
haftmann@51548
  1801
  interpret comp_fun_commute
wenzelm@60678
  1802
    by standard (simp add: fun_eq_iff left_commute)
haftmann@51548
  1803
  show ?thesis by (simp add: eq_fold)
haftmann@51548
  1804
qed
haftmann@51548
  1805
haftmann@63290
  1806
lemma union [simp]: "F (M + N) = F M \<^bold>* F N"
haftmann@51548
  1807
proof -
haftmann@51548
  1808
  interpret comp_fun_commute f
wenzelm@60678
  1809
    by standard (simp add: fun_eq_iff left_commute)
wenzelm@60678
  1810
  show ?thesis
wenzelm@60678
  1811
    by (induct N) (simp_all add: left_commute eq_fold)
haftmann@51548
  1812
qed
haftmann@51548
  1813
haftmann@51548
  1814
end
haftmann@51548
  1815
wenzelm@61076
  1816
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + :: 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
wenzelm@60678
  1817
  by standard (simp add: add_ac comp_def)
blanchet@59813
  1818
blanchet@59813
  1819
declare comp_fun_commute.fold_mset_insert[OF comp_fun_commute_plus_mset, simp]
blanchet@59813
  1820
nipkow@59998
  1821
lemma in_mset_fold_plus_iff[iff]: "x \<in># fold_mset (op +) M NN \<longleftrightarrow> x \<in># M \<or> (\<exists>N. N \<in># NN \<and> x \<in># N)"
blanchet@59813
  1822
  by (induct NN) auto
blanchet@59813
  1823
haftmann@54868
  1824
context comm_monoid_add
haftmann@54868
  1825
begin
haftmann@54868
  1826
wenzelm@61605
  1827
sublocale msetsum: comm_monoid_mset plus 0
haftmann@61832
  1828
  defines msetsum = msetsum.F ..
haftmann@51548
  1829
haftmann@60804
  1830
lemma (in semiring_1) msetsum_replicate_mset [simp]:
haftmann@60804
  1831
  "msetsum (replicate_mset n a) = of_nat n * a"
haftmann@60804
  1832
  by (induct n) (simp_all add: algebra_simps)
haftmann@60804
  1833
haftmann@51548
  1834
lemma setsum_unfold_msetsum:
nipkow@60513
  1835
  "setsum f A = msetsum (image_mset f (mset_set A))"
haftmann@51548
  1836
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1837
haftmann@51548
  1838
end
haftmann@51548
  1839
blanchet@59813
  1840
lemma msetsum_diff:
wenzelm@61076
  1841
  fixes M N :: "('a :: ordered_cancel_comm_monoid_diff) multiset"
haftmann@62430
  1842
  shows "N \<subseteq># M \<Longrightarrow> msetsum (M - N) = msetsum M - msetsum N"
Mathias@60397
  1843
  by (metis add_diff_cancel_right' msetsum.union subset_mset.diff_add)
blanchet@59813
  1844
nipkow@59949
  1845
lemma size_eq_msetsum: "size M = msetsum (image_mset (\<lambda>_. 1) M)"
nipkow@59949
  1846
proof (induct M)
nipkow@59949
  1847
  case empty then show ?case by simp
nipkow@59949
  1848
next
nipkow@59949
  1849
  case (add M x) then show ?case
nipkow@60495
  1850
    by (cases "x \<in> set_mset M")
haftmann@62430
  1851
      (simp_all add: size_multiset_overloaded_eq setsum.distrib setsum.delta' insert_absorb not_in_iff)
nipkow@59949
  1852
qed
nipkow@59949
  1853
eberlm@63099
  1854
lemma size_mset_set [simp]: "size (mset_set A) = card A"
eberlm@63099
  1855
  by (simp only: size_eq_msetsum card_eq_setsum setsum_unfold_msetsum)
eberlm@63099
  1856
haftmann@62366
  1857
syntax (ASCII)
haftmann@62366
  1858
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3SUM _:#_. _)" [0, 51, 10] 10)
haftmann@62366
  1859
syntax
haftmann@62366
  1860
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"  ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@62366
  1861
translations
haftmann@62366
  1862
  "\<Sum>i \<in># A. b" \<rightleftharpoons> "CONST msetsum (CONST image_mset (\<lambda>i. b) A)"
nipkow@59949
  1863
wenzelm@61955
  1864
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset"  ("\<Union>#_" [900] 900)
wenzelm@62837
  1865
  where "\<Union># MM \<equiv> msetsum MM" \<comment> \<open>FIXME ambiguous notation --
wenzelm@62837
  1866
    could likewise refer to \<open>\<Squnion>#\<close>\<close>
blanchet@59813
  1867
nipkow@60495
  1868
lemma set_mset_Union_mset[simp]: "set_mset (\<Union># MM) = (\<Union>M \<in> set_mset MM. set_mset M)"
blanchet@59813
  1869
  by (induct MM) auto
blanchet@59813
  1870
blanchet@59813
  1871
lemma in_Union_mset_iff[iff]: "x \<in># \<Union># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)"
blanchet@59813
  1872
  by (induct MM) auto
blanchet@59813
  1873
haftmann@62366
  1874
lemma count_setsum:
haftmann@62366
  1875
  "count (setsum f A) x = setsum (\<lambda>a. count (f a) x) A"
haftmann@62366
  1876
  by (induct A rule: infinite_finite_induct) simp_all
haftmann@62366
  1877
haftmann@62366
  1878
lemma setsum_eq_empty_iff:
haftmann@62366
  1879
  assumes "finite A"
haftmann@62366
  1880
  shows "setsum f A = {#} \<longleftrightarrow> (\<forall>a\<in>A. f a = {#})"
haftmann@62366
  1881
  using assms by induct simp_all
haftmann@51548
  1882
haftmann@54868
  1883
context comm_monoid_mult
haftmann@54868
  1884
begin
haftmann@54868
  1885
wenzelm@61605
  1886
sublocale msetprod: comm_monoid_mset times 1
haftmann@61832
  1887
  defines msetprod = msetprod.F ..
haftmann@51548
  1888
haftmann@51548
  1889
lemma msetprod_empty:
haftmann@51548
  1890
  "msetprod {#} = 1"
haftmann@51548
  1891
  by (fact msetprod.empty)
haftmann@51548
  1892
haftmann@51548
  1893
lemma msetprod_singleton:
haftmann@51548
  1894
  "msetprod {#x#} = x"
haftmann@51548
  1895
  by (fact msetprod.singleton)
haftmann@51548
  1896
haftmann@51548
  1897
lemma msetprod_Un:
blanchet@58425
  1898
  "msetprod (A + B) = msetprod A * msetprod B"
haftmann@51548
  1899
  by (fact msetprod.union)
haftmann@51548
  1900
haftmann@60804
  1901
lemma msetprod_replicate_mset [simp]:
haftmann@60804
  1902
  "msetprod (replicate_mset n a) = a ^ n"
haftmann@60804
  1903
  by (induct n) (simp_all add: ac_simps)
haftmann@60804
  1904
haftmann@51548
  1905
lemma setprod_unfold_msetprod:
nipkow@60513
  1906
  "setprod f A = msetprod (image_mset f (mset_set A))"
haftmann@51548
  1907
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1908
haftmann@51548
  1909
lemma msetprod_multiplicity:
nipkow@60495
  1910
  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
nipkow@59998
  1911
  by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
haftmann@51548
  1912
haftmann@51548
  1913
end
haftmann@51548
  1914
wenzelm@61955
  1915
syntax (ASCII)
wenzelm@61955
  1916
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3PROD _:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1917
syntax
wenzelm@61955
  1918
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"  ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1919
translations
wenzelm@61955
  1920
  "\<Prod>i \<in># A. b" \<rightleftharpoons> "CONST msetprod (CONST image_mset (\<lambda>i. b) A)"
haftmann@51548
  1921
haftmann@51548
  1922
lemma (in comm_semiring_1) dvd_msetprod:
haftmann@51548
  1923
  assumes "x \<in># A"
haftmann@51548
  1924
  shows "x dvd msetprod A"
haftmann@51548
  1925
proof -
haftmann@51548
  1926
  from assms have "A = (A - {#x#}) + {#x#}" by simp
haftmann@51548
  1927
  then obtain B where "A = B + {#x#}" ..
haftmann@51548
  1928
  then show ?thesis by simp
haftmann@51548
  1929
qed
haftmann@51548
  1930
haftmann@62430
  1931
lemma (in semidom) msetprod_zero_iff [iff]:
haftmann@62430
  1932
  "msetprod A = 0 \<longleftrightarrow> 0 \<in># A"
haftmann@62366
  1933
  by (induct A) auto
haftmann@62366
  1934
haftmann@62430
  1935
lemma (in semidom_divide) msetprod_diff:
haftmann@62430
  1936
  assumes "B \<subseteq># A" and "0 \<notin># B"
haftmann@62430
  1937
  shows "msetprod (A - B) = msetprod A div msetprod B"
haftmann@62430
  1938
proof -
haftmann@62430
  1939
  from assms obtain C where "A = B + C"
haftmann@62430
  1940
    by (metis subset_mset.add_diff_inverse)
haftmann@62430
  1941
  with assms show ?thesis by simp
haftmann@62430
  1942
qed
haftmann@62430
  1943
haftmann@62430
  1944
lemma (in semidom_divide) msetprod_minus:
haftmann@62430
  1945
  assumes "a \<in># A" and "a \<noteq> 0"
haftmann@62430
  1946
  shows "msetprod (A - {#a#}) = msetprod A div a"
haftmann@62430
  1947
  using assms msetprod_diff [of "{#a#}" A]
haftmann@62430
  1948
    by (auto simp add: single_subset_iff)
haftmann@62430
  1949
haftmann@62430
  1950
lemma (in normalization_semidom) normalized_msetprodI:
haftmann@62430
  1951
  assumes "\<And>a. a \<in># A \<Longrightarrow> normalize a = a"
haftmann@62430
  1952
  shows "normalize (msetprod A) = msetprod A"
haftmann@62430
  1953
  using assms by (induct A) (simp_all add: normalize_mult)
haftmann@62430
  1954
haftmann@51548
  1955
wenzelm@60500
  1956
subsection \<open>Alternative representations\<close>
wenzelm@60500
  1957
wenzelm@60500
  1958
subsubsection \<open>Lists\<close>
haftmann@51548
  1959
haftmann@39533
  1960
context linorder
haftmann@39533
  1961
begin
haftmann@39533
  1962
nipkow@60515
  1963
lemma mset_insort [simp]:
nipkow@60515
  1964
  "mset (insort_key k x xs) = {#x#} + mset xs"
haftmann@37107
  1965
  by (induct xs) (simp_all add: ac_simps)
blanchet@58425
  1966
nipkow@60515
  1967
lemma mset_sort [simp]:
nipkow@60515
  1968
  "mset (sort_key k xs) = mset xs"
haftmann@37107
  1969
  by (induct xs) (simp_all add: ac_simps)
haftmann@37107
  1970
wenzelm@60500
  1971
text \<open>
haftmann@34943
  1972
  This lemma shows which properties suffice to show that a function
wenzelm@61585
  1973
  \<open>f\<close> with \<open>f xs = ys\<close> behaves like sort.
wenzelm@60500
  1974
\<close>
haftmann@37074
  1975
haftmann@39533
  1976
lemma properties_for_sort_key:
nipkow@60515
  1977
  assumes "mset ys = mset xs"
wenzelm@60606
  1978
    and "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>x. f k = f x) ys = filter (\<lambda>x. f k = f x) xs"
wenzelm@60606
  1979
    and "sorted (map f ys)"
haftmann@39533
  1980
  shows "sort_key f xs = ys"
wenzelm@60606
  1981
  using assms
wenzelm@46921
  1982
proof (induct xs arbitrary: ys)
haftmann@34943
  1983
  case Nil then show ?case by simp
haftmann@34943
  1984
next
haftmann@34943
  1985
  case (Cons x xs)
haftmann@39533
  1986
  from Cons.prems(2) have
haftmann@40305
  1987
    "\<forall>k \<in> set ys. filter (\<lambda>x. f k = f x) (remove1 x ys) = filter (\<lambda>x. f k = f x) xs"
haftmann@39533
  1988
    by (simp add: filter_remove1)
haftmann@39533
  1989
  with Cons.prems have "sort_key f xs = remove1 x ys"
haftmann@39533
  1990
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
haftmann@62430
  1991
  moreover from Cons.prems have "x \<in># mset ys"
haftmann@62430
  1992
    by auto
haftmann@62430
  1993
  then have "x \<in> set ys"
haftmann@62430
  1994
    by simp
haftmann@39533
  1995
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
haftmann@34943
  1996
qed
haftmann@34943
  1997
haftmann@39533
  1998
lemma properties_for_sort:
nipkow@60515
  1999
  assumes multiset: "mset ys = mset xs"
wenzelm@60606
  2000
    and "sorted ys"
haftmann@39533
  2001
  shows "sort xs = ys"
haftmann@39533
  2002
proof (rule properties_for_sort_key)
nipkow@60515
  2003
  from multiset show "mset ys = mset xs" .
wenzelm@60500
  2004
  from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp
wenzelm@60678
  2005
  from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k
nipkow@60515
  2006
    by (rule mset_eq_length_filter)
wenzelm@60678
  2007
  then have "replicate (length (filter (\<lambda>y. k = y) ys)) k =
wenzelm@60678
  2008
    replicate (length (filter (\<lambda>x. k = x) xs)) k" for k
haftmann@39533
  2009
    by simp
wenzelm@60678
  2010
  then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k
haftmann@39533
  2011
    by (simp add: replicate_length_filter)
haftmann@39533
  2012
qed
haftmann@39533
  2013
haftmann@61031
  2014
lemma sort_key_inj_key_eq:
haftmann@61031
  2015
  assumes mset_equal: "mset xs = mset ys"
haftmann@61031
  2016
    and "inj_on f (set xs)"
haftmann@61031
  2017
    and "sorted (map f ys)"
haftmann@61031
  2018
  shows "sort_key f xs = ys"
haftmann@61031
  2019
proof (rule properties_for_sort_key)
haftmann@61031
  2020
  from mset_equal
haftmann@61031
  2021
  show "mset ys = mset xs" by simp
wenzelm@61188
  2022
  from \<open>sorted (map f ys)\<close>
haftmann@61031
  2023
  show "sorted (map f ys)" .
haftmann@61031
  2024
  show "[x\<leftarrow>ys . f k = f x] = [x\<leftarrow>xs . f k = f x]" if "k \<in> set ys" for k
haftmann@61031
  2025
  proof -
haftmann@61031
  2026
    from mset_equal
haftmann@61031
  2027
    have set_equal: "set xs = set ys" by (rule mset_eq_setD)
haftmann@61031
  2028
    with that have "insert k (set ys) = set ys" by auto
wenzelm@61188
  2029
    with \<open>inj_on f (set xs)\<close> have inj: "inj_on f (insert k (set ys))"
haftmann@61031
  2030
      by (simp add: set_equal)
haftmann@61031
  2031
    from inj have "[x\<leftarrow>ys . f k = f x] = filter (HOL.eq k) ys"
haftmann@61031
  2032
      by (auto intro!: inj_on_filter_key_eq)
haftmann@61031
  2033
    also have "\<dots> = replicate (count (mset ys) k) k"
haftmann@61031
  2034
      by (simp add: replicate_count_mset_eq_filter_eq)
haftmann@61031
  2035
    also have "\<dots> = replicate (count (mset xs) k) k"
haftmann@61031
  2036
      using mset_equal by simp
haftmann@61031
  2037
    also have "\<dots> = filter (HOL.eq k) xs"
haftmann@61031
  2038
      by (simp add: replicate_count_mset_eq_filter_eq)
haftmann@61031
  2039
    also have "\<dots> = [x\<leftarrow>xs . f k = f x]"
haftmann@61031
  2040
      using inj by (auto intro!: inj_on_filter_key_eq [symmetric] simp add: set_equal)
haftmann@61031
  2041
    finally show ?thesis .
haftmann@61031
  2042
  qed
haftmann@61031
  2043
qed
haftmann@61031
  2044
haftmann@61031
  2045
lemma sort_key_eq_sort_key:
haftmann@61031
  2046
  assumes "mset xs = mset ys"
haftmann@61031
  2047
    and "inj_on f (set xs)"
haftmann@61031
  2048
  shows "sort_key f xs = sort_key f ys"
haftmann@61031
  2049
  by (rule sort_key_inj_key_eq) (simp_all add: assms)
haftmann@61031
  2050
haftmann@40303
  2051
lemma sort_key_by_quicksort:
haftmann@40303
  2052
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
haftmann@40303
  2053
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
haftmann@40303
  2054
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
haftmann@40303
  2055
proof (rule properties_for_sort_key)
nipkow@60515
  2056
  show "mset ?rhs = mset ?lhs"
nipkow@60515
  2057
    by (rule multiset_eqI) (auto simp add: mset_filter)
haftmann@40303
  2058
  show "sorted (map f ?rhs)"
haftmann@40303
  2059
    by (auto simp add: sorted_append intro: sorted_map_same)
haftmann@40303
  2060
next
haftmann@40305
  2061
  fix l
haftmann@40305
  2062
  assume "l \<in> set ?rhs"
haftmann@40346
  2063
  let ?pivot = "f (xs ! (length xs div 2))"
haftmann@40346
  2064
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
haftmann@40306
  2065
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
haftmann@40305
  2066
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
haftmann@40346
  2067
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
haftmann@40346
  2068
  have "\<And>x P. P (f x) ?pivot \<and> f l = f x \<longleftrightarrow> P (f l) ?pivot \<and> f l = f x" by auto
haftmann@40346
  2069
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
haftmann@40346
  2070
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
haftmann@40346
  2071
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
haftmann@40306
  2072
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
haftmann@40305
  2073
  proof (cases "f l" ?pivot rule: linorder_cases)
wenzelm@46730
  2074
    case less
wenzelm@46730
  2075
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
wenzelm@46730
  2076
    with less show ?thesis
haftmann@40346
  2077
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40305
  2078
  next
haftmann@40306
  2079
    case equal then show ?thesis
haftmann@40346
  2080
      by (simp add: * less_le)
haftmann@40305
  2081
  next
wenzelm@46730
  2082
    case greater
wenzelm@46730
  2083
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
wenzelm@46730
  2084
    with greater show ?thesis
haftmann@40346
  2085
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40306
  2086
  qed
haftmann@40303
  2087
qed
haftmann@40303
  2088
haftmann@40303
  2089
lemma sort_by_quicksort:
haftmann@40303
  2090
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
haftmann@40303
  2091
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
haftmann@40303
  2092
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
haftmann@40303
  2093
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
haftmann@40303
  2094
wenzelm@60500
  2095
text \<open>A stable parametrized quicksort\<close>
haftmann@40347
  2096
haftmann@40347
  2097
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
haftmann@40347
  2098
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
haftmann@40347
  2099
haftmann@40347
  2100
lemma part_code [code]:
haftmann@40347
  2101
  "part f pivot [] = ([], [], [])"
haftmann@40347
  2102
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
haftmann@40347
  2103
     if x' < pivot then (x # lts, eqs, gts)
haftmann@40347
  2104
     else if x' > pivot then (lts, eqs, x # gts)
haftmann@40347
  2105
     else (lts, x # eqs, gts))"
haftmann@40347
  2106
  by (auto simp add: part_def Let_def split_def)
haftmann@40347
  2107
haftmann@40347
  2108
lemma sort_key_by_quicksort_code [code]:
wenzelm@60606
  2109
  "sort_key f xs =
wenzelm@60606
  2110
    (case xs of
wenzelm@60606
  2111
      [] \<Rightarrow> []
haftmann@40347
  2112
    | [x] \<Rightarrow> xs
haftmann@40347
  2113
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
wenzelm@60606
  2114
    | _ \<Rightarrow>
wenzelm@60606
  2115
        let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
wenzelm@60606
  2116
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  2117
proof (cases xs)
haftmann@40347
  2118
  case Nil then show ?thesis by simp
haftmann@40347
  2119
next
wenzelm@46921
  2120
  case (Cons _ ys) note hyps = Cons show ?thesis
wenzelm@46921
  2121
  proof (cases ys)
haftmann@40347
  2122
    case Nil with hyps show ?thesis by simp
haftmann@40347
  2123
  next
wenzelm@46921
  2124
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
wenzelm@46921
  2125
    proof (cases zs)
haftmann@40347
  2126
      case Nil with hyps show ?thesis by auto
haftmann@40347
  2127
    next
blanchet@58425
  2128
      case Cons
haftmann@40347
  2129
      from sort_key_by_quicksort [of f xs]
haftmann@40347
  2130
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  2131
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  2132
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
haftmann@40347
  2133
      with hyps Cons show ?thesis by (simp only: list.cases)
haftmann@40347
  2134
    qed
haftmann@40347
  2135
  qed
haftmann@40347
  2136
qed
haftmann@40347
  2137
haftmann@39533
  2138
end
haftmann@39533
  2139
haftmann@40347
  2140
hide_const (open) part
haftmann@40347
  2141
Mathias@63310
  2142
lemma mset_remdups_subset_eq: "mset (remdups xs) \<subseteq># mset xs"
Mathias@60397
  2143
  by (induct xs) (auto intro: subset_mset.order_trans)
haftmann@34943
  2144
nipkow@60515
  2145
lemma mset_update:
nipkow@60515
  2146
  "i < length ls \<Longrightarrow> mset (ls[i := v]) = mset ls - {#ls ! i#} + {#v#}"
haftmann@34943
  2147
proof (induct ls arbitrary: i)
haftmann@34943
  2148
  case Nil then show ?case by simp
haftmann@34943
  2149
next
haftmann@34943
  2150
  case (Cons x xs)
haftmann@34943
  2151
  show ?case
haftmann@34943
  2152
  proof (cases i)
haftmann@34943
  2153
    case 0 then show ?thesis by simp
haftmann@34943
  2154
  next
haftmann@34943
  2155
    case (Suc i')
haftmann@34943
  2156
    with Cons show ?thesis
haftmann@34943
  2157
      apply simp
haftmann@57512
  2158
      apply (subst add.assoc)
haftmann@57512
  2159
      apply (subst add.commute [of "{#v#}" "{#x#}"])
haftmann@57512
  2160
      apply (subst add.assoc [symmetric])
haftmann@34943
  2161
      apply simp
Mathias@63310
  2162
      apply (rule mset_subset_eq_multiset_union_diff_commute)
Mathias@63310
  2163
      apply (simp add: mset_subset_eq_single nth_mem_mset)
haftmann@34943
  2164
      done
haftmann@34943
  2165
  qed
haftmann@34943
  2166
qed
haftmann@34943
  2167
nipkow@60515
  2168
lemma mset_swap:
haftmann@34943
  2169
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
nipkow@60515
  2170
    mset (ls[j := ls ! i, i := ls ! j]) = mset ls"
nipkow@60515
  2171
  by (cases "i = j") (simp_all add: mset_update nth_mem_mset)
haftmann@34943
  2172
haftmann@34943
  2173
wenzelm@60500
  2174
subsection \<open>The multiset order\<close>
wenzelm@60500
  2175
wenzelm@60500
  2176
subsubsection \<open>Well-foundedness\<close>
wenzelm@10249
  2177
wenzelm@60606
  2178
definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  2179
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
wenzelm@60607
  2180
      (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}"
wenzelm@60606
  2181
wenzelm@60606
  2182
definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  2183
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
  2184
haftmann@62430
  2185
lemma mult1I:
haftmann@62430
  2186
  assumes "M = M0 + {#a#}" and "N = M0 + K" and "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
haftmann@62430
  2187
  shows "(N, M) \<in> mult1 r"
haftmann@62430
  2188
  using assms unfolding mult1_def by blast
haftmann@62430
  2189
haftmann@62430
  2190
lemma mult1E:
haftmann@62430
  2191
  assumes "(N, M) \<in> mult1 r"
haftmann@62430
  2192
  obtains a M0 K where "M = M0 + {#a#}" "N = M0 + K" "\<And>b. b \<in># K \<Longrightarrow> (b, a) \<in> r"
haftmann@62430
  2193
  using assms unfolding mult1_def by blast
haftmann@62430
  2194
berghofe@23751
  2195
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
nipkow@26178
  2196
by (simp add: mult1_def)
wenzelm@10249
  2197
wenzelm@60608
  2198
lemma less_add:
wenzelm@60608
  2199
  assumes mult1: "(N, M0 + {#a#}) \<in> mult1 r"
wenzelm@60608
  2200
  shows
wenzelm@60608
  2201
    "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
wenzelm@60608