src/HOL/Library/Multiset.thy
author Thomas Sewell <thomas.sewell@nicta.com.au>
Wed Jun 11 14:24:23 2014 +1000 (2014-06-11)
changeset 57492 74bf65a1910a
parent 57418 6ab1c7cb0b8d
child 57512 cc97b347b301
permissions -rw-r--r--
Hypsubst preserves equality hypotheses

Fixes included for various theories affected by this change.
wenzelm@10249
     1
(*  Title:      HOL/Library/Multiset.thy
paulson@15072
     2
    Author:     Tobias Nipkow, Markus Wenzel, Lawrence C Paulson, Norbert Voelker
blanchet@55129
     3
    Author:     Andrei Popescu, TU Muenchen
wenzelm@10249
     4
*)
wenzelm@10249
     5
haftmann@34943
     6
header {* (Finite) multisets *}
wenzelm@10249
     7
nipkow@15131
     8
theory Multiset
haftmann@51599
     9
imports Main
nipkow@15131
    10
begin
wenzelm@10249
    11
wenzelm@10249
    12
subsection {* The type of multisets *}
wenzelm@10249
    13
wenzelm@45694
    14
definition "multiset = {f :: 'a => nat. finite {x. f x > 0}}"
wenzelm@45694
    15
wenzelm@49834
    16
typedef 'a multiset = "multiset :: ('a => nat) set"
haftmann@34943
    17
  morphisms count Abs_multiset
wenzelm@45694
    18
  unfolding multiset_def
wenzelm@10249
    19
proof
wenzelm@45694
    20
  show "(\<lambda>x. 0::nat) \<in> {f. finite {x. f x > 0}}" by simp
wenzelm@10249
    21
qed
wenzelm@10249
    22
bulwahn@47429
    23
setup_lifting type_definition_multiset
wenzelm@19086
    24
haftmann@28708
    25
abbreviation Melem :: "'a => 'a multiset => bool"  ("(_/ :# _)" [50, 51] 50) where
kleing@25610
    26
  "a :# M == 0 < count M a"
kleing@25610
    27
wenzelm@26145
    28
notation (xsymbols)
wenzelm@26145
    29
  Melem (infix "\<in>#" 50)
wenzelm@10249
    30
nipkow@39302
    31
lemma multiset_eq_iff:
haftmann@34943
    32
  "M = N \<longleftrightarrow> (\<forall>a. count M a = count N a)"
nipkow@39302
    33
  by (simp only: count_inject [symmetric] fun_eq_iff)
haftmann@34943
    34
nipkow@39302
    35
lemma multiset_eqI:
haftmann@34943
    36
  "(\<And>x. count A x = count B x) \<Longrightarrow> A = B"
nipkow@39302
    37
  using multiset_eq_iff by auto
haftmann@34943
    38
haftmann@34943
    39
text {*
haftmann@34943
    40
 \medskip Preservation of the representing set @{term multiset}.
haftmann@34943
    41
*}
haftmann@34943
    42
haftmann@34943
    43
lemma const0_in_multiset:
haftmann@34943
    44
  "(\<lambda>a. 0) \<in> multiset"
haftmann@34943
    45
  by (simp add: multiset_def)
haftmann@34943
    46
haftmann@34943
    47
lemma only1_in_multiset:
haftmann@34943
    48
  "(\<lambda>b. if b = a then n else 0) \<in> multiset"
haftmann@34943
    49
  by (simp add: multiset_def)
haftmann@34943
    50
haftmann@34943
    51
lemma union_preserves_multiset:
haftmann@34943
    52
  "M \<in> multiset \<Longrightarrow> N \<in> multiset \<Longrightarrow> (\<lambda>a. M a + N a) \<in> multiset"
haftmann@34943
    53
  by (simp add: multiset_def)
haftmann@34943
    54
haftmann@34943
    55
lemma diff_preserves_multiset:
haftmann@34943
    56
  assumes "M \<in> multiset"
haftmann@34943
    57
  shows "(\<lambda>a. M a - N a) \<in> multiset"
haftmann@34943
    58
proof -
haftmann@34943
    59
  have "{x. N x < M x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    60
    by auto
haftmann@34943
    61
  with assms show ?thesis
haftmann@34943
    62
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    63
qed
haftmann@34943
    64
haftmann@41069
    65
lemma filter_preserves_multiset:
haftmann@34943
    66
  assumes "M \<in> multiset"
haftmann@34943
    67
  shows "(\<lambda>x. if P x then M x else 0) \<in> multiset"
haftmann@34943
    68
proof -
haftmann@34943
    69
  have "{x. (P x \<longrightarrow> 0 < M x) \<and> P x} \<subseteq> {x. 0 < M x}"
haftmann@34943
    70
    by auto
haftmann@34943
    71
  with assms show ?thesis
haftmann@34943
    72
    by (auto simp add: multiset_def intro: finite_subset)
haftmann@34943
    73
qed
haftmann@34943
    74
haftmann@34943
    75
lemmas in_multiset = const0_in_multiset only1_in_multiset
haftmann@41069
    76
  union_preserves_multiset diff_preserves_multiset filter_preserves_multiset
haftmann@34943
    77
haftmann@34943
    78
haftmann@34943
    79
subsection {* Representing multisets *}
haftmann@34943
    80
haftmann@34943
    81
text {* Multiset enumeration *}
haftmann@34943
    82
huffman@48008
    83
instantiation multiset :: (type) cancel_comm_monoid_add
haftmann@25571
    84
begin
haftmann@25571
    85
bulwahn@47429
    86
lift_definition zero_multiset :: "'a multiset" is "\<lambda>a. 0"
bulwahn@47429
    87
by (rule const0_in_multiset)
haftmann@25571
    88
haftmann@34943
    89
abbreviation Mempty :: "'a multiset" ("{#}") where
haftmann@34943
    90
  "Mempty \<equiv> 0"
haftmann@25571
    91
bulwahn@47429
    92
lift_definition plus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda>M N. (\<lambda>a. M a + N a)"
bulwahn@47429
    93
by (rule union_preserves_multiset)
haftmann@25571
    94
huffman@48008
    95
instance
huffman@48008
    96
by default (transfer, simp add: fun_eq_iff)+
haftmann@25571
    97
haftmann@25571
    98
end
wenzelm@10249
    99
bulwahn@47429
   100
lift_definition single :: "'a => 'a multiset" is "\<lambda>a b. if b = a then 1 else 0"
bulwahn@47429
   101
by (rule only1_in_multiset)
kleing@15869
   102
wenzelm@26145
   103
syntax
wenzelm@26176
   104
  "_multiset" :: "args => 'a multiset"    ("{#(_)#}")
nipkow@25507
   105
translations
nipkow@25507
   106
  "{#x, xs#}" == "{#x#} + {#xs#}"
nipkow@25507
   107
  "{#x#}" == "CONST single x"
nipkow@25507
   108
haftmann@34943
   109
lemma count_empty [simp]: "count {#} a = 0"
bulwahn@47429
   110
  by (simp add: zero_multiset.rep_eq)
wenzelm@10249
   111
haftmann@34943
   112
lemma count_single [simp]: "count {#b#} a = (if b = a then 1 else 0)"
bulwahn@47429
   113
  by (simp add: single.rep_eq)
nipkow@29901
   114
wenzelm@10249
   115
haftmann@34943
   116
subsection {* Basic operations *}
wenzelm@10249
   117
wenzelm@10249
   118
subsubsection {* Union *}
wenzelm@10249
   119
haftmann@34943
   120
lemma count_union [simp]: "count (M + N) a = count M a + count N a"
bulwahn@47429
   121
  by (simp add: plus_multiset.rep_eq)
wenzelm@10249
   122
wenzelm@10249
   123
wenzelm@10249
   124
subsubsection {* Difference *}
wenzelm@10249
   125
haftmann@49388
   126
instantiation multiset :: (type) comm_monoid_diff
haftmann@34943
   127
begin
haftmann@34943
   128
bulwahn@47429
   129
lift_definition minus_multiset :: "'a multiset => 'a multiset => 'a multiset" is "\<lambda> M N. \<lambda>a. M a - N a"
bulwahn@47429
   130
by (rule diff_preserves_multiset)
bulwahn@47429
   131
 
haftmann@49388
   132
instance
haftmann@49388
   133
by default (transfer, simp add: fun_eq_iff)+
haftmann@34943
   134
haftmann@34943
   135
end
haftmann@34943
   136
haftmann@34943
   137
lemma count_diff [simp]: "count (M - N) a = count M a - count N a"
bulwahn@47429
   138
  by (simp add: minus_multiset.rep_eq)
haftmann@34943
   139
wenzelm@17161
   140
lemma diff_empty [simp]: "M - {#} = M \<and> {#} - M = {#}"
haftmann@52289
   141
  by rule (fact Groups.diff_zero, fact Groups.zero_diff)
nipkow@36903
   142
nipkow@36903
   143
lemma diff_cancel[simp]: "A - A = {#}"
haftmann@52289
   144
  by (fact Groups.diff_cancel)
wenzelm@10249
   145
nipkow@36903
   146
