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