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