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