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