src/HOL/Library/Multiset.thy
author wenzelm
Mon Jul 06 22:06:02 2015 +0200 (2015-07-06)
changeset 60678 17ba2df56dee
parent 60613 f11e9fd70b7d
child 60752 b48830b670a1
permissions -rw-r--r--
tuned proofs;
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
abbreviation Melem :: "'a \<Rightarrow> 'a multiset \<Rightarrow> bool"  ("(_/ :# _)" [50, 51] 50) where
kleing@25610
    29
  "a :# M == 0 < count M a"
kleing@25610
    30
wenzelm@26145
    31
notation (xsymbols)
wenzelm@26145
    32
  Melem (infix "\<in>#" 50)
wenzelm@10249
    33
wenzelm@60606
    34
lemma multiset_eq_iff: "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
nipkow@39302
    35
  by (simp only: count_inject [symmetric] fun_eq_iff)
haftmann@34943
    36
wenzelm@60606
    37
lemma multiset_eqI: "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
nipkow@39302
    38
  using multiset_eq_iff by auto
haftmann@34943
    39
wenzelm@60606
    40
text \<open>Preservation of the representing set @{term multiset}.\<close>
wenzelm@60606
    41
wenzelm@60606
    42
lemma const0_in_multiset: "(\<lambda>a. 0) \<in> multiset"
haftmann@34943
    43
  by (simp add: multiset_def)
haftmann@34943
    44
wenzelm@60606
    45
lemma only1_in_multiset: "(\<lambda>b. if b = a then n else 0) \<in> multiset"
haftmann@34943
    46
  by (simp add: multiset_def)
haftmann@34943
    47
wenzelm@60606
    48
lemma union_preserves_multiset: "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
haftmann@34943
    49
  by (simp add: multiset_def)
haftmann@34943
    50
haftmann@34943
    51
lemma diff_preserves_multiset:
haftmann@34943
    52
  assumes "M \<in> multiset"
haftmann@34943
    53
  shows "(\<lambda>a. M a - N a) \<in> multiset"
haftmann@34943
    54
proof -
haftmann@34943
    55
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    56
    by auto
haftmann@34943
    57
  with assms show ?thesis
haftmann@34943
    58
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    59
qed
haftmann@34943
    60
haftmann@41069
    61
lemma filter_preserves_multiset:
haftmann@34943
    62
  assumes "M \<in> multiset"
haftmann@34943
    63
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
haftmann@34943
    64
proof -
haftmann@34943
    65
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    66
    by auto
haftmann@34943
    67
  with assms show ?thesis
haftmann@34943
    68
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    69
qed
haftmann@34943
    70
haftmann@34943
    71
lemmas in_multiset = const0_in_multiset only1_in_multiset
haftmann@41069
    72
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
haftmann@34943
    73
haftmann@34943
    74
wenzelm@60500
    75
subsection \<open>Representing multisets\<close>
wenzelm@60500
    76
wenzelm@60500
    77
text \<open>Multiset enumeration\<close>
haftmann@34943
    78
huffman@48008
    79
instantiation multiset :: (type) cancel_comm_monoid_add
haftmann@25571
    80
begin
haftmann@25571
    81
bulwahn@47429
    82
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
bulwahn@47429
    83
by (rule const0_in_multiset)
haftmann@25571
    84
haftmann@34943
    85
abbreviation Mempty :: "'a multiset" ("{#}") where
haftmann@34943
    86
  "Mempty \<equiv> 0"
haftmann@25571
    87
wenzelm@60606
    88
lift_definition plus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
bulwahn@47429
    89
by (rule union_preserves_multiset)
haftmann@25571
    90
wenzelm@60606
    91
lift_definition minus_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
haftmann@59815
    92
by (rule diff_preserves_multiset)
haftmann@59815
    93
huffman@48008
    94
instance
wenzelm@60678
    95
  by (standard; transfer; simp add: fun_eq_iff)
haftmann@25571
    96
haftmann@25571
    97
end
wenzelm@10249
    98
wenzelm@60606
    99
lift_definition single :: "'a \<Rightarrow> 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
bulwahn@47429
   100
by (rule only1_in_multiset)
kleing@15869
   101
wenzelm@26145
   102
syntax
wenzelm@60606
   103
  "_multiset" :: "args \<Rightarrow> 'a multiset"    ("{#(_)#}")
nipkow@25507
   104
translations
nipkow@25507
   105
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
   106
  "{#x#}" == "CONST single x"
nipkow@25507
   107
haftmann@34943
   108
lemma count_empty [simp]: "count {#} a = 0"
bulwahn@47429
   109
  by (simp add: zero_multiset.rep_eq)
wenzelm@10249
   110
haftmann@34943
   111
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
bulwahn@47429
   112
  by (simp add: single.rep_eq)
nipkow@29901
   113
wenzelm@10249
   114
wenzelm@60500
   115
subsection \<open>Basic operations\<close>
wenzelm@60500
   116
wenzelm@60500
   117
subsubsection \<open>Union\<close>
wenzelm@10249
   118
haftmann@34943
   119
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
bulwahn@47429
   120
  by (simp add: plus_multiset.rep_eq)
wenzelm@10249
   121
wenzelm@10249
   122
wenzelm@60500
   123
subsubsection \<open>Difference\<close>
wenzelm@10249
   124
haftmann@49388
   125
instantiation multiset :: (type) comm_monoid_diff
haftmann@34943
   126
begin
haftmann@34943
   127
haftmann@49388
   128
instance
wenzelm@60678
   129
  by (standard; transfer; simp add: fun_eq_iff)
haftmann@34943
   130
haftmann@34943
   131
end
haftmann@34943
   132
haftmann@34943
   133
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
bulwahn@47429
   134
  by (simp add: minus_multiset.rep_eq)
haftmann@34943
   135
wenzelm@17161
   136
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
haftmann@52289
   137
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
nipkow@36903
   138
nipkow@36903
   139
lemma diff_cancel[simp]: "A - A = {#}"
haftmann@52289
   140
  by (fact Groups.diff_cancel)
wenzelm@10249
   141
