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