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