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