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