nipkow@36903
   142
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
haftmann@52289
   143
  by (fact add_diff_cancel_right')
wenzelm@10249
   144
nipkow@36903
   145
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
haftmann@52289
   146
  by (fact add_diff_cancel_left')
haftmann@34943
   147
haftmann@52289
   148
lemma diff_right_commute:
wenzelm@60606
   149
  fixes M N Q :: "'a multiset"
wenzelm@60606
   150
  shows "M - N - Q = M - Q - N"
haftmann@52289
   151
  by (fact diff_right_commute)
haftmann@52289
   152
haftmann@52289
   153
lemma diff_add:
wenzelm@60606
   154
  fixes M N Q :: "'a multiset"
wenzelm@60606
   155
  shows "M - (N + Q) = M - N - Q"
haftmann@52289
   156
  by (rule sym) (fact diff_diff_add)
blanchet@58425
   157
wenzelm@60606
   158
lemma insert_DiffM: "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
nipkow@39302
   159
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   160
wenzelm@60606
   161
lemma insert_DiffM2 [simp]: "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
nipkow@39302
   162
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   163
wenzelm@60606
   164
lemma diff_union_swap: "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
nipkow@39302
   165
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   166
wenzelm@60606
   167
lemma diff_union_single_conv: "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
nipkow@39302
   168
  by (simp add: multiset_eq_iff)
bulwahn@26143
   169
wenzelm@10249
   170
wenzelm@60500
   171
subsubsection \<open>Equality of multisets\<close>
haftmann@34943
   172
haftmann@34943
   173
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
nipkow@39302
   174
  by (simp add: multiset_eq_iff)
haftmann@34943
   175
haftmann@34943
   176
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
nipkow@39302
   177
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   178
haftmann@34943
   179
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   180
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   181
haftmann@34943
   182
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   183
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   184
haftmann@34943
   185
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
nipkow@39302
   186
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   187
wenzelm@60606
   188
lemma diff_single_trivial: "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
nipkow@39302
   189
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   190
wenzelm@60606
   191
lemma diff_single_eq_union: "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
haftmann@34943
   192
  by auto
haftmann@34943
   193
wenzelm@60606
   194
lemma union_single_eq_diff: "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
haftmann@34943
   195
  by (auto dest: sym)
haftmann@34943
   196
wenzelm@60606
   197
lemma union_single_eq_member: "M + {#x#} = N \<Longrightarrow> x \<in># N"
haftmann@34943
   198
  by auto
haftmann@34943
   199
wenzelm@60606
   200
lemma union_is_single: "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}"
wenzelm@60606
   201
  (is "?lhs = ?rhs")
wenzelm@46730
   202
proof
wenzelm@60606
   203
  show ?lhs if ?rhs using that by auto
wenzelm@60606
   204
  show ?rhs if ?lhs
wenzelm@60606
   205
    using that by (simp add: multiset_eq_iff split: if_splits) (metis add_is_1)
haftmann@34943
   206
qed
haftmann@34943
   207
wenzelm@60606
   208
lemma single_is_union: "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
haftmann@34943
   209
  by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
haftmann@34943
   210
haftmann@34943
   211
lemma add_eq_conv_diff:
wenzelm@60606
   212
  "M + {#a#} = N + {#b#} \<longleftrightarrow> M = N \<and> a = b \<or> M = N - {#a#} + {#b#} \<and> N = M - {#b#} + {#a#}"
wenzelm@60606
   213
  (is "?lhs \<longleftrightarrow> ?rhs")
nipkow@44890
   214
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
haftmann@34943
   215
proof
wenzelm@60606
   216
  show ?lhs if ?rhs
wenzelm@60606
   217
    using that
wenzelm@60606
   218
    by (auto simp add: add.assoc add.commute [of "{#b#}"])
wenzelm@60606
   219
      (drule sym, simp add: add.assoc [symmetric])
wenzelm@60606
   220
  show ?rhs if ?lhs
haftmann@34943
   221
  proof (cases "a = b")
wenzelm@60500
   222
    case True with \<open>?lhs\<close> show ?thesis by simp
haftmann@34943
   223
  next
haftmann@34943
   224
    case False
wenzelm@60500
   225
    from \<open>?lhs\<close> have "a \<in># N + {#b#}" by (rule union_single_eq_member)
haftmann@34943
   226
    with False have "a \<in># N" by auto
wenzelm@60500
   227
    moreover from \<open>?lhs\<close> have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
haftmann@34943
   228
    moreover note False
haftmann@34943
   229
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
haftmann@34943
   230
  qed
haftmann@34943
   231
qed
haftmann@34943
   232
blanchet@58425
   233
lemma insert_noteq_member:
haftmann@34943
   234
  assumes BC: "B + {#b#} = C + {#c#}"
haftmann@34943
   235
   and bnotc: "b \<noteq> c"
haftmann@34943
   236
  shows "c \<in># B"
haftmann@34943
   237
proof -
haftmann@34943
   238
  have "c \<in># C + {#c#}" by simp
haftmann@34943
   239
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
haftmann@34943
   240
  then have "c \<in># B + {#b#}" using BC by simp
haftmann@34943
   241
  then show "c \<in># B" using nc by simp
haftmann@34943
   242
qed
haftmann@34943
   243
haftmann@34943
   244
lemma add_eq_conv_ex:
haftmann@34943
   245
  "(M + {#a#} = N + {#b#}) =
haftmann@34943
   246
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
haftmann@34943
   247
  by (auto simp add: add_eq_conv_diff)
haftmann@34943
   248
wenzelm@60606
   249
lemma multi_member_split: "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
wenzelm@60678
   250
  by (rule exI [where x = "M - {#x#}"]) simp
haftmann@51600
   251
blanchet@58425
   252
lemma multiset_add_sub_el_shuffle:
wenzelm@60606
   253
  assumes "c \<in># B"
wenzelm@60606
   254
    and "b \<noteq> c"
haftmann@58098
   255
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
haftmann@58098
   256
proof -
wenzelm@60500
   257
  from \<open>c \<in># B\<close> obtain A where B: "B = A + {#c#}"
haftmann@58098
   258
    by (blast dest: multi_member_split)
haftmann@58098
   259
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
blanchet@58425
   260
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}"
haftmann@58098
   261
    by (simp add: ac_simps)
haftmann@58098
   262
  then show ?thesis using B by simp
haftmann@58098
   263
qed
haftmann@58098
   264
haftmann@34943
   265
wenzelm@60500
   266
subsubsection \<open>Pointwise ordering induced by count\<close>
haftmann@34943
   267
Mathias@60397
   268
definition subseteq_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
Mathias@60400
   269
"subseteq_mset A B = (\<forall>a. count A a \<le> count B a)"
Mathias@60397
   270
Mathias@60397
   271
definition subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
Mathias@60400
   272
"subset_mset A B = (A <=# B \<and> A \<noteq> B)"
Mathias@60397
   273
Mathias@60397
   274
notation subseteq_mset (infix "\<le>#" 50)
Mathias@60397
   275
notation (xsymbols) subseteq_mset (infix "\<subseteq>#" 50)
Mathias@60397
   276
Mathias@60397
   277
notation (xsymbols) subset_mset (infix "\<subset>#" 50)
Mathias@60397
   278
wenzelm@60606
   279
interpretation subset_mset: ordered_ab_semigroup_add_imp_le "op +" "op -" "op \<subseteq>#" "op \<subset>#"
wenzelm@60678
   280
  by standard (auto simp add: subset_mset_def subseteq_mset_def multiset_eq_iff intro: order_trans antisym)
blanchet@59986
   281
wenzelm@60606
   282
lemma mset_less_eqI: "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le># B"
Mathias@60397
   283
  by (simp add: subseteq_mset_def)
haftmann@34943
   284
wenzelm@60606
   285
lemma mset_le_exists_conv: "(A::'a multiset) \<le># B \<longleftrightarrow> (\<exists>C. B = A + C)"
wenzelm@60678
   286
  unfolding subseteq_mset_def
wenzelm@60678
   287
  apply (rule iffI)
wenzelm@60678
   288
   apply (rule exI [where x = "B - A"])
wenzelm@60678
   289
   apply (auto intro: multiset_eq_iff [THEN iffD2])
wenzelm@60678
   290
  done
haftmann@34943
   291
Mathias@60397
   292
interpretation subset_mset: ordered_cancel_comm_monoid_diff  "op +" "op -" 0 "op \<le>#" "op <#"
wenzelm@60678
   293
  by standard (simp, fact mset_le_exists_conv)
haftmann@52289
   294
wenzelm@60606
   295
lemma mset_le_mono_add_right_cancel [simp]: "(A::'a multiset) + C \<le># B + C \<longleftrightarrow> A \<le># B"
Mathias@60397
   296
  by (fact subset_mset.add_le_cancel_right)
haftmann@34943
   297
wenzelm@60606
   298
lemma mset_le_mono_add_left_cancel [simp]: "C + (A::'a multiset) \<le># C + B \<longleftrightarrow> A \<le># B"
Mathias@60397
   299
  by (fact subset_mset.add_le_cancel_left)
haftmann@35268
   300
wenzelm@60606
   301
lemma mset_le_mono_add: "(A::'a multiset) \<le># B \<Longrightarrow> C \<le># D \<Longrightarrow> A + C \<le># B + D"
Mathias@60397
   302
  by (fact subset_mset.add_mono)
haftmann@34943
   303
wenzelm@60606
   304
lemma mset_le_add_left [simp]: "(A::'a multiset) \<le># A + B"
Mathias@60397
   305
  unfolding subseteq_mset_def by auto
haftmann@35268
   306
wenzelm@60606
   307
lemma mset_le_add_right [simp]: "B \<le># (A::'a multiset) + B"
Mathias@60397
   308
  unfolding subseteq_mset_def by auto
haftmann@34943
   309
wenzelm@60607
   310
lemma mset_le_single: "a \<in># B \<Longrightarrow> {#a#} \<le># B"
Mathias@60397
   311
  by (simp add: subseteq_mset_def)
haftmann@34943
   312
haftmann@35268
   313
lemma multiset_diff_union_assoc:
wenzelm@60606
   314
  fixes A B C D :: "'a multiset"
wenzelm@60606
   315
  shows "C \<le># B \<Longrightarrow> A + B - C = A + (B - C)"
Mathias@60397
   316
  by (simp add: subset_mset.diff_add_assoc)
haftmann@34943
   317
haftmann@34943
   318
lemma mset_le_multiset_union_diff_commute:
wenzelm@60606
   319
  fixes A B C D :: "'a multiset"
wenzelm@60606
   320
  shows "B \<le># A \<Longrightarrow> A - B + C = A + C - B"
Mathias@60397
   321
by (simp add: subset_mset.add_diff_assoc2)
Mathias@60397
   322
Mathias@60397
   323
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le># M"
Mathias@60397
   324
by(simp add: subseteq_mset_def)
Mathias@60397
   325
Mathias@60397
   326
lemma mset_lessD: "A <# B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
Mathias@60397
   327
apply (clarsimp simp: subset_mset_def subseteq_mset_def)
wenzelm@60678
   328
apply (erule allE [where x = x])
haftmann@34943
   329
apply auto
haftmann@34943
   330
done
haftmann@34943
   331
Mathias@60397
   332
lemma mset_leD: "A \<le># B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
Mathias@60397
   333
apply (clarsimp simp: subset_mset_def subseteq_mset_def)
wenzelm@60678
   334
apply (erule allE [where x = x])
haftmann@34943
   335
apply auto
haftmann@34943
   336
done
blanchet@58425
   337
Mathias@60397
   338
lemma mset_less_insertD: "(A + {#x#} <# B) \<Longrightarrow> (x \<in># B \<and> A <# B)"
haftmann@34943
   339
apply (rule conjI)
haftmann@34943
   340
 apply (simp add: mset_lessD)
Mathias@60397
   341
apply (clarsimp simp: subset_mset_def subseteq_mset_def)
haftmann@34943
   342
apply safe
haftmann@34943
   343
 apply (erule_tac x = a in allE)
haftmann@34943
   344
 apply (auto split: split_if_asm)
haftmann@34943
   345
done
haftmann@34943
   346
Mathias@60397
   347
lemma mset_le_insertD: "(A + {#x#} \<le># B) \<Longrightarrow> (x \<in># B \<and> A \<le># B)"
haftmann@34943
   348
apply (rule conjI)
haftmann@34943
   349
 apply (simp add: mset_leD)
Mathias@60397
   350
apply (force simp: subset_mset_def subseteq_mset_def split: split_if_asm)
haftmann@34943
   351
done
haftmann@34943
   352
Mathias@60397
   353
lemma mset_less_of_empty[simp]: "A <# {#} \<longleftrightarrow> False"
Mathias@60397
   354
  by (auto simp add: subseteq_mset_def subset_mset_def multiset_eq_iff)
Mathias@60397
   355
Mathias@60397
   356
lemma empty_le[simp]: "{#} \<le># A"
nipkow@55808
   357
  unfolding mset_le_exists_conv by auto
nipkow@55808
   358
Mathias@60397
   359
lemma le_empty[simp]: "(M \<le># {#}) = (M = {#})"
nipkow@55808
   360
  unfolding mset_le_exists_conv by auto
nipkow@55808
   361
Mathias@60397
   362
lemma multi_psub_of_add_self[simp]: "A <# A + {#x#}"
Mathias@60397
   363
  by (auto simp: subset_mset_def subseteq_mset_def)
Mathias@60397
   364
Mathias@60397
   365
lemma multi_psub_self[simp]: "(A::'a multiset) <# A = False"
haftmann@35268
   366
  by simp
haftmann@34943
   367
Mathias@60397
   368
lemma mset_less_add_bothsides: "N + {#x#} <# M + {#x#} \<Longrightarrow> N <# M"
Mathias@60397
   369
  by (fact subset_mset.add_less_imp_less_right)
haftmann@35268
   370
wenzelm@60606
   371
lemma mset_less_empty_nonempty: "{#} <# S \<longleftrightarrow> S \<noteq> {#}"
Mathias@60397
   372
  by (auto simp: subset_mset_def subseteq_mset_def)
haftmann@35268
   373
wenzelm@60606
   374
lemma mset_less_diff_self: "c \<in># B \<Longrightarrow> B - {#c#} <# B"
Mathias@60397
   375
  by (auto simp: subset_mset_def subseteq_mset_def multiset_eq_iff)
haftmann@35268
   376
haftmann@35268
   377
wenzelm@60500
   378
subsubsection \<open>Intersection\<close>
haftmann@35268
   379
Mathias@60397
   380
definition inf_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
Mathias@60397
   381
  multiset_inter_def: "inf_subset_mset A B = A - (A - B)"
Mathias@60397
   382
Mathias@60397
   383
interpretation subset_mset: semilattice_inf inf_subset_mset "op \<le>#" "op <#"
wenzelm@46921
   384
proof -
wenzelm@60678
   385
  have [simp]: "m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" for m n q :: nat
wenzelm@60678
   386
    by arith
wenzelm@60678
   387
  show "class.semilattice_inf op #\<inter> op \<le># op <#"
wenzelm@60678
   388
    by standard (auto simp add: multiset_inter_def subseteq_mset_def)
haftmann@35268
   389
qed
haftmann@35268
   390
haftmann@34943
   391
haftmann@41069
   392
lemma multiset_inter_count [simp]:
wenzelm@60606
   393
  fixes A B :: "'a multiset"
wenzelm@60606
   394
  shows "count (A #\<inter> B) x = min (count A x) (count B x)"
bulwahn@47429
   395
  by (simp add: multiset_inter_def)
haftmann@35268
   396
haftmann@35268
   397
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
wenzelm@46730
   398
  by (rule multiset_eqI) auto
haftmann@34943
   399
haftmann@35268
   400
lemma multiset_union_diff_commute:
haftmann@35268
   401
  assumes "B #\<inter> C = {#}"
haftmann@35268
   402
  shows "A + B - C = A - C + B"
nipkow@39302
   403
proof (rule multiset_eqI)
haftmann@35268
   404
  fix x
haftmann@35268
   405
  from assms have "min (count B x) (count C x) = 0"
wenzelm@46730
   406
    by (auto simp add: multiset_eq_iff)
haftmann@35268
   407
  then have "count B x = 0 \<or> count C x = 0"
haftmann@35268
   408
    by auto
haftmann@35268
   409
  then show "count (A + B - C) x = count (A - C + B) x"
haftmann@35268
   410
    by auto
haftmann@35268
   411
qed
haftmann@35268
   412
wenzelm@60606
   413
lemma empty_inter [simp]: "{#} #\<inter> M = {#}"
haftmann@51600
   414
  by (simp add: multiset_eq_iff)
haftmann@51600
   415
wenzelm@60606
   416
lemma inter_empty [simp]: "M #\<inter> {#} = {#}"
haftmann@51600
   417
  by (simp add: multiset_eq_iff)
haftmann@51600
   418
wenzelm@60606
   419
lemma inter_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
haftmann@51600
   420
  by (simp add: multiset_eq_iff)
haftmann@51600
   421
wenzelm@60606
   422
lemma inter_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
haftmann@51600
   423
  by (simp add: multiset_eq_iff)
haftmann@51600
   424
wenzelm@60606
   425
lemma inter_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
haftmann@51600
   426
  by (simp add: multiset_eq_iff)
haftmann@51600
   427
wenzelm@60606
   428
lemma inter_add_right2: "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
haftmann@51600
   429
  by (simp add: multiset_eq_iff)
haftmann@51600
   430
haftmann@35268
   431
wenzelm@60500
   432
subsubsection \<open>Bounded union\<close>
wenzelm@60678
   433
wenzelm@60678
   434
definition sup_subset_mset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"(infixl "#\<union>" 70)
wenzelm@60678
   435
  where "sup_subset_mset A B = A + (B - A)"
Mathias@60397
   436
Mathias@60397
   437
interpretation subset_mset: semilattice_sup sup_subset_mset "op \<le>#" "op <#"
haftmann@51623
   438
proof -
wenzelm@60678
   439
  have [simp]: "m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" for m n q :: nat
wenzelm@60678
   440
    by arith
Mathias@60397
   441
  show "class.semilattice_sup op #\<union> op \<le># op <#"
wenzelm@60678
   442
    by standard (auto simp add: sup_subset_mset_def subseteq_mset_def)
haftmann@51623
   443
qed
haftmann@51623
   444
wenzelm@60606
   445
lemma sup_subset_mset_count [simp]: "count (A #\<union> B) x = max (count A x) (count B x)"
Mathias@60397
   446
  by (simp add: sup_subset_mset_def)
haftmann@51623
   447
wenzelm@60606
   448
lemma empty_sup [simp]: "{#} #\<union> M = M"
haftmann@51623
   449
  by (simp add: multiset_eq_iff)
haftmann@51623
   450
wenzelm@60606
   451
lemma sup_empty [simp]: "M #\<union> {#} = M"
haftmann@51623
   452
  by (simp add: multiset_eq_iff)
haftmann@51623
   453
wenzelm@60606
   454
lemma sup_add_left1: "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
haftmann@51623
   455
  by (simp add: multiset_eq_iff)
haftmann@51623
   456
wenzelm@60606
   457
lemma sup_add_left2: "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
haftmann@51623
   458
  by (simp add: multiset_eq_iff)
haftmann@51623
   459
wenzelm@60606
   460
lemma sup_add_right1: "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
haftmann@51623
   461
  by (simp add: multiset_eq_iff)
haftmann@51623
   462
wenzelm@60606
   463
lemma sup_add_right2: "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
haftmann@51623
   464
  by (simp add: multiset_eq_iff)
haftmann@51623
   465
wenzelm@60500
   466
subsubsection \<open>Subset is an order\<close>
Mathias@60397
   467
interpretation subset_mset: order "op \<le>#" "op <#" by unfold_locales auto
haftmann@51623
   468
wenzelm@60500
   469
subsubsection \<open>Filter (with comprehension syntax)\<close>
wenzelm@60500
   470
wenzelm@60500
   471
text \<open>Multiset comprehension\<close>
haftmann@41069
   472
nipkow@59998
   473
lift_definition filter_mset :: "('a \<Rightarrow> bool) \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset"
nipkow@59998
   474
is "\<lambda>P M. \<lambda>x. if P x then M x else 0"
bulwahn@47429
   475
by (rule filter_preserves_multiset)
haftmann@35268
   476
wenzelm@60606
   477
lemma count_filter_mset [simp]: "count (filter_mset P M) a = (if P a then count M a else 0)"
nipkow@59998
   478
  by (simp add: filter_mset.rep_eq)
nipkow@59998
   479
wenzelm@60606
   480
lemma filter_empty_mset [simp]: "filter_mset P {#} = {#}"
nipkow@59998
   481
  by (rule multiset_eqI) simp
nipkow@59998
   482
wenzelm@60606
   483
lemma filter_single_mset [simp]: "filter_mset P {#x#} = (if P x then {#x#} else {#})"
nipkow@39302
   484
  by (rule multiset_eqI) simp
haftmann@35268
   485
wenzelm@60606
   486
lemma filter_union_mset [simp]: "filter_mset P (M + N) = filter_mset P M + filter_mset P N"
haftmann@41069
   487
  by (rule multiset_eqI) simp
haftmann@41069
   488
wenzelm@60606
   489
lemma filter_diff_mset [simp]: "filter_mset P (M - N) = filter_mset P M - filter_mset P N"
nipkow@39302
   490
  by (rule multiset_eqI) simp
haftmann@35268
   491
wenzelm@60606
   492
lemma filter_inter_mset [simp]: "filter_mset P (M #\<inter> N) = filter_mset P M #\<inter> filter_mset P N"
haftmann@41069
   493
  by (rule multiset_eqI) simp
haftmann@41069
   494
Mathias@60397
   495
lemma multiset_filter_subset[simp]: "filter_mset f M \<le># M"
Mathias@60397
   496
  by (simp add: mset_less_eqI)
Mathias@60397
   497
wenzelm@60606
   498
lemma multiset_filter_mono:
wenzelm@60606
   499
  assumes "A \<le># B"
Mathias@60397
   500
  shows "filter_mset f A \<le># filter_mset f B"
blanchet@58035
   501
proof -
blanchet@58035
   502
  from assms[unfolded mset_le_exists_conv]
blanchet@58035
   503
  obtain C where B: "B = A + C" by auto
blanchet@58035
   504
  show ?thesis unfolding B by auto
blanchet@58035
   505
qed
blanchet@58035
   506
haftmann@41069
   507
syntax
haftmann@41069
   508
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
haftmann@41069
   509
syntax (xsymbol)
haftmann@41069
   510
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
haftmann@41069
   511
translations
nipkow@59998
   512
  "{#x \<in># M. P#}" == "CONST filter_mset (\<lambda>x. P) M"
haftmann@41069
   513
wenzelm@10249
   514
wenzelm@60500
   515
subsubsection \<open>Set of elements\<close>
wenzelm@10249
   516
wenzelm@60606
   517
definition set_mset :: "'a multiset \<Rightarrow> 'a set"
wenzelm@60607
   518
  where "set_mset M = {x. x \<in># M}"
nipkow@60495
   519
nipkow@60495
   520
lemma set_mset_empty [simp]: "set_mset {#} = {}"
nipkow@60495
   521
by (simp add: set_mset_def)
nipkow@60495
   522
nipkow@60495
   523
lemma set_mset_single [simp]: "set_mset {#b#} = {b}"
nipkow@60495
   524
by (simp add: set_mset_def)
nipkow@60495
   525
nipkow@60495
   526
lemma set_mset_union [simp]: "set_mset (M + N) = set_mset M \<union> set_mset N"
nipkow@60495
   527
by (auto simp add: set_mset_def)
nipkow@60495
   528
nipkow@60495
   529
lemma set_mset_eq_empty_iff [simp]: "(set_mset M = {}) = (M = {#})"
nipkow@60495
   530
by (auto simp add: set_mset_def multiset_eq_iff)
nipkow@60495
   531
wenzelm@60607
   532
lemma mem_set_mset_iff [simp]: "(x \<in> set_mset M) = (x \<in># M)"
nipkow@60495
   533
by (auto simp add: set_mset_def)
nipkow@60495
   534
wenzelm@60607
   535
lemma set_mset_filter [simp]: "set_mset {# x\<in>#M. P x #} = set_mset M \<inter> {x. P x}"
nipkow@60495
   536
by (auto simp add: set_mset_def)
nipkow@60495
   537
nipkow@60495
   538
lemma finite_set_mset [iff]: "finite (set_mset M)"
nipkow@60495
   539
  using count [of M] by (simp add: multiset_def set_mset_def)
haftmann@34943
   540
wenzelm@60607
   541
lemma finite_Collect_mem [iff]: "finite {x. x \<in># M}"
nipkow@60495
   542
  unfolding set_mset_def[symmetric] by simp
nipkow@60495
   543
nipkow@60495
   544
lemma set_mset_mono: "A \<le># B \<Longrightarrow> set_mset A \<subseteq> set_mset B"
nipkow@60495
   545
  by (metis mset_leD subsetI mem_set_mset_iff)
nipkow@60495
   546
nipkow@60495
   547
lemma ball_set_mset_iff: "(\<forall>x \<in> set_mset M. P x) \<longleftrightarrow> (\<forall>x. x \<in># M \<longrightarrow> P x)"
blanchet@59813
   548
  by auto
blanchet@59813
   549
blanchet@59813
   550
wenzelm@60500
   551
subsubsection \<open>Size\<close>
wenzelm@10249
   552
blanchet@56656
   553
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
blanchet@56656
   554
blanchet@56656
   555
lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a"
blanchet@56656
   556
  by (auto simp: wcount_def add_mult_distrib)
blanchet@56656
   557
blanchet@56656
   558
definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
nipkow@60495
   559
  "size_multiset f M = setsum (wcount f M) (set_mset M)"
blanchet@56656
   560
blanchet@56656
   561
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
blanchet@56656
   562
wenzelm@60606
   563
instantiation multiset :: (type) size
wenzelm@60606
   564
begin
wenzelm@60606
   565
blanchet@56656
   566
definition size_multiset where
blanchet@56656
   567
  size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)"
haftmann@34943
   568
instance ..
wenzelm@60606
   569
haftmann@34943
   570
end
haftmann@34943
   571
blanchet@56656
   572
lemmas size_multiset_overloaded_eq =
blanchet@56656
   573
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
blanchet@56656
   574
blanchet@56656
   575
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
blanchet@56656
   576
by (simp add: size_multiset_def)
blanchet@56656
   577
haftmann@28708
   578
lemma size_empty [simp]: "size {#} = 0"
blanchet@56656
   579
by (simp add: size_multiset_overloaded_def)
blanchet@56656
   580
blanchet@56656
   581
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"
blanchet@56656
   582
by (simp add: size_multiset_eq)
wenzelm@10249
   583
haftmann@28708
   584
lemma size_single [simp]: "size {#b#} = 1"
blanchet@56656
   585
by (simp add: size_multiset_overloaded_def)
blanchet@56656
   586
blanchet@56656
   587
lemma setsum_wcount_Int:
nipkow@60495
   588
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_mset N) = setsum (wcount f N) A"
nipkow@26178
   589
apply (induct rule: finite_induct)
nipkow@26178
   590
 apply simp
nipkow@60495
   591
apply (simp add: Int_insert_left set_mset_def wcount_def)
blanchet@56656
   592
done
blanchet@56656
   593
blanchet@56656
   594
lemma size_multiset_union [simp]:
blanchet@56656
   595
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
haftmann@57418
   596
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union)
blanchet@56656
   597
apply (subst Int_commute)
blanchet@56656
   598
apply (simp add: setsum_wcount_Int)
nipkow@26178
   599
done
wenzelm@10249
   600
haftmann@28708
   601
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
blanchet@56656
   602
by (auto simp add: size_multiset_overloaded_def)
blanchet@56656
   603
blanchet@56656
   604
lemma size_multiset_eq_0_iff_empty [iff]: "(size_multiset f M = 0) = (M = {#})"
blanchet@56656
   605
by (auto simp add: size_multiset_eq multiset_eq_iff)
wenzelm@10249
   606
wenzelm@17161
   607
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
blanchet@56656
   608
by (auto simp add: size_multiset_overloaded_def)
nipkow@26016
   609
nipkow@26016
   610
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
nipkow@26178
   611
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
wenzelm@10249
   612
wenzelm@60607
   613
lemma size_eq_Suc_imp_elem: "size M = Suc n \<Longrightarrow> \<exists>a. a \<in># M"
blanchet@56656
   614
apply (unfold size_multiset_overloaded_eq)
nipkow@26178
   615
apply (drule setsum_SucD)
nipkow@26178
   616
apply auto
nipkow@26178
   617
done
wenzelm@10249
   618
haftmann@34943
   619
lemma size_eq_Suc_imp_eq_union:
haftmann@34943
   620
  assumes "size M = Suc n"
haftmann@34943
   621
  shows "\<exists>a N. M = N + {#a#}"
haftmann@34943
   622
proof -
haftmann@34943
   623
  from assms obtain a where "a \<in># M"
haftmann@34943
   624
    by (erule size_eq_Suc_imp_elem [THEN exE])
haftmann@34943
   625
  then have "M = M - {#a#} + {#a#}" by simp
haftmann@34943
   626
  then show ?thesis by blast
nipkow@23611
   627
qed
kleing@15869
   628
wenzelm@60606
   629
lemma size_mset_mono:
wenzelm@60606
   630
  fixes A B :: "'a multiset"
wenzelm@60606
   631
  assumes "A \<le># B"
wenzelm@60606
   632
  shows "size A \<le> size B"
nipkow@59949
   633
proof -
nipkow@59949
   634
  from assms[unfolded mset_le_exists_conv]
nipkow@59949
   635
  obtain C where B: "B = A + C" by auto
wenzelm@60606
   636
  show ?thesis unfolding B by (induct C) auto
nipkow@59949
   637
qed
nipkow@59949
   638
nipkow@59998
   639
