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