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