lemma size_filter_mset_lesseq[simp]: "size (filter_mset f M) \<le> size M"
nipkow@59949
   640
by (rule size_mset_mono[OF multiset_filter_subset])
nipkow@59949
   641
nipkow@59949
   642
lemma size_Diff_submset:
Mathias@60397
   643
  "M \<le># M' \<Longrightarrow> size (M' - M) = size M' - size(M::'a multiset)"
nipkow@59949
   644
by (metis add_diff_cancel_left' size_union mset_le_exists_conv)
nipkow@26016
   645
wenzelm@60500
   646
subsection \<open>Induction and case splits\<close>
wenzelm@10249
   647
wenzelm@18258
   648
theorem multiset_induct [case_names empty add, induct type: multiset]:
huffman@48009
   649
  assumes empty: "P {#}"
huffman@48009
   650
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
huffman@48009
   651
  shows "P M"
huffman@48009
   652
proof (induct n \<equiv> "size M" arbitrary: M)
huffman@48009
   653
  case 0 thus "P M" by (simp add: empty)
huffman@48009
   654
next
huffman@48009
   655
  case (Suc k)
huffman@48009
   656
  obtain N x where "M = N + {#x#}"
wenzelm@60500
   657
    using \<open>Suc k = size M\<close> [symmetric]
huffman@48009
   658
    using size_eq_Suc_imp_eq_union by fast
huffman@48009
   659
  with Suc add show "P M" by simp
wenzelm@10249
   660
qed
wenzelm@10249
   661
kleing@25610
   662
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
nipkow@26178
   663
by (induct M) auto
kleing@25610
   664
wenzelm@55913
   665
lemma multiset_cases [cases type]:
wenzelm@55913
   666
  obtains (empty) "M = {#}"
wenzelm@55913
   667
    | (add) N x where "M = N + {#x#}"
wenzelm@55913
   668
  using assms by (induct M) simp_all
kleing@25610
   669
haftmann@34943
   670
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
haftmann@34943
   671
by (cases "B = {#}") (auto dest: multi_member_split)
haftmann@34943
   672
wenzelm@60607
   673
lemma multiset_partition: "M = {# x\<in>#M. P x #} + {# x\<in>#M. \<not> P x #}"
nipkow@39302
   674
apply (subst multiset_eq_iff)
nipkow@26178
   675
apply auto
nipkow@26178
   676
done
wenzelm@10249
   677
Mathias@60397
   678
lemma mset_less_size: "(A::'a multiset) <# B \<Longrightarrow> size A < size B"
haftmann@34943
   679
proof (induct A arbitrary: B)
haftmann@34943
   680
  case (empty M)
haftmann@34943
   681
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
blanchet@58425
   682
  then obtain M' x where "M = M' + {#x#}"
haftmann@34943
   683
    by (blast dest: multi_nonempty_split)
haftmann@34943
   684
  then show ?case by simp
haftmann@34943
   685
next
haftmann@34943
   686
  case (add S x T)
Mathias@60397
   687
  have IH: "\<And>B. S <# B \<Longrightarrow> size S < size B" by fact
Mathias@60397
   688
  have SxsubT: "S + {#x#} <# T" by fact
Mathias@60397
   689
  then have "x \<in># T" and "S <# T" by (auto dest: mset_less_insertD)
blanchet@58425
   690
  then obtain T' where T: "T = T' + {#x#}"
haftmann@34943
   691
    by (blast dest: multi_member_split)
Mathias@60397
   692
  then have "S <# T'" using SxsubT
haftmann@34943
   693
    by (blast intro: mset_less_add_bothsides)
haftmann@34943
   694
  then have "size S < size T'" using IH by simp
haftmann@34943
   695
  then show ?case using T by simp
haftmann@34943
   696
qed
haftmann@34943
   697
haftmann@34943
   698
nipkow@59949
   699
lemma size_1_singleton_mset: "size M = 1 \<Longrightarrow> \<exists>a. M = {#a#}"
nipkow@59949
   700
by (cases M) auto
nipkow@59949
   701
wenzelm@60500
   702
subsubsection \<open>Strong induction and subset induction for multisets\<close>
wenzelm@60500
   703
wenzelm@60500
   704
text \<open>Well-foundedness of strict subset relation\<close>
haftmann@58098
   705
Mathias@60397
   706
lemma wf_less_mset_rel: "wf {(M, N :: 'a multiset). M <# N}"
haftmann@34943
   707
apply (rule wf_measure [THEN wf_subset, where f1=size])
haftmann@34943
   708
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
haftmann@34943
   709
done
haftmann@34943
   710
haftmann@34943
   711
lemma full_multiset_induct [case_names less]:
Mathias@60397
   712
assumes ih: "\<And>B. \<forall>(A::'a multiset). A <# B \<longrightarrow> P A \<Longrightarrow> P B"
haftmann@34943
   713
shows "P B"
haftmann@58098
   714
apply (rule wf_less_mset_rel [THEN wf_induct])
haftmann@58098
   715
apply (rule ih, auto)
haftmann@34943
   716
done
haftmann@34943
   717
haftmann@34943
   718
lemma multi_subset_induct [consumes 2, case_names empty add]:
wenzelm@60606
   719
  assumes "F \<le># A"
wenzelm@60606
   720
    and empty: "P {#}"
wenzelm@60606
   721
    and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
wenzelm@60606
   722
  shows "P F"
haftmann@34943
   723
proof -
wenzelm@60500
   724
  from \<open>F \<le># A\<close>
haftmann@34943
   725
  show ?thesis
haftmann@34943
   726
  proof (induct F)
haftmann@34943
   727
    show "P {#}" by fact
haftmann@34943
   728
  next
haftmann@34943
   729
    fix x F
Mathias@60397
   730
    assume P: "F \<le># A \<Longrightarrow> P F" and i: "F + {#x#} \<le># A"
haftmann@34943
   731
    show "P (F + {#x#})"
haftmann@34943
   732
    proof (rule insert)
haftmann@34943
   733
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
Mathias@60397
   734
      from i have "F \<le># A" by (auto dest: mset_le_insertD)
haftmann@34943
   735
      with P show "P F" .
haftmann@34943
   736
    qed
haftmann@34943
   737
  qed
haftmann@34943
   738
qed
wenzelm@26145
   739
wenzelm@17161
   740
wenzelm@60500
   741
subsection \<open>The fold combinator\<close>
huffman@48023
   742
nipkow@59998
   743
definition fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
huffman@48023
   744
where
nipkow@60495
   745
  "fold_mset f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_mset M)"
huffman@48023
   746
wenzelm@60606
   747
lemma fold_mset_empty [simp]: "fold_mset f s {#} = s"
nipkow@59998
   748
  by (simp add: fold_mset_def)
huffman@48023
   749
huffman@48023
   750
context comp_fun_commute
huffman@48023
   751
begin
huffman@48023
   752
wenzelm@60606
   753
lemma fold_mset_insert: "fold_mset f s (M + {#x#}) = f x (fold_mset f s M)"
haftmann@49822
   754
proof -
haftmann@49822
   755
  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
haftmann@49822
   756
    by (fact comp_fun_commute_funpow)
haftmann@49822
   757
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
haftmann@49822
   758
    by (fact comp_fun_commute_funpow)
haftmann@49822
   759
  show ?thesis
nipkow@60495
   760
  proof (cases "x \<in> set_mset M")
haftmann@49822
   761
    case False
haftmann@49822
   762
    then have *: "count (M + {#x#}) x = 1" by simp
nipkow@60495
   763
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_mset M) =
nipkow@60495
   764
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_mset M)"
haftmann@49822
   765
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
   766
    with False * show ?thesis
nipkow@59998
   767
      by (simp add: fold_mset_def del: count_union)
huffman@48023
   768
  next
haftmann@49822
   769
    case True
nipkow@60495
   770
    def N \<equiv> "set_mset M - {x}"
nipkow@60495
   771
    from N_def True have *: "set_mset M = insert x N" "x \<notin> N" "finite N" by auto
haftmann@49822
   772
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
haftmann@49822
   773
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
haftmann@49822
   774
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
nipkow@59998
   775
    with * show ?thesis by (simp add: fold_mset_def del: count_union) simp
huffman@48023
   776
  qed
huffman@48023
   777
qed
huffman@48023
   778
wenzelm@60606
   779
corollary fold_mset_single [simp]: "fold_mset f s {#x#} = f x s"
haftmann@49822
   780
proof -
nipkow@59998
   781
  have "fold_mset f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
haftmann@49822
   782
  then show ?thesis by simp
haftmann@49822
   783
qed
huffman@48023
   784
wenzelm@60606
   785
lemma fold_mset_fun_left_comm: "f x (fold_mset f s M) = fold_mset f (f x s) M"
haftmann@49822
   786
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
huffman@48023
   787
wenzelm@60606
   788
lemma fold_mset_union [simp]: "fold_mset f s (M + N) = fold_mset f (fold_mset f s M) N"
haftmann@49822
   789
proof (induct M)
huffman@48023
   790
  case empty then show ?case by simp
huffman@48023
   791
next
haftmann@49822
   792
  case (add M x)
haftmann@49822
   793
  have "M + {#x#} + N = (M + N) + {#x#}"
haftmann@57514
   794
    by (simp add: ac_simps)
haftmann@51548
   795
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
huffman@48023
   796
qed
huffman@48023
   797
huffman@48023
   798
lemma fold_mset_fusion:
huffman@48023
   799
  assumes "comp_fun_commute g"
wenzelm@60606
   800
    and *: "\<And>x y. h (g x y) = f x (h y)"
wenzelm@60606
   801
  shows "h (fold_mset g w A) = fold_mset f (h w) A"
huffman@48023
   802
proof -
huffman@48023
   803
  interpret comp_fun_commute g by (fact assms)
wenzelm@60606
   804
  from * show ?thesis by (induct A) auto
huffman@48023
   805
qed
huffman@48023
   806
huffman@48023
   807
end
huffman@48023
   808
wenzelm@60500
   809
text \<open>
huffman@48023
   810
  A note on code generation: When defining some function containing a
nipkow@59998
   811
  subterm @{term "fold_mset F"}, code generation is not automatic. When
huffman@48023
   812
  interpreting locale @{text left_commutative} with @{text F}, the
nipkow@59998
   813
  would be code thms for @{const fold_mset} become thms like
nipkow@59998
   814
  @{term "fold_mset F z {#} = z"} where @{text F} is not a pattern but
huffman@48023
   815
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
huffman@48023
   816
  constant with its own code thms needs to be introduced for @{text
huffman@48023
   817
  F}. See the image operator below.
wenzelm@60500
   818
\<close>
wenzelm@60500
   819
wenzelm@60500
   820
wenzelm@60500
   821
subsection \<open>Image\<close>
huffman@48023
   822
huffman@48023
   823
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
wenzelm@60607
   824
  "image_mset f = fold_mset (plus \<circ> single \<circ> f) {#}"
wenzelm@60607
   825
wenzelm@60607
   826
lemma comp_fun_commute_mset_image: "comp_fun_commute (plus \<circ> single \<circ> f)"
haftmann@49823
   827
proof
haftmann@57514
   828
qed (simp add: ac_simps fun_eq_iff)
huffman@48023
   829
huffman@48023
   830
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
haftmann@49823
   831
  by (simp add: image_mset_def)
huffman@48023
   832
huffman@48023
   833
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
haftmann@49823
   834
proof -
wenzelm@60607
   835
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
haftmann@49823
   836
    by (fact comp_fun_commute_mset_image)
haftmann@49823
   837
  show ?thesis by (simp add: image_mset_def)
haftmann@49823
   838
qed
huffman@48023
   839
wenzelm@60606
   840
lemma image_mset_union [simp]: "image_mset f (M + N) = image_mset f M + image_mset f N"
haftmann@49823
   841
proof -
wenzelm@60607
   842
  interpret comp_fun_commute "plus \<circ> single \<circ> f"
haftmann@49823
   843
    by (fact comp_fun_commute_mset_image)
haftmann@57514
   844
  show ?thesis by (induct N) (simp_all add: image_mset_def ac_simps)
haftmann@49823
   845
qed
haftmann@49823
   846
wenzelm@60606
   847
corollary image_mset_insert: "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
haftmann@49823
   848
  by simp
huffman@48023
   849
wenzelm@60606
   850
lemma set_image_mset [simp]: "set_mset (image_mset f M) = image f (set_mset M)"
haftmann@49823
   851
  by (induct M) simp_all
huffman@48040
   852
wenzelm@60606
   853
lemma size_image_mset [simp]: "size (image_mset f M) = size M"
haftmann@49823
   854
  by (induct M) simp_all
huffman@48023
   855
wenzelm@60606
   856
lemma image_mset_is_empty_iff [simp]: "image_mset f M = {#} \<longleftrightarrow> M = {#}"
haftmann@49823
   857
  by (cases M) auto
huffman@48023
   858
huffman@48023
   859
syntax
huffman@48023
   860
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
huffman@48023
   861
      ("({#_/. _ :# _#})")
huffman@48023
   862
translations
wenzelm@60606
   863
  "{#e. x:#M#}" == "CONST image_mset (\<lambda>x. e) M"
huffman@48023
   864
blanchet@59813
   865
syntax (xsymbols)
blanchet@59813
   866
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
blanchet@59813
   867
      ("({#_/. _ \<in># _#})")
blanchet@59813
   868
translations
blanchet@59813
   869
  "{#e. x \<in># M#}" == "CONST image_mset (\<lambda>x. e) M"
blanchet@59813
   870
huffman@48023
   871
syntax
blanchet@59813
   872
  "_comprehension3_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
huffman@48023
   873
      ("({#_/ | _ :# _./ _#})")
huffman@48023
   874
translations
wenzelm@60606
   875
  "{#e | x:#M. P#}" \<rightharpoonup> "{#e. x :# {# x:#M. P#}#}"
huffman@48023
   876
blanchet@59813
   877
syntax
blanchet@59813
   878
  "_comprehension4_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
blanchet@59813
   879
      ("({#_/ | _ \<in># _./ _#})")
blanchet@59813
   880
translations
wenzelm@60606
   881
  "{#e | x\<in>#M. P#}" \<rightharpoonup> "{#e. x \<in># {# x\<in>#M. P#}#}"
blanchet@59813
   882
wenzelm@60500
   883
text \<open>
wenzelm@60607
   884
  This allows to write not just filters like @{term "{#x\<in>#M. x<c#}"}
wenzelm@60607
   885
  but also images like @{term "{#x+x. x\<in>#M #}"} and @{term [source]
wenzelm@60607
   886
  "{#x+x|x\<in>#M. x<c#}"}, where the latter is currently displayed as
wenzelm@60607
   887
  @{term "{#x+x|x\<in>#M. x<c#}"}.
wenzelm@60500
   888
\<close>
huffman@48023
   889
nipkow@60495
   890
lemma in_image_mset: "y \<in># {#f x. x \<in># M#} \<longleftrightarrow> y \<in> f ` set_mset M"
nipkow@60498
   891
by (metis mem_set_mset_iff set_image_mset)
blanchet@59813
   892
blanchet@55467
   893
functor image_mset: image_mset
huffman@48023
   894
proof -
huffman@48023
   895
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
huffman@48023
   896
  proof
huffman@48023
   897
    fix A
huffman@48023
   898
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
huffman@48023
   899
      by (induct A) simp_all
huffman@48023
   900
  qed
huffman@48023
   901
  show "image_mset id = id"
huffman@48023
   902
  proof
huffman@48023
   903
    fix A
huffman@48023
   904
    show "image_mset id A = id A"
huffman@48023
   905
      by (induct A) simp_all
huffman@48023
   906
  qed
huffman@48023
   907
qed
huffman@48023
   908
blanchet@59813
   909
declare
blanchet@59813
   910
  image_mset.id [simp]
blanchet@59813
   911
  image_mset.identity [simp]
blanchet@59813
   912
blanchet@59813
   913
lemma image_mset_id[simp]: "image_mset id x = x"
blanchet@59813
   914
  unfolding id_def by auto
blanchet@59813
   915
blanchet@59813
   916
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
   917
  by (induct M) auto
blanchet@59813
   918
blanchet@59813
   919
lemma image_mset_cong_pair:
blanchet@59813
   920
  "(\<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
   921
  by (metis image_mset_cong split_cong)
haftmann@49717
   922
huffman@48023
   923
wenzelm@60500
   924
subsection \<open>Further conversions\<close>
haftmann@34943
   925
nipkow@60515
   926
primrec mset :: "'a list \<Rightarrow> 'a multiset" where
nipkow@60515
   927
  "mset [] = {#}" |
nipkow@60515
   928
  "mset (a # x) = mset x + {# a #}"
haftmann@34943
   929
haftmann@37107
   930
lemma in_multiset_in_set:
nipkow@60515
   931
  "x \<in># mset xs \<longleftrightarrow> x \<in> set xs"
haftmann@37107
   932
  by (induct xs) simp_all
haftmann@37107
   933
nipkow@60515
   934
lemma count_mset:
nipkow@60515
   935
  "count (mset xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@37107
   936
  by (induct xs) simp_all
haftmann@37107
   937
nipkow@60515
   938
lemma mset_zero_iff[simp]: "(mset x = {#}) = (x = [])"
blanchet@59813
   939
  by (induct x) auto
haftmann@34943
   940
nipkow@60515
   941
lemma mset_zero_iff_right[simp]: "({#} = mset x) = (x = [])"
haftmann@34943
   942
by (induct x) auto
haftmann@34943
   943
nipkow@60515
   944
lemma set_mset_mset[simp]: "set_mset (mset x) = set x"
haftmann@34943
   945
by (induct x) auto
haftmann@34943
   946
wenzelm@60607
   947
lemma mem_set_multiset_eq: "x \<in> set xs = (x \<in># mset xs)"
haftmann@34943
   948
by (induct xs) auto
haftmann@34943
   949
nipkow@60515
   950
lemma size_mset [simp]: "size (mset xs) = length xs"
huffman@48012
   951
  by (induct xs) simp_all
huffman@48012
   952
wenzelm@60606
   953
lemma mset_append [simp]: "mset (xs @ ys) = mset xs + mset ys"
haftmann@57514
   954
  by (induct xs arbitrary: ys) (auto simp: ac_simps)
haftmann@34943
   955
wenzelm@60607
   956
lemma mset_filter: "mset (filter P xs) = {#x \<in># mset xs. P x #}"
haftmann@40303
   957
  by (induct xs) simp_all
haftmann@40303
   958
nipkow@60515
   959
lemma mset_rev [simp]:
nipkow@60515
   960
  "mset (rev xs) = mset xs"
haftmann@40950
   961
  by (induct xs) simp_all
haftmann@40950
   962
nipkow@60515
   963
lemma surj_mset: "surj mset"
haftmann@34943
   964
apply (unfold surj_def)
haftmann@34943
   965
apply (rule allI)
haftmann@34943
   966
apply (rule_tac M = y in multiset_induct)
haftmann@34943
   967
 apply auto
haftmann@34943
   968
apply (rule_tac x = "x # xa" in exI)
haftmann@34943
   969
apply auto
haftmann@34943
   970
done
haftmann@34943
   971
nipkow@60515
   972
lemma set_count_greater_0: "set x = {a. count (mset x) a > 0}"
haftmann@34943
   973
by (induct x) auto
haftmann@34943
   974
haftmann@34943
   975
lemma distinct_count_atmost_1:
wenzelm@60606
   976
  "distinct x = (\<forall>a. count (mset x) a = (if a \<in> set x then 1 else 0))"
wenzelm@60678
   977
  apply (induct x, simp, rule iffI, simp_all)
wenzelm@60678
   978
  subgoal for a b
wenzelm@60678
   979
    apply (rule conjI)
wenzelm@60678
   980
    apply (simp_all add: set_mset_mset [symmetric] del: set_mset_mset)
wenzelm@60678
   981
    apply (erule_tac x = a in allE, simp)
wenzelm@60678
   982
    apply clarify
wenzelm@60678
   983
    apply (erule_tac x = aa in allE, simp)
wenzelm@60678
   984
    done
wenzelm@60678
   985
  done
haftmann@34943
   986
wenzelm@60606
   987
lemma mset_eq_setD: "mset xs = mset ys \<Longrightarrow> set xs = set ys"
nipkow@39302
   988
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
haftmann@34943
   989
nipkow@60515
   990
lemma set_eq_iff_mset_eq_distinct:
haftmann@34943
   991
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
nipkow@60515
   992
    (set x = set y) = (mset x = mset y)"
nipkow@39302
   993
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
haftmann@34943
   994
nipkow@60515
   995
lemma set_eq_iff_mset_remdups_eq:
nipkow@60515
   996
   "(set x = set y) = (mset (remdups x) = mset (remdups y))"
haftmann@34943
   997
apply (rule iffI)
nipkow@60515
   998
apply (simp add: set_eq_iff_mset_eq_distinct[THEN iffD1])
haftmann@34943
   999
apply (drule distinct_remdups [THEN distinct_remdups
nipkow@60515
  1000
      [THEN set_eq_iff_mset_eq_distinct [THEN iffD2]]])
haftmann@34943
  1001
apply simp
haftmann@34943
  1002
done
haftmann@34943
  1003
wenzelm@60606
  1004
lemma mset_compl_union [simp]: "mset [x\<leftarrow>xs. P x] + mset [x\<leftarrow>xs. \<not>P x] = mset xs"
haftmann@57514
  1005
  by (induct xs) (auto simp: ac_simps)
haftmann@34943
  1006
wenzelm@60607
  1007
lemma nth_mem_mset: "i < length ls \<Longrightarrow> (ls ! i) \<in># mset ls"
wenzelm@60678
  1008
proof (induct ls arbitrary: i)
wenzelm@60678
  1009
  case Nil
wenzelm@60678
  1010
  then show ?case by simp
wenzelm@60678
  1011
next
wenzelm@60678
  1012
  case Cons
wenzelm@60678
  1013
  then show ?case by (cases i) auto
wenzelm@60678
  1014
qed
haftmann@34943
  1015
wenzelm@60606
  1016
lemma mset_remove1[simp]: "mset (remove1 a xs) = mset xs - {#a#}"
wenzelm@60678
  1017
  by (induct xs) (auto simp add: multiset_eq_iff)
haftmann@34943
  1018
nipkow@60515
  1019
lemma mset_eq_length:
nipkow@60515
  1020
  assumes "mset xs = mset ys"
haftmann@37107
  1021
  shows "length xs = length ys"
nipkow@60515
  1022
  using assms by (metis size_mset)
nipkow@60515
  1023
nipkow@60515
  1024
lemma mset_eq_length_filter:
nipkow@60515
  1025
  assumes "mset xs = mset ys"
haftmann@39533
  1026
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
nipkow@60515
  1027
  using assms by (metis count_mset)
haftmann@39533
  1028
haftmann@45989
  1029
lemma fold_multiset_equiv:
haftmann@45989
  1030
  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
  1031
    and equiv: "mset xs = mset ys"
haftmann@49822
  1032
  shows "List.fold f xs = List.fold f ys"
wenzelm@60606
  1033
  using f equiv [symmetric]
wenzelm@46921
  1034
proof (induct xs arbitrary: ys)
wenzelm@60678
  1035
  case Nil
wenzelm@60678
  1036
  then show ?case by simp
haftmann@45989
  1037
next
haftmann@45989
  1038
  case (Cons x xs)
wenzelm@60678
  1039
  then have *: "set ys = set (x # xs)"
wenzelm@60678
  1040
    by (blast dest: mset_eq_setD)
blanchet@58425
  1041
  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
  1042
    by (rule Cons.prems(1)) (simp_all add: *)
wenzelm@60678
  1043
  moreover from * have "x \<in> set ys"
wenzelm@60678
  1044
    by simp
wenzelm@60678
  1045
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x"
wenzelm@60678
  1046
    by (fact fold_remove1_split)
wenzelm@60678
  1047
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)"
wenzelm@60678
  1048
    by (auto intro: Cons.hyps)
