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