lemma diff_union_cancelR [simp]: "M + N - N = (M::'a multiset)"
haftmann@52289
   147
  by (fact add_diff_cancel_right')
wenzelm@10249
   148
nipkow@36903
   149
lemma diff_union_cancelL [simp]: "N + M - N = (M::'a multiset)"
haftmann@52289
   150
  by (fact add_diff_cancel_left')
haftmann@34943
   151
haftmann@52289
   152
lemma diff_right_commute:
haftmann@52289
   153
  "(M::'a multiset) - N - Q = M - Q - N"
haftmann@52289
   154
  by (fact diff_right_commute)
haftmann@52289
   155
haftmann@52289
   156
lemma diff_add:
haftmann@52289
   157
  "(M::'a multiset) - (N + Q) = M - N - Q"
haftmann@52289
   158
  by (rule sym) (fact diff_diff_add)
haftmann@52289
   159
  
haftmann@34943
   160
lemma insert_DiffM:
haftmann@34943
   161
  "x \<in># M \<Longrightarrow> {#x#} + (M - {#x#}) = M"
nipkow@39302
   162
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   163
haftmann@34943
   164
lemma insert_DiffM2 [simp]:
haftmann@34943
   165
  "x \<in># M \<Longrightarrow> M - {#x#} + {#x#} = M"
nipkow@39302
   166
  by (clarsimp simp: multiset_eq_iff)
haftmann@34943
   167
haftmann@34943
   168
lemma diff_union_swap:
haftmann@34943
   169
  "a \<noteq> b \<Longrightarrow> M - {#a#} + {#b#} = M + {#b#} - {#a#}"
nipkow@39302
   170
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   171
haftmann@34943
   172
lemma diff_union_single_conv:
haftmann@34943
   173
  "a \<in># J \<Longrightarrow> I + J - {#a#} = I + (J - {#a#})"
nipkow@39302
   174
  by (simp add: multiset_eq_iff)
bulwahn@26143
   175
wenzelm@10249
   176
haftmann@34943
   177
subsubsection {* Equality of multisets *}
haftmann@34943
   178
haftmann@34943
   179
lemma single_not_empty [simp]: "{#a#} \<noteq> {#} \<and> {#} \<noteq> {#a#}"
nipkow@39302
   180
  by (simp add: multiset_eq_iff)
haftmann@34943
   181
haftmann@34943
   182
lemma single_eq_single [simp]: "{#a#} = {#b#} \<longleftrightarrow> a = b"
nipkow@39302
   183
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   184
haftmann@34943
   185
lemma union_eq_empty [iff]: "M + N = {#} \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   186
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   187
haftmann@34943
   188
lemma empty_eq_union [iff]: "{#} = M + N \<longleftrightarrow> M = {#} \<and> N = {#}"
nipkow@39302
   189
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   190
haftmann@34943
   191
lemma multi_self_add_other_not_self [simp]: "M = M + {#x#} \<longleftrightarrow> False"
nipkow@39302
   192
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   193
haftmann@34943
   194
lemma diff_single_trivial:
haftmann@34943
   195
  "\<not> x \<in># M \<Longrightarrow> M - {#x#} = M"
nipkow@39302
   196
  by (auto simp add: multiset_eq_iff)
haftmann@34943
   197
haftmann@34943
   198
lemma diff_single_eq_union:
haftmann@34943
   199
  "x \<in># M \<Longrightarrow> M - {#x#} = N \<longleftrightarrow> M = N + {#x#}"
haftmann@34943
   200
  by auto
haftmann@34943
   201
haftmann@34943
   202
lemma union_single_eq_diff:
haftmann@34943
   203
  "M + {#x#} = N \<Longrightarrow> M = N - {#x#}"
haftmann@34943
   204
  by (auto dest: sym)
haftmann@34943
   205
haftmann@34943
   206
lemma union_single_eq_member:
haftmann@34943
   207
  "M + {#x#} = N \<Longrightarrow> x \<in># N"
haftmann@34943
   208
  by auto
haftmann@34943
   209
haftmann@34943
   210
lemma union_is_single:
wenzelm@46730
   211
  "M + N = {#a#} \<longleftrightarrow> M = {#a#} \<and> N={#} \<or> M = {#} \<and> N = {#a#}" (is "?lhs = ?rhs")
wenzelm@46730
   212
proof
haftmann@34943
   213
  assume ?rhs then show ?lhs by auto
haftmann@34943
   214
next
wenzelm@46730
   215
  assume ?lhs then show ?rhs
wenzelm@46730
   216
    by (simp add: multiset_eq_iff split:if_splits) (metis add_is_1)
haftmann@34943
   217
qed
haftmann@34943
   218
haftmann@34943
   219
lemma single_is_union:
haftmann@34943
   220
  "{#a#} = M + N \<longleftrightarrow> {#a#} = M \<and> N = {#} \<or> M = {#} \<and> {#a#} = N"
haftmann@34943
   221
  by (auto simp add: eq_commute [of "{#a#}" "M + N"] union_is_single)
haftmann@34943
   222
haftmann@34943
   223
lemma add_eq_conv_diff:
haftmann@34943
   224
  "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
   225
(* shorter: by (simp add: multiset_eq_iff) fastforce *)
haftmann@34943
   226
proof
haftmann@34943
   227
  assume ?rhs then show ?lhs
haftmann@34943
   228
  by (auto simp add: add_assoc add_commute [of "{#b#}"])
haftmann@34943
   229
    (drule sym, simp add: add_assoc [symmetric])
haftmann@34943
   230
next
haftmann@34943
   231
  assume ?lhs
haftmann@34943
   232
  show ?rhs
haftmann@34943
   233
  proof (cases "a = b")
haftmann@34943
   234
    case True with `?lhs` show ?thesis by simp
haftmann@34943
   235
  next
haftmann@34943
   236
    case False
haftmann@34943
   237
    from `?lhs` have "a \<in># N + {#b#}" by (rule union_single_eq_member)
haftmann@34943
   238
    with False have "a \<in># N" by auto
haftmann@34943
   239
    moreover from `?lhs` have "M = N + {#b#} - {#a#}" by (rule union_single_eq_diff)
haftmann@34943
   240
    moreover note False
haftmann@34943
   241
    ultimately show ?thesis by (auto simp add: diff_right_commute [of _ "{#a#}"] diff_union_swap)
haftmann@34943
   242
  qed
haftmann@34943
   243
qed
haftmann@34943
   244
haftmann@34943
   245
lemma insert_noteq_member: 
haftmann@34943
   246
  assumes BC: "B + {#b#} = C + {#c#}"
haftmann@34943
   247
   and bnotc: "b \<noteq> c"
haftmann@34943
   248
  shows "c \<in># B"
haftmann@34943
   249
proof -
haftmann@34943
   250
  have "c \<in># C + {#c#}" by simp
haftmann@34943
   251
  have nc: "\<not> c \<in># {#b#}" using bnotc by simp
haftmann@34943
   252
  then have "c \<in># B + {#b#}" using BC by simp
haftmann@34943
   253
  then show "c \<in># B" using nc by simp
haftmann@34943
   254
qed
haftmann@34943
   255
haftmann@34943
   256
lemma add_eq_conv_ex:
haftmann@34943
   257
  "(M + {#a#} = N + {#b#}) =
haftmann@34943
   258
    (M = N \<and> a = b \<or> (\<exists>K. M = K + {#b#} \<and> N = K + {#a#}))"
haftmann@34943
   259
  by (auto simp add: add_eq_conv_diff)
haftmann@34943
   260
haftmann@51600
   261
lemma multi_member_split:
haftmann@51600
   262
  "x \<in># M \<Longrightarrow> \<exists>A. M = A + {#x#}"
haftmann@51600
   263
  by (rule_tac x = "M - {#x#}" in exI, simp)
haftmann@51600
   264
haftmann@34943
   265
haftmann@34943
   266
subsubsection {* Pointwise ordering induced by count *}
haftmann@34943
   267
haftmann@35268
   268
instantiation multiset :: (type) ordered_ab_semigroup_add_imp_le
haftmann@35268
   269
begin
haftmann@35268
   270
kuncar@55565
   271
lift_definition less_eq_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" is "\<lambda> A B. (\<forall>a. A a \<le> B a)" .
kuncar@55565
   272
bulwahn@47429
   273
lemmas mset_le_def = less_eq_multiset_def
haftmann@34943
   274
haftmann@35268
   275
definition less_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" where
haftmann@35268
   276
  mset_less_def: "(A::'a multiset) < B \<longleftrightarrow> A \<le> B \<and> A \<noteq> B"
haftmann@34943
   277
wenzelm@46921
   278
instance
wenzelm@46921
   279
  by default (auto simp add: mset_le_def mset_less_def multiset_eq_iff intro: order_trans antisym)
haftmann@35268
   280
haftmann@35268
   281
end
haftmann@34943
   282
haftmann@34943
   283
lemma mset_less_eqI:
haftmann@35268
   284
  "(\<And>x. count A x \<le> count B x) \<Longrightarrow> A \<le> B"
haftmann@34943
   285
  by (simp add: mset_le_def)
haftmann@34943
   286
haftmann@35268
   287
lemma mset_le_exists_conv:
haftmann@35268
   288
  "(A::'a multiset) \<le> B \<longleftrightarrow> (\<exists>C. B = A + C)"
haftmann@34943
   289
apply (unfold mset_le_def, rule iffI, rule_tac x = "B - A" in exI)
nipkow@39302
   290
apply (auto intro: multiset_eq_iff [THEN iffD2])
haftmann@34943
   291
done
haftmann@34943
   292
haftmann@52289
   293
instance multiset :: (type) ordered_cancel_comm_monoid_diff
haftmann@52289
   294
  by default (simp, fact mset_le_exists_conv)
haftmann@52289
   295
haftmann@35268
   296
lemma mset_le_mono_add_right_cancel [simp]:
haftmann@35268
   297
  "(A::'a multiset) + C \<le> B + C \<longleftrightarrow> A \<le> B"
haftmann@35268
   298
  by (fact add_le_cancel_right)
haftmann@34943
   299
haftmann@35268
   300
lemma mset_le_mono_add_left_cancel [simp]:
haftmann@35268
   301
  "C + (A::'a multiset) \<le> C + B \<longleftrightarrow> A \<le> B"
haftmann@35268
   302
  by (fact add_le_cancel_left)
haftmann@35268
   303
haftmann@35268
   304
lemma mset_le_mono_add:
haftmann@35268
   305
  "(A::'a multiset) \<le> B \<Longrightarrow> C \<le> D \<Longrightarrow> A + C \<le> B + D"
haftmann@35268
   306
  by (fact add_mono)
haftmann@34943
   307
haftmann@35268
   308
lemma mset_le_add_left [simp]:
haftmann@35268
   309
  "(A::'a multiset) \<le> A + B"
haftmann@35268
   310
  unfolding mset_le_def by auto
haftmann@35268
   311
haftmann@35268
   312
lemma mset_le_add_right [simp]:
haftmann@35268
   313
  "B \<le> (A::'a multiset) + B"
haftmann@35268
   314
  unfolding mset_le_def by auto
haftmann@34943
   315
haftmann@35268
   316
lemma mset_le_single:
haftmann@35268
   317
  "a :# B \<Longrightarrow> {#a#} \<le> B"
haftmann@35268
   318
  by (simp add: mset_le_def)
haftmann@34943
   319
haftmann@35268
   320
lemma multiset_diff_union_assoc:
haftmann@35268
   321
  "C \<le> B \<Longrightarrow> (A::'a multiset) + B - C = A + (B - C)"
nipkow@39302
   322
  by (simp add: multiset_eq_iff mset_le_def)
haftmann@34943
   323
haftmann@34943
   324
lemma mset_le_multiset_union_diff_commute:
nipkow@36867
   325
  "B \<le> A \<Longrightarrow> (A::'a multiset) - B + C = A + C - B"
nipkow@39302
   326
by (simp add: multiset_eq_iff mset_le_def)
haftmann@34943
   327
nipkow@39301
   328
lemma diff_le_self[simp]: "(M::'a multiset) - N \<le> M"
nipkow@39301
   329
by(simp add: mset_le_def)
nipkow@39301
   330
haftmann@35268
   331
lemma mset_lessD: "A < B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
haftmann@34943
   332
apply (clarsimp simp: mset_le_def mset_less_def)
haftmann@34943
   333
apply (erule_tac x=x in allE)
haftmann@34943
   334
apply auto
haftmann@34943
   335
done
haftmann@34943
   336
haftmann@35268
   337
lemma mset_leD: "A \<le> B \<Longrightarrow> x \<in># A \<Longrightarrow> x \<in># B"
haftmann@34943
   338
apply (clarsimp simp: mset_le_def mset_less_def)
haftmann@34943
   339
apply (erule_tac x = x in allE)
haftmann@34943
   340
apply auto
haftmann@34943
   341
done
haftmann@34943
   342
  
haftmann@35268
   343
lemma mset_less_insertD: "(A + {#x#} < B) \<Longrightarrow> (x \<in># B \<and> A < B)"
haftmann@34943
   344
apply (rule conjI)
haftmann@34943
   345
 apply (simp add: mset_lessD)
haftmann@34943
   346
apply (clarsimp simp: mset_le_def mset_less_def)
haftmann@34943
   347
apply safe
haftmann@34943
   348
 apply (erule_tac x = a in allE)
haftmann@34943
   349
 apply (auto split: split_if_asm)
haftmann@34943
   350
done
haftmann@34943
   351
haftmann@35268
   352
lemma mset_le_insertD: "(A + {#x#} \<le> B) \<Longrightarrow> (x \<in># B \<and> A \<le> B)"
haftmann@34943
   353
apply (rule conjI)
haftmann@34943
   354
 apply (simp add: mset_leD)
haftmann@34943
   355
apply (force simp: mset_le_def mset_less_def split: split_if_asm)
haftmann@34943
   356
done
haftmann@34943
   357
haftmann@35268
   358
lemma mset_less_of_empty[simp]: "A < {#} \<longleftrightarrow> False"
nipkow@39302
   359
  by (auto simp add: mset_less_def mset_le_def multiset_eq_iff)
haftmann@34943
   360
nipkow@55808
   361
lemma empty_le[simp]: "{#} \<le> A"
nipkow@55808
   362
  unfolding mset_le_exists_conv by auto
nipkow@55808
   363
nipkow@55808
   364
lemma le_empty[simp]: "(M \<le> {#}) = (M = {#})"
nipkow@55808
   365
  unfolding mset_le_exists_conv by auto
nipkow@55808
   366
haftmann@35268
   367
lemma multi_psub_of_add_self[simp]: "A < A + {#x#}"
haftmann@35268
   368
  by (auto simp: mset_le_def mset_less_def)
haftmann@34943
   369
haftmann@35268
   370
lemma multi_psub_self[simp]: "(A::'a multiset) < A = False"
haftmann@35268
   371
  by simp
haftmann@34943
   372
haftmann@34943
   373
lemma mset_less_add_bothsides:
haftmann@35268
   374
  "T + {#x#} < S + {#x#} \<Longrightarrow> T < S"
haftmann@35268
   375
  by (fact add_less_imp_less_right)
haftmann@35268
   376
haftmann@35268
   377
lemma mset_less_empty_nonempty:
haftmann@35268
   378
  "{#} < S \<longleftrightarrow> S \<noteq> {#}"
haftmann@35268
   379
  by (auto simp: mset_le_def mset_less_def)
haftmann@35268
   380
haftmann@35268
   381
lemma mset_less_diff_self:
haftmann@35268
   382
  "c \<in># B \<Longrightarrow> B - {#c#} < B"
nipkow@39302
   383
  by (auto simp: mset_le_def mset_less_def multiset_eq_iff)
haftmann@35268
   384
haftmann@35268
   385
haftmann@35268
   386
subsubsection {* Intersection *}
haftmann@35268
   387
haftmann@35268
   388
instantiation multiset :: (type) semilattice_inf
haftmann@35268
   389
begin
haftmann@35268
   390
haftmann@35268
   391
definition inf_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
haftmann@35268
   392
  multiset_inter_def: "inf_multiset A B = A - (A - B)"
haftmann@35268
   393
wenzelm@46921
   394
instance
wenzelm@46921
   395
proof -
haftmann@35268
   396
  have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> m \<le> q \<Longrightarrow> m \<le> n - (n - q)" by arith
wenzelm@46921
   397
  show "OFCLASS('a multiset, semilattice_inf_class)"
wenzelm@46921
   398
    by default (auto simp add: multiset_inter_def mset_le_def aux)
haftmann@35268
   399
qed
haftmann@35268
   400
haftmann@35268
   401
end
haftmann@35268
   402
haftmann@35268
   403
abbreviation multiset_inter :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<inter>" 70) where
haftmann@35268
   404
  "multiset_inter \<equiv> inf"
haftmann@34943
   405
haftmann@41069
   406
lemma multiset_inter_count [simp]:
haftmann@35268
   407
  "count (A #\<inter> B) x = min (count A x) (count B x)"
bulwahn@47429
   408
  by (simp add: multiset_inter_def)
haftmann@35268
   409
haftmann@35268
   410
lemma multiset_inter_single: "a \<noteq> b \<Longrightarrow> {#a#} #\<inter> {#b#} = {#}"
wenzelm@46730
   411
  by (rule multiset_eqI) auto
haftmann@34943
   412
haftmann@35268
   413
lemma multiset_union_diff_commute:
haftmann@35268
   414
  assumes "B #\<inter> C = {#}"
haftmann@35268
   415
  shows "A + B - C = A - C + B"
nipkow@39302
   416
proof (rule multiset_eqI)
haftmann@35268
   417
  fix x
haftmann@35268
   418
  from assms have "min (count B x) (count C x) = 0"
wenzelm@46730
   419
    by (auto simp add: multiset_eq_iff)
haftmann@35268
   420
  then have "count B x = 0 \<or> count C x = 0"
haftmann@35268
   421
    by auto
haftmann@35268
   422
  then show "count (A + B - C) x = count (A - C + B) x"
haftmann@35268
   423
    by auto
haftmann@35268
   424
qed
haftmann@35268
   425
haftmann@51600
   426
lemma empty_inter [simp]:
haftmann@51600
   427
  "{#} #\<inter> M = {#}"
haftmann@51600
   428
  by (simp add: multiset_eq_iff)
haftmann@51600
   429
haftmann@51600
   430
lemma inter_empty [simp]:
haftmann@51600
   431
  "M #\<inter> {#} = {#}"
haftmann@51600
   432
  by (simp add: multiset_eq_iff)
haftmann@51600
   433
haftmann@51600
   434
lemma inter_add_left1:
haftmann@51600
   435
  "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = M #\<inter> N"
haftmann@51600
   436
  by (simp add: multiset_eq_iff)
haftmann@51600
   437
haftmann@51600
   438
lemma inter_add_left2:
haftmann@51600
   439
  "x \<in># N \<Longrightarrow> (M + {#x#}) #\<inter> N = (M #\<inter> (N - {#x#})) + {#x#}"
haftmann@51600
   440
  by (simp add: multiset_eq_iff)
haftmann@51600
   441
haftmann@51600
   442
lemma inter_add_right1:
haftmann@51600
   443
  "\<not> x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = N #\<inter> M"
haftmann@51600
   444
  by (simp add: multiset_eq_iff)
haftmann@51600
   445
haftmann@51600
   446
lemma inter_add_right2:
haftmann@51600
   447
  "x \<in># N \<Longrightarrow> N #\<inter> (M + {#x#}) = ((N - {#x#}) #\<inter> M) + {#x#}"
haftmann@51600
   448
  by (simp add: multiset_eq_iff)
haftmann@51600
   449
haftmann@35268
   450
haftmann@51623
   451
subsubsection {* Bounded union *}
haftmann@51623
   452
haftmann@51623
   453
instantiation multiset :: (type) semilattice_sup
haftmann@51623
   454
begin
haftmann@51623
   455
haftmann@51623
   456
definition sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" where
haftmann@51623
   457
  "sup_multiset A B = A + (B - A)"
haftmann@51623
   458
haftmann@51623
   459
instance
haftmann@51623
   460
proof -
haftmann@51623
   461
  have aux: "\<And>m n q :: nat. m \<le> n \<Longrightarrow> q \<le> n \<Longrightarrow> m + (q - m) \<le> n" by arith
haftmann@51623
   462
  show "OFCLASS('a multiset, semilattice_sup_class)"
haftmann@51623
   463
    by default (auto simp add: sup_multiset_def mset_le_def aux)
haftmann@51623
   464
qed
haftmann@51623
   465
haftmann@51623
   466
end
haftmann@51623
   467
haftmann@51623
   468
abbreviation sup_multiset :: "'a multiset \<Rightarrow> 'a multiset \<Rightarrow> 'a multiset" (infixl "#\<union>" 70) where
haftmann@51623
   469
  "sup_multiset \<equiv> sup"
haftmann@51623
   470
haftmann@51623
   471
lemma sup_multiset_count [simp]:
haftmann@51623
   472
  "count (A #\<union> B) x = max (count A x) (count B x)"
haftmann@51623
   473
  by (simp add: sup_multiset_def)
haftmann@51623
   474
haftmann@51623
   475
lemma empty_sup [simp]:
haftmann@51623
   476
  "{#} #\<union> M = M"
haftmann@51623
   477
  by (simp add: multiset_eq_iff)
haftmann@51623
   478
haftmann@51623
   479
lemma sup_empty [simp]:
haftmann@51623
   480
  "M #\<union> {#} = M"
haftmann@51623
   481
  by (simp add: multiset_eq_iff)
haftmann@51623
   482
haftmann@51623
   483
lemma sup_add_left1:
haftmann@51623
   484
  "\<not> x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> N) + {#x#}"
haftmann@51623
   485
  by (simp add: multiset_eq_iff)
haftmann@51623
   486
haftmann@51623
   487
lemma sup_add_left2:
haftmann@51623
   488
  "x \<in># N \<Longrightarrow> (M + {#x#}) #\<union> N = (M #\<union> (N - {#x#})) + {#x#}"
haftmann@51623
   489
  by (simp add: multiset_eq_iff)
haftmann@51623
   490
haftmann@51623
   491
lemma sup_add_right1:
haftmann@51623
   492
  "\<not> x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = (N #\<union> M) + {#x#}"
haftmann@51623
   493
  by (simp add: multiset_eq_iff)
haftmann@51623
   494
haftmann@51623
   495
lemma sup_add_right2:
haftmann@51623
   496
  "x \<in># N \<Longrightarrow> N #\<union> (M + {#x#}) = ((N - {#x#}) #\<union> M) + {#x#}"
haftmann@51623
   497
  by (simp add: multiset_eq_iff)
haftmann@51623
   498
haftmann@51623
   499
haftmann@41069
   500
subsubsection {* Filter (with comprehension syntax) *}
haftmann@41069
   501
haftmann@41069
   502
text {* Multiset comprehension *}
haftmann@41069
   503
bulwahn@47429
   504
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
   505
by (rule filter_preserves_multiset)
haftmann@35268
   506
haftmann@41069
   507
hide_const (open) filter
haftmann@35268
   508
haftmann@41069
   509
lemma count_filter [simp]:
haftmann@41069
   510
  "count (Multiset.filter P M) a = (if P a then count M a else 0)"
bulwahn@47429
   511
  by (simp add: filter.rep_eq)
haftmann@41069
   512
haftmann@41069
   513
lemma filter_empty [simp]:
haftmann@41069
   514
  "Multiset.filter P {#} = {#}"
nipkow@39302
   515
  by (rule multiset_eqI) simp
haftmann@35268
   516
haftmann@41069
   517
lemma filter_single [simp]:
haftmann@41069
   518
  "Multiset.filter P {#x#} = (if P x then {#x#} else {#})"
haftmann@41069
   519
  by (rule multiset_eqI) simp
haftmann@41069
   520
haftmann@41069
   521
lemma filter_union [simp]:
haftmann@41069
   522
  "Multiset.filter P (M + N) = Multiset.filter P M + Multiset.filter P N"
nipkow@39302
   523
  by (rule multiset_eqI) simp
haftmann@35268
   524
haftmann@41069
   525
lemma filter_diff [simp]:
haftmann@41069
   526
  "Multiset.filter P (M - N) = Multiset.filter P M - Multiset.filter P N"
haftmann@41069
   527
  by (rule multiset_eqI) simp
haftmann@41069
   528
haftmann@41069
   529
lemma filter_inter [simp]:
haftmann@41069
   530
  "Multiset.filter P (M #\<inter> N) = Multiset.filter P M #\<inter> Multiset.filter P N"
nipkow@39302
   531
  by (rule multiset_eqI) simp
wenzelm@10249
   532
haftmann@41069
   533
syntax
haftmann@41069
   534
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ :# _./ _#})")
haftmann@41069
   535
syntax (xsymbol)
haftmann@41069
   536
  "_MCollect" :: "pttrn \<Rightarrow> 'a multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"    ("(1{# _ \<in># _./ _#})")
haftmann@41069
   537
translations
haftmann@41069
   538
  "{#x \<in># M. P#}" == "CONST Multiset.filter (\<lambda>x. P) M"
haftmann@41069
   539
wenzelm@10249
   540
wenzelm@10249
   541
subsubsection {* Set of elements *}
wenzelm@10249
   542
haftmann@34943
   543
definition set_of :: "'a multiset => 'a set" where
haftmann@34943
   544
  "set_of M = {x. x :# M}"
haftmann@34943
   545
wenzelm@17161
   546
lemma set_of_empty [simp]: "set_of {#} = {}"
nipkow@26178
   547
by (simp add: set_of_def)
wenzelm@10249
   548
wenzelm@17161
   549
lemma set_of_single [simp]: "set_of {#b#} = {b}"
nipkow@26178
   550
by (simp add: set_of_def)
wenzelm@10249
   551
wenzelm@17161
   552
lemma set_of_union [simp]: "set_of (M + N) = set_of M \<union> set_of N"
nipkow@26178
   553
by (auto simp add: set_of_def)
wenzelm@10249
   554
wenzelm@17161
   555
lemma set_of_eq_empty_iff [simp]: "(set_of M = {}) = (M = {#})"
nipkow@39302
   556
by (auto simp add: set_of_def multiset_eq_iff)
wenzelm@10249
   557
wenzelm@17161
   558
lemma mem_set_of_iff [simp]: "(x \<in> set_of M) = (x :# M)"
nipkow@26178
   559
by (auto simp add: set_of_def)
nipkow@26016
   560
haftmann@41069
   561
lemma set_of_filter [simp]: "set_of {# x:#M. P x #} = set_of M \<inter> {x. P x}"
nipkow@26178
   562
by (auto simp add: set_of_def)
wenzelm@10249
   563
haftmann@34943
   564
lemma finite_set_of [iff]: "finite (set_of M)"
haftmann@34943
   565
  using count [of M] by (simp add: multiset_def set_of_def)
haftmann@34943
   566
bulwahn@46756
   567
lemma finite_Collect_mem [iff]: "finite {x. x :# M}"
bulwahn@46756
   568
  unfolding set_of_def[symmetric] by simp
wenzelm@10249
   569
nipkow@55808
   570
lemma set_of_mono: "A \<le> B \<Longrightarrow> set_of A \<subseteq> set_of B"  
nipkow@55808
   571
  by (metis mset_leD subsetI mem_set_of_iff)
nipkow@55808
   572
wenzelm@10249
   573
subsubsection {* Size *}
wenzelm@10249
   574
blanchet@56656
   575
definition wcount where "wcount f M = (\<lambda>x. count M x * Suc (f x))"
blanchet@56656
   576
blanchet@56656
   577
lemma wcount_union: "wcount f (M + N) a = wcount f M a + wcount f N a"
blanchet@56656
   578
  by (auto simp: wcount_def add_mult_distrib)
blanchet@56656
   579
blanchet@56656
   580
definition size_multiset :: "('a \<Rightarrow> nat) \<Rightarrow> 'a multiset \<Rightarrow> nat" where
blanchet@56656
   581
  "size_multiset f M = setsum (wcount f M) (set_of M)"
blanchet@56656
   582
blanchet@56656
   583
lemmas size_multiset_eq = size_multiset_def[unfolded wcount_def]
blanchet@56656
   584
blanchet@56656
   585
instantiation multiset :: (type) size begin
blanchet@56656
   586
definition size_multiset where
blanchet@56656
   587
  size_multiset_overloaded_def: "size_multiset = Multiset.size_multiset (\<lambda>_. 0)"
haftmann@34943
   588
instance ..
haftmann@34943
   589
end
haftmann@34943
   590
blanchet@56656
   591
lemmas size_multiset_overloaded_eq =
blanchet@56656
   592
  size_multiset_overloaded_def[THEN fun_cong, unfolded size_multiset_eq, simplified]
blanchet@56656
   593
blanchet@56656
   594
lemma size_multiset_empty [simp]: "size_multiset f {#} = 0"
blanchet@56656
   595
by (simp add: size_multiset_def)
blanchet@56656
   596
haftmann@28708
   597
lemma size_empty [simp]: "size {#} = 0"
blanchet@56656
   598
by (simp add: size_multiset_overloaded_def)
blanchet@56656
   599
blanchet@56656
   600
lemma size_multiset_single [simp]: "size_multiset f {#b#} = Suc (f b)"
blanchet@56656
   601
by (simp add: size_multiset_eq)
wenzelm@10249
   602
haftmann@28708
   603
lemma size_single [simp]: "size {#b#} = 1"
blanchet@56656
   604
by (simp add: size_multiset_overloaded_def)
blanchet@56656
   605
blanchet@56656
   606
lemma setsum_wcount_Int:
blanchet@56656
   607
  "finite A \<Longrightarrow> setsum (wcount f N) (A \<inter> set_of N) = setsum (wcount f N) A"
nipkow@26178
   608
apply (induct rule: finite_induct)
nipkow@26178
   609
 apply simp
blanchet@56656
   610
apply (simp add: Int_insert_left set_of_def wcount_def)
blanchet@56656
   611
done
blanchet@56656
   612
blanchet@56656
   613
lemma size_multiset_union [simp]:
blanchet@56656
   614
  "size_multiset f (M + N::'a multiset) = size_multiset f M + size_multiset f N"
haftmann@57418
   615
apply (simp add: size_multiset_def setsum_Un_nat setsum.distrib setsum_wcount_Int wcount_union)
blanchet@56656
   616
apply (subst Int_commute)
blanchet@56656
   617
apply (simp add: setsum_wcount_Int)
nipkow@26178
   618
done
wenzelm@10249
   619
haftmann@28708
   620
lemma size_union [simp]: "size (M + N::'a multiset) = size M + size N"
blanchet@56656
   621
by (auto simp add: size_multiset_overloaded_def)
blanchet@56656
   622
blanchet@56656
   623
lemma size_multiset_eq_0_iff_empty [iff]: "(size_multiset f M = 0) = (M = {#})"
blanchet@56656
   624
by (auto simp add: size_multiset_eq multiset_eq_iff)
wenzelm@10249
   625
wenzelm@17161
   626
lemma size_eq_0_iff_empty [iff]: "(size M = 0) = (M = {#})"
blanchet@56656
   627
by (auto simp add: size_multiset_overloaded_def)
nipkow@26016
   628
nipkow@26016
   629
lemma nonempty_has_size: "(S \<noteq> {#}) = (0 < size S)"
nipkow@26178
   630
by (metis gr0I gr_implies_not0 size_empty size_eq_0_iff_empty)
wenzelm@10249
   631
wenzelm@17161
   632
lemma size_eq_Suc_imp_elem: "size M = Suc n ==> \<exists>a. a :# M"
blanchet@56656
   633
apply (unfold size_multiset_overloaded_eq)
nipkow@26178
   634
apply (drule setsum_SucD)
nipkow@26178
   635
apply auto
nipkow@26178
   636
done
wenzelm@10249
   637
haftmann@34943
   638
lemma size_eq_Suc_imp_eq_union:
haftmann@34943
   639
  assumes "size M = Suc n"
haftmann@34943
   640
  shows "\<exists>a N. M = N + {#a#}"
haftmann@34943
   641
proof -
haftmann@34943
   642
  from assms obtain a where "a \<in># M"
haftmann@34943
   643
    by (erule size_eq_Suc_imp_elem [THEN exE])
haftmann@34943
   644
  then have "M = M - {#a#} + {#a#}" by simp
haftmann@34943
   645
  then show ?thesis by blast
nipkow@23611
   646
qed
kleing@15869
   647
nipkow@26016
   648
nipkow@26016
   649
subsection {* Induction and case splits *}
wenzelm@10249
   650
wenzelm@18258
   651
theorem multiset_induct [case_names empty add, induct type: multiset]:
huffman@48009
   652
  assumes empty: "P {#}"
huffman@48009
   653
  assumes add: "\<And>M x. P M \<Longrightarrow> P (M + {#x#})"
huffman@48009
   654
  shows "P M"
huffman@48009
   655
proof (induct n \<equiv> "size M" arbitrary: M)
huffman@48009
   656
  case 0 thus "P M" by (simp add: empty)
huffman@48009
   657
next
huffman@48009
   658
  case (Suc k)
huffman@48009
   659
  obtain N x where "M = N + {#x#}"
huffman@48009
   660
    using `Suc k = size M` [symmetric]
huffman@48009
   661
    using size_eq_Suc_imp_eq_union by fast
huffman@48009
   662
  with Suc add show "P M" by simp
wenzelm@10249
   663
qed
wenzelm@10249
   664
kleing@25610
   665
lemma multi_nonempty_split: "M \<noteq> {#} \<Longrightarrow> \<exists>A a. M = A + {#a#}"
nipkow@26178
   666
by (induct M) auto
kleing@25610
   667
wenzelm@55913
   668
lemma multiset_cases [cases type]:
wenzelm@55913
   669
  obtains (empty) "M = {#}"
wenzelm@55913
   670
    | (add) N x where "M = N + {#x#}"
wenzelm@55913
   671
  using assms by (induct M) simp_all
kleing@25610
   672
haftmann@34943
   673
lemma multi_drop_mem_not_eq: "c \<in># B \<Longrightarrow> B - {#c#} \<noteq> B"
haftmann@34943
   674
by (cases "B = {#}") (auto dest: multi_member_split)
haftmann@34943
   675
nipkow@26033
   676
lemma multiset_partition: "M = {# x:#M. P x #} + {# x:#M. \<not> P x #}"
nipkow@39302
   677
apply (subst multiset_eq_iff)
nipkow@26178
   678
apply auto
nipkow@26178
   679
done
wenzelm@10249
   680
haftmann@35268
   681
lemma mset_less_size: "(A::'a multiset) < B \<Longrightarrow> size A < size B"
haftmann@34943
   682
proof (induct A arbitrary: B)
haftmann@34943
   683
  case (empty M)
haftmann@34943
   684
  then have "M \<noteq> {#}" by (simp add: mset_less_empty_nonempty)
haftmann@34943
   685
  then obtain M' x where "M = M' + {#x#}" 
haftmann@34943
   686
    by (blast dest: multi_nonempty_split)
haftmann@34943
   687
  then show ?case by simp
haftmann@34943
   688
next
haftmann@34943
   689
  case (add S x T)
haftmann@35268
   690
  have IH: "\<And>B. S < B \<Longrightarrow> size S < size B" by fact
haftmann@35268
   691
  have SxsubT: "S + {#x#} < T" by fact
haftmann@35268
   692
  then have "x \<in># T" and "S < T" by (auto dest: mset_less_insertD)
haftmann@34943
   693
  then obtain T' where T: "T = T' + {#x#}" 
haftmann@34943
   694
    by (blast dest: multi_member_split)
haftmann@35268
   695
  then have "S < T'" using SxsubT 
haftmann@34943
   696
    by (blast intro: mset_less_add_bothsides)
haftmann@34943
   697
  then have "size S < size T'" using IH by simp
haftmann@34943
   698
  then show ?case using T by simp
haftmann@34943
   699
qed
haftmann@34943
   700
haftmann@34943
   701
haftmann@34943
   702
subsubsection {* Strong induction and subset induction for multisets *}
haftmann@34943
   703
haftmann@34943
   704
text {* Well-foundedness of proper subset operator: *}
haftmann@34943
   705
haftmann@34943
   706
text {* proper multiset subset *}
haftmann@34943
   707
haftmann@34943
   708
definition
haftmann@34943
   709
  mset_less_rel :: "('a multiset * 'a multiset) set" where
haftmann@35268
   710
  "mset_less_rel = {(A,B). A < B}"
wenzelm@10249
   711
haftmann@34943
   712
lemma multiset_add_sub_el_shuffle: 
haftmann@34943
   713
  assumes "c \<in># B" and "b \<noteq> c" 
haftmann@34943
   714
  shows "B - {#c#} + {#b#} = B + {#b#} - {#c#}"
haftmann@34943
   715
proof -
haftmann@34943
   716
  from `c \<in># B` obtain A where B: "B = A + {#c#}" 
haftmann@34943
   717
    by (blast dest: multi_member_split)
haftmann@34943
   718
  have "A + {#b#} = A + {#b#} + {#c#} - {#c#}" by simp
haftmann@34943
   719
  then have "A + {#b#} = A + {#c#} + {#b#} - {#c#}" 
haftmann@34943
   720
    by (simp add: add_ac)
haftmann@34943
   721
  then show ?thesis using B by simp
haftmann@34943
   722
qed
haftmann@34943
   723
haftmann@34943
   724
lemma wf_mset_less_rel: "wf mset_less_rel"
haftmann@34943
   725
apply (unfold mset_less_rel_def)
haftmann@34943
   726
apply (rule wf_measure [THEN wf_subset, where f1=size])
haftmann@34943
   727
apply (clarsimp simp: measure_def inv_image_def mset_less_size)
haftmann@34943
   728
done
haftmann@34943
   729
haftmann@34943
   730
text {* The induction rules: *}
haftmann@34943
   731
haftmann@34943
   732
lemma full_multiset_induct [case_names less]:
haftmann@35268
   733
assumes ih: "\<And>B. \<forall>(A::'a multiset). A < B \<longrightarrow> P A \<Longrightarrow> P B"
haftmann@34943
   734
shows "P B"
haftmann@34943
   735
apply (rule wf_mset_less_rel [THEN wf_induct])
haftmann@34943
   736
apply (rule ih, auto simp: mset_less_rel_def)
haftmann@34943
   737
done
haftmann@34943
   738
haftmann@34943
   739
lemma multi_subset_induct [consumes 2, case_names empty add]:
haftmann@35268
   740
assumes "F \<le> A"
haftmann@34943
   741
  and empty: "P {#}"
haftmann@34943
   742
  and insert: "\<And>a F. a \<in># A \<Longrightarrow> P F \<Longrightarrow> P (F + {#a#})"
haftmann@34943
   743
shows "P F"
haftmann@34943
   744
proof -
haftmann@35268
   745
  from `F \<le> A`
haftmann@34943
   746
  show ?thesis
haftmann@34943
   747
  proof (induct F)
haftmann@34943
   748
    show "P {#}" by fact
haftmann@34943
   749
  next
haftmann@34943
   750
    fix x F
haftmann@35268
   751
    assume P: "F \<le> A \<Longrightarrow> P F" and i: "F + {#x#} \<le> A"
haftmann@34943
   752
    show "P (F + {#x#})"
haftmann@34943
   753
    proof (rule insert)
haftmann@34943
   754
      from i show "x \<in># A" by (auto dest: mset_le_insertD)
haftmann@35268
   755
      from i have "F \<le> A" by (auto dest: mset_le_insertD)
haftmann@34943
   756
      with P show "P F" .
haftmann@34943
   757
    qed
haftmann@34943
   758
  qed
haftmann@34943
   759
qed
wenzelm@26145
   760
wenzelm@17161
   761
huffman@48023
   762
subsection {* The fold combinator *}
huffman@48023
   763
haftmann@49822
   764
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
huffman@48023
   765
where
haftmann@49822
   766
  "fold f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_of M)"
huffman@48023
   767
haftmann@49822
   768
lemma fold_mset_empty [simp]:
haftmann@49822
   769
  "fold f s {#} = s"
haftmann@49822
   770
  by (simp add: fold_def)
huffman@48023
   771
huffman@48023
   772
context comp_fun_commute
huffman@48023
   773
begin
huffman@48023
   774
haftmann@49822
   775
lemma fold_mset_insert:
haftmann@49822
   776
  "fold f s (M + {#x#}) = f x (fold f s M)"
haftmann@49822
   777
proof -
haftmann@49822
   778
  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
haftmann@49822
   779
    by (fact comp_fun_commute_funpow)
haftmann@49822
   780
  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
haftmann@49822
   781
    by (fact comp_fun_commute_funpow)
haftmann@49822
   782
  show ?thesis
haftmann@49822
   783
  proof (cases "x \<in> set_of M")
haftmann@49822
   784
    case False
haftmann@49822
   785
    then have *: "count (M + {#x#}) x = 1" by simp
haftmann@49822
   786
    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_of M) =
haftmann@49822
   787
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_of M)"
haftmann@49822
   788
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
   789
    with False * show ?thesis
haftmann@49822
   790
      by (simp add: fold_def del: count_union)
huffman@48023
   791
  next
haftmann@49822
   792
    case True
haftmann@49822
   793
    def N \<equiv> "set_of M - {x}"
haftmann@49822
   794
    from N_def True have *: "set_of M = insert x N" "x \<notin> N" "finite N" by auto
haftmann@49822
   795
    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
haftmann@49822
   796
      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
haftmann@49822
   797
      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
haftmann@49822
   798
    with * show ?thesis by (simp add: fold_def del: count_union) simp
huffman@48023
   799
  qed
huffman@48023
   800
qed
huffman@48023
   801
haftmann@49822
   802
corollary fold_mset_single [simp]:
haftmann@49822
   803
  "fold f s {#x#} = f x s"
haftmann@49822
   804
proof -
haftmann@49822
   805
  have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
haftmann@49822
   806
  then show ?thesis by simp
haftmann@49822
   807
qed
huffman@48023
   808
haftmann@51548
   809
lemma fold_mset_fun_left_comm:
haftmann@49822
   810
  "f x (fold f s M) = fold f (f x s) M"
haftmann@49822
   811
  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
huffman@48023
   812
huffman@48023
   813
lemma fold_mset_union [simp]:
haftmann@49822
   814
  "fold f s (M + N) = fold f (fold f s M) N"
haftmann@49822
   815
proof (induct M)
huffman@48023
   816
  case empty then show ?case by simp
huffman@48023
   817
next
haftmann@49822
   818
  case (add M x)
haftmann@49822
   819
  have "M + {#x#} + N = (M + N) + {#x#}"
haftmann@49822
   820
    by (simp add: add_ac)
haftmann@51548
   821
  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_left_comm)
huffman@48023
   822
qed
huffman@48023
   823
huffman@48023
   824
lemma fold_mset_fusion:
huffman@48023
   825
  assumes "comp_fun_commute g"
haftmann@49822
   826
  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold g w A) = fold f (h w) A" (is "PROP ?P")
huffman@48023
   827
proof -
huffman@48023
   828
  interpret comp_fun_commute g by (fact assms)
huffman@48023
   829
  show "PROP ?P" by (induct A) auto
huffman@48023
   830
qed
huffman@48023
   831
huffman@48023
   832
end
huffman@48023
   833
huffman@48023
   834
text {*
huffman@48023
   835
  A note on code generation: When defining some function containing a
haftmann@49822
   836
  subterm @{term "fold F"}, code generation is not automatic. When
huffman@48023
   837
  interpreting locale @{text left_commutative} with @{text F}, the
haftmann@49822
   838
  would be code thms for @{const fold} become thms like
haftmann@49822
   839
  @{term "fold F z {#} = z"} where @{text F} is not a pattern but
huffman@48023
   840
  contains defined symbols, i.e.\ is not a code thm. Hence a separate
huffman@48023
   841
  constant with its own code thms needs to be introduced for @{text
huffman@48023
   842
  F}. See the image operator below.
huffman@48023
   843
*}
huffman@48023
   844
huffman@48023
   845
huffman@48023
   846
subsection {* Image *}
huffman@48023
   847
huffman@48023
   848
definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
haftmann@49822
   849
  "image_mset f = fold (plus o single o f) {#}"
huffman@48023
   850
haftmann@49823
   851
lemma comp_fun_commute_mset_image:
haftmann@49823
   852
  "comp_fun_commute (plus o single o f)"
haftmann@49823
   853
proof
haftmann@49823
   854
qed (simp add: add_ac fun_eq_iff)
huffman@48023
   855
huffman@48023
   856
lemma image_mset_empty [simp]: "image_mset f {#} = {#}"
haftmann@49823
   857
  by (simp add: image_mset_def)
huffman@48023
   858
huffman@48023
   859
lemma image_mset_single [simp]: "image_mset f {#x#} = {#f x#}"
haftmann@49823
   860
proof -
haftmann@49823
   861
  interpret comp_fun_commute "plus o single o f"
haftmann@49823
   862
    by (fact comp_fun_commute_mset_image)
haftmann@49823
   863
  show ?thesis by (simp add: image_mset_def)
haftmann@49823
   864
qed
huffman@48023
   865
huffman@48023
   866
lemma image_mset_union [simp]:
haftmann@49823
   867
  "image_mset f (M + N) = image_mset f M + image_mset f N"
haftmann@49823
   868
proof -
haftmann@49823
   869
  interpret comp_fun_commute "plus o single o f"
haftmann@49823
   870
    by (fact comp_fun_commute_mset_image)
haftmann@49823
   871
  show ?thesis by (induct N) (simp_all add: image_mset_def add_ac)
haftmann@49823
   872
qed
haftmann@49823
   873
haftmann@49823
   874
corollary image_mset_insert:
haftmann@49823
   875
  "image_mset f (M + {#a#}) = image_mset f M + {#f a#}"
haftmann@49823
   876
  by simp
huffman@48023
   877
haftmann@49823
   878
lemma set_of_image_mset [simp]:
haftmann@49823
   879
  "set_of (image_mset f M) = image f (set_of M)"
haftmann@49823
   880
  by (induct M) simp_all
huffman@48040
   881
haftmann@49823
   882
lemma size_image_mset [simp]:
haftmann@49823
   883
  "size (image_mset f M) = size M"
haftmann@49823
   884
  by (induct M) simp_all
huffman@48023
   885
haftmann@49823
   886
lemma image_mset_is_empty_iff [simp]:
haftmann@49823
   887
  "image_mset f M = {#} \<longleftrightarrow> M = {#}"
haftmann@49823
   888
  by (cases M) auto
huffman@48023
   889
huffman@48023
   890
syntax
huffman@48023
   891
  "_comprehension1_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> 'a multiset"
huffman@48023
   892
      ("({#_/. _ :# _#})")
huffman@48023
   893
translations
huffman@48023
   894
  "{#e. x:#M#}" == "CONST image_mset (%x. e) M"
huffman@48023
   895
huffman@48023
   896
syntax
huffman@48023
   897
  "_comprehension2_mset" :: "'a \<Rightarrow> 'b \<Rightarrow> 'b multiset \<Rightarrow> bool \<Rightarrow> 'a multiset"
huffman@48023
   898
      ("({#_/ | _ :# _./ _#})")
huffman@48023
   899
translations
huffman@48023
   900
  "{#e | x:#M. P#}" => "{#e. x :# {# x:#M. P#}#}"
huffman@48023
   901
huffman@48023
   902
text {*
huffman@48023
   903
  This allows to write not just filters like @{term "{#x:#M. x<c#}"}
huffman@48023
   904
  but also images like @{term "{#x+x. x:#M #}"} and @{term [source]
huffman@48023
   905
  "{#x+x|x:#M. x<c#}"}, where the latter is currently displayed as
huffman@48023
   906
  @{term "{#x+x|x:#M. x<c#}"}.
huffman@48023
   907
*}
huffman@48023
   908
blanchet@55467
   909
functor image_mset: image_mset
huffman@48023
   910
proof -
huffman@48023
   911
  fix f g show "image_mset f \<circ> image_mset g = image_mset (f \<circ> g)"
huffman@48023
   912
  proof
huffman@48023
   913
    fix A
huffman@48023
   914
    show "(image_mset f \<circ> image_mset g) A = image_mset (f \<circ> g) A"
huffman@48023
   915
      by (induct A) simp_all
huffman@48023
   916
  qed
huffman@48023
   917
  show "image_mset id = id"
huffman@48023
   918
  proof
huffman@48023
   919
    fix A
huffman@48023
   920
    show "image_mset id A = id A"
huffman@48023
   921
      by (induct A) simp_all
huffman@48023
   922
  qed
huffman@48023
   923
qed
huffman@48023
   924
haftmann@49717
   925
declare image_mset.identity [simp]
haftmann@49717
   926
huffman@48023
   927
haftmann@51548
   928
subsection {* Further conversions *}
haftmann@34943
   929
haftmann@34943
   930
primrec multiset_of :: "'a list \<Rightarrow> 'a multiset" where
haftmann@34943
   931
  "multiset_of [] = {#}" |
haftmann@34943
   932
  "multiset_of (a # x) = multiset_of x + {# a #}"
haftmann@34943
   933
haftmann@37107
   934
lemma in_multiset_in_set:
haftmann@37107
   935
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
haftmann@37107
   936
  by (induct xs) simp_all
haftmann@37107
   937
haftmann@37107
   938
lemma count_multiset_of:
haftmann@37107
   939
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@37107
   940
  by (induct xs) simp_all
haftmann@37107
   941
haftmann@34943
   942
lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
haftmann@34943
   943
by (induct x) auto
haftmann@34943
   944
haftmann@34943
   945
lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
haftmann@34943
   946
by (induct x) auto
haftmann@34943
   947
haftmann@40950
   948
lemma set_of_multiset_of[simp]: "set_of (multiset_of x) = set x"
haftmann@34943
   949
by (induct x) auto
haftmann@34943
   950
haftmann@34943
   951
lemma mem_set_multiset_eq: "x \<in> set xs = (x :# multiset_of xs)"
haftmann@34943
   952
by (induct xs) auto
haftmann@34943
   953
huffman@48012
   954
lemma size_multiset_of [simp]: "size (multiset_of xs) = length xs"
huffman@48012
   955
  by (induct xs) simp_all
huffman@48012
   956
haftmann@34943
   957
lemma multiset_of_append [simp]:
haftmann@34943
   958
  "multiset_of (xs @ ys) = multiset_of xs + multiset_of ys"
haftmann@34943
   959
  by (induct xs arbitrary: ys) (auto simp: add_ac)
haftmann@34943
   960
haftmann@40303
   961
lemma multiset_of_filter:
haftmann@40303
   962
  "multiset_of (filter P xs) = {#x :# multiset_of xs. P x #}"
haftmann@40303
   963
  by (induct xs) simp_all
haftmann@40303
   964
haftmann@40950
   965
lemma multiset_of_rev [simp]:
haftmann@40950
   966
  "multiset_of (rev xs) = multiset_of xs"
haftmann@40950
   967
  by (induct xs) simp_all
haftmann@40950
   968
haftmann@34943
   969
lemma surj_multiset_of: "surj multiset_of"
haftmann@34943
   970
apply (unfold surj_def)
haftmann@34943
   971
apply (rule allI)
haftmann@34943
   972
apply (rule_tac M = y in multiset_induct)
haftmann@34943
   973
 apply auto
haftmann@34943
   974
apply (rule_tac x = "x # xa" in exI)
haftmann@34943
   975
apply auto
haftmann@34943
   976
done
haftmann@34943
   977
haftmann@34943
   978
lemma set_count_greater_0: "set x = {a. count (multiset_of x) a > 0}"
haftmann@34943
   979
by (induct x) auto
haftmann@34943
   980
haftmann@34943
   981
lemma distinct_count_atmost_1:
haftmann@34943
   982
  "distinct x = (! a. count (multiset_of x) a = (if a \<in> set x then 1 else 0))"
haftmann@34943
   983
apply (induct x, simp, rule iffI, simp_all)
blanchet@55417
   984
apply (rename_tac a b)
haftmann@34943
   985
apply (rule conjI)
haftmann@34943
   986
apply (simp_all add: set_of_multiset_of [THEN sym] del: set_of_multiset_of)
haftmann@34943
   987
apply (erule_tac x = a in allE, simp, clarify)
haftmann@34943
   988
apply (erule_tac x = aa in allE, simp)
haftmann@34943
   989
done
haftmann@34943
   990
haftmann@34943
   991
lemma multiset_of_eq_setD:
haftmann@34943
   992
  "multiset_of xs = multiset_of ys \<Longrightarrow> set xs = set ys"
nipkow@39302
   993
by (rule) (auto simp add:multiset_eq_iff set_count_greater_0)
haftmann@34943
   994
haftmann@34943
   995
lemma set_eq_iff_multiset_of_eq_distinct:
haftmann@34943
   996
  "distinct x \<Longrightarrow> distinct y \<Longrightarrow>
haftmann@34943
   997
    (set x = set y) = (multiset_of x = multiset_of y)"
nipkow@39302
   998
by (auto simp: multiset_eq_iff distinct_count_atmost_1)
haftmann@34943
   999
haftmann@34943
  1000
lemma set_eq_iff_multiset_of_remdups_eq:
haftmann@34943
  1001
   "(set x = set y) = (multiset_of (remdups x) = multiset_of (remdups y))"
haftmann@34943
  1002
apply (rule iffI)
haftmann@34943
  1003
apply (simp add: set_eq_iff_multiset_of_eq_distinct[THEN iffD1])
haftmann@34943
  1004
apply (drule distinct_remdups [THEN distinct_remdups
haftmann@34943
  1005
      [THEN set_eq_iff_multiset_of_eq_distinct [THEN iffD2]]])
haftmann@34943
  1006
apply simp
haftmann@34943
  1007
done
haftmann@34943
  1008
haftmann@34943
  1009
lemma multiset_of_compl_union [simp]:
haftmann@34943
  1010
  "multiset_of [x\<leftarrow>xs. P x] + multiset_of [x\<leftarrow>xs. \<not>P x] = multiset_of xs"
haftmann@34943
  1011
  by (induct xs) (auto simp: add_ac)
haftmann@34943
  1012
haftmann@41069
  1013
lemma count_multiset_of_length_filter:
haftmann@39533
  1014
  "count (multiset_of xs) x = length (filter (\<lambda>y. x = y) xs)"
haftmann@39533
  1015
  by (induct xs) auto
haftmann@34943
  1016
haftmann@34943
  1017
lemma nth_mem_multiset_of: "i < length ls \<Longrightarrow> (ls ! i) :# multiset_of ls"
haftmann@34943
  1018
apply (induct ls arbitrary: i)
haftmann@34943
  1019
 apply simp
haftmann@34943
  1020
apply (case_tac i)
haftmann@34943
  1021
 apply auto
haftmann@34943
  1022
done
haftmann@34943
  1023
nipkow@36903
  1024
lemma multiset_of_remove1[simp]:
nipkow@36903
  1025
  "multiset_of (remove1 a xs) = multiset_of xs - {#a#}"
nipkow@39302
  1026
by (induct xs) (auto simp add: multiset_eq_iff)
haftmann@34943
  1027
haftmann@34943
  1028
lemma multiset_of_eq_length:
haftmann@37107
  1029
  assumes "multiset_of xs = multiset_of ys"
haftmann@37107
  1030
  shows "length xs = length ys"
huffman@48012
  1031
  using assms by (metis size_multiset_of)
haftmann@34943
  1032
haftmann@39533
  1033
lemma multiset_of_eq_length_filter:
haftmann@39533
  1034
  assumes "multiset_of xs = multiset_of ys"
haftmann@39533
  1035
  shows "length (filter (\<lambda>x. z = x) xs) = length (filter (\<lambda>y. z = y) ys)"
huffman@48012
  1036
  using assms by (metis count_multiset_of)
haftmann@39533
  1037
haftmann@45989
  1038
lemma fold_multiset_equiv:
haftmann@45989
  1039
  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
  1040
    and equiv: "multiset_of xs = multiset_of ys"
haftmann@49822
  1041
  shows "List.fold f xs = List.fold f ys"
wenzelm@46921
  1042
using f equiv [symmetric]
wenzelm@46921
  1043
proof (induct xs arbitrary: ys)
haftmann@45989
  1044
  case Nil then show ?case by simp
haftmann@45989
  1045
next
haftmann@45989
  1046
  case (Cons x xs)
haftmann@45989
  1047
  then have *: "set ys = set (x # xs)" by (blast dest: multiset_of_eq_setD)
haftmann@45989
  1048
  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
  1049
    by (rule Cons.prems(1)) (simp_all add: *)
haftmann@45989
  1050
  moreover from * have "x \<in> set ys" by simp
haftmann@49822
  1051
  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
haftmann@49822
  1052
  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
haftmann@45989
  1053
  ultimately show ?case by simp
haftmann@45989
  1054
qed
haftmann@45989
  1055
haftmann@51548
  1056
lemma multiset_of_insort [simp]:
haftmann@51548
  1057
  "multiset_of (insort x xs) = multiset_of xs + {#x#}"
haftmann@51548
  1058
  by (induct xs) (simp_all add: ac_simps)
haftmann@51548
  1059
haftmann@51600
  1060
lemma in_multiset_of:
haftmann@51600
  1061
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
haftmann@51600
  1062
  by (induct xs) simp_all
haftmann@51600
  1063
haftmann@51600
  1064
lemma multiset_of_map:
haftmann@51600
  1065
  "multiset_of (map f xs) = image_mset f (multiset_of xs)"
haftmann@51600
  1066
  by (induct xs) simp_all
haftmann@51600
  1067
haftmann@51548
  1068
definition multiset_of_set :: "'a set \<Rightarrow> 'a multiset"
haftmann@51548
  1069
where
haftmann@51548
  1070
  "multiset_of_set = folding.F (\<lambda>x M. {#x#} + M) {#}"
haftmann@51548
  1071
haftmann@51548
  1072
interpretation multiset_of_set!: folding "\<lambda>x M. {#x#} + M" "{#}"
haftmann@51548
  1073
where
haftmann@51548
  1074
  "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set"
haftmann@51548
  1075
proof -
haftmann@51548
  1076
  interpret comp_fun_commute "\<lambda>x M. {#x#} + M" by default (simp add: fun_eq_iff ac_simps)
haftmann@51548
  1077
  show "folding (\<lambda>x M. {#x#} + M)" by default (fact comp_fun_commute)
haftmann@51548
  1078
  from multiset_of_set_def show "folding.F (\<lambda>x M. {#x#} + M) {#} = multiset_of_set" ..
haftmann@51548
  1079
qed
haftmann@51548
  1080
haftmann@51600
  1081
lemma count_multiset_of_set [simp]:
haftmann@51600
  1082
  "finite A \<Longrightarrow> x \<in> A \<Longrightarrow> count (multiset_of_set A) x = 1" (is "PROP ?P")
haftmann@51600
  1083
  "\<not> finite A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?Q")
haftmann@51600
  1084
  "x \<notin> A \<Longrightarrow> count (multiset_of_set A) x = 0" (is "PROP ?R")
haftmann@51600
  1085
proof -
haftmann@51600
  1086
  { fix A
haftmann@51600
  1087
    assume "x \<notin> A"
haftmann@51600
  1088
    have "count (multiset_of_set A) x = 0"
haftmann@51600
  1089
    proof (cases "finite A")
haftmann@51600
  1090
      case False then show ?thesis by simp
haftmann@51600
  1091
    next
haftmann@51600
  1092
      case True from True `x \<notin> A` show ?thesis by (induct A) auto
haftmann@51600
  1093
    qed
haftmann@51600
  1094
  } note * = this
haftmann@51600
  1095
  then show "PROP ?P" "PROP ?Q" "PROP ?R"
haftmann@51600
  1096
  by (auto elim!: Set.set_insert)
haftmann@51600
  1097
qed -- {* TODO: maybe define @{const multiset_of_set} also in terms of @{const Abs_multiset} *}
haftmann@51600
  1098
haftmann@51548
  1099
context linorder
haftmann@51548
  1100
begin
haftmann@51548
  1101
haftmann@51548
  1102
definition sorted_list_of_multiset :: "'a multiset \<Rightarrow> 'a list"
haftmann@51548
  1103
where
haftmann@51548
  1104
  "sorted_list_of_multiset M = fold insort [] M"
haftmann@51548
  1105
haftmann@51548
  1106
lemma sorted_list_of_multiset_empty [simp]:
haftmann@51548
  1107
  "sorted_list_of_multiset {#} = []"
haftmann@51548
  1108
  by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1109
haftmann@51548
  1110
lemma sorted_list_of_multiset_singleton [simp]:
haftmann@51548
  1111
  "sorted_list_of_multiset {#x#} = [x]"
haftmann@51548
  1112
proof -
haftmann@51548
  1113
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1114
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1115
qed
haftmann@51548
  1116
haftmann@51548
  1117
lemma sorted_list_of_multiset_insert [simp]:
haftmann@51548
  1118
  "sorted_list_of_multiset (M + {#x#}) = List.insort x (sorted_list_of_multiset M)"
haftmann@51548
  1119
proof -
haftmann@51548
  1120
  interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
haftmann@51548
  1121
  show ?thesis by (simp add: sorted_list_of_multiset_def)
haftmann@51548
  1122
qed
haftmann@51548
  1123
haftmann@51548
  1124
end
haftmann@51548
  1125
haftmann@51548
  1126
lemma multiset_of_sorted_list_of_multiset [simp]:
haftmann@51548
  1127
  "multiset_of (sorted_list_of_multiset M) = M"
haftmann@51548
  1128
  by (induct M) simp_all
haftmann@51548
  1129
haftmann@51548
  1130
lemma sorted_list_of_multiset_multiset_of [simp]:
haftmann@51548
  1131
  "sorted_list_of_multiset (multiset_of xs) = sort xs"
haftmann@51548
  1132
  by (induct xs) simp_all
haftmann@51548
  1133
haftmann@51548
  1134
lemma finite_set_of_multiset_of_set:
haftmann@51548
  1135
  assumes "finite A"
haftmann@51548
  1136
  shows "set_of (multiset_of_set A) = A"
haftmann@51548
  1137
  using assms by (induct A) simp_all
haftmann@51548
  1138
haftmann@51548
  1139
lemma infinite_set_of_multiset_of_set:
haftmann@51548
  1140
  assumes "\<not> finite A"
haftmann@51548
  1141
  shows "set_of (multiset_of_set A) = {}"
haftmann@51548
  1142
  using assms by simp
haftmann@51548
  1143
haftmann@51548
  1144
lemma set_sorted_list_of_multiset [simp]:
haftmann@51548
  1145
  "set (sorted_list_of_multiset M) = set_of M"
haftmann@51548
  1146
  by (induct M) (simp_all add: set_insort)
haftmann@51548
  1147
haftmann@51548
  1148
lemma sorted_list_of_multiset_of_set [simp]:
haftmann@51548
  1149
  "sorted_list_of_multiset (multiset_of_set A) = sorted_list_of_set A"
haftmann@51548
  1150
  by (cases "finite A") (induct A rule: finite_induct, simp_all add: ac_simps)
haftmann@51548
  1151
haftmann@51548
  1152
haftmann@51548
  1153
subsection {* Big operators *}
haftmann@51548
  1154
haftmann@51548
  1155
no_notation times (infixl "*" 70)
haftmann@51548
  1156
no_notation Groups.one ("1")
haftmann@51548
  1157
haftmann@51548
  1158
locale comm_monoid_mset = comm_monoid
haftmann@51548
  1159
begin
haftmann@51548
  1160
haftmann@51548
  1161
definition F :: "'a multiset \<Rightarrow> 'a"
haftmann@51548
  1162
where
haftmann@51548
  1163
  eq_fold: "F M = Multiset.fold f 1 M"
haftmann@51548
  1164
haftmann@51548
  1165
lemma empty [simp]:
haftmann@51548
  1166
  "F {#} = 1"
haftmann@51548
  1167
  by (simp add: eq_fold)
haftmann@51548
  1168
haftmann@51548
  1169
lemma singleton [simp]:
haftmann@51548
  1170
  "F {#x#} = x"
haftmann@51548
  1171
proof -
haftmann@51548
  1172
  interpret comp_fun_commute
haftmann@51548
  1173
    by default (simp add: fun_eq_iff left_commute)
haftmann@51548
  1174
  show ?thesis by (simp add: eq_fold)
haftmann@51548
  1175
qed
haftmann@51548
  1176
haftmann@51548
  1177
lemma union [simp]:
haftmann@51548
  1178
  "F (M + N) = F M * F N"
haftmann@51548
  1179
proof -
haftmann@51548
  1180
  interpret comp_fun_commute f
haftmann@51548
  1181
    by default (simp add: fun_eq_iff left_commute)
haftmann@51548
  1182
  show ?thesis by (induct N) (simp_all add: left_commute eq_fold)
haftmann@51548
  1183
qed
haftmann@51548
  1184
haftmann@51548
  1185
end
haftmann@51548
  1186
haftmann@51548
  1187
notation times (infixl "*" 70)
haftmann@51548
  1188
notation Groups.one ("1")
haftmann@51548
  1189
haftmann@54868
  1190
context comm_monoid_add
haftmann@54868
  1191
begin
haftmann@54868
  1192
haftmann@54868
  1193
definition msetsum :: "'a multiset \<Rightarrow> 'a"
haftmann@51548
  1194
where
haftmann@51548
  1195
  "msetsum = comm_monoid_mset.F plus 0"
haftmann@51548
  1196
haftmann@54868
  1197
sublocale msetsum!: comm_monoid_mset plus 0
haftmann@51548
  1198
where
haftmann@51548
  1199
  "comm_monoid_mset.F plus 0 = msetsum"
haftmann@51548
  1200
proof -
haftmann@51548
  1201
  show "comm_monoid_mset plus 0" ..
haftmann@51548
  1202
  from msetsum_def show "comm_monoid_mset.F plus 0 = msetsum" ..
haftmann@51548
  1203
qed
haftmann@51548
  1204
haftmann@51548
  1205
lemma setsum_unfold_msetsum:
haftmann@51548
  1206
  "setsum f A = msetsum (image_mset f (multiset_of_set A))"
haftmann@51548
  1207
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1208
haftmann@51548
  1209
abbreviation msetsum_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
haftmann@51548
  1210
where
haftmann@51548
  1211
  "msetsum_image f M \<equiv> msetsum (image_mset f M)"
haftmann@51548
  1212
haftmann@51548
  1213
end
haftmann@51548
  1214
haftmann@51548
  1215
syntax
haftmann@51548
  1216
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
haftmann@51548
  1217
      ("(3SUM _:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1218
haftmann@51548
  1219
syntax (xsymbols)
haftmann@51548
  1220
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
haftmann@51548
  1221
      ("(3\<Sum>_:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1222
haftmann@51548
  1223
syntax (HTML output)
haftmann@51548
  1224
  "_msetsum_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_add" 
haftmann@51548
  1225
      ("(3\<Sum>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1226
haftmann@51548
  1227
translations
haftmann@51548
  1228
  "SUM i :# A. b" == "CONST msetsum_image (\<lambda>i. b) A"
haftmann@51548
  1229
haftmann@54868
  1230
context comm_monoid_mult
haftmann@54868
  1231
begin
haftmann@54868
  1232
haftmann@54868
  1233
definition msetprod :: "'a multiset \<Rightarrow> 'a"
haftmann@54868
  1234
where
haftmann@54868
  1235
  "msetprod = comm_monoid_mset.F times 1"
haftmann@54868
  1236
haftmann@54868
  1237
sublocale msetprod!: comm_monoid_mset times 1
haftmann@51548
  1238
where
haftmann@51548
  1239
  "comm_monoid_mset.F times 1 = msetprod"
haftmann@51548
  1240
proof -
haftmann@51548
  1241
  show "comm_monoid_mset times 1" ..
haftmann@51548
  1242
  from msetprod_def show "comm_monoid_mset.F times 1 = msetprod" ..
haftmann@51548
  1243
qed
haftmann@51548
  1244
haftmann@51548
  1245
lemma msetprod_empty:
haftmann@51548
  1246
  "msetprod {#} = 1"
haftmann@51548
  1247
  by (fact msetprod.empty)
haftmann@51548
  1248
haftmann@51548
  1249
lemma msetprod_singleton:
haftmann@51548
  1250
  "msetprod {#x#} = x"
haftmann@51548
  1251
  by (fact msetprod.singleton)
haftmann@51548
  1252
haftmann@51548
  1253
lemma msetprod_Un:
haftmann@51548
  1254
  "msetprod (A + B) = msetprod A * msetprod B" 
haftmann@51548
  1255
  by (fact msetprod.union)
haftmann@51548
  1256
haftmann@51548
  1257
lemma setprod_unfold_msetprod:
haftmann@51548
  1258
  "setprod f A = msetprod (image_mset f (multiset_of_set A))"
haftmann@51548
  1259
  by (cases "finite A") (induct A rule: finite_induct, simp_all)
haftmann@51548
  1260
haftmann@51548
  1261
lemma msetprod_multiplicity:
haftmann@51548
  1262
  "msetprod M = setprod (\<lambda>x. x ^ count M x) (set_of M)"
haftmann@51548
  1263
  by (simp add: Multiset.fold_def setprod.eq_fold msetprod.eq_fold funpow_times_power comp_def)
haftmann@51548
  1264
haftmann@51548
  1265
abbreviation msetprod_image :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b multiset \<Rightarrow> 'a"
haftmann@51548
  1266
where
haftmann@51548
  1267
  "msetprod_image f M \<equiv> msetprod (image_mset f M)"
haftmann@51548
  1268
haftmann@51548
  1269
end
haftmann@51548
  1270
haftmann@51548
  1271
syntax
haftmann@51548
  1272
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
haftmann@51548
  1273
      ("(3PROD _:#_. _)" [0, 51, 10] 10)
haftmann@51548
  1274
haftmann@51548
  1275
syntax (xsymbols)
haftmann@51548
  1276
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
haftmann@51548
  1277
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1278
haftmann@51548
  1279
syntax (HTML output)
haftmann@51548
  1280
  "_msetprod_image" :: "pttrn \<Rightarrow> 'b set \<Rightarrow> 'a \<Rightarrow> 'a::comm_monoid_mult" 
haftmann@51548
  1281
      ("(3\<Prod>_\<in>#_. _)" [0, 51, 10] 10)
haftmann@51548
  1282
haftmann@51548
  1283
translations
haftmann@51548
  1284
  "PROD i :# A. b" == "CONST msetprod_image (\<lambda>i. b) A"
haftmann@51548
  1285
haftmann@51548
  1286
lemma (in comm_semiring_1) dvd_msetprod:
haftmann@51548
  1287
  assumes "x \<in># A"
haftmann@51548
  1288
  shows "x dvd msetprod A"
haftmann@51548
  1289
proof -
haftmann@51548
  1290
  from assms have "A = (A - {#x#}) + {#x#}" by simp
haftmann@51548
  1291
  then obtain B where "A = B + {#x#}" ..
haftmann@51548
  1292
  then show ?thesis by simp
haftmann@51548
  1293
qed
haftmann@51548
  1294
haftmann@51548
  1295
haftmann@51548
  1296
subsection {* Cardinality *}
haftmann@51548
  1297
haftmann@51548
  1298
definition mcard :: "'a multiset \<Rightarrow> nat"
haftmann@51548
  1299
where
haftmann@51548
  1300
  "mcard = msetsum \<circ> image_mset (\<lambda>_. 1)"
haftmann@51548
  1301
haftmann@51548
  1302
lemma mcard_empty [simp]:
haftmann@51548
  1303
  "mcard {#} = 0"
haftmann@51548
  1304
  by (simp add: mcard_def)
haftmann@51548
  1305
haftmann@51548
  1306
lemma mcard_singleton [simp]:
haftmann@51548
  1307
  "mcard {#a#} = Suc 0"
haftmann@51548
  1308
  by (simp add: mcard_def)
haftmann@51548
  1309
haftmann@51548
  1310
lemma mcard_plus [simp]:
haftmann@51548
  1311
  "mcard (M + N) = mcard M + mcard N"
haftmann@51548
  1312
  by (simp add: mcard_def)
haftmann@51548
  1313
haftmann@51548
  1314
lemma mcard_empty_iff [simp]:
haftmann@51548
  1315
  "mcard M = 0 \<longleftrightarrow> M = {#}"
haftmann@51548
  1316
  by (induct M) simp_all
haftmann@51548
  1317
haftmann@51548
  1318
lemma mcard_unfold_setsum:
haftmann@51548
  1319
  "mcard M = setsum (count M) (set_of M)"
haftmann@51548
  1320
proof (induct M)
haftmann@51548
  1321
  case empty then show ?case by simp
haftmann@51548
  1322
next
haftmann@51548
  1323
  case (add M x) then show ?case
haftmann@51548
  1324
    by (cases "x \<in> set_of M")
haftmann@51548
  1325
      (simp_all del: mem_set_of_iff add: setsum.distrib setsum.delta' insert_absorb, simp)
haftmann@51548
  1326
qed
haftmann@51548
  1327
haftmann@51600
  1328
lemma size_eq_mcard:
haftmann@51600
  1329
  "size = mcard"
blanchet@56656
  1330
  by (simp add: fun_eq_iff size_multiset_overloaded_eq mcard_unfold_setsum)
haftmann@51600
  1331
haftmann@51600
  1332
lemma mcard_multiset_of:
haftmann@51600
  1333
  "mcard (multiset_of xs) = length xs"
haftmann@51600
  1334
  by (induct xs) simp_all
haftmann@51600
  1335
haftmann@51548
  1336
haftmann@51548
  1337
subsection {* Alternative representations *}
haftmann@51548
  1338
haftmann@51548
  1339
subsubsection {* Lists *}
haftmann@51548
  1340
haftmann@39533
  1341
context linorder
haftmann@39533
  1342
begin
haftmann@39533
  1343
haftmann@40210
  1344
lemma multiset_of_insort [simp]:
haftmann@39533
  1345
  "multiset_of (insort_key k x xs) = {#x#} + multiset_of xs"
haftmann@37107
  1346
  by (induct xs) (simp_all add: ac_simps)
haftmann@39533
  1347
 
haftmann@40210
  1348
lemma multiset_of_sort [simp]:
haftmann@39533
  1349
  "multiset_of (sort_key k xs) = multiset_of xs"
haftmann@37107
  1350
  by (induct xs) (simp_all add: ac_simps)
haftmann@37107
  1351
haftmann@34943
  1352
text {*
haftmann@34943
  1353
  This lemma shows which properties suffice to show that a function
haftmann@34943
  1354
  @{text "f"} with @{text "f xs = ys"} behaves like sort.
haftmann@34943
  1355
*}
haftmann@37074
  1356
haftmann@39533
  1357
lemma properties_for_sort_key:
haftmann@39533
  1358
  assumes "multiset_of ys = multiset_of xs"
haftmann@40305
  1359
  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
  1360
  and "sorted (map f ys)"
haftmann@39533
  1361
  shows "sort_key f xs = ys"
wenzelm@46921
  1362
using assms
wenzelm@46921
  1363
proof (induct xs arbitrary: ys)
haftmann@34943
  1364
  case Nil then show ?case by simp
haftmann@34943
  1365
next
haftmann@34943
  1366
  case (Cons x xs)
haftmann@39533
  1367
  from Cons.prems(2) have
haftmann@40305
  1368
    "\<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
  1369
    by (simp add: filter_remove1)
haftmann@39533
  1370
  with Cons.prems have "sort_key f xs = remove1 x ys"
haftmann@39533
  1371
    by (auto intro!: Cons.hyps simp add: sorted_map_remove1)
haftmann@39533
  1372
  moreover from Cons.prems have "x \<in> set ys"
haftmann@39533
  1373
    by (auto simp add: mem_set_multiset_eq intro!: ccontr)
haftmann@39533
  1374
  ultimately show ?case using Cons.prems by (simp add: insort_key_remove1)
haftmann@34943
  1375
qed
haftmann@34943
  1376
haftmann@39533
  1377
lemma properties_for_sort:
haftmann@39533
  1378
  assumes multiset: "multiset_of ys = multiset_of xs"
haftmann@39533
  1379
  and "sorted ys"
haftmann@39533
  1380
  shows "sort xs = ys"
haftmann@39533
  1381
proof (rule properties_for_sort_key)
haftmann@39533
  1382
  from multiset show "multiset_of ys = multiset_of xs" .
haftmann@39533
  1383
  from `sorted ys` show "sorted (map (\<lambda>x. x) ys)" by simp
haftmann@39533
  1384
  from multiset have "\<And>k. length (filter (\<lambda>y. k = y) ys) = length (filter (\<lambda>x. k = x) xs)"
haftmann@39533
  1385
    by (rule multiset_of_eq_length_filter)
haftmann@39533
  1386
  then have "\<And>k. replicate (length (filter (\<lambda>y. k = y) ys)) k = replicate (length (filter (\<lambda>x. k = x) xs)) k"
haftmann@39533
  1387
    by simp
haftmann@40305
  1388
  then show "\<And>k. k \<in> set ys \<Longrightarrow> filter (\<lambda>y. k = y) ys = filter (\<lambda>x. k = x) xs"
haftmann@39533
  1389
    by (simp add: replicate_length_filter)
haftmann@39533
  1390
qed
haftmann@39533
  1391
haftmann@40303
  1392
lemma sort_key_by_quicksort:
haftmann@40303
  1393
  "sort_key f xs = sort_key f [x\<leftarrow>xs. f x < f (xs ! (length xs div 2))]
haftmann@40303
  1394
    @ [x\<leftarrow>xs. f x = f (xs ! (length xs div 2))]
haftmann@40303
  1395
    @ sort_key f [x\<leftarrow>xs. f x > f (xs ! (length xs div 2))]" (is "sort_key f ?lhs = ?rhs")
haftmann@40303
  1396
proof (rule properties_for_sort_key)
haftmann@40303
  1397
  show "multiset_of ?rhs = multiset_of ?lhs"
haftmann@40303
  1398
    by (rule multiset_eqI) (auto simp add: multiset_of_filter)
haftmann@40303
  1399
next
haftmann@40303
  1400
  show "sorted (map f ?rhs)"
haftmann@40303
  1401
    by (auto simp add: sorted_append intro: sorted_map_same)
haftmann@40303
  1402
next
haftmann@40305
  1403
  fix l
haftmann@40305
  1404
  assume "l \<in> set ?rhs"
haftmann@40346
  1405
  let ?pivot = "f (xs ! (length xs div 2))"
haftmann@40346
  1406
  have *: "\<And>x. f l = f x \<longleftrightarrow> f x = f l" by auto
haftmann@40306
  1407
  have "[x \<leftarrow> sort_key f xs . f x = f l] = [x \<leftarrow> xs. f x = f l]"
haftmann@40305
  1408
    unfolding filter_sort by (rule properties_for_sort_key) (auto intro: sorted_map_same)
haftmann@40346
  1409
  with * have **: "[x \<leftarrow> sort_key f xs . f l = f x] = [x \<leftarrow> xs. f l = f x]" by simp
haftmann@40346
  1410
  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
  1411
  then have "\<And>P. [x \<leftarrow> sort_key f xs . P (f x) ?pivot \<and> f l = f x] =
haftmann@40346
  1412
    [x \<leftarrow> sort_key f xs. P (f l) ?pivot \<and> f l = f x]" by simp
haftmann@40346
  1413
  note *** = this [of "op <"] this [of "op >"] this [of "op ="]
haftmann@40306
  1414
  show "[x \<leftarrow> ?rhs. f l = f x] = [x \<leftarrow> ?lhs. f l = f x]"
haftmann@40305
  1415
  proof (cases "f l" ?pivot rule: linorder_cases)
wenzelm@46730
  1416
    case less
wenzelm@46730
  1417
    then have "f l \<noteq> ?pivot" and "\<not> f l > ?pivot" by auto
wenzelm@46730
  1418
    with less show ?thesis
haftmann@40346
  1419
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40305
  1420
  next
haftmann@40306
  1421
    case equal then show ?thesis
haftmann@40346
  1422
      by (simp add: * less_le)
haftmann@40305
  1423
  next
wenzelm@46730
  1424
    case greater
wenzelm@46730
  1425
    then have "f l \<noteq> ?pivot" and "\<not> f l < ?pivot" by auto
wenzelm@46730
  1426
    with greater show ?thesis
haftmann@40346
  1427
      by (simp add: filter_sort [symmetric] ** ***)
haftmann@40306
  1428
  qed
haftmann@40303
  1429
qed
haftmann@40303
  1430
haftmann@40303
  1431
lemma sort_by_quicksort:
haftmann@40303
  1432
  "sort xs = sort [x\<leftarrow>xs. x < xs ! (length xs div 2)]
haftmann@40303
  1433
    @ [x\<leftarrow>xs. x = xs ! (length xs div 2)]
haftmann@40303
  1434
    @ sort [x\<leftarrow>xs. x > xs ! (length xs div 2)]" (is "sort ?lhs = ?rhs")
haftmann@40303
  1435
  using sort_key_by_quicksort [of "\<lambda>x. x", symmetric] by simp
haftmann@40303
  1436
haftmann@40347
  1437
text {* A stable parametrized quicksort *}
haftmann@40347
  1438
haftmann@40347
  1439
definition part :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'b list \<times> 'b list \<times> 'b list" where
haftmann@40347
  1440
  "part f pivot xs = ([x \<leftarrow> xs. f x < pivot], [x \<leftarrow> xs. f x = pivot], [x \<leftarrow> xs. pivot < f x])"
haftmann@40347
  1441
haftmann@40347
  1442
lemma part_code [code]:
haftmann@40347
  1443
  "part f pivot [] = ([], [], [])"
haftmann@40347
  1444
  "part f pivot (x # xs) = (let (lts, eqs, gts) = part f pivot xs; x' = f x in
haftmann@40347
  1445
     if x' < pivot then (x # lts, eqs, gts)
haftmann@40347
  1446
     else if x' > pivot then (lts, eqs, x # gts)
haftmann@40347
  1447
     else (lts, x # eqs, gts))"
haftmann@40347
  1448
  by (auto simp add: part_def Let_def split_def)
haftmann@40347
  1449
haftmann@40347
  1450
lemma sort_key_by_quicksort_code [code]:
haftmann@40347
  1451
  "sort_key f xs = (case xs of [] \<Rightarrow> []
haftmann@40347
  1452
    | [x] \<Rightarrow> xs
haftmann@40347
  1453
    | [x, y] \<Rightarrow> (if f x \<le> f y then xs else [y, x])
haftmann@40347
  1454
    | _ \<Rightarrow> (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1455
       in sort_key f lts @ eqs @ sort_key f gts))"
haftmann@40347
  1456
proof (cases xs)
haftmann@40347
  1457
  case Nil then show ?thesis by simp
haftmann@40347
  1458
next
wenzelm@46921
  1459
  case (Cons _ ys) note hyps = Cons show ?thesis
wenzelm@46921
  1460
  proof (cases ys)
haftmann@40347
  1461
    case Nil with hyps show ?thesis by simp
haftmann@40347
  1462
  next
wenzelm@46921
  1463
    case (Cons _ zs) note hyps = hyps Cons show ?thesis
wenzelm@46921
  1464
    proof (cases zs)
haftmann@40347
  1465
      case Nil with hyps show ?thesis by auto
haftmann@40347
  1466
    next
haftmann@40347
  1467
      case Cons 
haftmann@40347
  1468
      from sort_key_by_quicksort [of f xs]
haftmann@40347
  1469
      have "sort_key f xs = (let (lts, eqs, gts) = part f (f (xs ! (length xs div 2))) xs
haftmann@40347
  1470
        in sort_key f lts @ eqs @ sort_key f gts)"
haftmann@40347
  1471
      by (simp only: split_def Let_def part_def fst_conv snd_conv)
haftmann@40347
  1472
      with hyps Cons show ?thesis by (simp only: list.cases)
haftmann@40347
  1473
    qed
haftmann@40347
  1474
  qed
haftmann@40347
  1475
qed
haftmann@40347
  1476
haftmann@39533
  1477
end
haftmann@39533
  1478
haftmann@40347
  1479
hide_const (open) part
haftmann@40347
  1480
haftmann@35268
  1481
lemma multiset_of_remdups_le: "multiset_of (remdups xs) \<le> multiset_of xs"
haftmann@35268
  1482
  by (induct xs) (auto intro: order_trans)
haftmann@34943
  1483
haftmann@34943
  1484
lemma multiset_of_update:
haftmann@34943
  1485
  "i < length ls \<Longrightarrow> multiset_of (ls[i := v]) = multiset_of ls - {#ls ! i#} + {#v#}"
haftmann@34943
  1486
proof (induct ls arbitrary: i)
haftmann@34943
  1487
  case Nil then show ?case by simp
haftmann@34943
  1488
next
haftmann@34943
  1489
  case (Cons x xs)
haftmann@34943
  1490
  show ?case
haftmann@34943
  1491
  proof (cases i)
haftmann@34943
  1492
    case 0 then show ?thesis by simp
haftmann@34943
  1493
  next
haftmann@34943
  1494
    case (Suc i')
haftmann@34943
  1495
    with Cons show ?thesis
haftmann@34943
  1496
      apply simp
haftmann@34943
  1497
      apply (subst add_assoc)
haftmann@34943
  1498
      apply (subst add_commute [of "{#v#}" "{#x#}"])
haftmann@34943
  1499
      apply (subst add_assoc [symmetric])
haftmann@34943
  1500
      apply simp
haftmann@34943
  1501
      apply (rule mset_le_multiset_union_diff_commute)
haftmann@34943
  1502
      apply (simp add: mset_le_single nth_mem_multiset_of)
haftmann@34943
  1503
      done
haftmann@34943
  1504
  qed
haftmann@34943
  1505
qed
haftmann@34943
  1506
haftmann@34943
  1507
lemma multiset_of_swap:
haftmann@34943
  1508
  "i < length ls \<Longrightarrow> j < length ls \<Longrightarrow>
haftmann@34943
  1509
    multiset_of (ls[j := ls ! i, i := ls ! j]) = multiset_of ls"
haftmann@34943
  1510
  by (cases "i = j") (simp_all add: multiset_of_update nth_mem_multiset_of)
haftmann@34943
  1511
haftmann@34943
  1512
haftmann@34943
  1513
subsection {* The multiset order *}
wenzelm@10249
  1514
wenzelm@10249
  1515
subsubsection {* Well-foundedness *}
wenzelm@10249
  1516
haftmann@28708
  1517
definition mult1 :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1518
  "mult1 r = {(N, M). \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and>
berghofe@23751
  1519
      (\<forall>b. b :# K --> (b, a) \<in> r)}"
wenzelm@10249
  1520
haftmann@28708
  1521
definition mult :: "('a \<times> 'a) set => ('a multiset \<times> 'a multiset) set" where
haftmann@37765
  1522
  "mult r = (mult1 r)\<^sup>+"
wenzelm@10249
  1523
berghofe@23751
  1524
lemma not_less_empty [iff]: "(M, {#}) \<notin> mult1 r"
nipkow@26178
  1525
by (simp add: mult1_def)
wenzelm@10249
  1526
berghofe@23751
  1527
lemma less_add: "(N, M0 + {#a#}) \<in> mult1 r ==>
berghofe@23751
  1528
    (\<exists>M. (M, M0) \<in> mult1 r \<and> N = M + {#a#}) \<or>
berghofe@23751
  1529
    (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K)"
wenzelm@19582
  1530
  (is "_ \<Longrightarrow> ?case1 (mult1 r) \<or> ?case2")
wenzelm@10249
  1531
proof (unfold mult1_def)
berghofe@23751
  1532
  let ?r = "\<lambda>K a. \<forall>b. b :# K --> (b, a) \<in> r"
nipkow@11464
  1533
  let ?R = "\<lambda>N M. \<exists>a M0 K. M = M0 + {#a#} \<and> N = M0 + K \<and> ?r K a"
berghofe@23751
  1534
  let ?case1 = "?case1 {(N, M). ?R N M}"
wenzelm@10249
  1535
berghofe@23751
  1536
  assume "(N, M0 + {#a#}) \<in> {(N, M). ?R N M}"
wenzelm@18258
  1537
  then have "\<exists>a' M0' K.
nipkow@11464
  1538
      M0 + {#a#} = M0' + {#a'#} \<and> N = M0' + K \<and> ?r K a'" by simp
wenzelm@18258
  1539
  then show "?case1 \<or> ?case2"
wenzelm@10249
  1540
  proof (elim exE conjE)
wenzelm@10249
  1541
    fix a' M0' K
wenzelm@10249
  1542
    assume N: "N = M0' + K" and r: "?r K a'"
wenzelm@10249
  1543
    assume "M0 + {#a#} = M0' + {#a'#}"
wenzelm@18258
  1544
    then have "M0 = M0' \<and> a = a' \<or>
nipkow@11464
  1545
        (\<exists>K'. M0 = K' + {#a'#} \<and> M0' = K' + {#a#})"
wenzelm@10249
  1546
      by (simp only: add_eq_conv_ex)
wenzelm@18258
  1547
    then show ?thesis
wenzelm@10249
  1548
    proof (elim disjE conjE exE)
wenzelm@10249
  1549
      assume "M0 = M0'" "a = a'"
nipkow@11464
  1550
      with N r have "?r K a \<and> N = M0 + K" by simp
wenzelm@18258
  1551
      then have ?case2 .. then show ?thesis ..
wenzelm@10249
  1552
    next
wenzelm@10249
  1553
      fix K'
wenzelm@10249
  1554
      assume "M0' = K' + {#a#}"
haftmann@34943
  1555
      with N have n: "N = K' + K + {#a#}" by (simp add: add_ac)
wenzelm@10249
  1556
wenzelm@10249
  1557
      assume "M0 = K' + {#a'#}"
wenzelm@10249
  1558
      with r have "?R (K' + K) M0" by blast
wenzelm@18258
  1559
      with n have ?case1 by simp then show ?thesis ..
wenzelm@10249
  1560
    qed
wenzelm@10249
  1561
  qed
wenzelm@10249
  1562
qed
wenzelm@10249
  1563
haftmann@54295
  1564
lemma all_accessible: "wf r ==> \<forall>M. M \<in> Wellfounded.acc (mult1 r)"
wenzelm@10249
  1565
proof
wenzelm@10249
  1566
  let ?R = "mult1 r"
haftmann@54295
  1567
  let ?W = "Wellfounded.acc ?R"
wenzelm@10249
  1568
  {
wenzelm@10249
  1569
    fix M M0 a
berghofe@23751
  1570
    assume M0: "M0 \<in> ?W"
berghofe@23751
  1571
      and wf_hyp: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1572
      and acc_hyp: "\<forall>M. (M, M0) \<in> ?R --> M + {#a#} \<in> ?W"
berghofe@23751
  1573
    have "M0 + {#a#} \<in> ?W"
berghofe@23751
  1574
    proof (rule accI [of "M0 + {#a#}"])
wenzelm@10249
  1575
      fix N
berghofe@23751
  1576
      assume "(N, M0 + {#a#}) \<in> ?R"
berghofe@23751
  1577
      then have "((\<exists>M. (M, M0) \<in> ?R \<and> N = M + {#a#}) \<or>
berghofe@23751
  1578
          (\<exists>K. (\<forall>b. b :# K --> (b, a) \<in> r) \<and> N = M0 + K))"
wenzelm@10249
  1579
        by (rule less_add)
berghofe@23751
  1580
      then show "N \<in> ?W"
wenzelm@10249
  1581
      proof (elim exE disjE conjE)
berghofe@23751
  1582
        fix M assume "(M, M0) \<in> ?R" and N: "N = M + {#a#}"
berghofe@23751
  1583
        from acc_hyp have "(M, M0) \<in> ?R --> M + {#a#} \<in> ?W" ..
berghofe@23751
  1584
        from this and `(M, M0) \<in> ?R` have "M + {#a#} \<in> ?W" ..
berghofe@23751
  1585
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
  1586
      next
wenzelm@10249
  1587
        fix K
wenzelm@10249
  1588
        assume N: "N = M0 + K"
berghofe@23751
  1589
        assume "\<forall>b. b :# K --> (b, a) \<in> r"
berghofe@23751
  1590
        then have "M0 + K \<in> ?W"
wenzelm@10249
  1591
        proof (induct K)
wenzelm@18730
  1592
          case empty
berghofe@23751
  1593
          from M0 show "M0 + {#} \<in> ?W" by simp
wenzelm@18730
  1594
        next
wenzelm@18730
  1595
          case (add K x)
berghofe@23751
  1596
          from add.prems have "(x, a) \<in> r" by simp
berghofe@23751
  1597
          with wf_hyp have "\<forall>M \<in> ?W. M + {#x#} \<in> ?W" by blast
berghofe@23751
  1598
          moreover from add have "M0 + K \<in> ?W" by simp
berghofe@23751
  1599
          ultimately have "(M0 + K) + {#x#} \<in> ?W" ..
haftmann@34943
  1600
          then show "M0 + (K + {#x#}) \<in> ?W" by (simp only: add_assoc)
wenzelm@10249
  1601
        qed
berghofe@23751
  1602
        then show "N \<in> ?W" by (simp only: N)
wenzelm@10249
  1603
      qed
wenzelm@10249
  1604
    qed
wenzelm@10249
  1605
  } note tedious_reasoning = this
wenzelm@10249
  1606
berghofe@23751
  1607
  assume wf: "wf r"
wenzelm@10249
  1608
  fix M
berghofe@23751
  1609
  show "M \<in> ?W"
wenzelm@10249
  1610
  proof (induct M)
berghofe@23751
  1611
    show "{#} \<in> ?W"
wenzelm@10249
  1612
    proof (rule accI)
berghofe@23751
  1613
      fix b assume "(b, {#}) \<in> ?R"
berghofe@23751
  1614
      with not_less_empty show "b \<in> ?W" by contradiction
wenzelm@10249
  1615
    qed
wenzelm@10249
  1616
berghofe@23751
  1617
    fix M a assume "M \<in> ?W"
berghofe@23751
  1618
    from wf have "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1619
    proof induct
wenzelm@10249
  1620
      fix a
berghofe@23751
  1621
      assume r: "!!b. (b, a) \<in> r ==> (\<forall>M \<in> ?W. M + {#b#} \<in> ?W)"
berghofe@23751
  1622
      show "\<forall>M \<in> ?W. M + {#a#} \<in> ?W"
wenzelm@10249
  1623
      proof
berghofe@23751
  1624
        fix M assume "M \<in> ?W"
berghofe@23751
  1625
        then show "M + {#a#} \<in> ?W"
wenzelm@23373
  1626
          by (rule acc_induct) (rule tedious_reasoning [OF _ r])
wenzelm@10249
  1627
      qed
wenzelm@10249
  1628
    qed
berghofe@23751
  1629
    from this and `M \<in> ?W` show "M + {#a#} \<in> ?W" ..
wenzelm@10249
  1630
  qed
wenzelm@10249
  1631
qed
wenzelm@10249
  1632
berghofe@23751
  1633
theorem wf_mult1: "wf r ==> wf (mult1 r)"
nipkow@26178
  1634
by (rule acc_wfI) (rule all_accessible)
wenzelm@10249
  1635
berghofe@23751
  1636
theorem wf_mult: "wf r ==> wf (mult r)"
nipkow@26178
  1637
unfolding mult_def by (rule wf_trancl) (rule wf_mult1)
wenzelm@10249
  1638
wenzelm@10249
  1639
wenzelm@10249
  1640
subsubsection {* Closure-free presentation *}
wenzelm@10249
  1641
wenzelm@10249
  1642
text {* One direction. *}
wenzelm@10249
  1643
wenzelm@10249
  1644
lemma mult_implies_one_step:
berghofe@23751
  1645
  "trans r ==> (M, N) \<in> mult r ==>
nipkow@11464
  1646
    \<exists>I J K. N = I + J \<and> M = I + K \<and> J \<noteq> {#} \<and>
berghofe@23751
  1647
    (\<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r)"
nipkow@26178
  1648
apply (unfold mult_def mult1_def set_of_def)
nipkow@26178
  1649
apply (erule converse_trancl_induct, clarify)
nipkow@26178
  1650
 apply (rule_tac x = M0 in exI, simp, clarify)
nipkow@26178
  1651
apply (case_tac "a :# K")
nipkow@26178
  1652
 apply (rule_tac x = I in exI)
nipkow@26178
  1653
 apply (simp (no_asm))
nipkow@26178
  1654
 apply (rule_tac x = "(K - {#a#}) + Ka" in exI)
haftmann@34943
  1655
 apply (simp (no_asm_simp) add: add_assoc [symmetric])
thomas@57492
  1656
 apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="?S + ?T" in arg_cong)
nipkow@26178
  1657
 apply (simp add: diff_union_single_conv)
nipkow@26178
  1658
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1659
 apply blast
nipkow@26178
  1660
apply (subgoal_tac "a :# I")
nipkow@26178
  1661
 apply (rule_tac x = "I - {#a#}" in exI)
nipkow@26178
  1662
 apply (rule_tac x = "J + {#a#}" in exI)
nipkow@26178
  1663
 apply (rule_tac x = "K + Ka" in exI)
nipkow@26178
  1664
 apply (rule conjI)
nipkow@39302
  1665
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1666
 apply (rule conjI)
thomas@57492
  1667
  apply (drule_tac f = "\<lambda>M. M - {#a#}" and x="?S + ?T" in arg_cong, simp)
nipkow@39302
  1668
  apply (simp add: multiset_eq_iff split: nat_diff_split)
nipkow@26178
  1669
 apply (simp (no_asm_use) add: trans_def)
nipkow@26178
  1670
 apply blast
nipkow@26178
  1671
apply (subgoal_tac "a :# (M0 + {#a#})")
nipkow@26178
  1672
 apply simp
nipkow@26178
  1673
apply (simp (no_asm))
nipkow@26178
  1674
done
wenzelm@10249
  1675
wenzelm@10249
  1676
lemma one_step_implies_mult_aux:
berghofe@23751
  1677
  "trans r ==>
berghofe@23751
  1678
    \<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
  1679
      --> (I + K, I + J) \<in> mult r"
nipkow@26178
  1680
apply (induct_tac n, auto)
nipkow@26178
  1681
apply (frule size_eq_Suc_imp_eq_union, clarify)
nipkow@26178
  1682
apply (rename_tac "J'", simp)
nipkow@26178
  1683
apply (erule notE, auto)
nipkow@26178
  1684
apply (case_tac "J' = {#}")
nipkow@26178
  1685
 apply (simp add: mult_def)
nipkow@26178
  1686
 apply (rule r_into_trancl)
nipkow@26178
  1687
 apply (simp add: mult1_def set_of_def, blast)
nipkow@26178
  1688
txt {* Now we know @{term "J' \<noteq> {#}"}. *}
nipkow@26178
  1689
apply (cut_tac M = K and P = "\<lambda>x. (x, a) \<in> r" in multiset_partition)
nipkow@26178
  1690
apply (erule_tac P = "\<forall>k \<in> set_of K. ?P k" in rev_mp)
nipkow@26178
  1691
apply (erule ssubst)
nipkow@26178
  1692
apply (simp add: Ball_def, auto)
nipkow@26178
  1693
apply (subgoal_tac
nipkow@26178
  1694
  "((I + {# x :# K. (x, a) \<in> r #}) + {# x :# K. (x, a) \<notin> r #},
nipkow@26178
  1695
    (I + {# x :# K. (x, a) \<in> r #}) + J') \<in> mult r")
nipkow@26178
  1696
 prefer 2
nipkow@26178
  1697
 apply force
haftmann@34943
  1698
apply (simp (no_asm_use) add: add_assoc [symmetric] mult_def)
nipkow@26178
  1699
apply (erule trancl_trans)
nipkow@26178
  1700
apply (rule r_into_trancl)
nipkow@26178
  1701
apply (simp add: mult1_def set_of_def)
nipkow@26178
  1702
apply (rule_tac x = a in exI)
nipkow@26178
  1703
apply (rule_tac x = "I + J'" in exI)
haftmann@34943
  1704
apply (simp add: add_ac)
nipkow@26178
  1705
done
wenzelm@10249
  1706
wenzelm@17161
  1707
lemma one_step_implies_mult:
berghofe@23751
  1708
  "trans r ==> J \<noteq> {#} ==> \<forall>k \<in> set_of K. \<exists>j \<in> set_of J. (k, j) \<in> r
berghofe@23751
  1709
    ==> (I + K, I + J) \<in> mult r"
nipkow@26178
  1710
using one_step_implies_mult_aux by blast
wenzelm@10249
  1711
wenzelm@10249
  1712
wenzelm@10249
  1713
subsubsection {* Partial-order properties *}
wenzelm@10249
  1714
haftmann@35273
  1715
definition less_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<#" 50) where
haftmann@35273
  1716
  "M' <# M \<longleftrightarrow> (M', M) \<in> mult {(x', x). x' < x}"
wenzelm@10249
  1717
haftmann@35273
  1718
definition le_multiset :: "'a\<Colon>order multiset \<Rightarrow> 'a multiset \<Rightarrow> bool" (infix "<=#" 50) where
haftmann@35273
  1719
  "M' <=# M \<longleftrightarrow> M' <# M \<or> M' = M"
haftmann@35273
  1720
haftmann@35308
  1721
notation (xsymbols) less_multiset (infix "\<subset>#" 50)
haftmann@35308
  1722
notation (xsymbols) le_multiset (infix "\<subseteq>#" 50)
wenzelm@10249
  1723
haftmann@35268
  1724
interpretation multiset_order: order le_multiset less_multiset
haftmann@35268
  1725
proof -
haftmann@35268
  1726
  have irrefl: "\<And>M :: 'a multiset. \<not> M \<subset># M"
haftmann@35268
  1727
  proof
haftmann@35268
  1728
    fix M :: "'a multiset"
haftmann@35268
  1729
    assume "M \<subset># M"
haftmann@35268
  1730
    then have MM: "(M, M) \<in> mult {(x, y). x < y}" by (simp add: less_multiset_def)
haftmann@35268
  1731
    have "trans {(x'::'a, x). x' < x}"
haftmann@35268
  1732
      by (rule transI) simp
haftmann@35268
  1733
    moreover note MM
haftmann@35268
  1734
    ultimately have "\<exists>I J K. M = I + J \<and> M = I + K
haftmann@35268
  1735
      \<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
  1736
      by (rule mult_implies_one_step)
haftmann@35268
  1737
    then obtain I J K where "M = I + J" and "M = I + K"
haftmann@35268
  1738
      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
  1739
    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
  1740
    have "finite (set_of K)" by simp
haftmann@35268
  1741
    moreover note aux2
haftmann@35268
  1742
    ultimately have "set_of K = {}"
haftmann@35268
  1743
      by (induct rule: finite_induct) (auto intro: order_less_trans)
haftmann@35268
  1744
    with aux1 show False by simp
haftmann@35268
  1745
  qed
haftmann@35268
  1746
  have trans: "\<And>K M N :: 'a multiset. K \<subset># M \<Longrightarrow> M \<subset># N \<Longrightarrow> K \<subset># N"
haftmann@35268
  1747
    unfolding less_multiset_def mult_def by (blast intro: trancl_trans)
wenzelm@46921
  1748
  show "class.order (le_multiset :: 'a multiset \<Rightarrow> _) less_multiset"
wenzelm@46921
  1749
    by default (auto simp add: le_multiset_def irrefl dest: trans)
haftmann@35268
  1750
qed
wenzelm@10249
  1751
wenzelm@46730
  1752
lemma mult_less_irrefl [elim!]: "M \<subset># (M::'a::order multiset) ==> R"
wenzelm@46730
  1753
  by simp
haftmann@26567
  1754
wenzelm@10249
  1755
wenzelm@10249
  1756
subsubsection {* Monotonicity of multiset union *}
wenzelm@10249
  1757
wenzelm@46730
  1758
lemma mult1_union: "(B, D) \<in> mult1 r ==> (C + B, C + D) \<in> mult1 r"
nipkow@26178
  1759
apply (unfold mult1_def)
nipkow@26178
  1760
apply auto
nipkow@26178
  1761
apply (rule_tac x = a in exI)
nipkow@26178
  1762
apply (rule_tac x = "C + M0" in exI)
haftmann@34943
  1763
apply (simp add: add_assoc)
nipkow@26178
  1764
done
wenzelm@10249
  1765
haftmann@35268
  1766
lemma union_less_mono2: "B \<subset># D ==> C + B \<subset># C + (D::'a::order multiset)"
nipkow@26178
  1767
apply (unfold less_multiset_def mult_def)
nipkow@26178
  1768
apply (erule trancl_induct)
noschinl@40249
  1769
 apply (blast intro: mult1_union)
noschinl@40249
  1770
apply (blast intro: mult1_union trancl_trans)
nipkow@26178
  1771
done
wenzelm@10249
  1772
haftmann@35268
  1773
lemma union_less_mono1: "B \<subset># D ==> B + C \<subset># D + (C::'a::order multiset)"
haftmann@34943
  1774
apply (subst add_commute [of B C])
haftmann@34943
  1775
apply (subst add_commute [of D C])
nipkow@26178
  1776
apply (erule union_less_mono2)
nipkow@26178
  1777
done
wenzelm@10249
  1778
wenzelm@17161
  1779
lemma union_less_mono:
haftmann@35268
  1780
  "A \<subset># C ==> B \<subset># D ==> A + B \<subset># C + (D::'a::order multiset)"
haftmann@35268
  1781
  by (blast intro!: union_less_mono1 union_less_mono2 multiset_order.less_trans)
wenzelm@10249
  1782
haftmann@35268
  1783
interpretation multiset_order: ordered_ab_semigroup_add plus le_multiset less_multiset
haftmann@35268
  1784
proof
haftmann@35268
  1785
qed (auto simp add: le_multiset_def intro: union_less_mono2)
wenzelm@26145
  1786
paulson@15072
  1787
krauss@29125
  1788
subsection {* Termination proofs with multiset orders *}
krauss@29125
  1789
krauss@29125
  1790
lemma multi_member_skip: "x \<in># XS \<Longrightarrow> x \<in># {# y #} + XS"
krauss@29125
  1791
  and multi_member_this: "x \<in># {# x #} + XS"
krauss@29125
  1792
  and multi_member_last: "x \<in># {# x #}"
krauss@29125
  1793
  by auto
krauss@29125
  1794
krauss@29125
  1795
definition "ms_strict = mult pair_less"
haftmann@37765
  1796
definition "ms_weak = ms_strict \<union> Id"
krauss@29125
  1797
krauss@29125
  1798
lemma ms_reduction_pair: "reduction_pair (ms_strict, ms_weak)"
krauss@29125
  1799
unfolding reduction_pair_def ms_strict_def ms_weak_def pair_less_def
krauss@29125
  1800
by (auto intro: wf_mult1 wf_trancl simp: mult_def)
krauss@29125
  1801
krauss@29125
  1802
lemma smsI:
krauss@29125
  1803
  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z + B) \<in> ms_strict"
krauss@29125
  1804
  unfolding ms_strict_def
krauss@29125
  1805
by (rule one_step_implies_mult) (auto simp add: max_strict_def pair_less_def elim!:max_ext.cases)
krauss@29125
  1806
krauss@29125
  1807
lemma wmsI:
krauss@29125
  1808
  "(set_of A, set_of B) \<in> max_strict \<or> A = {#} \<and> B = {#}
krauss@29125
  1809
  \<Longrightarrow> (Z + A, Z + B) \<in> ms_weak"
krauss@29125
  1810
unfolding ms_weak_def ms_strict_def
krauss@29125
  1811
by (auto simp add: pair_less_def max_strict_def elim!:max_ext.cases intro: one_step_implies_mult)
krauss@29125
  1812
krauss@29125
  1813
inductive pw_leq
krauss@29125
  1814
where
krauss@29125
  1815
  pw_leq_empty: "pw_leq {#} {#}"
krauss@29125
  1816
| pw_leq_step:  "\<lbrakk>(x,y) \<in> pair_leq; pw_leq X Y \<rbrakk> \<Longrightarrow> pw_leq ({#x#} + X) ({#y#} + Y)"
krauss@29125
  1817
krauss@29125
  1818
lemma pw_leq_lstep:
krauss@29125
  1819
  "(x, y) \<in> pair_leq \<Longrightarrow> pw_leq {#x#} {#y#}"
krauss@29125
  1820
by (drule pw_leq_step) (rule pw_leq_empty, simp)
krauss@29125
  1821
krauss@29125
  1822
lemma pw_leq_split:
krauss@29125
  1823
  assumes "pw_leq X Y"
krauss@29125
  1824
  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
  1825
  using assms
krauss@29125
  1826
proof (induct)
krauss@29125
  1827
  case pw_leq_empty thus ?case by auto
krauss@29125
  1828
next
krauss@29125
  1829
  case (pw_leq_step x y X Y)
krauss@29125
  1830
  then obtain A B Z where
krauss@29125
  1831
    [simp]: "X = A + Z" "Y = B + Z" 
krauss@29125
  1832
      and 1[simp]: "(set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#})" 
krauss@29125
  1833
    by auto
krauss@29125
  1834
  from pw_leq_step have "x = y \<or> (x, y) \<in> pair_less" 
krauss@29125
  1835
    unfolding pair_leq_def by auto
krauss@29125
  1836
  thus ?case
krauss@29125
  1837
  proof
krauss@29125
  1838
    assume [simp]: "x = y"
krauss@29125
  1839
    have
krauss@29125
  1840
      "{#x#} + X = A + ({#y#}+Z) 
krauss@29125
  1841
      \<and> {#y#} + Y = B + ({#y#}+Z)
krauss@29125
  1842
      \<and> ((set_of A, set_of B) \<in> max_strict \<or> (B = {#} \<and> A = {#}))"
krauss@29125
  1843
      by (auto simp: add_ac)
krauss@29125
  1844
    thus ?case by (intro exI)
krauss@29125
  1845
  next
krauss@29125
  1846
    assume A: "(x, y) \<in> pair_less"
krauss@29125
  1847
    let ?A' = "{#x#} + A" and ?B' = "{#y#} + B"
krauss@29125
  1848
    have "{#x#} + X = ?A' + Z"
krauss@29125
  1849
      "{#y#} + Y = ?B' + Z"
krauss@29125
  1850
      by (auto simp add: add_ac)
krauss@29125
  1851
    moreover have 
krauss@29125
  1852
      "(set_of ?A', set_of ?B') \<in> max_strict"
krauss@29125
  1853
      using 1 A unfolding max_strict_def 
krauss@29125
  1854
      by (auto elim!: max_ext.cases)
krauss@29125
  1855
    ultimately show ?thesis by blast
krauss@29125
  1856
  qed
krauss@29125
  1857
qed
krauss@29125
  1858
krauss@29125
  1859
lemma 
krauss@29125
  1860
  assumes pwleq: "pw_leq Z Z'"
krauss@29125
  1861
  shows ms_strictI: "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_strict"
krauss@29125
  1862
  and   ms_weakI1:  "(set_of A, set_of B) \<in> max_strict \<Longrightarrow> (Z + A, Z' + B) \<in> ms_weak"
krauss@29125
  1863
  and   ms_weakI2:  "(Z + {#}, Z' + {#}) \<in> ms_weak"
krauss@29125
  1864
proof -
krauss@29125
  1865
  from pw_leq_split[OF pwleq] 
krauss@29125
  1866
  obtain A' B' Z''
krauss@29125
  1867
    where [simp]: "Z = A' + Z''" "Z' = B' + Z''"
krauss@29125
  1868
    and mx_or_empty: "(set_of A', set_of B') \<in> max_strict \<or> (A' = {#} \<and> B' = {#})"
krauss@29125
  1869
    by blast
krauss@29125
  1870
  {
krauss@29125
  1871
    assume max: "(set_of A, set_of B) \<in> max_strict"
krauss@29125
  1872
    from mx_or_empty
krauss@29125
  1873
    have "(Z'' + (A + A'), Z'' + (B + B')) \<in> ms_strict"
krauss@29125
  1874
    proof
krauss@29125
  1875
      assume max': "(set_of A', set_of B') \<in> max_strict"
krauss@29125
  1876
      with max have "(set_of (A + A'), set_of (B + B')) \<in> max_strict"
krauss@29125
  1877
        by (auto simp: max_strict_def intro: max_ext_additive)
krauss@29125
  1878
      thus ?thesis by (rule smsI) 
krauss@29125
  1879
    next
krauss@29125
  1880
      assume [simp]: "A' = {#} \<and> B' = {#}"
krauss@29125
  1881
      show ?thesis by (rule smsI) (auto intro: max)
krauss@29125
  1882
    qed
krauss@29125
  1883
    thus "(Z + A, Z' + B) \<in> ms_strict" by (simp add:add_ac)
krauss@29125
  1884
    thus "(Z + A, Z' + B) \<in> ms_weak" by (simp add: ms_weak_def)
krauss@29125
  1885
  }
krauss@29125
  1886
  from mx_or_empty
krauss@29125
  1887
  have "(Z'' + A', Z'' + B') \<in> ms_weak" by (rule wmsI)
krauss@29125
  1888
  thus "(Z + {#}, Z' + {#}) \<in> ms_weak" by (simp add:add_ac)
krauss@29125
  1889
qed
krauss@29125
  1890
nipkow@39301
  1891
lemma empty_neutral: "{#} + x = x" "x + {#} = x"
krauss@29125
  1892
and nonempty_plus: "{# x #} + rs \<noteq> {#}"
krauss@29125
  1893
and nonempty_single: "{# x #} \<noteq> {#}"
krauss@29125
  1894
by auto
krauss@29125
  1895
krauss@29125
  1896
setup {*
krauss@29125
  1897
let
wenzelm@35402
  1898
  fun msetT T = Type (@{type_name multiset}, [T]);
krauss@29125
  1899
wenzelm@35402
  1900
  fun mk_mset T [] = Const (@{const_abbrev Mempty}, msetT T)
krauss@29125
  1901
    | mk_mset T [x] = Const (@{const_name single}, T --> msetT T) $ x
krauss@29125
  1902
    | mk_mset T (x :: xs) =
krauss@29125
  1903
          Const (@{const_name plus}, msetT T --> msetT T --> msetT T) $
krauss@29125
  1904
                mk_mset T [x] $ mk_mset T xs
krauss@29125
  1905
krauss@29125
  1906
  fun mset_member_tac m i =
krauss@29125
  1907
      (if m <= 0 then
krauss@29125
  1908
           rtac @{thm multi_member_this} i ORELSE rtac @{thm multi_member_last} i
krauss@29125
  1909
       else
krauss@29125
  1910
           rtac @{thm multi_member_skip} i THEN mset_member_tac (m - 1) i)
krauss@29125
  1911
krauss@29125
  1912
  val mset_nonempty_tac =
krauss@29125
  1913
      rtac @{thm nonempty_plus} ORELSE' rtac @{thm nonempty_single}
krauss@29125
  1914
krauss@29125
  1915
  val regroup_munion_conv =
wenzelm@35402
  1916
      Function_Lib.regroup_conv @{const_abbrev Mempty} @{const_name plus}
nipkow@39301
  1917
        (map (fn t => t RS eq_reflection) (@{thms add_ac} @ @{thms empty_neutral}))
krauss@29125
  1918
krauss@29125
  1919
  fun unfold_pwleq_tac i =
krauss@29125
  1920
    (rtac @{thm pw_leq_step} i THEN (fn st => unfold_pwleq_tac (i + 1) st))
krauss@29125
  1921
      ORELSE (rtac @{thm pw_leq_lstep} i)
krauss@29125
  1922
      ORELSE (rtac @{thm pw_leq_empty} i)
krauss@29125
  1923
krauss@29125
  1924
  val set_of_simps = [@{thm set_of_empty}, @{thm set_of_single}, @{thm set_of_union},
krauss@29125
  1925
                      @{thm Un_insert_left}, @{thm Un_empty_left}]
krauss@29125
  1926
in
krauss@29125
  1927
  ScnpReconstruct.multiset_setup (ScnpReconstruct.Multiset 
krauss@29125
  1928
  {
krauss@29125
  1929
    msetT=msetT, mk_mset=mk_mset, mset_regroup_conv=regroup_munion_conv,
krauss@29125
  1930
    mset_member_tac=mset_member_tac, mset_nonempty_tac=mset_nonempty_tac,
krauss@29125
  1931
    mset_pwleq_tac=unfold_pwleq_tac, set_of_simps=set_of_simps,
wenzelm@30595
  1932
    smsI'= @{thm ms_strictI}, wmsI2''= @{thm ms_weakI2}, wmsI1= @{thm ms_weakI1},
wenzelm@30595
  1933
    reduction_pair= @{thm ms_reduction_pair}
krauss@29125
  1934
  })
wenzelm@10249
  1935
end
krauss@29125
  1936
*}
krauss@29125
  1937
haftmann@34943
  1938
haftmann@34943
  1939
subsection {* Legacy theorem bindings *}
haftmann@34943
  1940
nipkow@39302
  1941
lemmas multi_count_eq = multiset_eq_iff [symmetric]
haftmann@34943
  1942
haftmann@34943
  1943
lemma union_commute: "M + N = N + (M::'a multiset)"
haftmann@34943
  1944
  by (fact add_commute)
haftmann@34943
  1945
haftmann@34943
  1946
lemma union_assoc: "(M + N) + K = M + (N + (K::'a multiset))"
haftmann@34943
  1947
  by (fact add_assoc)
haftmann@34943
  1948
haftmann@34943
  1949
lemma union_lcomm: "M + (N + K) = N + (M + (K::'a multiset))"
haftmann@34943
  1950
  by (fact add_left_commute)
haftmann@34943
  1951
haftmann@34943
  1952
lemmas union_ac = union_assoc union_commute union_lcomm
haftmann@34943
  1953
haftmann@34943
  1954
lemma union_right_cancel: "M + K = N + K \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1955
  by (fact add_right_cancel)
haftmann@34943
  1956
haftmann@34943
  1957
lemma union_left_cancel: "K + M = K + N \<longleftrightarrow> M = (N::'a multiset)"
haftmann@34943
  1958
  by (fact add_left_cancel)
haftmann@34943
  1959
haftmann@34943
  1960
lemma multi_union_self_other_eq: "(A::'a multiset) + X = A + Y \<Longrightarrow> X = Y"
haftmann@34943
  1961
  by (fact add_imp_eq)
haftmann@34943
  1962
haftmann@35268
  1963
lemma mset_less_trans: "(M::'a multiset) < K \<Longrightarrow> K < N \<Longrightarrow> M < N"
haftmann@35268
  1964
  by (fact order_less_trans)
haftmann@35268
  1965
haftmann@35268
  1966
lemma multiset_inter_commute: "A #\<inter> B = B #\<inter> A"
haftmann@35268
  1967
  by (fact inf.commute)
haftmann@35268
  1968
haftmann@35268
  1969
lemma multiset_inter_assoc: "A #\<inter> (B #\<inter> C) = A #\<inter> B #\<inter> C"
haftmann@35268
  1970
  by (fact inf.assoc [symmetric])
haftmann@35268
  1971
haftmann@35268
  1972
lemma multiset_inter_left_commute: "A #\<inter> (B #\<inter> C) = B #\<inter> (A #\<inter> C)"
haftmann@35268
  1973
  by (fact inf.left_commute)
haftmann@35268
  1974
haftmann@35268
  1975
lemmas multiset_inter_ac =
haftmann@35268
  1976
  multiset_inter_commute
haftmann@35268
  1977
  multiset_inter_assoc
haftmann@35268
  1978
  multiset_inter_left_commute
haftmann@35268
  1979
haftmann@35268
  1980
lemma mult_less_not_refl:
haftmann@35268
  1981
  "\<not> M \<subset># (M::'a::order multiset)"
haftmann@35268
  1982
  by (fact multiset_order.less_irrefl)
haftmann@35268
  1983
haftmann@35268
  1984
lemma mult_less_trans:
haftmann@35268
  1985
  "K \<subset># M ==> M \<subset># N ==> K \<subset># (N::'a::order multiset)"
haftmann@35268
  1986
  by (fact multiset_order.less_trans)
haftmann@35268
  1987
    
haftmann@35268
  1988
lemma mult_less_not_sym:
haftmann@35268
  1989
  "M \<subset># N ==> \<not> N \<subset># (M::'a::order multiset)"
haftmann@35268
  1990
  by (fact multiset_order.less_not_sym)
haftmann@35268
  1991
haftmann@35268
  1992
lemma mult_less_asym:
haftmann@35268
  1993
  "M \<subset># N ==> (\<not> P ==> N \<subset># (M::'a::order multiset)) ==> P"
haftmann@35268
  1994
  by (fact multiset_order.less_asym)
haftmann@34943
  1995
blanchet@35712
  1996
ML {*
blanchet@35712
  1997
fun multiset_postproc _ maybe_name all_values (T as Type (_, [elem_T]))
blanchet@35712
  1998
                      (Const _ $ t') =
blanchet@35712
  1999
    let
blanchet@35712
  2000
      val (maybe_opt, ps) =
blanchet@35712
  2001
        Nitpick_Model.dest_plain_fun t' ||> op ~~
blanchet@35712
  2002
        ||> map (apsnd (snd o HOLogic.dest_number))
blanchet@35712
  2003
      fun elems_for t =
blanchet@35712
  2004
        case AList.lookup (op =) ps t of
blanchet@35712
  2005
          SOME n => replicate n t
blanchet@35712
  2006
        | NONE => [Const (maybe_name, elem_T --> elem_T) $ t]
blanchet@35712
  2007
    in
blanchet@35712
  2008
      case maps elems_for (all_values elem_T) @
blanchet@37261
  2009
           (if maybe_opt then [Const (Nitpick_Model.unrep (), elem_T)]
blanchet@37261
  2010
            else []) of
blanchet@35712
  2011
        [] => Const (@{const_name zero_class.zero}, T)
blanchet@35712
  2012
      | ts => foldl1 (fn (t1, t2) =>
blanchet@35712
  2013
                         Const (@{const_name plus_class.plus}, T --> T --> T)
blanchet@35712
  2014
                         $ t1 $ t2)
blanchet@35712
  2015
                     (map (curry (op $) (Const (@{const_name single},
blanchet@35712
  2016
                                                elem_T --> T))) ts)
blanchet@35712
  2017
    end
blanchet@35712
  2018
  | multiset_postproc _ _ _ _ t = t
blanchet@35712
  2019
*}
blanchet@35712
  2020
blanchet@38287
  2021
declaration {*
blanchet@38287
  2022
Nitpick_Model.register_term_postprocessor @{typ "'a multiset"}
blanchet@38242
  2023
    multiset_postproc
blanchet@35712
  2024
*}
blanchet@35712
  2025
haftmann@49822
  2026
hide_const (open) fold
haftmann@49822
  2027
haftmann@51600
  2028
haftmann@51600
  2029
subsection {* Naive implementation using lists *}
haftmann@51600
  2030
haftmann@51600
  2031
code_datatype multiset_of
haftmann@51600
  2032
haftmann@51600
  2033
lemma [code]:
haftmann@51600
  2034
  "{#} = multiset_of []"
haftmann@51600
  2035
  by simp
haftmann@51600
  2036
haftmann@51600
  2037
lemma [code]:
haftmann@51600
  2038
  "{#x#} = multiset_of [x]"
haftmann@51600
  2039
  by simp
haftmann@51600
  2040
haftmann@51600
  2041
lemma union_code [code]:
haftmann@51600
  2042
  "multiset_of xs + multiset_of ys = multiset_of (xs @ ys)"
haftmann@51600
  2043
  by simp
haftmann@51600
  2044
haftmann@51600
  2045
lemma [code]:
haftmann@51600
  2046
  "image_mset f (multiset_of xs) = multiset_of (map f xs)"
haftmann@51600
  2047
  by (simp add: multiset_of_map)
haftmann@51600
  2048
haftmann@51600
  2049
lemma [code]:
haftmann@51600
  2050
  "Multiset.filter f (multiset_of xs) = multiset_of (filter f xs)"
haftmann@51600
  2051
  by (simp add: multiset_of_filter)
haftmann@51600
  2052
haftmann@51600
  2053
lemma [code]:
haftmann@51600
  2054
  "multiset_of xs - multiset_of ys = multiset_of (fold remove1 ys xs)"
haftmann@51600
  2055
  by (rule sym, induct ys arbitrary: xs) (simp_all add: diff_add diff_right_commute)
haftmann@51600
  2056
haftmann@51600
  2057
lemma [code]:
haftmann@51600
  2058
  "multiset_of xs #\<inter> multiset_of ys =
haftmann@51600
  2059
    multiset_of (snd (fold (\<lambda>x (ys, zs).
haftmann@51600
  2060
      if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, [])))"
haftmann@51600
  2061
proof -
haftmann@51600
  2062
  have "\<And>zs. multiset_of (snd (fold (\<lambda>x (ys, zs).
haftmann@51600
  2063
    if x \<in> set ys then (remove1 x ys, x # zs) else (ys, zs)) xs (ys, zs))) =
haftmann@51600
  2064
      (multiset_of xs #\<inter> multiset_of ys) + multiset_of zs"
haftmann@51623
  2065
    by (induct xs arbitrary: ys)
haftmann@51623
  2066
      (auto simp add: mem_set_multiset_eq inter_add_right1 inter_add_right2 ac_simps)
haftmann@51623
  2067
  then show ?thesis by simp
haftmann@51623
  2068
qed
haftmann@51623
  2069
haftmann@51623
  2070
lemma [code]:
haftmann@51623
  2071
  "multiset_of xs #\<union> multiset_of ys =
haftmann@51623
  2072
    multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, [])))"
haftmann@51623
  2073
proof -
haftmann@51623
  2074
  have "\<And>zs. multiset_of (split append (fold (\<lambda>x (ys, zs). (remove1 x ys, x # zs)) xs (ys, zs))) =
haftmann@51623
  2075
      (multiset_of xs #\<union> multiset_of ys) + multiset_of zs"
haftmann@51623
  2076
    by (induct xs arbitrary: ys) (simp_all add: multiset_eq_iff)
haftmann@51600
  2077
  then show ?thesis by simp
haftmann@51600
  2078
qed
haftmann@51600
  2079
haftmann@51600
  2080
lemma [code_unfold]:
haftmann@51600
  2081
  "x \<in># multiset_of xs \<longleftrightarrow> x \<in> set xs"
haftmann@51600
  2082
  by (simp add: in_multiset_of)
haftmann@51600
  2083
haftmann@51600
  2084
lemma [code]:
haftmann@51600
  2085
  "count (multiset_of xs) x = fold (\<lambda>y. if x = y then Suc else id) xs 0"
haftmann@51600
  2086
proof -
haftmann@51600
  2087
  have "\<And>n. fold (\<lambda>y. if x = y then Suc else id) xs n = count (multiset_of xs) x + n"
haftmann@51600
  2088
    by (induct xs) simp_all
haftmann@51600
  2089
  then show ?thesis by simp
haftmann@51600
  2090
qed
haftmann@51600
  2091
haftmann@51600
  2092
lemma [code]:
haftmann@51600
  2093
  "set_of (multiset_of xs) = set xs"
haftmann@51600
  2094
  by simp
haftmann@51600
  2095
haftmann@51600
  2096
lemma [code]:
haftmann@51600
  2097
  "sorted_list_of_multiset (multiset_of xs) = sort xs"
haftmann@51600
  2098
  by (induct xs) simp_all
haftmann@51600
  2099
haftmann@51600
  2100
lemma [code]: -- {* not very efficient, but representation-ignorant! *}
haftmann@51600
  2101
  "multiset_of_set A = multiset_of (sorted_list_of_set A)"
haftmann@51600
  2102
  apply (cases "finite A")
haftmann@51600
  2103
  apply simp_all
haftmann@51600
  2104
  apply (induct A rule: finite_induct)
haftmann@51600
  2105
  apply (simp_all add: union_commute)
haftmann@51600
  2106
  done
haftmann@51600
  2107
haftmann@51600
  2108
lemma [code]:
haftmann@51600
  2109
  "mcard (multiset_of xs) = length xs"
haftmann@51600
  2110
  by (simp add: mcard_multiset_of)
haftmann@51600
  2111
nipkow@55808
  2112
fun ms_lesseq_impl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool option" where 
nipkow@55808
  2113
  "ms_lesseq_impl [] ys = Some (ys \<noteq> [])"
nipkow@55808
  2114
| "ms_lesseq_impl (Cons x xs) ys = (case List.extract (op = x) ys of 
nipkow@55808
  2115
     None \<Rightarrow> None
nipkow@55808
  2116
   | Some (ys1,_,ys2) \<Rightarrow> ms_lesseq_impl xs (ys1 @ ys2))"
nipkow@55808
  2117
nipkow@55808
  2118
lemma ms_lesseq_impl: "(ms_lesseq_impl xs ys = None \<longleftrightarrow> \<not> multiset_of xs \<le> multiset_of ys) \<and>
nipkow@55808
  2119
  (ms_lesseq_impl xs ys = Some True \<longleftrightarrow> multiset_of xs < multiset_of ys) \<and>
nipkow@55808
  2120
  (ms_lesseq_impl xs ys = Some False \<longrightarrow> multiset_of xs = multiset_of ys)"
nipkow@55808
  2121
proof (induct xs arbitrary: ys)
nipkow@55808
  2122
  case (Nil ys)
nipkow@55808
  2123
  show ?case by (auto simp: mset_less_empty_nonempty)
nipkow@55808
  2124
next
nipkow@55808
  2125
  case (Cons x xs ys)
nipkow@55808
  2126
  show ?case
nipkow@55808
  2127
  proof (cases "List.extract (op = x) ys")
nipkow@55808
  2128
    case None
nipkow@55808
  2129
    hence x: "x \<notin> set ys" by (simp add: extract_None_iff)
nipkow@55808
  2130
    {
nipkow@55808
  2131
      assume "multiset_of (x # xs) \<le> multiset_of ys"
nipkow@55808
  2132
      from set_of_mono[OF this] x have False by simp
nipkow@55808
  2133
    } note nle = this
nipkow@55808
  2134
    moreover
nipkow@55808
  2135
    {
nipkow@55808
  2136
      assume "multiset_of (x # xs) < multiset_of ys"
nipkow@55808
  2137
      hence "multiset_of (x # xs) \<le> multiset_of ys" by auto
nipkow@55808
  2138
      from nle[OF this] have False .
nipkow@55808
  2139
    }
nipkow@55808
  2140
    ultimately show ?thesis using None by auto
nipkow@55808
  2141
  next
nipkow@55808
  2142
    case (Some res)
nipkow@55808
  2143
    obtain ys1 y ys2 where res: "res = (ys1,y,ys2)" by (cases res, auto)
nipkow@55808
  2144
    note Some = Some[unfolded res]
nipkow@55808
  2145
    from extract_SomeE[OF Some] have "ys = ys1 @ x # ys2" by simp
nipkow@55808
  2146
    hence id: "multiset_of ys = multiset_of (ys1 @ ys2) + {#x#}" 
nipkow@55808
  2147
      by (auto simp: ac_simps)
nipkow@55808
  2148
    show ?thesis unfolding ms_lesseq_impl.simps
nipkow@55808
  2149
      unfolding Some option.simps split
nipkow@55808
  2150
      unfolding id
nipkow@55808
  2151
      using Cons[of "ys1 @ ys2"]
nipkow@55808
  2152
      unfolding mset_le_def mset_less_def by auto
nipkow@55808
  2153
  qed
nipkow@55808
  2154
qed
nipkow@55808
  2155
nipkow@55808
  2156
lemma [code]: "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> ms_lesseq_impl xs ys \<noteq> None"
nipkow@55808
  2157
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
nipkow@55808
  2158
nipkow@55808
  2159
lemma [code]: "multiset_of xs < multiset_of ys \<longleftrightarrow> ms_lesseq_impl xs ys = Some True"
nipkow@55808
  2160
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
haftmann@51600
  2161
haftmann@51600
  2162
instantiation multiset :: (equal) equal
haftmann@51600
  2163
begin
haftmann@51600
  2164
haftmann@51600
  2165
definition
nipkow@55808
  2166
  [code del]: "HOL.equal A (B :: 'a multiset) \<longleftrightarrow> A = B"
nipkow@55808
  2167
lemma [code]: "HOL.equal (multiset_of xs) (multiset_of ys) \<longleftrightarrow> ms_lesseq_impl xs ys = Some False"
nipkow@55808
  2168
  unfolding equal_multiset_def
nipkow@55808
  2169
  using ms_lesseq_impl[of xs ys] by (cases "ms_lesseq_impl xs ys", auto)
haftmann@51600
  2170
haftmann@51600
  2171
instance
nipkow@55808
  2172
  by default (simp add: equal_multiset_def)
blanchet@37169
  2173
end
haftmann@49388
  2174
haftmann@51600
  2175
lemma [code]:
haftmann@51600
  2176
  "msetsum (multiset_of xs) = listsum xs"
haftmann@51600
  2177
  by (induct xs) (simp_all add: add.commute)
haftmann@51600
  2178
haftmann@51600
  2179
lemma [code]:
haftmann@51600
  2180
  "msetprod (multiset_of xs) = fold times xs 1"
haftmann@51600
  2181
proof -
haftmann@51600
  2182
  have "\<And>x. fold times xs x = msetprod (multiset_of xs) * x"
haftmann@51600
  2183
    by (induct xs) (simp_all add: mult.assoc)
haftmann@51600
  2184
  then show ?thesis by simp
haftmann@51600
  2185
qed
haftmann@51600
  2186
haftmann@51600
  2187
lemma [code]:
haftmann@51600
  2188
  "size = mcard"
haftmann@51600
  2189
  by (fact size_eq_mcard)
haftmann@51600
  2190
haftmann@51600
  2191
text {*
haftmann@51600
  2192
  Exercise for the casual reader: add implementations for @{const le_multiset}
haftmann@51600
  2193
  and @{const less_multiset} (multiset order).
haftmann@51600
  2194
*}
haftmann@51600
  2195
haftmann@51600
  2196
text {* Quickcheck generators *}
haftmann@51600
  2197
haftmann@51600
  2198
definition (in term_syntax)
haftmann@51600
  2199
  msetify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
haftmann@51600
  2200
    \<Rightarrow> 'a multiset \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
haftmann@51600
  2201
  [code_unfold]: "msetify xs = Code_Evaluation.valtermify multiset_of {\<cdot>} xs"
haftmann@51600
  2202
haftmann@51600
  2203
notation fcomp (infixl "\<circ>>" 60)
haftmann@51600
  2204
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@51600
  2205
haftmann@51600
  2206
instantiation multiset :: (random) random
haftmann@51600
  2207
begin
haftmann@51600
  2208
haftmann@51600
  2209
definition
haftmann@51600
  2210
  "Quickcheck_Random.random i = Quickcheck_Random.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (msetify xs))"
haftmann@51600
  2211
haftmann@51600
  2212
instance ..
haftmann@51600
  2213
haftmann@51600
  2214
end
haftmann@51600
  2215
haftmann@51600
  2216
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@51600
  2217
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@51600
  2218
haftmann@51600
  2219
instantiation multiset :: (full_exhaustive) full_exhaustive
haftmann@51600
  2220
begin
haftmann@51600
  2221
haftmann@51600
  2222
definition full_exhaustive_multiset :: "('a multiset \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
haftmann@51600
  2223
where
haftmann@51600
  2224
  "full_exhaustive_multiset f i = Quickcheck_Exhaustive.full_exhaustive (\<lambda>xs. f (msetify xs)) i"
haftmann@51600
  2225
haftmann@51600
  2226
instance ..
haftmann@51600
  2227
haftmann@51600
  2228
end
haftmann@51600
  2229
haftmann@51600
  2230
hide_const (open) msetify
haftmann@51600
  2231
blanchet@55129
  2232
blanchet@55129
  2233
subsection {* BNF setup *}
blanchet@55129
  2234
blanchet@55129
  2235
lemma setsum_gt_0_iff:
blanchet@55129
  2236
fixes f :: "'a \<Rightarrow> nat" assumes "finite A"
blanchet@55129
  2237
shows "setsum f A > 0 \<longleftrightarrow> (\<exists> a \<in> A. f a > 0)"
blanchet@55129
  2238
(is "?L \<longleftrightarrow> ?R")
blanchet@55129
  2239
proof-
blanchet@55129
  2240
  have "?L \<longleftrightarrow> \<not> setsum f A = 0" by fast
blanchet@55129
  2241
  also have "... \<longleftrightarrow> (\<exists> a \<in> A. f a \<noteq> 0)" using assms by simp
blanchet@55129
  2242
  also have "... \<longleftrightarrow> ?R" by simp
blanchet@55129
  2243
  finally show ?thesis .
blanchet@55129
  2244
qed
blanchet@55129
  2245
blanchet@55129
  2246
lift_definition mmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" is
blanchet@55129
  2247
  "\<lambda>h f b. setsum f {a. h a = b \<and> f a > 0} :: nat"
blanchet@55129
  2248
unfolding multiset_def proof safe
blanchet@55129
  2249
  fix h :: "'a \<Rightarrow> 'b" and f :: "'a \<Rightarrow> nat"
blanchet@55129
  2250
  assume fin: "finite {a. 0 < f a}"  (is "finite ?A")
blanchet@55129
  2251
  show "finite {b. 0 < setsum f {a. h a = b \<and> 0 < f a}}"
blanchet@55129
  2252
  (is "finite {b. 0 < setsum f (?As b)}")
blanchet@55129
  2253
  proof- let ?B = "{b. 0 < setsum f (?As b)}"
blanchet@55129
  2254
    have "\<And> b. finite (?As b)" using fin by simp
blanchet@55129
  2255
    hence B: "?B = {b. ?As b \<noteq> {}}" by (auto simp add: setsum_gt_0_iff)
blanchet@55129
  2256
    hence "?B \<subseteq> h ` ?A" by auto
blanchet@55129
  2257
    thus ?thesis using finite_surj[OF fin] by auto
blanchet@55129
  2258
  qed
blanchet@55129
  2259
qed
blanchet@55129
  2260
blanchet@55129
  2261
lemma mmap_id0: "mmap id = id"
blanchet@55129
  2262
proof (intro ext multiset_eqI)
blanchet@55129
  2263
  fix f a show "count (mmap id f) a = count (id f) a"
blanchet@55129
  2264
  proof (cases "count f a = 0")
blanchet@55129
  2265
    case False
blanchet@55129
  2266
    hence 1: "{aa. aa = a \<and> aa \<in># f} = {a}" by auto
blanchet@55129
  2267
    thus ?thesis by transfer auto
blanchet@55129
  2268
  qed (transfer, simp)
blanchet@55129
  2269
qed
blanchet@55129
  2270
blanchet@55129
  2271
lemma inj_on_setsum_inv:
blanchet@55129
  2272
assumes 1: "(0::nat) < setsum (count f) {a. h a = b' \<and> a \<in># f}" (is "0 < setsum (count f) ?A'")
blanchet@55129
  2273
and     2: "{a. h a = b \<and> a \<in># f} = {a. h a = b' \<and> a \<in># f}" (is "?A = ?A'")
blanchet@55129
  2274
shows "b = b'"
blanchet@55129
  2275
using assms by (auto simp add: setsum_gt_0_iff)
blanchet@55129
  2276
blanchet@55129
  2277
lemma mmap_comp:
blanchet@55129
  2278
fixes h1 :: "'a \<Rightarrow> 'b" and h2 :: "'b \<Rightarrow> 'c&quo