haftmann@45989
  1049
  ultimately show ?case by simp
haftmann@45989
  1050
qed
haftmann@45989
  1051
wenzelm@60606
  1052
lemma mset_insort [simp]: "mset (insort x xs) = mset xs + {#x#}"
haftmann@51548
  1053
  by (induct xs) (simp_all add: ac_simps)
haftmann@51548
  1054
wenzelm@60606
  1055
lemma mset_map: "mset (map f xs) = image_mset f (mset xs)"
haftmann@51600
  1056
  by (induct xs) simp_all
haftmann@51600
  1057
nipkow@60513
  1058
definition mset_set :: "'a set \<Rightarrow> 'a multiset"
haftmann@51548
  1059
where
nipkow@60513
  1060
  "mset_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
nipkow@60513
  1061
nipkow@60513
  1062
interpretation mset_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
haftmann@51548
  1063
where
nipkow@60513
  1064
  "folding.F (\<lambda>x M. {#x#} + M) {#} = mset_set"
haftmann@51548
  1065
proof -
wenzelm@60678
  1066
  interpret comp_fun_commute "\<lambda>x M. {#x#} + M"
wenzelm@60678
  1067
    by standard (simp add: fun_eq_iff ac_simps)
wenzelm@60678
  1068
  show "folding (\<lambda>x M. {#x#} + M)"
wenzelm@60678
  1069
    by standard (fact comp_fun_commute)
nipkow@60513
  1070
  from mset_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = mset_set" ..
haftmann@51548
  1071
qed
haftmann@51548
  1072
nipkow@60513
  1073
lemma count_mset_set [simp]:
nipkow@60513
  1074
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (mset_set A) x = 1" (is "PROP ?P")
nipkow@60513
  1075
  "\<not> finite A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?Q")
nipkow@60513
  1076
  "x \<notin> A \<Longrightarrow> count (mset_set A) x = 0" (is "PROP ?R")
haftmann@51600
  1077
proof -
wenzelm@60606
  1078
  have *: "count (mset_set A) x = 0" if "x \<notin> A" for A
wenzelm@60606
  1079
  proof (cases "finite A")
wenzelm@60606
  1080
    case False then show ?thesis by simp
wenzelm@60606
  1081
  next
wenzelm@60606
  1082
    case True from True \<open>x \<notin> A\<close> show ?thesis by (induct A) auto
wenzelm@60606
  1083
  qed
haftmann@51600
  1084
  then show "PROP ?P" "PROP ?Q" "PROP ?R"
haftmann@51600
  1085
  by (auto elim!: Set.set_insert)
nipkow@60513
  1086
qed -- \<open>TODO: maybe define @{const mset_set} also in terms of @{const Abs_multiset}\<close>
nipkow@60513
  1087
nipkow@60513
  1088
lemma elem_mset_set[simp, intro]: "finite A \<Longrightarrow> x \<in># mset_set A \<longleftrightarrow> x \<in> A"
blanchet@59813
  1089
  by (induct A rule: finite_induct) simp_all
blanchet@59813
  1090
haftmann@51548
  1091
context linorder
haftmann@51548
  1092
begin
haftmann@51548
  1093
haftmann@51548
  1094
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
haftmann@51548
  1095
where
nipkow@59998
  1096
  "sorted_list_of_multiset M = fold_mset insort [] M"
haftmann@51548
  1097
haftmann@51548
  1098
lemma sorted_list_of_multiset_empty [simp]:
haftmann@51548
  1099
  "sorted_list_of_multiset {#} = []"
haftmann@51548
  1100
  by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1101
haftmann@51548
  1102
lemma sorted_list_of_multiset_singleton [simp]:
haftmann@51548
  1103
  "sorted_list_of_multiset {#x#} = [x]"
haftmann@51548
  1104
proof -
haftmann@51548
  1105
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1106
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1107
qed
haftmann@51548
  1108
haftmann@51548
  1109
lemma sorted_list_of_multiset_insert [simp]:
haftmann@51548
  1110
  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
haftmann@51548
  1111
proof -
haftmann@51548
  1112
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1113
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1114
qed
haftmann@51548
  1115
haftmann@51548
  1116
end
haftmann@51548
  1117
nipkow@60515
  1118
lemma mset_sorted_list_of_multiset [simp]:
nipkow@60515
  1119
  "mset (sorted_list_of_multiset M) = M"
nipkow@60513
  1120
by (induct M) simp_all
haftmann@51548
  1121
nipkow@60515
  1122
lemma sorted_list_of_multiset_mset [simp]:
nipkow@60515
  1123
  "sorted_list_of_multiset (mset xs) = sort xs"
nipkow@60513
  1124
by (induct xs) simp_all
nipkow@60513
  1125
nipkow@60513
  1126
lemma finite_set_mset_mset_set[simp]:
nipkow@60513
  1127
  "finite A \<Longrightarrow> set_mset (mset_set A) = A"
nipkow@60513
  1128
by (induct A rule: finite_induct) simp_all
nipkow@60513
  1129
nipkow@60513
  1130
lemma infinite_set_mset_mset_set:
nipkow@60513
  1131
  "\<not> finite A \<Longrightarrow> set_mset (mset_set A) = {}"
nipkow@60513
  1132
by simp
haftmann@51548
  1133
haftmann@51548
  1134
lemma set_sorted_list_of_multiset [simp]:
nipkow@60495
  1135
  "set (sorted_list_of_multiset M) = set_mset M"
nipkow@60513
  1136
by (induct M) (simp_all add: set_insort)
nipkow@60513
  1137
nipkow@60513
  1138
lemma sorted_list_of_mset_set [simp]:
nipkow@60513
  1139
  "sorted_list_of_multiset (mset_set A) = sorted_list_of_set A"
nipkow@60513
  1140
by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
haftmann@51548
  1141
haftmann@51548
  1142
wenzelm@60500
  1143
subsection \<open>Big operators\<close>
haftmann@51548
  1144
haftmann@51548
  1145
no_notation times (infixl "*" 70)
haftmann@51548
  1146
no_notation Groups.one ("1")
haftmann@51548
  1147
haftmann@51548
  1148
locale comm_monoid_mset = comm_monoid
haftmann@51548
  1149
begin
haftmann@51548
  1150
haftmann@51548
  1151
definition F :: "'a multiset \<Rightarrow> 'a"
wenzelm@60606
  1152
  where eq_fold: "F M = fold_mset f 1 M"
wenzelm@60606
  1153
wenzelm@60606
  1154
lemma empty [simp]: "F {#} = 1"
haftmann@51548
  1155
  by (simp add: eq_fold)
haftmann@51548
  1156
wenzelm@60678
  1157
lemma singleton [simp]: "F {#x#} = x"
haftmann@51548
  1158
proof -
haftmann@51548
  1159
  interpret comp_fun_commute
wenzelm@60678
  1160
    by standard (simp add: fun_eq_iff left_commute)
haftmann@51548
  1161
  show ?thesis by (simp add: eq_fold)
haftmann@51548
  1162
qed
haftmann@51548
  1163
wenzelm@60606
  1164
lemma union [simp]: "F (M + N) = F M * F N"
haftmann@51548
  1165
proof -
haftmann@51548
  1166
  interpret comp_fun_commute f
wenzelm@60678
  1167
    by standard (simp add: fun_eq_iff left_commute)
wenzelm@60678
  1168
  show ?thesis
wenzelm@60678
  1169
    by (induct N) (simp_all add: left_commute eq_fold)
haftmann@51548
  1170
qed
haftmann@51548
  1171
haftmann@51548
  1172
end
haftmann@51548
  1173
blanchet@59813
  1174
lemma comp_fun_commute_plus_mset[simp]: "comp_fun_commute (op + \<Colon> 'a multiset \<Rightarrow> _ \<Rightarrow> _)"
wenzelm@60678
  1175
  by standard (simp add: add_ac comp_def)
blanchet@59813
  1176
blanchet@59813
  1177
declare comp_fun_commute.fold_mset_insert[OF comp_fun_commute_plus_mset, simp]
blanchet@59813
  1178
nipkow@59998
  1179
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
  1180
  by (induct NN) auto
blanchet@59813
  1181
haftmann@51548
  1182
notation times (infixl "*" 70)
haftmann@51548
  1183
notation Groups.one ("1")
haftmann@51548
  1184
haftmann@54868
  1185
context comm_monoid_add
haftmann@54868
  1186
begin
haftmann@54868
  1187
haftmann@54868
  1188
definition msetsum :: "'a multiset \<Rightarrow> 'a"
wenzelm@60606
  1189
  where "msetsum = comm_monoid_mset.F plus 0"
haftmann@51548
  1190
haftmann@54868
  1191
sublocale msetsum!: comm_monoid_mset plus 0
wenzelm@60606
  1192
  where "comm_monoid_mset.F plus 0 = msetsum"
haftmann@51548
  1193
proof -
haftmann@51548
  1194
  show "comm_monoid_mset plus 0" ..
haftmann@51548
  1195
  from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
haftmann@51548
  1196
qed
haftmann@51548
  1197
haftmann@51548
  1198
lemma setsum_unfold_msetsum:
nipkow@60513
  1199
  "setsum f A = msetsum (image_mset f (mset_set A))"
haftmann@51548
  1200
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1201
haftmann@51548
  1202
end
haftmann@51548
  1203
blanchet@59813
  1204
lemma msetsum_diff:
blanchet@59813
  1205
  fixes M N :: "('a \<Colon> ordered_cancel_comm_monoid_diff) multiset"
Mathias@60397
  1206
  shows "N \<le># M \<Longrightarrow> msetsum (M - N) = msetsum M - msetsum N"
