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