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