Mathias@60397
  1207
  by (metis add_diff_cancel_right' msetsum.union subset_mset.diff_add)
blanchet@59813
  1208
nipkow@59949
  1209
lemma size_eq_msetsum: "size M = msetsum (image_mset (\<lambda>_. 1) M)"
nipkow@59949
  1210
proof (induct M)
nipkow@59949
  1211
  case empty then show ?case by simp
nipkow@59949
  1212
next
nipkow@59949
  1213
  case (add M x) then show ?case
nipkow@60495
  1214
    by (cases "x \<in> set_mset M")
nipkow@60495
  1215
      (simp_all del: mem_set_mset_iff add: size_multiset_overloaded_eq setsum.distrib setsum.delta' insert_absorb, simp)
nipkow@59949
  1216
qed
nipkow@59949
  1217
nipkow@59949
  1218
blanchet@59813
  1219
abbreviation Union_mset :: "'a multiset multiset \<Rightarrow> 'a multiset" where
blanchet@59813
  1220
  "Union_mset MM \<equiv> msetsum MM"
blanchet@59813
  1221
blanchet@59813
  1222
notation (xsymbols) Union_mset ("\<Union>#_" [900] 900)
blanchet@59813
  1223
nipkow@60495
  1224
lemma set_mset_Union_mset[simp]: "set_mset (\<Union># MM) = (\<Union>M \<in> set_mset MM. set_mset M)"
blanchet@59813
  1225
  by (induct MM) auto
blanchet@59813
  1226
blanchet@59813
  1227
lemma in_Union_mset_iff[iff]: "x \<in># \<Union># MM \<longleftrightarrow> (\<exists>M. M \<in># MM \<and> x \<in># M)"
blanchet@59813
  1228
  by (induct MM) auto
blanchet@59813
  1229
haftmann@51548
  1230
syntax
blanchet@58425
  1231
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
haftmann@51548
  1232
      ("(3SUM _:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1233
haftmann@51548
  1234
syntax (xsymbols)
blanchet@58425
  1235
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
haftmann@57518
  1236
      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1237
haftmann@51548
  1238
syntax (HTML output)
blanchet@58425
  1239
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add"
haftmann@51548
  1240
      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1241
haftmann@51548
  1242
translations
haftmann@57518
  1243
  "SUM i :# A. b" == "CONST msetsum (CONST image_mset (\<lambda>i. b) A)"
haftmann@51548
  1244
haftmann@54868
  1245
context comm_monoid_mult
haftmann@54868
  1246
begin
haftmann@54868
  1247
haftmann@54868
  1248
definition msetprod :: "'a multiset \<Rightarrow> 'a"
wenzelm@60606
  1249
  where "msetprod = comm_monoid_mset.F times 1"
haftmann@54868
  1250
haftmann@54868
  1251
sublocale msetprod!: comm_monoid_mset times 1
wenzelm@60606
  1252
  where "comm_monoid_mset.F times 1 = msetprod"
haftmann@51548
  1253
proof -
haftmann@51548
  1254
  show "comm_monoid_mset times 1" ..
wenzelm@60606
  1255
  show "comm_monoid_mset.F times 1 = msetprod" using msetprod_def ..
haftmann@51548
  1256
qed
haftmann@51548
  1257
haftmann@51548
  1258
lemma msetprod_empty:
haftmann@51548
  1259
  "msetprod {#} = 1"
haftmann@51548
  1260
  by (fact msetprod.empty)
haftmann@51548
  1261
haftmann@51548
  1262
lemma msetprod_singleton:
haftmann@51548
  1263
  "msetprod {#x#} = x"
haftmann@51548
  1264
  by (fact msetprod.singleton)
haftmann@51548
  1265
haftmann@51548
  1266
lemma msetprod_Un:
blanchet@58425
  1267
  "msetprod (A + B) = msetprod A * msetprod B"
haftmann@51548
  1268
  by (fact msetprod.union)
haftmann@51548
  1269
haftmann@51548
  1270
lemma setprod_unfold_msetprod:
nipkow@60513
  1271
  "setprod f A = msetprod (image_mset f (mset_set A))"
haftmann@51548
  1272
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1273
haftmann@51548
  1274
lemma msetprod_multiplicity:
nipkow@60495
  1275
  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_mset M)"
nipkow@59998
  1276
  by (simp add: fold_mset_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
haftmann@51548
  1277
haftmann@51548
  1278
end
haftmann@51548
  1279
haftmann@51548
  1280
syntax
blanchet@58425
  1281
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
haftmann@51548
  1282
      ("(3PROD _:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1283
haftmann@51548
  1284
syntax (xsymbols)
blanchet@58425
  1285
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
haftmann@51548
  1286
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1287
haftmann@51548
  1288
syntax (HTML output)
blanchet@58425
  1289
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult"
haftmann@51548
  1290
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1291
haftmann@51548
  1292
translations
haftmann@57518
  1293
  "PROD i :# A. b" == "CONST msetprod (CONST image_mset (\<lambda>i. b) A)"
haftmann@51548
  1294
haftmann@51548
  1295
lemma (in comm_semiring_1) dvd_msetprod:
haftmann@51548
  1296
  assumes "x \<in># A"
haftmann@51548
  1297
  shows "x dvd msetprod A"
haftmann@51548
  1298
proof -
haftmann@51548
  1299
  from assms have "A = (A - {#x#}) + {#x#}" by simp
haftmann@51548
  1300
  then obtain B where "A = B + {#x#}" ..
haftmann@51548
  1301
  then show ?thesis by simp
haftmann@51548
  1302
qed
haftmann@51548
  1303
haftmann@51548
  1304
wenzelm@60500
  1305
subsection \<open>Replicate operation\<close>
blanchet@59813
  1306
blanchet@59813
  1307
definition replicate_mset :: "nat \<Rightarrow> 'a \<Rightarrow> 'a multiset" where
blanchet@59813
  1308
  "replicate_mset n x = ((op + {#x#}) ^^ n) {#}"
blanchet@59813
  1309
blanchet@59813
  1310
lemma replicate_mset_0[simp]: "replicate_mset 0 x = {#}"
blanchet@59813
  1311
  unfolding replicate_mset_def by simp
blanchet@59813
  1312
blanchet@59813
  1313
lemma replicate_mset_Suc[simp]: "replicate_mset (Suc n) x = replicate_mset n x + {#x#}"
blanchet@59813
  1314
  unfolding replicate_mset_def by (induct n) (auto intro: add.commute)
blanchet@59813
  1315
blanchet@59813
  1316
lemma in_replicate_mset[simp]: "x \<in># replicate_mset n y \<longleftrightarrow> n > 0 \<and> x = y"
blanchet@59813
  1317
  unfolding replicate_mset_def by (induct n) simp_all
blanchet@59813
  1318
blanchet@59813
  1319
lemma count_replicate_mset[simp]: "count (replicate_mset n x) y = (if y = x then n else 0)"
blanchet@59813
  1320
  unfolding replicate_mset_def by (induct n) simp_all
blanchet@59813
  1321
nipkow@60495
  1322
lemma set_mset_replicate_mset_subset[simp]: "set_mset (replicate_mset n x) = (if n = 0 then {} else {x})"
blanchet@59813
  1323
  by (auto split: if_splits)
blanchet@59813
  1324
nipkow@59949
  1325
lemma size_replicate_mset[simp]: "size (replicate_mset n M) = n"
blanchet@59813
  1326
  by (induct n, simp_all)
blanchet@59813
  1327
Mathias@60397
  1328
lemma count_le_replicate_mset_le: "n \<le> count M x \<longleftrightarrow> replicate_mset n x \<le># M"
Mathias@60397
  1329
  by (auto simp add: assms mset_less_eqI) (metis count_replicate_mset subseteq_mset_def)
Mathias@60397
  1330
blanchet@59813
  1331
blanchet@59813
  1332
lemma filter_eq_replicate_mset: "{#y \<in># D. y = x#} = replicate_mset (count D x) x"
blanchet@59813
  1333
  by (induct D) simp_all
blanchet@59813
  1334
haftmann@51548
  1335
wenzelm@60500
  1336
subsection \<open>Alternative representations\<close>
wenzelm@60500
  1337
wenzelm@60500
  1338
subsubsection \<open>Lists\<close>
haftmann@51548
  1339
haftmann@39533
  1340
context linorder
haftmann@39533
  1341
begin
haftmann@39533
  1342
nipkow@60515
  1343
lemma mset_insort [simp]:
nipkow@60515
  1344
  "mset (insort_key k x xs) = {#x#} + mset xs"
haftmann@37107
  1345
  by (induct xs) (simp_all add: ac_simps)
blanchet@58425
  1346
nipkow@60515
  1347
lemma mset_sort [simp]:
nipkow@60515
  1348
  "mset (sort_key k xs) = mset xs"
haftmann@37107
  1349
  by (induct xs) (simp_all add: ac_simps)
haftmann@37107
  1350
wenzelm@60500
  1351
text \<open>
haftmann@34943
  1352
  This lemma shows which properties suffice to show that a function
haftmann@34943
  1353
  @{text "f"} with @{text "f xs = ys"} behaves like sort.
wenzelm@60500
  1354
\<close>
haftmann@37074
  1355
haftmann@39533
  1356
lemma properties_for_sort_key:
nipkow@60515
  1357
  assumes "mset ys = mset xs"
wenzelm@60606
  1358
    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
  1359
    and "sorted (map f ys)"
haftmann@39533
  1360
  shows "sort_key f xs = ys"
wenzelm@60606
  1361
  using assms
wenzelm@46921
  1362
proof (induct xs arbitrary: ys)
haftmann@34943
  1363
  case Nil then show ?case by simp
haftmann@34943
  1364
next
haftmann@34943
  1365
  case (Cons x xs)
haftmann@39533
  1366
  from Cons.prems(2) have
haftmann@40305
  1367
    "\<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
  1368
    by (simp add: filter_remove1)
haftmann@39533
  1369
  with Cons.prems have "sort_key f xs = remove1 x ys"
haftmann@39533
  1370
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
haftmann@39533
  1371
  moreover from Cons.prems have "x \<in> set ys"
haftmann@39533
  1372
    by (auto simp add: mem_set_multiset_eq intro!: ccontr)
haftmann@39533
  1373
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
haftmann@34943
  1374
qed
haftmann@34943
  1375
haftmann@39533
  1376
lemma properties_for_sort:
nipkow@60515
  1377
  assumes multiset: "mset ys = mset xs"
wenzelm@60606
  1378
    and "sorted ys"
haftmann@39533
  1379
  shows "sort xs = ys"
haftmann@39533
  1380
proof (rule properties_for_sort_key)
nipkow@60515
  1381
  from multiset show "mset ys = mset xs" .
wenzelm@60500
  1382
  from \<open>sorted ys\<close> show "sorted (map (\<lambda>x. x) ys)" by simp
wenzelm@60678
  1383
  from multiset have "length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)" for k
nipkow@60515
  1384
    by (rule mset_eq_length_filter)
wenzelm@60678
  1385
  then have "replicate (length (filter (\<lambda>y. k = y) ys)) k =
wenzelm@60678
  1386
    replicate (length (filter (\<lambda>x. k = x) xs)) k" for k
haftmann@39533
  1387
    by simp
wenzelm@60678
  1388
  then show "k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs" for k
haftmann@39533
  1389
    by (simp add: replicate_length_filter)
haftmann@39533
  1390
qed
haftmann@39533
  1391
haftmann@40303
  1392
lemma sort_key_by_quicksort:
haftmann@40303
  1393
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
haftmann@40303
  1394
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
haftmann@40303
  1395
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
haftmann@40303
  1396
proof (rule properties_for_sort_key)
nipkow@60515
  1397
  show "mset ?rhs = mset ?lhs"
nipkow@60515
  1398
    by (rule multiset_eqI) (auto simp add: mset_filter)
haftmann@40303
  1399
  show "sorted (map f ?rhs)"
haftmann@40303
  1400
    by (auto simp add: sorted_append intro: sorted_map_same)
haftmann@40303
  1401
next
haftmann@40305
  1402
  fix l
haftmann@40305
  1403
  assume "l \<in> set ?rhs"
haftmann@40346
  1404
  let ?pivot = "f (xs ! (length xs div 2))"
haftmann@40346
  1405
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
haftmann@40306
  1406
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
haftmann@40305
  1407
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
haftmann@40346
  1408
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
haftmann@40346
  1409
  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
  1410
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
haftmann@40346
  1411
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
haftmann@40346
  1412
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
haftmann@40306
  1413
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
haftmann@40305
  1414
  proof (cases "f l" ?pivot rule: linorder_cases)
wenzelm@46730
  1415
    case less
wenzelm@46730
  1416
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
wenzelm@46730
  1417
    with less show ?thesis
haftmann@40346
  1418
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40305
  1419
  next
haftmann@40306
  1420
    case equal then show ?thesis
haftmann@40346
  1421
      by (simp add: * less_le)
haftmann@40305
  1422
  next
wenzelm@46730
  1423
    case greater
wenzelm@46730
  1424
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
wenzelm@46730
  1425
    with greater show ?thesis
haftmann@40346
  1426
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40306
  1427
  qed
haftmann@40303
  1428
qed
haftmann@40303
  1429
haftmann@40303
  1430
lemma sort_by_quicksort:
haftmann@40303
  1431
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
haftmann@40303
  1432
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
haftmann@40303
  1433
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
haftmann@40303
  1434
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
haftmann@40303
  1435
wenzelm@60500
  1436
text \<open>A stable parametrized quicksort\<close>
haftmann@40347
  1437
haftmann@40347
  1438
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
haftmann@40347
  1439
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
haftmann@40347
  1440
haftmann@40347
  1441
lemma part_code [code]:
haftmann@40347
  1442
  "part f pivot [] = ([], [], [])"
haftmann@40347
  1443
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
haftmann@40347
  1444
     if x' < pivot then (x # lts, eqs, gts)
haftmann@40347
  1445
     else if x' > pivot then (lts, eqs, x # gts)
haftmann@40347
  1446
     else (lts, x # eqs, gts))"
haftmann@40347
  1447
  by (auto simp add: part_def Let_def split_def)
haftmann@40347
  1448
haftmann@40347
  1449
lemma sort_key_by_quicksort_code [code]:
wenzelm@60606
  1450
  "sort_key f xs =
wenzelm@60606
  1451
    (case xs of
wenzelm@60606
  1452
      [] \<Rightarrow> []
haftmann@40347
  1453
    | [x] \<Rightarrow> xs
haftmann@40347
  1454
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
wenzelm@60606
  1455
    | _ \<Rightarrow>
wenzelm@60606
  1456
        let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
wenzelm@60606
  1457
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1458
proof (cases xs)
haftmann@40347
  1459
  case Nil then show ?thesis by simp
haftmann@40347
  1460
next
wenzelm@46921
  1461
  case (Cons _ ys) note hyps = Cons show ?thesis
wenzelm@46921
  1462
  proof (cases ys)
haftmann@40347
  1463
    case Nil with hyps show ?thesis by simp
haftmann@40347
  1464
  next
wenzelm@46921
  1465
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
wenzelm@46921
  1466
    proof (cases zs)
haftmann@40347
  1467
      case Nil with hyps show ?thesis by auto
haftmann@40347
  1468
    next
blanchet@58425
  1469
      case Cons
haftmann@40347
  1470
      from sort_key_by_quicksort [of f xs]
haftmann@40347
  1471
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1472
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1473
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
haftmann@40347
  1474
      with hyps Cons show ?thesis by (simp only: list.cases)
haftmann@40347
  1475
    qed
haftmann@40347
  1476
  qed
haftmann@40347
  1477
qed
haftmann@40347
  1478
haftmann@39533
  1479
end
haftmann@39533
  1480
haftmann@40347
  1481
hide_const (open) part
haftmann@40347
  1482
nipkow@60515
  1483
lemma mset_remdups_le: "mset (remdups xs) \<le># mset xs"
Mathias@60397
  1484
  by (induct xs) (auto intro: subset_mset.order_trans)
haftmann@34943
  1485
nipkow@60515
  1486
lemma mset_update:
nipkow@60515
  1487
  "i < length ls \<Longrightarrow> mset (ls[i := v]) = mset ls - {#ls ! i#} + {#v#}"
haftmann@34943
  1488
proof (induct ls arbitrary: i)
haftmann@34943
  1489
  case Nil then show ?case by simp
haftmann@34943
  1490
next
haftmann@34943
  1491
  case (Cons x xs)
haftmann@34943
  1492
  show ?case
haftmann@34943
  1493
  proof (cases i)
haftmann@34943
  1494
    case 0 then show ?thesis by simp
haftmann@34943
  1495
  next
haftmann@34943
  1496
    case (Suc i')
haftmann@34943
  1497
    with Cons show ?thesis
haftmann@34943
  1498
      apply simp
haftmann@57512
  1499
      apply (subst add.assoc)
haftmann@57512
  1500
      apply (subst add.commute [of "{#v#}" "{#x#}"])
haftmann@57512
  1501
      apply (subst add.assoc [symmetric])
haftmann@34943
  1502
      apply simp
haftmann@34943
  1503
      apply (rule mset_le_multiset_union_diff_commute)
nipkow@60515
  1504
      apply (simp add: mset_le_single nth_mem_mset)
haftmann@34943
  1505
      done
haftmann@34943
  1506
  qed
haftmann@34943
  1507
qed
haftmann@34943
  1508
nipkow@60515
  1509
lemma mset_swap:
haftmann@34943
  1510
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
nipkow@60515
  1511
    mset (ls[j := ls ! i, i := ls ! j]) = mset ls"
nipkow@60515
  1512
  by (cases "i = j") (simp_all add: mset_update nth_mem_mset)
haftmann@34943
  1513
haftmann@34943
  1514
wenzelm@60500
  1515
subsection \<open>The multiset order\<close>
wenzelm@60500
  1516
wenzelm@60500
  1517
subsubsection \<open>Well-foundedness\<close>
wenzelm@10249
  1518
wenzelm@60606
  1519
definition mult1 :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1520
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
wenzelm@60607
  1521
      (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r)}"
wenzelm@60606
  1522
wenzelm@60606
  1523
definition mult :: "('a \<times> 'a) set \<Rightarrow> ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1524
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
  1525
berghofe@23751
  1526
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
nipkow@26178
  1527
by (simp add: mult1_def)
wenzelm@10249
  1528
wenzelm@60608
  1529
lemma less_add:
wenzelm@60608
  1530
  assumes mult1: "(N, M0 + {#a#}) \<in> mult1 r"
wenzelm@60608
  1531
  shows
wenzelm@60608
  1532
    "(\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
wenzelm@60608
  1533
     (\<exists>K. (\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@60608
  1534
proof -
wenzelm@60607
  1535
  let ?r = "\<lambda>K a. \<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r"
nipkow@11464
  1536
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
wenzelm@60608
  1537
  obtain a' M0' K where M0: "M0 + {#a#} = M0' + {#a'#}"
wenzelm@60608
  1538
    and N: "N = M0' + K"
wenzelm@60608
  1539
    and r: "?r K a'"
wenzelm@60608
  1540
    using mult1 unfolding mult1_def by auto
wenzelm@60608
  1541
  show ?thesis (is "?case1 \<or> ?case2")
wenzelm@60606
  1542
  proof -
wenzelm@60606
  1543
    from M0 consider "M0 = M0'" "a = a'"
wenzelm@60606
  1544
      | K' where "M0 = K' + {#a'#}" "M0' = K' + {#a#}"
wenzelm@60606
  1545
      by atomize_elim (simp only: add_eq_conv_ex)
wenzelm@18258
  1546
    then show ?thesis
wenzelm@60606
  1547
    proof cases
wenzelm@60606
  1548
      case 1
nipkow@11464
  1549
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@60606
  1550
      then have ?case2 ..
wenzelm@60606
  1551
      then show ?thesis ..
wenzelm@10249
  1552
    next
wenzelm@60606
  1553
      case 2
wenzelm@60606
  1554
      from N 2(2) have n: "N = K' + K + {#a#}" by (simp add: ac_simps)
wenzelm@60606
  1555
      with r 2(1) have "?R (K' + K) M0" by blast
wenzelm@60608
  1556
      with n have ?case1 by (simp add: mult1_def)
wenzelm@60606
  1557
      then show ?thesis ..
wenzelm@10249
  1558
    qed
wenzelm@10249
  1559
  qed
wenzelm@10249
  1560
qed
wenzelm@10249
  1561
wenzelm@60608
  1562
lemma all_accessible:
wenzelm@60608
  1563
  assumes "wf r"
wenzelm@60608
  1564
  shows "\<forall>M. M \<in> Wellfounded.acc (mult1 r)"
wenzelm@10249
  1565
proof
wenzelm@10249
  1566
  let ?R = "mult1 r"
haftmann@54295
  1567
  let ?W = "Wellfounded.acc ?R"
wenzelm@10249
  1568
  {
wenzelm@10249
  1569
    fix M M0 a
berghofe@23751
  1570
    assume M0: "M0 \<in> ?W"
wenzelm@60606
  1571
      and wf_hyp: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
wenzelm@60606
  1572
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W"
berghofe@23751
  1573
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
  1574
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
  1575
      fix N
berghofe@23751
  1576
      assume "(N, M0 + {#a#}) \<in> ?R"
wenzelm@60608
  1577
      then consider M where "(M, M0) \<in> ?R" "N = M + {#a#}"
wenzelm@60608
  1578
        | K where "\<forall>b. b \<in># K \<longrightarrow> (b, a) \<in> r" "N = M0 + K"
wenzelm@60608
  1579
        by atomize_elim (rule less_add)
berghofe@23751
  1580
      then show "N \<in> ?W"
wenzelm@60608
  1581
      proof cases
wenzelm@60608
  1582
        case 1
wenzelm@60606
  1583
        from acc_hyp have "(M, M0) \<in> ?R \<longrightarrow> M + {#a#} \<in> ?W" ..
wenzelm@60500
  1584
        from this and \<open>(M, M0) \<in> ?R\<close> have "M + {#a#} \<in> ?W" ..
wenzelm@60608
  1585
        then show "N \<in> ?W" by (simp only: \<open>N = M + {#a#}\<close>)
wenzelm@10249
  1586
      next
wenzelm@60608
  1587
        case 2
wenzelm@60608
  1588
        from this(1) have "M0 + K \<in> ?W"
wenzelm@10249
  1589
        proof (induct K)
wenzelm@18730
  1590
          case empty
berghofe@23751
  1591
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
  1592
        next
wenzelm@18730
  1593
          case (add K x)
berghofe@23751
  1594
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
  1595
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
  1596
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
  1597
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
haftmann@57512
  1598
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add.assoc)
wenzelm@10249
  1599
        qed
wenzelm@60608
  1600
        then show "N \<in> ?W" by (simp only: 2(2))
wenzelm@10249
  1601
      qed
wenzelm@10249
  1602
    qed
wenzelm@10249
  1603
  } note tedious_reasoning = this
wenzelm@10249
  1604
wenzelm@60608
  1605
  show "M \<in> ?W" for M
wenzelm@10249
  1606
  proof (induct M)
berghofe@23751
  1607
    show "{#} \<in> ?W"
wenzelm@10249
  1608
    proof (rule accI)
berghofe@23751
  1609
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
  1610
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
  1611
    qed
wenzelm@10249
  1612
berghofe@23751
  1613
    fix M a assume "M \<in> ?W"
wenzelm@60608
  1614
    from \<open>wf r\<close> have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1615
    proof induct
wenzelm@10249
  1616
      fix a
wenzelm@60606
  1617
      assume r: "\<And>b. (b, a) \<in> r \<Longrightarrow> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1618
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1619
      proof
berghofe@23751
  1620
        fix M assume "M \<in> ?W"
berghofe@23751
  1621
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
  1622
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
  1623
      qed
wenzelm@10249
  1624
    qed
wenzelm@60500
  1625
    from this and \<open>M \<in> ?W\<close> show "M + {#a#} \<in> ?W" ..
wenzelm@10249
  1626
  qed
wenzelm@10249
  1627
qed
wenzelm@10249
  1628
wenzelm@60606
  1629
theorem wf_mult1: "wf r \<Longrightarrow> wf (mult1 r)"
nipkow@26178
  1630
by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
  1631
wenzelm@60606
  1632
theorem wf_mult: "wf r \<Longrightarrow> wf (mult r)"
nipkow@26178
  1633
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
  1634
wenzelm@10249
  1635
wenzelm@60500
  1636
subsubsection \<open>Closure-free presentation\<close>
wenzelm@60500
  1637
wenzelm@60500
  1638
text \<open>One direction.\<close>
wenzelm@10249
  1639
wenzelm@10249
  1640
lemma mult_implies_one_step:
wenzelm@60606
  1641
  "trans r \<Longrightarrow> (M, N) \<in> mult r \<Longrightarrow>
nipkow@11464
  1642
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
nipkow@60495
  1643
    (\<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r)"
nipkow@60495
  1644
apply (unfold mult_def mult1_def set_mset_def)
nipkow@26178
  1645
apply (erule converse_trancl_induct, clarify)
nipkow@26178
  1646
 apply (rule_tac x = M0 in exI, simp, clarify)
wenzelm@60607
  1647
apply (case_tac "a \<in># K")
nipkow@26178
  1648
 apply (rule_tac x = I in exI)
nipkow@26178
  1649
 apply (simp (no_asm))
nipkow@26178
  1650
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
haftmann@57512
  1651
 apply (simp (no_asm_simp) add: add.assoc [symmetric])
wenzelm@59807
  1652
 apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong)
nipkow@26178
  1653
 apply (simp add: diff_union_single_conv)
nipkow@26178
  1654
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1655
 apply blast
wenzelm@60607
  1656
apply (subgoal_tac "a \<in># I")
nipkow@26178
  1657
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow@26178
  1658
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow@26178
  1659
 apply (rule_tac x = "K + Ka" in exI)
nipkow@26178
  1660
 apply (rule conjI)
nipkow@39302
  1661
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1662
 apply (rule conjI)
wenzelm@59807
  1663
  apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="S + T" for S T in arg_cong, simp)
nipkow@39302
  1664
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1665
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1666
 apply blast
wenzelm@60607
  1667
apply (subgoal_tac "a \<in># (M0 + {#a#})")
nipkow@26178
  1668
 apply simp
nipkow@26178
  1669
apply (simp (no_asm))
nipkow@26178
  1670
done
wenzelm@10249
  1671
wenzelm@10249
  1672
lemma one_step_implies_mult_aux:
wenzelm@60678
  1673
  "\<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
  1674
    \<longrightarrow> (I + K, I + J) \<in> mult r"
wenzelm@60678
  1675
apply (induct n)
wenzelm@60678
  1676
 apply auto
nipkow@26178
  1677
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow@26178
  1678
apply (rename_tac "J'", simp)
nipkow@26178
  1679
apply (erule notE, auto)
nipkow@26178
  1680
apply (case_tac "J' = {#}")
nipkow@26178
  1681
 apply (simp add: mult_def)
nipkow@26178
  1682
 apply (rule r_into_trancl)
nipkow@60495
  1683
 apply (simp add: mult1_def set_mset_def, blast)
wenzelm@60500
  1684
txt \<open>Now we know @{term "J' \<noteq> {#}"}.\<close>
nipkow@26178
  1685
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@60495
  1686
apply (erule_tac P = "\<forall>k \<in> set_mset K. P k" for P in rev_mp)
nipkow@26178
  1687
apply (erule ssubst)
nipkow@26178
  1688
apply (simp add: Ball_def, auto)
nipkow@26178
  1689
apply (subgoal_tac
wenzelm@60607
  1690
  "((I + {# x \<in># K. (x, a) \<in> r #}) + {# x \<in># K. (x, a) \<notin> r #},
wenzelm@60607
  1691
    (I + {# x \<in># K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow@26178
  1692
 prefer 2
nipkow@26178
  1693
 apply force
haftmann@57512
  1694
apply (simp (no_asm_use) add: add.assoc [symmetric] mult_def)
nipkow@26178
  1695
apply (erule trancl_trans)
nipkow@26178
  1696
apply (rule r_into_trancl)
nipkow@60495
  1697
apply (simp add: mult1_def set_mset_def)
nipkow@26178
  1698
apply (rule_tac x = a in exI)
nipkow@26178
  1699
apply (rule_tac x = "I + J'" in exI)
haftmann@57514
  1700
apply (simp add: ac_simps)
nipkow@26178
  1701
done
wenzelm@10249
  1702
wenzelm@17161
  1703
lemma one_step_implies_mult:
wenzelm@60606
  1704
  "trans r \<Longrightarrow> J \<noteq> {#} \<Longrightarrow> \<forall>k \<in> set_mset K. \<exists>j \<in> set_mset J. (k, j) \<in> r
wenzelm@60606
  1705
    \<Longrightarrow> (I + K, I + J) \<in> mult r"
nipkow@26178
  1706
using one_step_implies_mult_aux by blast
wenzelm@10249
  1707
wenzelm@10249
  1708
wenzelm@60500
  1709
subsubsection \<open>Partial-order properties\<close>
wenzelm@10249
  1710
blanchet@59958
  1711
definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "#<#" 50) where
blanchet@59958
  1712
  "M' #<# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
blanchet@59958
  1713
blanchet@59958
  1714
definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "#<=#" 50) where
blanchet@59958
  1715
  "M' #<=# M \<longleftrightarrow> M' #<# M \<or> M' = M"
blanchet@59958
  1716
blanchet@59958
  1717
notation (xsymbols) less_multiset (infix "#\<subset>#" 50)
blanchet@59958
  1718
notation (xsymbols) le_multiset (infix "#\<subseteq>#" 50)
wenzelm@10249
  1719
haftmann@35268
  1720
interpretation multiset_order: order le_multiset less_multiset
haftmann@35268
  1721
proof -
wenzelm@60606
  1722
  have irrefl: "\<not> M #\<subset># M" for M :: "'a multiset"
haftmann@35268
  1723
  proof
blanchet@59958
  1724
    assume "M #\<subset># M"
haftmann@35268
  1725
    then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
haftmann@35268
  1726
    have "trans {(x'::'a, x). x' < x}"
haftmann@35268
  1727
      by (rule transI) simp
haftmann@35268
  1728
    moreover note MM
haftmann@35268
  1729
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
nipkow@60495
  1730
      \<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
  1731
      by (rule mult_implies_one_step)
haftmann@35268
  1732
    then obtain I J K where "M = I + J" and "M = I + K"
nipkow@60495
  1733
      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
  1734
    then have *: "K \<noteq> {#}" and **: "\<forall>k\<in>set_mset K. \<exists>j\<in>set_mset K. k < j" by auto
nipkow@60495
  1735
    have "finite (set_mset K)" by simp
wenzelm@60678
  1736
    moreover note **
nipkow@60495
  1737
    ultimately have "set_mset K = {}"
haftmann@35268
  1738
      by (induct rule: finite_induct) (auto intro: order_less_trans)
wenzelm@60678
  1739
    with * show False by simp
haftmann@35268
  1740
  qed
wenzelm@60678
  1741
  have trans: "K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># N" for K M N :: "'a multiset"
haftmann@35268
  1742
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
wenzelm@46921
  1743
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
wenzelm@60678
  1744
    by standard (auto simp add: le_multiset_def irrefl dest: trans)
haftmann@35268
  1745
qed
wenzelm@10249
  1746
wenzelm@60678
  1747
lemma mult_less_irrefl [elim!]:
wenzelm@60678
  1748
  fixes M :: "'a::order multiset"
wenzelm@60678
  1749
  shows "M #\<subset># M \<Longrightarrow> R"
wenzelm@46730
  1750
  by simp
haftmann@26567
  1751
wenzelm@10249
  1752
wenzelm@60500
  1753
subsubsection \<open>Monotonicity of multiset union\<close>
wenzelm@10249
  1754
wenzelm@60606
  1755
lemma mult1_union: "(B, D) \<in> mult1 r \<Longrightarrow> (C + B, C + D) \<in> mult1 r"
nipkow@26178
  1756
apply (unfold mult1_def)
nipkow@26178
  1757
apply auto
nipkow@26178
  1758
apply (rule_tac x = a in exI)
nipkow@26178
  1759
apply (rule_tac x = "C + M0" in exI)
haftmann@57512
  1760
apply (simp add: add.assoc)
nipkow@26178
  1761
done
wenzelm@10249
  1762
wenzelm@60606
  1763
lemma union_less_mono2: "B #\<subset># D \<Longrightarrow> C + B #\<subset># C + (D::'a::order multiset)"
nipkow@26178
  1764
apply (unfold less_multiset_def mult_def)
nipkow@26178
  1765
apply (erule trancl_induct)
noschinl@40249
  1766
 apply (blast intro: mult1_union)
noschinl@40249
  1767
apply (blast intro: mult1_union trancl_trans)
nipkow@26178
  1768
done
wenzelm@10249
  1769
wenzelm@60606
  1770
lemma union_less_mono1: "B #\<subset># D \<Longrightarrow> B + C #\<subset># D + (C::'a::order multiset)"
haftmann@57512
  1771
apply (subst add.commute [of B C])
haftmann@57512
  1772
apply (subst add.commute [of D C])
nipkow@26178
  1773
apply (erule union_less_mono2)
nipkow@26178
  1774
done
wenzelm@10249
  1775
wenzelm@17161
  1776
lemma union_less_mono:
wenzelm@60606
  1777
  fixes A B C D :: "'a::order multiset"
wenzelm@60606
  1778
  shows "A #\<subset># C \<Longrightarrow> B #\<subset># D \<Longrightarrow> A + B #\<subset># C + D"
haftmann@35268
  1779
  by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
wenzelm@10249
  1780
haftmann@35268
  1781
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
wenzelm@60678
  1782
  by standard (auto simp add: le_multiset_def intro: union_less_mono2)
wenzelm@26145
  1783
paulson@15072
  1784
wenzelm@60500
  1785
subsubsection \<open>Termination proofs with multiset orders\<close>
krauss@29125
  1786
krauss@29125
  1787
lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
krauss@29125
  1788
  and multi_member_this: "x \<in># {# x #} + XS"
krauss@29125
  1789
  and multi_member_last: "x \<in># {# x #}"
krauss@29125
  1790
  by auto
krauss@29125
  1791
krauss@29125
  1792
definition "ms_strict = mult pair_less"
haftmann@37765
  1793
definition "ms_weak = ms_strict \<union> Id"
krauss@29125
  1794
krauss@29125
  1795
lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
krauss@29125
  1796
unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
krauss@29125
  1797
by (auto intro: wf_mult1 wf_trancl simp: mult_def)
krauss@29125
  1798
krauss@29125
  1799
lemma smsI:
nipkow@60495
  1800
  "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
krauss@29125
  1801
  unfolding ms_strict_def
krauss@29125
  1802
by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
krauss@29125
  1803
krauss@29125
  1804
lemma wmsI:
nipkow@60495
  1805
  "(set_mset A, set_mset B) \<in> max_strict \<or> A = {#} \<and> B = {#}
krauss@29125
  1806
  \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
krauss@29125
  1807
unfolding ms_weak_def ms_strict_def
krauss@29125
  1808
by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
krauss@29125
  1809
krauss@29125
  1810
inductive pw_leq
krauss@29125
  1811
where
krauss@29125
  1812
  pw_leq_empty: "pw_leq {#} {#}"
krauss@29125
  1813
| pw_leq_step:  "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
krauss@29125
  1814
krauss@29125
  1815
lemma pw_leq_lstep:
krauss@29125
  1816
  "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
krauss@29125
  1817
by (drule pw_leq_step) (rule pw_leq_empty, simp)
krauss@29125
  1818
krauss@29125
  1819
lemma pw_leq_split:
krauss@29125
  1820
  assumes "pw_leq X Y"
nipkow@60495
  1821
  shows "\<exists>A B Z. X = A + Z \<and> Y = B + Z \<and> ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
krauss@29125
  1822
  using assms
wenzelm@60606
  1823
proof induct
krauss@29125
  1824
  case pw_leq_empty thus ?case by auto
krauss@29125
  1825
next
krauss@29125
  1826
  case (pw_leq_step x y X Y)
krauss@29125
  1827
  then obtain A B Z where
blanchet@58425
  1828
    [simp]: "X = A + Z" "Y = B + Z"
nipkow@60495
  1829
      and 1[simp]: "(set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#})"
krauss@29125
  1830
    by auto
wenzelm@60606
  1831
  from pw_leq_step consider "x = y" | "(x, y) \<in> pair_less"
krauss@29125
  1832
    unfolding pair_leq_def by auto
krauss@29125
  1833
  thus ?case
wenzelm@60606
  1834
  proof cases
wenzelm@60606
  1835
    case [simp]: 1
wenzelm@60606
  1836
    have "{#x#} + X = A + ({#y#}+Z) \<and> {#y#} + Y = B + ({#y#}+Z) \<and>
wenzelm@60606
  1837
      ((set_mset A, set_mset B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
haftmann@57514
  1838
      by (auto simp: ac_simps)
wenzelm@60606
  1839
    thus ?thesis by blast
krauss@29125
  1840
  next
wenzelm@60606
  1841
    case 2
krauss@29125
  1842
    let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
krauss@29125
  1843
    have "{#x#} + X = ?A' + Z"
krauss@29125
  1844
      "{#y#} + Y = ?B' + Z"
haftmann@57514
  1845
      by (auto simp add: ac_simps)
blanchet@58425
  1846
    moreover have
nipkow@60495
  1847
      "(set_mset ?A', set_mset ?B') \<in> max_strict"
wenzelm@60606
  1848
      using 1 2 unfolding max_strict_def
krauss@29125
  1849
      by (auto elim!: max_ext.cases)
krauss@29125
  1850
    ultimately show ?thesis by blast
krauss@29125
  1851
  qed
krauss@29125
  1852
qed
krauss@29125
  1853
blanchet@58425
  1854
lemma
krauss@29125
  1855
  assumes pwleq: "pw_leq Z Z'"
nipkow@60495
  1856
  shows ms_strictI: "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
wenzelm@60606
  1857
    and ms_weakI1:  "(set_mset A, set_mset B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
wenzelm@60606
  1858
    and ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
krauss@29125
  1859
proof -
blanchet@58425
  1860
  from pw_leq_split[OF pwleq]
krauss@29125
  1861
  obtain A' B' Z''
krauss@29125
  1862
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
nipkow@60495
  1863
    and mx_or_empty: "(set_mset A', set_mset B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
krauss@29125
  1864
    by blast
krauss@29125
  1865
  {
nipkow@60495
  1866
    assume max: "(set_mset A, set_mset B) \<in> max_strict"
krauss@29125
  1867
    from mx_or_empty
krauss@29125
  1868
    have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
krauss@29125
  1869
    proof
nipkow@60495
  1870
      assume max': "(set_mset A', set_mset B') \<in> max_strict"
nipkow@60495
  1871
      with max have "(set_mset (A + A'), set_mset (B + B')) \<in> max_strict"
krauss@29125
  1872
        by (auto simp: max_strict_def intro: max_ext_additive)
blanchet@58425
  1873
      thus ?thesis by (rule smsI)
krauss@29125
  1874
    next
krauss@29125
  1875
      assume [simp]: "A' = {#} \<and> B' = {#}"
krauss@29125
  1876
      show ?thesis by (rule smsI) (auto intro: max)
krauss@29125
  1877
    qed
wenzelm@60606
  1878
    thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add: ac_simps)
krauss@29125
  1879
    thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
krauss@29125
  1880
  }
krauss@29125
  1881
  from mx_or_empty
krauss@29125
  1882
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
haftmann@57514
  1883
  thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:ac_simps)
krauss@29125
  1884
qed
krauss@29125
  1885
nipkow@39301
  1886
lemma empty_neutral: "{#} + x = x" "x + {#} = x"
krauss@29125
  1887
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
krauss@29125
  1888
and nonempty_single: "{# x #} \<noteq> {#}"
krauss@29125
  1889
by auto
krauss@29125
  1890
wenzelm@60500
  1891
setup \<open>
wenzelm@60606
  1892
  let
wenzelm@60606
  1893
    fun msetT T = Type (@{type_name multiset}, [T]);
wenzelm@60606
  1894
wenzelm@60606
  1895
    fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
wenzelm@60606
  1896
      | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
wenzelm@60606
  1897
      | mk_mset T (x :: xs) =
wenzelm@60606
  1898
            Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
wenzelm@60606
  1899
                  mk_mset T [x] $ mk_mset T xs
wenzelm@60606
  1900
wenzelm@60606
  1901
    fun mset_member_tac m i =
wenzelm@60606
  1902
      if m <= 0 then
wenzelm@60606
  1903
        rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
wenzelm@60606
  1904
      else
wenzelm@60606
  1905
        rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i
wenzelm@60606
  1906
wenzelm@60606
  1907
    val mset_nonempty_tac =
krauss@29125
  1908
      rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
krauss@29125
  1909
wenzelm@60606
  1910
    fun regroup_munion_conv ctxt =
wenzelm@60606
  1911
      Function_Lib.regroup_conv ctxt @{const_abbrev Mempty} @{const_name plus}
wenzelm@60606
  1912
        (map (fn t => t RS eq_reflection) (@{thms ac_simps} @ @{thms empty_neutral}))
wenzelm@60606
  1913
wenzelm@60606
  1914
    fun unfold_pwleq_tac i =
wenzelm@60606
  1915
      (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
wenzelm@60606
  1916
        ORELSE (rtac @{thm pw_leq_lstep} i)
wenzelm@60606
  1917
        ORELSE (rtac @{thm pw_leq_empty} i)
wenzelm@60606
  1918
wenzelm@60606
  1919
    val set_mset_simps = [@{thm set_mset_empty}, @{thm set_mset_single}, @{thm set_mset_union},
wenzelm@60606
  1920
                        @{thm Un_insert_left}, @{thm Un_empty_left}]
wenzelm@60606
  1921
  in
wenzelm@60606
  1922
    ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset
wenzelm@60606
  1923
    {
wenzelm@60606
  1924
      msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
wenzelm@60606
  1925
      mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
wenzelm@60606
  1926
      mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_mset_simps,
wenzelm@60606
  1927
      smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
wenzelm@60606
  1928
      reduction_pair= @{thm ms_reduction_pair}
wenzelm@60606
  1929
    })
wenzelm@60606
  1930
  end
wenzelm@60500
  1931
\<close>
wenzelm@60500
  1932
wenzelm@60500
  1933
wenzelm@60500
  1934
subsection \<open>Legacy theorem bindings\<close>
haftmann@34943
  1935
nipkow@39302
  1936
lemmas multi_count_eq = multiset_eq_iff [symmetric]
haftmann@34943
  1937
haftmann@34943
  1938
lemma union_commute: "M + N = N + (M::'a multiset)"
haftmann@57512
  1939
  by (fact add.commute)
haftmann@34943
  1940
haftmann@34943
  1941
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
haftmann@57512
  1942
  by (fact add.assoc)
haftmann@34943
  1943
haftmann@34943
  1944
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
haftmann@57512
  1945
  by (fact add.left_commute)
haftmann@34943
  1946
haftmann@34943
  1947
lemmas union_ac = union_assoc union_commute union_lcomm
haftmann@34943
  1948
haftmann@34943
  1949
lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1950
  by (fact add_right_cancel)
haftmann@34943
  1951
haftmann@34943
  1952
lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1953
  by (fact add_left_cancel)
haftmann@34943
  1954
haftmann@34943
  1955
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
haftmann@59557
  1956
  by (fact add_left_imp_eq)
haftmann@34943
  1957
Mathias@60397
  1958
lemma mset_less_trans: "(M::'a multiset) <# K \<Longrightarrow> K <# N \<Longrightarrow> M <# N"
Mathias@60397
  1959
  by (fact subset_mset.less_trans)
haftmann@35268
  1960
haftmann@35268
  1961
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
Mathias@60397
  1962
  by (fact subset_mset.inf.commute)
haftmann@35268
  1963
haftmann@35268
  1964
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
Mathias@60397
  1965
  by (fact subset_mset.inf.assoc [symmetric])
haftmann@35268
  1966
haftmann@35268
  1967
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
Mathias@60397
  1968
  by (fact subset_mset.inf.left_commute)
haftmann@35268
  1969
haftmann@35268
  1970
lemmas multiset_inter_ac =
haftmann@35268
  1971
  multiset_inter_commute
haftmann@35268
  1972
  multiset_inter_assoc
haftmann@35268
  1973
  multiset_inter_left_commute
haftmann@35268
  1974
wenzelm@60606
  1975
lemma mult_less_not_refl: "\<not> M #\<subset># (M::'a::order multiset)"
haftmann@35268
  1976
  by (fact multiset_order.less_irrefl)
haftmann@35268
  1977
wenzelm@60606
  1978
lemma mult_less_trans: "K #\<subset># M \<Longrightarrow> M #\<subset># N \<Longrightarrow> K #\<subset># (N::'a::order multiset)"
haftmann@35268
  1979
  by (fact multiset_order.less_trans)
blanchet@58425
  1980
wenzelm@60606
  1981
lemma mult_less_not_sym: "M #\<subset># N \<Longrightarrow> \<not> N #\<subset># (M::'a::order multiset)"
haftmann@35268
  1982
  by (fact multiset_order.less_not_sym)
haftmann@35268
  1983
wenzelm@60606
  1984
lemma mult_less_asym: "M #\<subset># N \<Longrightarrow> (\<not> P \<Longrightarrow> N #\<subset># (M::'a::order multiset)) \<Longrightarrow> P"
haftmann@35268
  1985
  by (fact multiset_order.less_asym)
haftmann@34943
  1986
wenzelm@60500
  1987
declaration \<open>
wenzelm@60606
  1988
  let
wenzelm@60606
  1989
    fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T])) (Const _ $ t') =
wenzelm@60606
  1990
          let
wenzelm@60606
  1991
            val (maybe_opt, ps) =
wenzelm@60606
  1992
              Nitpick_Model.dest_plain_fun t'
wenzelm@60606
  1993
              ||> op ~~
wenzelm@60606
  1994
              ||> map (apsnd (snd o HOLogic.dest_number))
wenzelm@60606
  1995
            fun elems_for t =
wenzelm@60606
  1996
              (case AList.lookup (op =) ps t of
wenzelm@60606
  1997
                SOME n => replicate n t
wenzelm@60606
  1998
              | NONE => [Const (maybe_name, elem_T --> elem_T) $ t])
wenzelm@60606
  1999
          in
wenzelm@60606
  2000
            (case maps elems_for (all_values elem_T) @
wenzelm@60606
  2001
                 (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)] else []) of
wenzelm@60606
  2002
              [] => Const (@{const_name zero_class.zero}, T)
wenzelm@60606
  2003
            | ts =>
wenzelm@60606
  2004
                foldl1 (fn (t1, t2) =>
wenzelm@60606
  2005
                    Const (@{const_name plus_class.plus}, T --> T --> T) $ t1 $ t2)
wenzelm@60606
  2006
                  (map (curry (op $) (Const (@{const_name single}, elem_T --> T))) ts))
wenzelm@60606
  2007
          end
wenzelm@60606
  2008
      | multiset_postproc _ _ _ _ t = t
wenzelm@60606
  2009
  in Nitpick_Model.register_term_postprocessor @{typ "'a multiset"} multiset_postproc end
wenzelm@60500
  2010
\<close>
wenzelm@60500
  2011
wenzelm@60500
  2012
wenzelm@60500
  2013
subsection \<open>Naive implementation using lists\<close>
haftmann@51600
  2014
nipkow@60515
  2015
code_datatype mset
haftmann@51600
  2016
wenzelm@60606
  2017
lemma [code]: "{#} = mset []"
haftmann@51600
  2018
  by simp
haftmann@51600
  2019
wenzelm@60606
  2020
lemma [code]: "{#x#} = mset [x]"
haftmann@51600
  2021
  by simp
haftmann@51600
  2022
wenzelm@60606
  2023
lemma union_code [code]: "mset xs + mset ys = mset (xs @ ys)"
haftmann@51600
  2024
  by simp
haftmann@51600
  2025
wenzelm@60606
  2026
lemma [code]: "image_mset f (mset xs) = mset (map f xs)"
nipkow@60515
  2027
  by (simp add: mset_map)
haftmann@51600
  2028
wenzelm@60606
  2029
lemma [code]: "filter_mset f (mset xs) = mset (filter f xs)"
nipkow@60515
  2030
  by (simp add: mset_filter)
haftmann@51600
  2031
wenzelm@60606
  2032
lemma [code]: "mset xs - mset ys = mset (fold remove1 ys xs)"
haftmann@51600
  2033
  by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute)
haftmann@51600
  2034
haftmann@51600
  2035
lemma [code]:
nipkow@60515
  2036
  "mset xs #\<inter> mset ys =
nipkow@60515
  2037
    mset (snd (fold (\<lambda>x (ys, zs).
haftmann@51600
  2038
      if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))"
haftmann@51600
  2039
proof -
nipkow@60515
  2040
  have "\<And>zs. mset (snd (fold (\<lambda>x (ys, zs).
haftmann@51600
  2041
    if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
nipkow@60515
  2042
      (mset xs #\<inter> mset ys) + mset zs"
haftmann@51623
  2043
    by (induct xs arbitrary: ys)
haftmann@51623
  2044
      (auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps)
haftmann@51623
  2045
  then show ?thesis by simp
haftmann@51623
  2046
qed
haftmann@51623
  2047
haftmann@51623
  2048
lemma [code]:
nipkow@60515
  2049
  "mset xs #\<union> mset ys =
nipkow@60515
  2050
    mset (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
haftmann@51623
  2051
proof -
nipkow@60515
  2052
  have "\<And>zs. mset (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
nipkow@60515
  2053
      (mset xs #\<union> mset ys) + mset zs"
haftmann@51623
  2054
    by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff)
haftmann@51600
  2055
  then show ?thesis by simp
haftmann@51600
  2056
qed
haftmann@51600
  2057
blanchet@59813
  2058
declare in_multiset_in_set [code_unfold]
haftmann@51600
  2059
wenzelm@60606
  2060
lemma [code]: "count (mset xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0"
haftmann@51600
  2061
proof -
nipkow@60515
  2062
  have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (mset xs) x + n"
haftmann@51600
  2063
    by (induct xs) simp_all
haftmann@51600
  2064
  then show ?thesis by simp
haftmann@51600
  2065
qed
haftmann@51600
  2066
nipkow@60515
  2067
declare set_mset_mset [code]
nipkow@60515
  2068
nipkow@60515
  2069
declare sorted_list_of_multiset_mset [code]
haftmann@51600
  2070
wenzelm@60500
  2071
lemma [code]: -- \<open>not very efficient, but representation-ignorant!\<close>
nipkow@60515
  2072
  "mset_set A = mset (sorted_list_of_set A)"
haftmann@51600
  2073
  apply (cases "finite A")
haftmann@51600
  2074
  apply simp_all
haftmann@51600
  2075
  apply (induct A rule: finite_induct)
blanchet@59813
  2076
  apply (simp_all add: add.commute)
haftmann@51600
  2077
  done
haftmann@51600
  2078
nipkow@60515
  2079
declare size_mset [code]
haftmann@51600
  2080
blanchet@58425
  2081
fun ms_lesseq_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where
nipkow@55808
  2082
  "ms_lesseq_impl [] ys = Some (ys \<noteq> [])"
blanchet@58425
  2083
| "ms_lesseq_impl (Cons x xs) ys = (case List.extract (op = x) ys of
nipkow@55808
  2084
     None \<Rightarrow> None
nipkow@55808
  2085
   | Some (ys1,_,ys2) \<Rightarrow> ms_lesseq_impl xs (ys1 @ ys2))"
nipkow@55808
  2086
nipkow@60515
  2087
lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None \<longleftrightarrow> \<not> mset xs \<le># mset ys) \<and>
nipkow@60515
  2088
  (ms_lesseq_impl xs ys = Some True \<longleftrightarrow> mset xs <# mset ys) \<and>
nipkow@60515
  2089
  (ms_lesseq_impl xs ys = Some False \<longrightarrow> mset xs = mset ys)"
nipkow@55808
  2090
proof (induct xs arbitrary: ys)
nipkow@55808
  2091
  case (Nil ys)
nipkow@55808
  2092
  show ?case by (auto simp: mset_less_empty_nonempty)
nipkow@55808
  2093
next
nipkow@55808
  2094
  case (Cons x xs ys)
nipkow@55808
  2095
  show ?case
nipkow@55808
  2096
  proof (cases "List.extract (op = x) ys")
nipkow@55808
  2097
    case None
nipkow@55808
  2098
    hence x: "x \<notin> set ys" by (simp add: extract_None_iff)
nipkow@55808
  2099
    {
nipkow@60515
  2100
      assume "mset (x # xs) \<le># mset ys"
nipkow@60495
  2101
      from set_mset_mono[OF this] x have False by simp
nipkow@55808
  2102
    } note nle = this
nipkow@55808
  2103
    moreover
nipkow@55808
  2104
    {
nipkow@60515
  2105
      assume "mset (x # xs) <# mset ys"
nipkow@60515
  2106
      hence "mset (x # xs) \<le># mset ys" by auto
nipkow@55808
  2107
      from nle[OF this] have False .
nipkow@55808
  2108
    }
nipkow@55808
  2109
    ultimately show ?thesis using None by auto
nipkow@55808
  2110
  next
nipkow@55808
  2111
    case (Some res)
nipkow@55808
  2112
    obtain ys1 y ys2 where res: "res = (ys1,y,ys2)" by (cases res, auto)
nipkow@55808
  2113
    note Some = Some[unfolded res]
nipkow@55808
  2114
    from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
nipkow@60515
  2115
    hence id: "mset ys = mset (ys1 @ ys2) + {#x#}"
nipkow@55808
  2116
      by (auto simp: ac_simps)
nipkow@55808
  2117
    show ?thesis unfolding ms_lesseq_impl.simps
nipkow@55808
  2118
      unfolding Some option.simps split
nipkow@55808
  2119
      unfolding id
nipkow@55808
  2120
      using Cons[of "ys1 @ ys2"]
Mathias@60397
  2121
      unfolding subset_mset_def subseteq_mset_def by auto
nipkow@55808
  2122
  qed
nipkow@55808
  2123
qed
nipkow@55808
  2124
nipkow@60515
  2125
lemma [code]: "mset xs \<le># mset ys \<longleftrightarrow> ms_lesseq_impl xs ys \<noteq> None"
nipkow@55808
  2126
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
nipkow@55808
  2127
nipkow@60515
  2128
lemma [code]: "mset xs <# mset ys \<longleftrightarrow> ms_lesseq_impl xs ys = Some True"
nipkow@55808
  2129
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
haftmann@51600
  2130
haftmann@51600
  2131
instantiation multiset :: (equal) equal
haftmann@51600
  2132
begin
haftmann@51600
  2133
haftmann@51600
  2134
definition
nipkow@55808
  2135
  [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B"
nipkow@60515
  2136
lemma [code]: "HOL.equal (mset xs) (mset ys) \<longleftrightarrow> ms_lesseq_impl xs ys = Some False"
nipkow@55808
  2137
  unfolding equal_multiset_def
nipkow@55808
  2138
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
haftmann@51600
  2139
haftmann@51600
  2140
instance
wenzelm@60678
  2141
  by standard (simp add: equal_multiset_def)
wenzelm@60678
  2142
blanchet@37169
  2143
end
haftmann@49388
  2144
wenzelm@60606
  2145
lemma [code]: "msetsum (mset xs) = listsum xs"
haftmann@51600
  2146
  by (induct xs) (simp_all add: add.commute)
haftmann@51600
  2147
wenzelm@60606
  2148
lemma [code]: "msetprod (mset xs) = fold times xs 1"
haftmann@51600
  2149
proof -
nipkow@60515
  2150
  have "\<And>x. fold times xs x = msetprod (mset xs) * x"
haftmann@51600
  2151
    by (induct xs) (simp_all add: mult.assoc)
haftmann@51600
  2152
  then show ?thesis by simp
haftmann@51600
  2153
qed
haftmann@51600
  2154
wenzelm@60500
  2155
text \<open>
haftmann@51600
  2156
  Exercise for the casual reader: add implementations for @{const le_multiset}
haftmann@51600
  2157
  and @{const less_multiset} (multiset order).
wenzelm@60500
  2158
\<close>
wenzelm@60500
  2159
wenzelm@60500
  2160
text \<open>Quickcheck generators\<close>
haftmann@51600
  2161
haftmann@51600
  2162
definition (in term_syntax)
haftmann@51600
  2163
  msetify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
haftmann@51600
  2164
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
nipkow@60515
  2165
  [code_unfold]: "msetify xs = Code_Evaluation.valtermify mset {\<cdot>} xs"
haftmann@51600
  2166
haftmann@51600
  2167
notation fcomp (infixl "\<circ>>" 60)
haftmann@51600
  2168
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@51600
  2169
haftmann@51600
  2170
instantiation multiset :: (random) random
haftmann@51600
  2171
begin
haftmann@51600
  2172
haftmann@51600
  2173
definition
haftmann@51600
  2174
  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (msetify xs))"
haftmann@51600
  2175
haftmann@51600
  2176
instance ..
haftmann@51600
  2177
haftmann@51600
  2178
end
haftmann@51600
  2179
haftmann@51600
  2180
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@51600
  2181
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@51600
  2182
haftmann@51600
  2183
instantiation multiset :: (full_exhaustive) full_exhaustive
haftmann@51600
  2184
begin
haftmann@51600
  2185
haftmann@51600
  2186
definition full_exhaustive_multiset :: "('a multiset \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
haftmann@51600
  2187
where
haftmann@51600
  2188
  "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (\<lambda>xs. f (msetify xs)) i"
haftmann@51600
  2189
haftmann@51600
  2190
instance ..
haftmann@51600
  2191
haftmann@51600
  2192
end
haftmann@51600
  2193
haftmann@51600
  2194
hide_const (open) msetify
haftmann@51600
  2195
blanchet@55129
  2196
wenzelm@60500
  2197
subsection \<open>BNF setup\<close>
blanchet@55129
  2198
blanchet@57966
  2199
definition rel_mset where
nipkow@60515
  2200
  "rel_mset R X Y \<longleftrightarrow> (\<exists>xs ys. mset xs = X \<and> mset ys = Y \<and> list_all2 R xs ys)"
nipkow@60515
  2201
nipkow@60515
  2202
lemma mset_zip_take_Cons_drop_twice:
blanchet@57966
  2203
  assumes "length xs = length ys" "j \<le> length xs"
nipkow@60515
  2204
  shows "mset (zip (take j xs @ x # drop j xs) (take j ys @ y # drop j ys)) =
nipkow@60515
  2205
    mset (zip xs ys) + {#(x, y)#}"
wenzelm@60606
  2206
  using assms
blanchet@57966
  2207
proof (induct xs ys arbitrary: x y j rule: list_induct2)
blanchet@57966
  2208
  case Nil
blanchet@57966
  2209
  thus ?case
blanchet@57966
  2210
    by simp
blanchet@55129
  2211
next
blanchet@57966
  2212
  case (Cons x xs y ys)
blanchet@57966
  2213
  thus ?case
blanchet@57966
  2214
  proof (cases "j = 0")
blanchet@57966
  2215
    case True
blanchet@57966
  2216
    thus ?thesis
blanchet@57966
  2217
      by simp
blanchet@55129
  2218
  next
blanchet@57966
  2219
    case False
blanchet@57966
  2220
    then obtain k where k: "j = Suc k"
wenzelm@60678
  2221
      by (cases j) simp
blanchet@57966
  2222
    hence "k \<le> length xs"
blanchet@57966
  2223
      using Cons.prems by auto
nipkow@60515
  2224
    hence "mset (zip (take k xs @ x # drop k xs) (take k ys @ y # drop k ys)) =
nipkow@60515
  2225
      mset (zip xs ys) + {#(x, y)#}"
blanchet@57966
  2226
      by (rule Cons.hyps(2))
blanchet@57966
  2227
    thus ?thesis
blanchet@57966
  2228
      unfolding k by (auto simp: add.commute union_lcomm)
blanchet@58425
  2229
  qed
blanchet@55129
  2230
qed
blanchet@55129
  2231
nipkow@60515
  2232
lemma ex_mset_zip_left:
nipkow@60515
  2233
  assumes "length xs = length ys" "mset xs' = mset xs"
nipkow@60515
  2234
  shows "\<exists>ys'. length ys' = length xs' \<and> mset (zip xs' ys') = mset (zip xs ys)"
blanchet@58425
  2235
using assms
blanchet@57966
  2236
proof (induct xs ys arbitrary: xs' rule: list_induct2)
blanchet@57966
  2237
  case Nil
blanchet@57966
  2238
  thus ?case
blanchet@57966
  2239
    by auto
blanchet@57966
  2240
next
blanchet@57966
  2241
  case (Cons x xs y ys xs')
blanchet@57966
  2242
  obtain j where j_len: "j < length xs'" and nth_j: "xs' ! j = x"
nipkow@60515
  2243
    by (metis Cons.prems in_set_conv_nth list.set_intros(1) mset_eq_setD)
blanchet@58425
  2244
blanchet@58425
  2245
  def xsa \<equiv> "take j xs' @ drop (Suc j) xs'"
nipkow@60515
  2246
  have "mset xs' = {#x#} + mset xsa"
blanchet@57966
  2247
    unfolding xsa_def using j_len nth_j
nipkow@58247
  2248
    by (metis (no_types) ab_semigroup_add_class.add_ac(1) append_take_drop_id Cons_nth_drop_Suc
nipkow@60515
  2249
      mset.simps(2) union_code add.commute)
nipkow@60515
  2250
  hence ms_x: "mset xsa = mset xs"
nipkow@60515
  2251
    by (metis Cons.prems add.commute add_right_imp_eq mset.simps(2))
blanchet@57966
  2252
  then obtain ysa where
nipkow@60515
  2253
    len_a: "length ysa = length xsa" and ms_a: "mset (zip xsa ysa) = mset (zip xs ys)"
blanchet@57966
  2254
    using Cons.hyps(2) by blast
blanchet@57966
  2255
blanchet@57966
  2256
  def ys' \<equiv> "take j ysa @ y # drop j ysa"
blanchet@57966
  2257
  have xs': "xs' = take j xsa @ x # drop j xsa"
blanchet@57966
  2258
    using ms_x j_len nth_j Cons.prems xsa_def
nipkow@58247
  2259
    by (metis append_eq_append_conv append_take_drop_id diff_Suc_Suc Cons_nth_drop_Suc length_Cons
nipkow@60515
  2260
      length_drop size_mset)
blanchet@57966
  2261
  have j_len': "j \<le> length xsa"
blanchet@57966
  2262
    using j_len xs' xsa_def
blanchet@57966
  2263
    by (metis add_Suc_right append_take_drop_id length_Cons length_append less_eq_Suc_le not_less)
blanchet@57966
  2264
  have "length ys' = length xs'"
blanchet@57966
  2265
    unfolding ys'_def using Cons.prems len_a ms_x
nipkow@60515
  2266
    by (metis add_Suc_right append_take_drop_id length_Cons length_append mset_eq_length)
nipkow@60515
  2267
  moreover have "mset (zip xs' ys') = mset (zip (x # xs) (y # ys))"
blanchet@57966
  2268
    unfolding xs' ys'_def
nipkow@60515
  2269
    by (rule trans[OF mset_zip_take_Cons_drop_twice])
blanchet@57966
  2270
      (auto simp: len_a ms_a j_len' add.commute)
blanchet@57966
  2271
  ultimately show ?case
blanchet@57966
  2272
    by blast
blanchet@55129
  2273
qed
blanchet@55129
  2274
blanchet@57966
  2275
lemma list_all2_reorder_left_invariance:
nipkow@60515
  2276
  assumes rel: "list_all2 R xs ys" and ms_x: "mset xs' = mset xs"
nipkow@60515
  2277
  shows "\<exists>ys'. list_all2 R xs' ys' \<and> mset ys' = mset ys"
blanchet@57966
  2278
proof -
blanchet@57966
  2279
  have len: "length xs = length ys"
blanchet@57966
  2280
    using rel list_all2_conv_all_nth by auto
blanchet@57966
  2281
  obtain ys' where
nipkow@60515
  2282
    len': "length xs' = length ys'" and ms_xy: "mset (zip xs' ys') = mset (zip xs ys)"
nipkow@60515
  2283
    using len ms_x by (metis ex_mset_zip_left)
blanchet@57966
  2284
  have "list_all2 R xs' ys'"
nipkow@60515
  2285
    using assms(1) len' ms_xy unfolding list_all2_iff by (blast dest: mset_eq_setD)
nipkow@60515
  2286
  moreover have "mset ys' = mset ys"
nipkow@60515
  2287
    using len len' ms_xy map_snd_zip mset_map by metis
blanchet@57966
  2288
  ultimately show ?thesis
blanchet@57966
  2289
    by blast
blanchet@55129
  2290
qed
blanchet@55129
  2291
nipkow@60515
  2292
lemma ex_mset: "\<exists>xs. mset xs = X"
nipkow@60515
  2293
  by (induct X) (simp, metis mset.simps(2))
blanchet@55129
  2294
blanchet@55129
  2295
bnf "'a multiset"
blanchet@57966
  2296
  map: image_mset
nipkow@60495
  2297
  sets: set_mset
blanchet@55129
  2298
  bd: natLeq
blanchet@55129
  2299
  wits: "{#}"
blanchet@57966
  2300
  rel: rel_mset
blanchet@57966
  2301
proof -
blanchet@57966
  2302
  show "image_mset id = id"
blanchet@57966
  2303
    by (rule image_mset.id)
wenzelm@60606
  2304
  show "image_mset (g \<circ> f) = image_mset g \<circ> image_mset f" for f g
blanchet@59813
  2305
    unfolding comp_def by (rule ext) (simp add: comp_def image_mset.compositionality)
wenzelm@60606
  2306
  show "(\<And>z. z \<in> set_mset X \<Longrightarrow> f z = g z) \<Longrightarrow> image_mset f X = image_mset g X" for f g X
wenzelm@60606
  2307
    by (induct X) (simp_all (no_asm),
nipkow@60495
  2308
      metis One_nat_def Un_iff count_single mem_set_mset_iff set_mset_union zero_less_Suc)
wenzelm@60606
  2309
  show "set_mset \<circ> image_mset f = op ` f \<circ> set_mset" for f
blanchet@57966
  2310
    by auto
blanchet@57966
  2311
  show "card_order natLeq"
blanchet@57966
  2312
    by (rule natLeq_card_order)
blanchet@57966
  2313
  show "BNF_Cardinal_Arithmetic.cinfinite natLeq"
blanchet@57966
  2314
    by (rule natLeq_cinfinite)
wenzelm@60606
  2315
  show "ordLeq3 (card_of (set_mset X)) natLeq" for X
blanchet@57966
  2316
    by transfer
blanchet@57966
  2317
      (auto intro!: ordLess_imp_ordLeq simp: finite_iff_ordLess_natLeq[symmetric] multiset_def)
wenzelm@60606
  2318
  show "rel_mset R OO rel_mset S \<le> rel_mset (R OO S)" for R S
blanchet@57966
  2319
    unfolding rel_mset_def[abs_def] OO_def
blanchet@57966
  2320
    apply clarify
wenzelm@60678
  2321
    subgoal for X Z Y xs ys' ys zs
wenzelm@60678
  2322
      apply (drule list_all2_reorder_left_invariance [where xs = ys' and ys = zs and xs' = ys])
wenzelm@60678
  2323
      apply (auto intro: list_all2_trans)
wenzelm@60678
  2324
      done
wenzelm@60606
  2325
    done
wenzelm@60606
  2326
  show "rel_mset R =
nipkow@60495
  2327
    (BNF_Def.Grp {x. set_mset x \<subseteq> {(x, y). R x y}} (image_mset fst))\<inverse>\<inverse> OO
wenzelm@60606
  2328
    BNF_Def.Grp {x. set_mset x \<subseteq> {(x, y). R x y}} (image_mset snd)" for R
blanchet@57966
  2329
    unfolding rel_mset_def[abs_def] BNF_Def.Grp_def OO_def
blanchet@57966
  2330
    apply (rule ext)+
blanchet@57966
  2331
    apply auto
nipkow@60515
  2332
     apply (rule_tac x = "mset (zip xs ys)" in exI; auto)
nipkow@60515
  2333
        apply (metis list_all2_lengthD map_fst_zip mset_map)
blanchet@57966
  2334
       apply (auto simp: list_all2_iff)[1]
nipkow@60515
  2335
      apply (metis list_all2_lengthD map_snd_zip mset_map)
blanchet@57966
  2336
     apply (auto simp: list_all2_iff)[1]
blanchet@57966
  2337
    apply (rename_tac XY)
nipkow@60515
  2338
    apply (cut_tac X = XY in ex_mset)
blanchet@57966
  2339
    apply (erule exE)
blanchet@57966
  2340
    apply (rename_tac xys)
blanchet@57966
  2341
    apply (rule_tac x = "map fst xys" in exI)
nipkow@60515
  2342
    apply (auto simp: mset_map)
blanchet@57966
  2343
    apply (rule_tac x = "map snd xys" in exI)
nipkow@60515
  2344
    apply (auto simp: mset_map list_all2I subset_eq zip_map_fst_snd)
wenzelm@59997
  2345
    done
wenzelm@60606
  2346
  show "z \<in> set_mset {#} \<Longrightarrow> False" for z
blanchet@57966
  2347
    by auto
blanchet@57966
  2348
qed
blanchet@57966
  2349
wenzelm@60606
  2350
inductive rel_mset'
wenzelm@60606
  2351
where
blanchet@57966
  2352
  Zero[intro]: "rel_mset' R {#} {#}"
blanchet@57966
  2353
| Plus[intro]: "\<lbrakk>R a b; rel_mset' R M N\<rbrakk> \<Longrightarrow> rel_mset' R (M + {#a#}) (N + {#b#})"
blanchet@57966
  2354
blanchet@57966
  2355
lemma rel_mset_Zero: "rel_mset R {#} {#}"
blanchet@57966
  2356
unfolding rel_mset_def Grp_def by auto
blanchet@55129
  2357
blanchet@55129
  2358
declare multiset.count[simp]
blanchet@55129
  2359
declare Abs_multiset_inverse[simp]
blanchet@55129
  2360
declare multiset.count_inverse[simp]
blanchet@55129
  2361
declare union_preserves_multiset[simp]
blanchet@55129
  2362
blanchet@57966
  2363
lemma rel_mset_Plus:
wenzelm@60606
  2364
  assumes ab: "R a b"
wenzelm@60606
  2365
    and MN: "rel_mset R M N"
wenzelm@60606
  2366
  shows "rel_mset R (M + {#a#}) (N + {#b#})"
wenzelm@60606
  2367
proof -
wenzelm@60606
  2368
  have "\<exists>ya. image_mset fst y + {#a#} = image_mset fst ya \<and>
wenzelm@60606
  2369
    image_mset snd y + {#b#} = image_mset snd ya \<and>
wenzelm@60606
  2370
    set_mset ya \<subseteq> {(x, y). R x y}"
wenzelm@60606
  2371
    if "R a b" and "set_mset y \<subseteq> {(x, y). R x y}" for y
wenzelm@60606
  2372
    using that by (intro exI[of _ "y + {#(a,b)#}"]) auto
blanchet@55129
  2373
  thus ?thesis
blanchet@55129
  2374
  using assms
blanchet@57966
  2375
  unfolding multiset.rel_compp_Grp Grp_def by blast
blanchet@55129
  2376
qed
blanchet@55129
  2377
wenzelm@60606
  2378
lemma rel_mset'_imp_rel_mset: "rel_mset' R M N \<Longrightarrow> rel_mset R M N"
wenzelm@60678
  2379
  by (induct rule: rel_mset'.induct) (auto simp: rel_mset_Zero rel_mset_Plus)
blanchet@57966
  2380
wenzelm@60606
  2381
lemma rel_mset_size: "rel_mset R M N \<Longrightarrow> size M = size N"
wenzelm@60678
  2382
  unfolding multiset.rel_compp_Grp Grp_def by auto
blanchet@55129
  2383
blanchet@55129
  2384
lemma multiset_induct2[case_names empty addL addR]:
wenzelm@60678
  2385
  assumes empty: "P {#} {#}"
wenzelm@60678
  2386
    and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
wenzelm@60678
  2387
    and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
wenzelm@60678
  2388
  shows "P M N"
blanchet@55129
  2389
apply(induct N rule: multiset_induct)
blanchet@55129
  2390
  apply(induct M rule: multiset_induct, rule empty, erule addL)
blanchet@55129
  2391
  apply(induct M rule: multiset_induct, erule addR, erule addR)
blanchet@55129
  2392
done
blanchet@55129
  2393
nipkow@59949
  2394
lemma multiset_induct2_size[consumes 1, case_names empty add]:
wenzelm@60606
  2395
  assumes c: "size M = size N"
wenzelm@60606
  2396
    and empty: "P {#} {#}"
wenzelm@60606
  2397
    and add: "\<And>M N a b. P M N \<Longrightarrow> P (M + {#a#}) (N + {#b#})"
wenzelm@60606
  2398
  shows "P M N"
wenzelm@60678
  2399
  using c
wenzelm@60678
  2400
proof (induct M arbitrary: N rule: measure_induct_rule[of size])
wenzelm@60606
  2401
  case (less M)
wenzelm@60606
  2402
  show ?case
blanchet@55129
  2403
  proof(cases "M = {#}")
blanchet@55129
  2404
    case True hence "N = {#}" using less.prems by auto
blanchet@55129
  2405
    thus ?thesis using True empty by auto
blanchet@55129
  2406
  next
blanchet@55129
  2407
    case False then obtain M1 a where M: "M = M1 + {#a#}" by (metis multi_nonempty_split)
blanchet@55129
  2408
    have "N \<noteq> {#}" using False less.prems by auto