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